mikroSDK Reference Manual
drv_can.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2023 MikroElektronika d.o.o.
4 ** Contact: https://www.mikroe.com/contact
5 **
6 ** This file is part of the mikroSDK package
7 **
8 ** Commercial License Usage
9 **
10 ** Licensees holding valid commercial NECTO compilers AI licenses may use this
11 ** file in accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The MikroElektronika Company.
14 ** For licensing terms and conditions see
15 ** https://www.mikroe.com/legal/software-license-agreement.
16 ** For further information use the contact form at
17 ** https://www.mikroe.com/contact.
18 **
19 **
20 ** GNU Lesser General Public License Usage
21 **
22 ** Alternatively, this file may be used for
23 ** non-commercial projects under the terms of the GNU Lesser
24 ** General Public License version 3 as published by the Free Software
25 ** Foundation: https://www.gnu.org/licenses/lgpl-3.0.html.
26 **
27 ** The above copyright notice and this permission notice shall be
28 ** included in all copies or substantial portions of the Software.
29 **
30 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 ** OF MERCHANTABILITY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
32 ** TO THE WARRANTIES FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
34 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
35 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36 ** OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 **
38 ****************************************************************************/
44 #ifndef _DRV_CAN_H_
45 #define _DRV_CAN_H_
46 
47 #ifdef __cplusplus
48 extern "C"{
49 #endif
50 
51 #include "drv_name.h"
52 #include "hal_can.h"
53 
57 typedef enum
58 {
60  CAN_ERROR = (-1)
61 } can_err_t;
62 
66 typedef enum
67 {
68  /* Universal Modes. */
72  /* STM32 and RISC-V Modes. */
75  /* NXP Modes. */
80  /* dsPIC Modes.*/
82  /* PIC/PIC32 Modes. */
84 } can_mode_t;
85 
89 typedef enum
90 {
95 } can_state_t;
96 
100 typedef enum
101 {
105 
109 typedef enum
110 {
114 
118 typedef enum
119 {
123 
127 typedef enum
128 {
132 
136 typedef enum
137 {
171 
175 typedef enum
176 {
180 
271 typedef struct
272 {
273  union {
274  struct {
275  uint16_t can_filter_id_low;
276  uint16_t can_filter_id_high;
277  };
278  uint32_t can_filter_id;
279  };
280  union {
281  struct {
282  uint16_t can_filter_mask_id_low;
283  uint16_t can_filter_mask_id_high;
284  };
285  uint32_t can_filter_mask_id;
286  };
287  uint8_t can_filter_bank;
288  can_frame_type_t can_filter_frame_type[4];
289  can_filter_activation_t can_filter_enable;
290  can_filter_mode_t can_filter_mode;
291  can_filter_scale_t can_filter_scale;
292  can_filter_fifo_t can_filter_fifo;
294 
320 typedef struct
321 {
322  pin_name_t tx_pin;
323  pin_name_t rx_pin;
324  can_mode_t mode;
325  uint32_t frequency;
326 } can_config_t;
327 
338 typedef struct
339 {
340  handle_t handle;
341  can_config_t config;
342  can_filter_config_t filter_config;
343 } can_t;
344 
356 typedef struct
357 {
358  union {
359  struct {
360  uint16_t std_id;
361  uint16_t ext_id;
362  };
363  uint32_t id;
364  };
365  can_frame_format_t frame_format;
366  can_frame_type_t frame_type;
367  uint8_t data_len;
368  uint8_t message_data[8];
369 } can_message_t;
370 
382 typedef struct
383 {
384  can_message_t message; /* CAN message structure. */
386 
398 typedef struct
399 {
400  can_message_t message; /* CAN message structure. */
401  uint8_t rx_fifo_number; /* Receive FIFO number. */
403 
478 void can_configure_default( can_config_t *config );
479 
514 
550 err_t can_open( can_t *obj, can_config_t *config );
551 
575 err_t can_init( can_t *obj );
576 
606 err_t can_set_filter( can_t *obj, can_filter_config_t *filter_config );
607 
633 err_t can_set_frequency( can_t *obj, uint32_t frequency );
634 
656 
682 err_t can_set_mode( can_t *obj, can_mode_t mode );
683 
704 err_t can_get_mode( can_t *obj );
705 
752 err_t can_transmit( can_t *obj, can_transmit_message_struct *transmit_message );
753 
773 void can_transmission_stop( can_t *obj );
774 
807 err_t can_receive( can_t *obj, can_receive_message_struct *receive_message );
808 
833 err_t can_close( can_t *obj );
834  // drvcangroup // drvgroup // pergroup
838 
839 #ifdef __cplusplus
840 }
841 #endif
842 
843 #endif // _DRV_CAN_H_
844 // ------------------------------------------------------------------------- END
CAN_FILTER_FIFO10
Definition: drv_can.h:147
can_get_frequency
err_t can_get_frequency(can_t *obj)
Get CAN module frequency.
CAN_FILTER_FIFO22
Definition: drv_can.h:159
CAN_FILTER_FIFO7
Definition: drv_can.h:144
CAN_STATE_ERROR
Definition: drv_can.h:93
CAN_FRAME_TYPE_DATA
Definition: drv_can.h:110
can_set_mode
err_t can_set_mode(can_t *obj, can_mode_t mode)
Set CAN module mode.
drv_name.h
Pin and port name type definitions.
CAN_MODE_LOOPBACK_SILENT
Definition: drv_can.h:73
CAN_FILTER_FIFO26
Definition: drv_can.h:163
CAN_FILTER_FIFO25
Definition: drv_can.h:162
CAN_FILTER_MODE_IDMASK
Definition: drv_can.h:119
CAN_FILTER_FIFO30
Definition: drv_can.h:167
CAN_MODE_LISTENALL
Definition: drv_can.h:80
CAN_STATE_RESET
Definition: drv_can.h:90
can_t
CAN driver context structure.
Definition: drv_can.h:337
can_mode_t
can_mode_t
Definition: drv_can.h:65
CAN_FILTER_FIFO4
Definition: drv_can.h:141
can_frame_format_t
can_frame_format_t
Definition: drv_can.h:99
CAN_MODE_NORMAL
Definition: drv_can.h:68
CAN_FILTER_DISABLE
Definition: drv_can.h:176
can_filter_configure_default
void can_filter_configure_default(can_filter_config_t *filter_config)
Configure CAN Driver filter configuration structure.
CAN_FILTER_FIFO21
Definition: drv_can.h:158
can_configure_default
void can_configure_default(can_config_t *config)
Configure CAN Driver configuration structure.
CAN_FILTER_FIFO24
Definition: drv_can.h:161
CAN_MODE_LOOPBACK
Definition: drv_can.h:69
CAN_FILTER_FIFO9
Definition: drv_can.h:146
can_set_frequency
err_t can_set_frequency(can_t *obj, uint32_t frequency)
Set CAN module frequency.
CAN_FILTER_FIFO20
Definition: drv_can.h:157
CAN_FILTER_FIFO18
Definition: drv_can.h:155
can_message_t
CAN driver message structure.
Definition: drv_can.h:355
can_filter_mode_t
can_filter_mode_t
Definition: drv_can.h:117
CAN_FILTER_FIFO3
Definition: drv_can.h:140
CAN_FRAME_FORMAT_EXTENDED_29BITS
Definition: drv_can.h:102
CAN_FILTER_FIFO31
Definition: drv_can.h:168
CAN_MODE_CONFIGURATION
Definition: drv_can.h:82
CAN_FILTER_SCALE_16BIT
Definition: drv_can.h:128
can_open
err_t can_open(can_t *obj, can_config_t *config)
Open the CAN driver object.
CAN_FILTER_MODE_IDLIST
Definition: drv_can.h:120
can_filter_activation_t
can_filter_activation_t
Definition: drv_can.h:174
can_config_t
CAN init configuration structure.
Definition: drv_can.h:319
CAN_MODE_FREEZE
Definition: drv_can.h:78
can_state_t
can_state_t
Definition: drv_can.h:88
CAN_FILTER_FIFO2
Definition: drv_can.h:139
CAN_FILTER_FIFO11
Definition: drv_can.h:148
can_frame_type_t
can_frame_type_t
Definition: drv_can.h:108
can_get_mode
err_t can_get_mode(can_t *obj)
Get CAN module mode.
can_set_filter
err_t can_set_filter(can_t *obj, can_filter_config_t *filter_config)
Initialize CAN module filter.
CAN_FILTER_FIFO27
Definition: drv_can.h:164
CAN_FILTER_FIFO23
Definition: drv_can.h:160
CAN_FILTER_ENABLE
Definition: drv_can.h:177
can_filter_scale_t
can_filter_scale_t
Definition: drv_can.h:126
can_transmit_message_struct
CAN driver transmit message structure.
Definition: drv_can.h:381
CAN_FILTER_FIFO16
Definition: drv_can.h:153
CAN_FILTER_FIFO17
Definition: drv_can.h:154
CAN_FILTER_FIFO15
Definition: drv_can.h:152
CAN_FILTER_FIFO19
Definition: drv_can.h:156
CAN_MODE_LISTENONLY
Definition: drv_can.h:75
CAN_STATE_LISTENING
Definition: drv_can.h:92
CAN_ERROR
Definition: drv_can.h:59
CAN_FILTER_FIFO6
Definition: drv_can.h:143
can_receive_message_struct
CAN driver receive message structure.
Definition: drv_can.h:397
CAN_MODE_DISABLE
Definition: drv_can.h:70
CAN_FILTER_FIFO28
Definition: drv_can.h:165
CAN_MODE_STOP
Definition: drv_can.h:77
CAN_FILTER_FIFO5
Definition: drv_can.h:142
CAN_FILTER_FIFO12
Definition: drv_can.h:149
can_transmission_stop
void can_transmission_stop(can_t *obj)
Stop transmit operation.
CAN_FILTER_FIFO14
Definition: drv_can.h:151
can_err_t
can_err_t
Definition: drv_can.h:56
CAN_SUCCESS
Definition: drv_can.h:58
can_receive
err_t can_receive(can_t *obj, can_receive_message_struct *receive_message)
Get data from the RX FIFO.
CAN_FILTER_FIFO1
Definition: drv_can.h:138
CAN_FRAME_FORMAT_STANDARD_11BITS
Definition: drv_can.h:101
pin_name_t
hal_pin_name_t pin_name_t
Definition: drv_name.h:73
CAN_FILTER_FIFO29
Definition: drv_can.h:166
CAN_MODE_DOZE
Definition: drv_can.h:76
CAN_FILTER_SCALE_32BIT
Definition: drv_can.h:129
err_t
int32_t err_t
Definition: hal_target.h:63
can_filter_config_t
CAN filter init configuration structure.
Definition: drv_can.h:270
can_init
err_t can_init(can_t *obj)
Initialize CAN module.
can_filter_fifo_t
can_filter_fifo_t
Definition: drv_can.h:135
CAN_STATE_READY
Definition: drv_can.h:91
CAN_FRAME_TYPE_RTR
Definition: drv_can.h:111
can_transmit
err_t can_transmit(can_t *obj, can_transmit_message_struct *transmit_message)
Transmit data to the first free TX mailbox.
can_close
err_t can_close(can_t *obj)
Close CAN driver object.
CAN_FILTER_FIFO0
Definition: drv_can.h:137
CAN_FILTER_FIFO8
Definition: drv_can.h:145
CAN_FILTER_FIFO13
Definition: drv_can.h:150
CAN_MODE_SILENT
Definition: drv_can.h:72