Subversion Repositories HomeAutomation

Rev

Rev 2291 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  * CAN_driver.h
  3.  *
  4.  * Created: 10/17/2013 2:49:16 PM
  5.  *  Author: Scott_Schmit
  6.    
  7.     The contents of this file were copy & pasted from the CAN Software Library
  8.     on Atmel.com. The driver was written for AT90CANxx devices, but was modified
  9.     as an ATmegaxxM1 driver.
  10.  
  11.  */
  12.  
  13. //******************************************************************************
  14. //! @file $RCSfile: can_drv.h,v $
  15. //!
  16. //! Copyright (c) 2007 Atmel.
  17. //!
  18. //! Use of this program is subject to Atmel's End User License Agreement.
  19. //! Please read file license.txt for copyright notice.
  20. //!
  21. //! @brief This file contains the prototypes and the macros of the
  22. //!        low level functions (drivers) of:
  23. //!             - CAN (Controller Array Network)
  24. //!             - for AT90CAN128/64/32.
  25. //!
  26. //! This file can be parsed by Doxygen for automatic documentation generation.
  27. //! This file has been validated with AVRStudio-413528/WinAVR-20070122.
  28. //!
  29. //! @version $Revision: 3.20 $ $Name: jtellier $
  30. //!
  31. //! @todo
  32. //! @bug
  33. //******************************************************************************
  34.  
  35. #ifndef _CAN_DRV_H_
  36. #define _CAN_DRV_H_
  37.  
  38. #include "config.h"
  39. #include <avr/io.h>
  40.  
  41. //_____ I N C L U D E S ________________________________________________________
  42.  
  43. //_____ D E F I N I T I O N S __________________________________________________
  44. #define Max(a, b)          ( (a)>(b) ? (a) : (b) )
  45. #define Min(a, b)          ( (a)<(b) ? (a) : (b) )
  46. typedef uint8_t Bool;
  47.  
  48.     // ----------
  49. #ifndef F_CPU
  50. #  error  You must define F_CPU in "config.h" file
  51. #endif
  52.     // ----------
  53. #ifndef CAN_BAUDRATE
  54. #  error  You must define CAN_BAUDRATE in "config.h" file
  55. #endif
  56. #define CAN_AUTOBAUD    0
  57.     // ----------
  58. #if F_CPU == 16000000UL             //!< Fclkio = 16 MHz, Tclkio = 62.5 ns
  59. #   if   CAN_BAUDRATE == 100       //!< -- 100Kb/s, 16x Tscl, sampling at 75%
  60. #       define CONF_CANBT1  0x12       // Tscl  = 10x Tclkio = 625 ns
  61. #       define CONF_CANBT2  0x0C       // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
  62. #       define CONF_CANBT3  0x37       // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
  63. #   elif CAN_BAUDRATE == 125       //!< -- 125Kb/s, 16x Tscl, sampling at 75%
  64. #       define CONF_CANBT1  0x0E       // Tscl  = 8x Tclkio = 500 ns
  65. #       define CONF_CANBT2  0x0C       // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
  66. #       define CONF_CANBT3  0x37       // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
  67. #   elif CAN_BAUDRATE == 200       //!< -- 200Kb/s, 16x Tscl, sampling at 75%
  68. #       define CONF_CANBT1  0x08       // Tscl  = 5x Tclkio = 312.5 ns
  69. #       define CONF_CANBT2  0x0C       // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
  70. #       define CONF_CANBT3  0x37       // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
  71. #   elif CAN_BAUDRATE == 250       //!< -- 250Kb/s, 16x Tscl, sampling at 75%
  72. #       define CONF_CANBT1  0x06       // Tscl  = 4x Tclkio = 250 ns
  73. #       define CONF_CANBT2  0x0C       // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
  74. #       define CONF_CANBT3  0x37       // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
  75. #   elif CAN_BAUDRATE == 500       //!< -- 500Kb/s, 8x Tscl, sampling at 75%
  76. #       define CONF_CANBT1  0x06       // Tscl = 4x Tclkio = 250 ns
  77. #       define CONF_CANBT2  0x04       // Tsync = 1x Tscl, Tprs = 3x Tscl, Tsjw = 1x Tscl
  78. #       define CONF_CANBT3  0x13       // Tpsh1 = 2x Tscl, Tpsh2 = 2x Tscl, 3 sample points
  79. #   elif CAN_BAUDRATE == 1000      //!< -- 1 Mb/s, 8x Tscl, sampling at 75%
  80. #       define CONF_CANBT1  0x02       // Tscl  = 2x Tclkio = 125 ns
  81. #       define CONF_CANBT2  0x04       // Tsync = 1x Tscl, Tprs = 3x Tscl, Tsjw = 1x Tscl
  82. #       define CONF_CANBT3  0x13       // Tpsh1 = 2x Tscl, Tpsh2 = 2x Tscl, 3 sample points
  83. #   elif CAN_BAUDRATE == CAN_AUTOBAUD  // Treated later in the file
  84. #       define CONF_CANBT1  0x00       // Unused
  85. #       define CONF_CANBT2  0x00       // Unused
  86. #       define CONF_CANBT3  0x00       // Unused
  87. #   else
  88. #       error This CAN_BAUDRATE value is not in "CAN_driver.h" file
  89. #   endif
  90.  
  91. #elif F_CPU == 12000000UL           //!< Fclkio = 12 MHz, Tclkio = 83.333 ns
  92. #   if   CAN_BAUDRATE == 100       //!< -- 100Kb/s, 20x Tscl, sampling at 75%
  93. #       define CONF_CANBT1  0x0A       // Tscl  = 6x Tclkio = 500 ns
  94. #       define CONF_CANBT2  0x0E       // Tsync = 1x Tscl, Tprs = 8x Tscl, Tsjw = 1x Tscl
  95. #       define CONF_CANBT3  0x4B       // Tpsh1 = 6x Tscl, Tpsh2 = 5x Tscl, 3 sample points
  96. #   elif CAN_BAUDRATE == 125       //!< -- 125Kb/s, 16x Tscl, sampling at 75%
  97. #       define CONF_CANBT1  0x0A       // Tscl  = 6x Tclkio = 500 ns
  98. #       define CONF_CANBT2  0x0C       // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
  99. #       define CONF_CANBT3  0x37       // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
  100. #   elif CAN_BAUDRATE == 200       //!< -- 200Kb/s, 20x Tscl, sampling at 75%
  101. #       define CONF_CANBT1  0x04       // Tscl  = 3x Tclkio = 250 ns
  102. #       define CONF_CANBT2  0x0E       // Tsync = 1x Tscl, Tprs = 8x Tscl, Tsjw = 1x Tscl
  103. #       define CONF_CANBT3  0x4B       // Tpsh1 = 6x Tscl, Tpsh2 = 5x Tscl, 3 sample points
  104. #   elif CAN_BAUDRATE == 250       //!< -- 250Kb/s, 16x Tscl, sampling at 75%
  105. #       define CONF_CANBT1  0x04       // Tscl  = 3x Tclkio = 250 ns
  106. #       define CONF_CANBT2  0x0C       // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
  107. #       define CONF_CANBT3  0x37       // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
  108. #   elif CAN_BAUDRATE == 500       //!< -- 500Kb/s, 12x Tscl, sampling at 75%
  109. #       define CONF_CANBT1  0x02       // Tscl  = 2x Tclkio = 166.666 ns
  110. #       define CONF_CANBT2  0x08       // Tsync = 1x Tscl, Tprs = 5x Tscl, Tsjw = 1x Tscl
  111. #       define CONF_CANBT3  0x25       // Tpsh1 = 3x Tscl, Tpsh2 = 3x Tscl, 3 sample points
  112. #   elif CAN_BAUDRATE == 1000      //!< -- 1 Mb/s, 12x Tscl, sampling at 75%
  113. #       define CONF_CANBT1  0x00       // Tscl  = 1x Tclkio = 83.333 ns
  114. #       define CONF_CANBT2  0x08       // Tsync = 1x Tscl, Tprs = 5x Tscl, Tsjw = 1x Tscl
  115. #       define CONF_CANBT3  0x25       // Tpsh1 = 3x Tscl, Tpsh2 = 3x Tscl, 3 sample points
  116. #   elif CAN_BAUDRATE == CAN_AUTOBAUD  // Treated later in the file
  117. #       define CONF_CANBT1  0x00       // Unused
  118. #       define CONF_CANBT2  0x00       // Unused
  119. #       define CONF_CANBT3  0x00       // Unused
  120. #   else
  121. #       error This CAN_BAUDRATE value is not in "can_drv.h" file
  122. #   endif
  123.  
  124. #elif F_CPU == 8000000UL              //!< Fclkio = 8 MHz, Tclkio = 125 ns
  125. #   if   CAN_BAUDRATE == 100       //!< -- 100Kb/s, 16x Tscl, sampling at 75%
  126. #       define CONF_CANBT1  0x08       // Tscl  = 5x Tclkio = 625 ns
  127. #       define CONF_CANBT2  0x0C       // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
  128. #       define CONF_CANBT3  0x37       // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
  129. #   elif CAN_BAUDRATE == 125       //!< -- 125Kb/s, 16x Tscl, sampling at 75%
  130. #       define CONF_CANBT1  0x06       // Tscl  = 4x Tclkio = 500 ns
  131. #       define CONF_CANBT2  0x0C       // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
  132. #       define CONF_CANBT3  0x37       // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
  133. #   elif CAN_BAUDRATE == 200       //!< -- 200Kb/s, 20x Tscl, sampling at 75%
  134. #       define CONF_CANBT1  0x02       // Tscl  = 2x Tclkio = 250 ns
  135. #       define CONF_CANBT2  0x0E       // Tsync = 1x Tscl, Tprs = 8x Tscl, Tsjw = 1x Tscl
  136. #       define CONF_CANBT3  0x4B       // Tpsh1 = 6x Tscl, Tpsh2 = 5x Tscl, 3 sample points
  137. #   elif CAN_BAUDRATE == 250       //!< -- 250Kb/s, 16x Tscl, sampling at 75%
  138. #       define CONF_CANBT1  0x02       // Tscl  = 2x Tclkio = 250 ns
  139. #       define CONF_CANBT2  0x0C       // Tsync = 1x Tscl, Tprs = 7x Tscl, Tsjw = 1x Tscl
  140. #       define CONF_CANBT3  0x37       // Tpsh1 = 4x Tscl, Tpsh2 = 4x Tscl, 3 sample points
  141. #   elif CAN_BAUDRATE == 500       //!< -- 500Kb/s, 8x Tscl, sampling at 75%
  142. #       define CONF_CANBT1  0x02       // Tscl  = 2x Tclkio = 250 ns
  143. #       define CONF_CANBT2  0x04       // Tsync = 1x Tscl, Tprs = 3x Tscl, Tsjw = 1x Tscl
  144. #       define CONF_CANBT3  0x13       // Tpsh1 = 2x Tscl, Tpsh2 = 2x Tscl, 3 sample points
  145. #   elif CAN_BAUDRATE == 1000      //!< -- 1 Mb/s, 8x Tscl, sampling at 75%
  146. #       define CONF_CANBT1  0x00       // Tscl  = 1x Tclkio = 125 ns
  147. #       define CONF_CANBT2  0x04       // Tsync = 1x Tscl, Tprs = 3x Tscl, Tsjw = 1x Tscl
  148. #       define CONF_CANBT3  0x13       // Tpsh1 = 2x Tscl, Tpsh2 = 2x Tscl, 3 sample points
  149. #   elif CAN_BAUDRATE == CAN_AUTOBAUD  // Treated later in the file
  150. #       define CONF_CANBT1  0x00       // Unused
  151. #       define CONF_CANBT2  0x00       // Unused
  152. #       define CONF_CANBT3  0x00       // Unused
  153. #   else
  154. #       error This CAN_BAUDRATE value is not in "can_drv.h" file
  155. #   endif
  156.  
  157. #else
  158. #   error This F_CPU value is not in "can_drv.h" file
  159. #endif
  160.     // ----------
  161.     //! The first action of "can_bit_timing()" is to reset the CAN controller
  162.     //! "can_bit_timing()" lets the CAN controller disable
  163.     //! "can_bit_timing()" returns 1 if the setting of CANBTx registers is available
  164.     //!                and returns 0 if the setting of CANBTx registers is wrong
  165. #if     CAN_BAUDRATE == CAN_AUTOBAUD
  166. #error CAN auto baudrate not implemented
  167. //#   define Can_bit_timing(mode)  (can_auto_baudrate(mode) )
  168. #else
  169. #   define Can_bit_timing(mode)  (can_fixed_baudrate(mode))
  170. #endif
  171.     // ----------
  172. #define CAN_PORT_IN     PINC // edited for ATmegaxxM1 (not used?)
  173. #define CAN_PORT_DIR    DDRC // edited for ATmegaxxM1 (not used?)
  174. #define CAN_PORT_OUT    PORTC // edited for ATmegaxxM1 (not used?)
  175. #define CAN_INPUT_PIN   3 // edited for ATmegaxxM1 (not used?)
  176. #define CAN_OUTPUT_PIN  2 // edited for ATmegaxxM1 (not used?)
  177.     // ----------
  178. #define ERR_GEN_MSK ((1<<SERG)|(1<<CERG)|(1<<FERG)|(1<<AERG))            //! MaSK for GENeral ERRors INTerrupts
  179. #define INT_GEN_MSK ((1<<BOFFIT)|(1<<BXOK)|(ERR_GEN_MSK))                //! MaSK for GENeral INTerrupts
  180.  
  181. #define BRP_MSK     ((1<<BRP5)|(1<<BRP4)|(1<<BRP3)|(1<<BRP2)|(1<<BRP1)|(1<<BRP0))  //! Mask for BRP in CANBT1
  182. #define SJW_MSK     ((1<<SJW1)|(1<<SJW0))                                //! MaSK for SJW  in CANBT2
  183. #define PRS_MSK     ((1<<PRS2)|(1<<PRS1)|(1<<PRS0))                      //! MaSK for PRS  in CANBT2
  184. #define PHS2_MSK    ((1<<PHS22)|(1<<PHS21)|(1<<PHS20))                   //! MaSK for PHS2 in CANBT2
  185. #define PHS1_MSK    ((1<<PHS12)|(1<<PHS11)|(1<<PHS10))                   //! MaSK for PHS1 in CANBT2
  186. #define BRP         (BRP0)                                               //! BRP  in CANBT1
  187. #define SJW         (SJW0)                                               //! SJW  in CANBT2
  188. #define PRS         (PRS0)                                               //! PRS  in CANBT2
  189. #define PHS2        (PHS20)                                              //! PHS2 in CANBT2
  190. #define PHS1        (PHS10)                                              //! PHS1 in CANBT2
  191.  
  192. #define HPMOB_MSK   ((1<<HPMOB3)|(1<<HPMOB2)|(1<<HPMOB1)|(1<<HPMOB0))    //! MaSK for MOb in HPMOB
  193. #define MOBNB_MSK   ((1<<MOBNB3)|(1<<MOBNB2)|(1<<MOBNB1)|(1<<MOBNB0))    //! MaSK for MOb in CANPAGE
  194.  
  195. #define ERR_MOB_MSK ((1<<BERR)|(1<<SERR)|(1<<CERR)|(1<<FERR)|(1<<AERR))  //! MaSK for MOb ERRors
  196. #define INT_MOB_MSK ((1<<TXOK)|(1<<RXOK)|(1<<BERR)|(1<<SERR)|(1<<CERR)|(1<<FERR)|(1<<AERR)) //! MaSK for MOb INTerrupts
  197.  
  198. #define CONMOB_MSK  ((1<<CONMOB1)|(1<<CONMOB0))                          //! MaSK for CONfiguration MOb
  199. #define DLC_MSK     ((1<<DLC3)|(1<<DLC2)|(1<<DLC1)|(1<<DLC0))            //! MaSK for Data Length Coding
  200. #define CONMOB      (CONMOB0)                                            //! CONfiguration MOb
  201. #define DLC         (DLC0)                                               //! Data Length Coding
  202.     // ----------
  203. #define BRP_MIN     1       //! Prescaler of F_CPU (TQ generation)
  204. #define BRP_MAX     64
  205. #define NTQ_MIN     8       //! Number of TQ in one CAN bit
  206. #define NTQ_MAX     25
  207.                             //! True segment values in TQ
  208. #define PRS_MIN     1       //! Propagation segment
  209. #define PRS_MAX     8
  210. #define PHS1_MIN    2       //! Phase segment 1
  211. #define PHS1_MAX    8
  212. #define PHS2_MIN    2       //! Phase segment 2
  213. #define PHS2_MAX    8
  214. #define SJW_MIN     1       //! Synchro jump width
  215. #define SJW_MAX     4
  216.     // ----------
  217. //#define NB_MOB       15
  218. #define NB_MOB       6 // edited for ATmegaxxM1
  219. #define NB_DATA_MAX  8
  220. #define LAST_MOB_NB  (NB_MOB-1)
  221. #define NO_MOB       0xFF
  222.     // ----------
  223. //typedef enum {
  224. //        MOB_0,  MOB_1, MOB_2,  MOB_3,  MOB_4,  MOB_5,  MOB_6, MOB_7,
  225. //        MOB_8,  MOB_9, MOB_10, MOB_11, MOB_12, MOB_13, MOB_14        } can_mob_t;
  226. typedef enum {MOB_0,  MOB_1, MOB_2,  MOB_3,  MOB_4,  MOB_5} can_mob_t;// edited for ATmegaxxM1
  227.     // ----------
  228. #define STATUS_CLEARED            0x00
  229.     // ----------
  230. #define MOB_NOT_COMPLETED         0x00                                              // 0x00
  231. #define MOB_TX_COMPLETED        (1<<TXOK)                                           // 0x40
  232. #define MOB_RX_COMPLETED        (1<<RXOK)                                           // 0x20
  233. #define MOB_RX_COMPLETED_DLCW  ((1<<RXOK)|(1<<DLCW))                                // 0xA0
  234. #define MOB_ACK_ERROR           (1<<AERR)                                           // 0x01
  235. #define MOB_FORM_ERROR          (1<<FERR)                                           // 0x02
  236. #define MOB_CRC_ERROR           (1<<CERR)                                           // 0x04
  237. #define MOB_STUFF_ERROR         (1<<SERR)                                           // 0x08
  238. #define MOB_BIT_ERROR           (1<<BERR)                                           // 0x10
  239. #define MOB_PENDING            ((1<<RXOK)|(1<<TXOK))                                // 0x60
  240. #define MOB_NOT_REACHED        ((1<<AERR)|(1<<FERR)|(1<<CERR)|(1<<SERR)|(1<<BERR))  // 0x1F
  241. #define MOB_DISABLE               0xFF                                              // 0xFF
  242.     // ----------
  243. #define MOB_Tx_ENA  1
  244. #define MOB_Rx_ENA  2
  245. #define MOB_Rx_BENA 3
  246.     // ----------
  247.  
  248. //_____ M A C R O S ____________________________________________________________
  249.  
  250.     // ----------
  251. #define Can_reset()       ( CANGCON  =  (1<<SWRES) )
  252. #define Can_enable()      ( CANGCON |=  (1<<ENASTB))
  253. #define Can_disable()     ( CANGCON &= ~(1<<ENASTB))
  254.     // ----------
  255. #define Can_full_abort()  { CANGCON |=  (1<<ABRQ); CANGCON &= ~(1<<ABRQ); }
  256.     // ----------
  257. #define Can_conf_bt()     { CANBT1=CONF_CANBT1; CANBT2=CONF_CANBT2; CANBT3=CONF_CANBT3; }
  258.     // ----------
  259. #define Can_set_mob(mob)       { CANPAGE = ((mob) << 4);}
  260. #define Can_set_mask_mob()     {  CANIDM4=0xFF; CANIDM3=0xFF; CANIDM2=0xFF; CANIDM1=0xFF; }
  261. #define Can_clear_mask_mob()   {  CANIDM4=0x00; CANIDM3=0x00; CANIDM2=0x00; CANIDM1=0x00; }
  262. #define Can_clear_status_mob() { CANSTMOB=0x00; }
  263. #define Can_clear_mob()        { uint8_t  volatile *__i_; for (__i_=&CANSTMOB; __i_<&CANSTML; __i_++) { *__i_=0x00 ;}}
  264.     // ----------
  265. #define Can_mob_abort()   ( DISABLE_MOB )
  266.     // ----------
  267. #define Can_set_dlc(dlc)  ( CANCDMOB |= (dlc)        )
  268. #define Can_set_ide()     ( CANCDMOB |= (1<<IDE)     )
  269. #define Can_set_rtr()     ( CANIDT4  |= (1<<RTRTAG)  )
  270. #define Can_set_rplv()    ( CANCDMOB |= (1<<RPLV)    )
  271.     // ----------
  272. #define Can_clear_dlc()   ( CANCDMOB &= ~DLC_MSK     )
  273. #define Can_clear_ide()   ( CANCDMOB &= ~(1<<IDE)    )
  274. #define Can_clear_rtr()   ( CANIDT4  &= ~(1<<RTRTAG) )
  275. #define Can_clear_rplv()  ( CANCDMOB &= ~(1<<RPLV)   )
  276.     // ----------
  277. #define DISABLE_MOB       ( CANCDMOB &= (~CONMOB_MSK) )
  278. #define Can_config_tx()        { DISABLE_MOB; CANCDMOB |= (MOB_Tx_ENA  << CONMOB); }
  279. #define Can_config_rx()        { DISABLE_MOB; CANCDMOB |= (MOB_Rx_ENA  << CONMOB); }
  280. #define Can_config_rx_buffer() {              CANCDMOB |= (MOB_Rx_BENA << CONMOB); }
  281.     // ----------
  282. #define Can_get_dlc()      ((CANCDMOB &  DLC_MSK)     >> DLC   )
  283. #define Can_get_ide()      ((CANCDMOB &  (1<<IDE))    >> IDE   )
  284. #define Can_get_rtr()      ((CANIDT4  &  (1<<RTRTAG)) >> RTRTAG)
  285.     // ----------
  286. #define Can_set_rtrmsk()   ( CANIDM4 |= (1<<RTRMSK) )
  287. #define Can_set_idemsk()   ( CANIDM4 |= (1<<IDEMSK) )
  288.     // ----------
  289. #define Can_clear_rtrmsk() ( CANIDM4 &= ~(1<<RTRMSK) )
  290. #define Can_clear_idemsk() ( CANIDM4 &= ~(1<<IDEMSK) )
  291.     // ----------
  292.                 //!< STD ID TAG Reading
  293. #define Can_get_std_id(identifier)  { *((uint8_t *)(&(identifier))+1) =  CANIDT1>>5              ; \
  294.                                       *((uint8_t *)(&(identifier))  ) = (CANIDT2>>5)+(CANIDT1<<3); }
  295.     // ----------
  296.                 //!< EXT ID TAG Reading
  297. #define Can_get_ext_id(identifier)  { *((uint8_t *)(&(identifier))+3) =  CANIDT1>>3              ; \
  298.                                       *((uint8_t *)(&(identifier))+2) = (CANIDT2>>3)+(CANIDT1<<5); \
  299.                                       *((uint8_t *)(&(identifier))+1) = (CANIDT3>>3)+(CANIDT2<<5); \
  300.                                       *((uint8_t *)(&(identifier))  ) = (CANIDT4>>3)+(CANIDT3<<5); }
  301.     // ----------
  302.                 //!< STD ID Construction
  303. #define CAN_SET_STD_ID_10_4(identifier)  (((*((uint8_t *)(&(identifier))+1))<<5)+((* (uint8_t *)(&(identifier)))>>3))
  304. #define CAN_SET_STD_ID_3_0( identifier)  (( * (uint8_t *)(&(identifier))   )<<5)
  305.     // ----------
  306.                 //!< STD ID TAG writing
  307. #define Can_set_std_id(identifier)  { CANIDT1   = CAN_SET_STD_ID_10_4(identifier); \
  308.                                       CANIDT2   = CAN_SET_STD_ID_3_0( identifier); \
  309.                                       CANCDMOB &= (~(1<<IDE))                    ; }
  310.     // ----------
  311.                 //!< STD ID MASK writing
  312. #define Can_set_std_msk(mask)       { CANIDM1   = CAN_SET_STD_ID_10_4(mask); \
  313.                                       CANIDM2   = CAN_SET_STD_ID_3_0( mask); }
  314.     // ----------
  315.                 //!< EXT ID Construction
  316. #define CAN_SET_EXT_ID_28_21(identifier)  (((*((uint8_t *)(&(identifier))+3))<<3)+((*((uint8_t *)(&(identifier))+2))>>5))
  317. #define CAN_SET_EXT_ID_20_13(identifier)  (((*((uint8_t *)(&(identifier))+2))<<3)+((*((uint8_t *)(&(identifier))+1))>>5))
  318. #define CAN_SET_EXT_ID_12_5( identifier)  (((*((uint8_t *)(&(identifier))+1))<<3)+((* (uint8_t *)(&(identifier))   )>>5))
  319. #define CAN_SET_EXT_ID_4_0(  identifier)   ((* (uint8_t *)(&(identifier))   )<<3)
  320.     // ----------
  321.                 //!< EXT ID TAG writing
  322. #define Can_set_ext_id(identifier)  { CANIDT1   = CAN_SET_EXT_ID_28_21(identifier); \
  323.                                       CANIDT2   = CAN_SET_EXT_ID_20_13(identifier); \
  324.                                       CANIDT3   = CAN_SET_EXT_ID_12_5( identifier); \
  325.                                       CANIDT4   = CAN_SET_EXT_ID_4_0(  identifier); \
  326.                                       CANCDMOB |= (1<<IDE);                         }
  327.     // ----------
  328.                 //!< EXT ID MASK writing
  329. #define Can_set_ext_msk(mask)       { CANIDM1   = CAN_SET_EXT_ID_28_21(mask); \
  330.                                       CANIDM2   = CAN_SET_EXT_ID_20_13(mask); \
  331.                                       CANIDM3   = CAN_SET_EXT_ID_12_5( mask); \
  332.                                       CANIDM4   = CAN_SET_EXT_ID_4_0(  mask); }
  333.     // ----------
  334.  
  335. //_____ D E C L A R A T I O N S ________________________________________________
  336.  
  337. //------------------------------------------------------------------------------
  338. //  @fn can_clear_all_mob
  339. //!
  340. //! This function clears the Mailbox content.
  341. //! It uses Can_clear_mob() macro and clears data FIFO o MOb[0] upto
  342. //! MOb[LAST_MOB_NB].
  343. //!
  344. //! @warning none.
  345. //!
  346. //! @param none.
  347. //!
  348. //! @return none.
  349. //!
  350. extern  void can_clear_all_mob(void);
  351.  
  352. //------------------------------------------------------------------------------
  353. //  @fn can_get_mob_free
  354. //!
  355. //! This function returns the number of the first MOb available or 0xFF if
  356. //! no MOb available.
  357. //!
  358. //! @warning none.
  359. //!
  360. //! @param none.
  361. //!
  362. //! @return Handle of MOb.
  363. //!          - MOb[0] upto MOb[LAST_MOB_NB]
  364. //!          - 0xFF if no MOb
  365. //!
  366. extern  uint8_t can_get_mob_free(void);
  367.  
  368. //------------------------------------------------------------------------------
  369. //  @fn can_get_mob_status
  370. //!
  371. //! This function returns information "MOB completed its job"
  372. //! if one of the RXOK or TXOK Flag is set or "MOB not completed its job
  373. //! if no RXOK and TXOK flags are set.
  374. //! Previously, this function checks if the MOb is configured or not and in
  375. //!  case of the MOB not configured, the function returns "MOB_DISABLE".
  376. //!
  377. //! @warning none.
  378. //!
  379. //! @param none.
  380. //!
  381. //! @return MOb Status.
  382. //!          -  MOB_NOT_COMPLETED
  383. //!          -  MOB_TX_COMPLETED
  384. //!          -  MOB_RX_COMPLETED
  385. //!          -  MOB_RX_DLC_WARNING
  386. //!          -  MOB_DISABLE
  387. //!          or should be a combination of the following errors
  388. //!          -  MOB_ACK_ERROR
  389. //!          -  MOB_FORM_ERROR
  390. //!          -  MOB_CRC_ERROR
  391. //!          -  MOB_STUFF_ERROR
  392. //!          -  MOB_BIT_ERROR
  393. //!
  394. extern  uint8_t can_get_mob_status(void);
  395.  
  396. //------------------------------------------------------------------------------
  397. //  @fn can_get_data
  398. //!
  399. //! This function copy the data from the selected MOb to the address
  400. //! passed as parameter.
  401. //!
  402. //! @warning none.
  403. //!
  404. //! @param CAN message data address.
  405. //!
  406. //! @return none.
  407. //!
  408. extern  void can_get_data(uint8_t* p_can_message_data);
  409.  
  410.  
  411. //------------------------------------------------------------------------------
  412. //  @fn can_fixed_baudrate
  413. //!
  414. //! This function programs the CANBTx registers with the predefined values
  415. //! CONF_CANBT1, CONF_CANBT2, CONF_CANBT3.
  416. //!
  417. //! @warning
  418. //!
  419. //! @param (unused!)
  420. //!
  421. //! @return Baudrate Status
  422. //!         fixed = 1: baudrate performed
  423. //!
  424. extern uint8_t can_fixed_baudrate(uint8_t eval);
  425.  
  426. //______________________________________________________________________________
  427.  
  428. #endif // _CAN_DRV_H_
  429.