Subversion Repositories HomeAutomation

Rev

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

  1. #ifndef MCP2510_H_
  2. #define MCP2510_H_
  3.  
  4. /* -----------------------------------------------------------------------------
  5.  * Includes
  6.  * ---------------------------------------------------------------------------*/
  7. #include <inttypes.h>
  8.  
  9. /* -----------------------------------------------------------------------------
  10.  * Function prototypes
  11.  * ---------------------------------------------------------------------------*/
  12. void MCP_init(void);
  13. void MCP_write(uint8_t value, uint8_t address);
  14. uint8_t MCP_read(uint8_t address);
  15.  
  16.  
  17. /* -----------------------------------------------------------------------------
  18.  * "OP-codes" used when communicating with the MCP. All communication starts
  19.  * with one of these bytes.
  20.  * ---------------------------------------------------------------------------*/
  21. #define MCP_OP_RESET    0xC0    /**< Reset operation. Resets internal registers to default state. */
  22. #define MCP_OP_READ     0x03    /**< Read operation. Reads data from register beginning at selected address. */
  23. #define MCP_OP_WRITE    0x02    /**< Write operation. Writes data to register beginning at selected address. */
  24. #define MCP_OP_BITMOD   0X05    /**< Bit modify operation. Writes data to certain bits in a register. */
  25.  
  26.  
  27.  
  28. /* -----------------------------------------------------------------------------
  29.  * Macros for register operation
  30.  * ---------------------------------------------------------------------------*/
  31.  
  32. #define MCP_REG_OPEN(reg) {\
  33.         uint8_t __tmpdata = MCP_read(reg);  /* save reg contents */ \
  34.         uint8_t __tmpaddr = reg;            /* save reg address */
  35.        
  36.  
  37. #define MCP_REG_CLOSE()\
  38.         MCP_write(__tmpdata,__tmpaddr);     /* write back to reg */ \
  39.         }
  40.  
  41. /**
  42.  * Writes a value to a bitmask in a register. The macro will automatically read
  43.  * the current contents of the register into a temporary variable. The bitfield
  44.  * will then be set in this variable, after which the variable is written back
  45.  * to the register again.
  46.  *
  47.  * @param reg The register address, for example RXB0CTRL.
  48.  * @param field The bitfield position, for example RXB0CTRL_RXM.
  49.  * @value value The new bitfield value.
  50.  */
  51. #define MCP_WRITE_B(reg,field,value)\
  52.     { \
  53.         uint8_t __tmpreg = MCP_read(reg); /* read current register contents */ \
  54.         __tmpreg &= ~((_##field##_M)<<(_##field##_B)); /* clear all bits in field */ \
  55.         __tmpreg |= (((value)<<(_##field##_B))&_##field##_M); /* shift in new value */ \
  56.         MCP_write(__tmpreg,reg); /* write back to reg */ \
  57.     }
  58.  
  59. /**
  60.  * Writes a new value to a register.
  61.  *
  62.  * @param reg The register address.
  63.  * @param value The new register contents.
  64.  */
  65. #define MCP_WRITE_R(reg,value) MCP_write(value,reg)
  66.  
  67.  
  68. /* -----------------------------------------------------------------------------
  69.  * Register definitions (addresses, bitfield positions, bitfield masks).
  70.  * These definitions are used by the macros above. The register numbers are
  71.  * references to the MCP2510 data sheet.
  72.  * ---------------------------------------------------------------------------*/
  73.  
  74. /* reg 3-1 */
  75. #define TXBnCTRL(n)             (0x30+n*0x10)
  76. #define TXBnCTRL_ABTF
  77. #define _TXBnCTRL_ABTF_B        6
  78. #define _TXBnCTRL_ABTF_M        0x40
  79. #define TXBnCTRL_MLOA
  80. #define _TXBnCTRL_MLOA_B        5
  81. #define _TXBnCTRL_MLOA_M        0x20
  82. #define TXBnCTRL_TXERR
  83. #define _TXBnCTRL_TXERR_B       4
  84. #define _TXBnCTRL_TXERR_M       0x10
  85. #define TXBnCTRL_TXREQ
  86. #define _TXBnCTRL_TXREQ_B       3
  87. #define _TXBnCTRL_TXREQ_M       0x08
  88. #define TXBnCTRL_TXP
  89. #define _TXBnCTRL_TXP_B         0
  90. #define _TXBnCTRL_TXP_M         0x03
  91.  
  92. /* reg 3-2 */
  93. #define TXRTSCTRL               (0x0D)
  94. #define TXRTSCTRL_B2RTS
  95. #define _TXRTSCTRL_B2RTS_B      5
  96. #define _TXRTSCTRL_B2RTS_M      0x20
  97. #define TXRTSCTRL_B1RTS
  98. #define _TXRTSCTRL_B1RTS_B      4
  99. #define _TXRTSCTRL_B1RTS_M      0x10
  100. #define TXRTSCTRL_B0RTS
  101. #define _TXRTSCTRL_B0RTS_B      3
  102. #define _TXRTSCTRL_B0RTS_M      0x08
  103. #define TXRTSCTRL_B2RTSM
  104. #define _TXRTSCTRL_B2RTSM_B     2
  105. #define _TXRTSCTRL_B2RTSM_M     0x04
  106. #define TXRTSCTRL_B1RTSM
  107. #define _TXRTSCTRL_B1RTSM_B     1
  108. #define _TXRTSCTRL_B1RTSM_M     0x02
  109. #define TXRTSCTRL_B0RTSM
  110. #define _TXRTSCTRL_B0RTSM_B     0
  111. #define _TXRTSCTRL_B0RTSM_M     0x01
  112.  
  113. /* reg 3-3 */
  114. #define TXBnSIDH(n)             (0x31+n*0x10)
  115. #define TXBnSIDH_SID
  116. #define _TXBnSIDH_SID_B         0
  117. #define _TXBnSIDH_SID_M         0xff
  118.  
  119. /* reg 3-4 */
  120. #define TXBnSIDL(n)             (0x32+n*0x10)
  121. #define TXBnSIDL_SID
  122. #define _TXBnSIDL_SID_B         5
  123. #define _TXBnSIDL_SID_M         0xe0
  124. #define TXBnSIDL_EXIDE
  125. #define _TXBnSIDL_EXIDE_B       3
  126. #define _TXBnSIDL_EXIDE_M       0x08
  127. #define TXBnSIDL_EID
  128. #define _TXBnSIDL_EID_B         0
  129. #define _TXBnSIDL_EID_M         0x03
  130.  
  131. /* reg 3-5 */
  132. #define TXBnEID8(n)             (0x33+n*0x10)
  133. #define TXBnEID8_EID
  134. #define _TXBnEID8_EID_B         0
  135. #define _TXBnEID8_EID_M         0xff
  136.  
  137. /* reg 3-6 */
  138. #define TXBnEID0(n)             (0x34+n*0x10)
  139. #define TXBnEID0_EID
  140. #define _TXBnEID0_EID_B         0
  141. #define _TXBnEID0_EID_M         0xff
  142.  
  143. /* reg 3-7 */
  144. #define TXBnDLC(n)              (0x35+n*0x10)
  145. #define TXBnDLC_RTR
  146. #define _TXBnDLC_RTR_B          6
  147. #define _TXBnDLC_RTR_M          0x40
  148. #define TXBnDLC_DLC
  149. #define _TXBnDLC_DLC_B          0
  150. #define _TXBnDLC_DLC_M          0x0f
  151.  
  152. /* regs 3-8 are just data bytes */
  153. #define TXBnDm(n,m)             ((0x36+n*0x10)+m*0x01)  /* txbuf n, byte m */
  154.  
  155. /* reg 4-1 */
  156. #define RXB0CTRL                (0x60)
  157. #define RXB0CTRL_RXM
  158. #define _RXB0CTRL_RXM_B         5
  159. #define _RXB0CTRL_RXM_M         0x60
  160. #define RXB0CTRL_RXRTR
  161. #define _RXB0CTRL_RXRTR_B       3
  162. #define _RXB0CTRL_RXRTR_M       0x08
  163. #define RXB0CTRL_BUKT
  164. #define _RXB0CTRL_BUKT_B        2
  165. #define _RXB0CTRL_BUKT_M        0x04
  166. #define RXB0CTRL_FILHIT
  167. #define _RXB0CTRL_FILHIT_B      0
  168. #define _RXB0CTRL_FILHIT_M      0x01
  169.  
  170. /* reg 4-2 */
  171. #define RXB1CTRL                (0x70)
  172. #define RXB1CTRL_RXM
  173. #define _RXB1CTRL_RXM_B         5
  174. #define _RXB1CTRL_RXM_M         0x60
  175. #define RXB1CTRL_RXRTR
  176. #define _RXB1CTRL_RXRTR_B       3
  177. #define _RXB1CTRL_RXRTR_M       0x08
  178. #define RXB1CTRL_FILHIT
  179. #define _RXB1CTRL_FILHIT_B      0
  180. #define _RXB1CTRL_FILHIT_M      0x07
  181.  
  182. /* reg 4-3 */
  183. #define BFPCTRL                 (0x0C)
  184. #define BFPCTRL_B1BFS
  185. #define _BFPCTRL_B1BFS_B        5
  186. #define _BFPCTRL_B1BFS_M        0x20
  187. #define BFPCTRL_B0BFS
  188. #define _BFPCTRL_B0BFS_B        4
  189. #define _BFPCTRL_B0BFS_M        0x10
  190. #define BFPCTRL_B1BFE
  191. #define _BFPCTRL_B1BFE_B        3
  192. #define _BFPCTRL_B1BFE_M        0x08
  193. #define BFPCTRL_B0BFE
  194. #define _BFPCTRL_B0BFE_B        2
  195. #define _BFPCTRL_B0BFE_M        0x04
  196. #define BFPCTRL_B1BFM
  197. #define _BFPCTRL_B1BFM_B        1
  198. #define _BFPCTRL_B1BFM_M        0x02
  199. #define BFPCTRL_B0BFM
  200. #define _BFPCTRL_B0BFM_B        0
  201. #define _BFPCTRL_B0BFM_M        0x01
  202.  
  203. /* reg 4-4 */
  204. #define RXBnSIDH(n)             (0x61+n*0x10)
  205. #define RXBnSIDH_SID
  206. #define _RXBnSIDH_SID_B         0
  207. #define _RXBnSIDH_SID_M         0xff
  208.  
  209. /* reg 4-5 */
  210. #define RXBnSIDL(n)             (0x62+n*0x10)
  211. #define RXBnSIDL_SID
  212. #define _RXBnSIDL_SID_B         5
  213. #define _RXBnSIDL_SID_M         0xe0
  214. #define RXBnSIDL_SRR
  215. #define _RXBnSIDL_SRR_B         4
  216. #define _RXBnSIDL_SRR_M         0x10
  217. #define RXBnSIDL_IDE
  218. #define _RXBnSIDL_IDE_B         3
  219. #define _RXBnSIDL_IDE_M         0x08
  220. #define RXBnSIDL_EID
  221. #define _RXBnSIDL_EID_B         0
  222. #define _RXBnSIDL_EID_M         0x03
  223.  
  224. /* reg 4-6 */
  225. #define RXBnEID8(n)             (0x63+n*0x10)
  226. #define RXBnEID8_EID
  227. #define _RXBnEID8_EID_B         0
  228. #define _RXBnEID8_EID_M         0xff
  229.  
  230. /* reg 4-7 */
  231. #define RXBnEID0(n)             (0x64+n*0x10)
  232. #define RXBnEID0_EID
  233. #define _RXBnEID0_EID_B         0
  234. #define _RXBnEID0_EID_M         0xff
  235.  
  236. /* reg 4-8 */
  237. #define RXBnDLC(n)              (0x65+n*0x10)
  238. #define RXBnDLC_RTR
  239. #define _RXBnDLC_RTR_B          6
  240. #define _RXBnDLC_RTR_M          0x40
  241. #define RXBnDLC_RB
  242. #define _RXBnDLC_RB_B           4
  243. #define _RXBnDLC_RB_M           0x30
  244. #define RXBnDLC_DLC
  245. #define _RXBnDLC_DLC_B          0
  246. #define _RXBnDLC_DLC_M          0x0f
  247.  
  248. /* regs 4-9 are just data bytes */
  249. #define RXBnDm(n,m)             ((0x66+n*0x10)+m*0x01)  /* rxbuf n, byte m */
  250.  
  251. /* reg 4-10 */
  252. #define RXF0SIDH                (0x00)
  253. #define RXF1SIDH                (0x04)
  254. #define RXF2SIDH                (0x08)
  255. #define RXF3SIDH                (0x10)
  256. #define RXF4SIDH                (0x14)
  257. #define RXF5SIDH                (0x18)
  258. #define RXFnSIDH_SID
  259. #define _RXFnSIDH_SID_B         0
  260. #define _RXFnSIDH_SID_M         0xffffffff
  261.  
  262. /* reg 4-11 */
  263. #define RXF0SIDL                (0x01)
  264. #define RXF1SIDL                (0x05)
  265. #define RXF2SIDL                (0x09)
  266. #define RXF3SIDL                (0x11)
  267. #define RXF4SIDL                (0x15)
  268. #define RXF5SIDL                (0x19)
  269. #define RXFnSIDL_SID
  270. #define _RXFnSIDL_SID_B         5
  271. #define _RXFnSIDL_SID_M         0xe0
  272. #define RXFnSIDL_EXIDE
  273. #define _RXFnSIDL_EXIDE_B       3
  274. #define _RXFnSIDL_EXIDE_M       0x08
  275. #define RXFnSIDL_EID
  276. #define _RXFnSIDL_EID_B         0
  277. #define _RXFnSIDL_EID_M         0x03
  278.  
  279. /* reg 4-12 */
  280. #define RXF0EID8                (0x02)
  281. #define RXF1EID8                (0x06)
  282. #define RXF2EID8                (0x0A)
  283. #define RXF3EID8                (0x12)
  284. #define RXF4EID8                (0x16)
  285. #define RXF5EID8                (0x1A)
  286. #define RXFnEID8_EID
  287. #define _RXFnEID8_EID_B         0
  288. #define _RXFnEID8_EID_M         0xff
  289.  
  290. /* reg 4-13 */
  291. #define RXF0EID0                (0x03)
  292. #define RXF1EID0                (0x07)
  293. #define RXF2EID0                (0x0B)
  294. #define RXF3EID0                (0x13)
  295. #define RXF4EID0                (0x17)
  296. #define RXF5EID0                (0x1B)
  297. #define RXFnEID0_EID
  298. #define _RXFnEID0_EID_B         0
  299. #define _RXFnEID0_EID_M         0xff
  300.  
  301. /* reg 4-14 */
  302. #define RXMnSIDH(n)             (0x20+n*0x04)
  303. #define RXMnSIDH_SID
  304. #define _RXMnSIDH_SID_B         0
  305. #define _RXMnSIDH_SID_M         0xff
  306.  
  307. /* reg 4-15 */
  308. #define RXMnSIDL(n)             (0x21+n*0x04)
  309. #define RXMnSIDL_SID
  310. #define _RXMnSIDL_SID_B         5
  311. #define _RXMnSIDL_SID_M         0xe0
  312. #define RXMnSIDL_EID
  313. #define _RXMnSIDL_EID_B         0
  314. #define _RXMnSIDL_EID_M         0x03
  315.  
  316. /* reg 4-16 */
  317. #define RXMnEID8(n)             (0x22+n*0x04)
  318. #define RXMnEID8_EID
  319. #define _RXMnEID8_EID_B         0
  320. #define _RXMnEID8_EID_M         0xff
  321.  
  322. /* reg 4-18 */
  323. #define RXMnEID0(n)             (0x23+n*0x04)
  324. #define RXMnEID0_EID
  325. #define _RXMnEID0_EID_B         0
  326. #define _RXMnEID0_EID_M         0xff
  327.  
  328. /* reg 5-1 */
  329. #define CNF1                    (0x2A)
  330. #define CNF1_SJW
  331. #define _CNF1_SJW_B             6
  332. #define _CNF1_SJW_M             0xc0
  333. #define CNF1_BRP
  334. #define _CNF1_BRP_B             0
  335. #define _CNF1_BRP_M             0x3f
  336.  
  337. /* reg 5-2 */
  338. #define CNF2                    (0x29)
  339. #define CNF2_BTLMODE
  340. #define _CNF2_BTLMODE_B         7
  341. #define _CNF2_BTLMODE_M         0x80
  342. #define CNF2_SAM
  343. #define _CNF2_SAM_B             6
  344. #define _CNF2_SAM_M             0x40
  345. #define CNF2_PHSEG1
  346. #define _CNF2_PHSEG1_B          3
  347. #define _CNF2_PHSEG1_M          0x38
  348. #define CNF2_PRSEG
  349. #define _CNF2_PRSEG_B           0
  350. #define _CNF2_PRSEG_M           0x07
  351.  
  352. /* reg 5-3 */
  353. #define CNF3                    (0x28)
  354. #define CNF3_WAKFIL
  355. #define _CNF3_WAKFIL_B          6
  356. #define _CNF3_WAKFIL_M          0x40
  357. #define CNF3_PHSEG2
  358. #define _CNF3_PHSEG2_B          0
  359. #define _CNF3_PHSEG2_M          0x07
  360.  
  361. /* reg 6-1 */
  362. #define TEC                     (0x1C)
  363. #define TEC_TEC
  364. #define _TEC_TEC_B              0
  365. #define _TEC_TEC_M              0xff
  366.  
  367. /* reg 6-2 */
  368. #define REC                     (0x1D)
  369. #define REC_REC
  370. #define _REC_REC_B              0
  371. #define _REC_REC_M              0xff
  372.  
  373. /* reg 6-3 */
  374. #define EFLG                    (0x2D)
  375. #define EFLG_RX1OVR
  376. #define _EFLG_RX1OVR_B          7
  377. #define _EFLG_RX1OVR_M          0x80
  378. #define EFLG_RX0OVR
  379. #define _EFLG_RX0OVR_B          6
  380. #define _EFLG_RX0OVR_M          0x40
  381. #define EFLG_TXBO
  382. #define _EFLG_TXBO_B            5
  383. #define _EFLG_TXBO_M            0x20
  384. #define EFLG_TXEP
  385. #define _EFLG_TXEP_B            4
  386. #define _EFLG_TXEP_M            0x10
  387. #define EFLG_RXEP
  388. #define _EFLG_RXEP_B            3
  389. #define _EFLG_RXEP_M            0x08
  390. #define EFLG_TXWAR
  391. #define _EFLG_TXWAR_B           2
  392. #define _EFLG_TXWAR_M           0x04
  393. #define EFLG_RXWAR
  394. #define _EFLG_RXWAR_B           1
  395. #define _EFLG_RXWAR_M           0x02
  396. #define EFLG_EWARN
  397. #define _EFLG_EWARN_B           0
  398. #define _EFLG_EWARN_M           0xb01
  399.  
  400. /* reg 7-1 */
  401. #define CANINTE                 (0x2B)
  402. #define CANINTE_MERRE
  403. #define _CANINTE_MERRE_B        7
  404. #define _CANINTE_MERRE_M        0x80
  405. #define CANINTE_WAKIE
  406. #define _CANINTE_WAKIE_B        6
  407. #define _CANINTE_WAKIE_M        0x40
  408. #define CANINTE_ERRIE
  409. #define _CANINTE_ERRIE_B        5
  410. #define _CANINTE_ERRIE_M        0x20
  411. #define CANINTE_TX2IE
  412. #define _CANINTE_TX2IE_B        4
  413. #define _CANINTE_TX2IE_M        0x10
  414. #define CANINTE_TX1IE
  415. #define _CANINTE_TX1IE_B        3
  416. #define _CANINTE_TX1IE_M        0x08
  417. #define CANINTE_TX0IE
  418. #define _CANINTE_TX0IE_B        2
  419. #define _CANINTE_TX0IE_M        0x04
  420. #define CANINTE_RX1IE
  421. #define _CANINTE_RX1IE_B        1
  422. #define _CANINTE_RX1IE_M        0x02
  423. #define CANINTE_RX0IE
  424. #define _CANINTE_RX0IE_B        0
  425. #define _CANINTE_RX0IE_M        0x01
  426.  
  427. /* reg 7-2 */
  428. #define CANINTF                 (0x2C)
  429. #define CANINTF_MERRF
  430. #define _CANINTF_MERRF_B        7
  431. #define _CANINTF_MERRF_M        0x80
  432. #define CANINTF_WAKIF
  433. #define _CANINTF_WAKIF_B        6
  434. #define _CANINTF_WAKIF_M        0x40
  435. #define CANINTF_ERRIF
  436. #define _CANINTF_ERRIF_B        5
  437. #define _CANINTF_ERRIF_M        0x20
  438. #define CANINTF_TX2IF
  439. #define _CANINTF_TX2IF_B        4
  440. #define _CANINTF_TX2IF_M        0x10
  441. #define CANINTF_TX1IF
  442. #define _CANINTF_TX1IF_B        3
  443. #define _CANINTF_TX1IF_M        0x08
  444. #define CANINTF_TX0IF
  445. #define _CANINTF_TX0IF_B        2
  446. #define _CANINTF_TX0IF_M        0x04
  447. #define CANINTF_RX1IF
  448. #define _CANINTF_RX1IF_B        1
  449. #define _CANINTF_RX1IF_M        0x02
  450. #define CANINTF_RX0IF
  451. #define _CANINTF_RX0IF_B        0
  452. #define _CANINTF_RX0IF_M        0x01
  453.  
  454. /* reg 9-1 */
  455. #define CANCTRL                 (0x0F)
  456. #define CANCTRL_REQOP
  457. #define _CANCTRL_REQOP_B        5
  458. #define _CANCTRL_REQOP_M        0xe0
  459. #define CANCTRL_ABAT
  460. #define _CANCTRL_ABAT_B         4
  461. #define _CANCTRL_ABAT_M         0x10
  462. #define CANCTRL_CLKEN
  463. #define _CANCTRL_CLKEN_B        2
  464. #define _CANCTRL_CLKEN_M        0x04
  465. #define CANCTRL_CLKPRE
  466. #define _CANCTRL_CLKPRE_B       0
  467. #define _CANCTRL_CLKPRE_M       0x03
  468.  
  469. /* reg 9-2 */
  470. #define CANSTAT                 (0x0E)
  471. #define CANSTAT_OPMOD
  472. #define _CANSTAT_OPMOD_B        5
  473. #define _CANSTAT_OPMOD_M        0xe0
  474. #define CANSTAT_ICOD
  475. #define _CANSTAT_ICOD_B         1
  476. #define _CANSTAT_ICOD_M         0x0e
  477.  
  478.  
  479. /* -----------------------------------------------------------------------------
  480.  * Various other useful definitions.
  481.  * ---------------------------------------------------------------------------*/
  482.  
  483. #define REQOP_NORMAL        0
  484. #define REQOP_SLEEP         1
  485. #define REQOP_LOOPBACK      2
  486. #define REQOP_LISTENONLY    3
  487. #define REQOP_CONFIG        4
  488.  
  489.  
  490. #endif /*MCP2510_H_*/
  491.