AtMega CAN Library
Well documented CAN library for AtMega processors utilizing the original Atmel driver
can_drv.h
Go to the documentation of this file.
1 //******************************************************************************
21 //******************************************************************************
22 
23 #ifndef _CAN_DRV_H_
24 #define _CAN_DRV_H_
25 
26 //_____ I N C L U D E S ________________________________________________________
27 #ifdef BOOTLOADER
28 #include "loaderconfiguration.h"
29 #else
30  #include "canlibraryconfiguration.h"
31  #ifndef _CANLIBRARYCONFIGURATION_H_
32  #error You need to #include the "configuration.h" file to set port and speed options.
33  #endif
34 #endif
35 
36 #include <stdint.h>
37 #include <avr/io.h>
38 
39 //_____ D E F I N I T I O N S __________________________________________________
40 #define max(a,b) \
41 ({ __typeof__ (a) _a = (a); \
42  __typeof__ (b) _b = (b); \
43  _a > _b ? _a : _b; })
44 
45 
46  // ----------
47 #ifndef F_CPU
48 # error You must define F_CPU in project properties.
49 #endif
50  // ----------
51 #ifndef CAN_BAUDRATE
52 # error You must define CAN_BAUDRATE in your "configuration.h" file.
53 #endif
54 #define CAN_AUTOBAUD 0
55  // ----------
56 #if F_CPU == 16000000
57 # if CAN_BAUDRATE == 100
58 # define CONF_CANBT1 0x12 // Tscl = 10x Tclkio = 625 ns
59 # define CONF_CANBT2 0x0C // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
60 # define CONF_CANBT3 0x37 // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
61 # elif CAN_BAUDRATE == 125
62 # define CONF_CANBT1 0x0E // Tscl = 8x Tclkio = 500 ns
63 # define CONF_CANBT2 0x0C // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
64 # define CONF_CANBT3 0x37 // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
65 # elif CAN_BAUDRATE == 200
66 # define CONF_CANBT1 0x08 // Tscl = 5x Tclkio = 312.5 ns
67 # define CONF_CANBT2 0x0C // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
68 # define CONF_CANBT3 0x37 // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
69 # elif CAN_BAUDRATE == 250
70 # define CONF_CANBT1 0x06 // Tscl = 4x Tclkio = 250 ns
71 # define CONF_CANBT2 0x0C // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
72 # define CONF_CANBT3 0x37 // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
73 # elif CAN_BAUDRATE == 500
74 # define CONF_CANBT1 0x06 // Tscl = 4x Tclkio = 250 ns
75 # define CONF_CANBT2 0x04 // Tsync = 1x Tscl, Tprs = 3x Tscl, Tsjw = 1x Tscl
76 # define CONF_CANBT3 0x13 // Tpsh1 = 2x Tscl, Tpsh2 = 2x Tscl, 3 sample points
77 # elif CAN_BAUDRATE == 1000
78 # define CONF_CANBT1 0x02 // Tscl = 2x Tclkio = 125 ns
79 # define CONF_CANBT2 0x04 // Tsync = 1x Tscl, Tprs = 3x Tscl, Tsjw = 1x Tscl
80 # define CONF_CANBT3 0x13 // Tpsh1 = 2x Tscl, Tpsh2 = 2x Tscl, 3 sample points
81 # elif CAN_BAUDRATE == CAN_AUTOBAUD // Treated later in the file
82 # define CONF_CANBT1 0x00 // Unused
83 # define CONF_CANBT2 0x00 // Unused
84 # define CONF_CANBT3 0x00 // Unused
85 # else
86 # error This CAN_BAUDRATE value is not in "can_drv.h" file
87 # endif
88 
89 #elif F_CPU == 12000000
90 # if CAN_BAUDRATE == 100
91 # define CONF_CANBT1 0x0A // Tscl = 6x Tclkio = 500 ns
92 # define CONF_CANBT2 0x0E // Tsync = 1x Tscl, Tprs = 8x Tscl, Tsjw = 1x Tscl
93 # define CONF_CANBT3 0x4B // Tpsh1 = 6x Tscl, Tpsh2 = 5x Tscl, 3 sample points
94 # elif CAN_BAUDRATE == 125
95 # define CONF_CANBT1 0x0A // Tscl = 6x Tclkio = 500 ns
96 # define CONF_CANBT2 0x0C // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
97 # define CONF_CANBT3 0x37 // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
98 # elif CAN_BAUDRATE == 200
99 # define CONF_CANBT1 0x04 // Tscl = 3x Tclkio = 250 ns
100 # define CONF_CANBT2 0x0E // Tsync = 1x Tscl, Tprs = 8x Tscl, Tsjw = 1x Tscl
101 # define CONF_CANBT3 0x4B // Tpsh1 = 6x Tscl, Tpsh2 = 5x Tscl, 3 sample points
102 # elif CAN_BAUDRATE == 250
103 # define CONF_CANBT1 0x04 // Tscl = 3x Tclkio = 250 ns
104 # define CONF_CANBT2 0x0C // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
105 # define CONF_CANBT3 0x37 // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
106 # elif CAN_BAUDRATE == 500
107 # define CONF_CANBT1 0x02 // Tscl = 2x Tclkio = 166.666 ns
108 # define CONF_CANBT2 0x08 // Tsync = 1x Tscl, Tprs = 5x Tscl, Tsjw = 1x Tscl
109 # define CONF_CANBT3 0x25 // Tpsh1 = 3x Tscl, Tpsh2 = 3x Tscl, 3 sample points
110 # elif CAN_BAUDRATE == 1000
111 # define CONF_CANBT1 0x00 // Tscl = 1x Tclkio = 83.333 ns
112 # define CONF_CANBT2 0x08 // Tsync = 1x Tscl, Tprs = 5x Tscl, Tsjw = 1x Tscl
113 # define CONF_CANBT3 0x25 // Tpsh1 = 3x Tscl, Tpsh2 = 3x Tscl, 3 sample points
114 # elif CAN_BAUDRATE == CAN_AUTOBAUD // Treated later in the file
115 # define CONF_CANBT1 0x00 // Unused
116 # define CONF_CANBT2 0x00 // Unused
117 # define CONF_CANBT3 0x00 // Unused
118 # else
119 # error This CAN_BAUDRATE value is not in "can_drv.h" file
120 # endif
121 
122 #elif F_CPU == 8000000
123 # if CAN_BAUDRATE == 100
124 # define CONF_CANBT1 0x08 // Tscl = 5x Tclkio = 625 ns
125 # define CONF_CANBT2 0x0C // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
126 # define CONF_CANBT3 0x37 // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
127 # elif CAN_BAUDRATE == 125
128 # define CONF_CANBT1 0x06 // Tscl = 4x Tclkio = 500 ns
129 # define CONF_CANBT2 0x0C // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
130 # define CONF_CANBT3 0x37 // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
131 # elif CAN_BAUDRATE == 200
132 # define CONF_CANBT1 0x02 // Tscl = 2x Tclkio = 250 ns
133 # define CONF_CANBT2 0x0E // Tsync = 1x Tscl, Tprs = 8x Tscl, Tsjw = 1x Tscl
134 # define CONF_CANBT3 0x4B // Tpsh1 = 6x Tscl, Tpsh2 = 5x Tscl, 3 sample points
135 # elif CAN_BAUDRATE == 250
136 # define CONF_CANBT1 0x02 // Tscl = 2x Tclkio = 250 ns
137 # define CONF_CANBT2 0x0C // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
138 # define CONF_CANBT3 0x37 // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
139 # elif CAN_BAUDRATE == 500
140 # define CONF_CANBT1 0x02 // Tscl = 2x Tclkio = 250 ns
141 # define CONF_CANBT2 0x04 // Tsync = 1x Tscl, Tprs = 3x Tscl, Tsjw = 1x Tscl
142 # define CONF_CANBT3 0x13 // Tpsh1 = 2x Tscl, Tpsh2 = 2x Tscl, 3 sample points
143 # elif CAN_BAUDRATE == 1000
144 # define CONF_CANBT1 0x00 // Tscl = 1x Tclkio = 125 ns
145 # define CONF_CANBT2 0x04 // Tsync = 1x Tscl, Tprs = 3x Tscl, Tsjw = 1x Tscl
146 # define CONF_CANBT3 0x13 // Tpsh1 = 2x Tscl, Tpsh2 = 2x Tscl, 3 sample points
147 # elif CAN_BAUDRATE == CAN_AUTOBAUD // Treated later in the file
148 # define CONF_CANBT1 0x00 // Unused
149 # define CONF_CANBT2 0x00 // Unused
150 # define CONF_CANBT3 0x00 // Unused
151 # else
152 # error This CAN_BAUDRATE value is not in "can_drv.h" file
153 # endif
154 
155 #else
156 # error This FOSC value is not in "can_drv.h" file
157 #endif
158  // ----------
163 #if CAN_BAUDRATE == CAN_AUTOBAUD
164 # define Can_bit_timing(mode) (can_auto_baudrate(mode) )
165 #else
166 # define Can_bit_timing(mode) (can_fixed_baudrate(mode))
167 #endif
168 
169  // ----------
170 #define ERR_GEN_MSK ((1<<SERG)|(1<<CERG)|(1<<FERG)|(1<<AERG))
171 #define INT_GEN_MSK ((1<<BOFFIT)|(1<<BXOK)|(ERR_GEN_MSK))
172 
173 #define BRP_MSK ((1<<BRP5)|(1<<BRP4)|(1<<BRP3)|(1<<BRP2)|(1<<BRP1)|(1<<BRP0))
174 #define SJW_MSK ((1<<SJW1)|(1<<SJW0))
175 #define PRS_MSK ((1<<PRS2)|(1<<PRS1)|(1<<PRS0))
176 #define PHS2_MSK ((1<<PHS22)|(1<<PHS21)|(1<<PHS20))
177 #define PHS1_MSK ((1<<PHS12)|(1<<PHS11)|(1<<PHS10))
178 #define BRP (BRP0)
179 #define SJW (SJW0)
180 #define PRS (PRS0)
181 #define PHS2 (PHS20)
182 #define PHS1 (PHS10)
183 
184 #define HPMOB_MSK ((1<<HPMOB3)|(1<<HPMOB2)|(1<<HPMOB1)|(1<<HPMOB0))
185 #define MOBNB_MSK ((1<<MOBNB3)|(1<<MOBNB2)|(1<<MOBNB1)|(1<<MOBNB0))
186 
187 #define ERR_MOB_MSK ((1<<BERR)|(1<<SERR)|(1<<CERR)|(1<<FERR)|(1<<AERR))
188 #define INT_MOB_MSK ((1<<TXOK)|(1<<RXOK)|(1<<BERR)|(1<<SERR)|(1<<CERR)|(1<<FERR)|(1<<AERR))
189 
190 #define CONMOB_MSK ((1<<CONMOB1)|(1<<CONMOB0))
191 #define DLC_MSK ((1<<DLC3)|(1<<DLC2)|(1<<DLC1)|(1<<DLC0))
192 #define CONMOB (CONMOB0)
193 #define DLC (DLC0)
194  // ----------
195 #define BRP_MIN 1
196 #define BRP_MAX 64
197 #define NTQ_MIN 8
198 #define NTQ_MAX 25
199 #define PRS_MIN 1
201 #define PRS_MAX 8
202 #define PHS1_MIN 2
203 #define PHS1_MAX 8
204 #define PHS2_MIN 2
205 #define PHS2_MAX 8
206 #define SJW_MIN 1
207 #define SJW_MAX 4
208  // ----------
209 
210 #define NB_DATA_MAX 8
211 #define LAST_MOB_NB (NB_MOB-1)
212 #define NO_MOB 0xFF
213  // ----------
214 typedef enum {
215  MOB_0, MOB_1, MOB_2, MOB_3, MOB_4, MOB_5, MOB_6, MOB_7,
216  MOB_8, MOB_9, MOB_10, MOB_11, MOB_12, MOB_13, MOB_14 } can_mob_t;
217  // ----------
218 #define STATUS_CLEARED 0x00
219  // ----------
220 #define MOB_NOT_COMPLETED 0x00 // 0x00
221 #define MOB_TX_COMPLETED (1<<TXOK) // 0x40
222 #define MOB_RX_COMPLETED (1<<RXOK) // 0x20
223 #define MOB_RX_COMPLETED_DLCW ((1<<RXOK)|(1<<DLCW)) // 0xA0
224 #define MOB_ACK_ERROR (1<<AERR) // 0x01
225 #define MOB_FORM_ERROR (1<<FERR) // 0x02
226 #define MOB_CRC_ERROR (1<<CERR) // 0x04
227 #define MOB_STUFF_ERROR (1<<SERR) // 0x08
228 #define MOB_BIT_ERROR (1<<BERR) // 0x10
229 #define MOB_PENDING ((1<<RXOK)|(1<<TXOK)) // 0x60
230 #define MOB_NOT_REACHED ((1<<AERR)|(1<<FERR)|(1<<CERR)|(1<<SERR)|(1<<BERR)) // 0x1F
231 #define MOB_DISABLE 0xFF // 0xFF
232  // ----------
233 #define MOB_Tx_ENA 1
234 #define MOB_Rx_ENA 2
235 #define MOB_Rx_BENA 3
236  // ----------
237 
238 //_____ M A C R O S ____________________________________________________________
239 
240  // ----------
241 #define Can_reset() ( CANGCON = (1<<SWRES) )
242 #define Can_enable() ( CANGCON |= (1<<ENASTB))
243 #define Can_disable() ( CANGCON &= ~(1<<ENASTB))
244  // ----------
245 #define Can_full_abort() { CANGCON |= (1<<ABRQ); CANGCON &= ~(1<<ABRQ); }
246  // ----------
247 #define Can_conf_bt() { CANBT1=CONF_CANBT1; CANBT2=CONF_CANBT2; CANBT3=CONF_CANBT3; }
248  // ----------
249 #define Can_set_mob(mob) { CANPAGE = ((mob) << 4);}
250 #define Can_set_mask_mob() { CANIDM4=0xFF; CANIDM3=0xFF; CANIDM2=0xFF; CANIDM1=0xFF; }
251 #define Can_clear_mask_mob() { CANIDM4=0x00; CANIDM3=0x00; CANIDM2=0x00; CANIDM1=0x00; }
252 #define Can_clear_status_mob() { CANSTMOB=0x00; }
253 #define Can_clear_mob() { uint8_t volatile *__i_; for (__i_=&CANSTMOB; __i_<&CANSTML; __i_++) { *__i_=0x00 ;}}
254  // ----------
255 #define Can_mob_abort() ( DISABLE_MOB )
256  // ----------
257 #define Can_set_dlc(dlc) ( CANCDMOB |= (dlc) )
258 #define Can_set_ide() ( CANCDMOB |= (1<<IDE) )
259 #define Can_set_rtr() ( CANIDT4 |= (1<<RTRTAG) )
260 #define Can_set_rplv() ( CANCDMOB |= (1<<RPLV) )
261  // ----------
262 #define Can_clear_dlc() ( CANCDMOB &= ~DLC_MSK )
263 #define Can_clear_ide() ( CANCDMOB &= ~(1<<IDE) )
264 #define Can_clear_rtr() ( CANIDT4 &= ~(1<<RTRTAG) )
265 #define Can_clear_rplv() ( CANCDMOB &= ~(1<<RPLV) )
266  // ----------
267 #define DISABLE_MOB ( CANCDMOB &= (~CONMOB_MSK) )
268 #define Can_config_tx() { DISABLE_MOB; CANCDMOB |= (MOB_Tx_ENA << CONMOB); }
269 #define Can_config_rx() { DISABLE_MOB; CANCDMOB |= (MOB_Rx_ENA << CONMOB); }
270 #define Can_config_rx_buffer() { CANCDMOB |= (MOB_Rx_BENA << CONMOB); }
271  // ----------
272 #define Can_get_dlc() ((CANCDMOB & DLC_MSK) >> DLC )
273 #define Can_get_ide() ((CANCDMOB & (1<<IDE)) >> IDE )
274 #define Can_get_rtr() ((CANIDT4 & (1<<RTRTAG)) >> RTRTAG)
275  // ----------
276 #define Can_set_rtrmsk() ( CANIDM4 |= (1<<RTRMSK) )
277 #define Can_set_idemsk() ( CANIDM4 |= (1<<IDEMSK) )
278  // ----------
279 #define Can_clear_rtrmsk() ( CANIDM4 &= ~(1<<RTRMSK) )
280 #define Can_clear_idemsk() ( CANIDM4 &= ~(1<<IDEMSK) )
281  // ----------
283 #define Can_get_std_id(identifier) { *((uint8_t *)(&(identifier))+1) = CANIDT1>>5 ; \
284  *((uint8_t *)(&(identifier)) ) = (CANIDT2>>5)+(CANIDT1<<3); }
285  // ----------
287 #define Can_get_ext_id(identifier) { *((uint8_t *)(&(identifier))+3) = CANIDT1>>3 ; \
288  *((uint8_t *)(&(identifier))+2) = (CANIDT2>>3)+(CANIDT1<<5); \
289  *((uint8_t *)(&(identifier))+1) = (CANIDT3>>3)+(CANIDT2<<5); \
290  *((uint8_t *)(&(identifier)) ) = (CANIDT4>>3)+(CANIDT3<<5); }
291  // ----------
293 #define CAN_SET_STD_ID_10_4(identifier) (((*((uint8_t *)(&(identifier))+1))<<5)+((* (uint8_t *)(&(identifier)))>>3))
294 #define CAN_SET_STD_ID_3_0( identifier) (( * (uint8_t *)(&(identifier)) )<<5)
295  // ----------
297 #define Can_set_std_id(identifier) { CANIDT1 = CAN_SET_STD_ID_10_4(identifier); \
298  CANIDT2 = CAN_SET_STD_ID_3_0( identifier); \
299  CANCDMOB &= (~(1<<IDE)) ; }
300  // ----------
302 #define Can_set_std_msk(mask) { CANIDM1 = CAN_SET_STD_ID_10_4(mask); \
303  CANIDM2 = CAN_SET_STD_ID_3_0( mask); }
304  // ----------
306 #define CAN_SET_EXT_ID_28_21(identifier) (((*((uint8_t *)(&(identifier))+3))<<3)+((*((uint8_t *)(&(identifier))+2))>>5))
307 #define CAN_SET_EXT_ID_20_13(identifier) (((*((uint8_t *)(&(identifier))+2))<<3)+((*((uint8_t *)(&(identifier))+1))>>5))
308 #define CAN_SET_EXT_ID_12_5( identifier) (((*((uint8_t *)(&(identifier))+1))<<3)+((* (uint8_t *)(&(identifier)) )>>5))
309 #define CAN_SET_EXT_ID_4_0( identifier) ((* (uint8_t *)(&(identifier)) )<<3)
310  // ----------
312 #define Can_set_ext_id(identifier) { CANIDT1 = CAN_SET_EXT_ID_28_21(identifier); \
313  CANIDT2 = CAN_SET_EXT_ID_20_13(identifier); \
314  CANIDT3 = CAN_SET_EXT_ID_12_5( identifier); \
315  CANIDT4 = CAN_SET_EXT_ID_4_0( identifier); \
316  CANCDMOB |= (1<<IDE); }
317  // ----------
319 #define Can_set_ext_msk(mask) { CANIDM1 = CAN_SET_EXT_ID_28_21(mask); \
320  CANIDM2 = CAN_SET_EXT_ID_20_13(mask); \
321  CANIDM3 = CAN_SET_EXT_ID_12_5( mask); \
322  CANIDM4 = CAN_SET_EXT_ID_4_0( mask); }
323  // ----------
324 
325 //_____ D E C L A R A T I O N S ________________________________________________
326 //------------------------------------------------------------------------------
327 // @fn can_clear_all_mob
339 extern void can_clear_all_mob(void);
340 
341 //------------------------------------------------------------------------------
342 // @fn can_get_mob_free
355 extern uint8_t can_get_mob_free(void);
356 
357 //------------------------------------------------------------------------------
358 // @fn can_get_mob_status
383 extern uint8_t can_get_mob_status(void);
384 
385 //------------------------------------------------------------------------------
386 // @fn can_get_data
397 extern void can_get_data(uint8_t* p_can_message_data);
398 
399 //------------------------------------------------------------------------------
400 // @fn can_auto_baudrate
415 extern uint8_t can_auto_baudrate(uint8_t eval);
416 
417 
418 //------------------------------------------------------------------------------
419 // @fn can_fixed_baudrate
431 extern uint8_t can_fixed_baudrate(uint8_t eval);
432 
433 //______________________________________________________________________________
434 
435 #endif // _CAN_DRV_H_
436 
437 
uint8_t can_get_mob_status(void)
Definition: can_drv.c:110
void can_get_data(uint8_t *p_can_message_data)
Definition: can_drv.c:153
uint8_t can_fixed_baudrate(uint8_t eval)
Definition: can_drv.c:395
uint8_t can_auto_baudrate(uint8_t eval)
Definition: can_drv.c:179
void can_clear_all_mob(void)
Definition: can_drv.c:42
uint8_t can_get_mob_free(void)
Definition: can_drv.c:66