sja1000.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * Copyright (C) 2004 by Ole Reinhardt <ole.reinhardt@kernelconcepts.de>,
  3. * Kernelconcepts http://www.kernelconcepts.de
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. *
  33. */
  34. /*!
  35. * \file arch/avr/dev/sja1000.c
  36. * \brief Driver for SJA1000 CAN-Bus controller
  37. *
  38. *
  39. * The SJA1000 controller is connected to the memory bus. It's base
  40. * address and interrupt is set by NutRegisterDevice.
  41. *
  42. * Have a look to our m-can board if you have questions.
  43. *
  44. * \verbatim
  45. * $Id: sja1000.c 5472 2013-12-06 00:16:28Z olereinhardt $
  46. * \endverbatim
  47. */
  48. /*!
  49. * \addtogroup xgCanSJA1000
  50. */
  51. /*@{*/
  52. /* Not ported. */
  53. #ifdef __GNUC__
  54. #include <string.h>
  55. #include <sys/heap.h>
  56. #include <sys/thread.h>
  57. #include <sys/event.h>
  58. #include <sys/atom.h>
  59. #include <sys/timer.h>
  60. #include <sys/semaphore.h>
  61. #include <sys/nutconfig.h>
  62. #include <dev/irqreg.h>
  63. #include <dev/can_dev.h>
  64. #include <dev/sja1000.h>
  65. #include <cfg/arch/avr.h>
  66. #ifndef SJA_SIGNAL
  67. #define SJA_SIGNAL sig_INTERRUPT7
  68. #endif
  69. #ifndef SJA_EICR
  70. #define SJA_EICR EICRB
  71. #endif
  72. #ifndef SJA_SIGNAL_BIT
  73. #define SJA_SIGNAL_BIT 7
  74. #endif
  75. #ifndef SJA_BASE
  76. #define SJA_BASE 0x8800
  77. #endif
  78. CANINFO dcb_sja1000;
  79. volatile uint16_t sja_base = 0x0000;
  80. struct _CANBuffer {
  81. CANFRAME *dataptr; // the physical memory address where the buffer is stored
  82. uint16_t size; // the allocated size of the buffer
  83. uint16_t datalength; // the length of the data currently in the buffer
  84. uint16_t dataindex; // the index into the buffer where the data starts
  85. SEM empty;
  86. SEM full;
  87. };
  88. typedef struct _CANBuffer CANBuffer;
  89. #ifndef CAN_BufSize
  90. #define CAN_BufSize 64
  91. #endif
  92. CANBuffer CAN_RX_BUF;
  93. CANBuffer CAN_TX_BUF;
  94. void CANBufferInit(CANBuffer * buffer,uint16_t size)
  95. {
  96. NutSemInit(&buffer->full, 0);
  97. NutSemInit(&buffer->empty, CAN_BufSize - 1);
  98. // set start pointer of the buffer
  99. buffer->dataptr = NutHeapAlloc(size * sizeof(CANFRAME));
  100. buffer->size = size;
  101. // initialize index and length
  102. buffer->dataindex = 0;
  103. buffer->datalength = 0;
  104. }
  105. // access routines
  106. CANFRAME CANBufferGetMutex(CANBuffer * buffer)
  107. {
  108. CANFRAME data;
  109. NutSemWait(&buffer->full);
  110. // NutSemWait(&buffer->mutex);
  111. // check to see if there's data in the buffer
  112. if (buffer->datalength) {
  113. // get the first frame from buffer
  114. data = buffer->dataptr[buffer->dataindex];
  115. // move index down and decrement length
  116. buffer->dataindex++;
  117. if (buffer->dataindex >= buffer->size) {
  118. buffer->dataindex %= buffer->size;
  119. }
  120. buffer->datalength--;
  121. }
  122. // NutSemPost(&buffer->mutex);
  123. NutSemPost(&buffer->empty);
  124. // return
  125. return data;
  126. }
  127. void CANBufferPutMutex(CANBuffer * buffer, CANFRAME * data)
  128. {
  129. NutSemWait(&buffer->empty);
  130. // NutSemWait(&buffer->mutex);
  131. // make sure the buffer has room
  132. if (buffer->datalength < buffer->size) {
  133. // save frame at end of buffer
  134. buffer->dataptr[(buffer->dataindex + buffer->datalength) % buffer->size] = *data;
  135. // increment the length
  136. buffer->datalength++;
  137. // return success
  138. }
  139. // NutSemPost(&buffer->mutex);
  140. NutSemPost(&buffer->full);
  141. }
  142. CANFRAME CANBufferGet(CANBuffer * buffer)
  143. {
  144. CANFRAME data;
  145. // check to see if there's data in the buffer
  146. if (buffer->datalength) {
  147. // get the first frame from buffer
  148. data = buffer->dataptr[buffer->dataindex];
  149. // move index down and decrement length
  150. buffer->dataindex++;
  151. if (buffer->dataindex >= buffer->size) {
  152. buffer->dataindex %= buffer->size;
  153. }
  154. buffer->datalength--;
  155. }
  156. // return
  157. return data;
  158. }
  159. void CANBufferPut(CANBuffer * buffer, CANFRAME * data)
  160. {
  161. // make sure the buffer has room
  162. if (buffer->datalength < buffer->size) {
  163. // save frame at end of buffer
  164. buffer->dataptr[(buffer->dataindex + buffer->datalength) % buffer->size] = *data;
  165. // increment the length
  166. buffer->datalength++;
  167. // return success
  168. }
  169. }
  170. uint16_t CANBufferFree(CANBuffer * buffer)
  171. {
  172. // check to see if the buffer has room
  173. // return true if there is room
  174. return (buffer->size - buffer->datalength);
  175. }
  176. /*!
  177. * \fn SJARxAvail(NUTDEVICE * dev)
  178. * \brief checks if data is available in input buffer
  179. *
  180. * \param dev Pointer to the device structure
  181. */
  182. inline uint8_t SJARxAvail(NUTDEVICE * dev)
  183. {
  184. return CAN_RX_BUF.datalength;
  185. }
  186. /*!
  187. * \fn SJATxFree(NUTDEVICE * dev)
  188. * \brief checks if there's still space in output buffer
  189. *
  190. * \param dev Pointer to the device structure
  191. */
  192. inline uint8_t SJATxFree(NUTDEVICE * dev)
  193. {
  194. return CANBufferFree(&CAN_TX_BUF);
  195. }
  196. /*!
  197. * \fn SJAOutput(NUTDEVICE * dev, CANFRAME * frame)
  198. * \brief Write a frame from to output buffer
  199. *
  200. * This function writes a frame to the output buffer. If the output buffer
  201. * is full the function will block until frames are send.
  202. *
  203. * \param dev Pointer to the device structure
  204. *
  205. * \param frame Pointer to the receive frame
  206. */
  207. void SJAOutput(NUTDEVICE * dev, CANFRAME * frame)
  208. {
  209. CANINFO *ci;
  210. ci = (CANINFO *) dev->dev_dcb;
  211. CANBufferPutMutex(&CAN_TX_BUF, frame);
  212. NutEventPostAsync(&ci->can_tx_rdy);
  213. }
  214. /*!
  215. * \fn SJAInput(NUTDEVICE * dev, CANFRAME * frame)
  216. * \brief Reads a frame from input buffer
  217. *
  218. * This function reads a frame from the input buffer. If the input buffer
  219. * is empty the function will block unitl new frames are received.
  220. *
  221. * \param dev Pointer to the device structure
  222. *
  223. * \param frame Pointer to the receive frame
  224. * \return 1 if timeout, 0 otherwise
  225. */
  226. uint8_t SJAInput(NUTDEVICE * dev, CANFRAME * frame)
  227. {
  228. uint8_t ready = 0;
  229. CANINFO *ci;
  230. ci = (CANINFO *) dev->dev_dcb;
  231. while (!ready)
  232. {
  233. if (CAN_RX_BUF.datalength==0)
  234. {
  235. uint32_t timeout = ((IFCAN *) (dev->dev_icb))->can_rtimeout;
  236. if (NutEventWait(&ci->can_rx_rdy, timeout))
  237. return 1;
  238. }
  239. NutEnterCritical();
  240. if (CAN_RX_BUF.datalength)
  241. {
  242. *frame = CANBufferGet(&CAN_RX_BUF);
  243. ready = 1;
  244. }
  245. NutExitCritical();
  246. }
  247. SJA1000_IEN |= (RIE_Bit); // enables IRQ since buffer has space
  248. return 0;
  249. }
  250. /*!
  251. * \fn SJASetAccCode(NUTDEVICE * dev, uint8_t * ac)
  252. * \brief Sets the acceptance code
  253. *
  254. *
  255. * \param dev Pointer to the device structure
  256. *
  257. * \param ac 4 byte char array with the acceptance code
  258. */
  259. void SJASetAccCode(NUTDEVICE * dev, uint8_t * ac)
  260. {
  261. memcpy(((IFCAN *) (dev->dev_icb))->can_acc_code, ac, 4);
  262. while ((SJA1000_MODECTRL & RM_RR_Bit) == 0x00) // enter reset state
  263. SJA1000_MODECTRL = (RM_RR_Bit | SJA1000_MODECTRL);
  264. SJA1000_AC0 = ac[0];
  265. SJA1000_AC1 = ac[1];
  266. SJA1000_AC2 = ac[2];
  267. SJA1000_AC3 = ac[3];
  268. SJA1000_MODECTRL = (AFM_Bit);
  269. //*** Note - if you change SJA1000_MODECTRL, change it in
  270. // functions CAN_Init and CAN_SetAccMask also.
  271. do {
  272. SJA1000_MODECTRL = 0x00;
  273. }
  274. while ((SJA1000_MODECTRL & RM_RR_Bit) != 0x00); // leave reset state
  275. }
  276. /*!
  277. * \fn SJASetAccMask(NUTDEVICE * dev, uint8_t * am)
  278. * \brief Sets the acceptance mask
  279. *
  280. *
  281. * \param dev Pointer to the device structure
  282. *
  283. * \param am 4 byte char array with the acceptance mask
  284. */
  285. void SJASetAccMask(NUTDEVICE * dev, uint8_t * am)
  286. {
  287. memcpy(((IFCAN *) (dev->dev_icb))->can_acc_mask, am, 4);
  288. while ((SJA1000_MODECTRL & RM_RR_Bit) == 0x00) // enter reset state
  289. SJA1000_MODECTRL = (RM_RR_Bit | SJA1000_MODECTRL);
  290. SJA1000_AM0 = am[0];
  291. SJA1000_AM1 = am[1]; // mask off lower nibble (SJA manual p44)
  292. SJA1000_AM2 = am[2];
  293. SJA1000_AM3 = am[3];
  294. SJA1000_MODECTRL = (AFM_Bit);
  295. //*** Note - if you change SJA1000_MODECTRL, change it in
  296. // functions CAN_Init and CAN_SetAccCode also.
  297. do {
  298. SJA1000_MODECTRL = 0x00;
  299. }
  300. while ((SJA1000_MODECTRL & RM_RR_Bit) != 0x00); // leave reset state
  301. }
  302. /*!
  303. * \fn SJASetBaudrate(NUTDEVICE * dev, uint32_t baudrate)
  304. * \brief Sets the baudrate
  305. *
  306. *
  307. * \param dev Pointer to the device structure
  308. *
  309. * \param baudrate Baudrate (One of the defined baudrates. See sja1000.h)
  310. */
  311. uint8_t SJASetBaudrate(NUTDEVICE * dev, uint32_t baudrate)
  312. {
  313. uint8_t result = 0;
  314. ((IFCAN *) (dev->dev_icb))->can_baudrate = baudrate;
  315. while ((SJA1000_MODECTRL & RM_RR_Bit) == 0x00) // enter reset state
  316. SJA1000_MODECTRL = (RM_RR_Bit | SJA1000_MODECTRL);
  317. switch (baudrate) // setting actual bustiming
  318. { // all @ 16 Mhz
  319. case CAN_SPEED_10K:
  320. SJA1000_BT0 = 113;
  321. SJA1000_BT1 = 28;
  322. break;
  323. case CAN_SPEED_20K:
  324. SJA1000_BT0 = 88;
  325. SJA1000_BT1 = 28;
  326. break;
  327. case CAN_SPEED_50K:
  328. SJA1000_BT0 = 73;
  329. SJA1000_BT1 = 28;
  330. break;
  331. case CAN_SPEED_100K:
  332. SJA1000_BT0 = 68;
  333. SJA1000_BT1 = 28;
  334. break;
  335. case CAN_SPEED_125K:
  336. SJA1000_BT0 = 67;
  337. SJA1000_BT1 = 28;
  338. break;
  339. case CAN_SPEED_250K:
  340. SJA1000_BT0 = 65;
  341. SJA1000_BT1 = 28;
  342. break;
  343. case CAN_SPEED_500K:
  344. SJA1000_BT0 = 64;
  345. SJA1000_BT1 = 28;
  346. break;
  347. case CAN_SPEED_800K:
  348. SJA1000_BT0 = 64;
  349. SJA1000_BT1 = 22;
  350. break;
  351. case CAN_SPEED_1M:
  352. SJA1000_BT0 = 64;
  353. SJA1000_BT1 = 20;
  354. break;
  355. default:
  356. result = 1;
  357. }
  358. do {
  359. SJA1000_MODECTRL = 0x00;
  360. }
  361. while ((SJA1000_MODECTRL & RM_RR_Bit) != 0x00); // leave reset state
  362. return result;
  363. }
  364. /*!
  365. * \fn SJATxFrame(CANFRAME * CAN_frame)
  366. * \brief Sends a CAN Frane
  367. *
  368. *
  369. * \param CAN_frame Pointer to the send frame
  370. */
  371. void SJATxFrame(CANFRAME * CAN_frame)
  372. {
  373. uint32_t temp_id;
  374. if (CAN_frame->ext) {
  375. temp_id = CAN_frame->id << 3;
  376. SJA1000_TxFrameInfo = CAN_frame->len | CAN_29 | (CAN_frame->rtr ? CAN_RTR : 0);
  377. SJA1000_Tx1 = (uint8_t) (temp_id >> 24); // load High Byte
  378. SJA1000_Tx2 = (uint8_t) (temp_id >> 16); // load High Byte
  379. SJA1000_Tx3 = (uint8_t) (temp_id >> 8); // load High Byte
  380. SJA1000_Tx4 = (uint8_t) (temp_id & 0x00F8); // Low Byte and ignore bit 0-2
  381. SJA1000_Tx5 = CAN_frame->byte[0];
  382. SJA1000_Tx6 = CAN_frame->byte[1];
  383. SJA1000_Tx7 = CAN_frame->byte[2];
  384. SJA1000_Tx8 = CAN_frame->byte[3];
  385. SJA1000_Tx9 = CAN_frame->byte[4];
  386. SJA1000_Tx10 = CAN_frame->byte[5];
  387. SJA1000_Tx11 = CAN_frame->byte[6];
  388. SJA1000_Tx12 = CAN_frame->byte[7];
  389. } else {
  390. temp_id = CAN_frame->id << 21;
  391. SJA1000_TxFrameInfo = CAN_frame->len | (CAN_frame->rtr ? CAN_RTR : 0);
  392. SJA1000_Tx1 = (uint8_t) (temp_id >> 24); // load High Byte
  393. SJA1000_Tx2 = (uint8_t) (temp_id >> 16) & 0xE0; // Low Byte and ignore bit 0-4
  394. SJA1000_Tx3 = CAN_frame->byte[0];
  395. SJA1000_Tx4 = CAN_frame->byte[1];
  396. SJA1000_Tx5 = CAN_frame->byte[2];
  397. SJA1000_Tx6 = CAN_frame->byte[3];
  398. SJA1000_Tx7 = CAN_frame->byte[4];
  399. SJA1000_Tx8 = CAN_frame->byte[5];
  400. SJA1000_Tx9 = CAN_frame->byte[6];
  401. SJA1000_Tx10 = CAN_frame->byte[7];
  402. }
  403. SJA1000_CMD = TR_Bit; // Start Transmission
  404. }
  405. /*!
  406. * \fn SJARxFrame(CANFRAME * CAN_frame)
  407. * \brief Receives a CAN Frane
  408. *
  409. *
  410. * \param CAN_frame Pointer to the receive frame
  411. */
  412. void SJARxFrame(CANFRAME * CAN_frame)
  413. {
  414. uint8_t FrameInfo = SJA1000_RxFrameInfo;
  415. CAN_frame->len = FrameInfo & 0x0F; // frame info mask off higher 4 bits
  416. CAN_frame->ext = FrameInfo & CAN_29 ? 1 : 0;
  417. CAN_frame->rtr = FrameInfo & CAN_RTR ? 1 : 0;
  418. if (CAN_frame->ext) {
  419. CAN_frame->id = (((uint32_t) SJA1000_Rx1 << 24) |
  420. ((uint32_t) SJA1000_Rx2 << 16) |
  421. ((uint32_t) SJA1000_Rx3 << 8) |
  422. ((uint32_t) SJA1000_Rx4 & 0xF8)) >> 3;
  423. CAN_frame->byte[0] = SJA1000_Rx5;
  424. CAN_frame->byte[1] = SJA1000_Rx6;
  425. CAN_frame->byte[2] = SJA1000_Rx7;
  426. CAN_frame->byte[3] = SJA1000_Rx8;
  427. CAN_frame->byte[4] = SJA1000_Rx9;
  428. CAN_frame->byte[5] = SJA1000_Rx10;
  429. CAN_frame->byte[6] = SJA1000_Rx11;
  430. CAN_frame->byte[7] = SJA1000_Rx12; // just fill the whole struct, less CPU cycles
  431. } else {
  432. CAN_frame->id = (((uint32_t) SJA1000_Rx1 << 24) |
  433. (uint32_t) SJA1000_Rx2 << 16) >> 21; // id_h and id_l
  434. CAN_frame->byte[0] = SJA1000_Rx3;
  435. CAN_frame->byte[1] = SJA1000_Rx4;
  436. CAN_frame->byte[2] = SJA1000_Rx5;
  437. CAN_frame->byte[3] = SJA1000_Rx6;
  438. CAN_frame->byte[4] = SJA1000_Rx7;
  439. CAN_frame->byte[5] = SJA1000_Rx8;
  440. CAN_frame->byte[6] = SJA1000_Rx9;
  441. CAN_frame->byte[7] = SJA1000_Rx10; // just fill the whole struct, less CPU cycles
  442. }
  443. SJA1000_CMD = RRB_Bit; // release the receive buffer
  444. }
  445. /*!
  446. * \fn CAN_Tx(void *arg)
  447. * \brief CAN transmitter thread.
  448. *
  449. *
  450. * This thread transmits data if there's some in the output buffer.
  451. * It runs with high priority.
  452. */
  453. THREAD(CAN_Tx, arg)
  454. {
  455. NUTDEVICE *dev;
  456. CANINFO *ci;
  457. CANFRAME out_frame;
  458. dev = arg;
  459. ci = (CANINFO *) dev->dev_dcb;
  460. NutThreadSetPriority(16);
  461. while (1) {
  462. NutEventWait(&ci->can_tx_rdy, NUT_WAIT_INFINITE);
  463. while ((SJA1000_STATUS & TBS_Bit) == TBS_Bit) // if transmit buffer released
  464. {
  465. out_frame = CANBufferGetMutex(&CAN_TX_BUF);
  466. SJATxFrame(&out_frame); // using SJA1000 TX buffer
  467. ci->can_tx_frames++;
  468. }
  469. }
  470. }
  471. /*!
  472. * \fn SJAInterrupt (void * arg)
  473. * \brief SJA interrupt entry.
  474. *
  475. * The interrupt handler posts events to the rx and tx thread wait queue.
  476. * receive interrupt will be disabled on reception and will be enabled by the
  477. * rx thread again. Otherwise interrupt would not stop (level triggered)
  478. */
  479. static void SJAInterrupt(void *arg)
  480. {
  481. CANINFO *ci;
  482. volatile uint8_t irq = SJA1000_INT;
  483. CANFRAME in_frame;
  484. ci = (CANINFO *) (((NUTDEVICE *) arg)->dev_dcb);
  485. ci->can_interrupts++;
  486. if (((irq & TI_Bit) == TI_Bit)) // transmit IRQ fired
  487. {
  488. NutEventPostFromIrq(&ci->can_tx_rdy);
  489. }
  490. if ((irq & RI_Bit) == RI_Bit) // Receive IRQ fired
  491. {
  492. if (CAN_RX_BUF.size-CAN_RX_BUF.datalength > 0)
  493. {
  494. SJARxFrame(&in_frame);
  495. CANBufferPut(&CAN_RX_BUF, &in_frame);
  496. if (CAN_RX_BUF.size==CAN_RX_BUF.datalength)
  497. SJA1000_IEN &= (~RIE_Bit); // Disable RX IRQ until data has been poped from input buffer
  498. NutEventPostFromIrq(&ci->can_rx_rdy);
  499. ci->can_rx_frames++;
  500. }
  501. }
  502. if ((irq & EI_Bit) == EI_Bit) //Error IRQ fired
  503. {
  504. ci->can_errors++;
  505. // TODO: Handle error
  506. } else if ((irq & DOI_Bit) == DOI_Bit) //Error IRQ fired
  507. {
  508. ci->can_overruns++;
  509. SJA1000_CMD = CDO_Bit; // Clear DO status;
  510. // TODO: Handle overrun
  511. }
  512. }
  513. /*!
  514. * \fn SJAInit(NUTDEVICE * dev)
  515. * \brief Initialize SJA1000 Canbus interface.
  516. *
  517. *
  518. * Applications typically do not use this function, but
  519. * call NutRegisterDevice().
  520. *
  521. * \param dev Identifies the device to initialize. The
  522. * structure must be properly set.
  523. */
  524. int SJAInit(NUTDEVICE * dev)
  525. {
  526. IFCAN *ifc;
  527. sja_base = dev->dev_base;
  528. if (sja_base == 0x0000) sja_base = SJA_BASE;
  529. ifc = dev->dev_icb;
  530. memset(dev->dev_dcb, 0, sizeof(CANINFO));
  531. CANBufferInit(&CAN_RX_BUF, CAN_BufSize);
  532. CANBufferInit(&CAN_TX_BUF, CAN_BufSize);
  533. while ((SJA1000_MODECTRL & RM_RR_Bit) == 0x00) // entering reset mode
  534. SJA1000_MODECTRL = (RM_RR_Bit | SJA1000_MODECTRL);
  535. SJA1000_CLK_DIV = (CANMode_Bit | CBP_Bit | DivBy2 | ClkOff_Bit); // sets clock divide register
  536. SJA1000_IEN = (ClrIntEnSJA); // Disables CAN IRQ
  537. SJA1000_AC0 = ifc->can_acc_code[0];
  538. SJA1000_AC1 = ifc->can_acc_code[1];
  539. SJA1000_AC2 = ifc->can_acc_code[2];
  540. SJA1000_AC3 = ifc->can_acc_code[3];
  541. SJA1000_AM0 = ifc->can_acc_mask[0];
  542. SJA1000_AM1 = ifc->can_acc_mask[1];
  543. SJA1000_AM2 = ifc->can_acc_mask[2];
  544. SJA1000_AM3 = ifc->can_acc_mask[3];
  545. switch (ifc->can_baudrate) // setting actual bustiming
  546. { // all @ 16 Mhz
  547. case CAN_SPEED_10K:
  548. SJA1000_BT0 = 113;
  549. SJA1000_BT1 = 28;
  550. break;
  551. case CAN_SPEED_20K:
  552. SJA1000_BT0 = 88;
  553. SJA1000_BT1 = 28;
  554. break;
  555. case CAN_SPEED_50K:
  556. SJA1000_BT0 = 73;
  557. SJA1000_BT1 = 28;
  558. break;
  559. case CAN_SPEED_100K:
  560. SJA1000_BT0 = 68;
  561. SJA1000_BT1 = 28;
  562. break;
  563. case CAN_SPEED_125K:
  564. SJA1000_BT0 = 67;
  565. SJA1000_BT1 = 28;
  566. break;
  567. case CAN_SPEED_250K:
  568. SJA1000_BT0 = 65;
  569. SJA1000_BT1 = 28;
  570. break;
  571. case CAN_SPEED_500K:
  572. SJA1000_BT0 = 64;
  573. SJA1000_BT1 = 28;
  574. break;
  575. case CAN_SPEED_800K:
  576. SJA1000_BT0 = 64;
  577. SJA1000_BT1 = 22;
  578. break;
  579. case CAN_SPEED_1M:
  580. SJA1000_BT0 = 64;
  581. SJA1000_BT1 = 20;
  582. break;
  583. default:
  584. return errCAN_INVALID_BAUD;
  585. }
  586. SJA1000_OUTCTRL = (Tx1Float | Tx0PshPull | NormalMode); // Set up Output Control Register
  587. SJA1000_IEN = (RIE_Bit | TIE_Bit | EIE_Bit | DOIE_Bit ); // Enables receive IRQ
  588. SJA1000_MODECTRL = (AFM_Bit); // set single filter mode + sleep
  589. // *** Note - if you change SJA1000_MODECTRL, change it in
  590. // functions SJASetAccMask and SJASetAccCode also.
  591. do {
  592. SJA1000_MODECTRL = 0x00;
  593. }
  594. while ((SJA1000_MODECTRL & RM_RR_Bit) != 0x00); // leaves reset mode
  595. NutEnterCritical();
  596. if (NutRegisterIrqHandler(&SJA_SIGNAL, SJAInterrupt, dev)) {
  597. NutExitCritical();
  598. return -1;
  599. }
  600. cbi(EIMSK, SJA_SIGNAL_BIT);
  601. if (SJA_SIGNAL_BIT < 4) // Set corresponding interrupt to low
  602. { // level interrupt
  603. #ifdef __AVR_ENHANCED__
  604. cbi(EICRA, (SJA_SIGNAL_BIT << 1));
  605. cbi(EICRA, (SJA_SIGNAL_BIT << 1) + 1);
  606. #endif /* __AVR_ENHANCED__ */
  607. } else {
  608. cbi(EICR, ((SJA_SIGNAL_BIT - 4) << 1));
  609. cbi(EICR, ((SJA_SIGNAL_BIT - 4) << 1) + 1);
  610. }
  611. SJA1000_INT; // Read interrupt register to clear pendin bits
  612. sbi(EIMSK, SJA_SIGNAL_BIT);
  613. sbi(PORTE, SJA_SIGNAL_BIT);
  614. NutThreadCreate("sjacantx", CAN_Tx, dev, 256);
  615. NutExitCritical();
  616. return 0;
  617. }
  618. /*!
  619. * \brief Interface information structure.
  620. *
  621. * This struct stores some interface parameters like bautdate and
  622. * acceptance mask / code. Beside this Callback handlers are registered.
  623. */
  624. IFCAN ifc_sja1000 = {
  625. CAN_IF_2B, /*!< \brief Interface type. */
  626. CAN_SPEED_500K, /*!< \brief Baudrate of device. */
  627. {0xFF, 0xFF, 0xFF, 0xFF}
  628. , /*!< \brief Acceptance mask */
  629. {0x00, 0x00, 0x00, 0x00}
  630. , /*!< \brief Acceptance code */
  631. NUT_WAIT_INFINITE, /*!< \brief Receive time-out */
  632. SJARxAvail, /*!< \brief Data in RxBuffer available? */
  633. SJATxFree, /*!< \brief TxBuffer free? */
  634. SJAInput, /*!< \brief CAN Input routine */
  635. SJAOutput, /*!< \brief CAN Output routine */
  636. SJASetAccCode, /*!< \brief Set acceptance code */
  637. SJASetAccMask, /*!< \brief Set acceptance mask */
  638. SJASetBaudrate /*!< \brief Set baudrate */
  639. };
  640. /*!
  641. * \brief Device information structure.
  642. *
  643. * Applications must pass this structure to NutRegisterDevice()
  644. * to bind this CAN device driver to the Nut/OS kernel.
  645. *
  646. * \note The interrupt handler of this driver uses a significant amount
  647. * of stack space, which may require to increase thread stacks of
  648. * all threads by at least 32 bytes. Furthermore, it requires quite
  649. * some time to execute and may degrade overall system performance.
  650. */
  651. NUTDEVICE devSJA1000 = {
  652. 0, /*!< Pointer to next device. */
  653. {'s', 'j', 'a', '1', '0', '0', '0', 0, 0}
  654. , /*!< Unique device name. */
  655. IFTYP_CAN, /*!< Type of device. */
  656. 0, /*!< Base address. */
  657. 0, /*!< First interrupt number. */
  658. &ifc_sja1000, /*!< Interface control block. */
  659. &dcb_sja1000, /*!< Driver control block. */
  660. SJAInit, /*!< Driver initialization routine. */
  661. 0, /*!< Driver specific control function. */
  662. 0, /*!< Read from device. */
  663. 0, /*!< Write to device. */
  664. 0, /*!< Write from program space data to device. */
  665. 0, /*!< Open a device or file. */
  666. 0, /*!< Close a device or file. */
  667. 0, /*!< Request file size. */
  668. 0, /*!< Select function, optional, not yet implemented */
  669. };
  670. #else
  671. void keep_icc_happy(void)
  672. {
  673. }
  674. #endif
  675. /*@}*/