2 * Copyright (c) 2015-2017 ARM Limited. All rights reserved.
4 * SPDX-License-Identifier: Apache-2.0
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * Project: CAN (Controller Area Network) Driver definitions
26 * ARM_CAN_STATUS made volatile
39 #include "Driver_Common.h"
41 #define ARM_CAN_API_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(1,1) /* API version */
44 /****** CAN Bitrate selection codes *****/
45 typedef enum _ARM_CAN_BITRATE_SELECT {
46 ARM_CAN_BITRATE_NOMINAL, ///< Select nominal (flexible data-rate arbitration) bitrate
47 ARM_CAN_BITRATE_FD_DATA ///< Select flexible data-rate data bitrate
48 } ARM_CAN_BITRATE_SELECT;
50 /****** CAN Bit Propagation Segment codes (PROP_SEG) *****/
51 #define ARM_CAN_BIT_PROP_SEG_Pos 0UL ///< bits 7..0
52 #define ARM_CAN_BIT_PROP_SEG_Msk (0xFFUL << ARM_CAN_BIT_PROP_SEG_Pos)
53 #define ARM_CAN_BIT_PROP_SEG(x) (((x) << ARM_CAN_BIT_PROP_SEG_Pos) & ARM_CAN_BIT_PROP_SEG_Msk)
55 /****** CAN Bit Phase Buffer Segment 1 (PHASE_SEG1) codes *****/
56 #define ARM_CAN_BIT_PHASE_SEG1_Pos 8UL ///< bits 15..8
57 #define ARM_CAN_BIT_PHASE_SEG1_Msk (0xFFUL << ARM_CAN_BIT_PHASE_SEG1_Pos)
58 #define ARM_CAN_BIT_PHASE_SEG1(x) (((x) << ARM_CAN_BIT_PHASE_SEG1_Pos) & ARM_CAN_BIT_PHASE_SEG1_Msk)
60 /****** CAN Bit Phase Buffer Segment 2 (PHASE_SEG2) codes *****/
61 #define ARM_CAN_BIT_PHASE_SEG2_Pos 16UL ///< bits 23..16
62 #define ARM_CAN_BIT_PHASE_SEG2_Msk (0xFFUL << ARM_CAN_BIT_PHASE_SEG2_Pos)
63 #define ARM_CAN_BIT_PHASE_SEG2(x) (((x) << ARM_CAN_BIT_PHASE_SEG2_Pos) & ARM_CAN_BIT_PHASE_SEG2_Msk)
65 /****** CAN Bit (Re)Synchronization Jump Width Segment (SJW) *****/
66 #define ARM_CAN_BIT_SJW_Pos 24UL ///< bits 28..24
67 #define ARM_CAN_BIT_SJW_Msk (0x1FUL << ARM_CAN_BIT_SJW_Pos)
68 #define ARM_CAN_BIT_SJW(x) (((x) << ARM_CAN_BIT_SJW_Pos) & ARM_CAN_BIT_SJW_Msk)
70 /****** CAN Mode codes *****/
71 typedef enum _ARM_CAN_MODE {
72 ARM_CAN_MODE_INITIALIZATION, ///< Initialization mode
73 ARM_CAN_MODE_NORMAL, ///< Normal operation mode
74 ARM_CAN_MODE_RESTRICTED, ///< Restricted operation mode
75 ARM_CAN_MODE_MONITOR, ///< Bus monitoring mode
76 ARM_CAN_MODE_LOOPBACK_INTERNAL, ///< Loopback internal mode
77 ARM_CAN_MODE_LOOPBACK_EXTERNAL ///< Loopback external mode
80 /****** CAN Filter Operation codes *****/
81 typedef enum _ARM_CAN_FILTER_OPERATION {
82 ARM_CAN_FILTER_ID_EXACT_ADD, ///< Add exact id filter
83 ARM_CAN_FILTER_ID_EXACT_REMOVE, ///< Remove exact id filter
84 ARM_CAN_FILTER_ID_RANGE_ADD, ///< Add range id filter
85 ARM_CAN_FILTER_ID_RANGE_REMOVE, ///< Remove range id filter
86 ARM_CAN_FILTER_ID_MASKABLE_ADD, ///< Add maskable id filter
87 ARM_CAN_FILTER_ID_MASKABLE_REMOVE ///< Remove maskable id filter
88 } ARM_CAN_FILTER_OPERATION;
90 /****** CAN Object Configuration codes *****/
91 typedef enum _ARM_CAN_OBJ_CONFIG {
92 ARM_CAN_OBJ_INACTIVE, ///< CAN object inactive
93 ARM_CAN_OBJ_TX, ///< CAN transmit object
94 ARM_CAN_OBJ_RX, ///< CAN receive object
95 ARM_CAN_OBJ_RX_RTR_TX_DATA, ///< CAN object that on RTR reception automatically transmits Data Frame
96 ARM_CAN_OBJ_TX_RTR_RX_DATA ///< CAN object that transmits RTR and automatically receives Data Frame
100 \brief CAN Object Capabilities
102 typedef struct _ARM_CAN_OBJ_CAPABILITIES {
103 uint32_t tx : 1; ///< Object supports transmission
104 uint32_t rx : 1; ///< Object supports reception
105 uint32_t rx_rtr_tx_data : 1; ///< Object supports RTR reception and automatic Data Frame transmission
106 uint32_t tx_rtr_rx_data : 1; ///< Object supports RTR transmission and automatic Data Frame reception
107 uint32_t multiple_filters : 1; ///< Object allows assignment of multiple filters to it
108 uint32_t exact_filtering : 1; ///< Object supports exact identifier filtering
109 uint32_t range_filtering : 1; ///< Object supports range identifier filtering
110 uint32_t mask_filtering : 1; ///< Object supports mask identifier filtering
111 uint32_t message_depth : 8; ///< Number of messages buffers (FIFO) for that object
112 uint32_t reserved : 16; ///< Reserved (must be zero)
113 } ARM_CAN_OBJ_CAPABILITIES;
115 /****** CAN Control Function Operation codes *****/
116 #define ARM_CAN_CONTROL_Pos 0UL
117 #define ARM_CAN_CONTROL_Msk (0xFFUL << ARM_CAN_CONTROL_Pos)
118 #define ARM_CAN_SET_FD_MODE (1UL << ARM_CAN_CONTROL_Pos) ///< Set FD operation mode; arg: 0 = disable, 1 = enable
119 #define ARM_CAN_ABORT_MESSAGE_SEND (2UL << ARM_CAN_CONTROL_Pos) ///< Abort sending of CAN message; arg = object
120 #define ARM_CAN_CONTROL_RETRANSMISSION (3UL << ARM_CAN_CONTROL_Pos) ///< Enable/disable automatic retransmission; arg: 0 = disable, 1 = enable (default state)
121 #define ARM_CAN_SET_TRANSCEIVER_DELAY (4UL << ARM_CAN_CONTROL_Pos) ///< Set transceiver delay; arg = delay in time quanta
123 /****** CAN ID Frame Format codes *****/
124 #define ARM_CAN_ID_IDE_Pos 31UL
125 #define ARM_CAN_ID_IDE_Msk (1UL << ARM_CAN_ID_IDE_Pos)
127 /****** CAN Identifier encoding *****/
128 #define ARM_CAN_STANDARD_ID(id) (id & 0x000007FFUL) ///< CAN identifier in standard format (11-bits)
129 #define ARM_CAN_EXTENDED_ID(id) ((id & 0x1FFFFFFFUL) | ARM_CAN_ID_IDE_Msk)///< CAN identifier in extended format (29-bits)
132 \brief CAN Message Information
134 typedef struct _ARM_CAN_MSG_INFO {
135 uint32_t id; ///< CAN identifier with frame format specifier (bit 31)
136 uint32_t rtr : 1; ///< Remote transmission request frame
137 uint32_t edl : 1; ///< Flexible data-rate format extended data length
138 uint32_t brs : 1; ///< Flexible data-rate format with bitrate switch
139 uint32_t esi : 1; ///< Flexible data-rate format error state indicator
140 uint32_t dlc : 4; ///< Data length code
141 uint32_t reserved : 24;
144 /****** CAN specific error code *****/
145 #define ARM_CAN_INVALID_BITRATE_SELECT (ARM_DRIVER_ERROR_SPECIFIC - 1) ///< Bitrate selection not supported
146 #define ARM_CAN_INVALID_BITRATE (ARM_DRIVER_ERROR_SPECIFIC - 2) ///< Requested bitrate not supported
147 #define ARM_CAN_INVALID_BIT_PROP_SEG (ARM_DRIVER_ERROR_SPECIFIC - 3) ///< Propagation segment value not supported
148 #define ARM_CAN_INVALID_BIT_PHASE_SEG1 (ARM_DRIVER_ERROR_SPECIFIC - 4) ///< Phase segment 1 value not supported
149 #define ARM_CAN_INVALID_BIT_PHASE_SEG2 (ARM_DRIVER_ERROR_SPECIFIC - 5) ///< Phase segment 2 value not supported
150 #define ARM_CAN_INVALID_BIT_SJW (ARM_DRIVER_ERROR_SPECIFIC - 6) ///< SJW value not supported
151 #define ARM_CAN_NO_MESSAGE_AVAILABLE (ARM_DRIVER_ERROR_SPECIFIC - 7) ///< Message is not available
153 /****** CAN Status codes *****/
154 #define ARM_CAN_UNIT_STATE_INACTIVE (0U) ///< Unit state: Not active on bus (initialize or error bus off)
155 #define ARM_CAN_UNIT_STATE_ACTIVE (1U) ///< Unit state: Active on bus (can generate active error frame)
156 #define ARM_CAN_UNIT_STATE_PASSIVE (2U) ///< Unit state: Error passive (can not generate active error frame)
157 #define ARM_CAN_LEC_NO_ERROR (0U) ///< Last error code: No error
158 #define ARM_CAN_LEC_BIT_ERROR (1U) ///< Last error code: Bit error
159 #define ARM_CAN_LEC_STUFF_ERROR (2U) ///< Last error code: Bit stuffing error
160 #define ARM_CAN_LEC_CRC_ERROR (3U) ///< Last error code: CRC error
161 #define ARM_CAN_LEC_FORM_ERROR (4U) ///< Last error code: Illegal fixed-form bit
162 #define ARM_CAN_LEC_ACK_ERROR (5U) ///< Last error code: Acknowledgment error
167 typedef volatile struct _ARM_CAN_STATUS {
168 uint32_t unit_state : 4; ///< Unit bus state
169 uint32_t last_error_code : 4; ///< Last error code
170 uint32_t tx_error_count : 8; ///< Transmitter error count
171 uint32_t rx_error_count : 8; ///< Receiver error count
172 uint32_t reserved : 8;
176 /****** CAN Unit Event *****/
177 #define ARM_CAN_EVENT_UNIT_ACTIVE (1U) ///< Unit entered Error Active state
178 #define ARM_CAN_EVENT_UNIT_WARNING (2U) ///< Unit entered Error Warning state (one or both error counters >= 96)
179 #define ARM_CAN_EVENT_UNIT_PASSIVE (3U) ///< Unit entered Error Passive state
180 #define ARM_CAN_EVENT_UNIT_BUS_OFF (4U) ///< Unit entered bus off state
182 /****** CAN Send/Receive Event *****/
183 #define ARM_CAN_EVENT_SEND_COMPLETE (1UL << 0) ///< Send complete
184 #define ARM_CAN_EVENT_RECEIVE (1UL << 1) ///< Message received
185 #define ARM_CAN_EVENT_RECEIVE_OVERRUN (1UL << 2) ///< Received message overrun
188 // Function documentation
190 \fn ARM_DRIVER_VERSION ARM_CAN_GetVersion (void)
191 \brief Get driver version.
192 \return \ref ARM_DRIVER_VERSION
194 \fn ARM_CAN_CAPABILITIES ARM_CAN_GetCapabilities (void)
195 \brief Get driver capabilities.
196 \return \ref ARM_CAN_CAPABILITIES
198 \fn int32_t ARM_CAN_Initialize (ARM_CAN_SignalUnitEvent_t cb_unit_event,
199 ARM_CAN_SignalObjectEvent_t cb_object_event)
200 \brief Initialize CAN interface and register signal (callback) functions.
201 \param[in] cb_unit_event Pointer to \ref ARM_CAN_SignalUnitEvent callback function
202 \param[in] cb_object_event Pointer to \ref ARM_CAN_SignalObjectEvent callback function
203 \return \ref execution_status
205 \fn int32_t ARM_CAN_Uninitialize (void)
206 \brief De-initialize CAN interface.
207 \return \ref execution_status
209 \fn int32_t ARM_CAN_PowerControl (ARM_POWER_STATE state)
210 \brief Control CAN interface power.
211 \param[in] state Power state
212 - \ref ARM_POWER_OFF : power off: no operation possible
213 - \ref ARM_POWER_LOW : low power mode: retain state, detect and signal wake-up events
214 - \ref ARM_POWER_FULL : power on: full operation at maximum performance
215 \return \ref execution_status
217 \fn uint32_t ARM_CAN_GetClock (void)
218 \brief Retrieve CAN base clock frequency.
219 \return base clock frequency
221 \fn int32_t ARM_CAN_SetBitrate (ARM_CAN_BITRATE_SELECT select, uint32_t bitrate, uint32_t bit_segments)
222 \brief Set bitrate for CAN interface.
223 \param[in] select Bitrate selection
224 - \ref ARM_CAN_BITRATE_NOMINAL : nominal (flexible data-rate arbitration) bitrate
225 - \ref ARM_CAN_BITRATE_FD_DATA : flexible data-rate data bitrate
226 \param[in] bitrate Bitrate
227 \param[in] bit_segments Bit segments settings
228 - \ref ARM_CAN_BIT_PROP_SEG(x) : number of time quanta for propagation time segment
229 - \ref ARM_CAN_BIT_PHASE_SEG1(x) : number of time quanta for phase buffer segment 1
230 - \ref ARM_CAN_BIT_PHASE_SEG2(x) : number of time quanta for phase buffer Segment 2
231 - \ref ARM_CAN_BIT_SJW(x) : number of time quanta for (re-)synchronization jump width
232 \return \ref execution_status
234 \fn int32_t ARM_CAN_SetMode (ARM_CAN_MODE mode)
235 \brief Set operating mode for CAN interface.
236 \param[in] mode Operating mode
237 - \ref ARM_CAN_MODE_INITIALIZATION : initialization mode
238 - \ref ARM_CAN_MODE_NORMAL : normal operation mode
239 - \ref ARM_CAN_MODE_RESTRICTED : restricted operation mode
240 - \ref ARM_CAN_MODE_MONITOR : bus monitoring mode
241 - \ref ARM_CAN_MODE_LOOPBACK_INTERNAL : loopback internal mode
242 - \ref ARM_CAN_MODE_LOOPBACK_EXTERNAL : loopback external mode
243 \return \ref execution_status
245 \fn ARM_CAN_OBJ_CAPABILITIES ARM_CAN_ObjectGetCapabilities (uint32_t obj_idx)
246 \brief Retrieve capabilities of an object.
247 \param[in] obj_idx Object index
248 \return \ref ARM_CAN_OBJ_CAPABILITIES
250 \fn int32_t ARM_CAN_ObjectSetFilter (uint32_t obj_idx, ARM_CAN_FILTER_OPERATION operation, uint32_t id, uint32_t arg)
251 \brief Add or remove filter for message reception.
252 \param[in] obj_idx Object index of object that filter should be or is assigned to
253 \param[in] operation Operation on filter
254 - \ref ARM_CAN_FILTER_ID_EXACT_ADD : add exact id filter
255 - \ref ARM_CAN_FILTER_ID_EXACT_REMOVE : remove exact id filter
256 - \ref ARM_CAN_FILTER_ID_RANGE_ADD : add range id filter
257 - \ref ARM_CAN_FILTER_ID_RANGE_REMOVE : remove range id filter
258 - \ref ARM_CAN_FILTER_ID_MASKABLE_ADD : add maskable id filter
259 - \ref ARM_CAN_FILTER_ID_MASKABLE_REMOVE : remove maskable id filter
260 \param[in] id ID or start of ID range (depending on filter type)
261 \param[in] arg Mask or end of ID range (depending on filter type)
262 \return \ref execution_status
264 \fn int32_t ARM_CAN_ObjectConfigure (uint32_t obj_idx, ARM_CAN_OBJ_CONFIG obj_cfg)
265 \brief Configure object.
266 \param[in] obj_idx Object index
267 \param[in] obj_cfg Object configuration state
268 - \ref ARM_CAN_OBJ_INACTIVE : deactivate object
269 - \ref ARM_CAN_OBJ_RX : configure object for reception
270 - \ref ARM_CAN_OBJ_TX : configure object for transmission
271 - \ref ARM_CAN_OBJ_RX_RTR_TX_DATA : configure object that on RTR reception automatically transmits Data Frame
272 - \ref ARM_CAN_OBJ_TX_RTR_RX_DATA : configure object that transmits RTR and automatically receives Data Frame
273 \return \ref execution_status
275 \fn int32_t ARM_CAN_MessageSend (uint32_t obj_idx, ARM_CAN_MSG_INFO *msg_info, const uint8_t *data, uint8_t size)
276 \brief Send message on CAN bus.
277 \param[in] obj_idx Object index
278 \param[in] msg_info Pointer to CAN message information
279 \param[in] data Pointer to data buffer
280 \param[in] size Number of data bytes to send
281 \return value >= 0 number of data bytes accepted to send
282 \return value < 0 \ref execution_status
284 \fn int32_t ARM_CAN_MessageRead (uint32_t obj_idx, ARM_CAN_MSG_INFO *msg_info, uint8_t *data, uint8_t size)
285 \brief Read message received on CAN bus.
286 \param[in] obj_idx Object index
287 \param[out] msg_info Pointer to read CAN message information
288 \param[out] data Pointer to data buffer for read data
289 \param[in] size Maximum number of data bytes to read
290 \return value >= 0 number of data bytes read
291 \return value < 0 \ref execution_status
293 \fn int32_t ARM_CAN_Control (uint32_t control, uint32_t arg)
294 \brief Control CAN interface.
295 \param[in] control Operation
296 - \ref ARM_CAN_SET_FD_MODE : set FD operation mode
297 - \ref ARM_CAN_ABORT_MESSAGE_SEND : abort sending of CAN message
298 - \ref ARM_CAN_CONTROL_RETRANSMISSION : enable/disable automatic retransmission
299 - \ref ARM_CAN_SET_TRANSCEIVER_DELAY : set transceiver delay
300 \param[in] arg Argument of operation
301 \return \ref execution_status
303 \fn ARM_CAN_STATUS ARM_CAN_GetStatus (void)
304 \brief Get CAN status.
305 \return CAN status \ref ARM_CAN_STATUS
307 \fn void ARM_CAN_SignalUnitEvent (uint32_t event)
308 \brief Signal CAN unit event.
309 \param[in] event \ref CAN_unit_events
312 \fn void ARM_CAN_SignalObjectEvent (uint32_t obj_idx, uint32_t event)
313 \brief Signal CAN object event.
314 \param[in] obj_idx Object index
315 \param[in] event \ref CAN_events
319 typedef void (*ARM_CAN_SignalUnitEvent_t) (uint32_t event); ///< Pointer to \ref ARM_CAN_SignalUnitEvent : Signal CAN Unit Event.
320 typedef void (*ARM_CAN_SignalObjectEvent_t) (uint32_t obj_idx, uint32_t event); ///< Pointer to \ref ARM_CAN_SignalObjectEvent : Signal CAN Object Event.
324 \brief CAN Device Driver Capabilities.
326 typedef struct _ARM_CAN_CAPABILITIES {
327 uint32_t num_objects : 8; ///< Number of \ref can_objects available
328 uint32_t reentrant_operation : 1; ///< Support for reentrant calls to \ref ARM_CAN_MessageSend, \ref ARM_CAN_MessageRead, \ref ARM_CAN_ObjectConfigure and abort message sending used by \ref ARM_CAN_Control
329 uint32_t fd_mode : 1; ///< Support for CAN with flexible data-rate mode (CAN_FD) (set by \ref ARM_CAN_Control)
330 uint32_t restricted_mode : 1; ///< Support for restricted operation mode (set by \ref ARM_CAN_SetMode)
331 uint32_t monitor_mode : 1; ///< Support for bus monitoring mode (set by \ref ARM_CAN_SetMode)
332 uint32_t internal_loopback : 1; ///< Support for internal loopback mode (set by \ref ARM_CAN_SetMode)
333 uint32_t external_loopback : 1; ///< Support for external loopback mode (set by \ref ARM_CAN_SetMode)
334 uint32_t reserved : 18; ///< Reserved (must be zero)
335 } ARM_CAN_CAPABILITIES;
339 \brief Access structure of the CAN Driver.
341 typedef struct _ARM_DRIVER_CAN {
342 ARM_DRIVER_VERSION (*GetVersion) (void); ///< Pointer to \ref ARM_CAN_GetVersion : Get driver version.
343 ARM_CAN_CAPABILITIES (*GetCapabilities) (void); ///< Pointer to \ref ARM_CAN_GetCapabilities : Get driver capabilities.
344 int32_t (*Initialize) (ARM_CAN_SignalUnitEvent_t cb_unit_event,
345 ARM_CAN_SignalObjectEvent_t cb_object_event); ///< Pointer to \ref ARM_CAN_Initialize : Initialize CAN interface.
346 int32_t (*Uninitialize) (void); ///< Pointer to \ref ARM_CAN_Uninitialize : De-initialize CAN interface.
347 int32_t (*PowerControl) (ARM_POWER_STATE state); ///< Pointer to \ref ARM_CAN_PowerControl : Control CAN interface power.
348 uint32_t (*GetClock) (void); ///< Pointer to \ref ARM_CAN_GetClock : Retrieve CAN base clock frequency.
349 int32_t (*SetBitrate) (ARM_CAN_BITRATE_SELECT select,
351 uint32_t bit_segments); ///< Pointer to \ref ARM_CAN_SetBitrate : Set bitrate for CAN interface.
352 int32_t (*SetMode) (ARM_CAN_MODE mode); ///< Pointer to \ref ARM_CAN_SetMode : Set operating mode for CAN interface.
353 ARM_CAN_OBJ_CAPABILITIES (*ObjectGetCapabilities) (uint32_t obj_idx); ///< Pointer to \ref ARM_CAN_ObjectGetCapabilities : Retrieve capabilities of an object.
354 int32_t (*ObjectSetFilter) (uint32_t obj_idx,
355 ARM_CAN_FILTER_OPERATION operation,
357 uint32_t arg); ///< Pointer to \ref ARM_CAN_ObjectSetFilter : Add or remove filter for message reception.
358 int32_t (*ObjectConfigure) (uint32_t obj_idx,
359 ARM_CAN_OBJ_CONFIG obj_cfg); ///< Pointer to \ref ARM_CAN_ObjectConfigure : Configure object.
360 int32_t (*MessageSend) (uint32_t obj_idx,
361 ARM_CAN_MSG_INFO *msg_info,
363 uint8_t size); ///< Pointer to \ref ARM_CAN_MessageSend : Send message on CAN bus.
364 int32_t (*MessageRead) (uint32_t obj_idx,
365 ARM_CAN_MSG_INFO *msg_info,
367 uint8_t size); ///< Pointer to \ref ARM_CAN_MessageRead : Read message received on CAN bus.
368 int32_t (*Control) (uint32_t control,
369 uint32_t arg); ///< Pointer to \ref ARM_CAN_Control : Control CAN interface.
370 ARM_CAN_STATUS (*GetStatus) (void); ///< Pointer to \ref ARM_CAN_GetStatus : Get CAN status.
371 } const ARM_DRIVER_CAN;
377 #endif /* DRIVER_CAN_H_ */