can.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  1. //*****************************************************************************
  2. //
  3. // can.c - Driver for the CAN module.
  4. //
  5. // Copyright (c) 2006-2017 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  15. // Redistributions in binary form must reproduce the above copyright
  16. // notice, this list of conditions and the following disclaimer in the
  17. // documentation and/or other materials provided with the
  18. // distribution.
  19. //
  20. // Neither the name of Texas Instruments Incorporated nor the names of
  21. // its contributors may be used to endorse or promote products derived
  22. // from this software without specific prior written permission.
  23. //
  24. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //*****************************************************************************
  37. //*****************************************************************************
  38. //
  39. //! \addtogroup can_api
  40. //! @{
  41. //
  42. //*****************************************************************************
  43. #include <ti/devices/msp432e4/inc/msp432e411y.h>
  44. #include "types.h"
  45. #include <stdbool.h>
  46. #include <stdint.h>
  47. #include "inc/hw_can.h"
  48. #include "inc/hw_nvic.h"
  49. #include "inc/hw_sysctl.h"
  50. #include "can.h"
  51. #include "debug.h"
  52. #include "interrupt.h"
  53. //*****************************************************************************
  54. //
  55. // This is the maximum number that can be stored as an 11bit Message
  56. // identifier.
  57. //
  58. //*****************************************************************************
  59. #define CAN_MAX_11BIT_MSG_ID 0x7ff
  60. //*****************************************************************************
  61. //
  62. // The maximum CAN bit timing divisor is 19.
  63. //
  64. //*****************************************************************************
  65. #define CAN_MAX_BIT_DIVISOR 19
  66. //*****************************************************************************
  67. //
  68. // The minimum CAN bit timing divisor is 4.
  69. //
  70. //*****************************************************************************
  71. #define CAN_MIN_BIT_DIVISOR 4
  72. //*****************************************************************************
  73. //
  74. // The maximum CAN pre-divisor is 1024.
  75. //
  76. //*****************************************************************************
  77. #define CAN_MAX_PRE_DIVISOR 1024
  78. //*****************************************************************************
  79. //
  80. // The minimum CAN pre-divisor is 1.
  81. //
  82. //*****************************************************************************
  83. #define CAN_MIN_PRE_DIVISOR 1
  84. //*****************************************************************************
  85. //
  86. // Converts a set of CAN bit timing values into the value that needs to be
  87. // programmed into the CAN_BIT register to achieve those timings.
  88. //
  89. //*****************************************************************************
  90. #define CAN_BIT_VALUE(seg1, seg2, sjw) \
  91. ((((seg1 - 1) << CAN_BIT_TSEG1_S) & \
  92. CAN_BIT_TSEG1_M) | \
  93. (((seg2 - 1) << CAN_BIT_TSEG2_S) & \
  94. CAN_BIT_TSEG2_M) | \
  95. (((sjw - 1) << CAN_BIT_SJW_S) & \
  96. CAN_BIT_SJW_M))
  97. //*****************************************************************************
  98. //
  99. // This table is used by the CANBitRateSet() API as the register defaults for
  100. // the bit timing values.
  101. //
  102. //*****************************************************************************
  103. static const uint16_t g_ui16CANBitValues[] =
  104. {
  105. CAN_BIT_VALUE(2, 1, 1), // 4 clocks/bit
  106. CAN_BIT_VALUE(3, 1, 1), // 5 clocks/bit
  107. CAN_BIT_VALUE(3, 2, 2), // 6 clocks/bit
  108. CAN_BIT_VALUE(4, 2, 2), // 7 clocks/bit
  109. CAN_BIT_VALUE(4, 3, 3), // 8 clocks/bit
  110. CAN_BIT_VALUE(5, 3, 3), // 9 clocks/bit
  111. CAN_BIT_VALUE(5, 4, 4), // 10 clocks/bit
  112. CAN_BIT_VALUE(6, 4, 4), // 11 clocks/bit
  113. CAN_BIT_VALUE(6, 5, 4), // 12 clocks/bit
  114. CAN_BIT_VALUE(7, 5, 4), // 13 clocks/bit
  115. CAN_BIT_VALUE(7, 6, 4), // 14 clocks/bit
  116. CAN_BIT_VALUE(8, 6, 4), // 15 clocks/bit
  117. CAN_BIT_VALUE(8, 7, 4), // 16 clocks/bit
  118. CAN_BIT_VALUE(9, 7, 4), // 17 clocks/bit
  119. CAN_BIT_VALUE(9, 8, 4), // 18 clocks/bit
  120. CAN_BIT_VALUE(10, 8, 4) // 19 clocks/bit
  121. };
  122. //*****************************************************************************
  123. //
  124. //! \internal
  125. //! Checks a CAN base address.
  126. //!
  127. //! \param ui32Base is the base address of the CAN controller.
  128. //!
  129. //! This function determines if a CAN controller base address is valid.
  130. //!
  131. //! \return Returns \b true if the base address is valid and \b false
  132. //! otherwise.
  133. //
  134. //*****************************************************************************
  135. #ifdef DEBUG
  136. static bool
  137. _CANBaseValid(uint32_t ui32Base)
  138. {
  139. return ((ui32Base == CAN0_BASE) || (ui32Base == CAN1_BASE));
  140. }
  141. #endif
  142. //*****************************************************************************
  143. //
  144. //! Returns the CAN controller interrupt number.
  145. //!
  146. //! \param ui32Base is the base address of the selected CAN controller
  147. //!
  148. //! This function returns the interrupt number for the CAN module with the base
  149. //! address passed in the \e ui32Base parameter.
  150. //!
  151. //! \return Returns a CAN interrupt number or 0 if the interrupt does not
  152. //! exist.
  153. //
  154. //*****************************************************************************
  155. static uint_fast8_t
  156. _CANIntNumberGet(uint32_t ui32Base)
  157. {
  158. uint_fast8_t ui8Int;
  159. ASSERT((ui32Base == CAN0_BASE) || (ui32Base == CAN1_BASE));
  160. ui8Int = 0;
  161. //
  162. // Find the valid interrupt number for this CAN controller.
  163. //
  164. if (ui32Base == CAN0_BASE)
  165. {
  166. ui8Int = INT_CAN0;
  167. }
  168. else if (ui32Base == CAN1_BASE)
  169. {
  170. ui8Int = INT_CAN1;
  171. }
  172. return (ui8Int);
  173. }
  174. //*****************************************************************************
  175. //
  176. //! \internal
  177. //! Copies data from a buffer to the CAN Data registers.
  178. //!
  179. //! \param pui8Data is a pointer to the data to be written out to the CAN
  180. //! controller's data registers.
  181. //! \param pui32Register is an uint32_t pointer to the first register of the
  182. //! CAN controller's data registers. For example, in order to use the IF1
  183. //! register set on CAN controller 0, the value would be: \b CAN0_BASE \b +
  184. //! \b CAN_O_IF1DA1.
  185. //! \param iSize is the number of bytes to copy into the CAN controller.
  186. //!
  187. //! This function takes the steps necessary to copy data from a contiguous
  188. //! buffer in memory into the non-contiguous data registers used by the CAN
  189. //! controller. This function is rarely used outside of the CANMessageSet()
  190. //! function.
  191. //!
  192. //! \return None.
  193. //
  194. //*****************************************************************************
  195. static void
  196. _CANDataRegWrite(uint8_t *pui8Data, uint32_t *pui32Register, uint32_t ui32Size)
  197. {
  198. uint32_t ui32Idx, ui32Value;
  199. //
  200. // Loop always copies 1 or 2 bytes per iteration.
  201. //
  202. for (ui32Idx = 0; ui32Idx < ui32Size;)
  203. {
  204. //
  205. // Write out the data 16 bits at a time since this is how the registers
  206. // are aligned in memory.
  207. //
  208. ui32Value = pui8Data[ui32Idx++];
  209. //
  210. // Only write the second byte if needed otherwise the value is zero.
  211. //
  212. if (ui32Idx < ui32Size)
  213. {
  214. ui32Value |= (pui8Data[ui32Idx++] << 8);
  215. }
  216. HWREG(pui32Register++) = ui32Value;
  217. }
  218. }
  219. //*****************************************************************************
  220. //
  221. //! \internal
  222. //! Copies data from a buffer to the CAN Data registers.
  223. //!
  224. //! \param pui8Data is a pointer to the location to store the data read from
  225. //! the CAN controller's data registers.
  226. //! \param pui32Register is an uint32_t pointer to the first register of the
  227. //! CAN controller's data registers. For example, in order to use the IF1
  228. //! register set on CAN controller 1, the value would be: \b CAN0_BASE \b +
  229. //! \b CAN_O_IF1DA1.
  230. //! \param iSize is the number of bytes to copy from the CAN controller.
  231. //!
  232. //! This function takes the steps necessary to copy data to a contiguous buffer
  233. //! in memory from the non-contiguous data registers used by the CAN
  234. //! controller. This function is rarely used outside of the CANMessageGet()
  235. //! function.
  236. //!
  237. //! \return None.
  238. //
  239. //*****************************************************************************
  240. static void
  241. _CANDataRegRead(uint8_t *pui8Data, uint32_t *pui32Register, uint32_t ui32Size)
  242. {
  243. uint32_t ui32Idx, ui32Value;
  244. //
  245. // Loop always copies 1 or 2 bytes per iteration.
  246. //
  247. for (ui32Idx = 0; ui32Idx < ui32Size;)
  248. {
  249. //
  250. // Read out the data 16 bits at a time since this is how the registers
  251. // are aligned in memory.
  252. //
  253. ui32Value = HWREG(pui32Register++);
  254. //
  255. // Store the first byte.
  256. //
  257. pui8Data[ui32Idx++] = (uint8_t)ui32Value;
  258. //
  259. // Only read the second byte if needed.
  260. //
  261. if (ui32Idx < ui32Size)
  262. {
  263. pui8Data[ui32Idx++] = (uint8_t)(ui32Value >> 8);
  264. }
  265. }
  266. }
  267. //*****************************************************************************
  268. //
  269. //! Initializes the CAN controller after reset.
  270. //!
  271. //! \param ui32Base is the base address of the CAN controller.
  272. //!
  273. //! After reset, the CAN controller is left in the disabled state. However,
  274. //! the memory used for message objects contains undefined values and must be
  275. //! cleared prior to enabling the CAN controller the first time. This prevents
  276. //! unwanted transmission or reception of data before the message objects are
  277. //! configured. This function must be called before enabling the controller
  278. //! the first time.
  279. //!
  280. //! \return None.
  281. //
  282. //*****************************************************************************
  283. void
  284. CANInit(uint32_t ui32Base)
  285. {
  286. uint32_t ui32Msg;
  287. //
  288. // Check the arguments.
  289. //
  290. ASSERT(_CANBaseValid(ui32Base));
  291. //
  292. // Place CAN controller in init state, regardless of previous state. This
  293. // puts controller in idle, and allow the message object RAM to be
  294. // programmed.
  295. //
  296. HWREG(ui32Base + CAN_O_CTL) = CAN_CTL_INIT;
  297. //
  298. // Wait for busy bit to clear
  299. //
  300. while (HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  301. {
  302. }
  303. //
  304. // Clear the message value bit in the arbitration register. This indicates
  305. // the message is not valid and is a "safe" condition to leave the message
  306. // object. The same arb reg is used to program all the message objects.
  307. //
  308. HWREG(ui32Base + CAN_O_IF1CMSK) = (CAN_IF1CMSK_WRNRD | CAN_IF1CMSK_ARB |
  309. CAN_IF1CMSK_CONTROL);
  310. HWREG(ui32Base + CAN_O_IF1ARB2) = 0;
  311. HWREG(ui32Base + CAN_O_IF1MCTL) = 0;
  312. //
  313. // Loop through to program all 32 message objects
  314. //
  315. for (ui32Msg = 1; ui32Msg <= 32; ui32Msg++)
  316. {
  317. //
  318. // Wait for busy bit to clear
  319. //
  320. while (HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  321. {
  322. }
  323. //
  324. // Initiate programming the message object
  325. //
  326. HWREG(ui32Base + CAN_O_IF1CRQ) = ui32Msg;
  327. }
  328. //
  329. // Make sure that the interrupt and new data flags are updated for the
  330. // message objects.
  331. //
  332. HWREG(ui32Base + CAN_O_IF1CMSK) = (CAN_IF1CMSK_NEWDAT |
  333. CAN_IF1CMSK_CLRINTPND);
  334. //
  335. // Loop through to program all 32 message objects
  336. //
  337. for (ui32Msg = 1; ui32Msg <= 32; ui32Msg++)
  338. {
  339. //
  340. // Wait for busy bit to clear.
  341. //
  342. while (HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  343. {
  344. }
  345. //
  346. // Initiate programming the message object
  347. //
  348. HWREG(ui32Base + CAN_O_IF1CRQ) = ui32Msg;
  349. }
  350. //
  351. // Acknowledge any pending status interrupts.
  352. //
  353. HWREG(ui32Base + CAN_O_STS);
  354. }
  355. //*****************************************************************************
  356. //
  357. //! Enables the CAN controller.
  358. //!
  359. //! \param ui32Base is the base address of the CAN controller to enable.
  360. //!
  361. //! Enables the CAN controller for message processing. Once enabled, the
  362. //! controller automatically transmits any pending frames, and processes any
  363. //! received frames. The controller can be stopped by calling CANDisable().
  364. //! Prior to calling CANEnable(), CANInit() must have been called to
  365. //! initialize the controller and the CAN bus clock must be configured by
  366. //! calling CANBitTimingSet().
  367. //!
  368. //! \return None.
  369. //
  370. //*****************************************************************************
  371. void
  372. CANEnable(uint32_t ui32Base)
  373. {
  374. //
  375. // Check the arguments.
  376. //
  377. ASSERT(_CANBaseValid(ui32Base));
  378. //
  379. // Clear the init bit in the control register.
  380. //
  381. HWREG(ui32Base + CAN_O_CTL) &= ~CAN_CTL_INIT;
  382. }
  383. //*****************************************************************************
  384. //
  385. //! Disables the CAN controller.
  386. //!
  387. //! \param ui32Base is the base address of the CAN controller to disable.
  388. //!
  389. //! Disables the CAN controller for message processing. When disabled, the
  390. //! controller no longer automatically processes data on the CAN bus. The
  391. //! controller can be restarted by calling CANEnable(). The state of the CAN
  392. //! controller and the message objects in the controller are left as they were
  393. //! before this call was made.
  394. //!
  395. //! \return None.
  396. //
  397. //*****************************************************************************
  398. void
  399. CANDisable(uint32_t ui32Base)
  400. {
  401. //
  402. // Check the arguments.
  403. //
  404. ASSERT(_CANBaseValid(ui32Base));
  405. //
  406. // Set the init bit in the control register.
  407. //
  408. HWREG(ui32Base + CAN_O_CTL) |= CAN_CTL_INIT;
  409. }
  410. //*****************************************************************************
  411. //
  412. //! Reads the current settings for the CAN controller bit timing.
  413. //!
  414. //! \param ui32Base is the base address of the CAN controller.
  415. //! \param psClkParms is a pointer to a structure to hold the timing
  416. //! parameters.
  417. //!
  418. //! This function reads the current configuration of the CAN controller bit
  419. //! clock timing and stores the resulting information in the structure
  420. //! supplied by the caller. Refer to CANBitTimingSet() for the meaning of the
  421. //! values that are returned in the structure pointed to by \e psClkParms.
  422. //!
  423. //! \return None.
  424. //
  425. //*****************************************************************************
  426. void
  427. CANBitTimingGet(uint32_t ui32Base, tCANBitClkParms *psClkParms)
  428. {
  429. uint32_t ui32BitReg;
  430. //
  431. // Check the arguments.
  432. //
  433. ASSERT(_CANBaseValid(ui32Base));
  434. ASSERT(psClkParms);
  435. //
  436. // Read out all the bit timing values from the CAN controller registers.
  437. //
  438. ui32BitReg = HWREG(ui32Base + CAN_O_BIT);
  439. //
  440. // Set the phase 2 segment.
  441. //
  442. psClkParms->ui32Phase2Seg =
  443. ((ui32BitReg & CAN_BIT_TSEG2_M) >> CAN_BIT_TSEG2_S) + 1;
  444. //
  445. // Set the phase 1 segment.
  446. //
  447. psClkParms->ui32SyncPropPhase1Seg =
  448. ((ui32BitReg & CAN_BIT_TSEG1_M) >> CAN_BIT_TSEG1_S) + 1;
  449. //
  450. // Set the synchronous jump width.
  451. //
  452. psClkParms->ui32SJW = ((ui32BitReg & CAN_BIT_SJW_M) >> CAN_BIT_SJW_S) + 1;
  453. //
  454. // Set the pre-divider for the CAN bus bit clock.
  455. //
  456. psClkParms->ui32QuantumPrescaler =
  457. ((ui32BitReg & CAN_BIT_BRP_M) |
  458. ((HWREG(ui32Base + CAN_O_BRPE) & CAN_BRPE_BRPE_M) << 6)) + 1;
  459. }
  460. //*****************************************************************************
  461. //
  462. //! Sets the CAN bit timing values to a nominal setting based on a desired
  463. //! bit rate.
  464. //!
  465. //! \param ui32Base is the base address of the CAN controller.
  466. //! \param ui32SourceClock is the system clock for the device in Hz.
  467. //! \param ui32BitRate is the desired bit rate.
  468. //!
  469. //! This function sets the CAN bit timing for the bit rate passed in the
  470. //! \e ui32BitRate parameter based on the \e ui32SourceClock parameter.
  471. //! Because the CAN clock is based off of the system clock, the calling
  472. //! function must pass in the source clock rate either by retrieving it from
  473. //! SysCtlClockGet() or using a specific value in Hz. The CAN bit timing is
  474. //! calculated assuming a minimal amount of propagation delay, which works for
  475. //! most cases where the network length is short. If tighter timing
  476. //! requirements or longer network lengths are needed, then the
  477. //! CANBitTimingSet() function is available for full customization of all of
  478. //! the CAN bit timing values. Because not all bit rates can be matched
  479. //! exactly, the bit rate is set to the value closest to the desired bit rate
  480. //! without being higher than the \e ui32BitRate value.
  481. //!
  482. //! \return This function returns the bit rate that the CAN controller was
  483. //! configured to use or it returns 0 to indicate that the bit rate was not
  484. //! changed because the requested bit rate was not valid.
  485. //!
  486. //*****************************************************************************
  487. uint32_t
  488. CANBitRateSet(uint32_t ui32Base, uint32_t ui32SourceClock,
  489. uint32_t ui32BitRate)
  490. {
  491. uint32_t ui32DesiredRatio;
  492. uint32_t ui32CANBits;
  493. uint32_t ui32PreDivide;
  494. uint32_t ui32RegValue;
  495. uint16_t ui16CANCTL;
  496. //
  497. // Check the arguments.
  498. //
  499. ASSERT(_CANBaseValid(ui32Base));
  500. ASSERT(ui32SourceClock);
  501. ASSERT(ui32BitRate);
  502. //
  503. // Calculate the desired clock rate.
  504. //
  505. ui32DesiredRatio = ui32SourceClock / ui32BitRate;
  506. //
  507. // Make sure that the ratio of CAN bit rate to processor clock is not too
  508. // small or too large.
  509. //
  510. ASSERT(ui32DesiredRatio <= (CAN_MAX_PRE_DIVISOR * CAN_MAX_BIT_DIVISOR));
  511. ASSERT(ui32DesiredRatio >= (CAN_MIN_PRE_DIVISOR * CAN_MIN_BIT_DIVISOR));
  512. //
  513. // Make sure that the Desired Ratio is not too large. This enforces the
  514. // requirement that the bit rate is larger than requested.
  515. //
  516. if ((ui32SourceClock / ui32DesiredRatio) > ui32BitRate)
  517. {
  518. ui32DesiredRatio += 1;
  519. }
  520. //
  521. // Check all possible values to find a matching value.
  522. //
  523. while (ui32DesiredRatio <= (CAN_MAX_PRE_DIVISOR * CAN_MAX_BIT_DIVISOR))
  524. {
  525. //
  526. // Loop through all possible CAN bit divisors.
  527. //
  528. for (ui32CANBits = CAN_MAX_BIT_DIVISOR;
  529. ui32CANBits >= CAN_MIN_BIT_DIVISOR; ui32CANBits--)
  530. {
  531. //
  532. // For a given CAN bit divisor save the pre divisor.
  533. //
  534. ui32PreDivide = ui32DesiredRatio / ui32CANBits;
  535. //
  536. // If the calculated divisors match the desired clock ratio then
  537. // return these bit rate and set the CAN bit timing.
  538. //
  539. if ((ui32PreDivide * ui32CANBits) == ui32DesiredRatio)
  540. {
  541. //
  542. // Start building the bit timing value by adding the bit timing
  543. // in time quanta.
  544. //
  545. ui32RegValue = g_ui16CANBitValues[ui32CANBits -
  546. CAN_MIN_BIT_DIVISOR];
  547. //
  548. // To set the bit timing register, the controller must be
  549. // placed in init mode (if not already), and also configuration
  550. // change bit enabled. The state of the register must be
  551. // saved so it can be restored.
  552. //
  553. ui16CANCTL = HWREG(ui32Base + CAN_O_CTL);
  554. HWREG(ui32Base + CAN_O_CTL) = ui16CANCTL | CAN_CTL_INIT |
  555. CAN_CTL_CCE;
  556. //
  557. // Now add in the pre-scalar on the bit rate.
  558. //
  559. ui32RegValue |= ((ui32PreDivide - 1) & CAN_BIT_BRP_M);
  560. //
  561. // Set the clock bits in the and the lower bits of the
  562. // pre-scalar.
  563. //
  564. HWREG(ui32Base + CAN_O_BIT) = ui32RegValue;
  565. //
  566. // Set the divider upper bits in the extension register.
  567. //
  568. HWREG(ui32Base + CAN_O_BRPE) = ((ui32PreDivide - 1) >> 6) &
  569. CAN_BRPE_BRPE_M;
  570. //
  571. // Restore the saved CAN Control register.
  572. //
  573. HWREG(ui32Base + CAN_O_CTL) = ui16CANCTL;
  574. //
  575. // Return the computed bit rate.
  576. //
  577. return (ui32SourceClock / (ui32PreDivide * ui32CANBits));
  578. }
  579. }
  580. //
  581. // Move the divisor up one and look again. Only in rare cases are
  582. // more than 2 loops required to find the value.
  583. //
  584. ui32DesiredRatio++;
  585. }
  586. //
  587. // A valid combination could not be found, so return 0 to indicate that the
  588. // bit rate was not changed.
  589. //
  590. return (0);
  591. }
  592. //*****************************************************************************
  593. //
  594. //! Configures the CAN controller bit timing.
  595. //!
  596. //! \param ui32Base is the base address of the CAN controller.
  597. //! \param psClkParms points to the structure with the clock parameters.
  598. //!
  599. //! Configures the various timing parameters for the CAN bus bit timing:
  600. //! Propagation segment, Phase Buffer 1 segment, Phase Buffer 2 segment, and
  601. //! the Synchronization Jump Width. The values for Propagation and Phase
  602. //! Buffer 1 segments are derived from the combination
  603. //! \e psClkParms->ui32SyncPropPhase1Seg parameter. Phase Buffer 2 is
  604. //! determined from the \e psClkParms->ui32Phase2Seg parameter. These two
  605. //! parameters, along with \e psClkParms->ui32SJW are based in units of bit
  606. //! time quanta. The actual quantum time is determined by the
  607. //! \e psClkParms->ui32QuantumPrescaler value, which specifies the divisor for
  608. //! the CAN module clock.
  609. //!
  610. //! The total bit time, in quanta, is the sum of the two Seg parameters,
  611. //! as follows:
  612. //!
  613. //! bit_time_q = ui32SyncPropPhase1Seg + ui32Phase2Seg + 1
  614. //!
  615. //! Note that the Sync_Seg is always one quantum in duration, and is added
  616. //! to derive the correct duration of Prop_Seg and Phase1_Seg.
  617. //!
  618. //! The equation to determine the actual bit rate is as follows:
  619. //!
  620. //! CAN Clock /
  621. //! ((\e ui32SyncPropPhase1Seg + \e ui32Phase2Seg + 1) *
  622. //! (\e ui32QuantumPrescaler))
  623. //!
  624. //! Thus with \e ui32SyncPropPhase1Seg = 4, \e ui32Phase2Seg = 1,
  625. //! \e ui32QuantumPrescaler = 2 and an 8 MHz CAN clock, the bit rate is
  626. //! (8 MHz) / ((5 + 2 + 1) * 2) or 500 Kbit/sec.
  627. //!
  628. //! \return None.
  629. //
  630. //*****************************************************************************
  631. void
  632. CANBitTimingSet(uint32_t ui32Base, tCANBitClkParms *psClkParms)
  633. {
  634. uint32_t ui32BitReg, ui32SavedInit;
  635. //
  636. // Check the arguments.
  637. //
  638. ASSERT(_CANBaseValid(ui32Base));
  639. ASSERT(psClkParms);
  640. //
  641. // The phase 1 segment must be in the range from 2 to 16.
  642. //
  643. ASSERT((psClkParms->ui32SyncPropPhase1Seg >= 2) &&
  644. (psClkParms->ui32SyncPropPhase1Seg <= 16));
  645. //
  646. // The phase 2 segment must be in the range from 1 to 8.
  647. //
  648. ASSERT((psClkParms->ui32Phase2Seg >= 1) &&
  649. (psClkParms->ui32Phase2Seg <= 8));
  650. //
  651. // The synchronous jump windows must be in the range from 1 to 4.
  652. //
  653. ASSERT((psClkParms->ui32SJW >= 1) && (psClkParms->ui32SJW <= 4));
  654. //
  655. // The CAN clock pre-divider must be in the range from 1 to 1024.
  656. //
  657. ASSERT((psClkParms->ui32QuantumPrescaler <= 1024) &&
  658. (psClkParms->ui32QuantumPrescaler >= 1));
  659. //
  660. // To set the bit timing register, the controller must be placed in init
  661. // mode (if not already), and also configuration change bit enabled. State
  662. // of the init bit must be saved so it can be restored at the end.
  663. //
  664. ui32SavedInit = HWREG(ui32Base + CAN_O_CTL);
  665. HWREG(ui32Base + CAN_O_CTL) = ui32SavedInit | CAN_CTL_INIT | CAN_CTL_CCE;
  666. //
  667. // Set the bit fields of the bit timing register according to the parms.
  668. //
  669. ui32BitReg = (((psClkParms->ui32Phase2Seg - 1) << CAN_BIT_TSEG2_S) &
  670. CAN_BIT_TSEG2_M);
  671. ui32BitReg |= (((psClkParms->ui32SyncPropPhase1Seg - 1) <<
  672. CAN_BIT_TSEG1_S) & CAN_BIT_TSEG1_M);
  673. ui32BitReg |= ((psClkParms->ui32SJW - 1) << CAN_BIT_SJW_S) & CAN_BIT_SJW_M;
  674. ui32BitReg |= (psClkParms->ui32QuantumPrescaler - 1) & CAN_BIT_BRP_M;
  675. HWREG(ui32Base + CAN_O_BIT) = ui32BitReg;
  676. //
  677. // Set the divider upper bits in the extension register.
  678. //
  679. HWREG(ui32Base + CAN_O_BRPE) =
  680. ((psClkParms->ui32QuantumPrescaler - 1) >> 6) & CAN_BRPE_BRPE_M;
  681. //
  682. // Clear the config change bit, and restore the init bit.
  683. //
  684. ui32SavedInit &= ~CAN_CTL_CCE;
  685. //
  686. // If Init was not set before, then clear it.
  687. //
  688. if (ui32SavedInit & CAN_CTL_INIT)
  689. {
  690. ui32SavedInit &= ~CAN_CTL_INIT;
  691. }
  692. HWREG(ui32Base + CAN_O_CTL) = ui32SavedInit;
  693. }
  694. //*****************************************************************************
  695. //
  696. //! Registers an interrupt handler for the CAN controller.
  697. //!
  698. //! \param ui32Base is the base address of the CAN controller.
  699. //! \param pfnHandler is a pointer to the function to be called when the
  700. //! enabled CAN interrupts occur.
  701. //!
  702. //! This function registers the interrupt handler in the interrupt vector
  703. //! table, and enables CAN interrupts on the interrupt controller; specific CAN
  704. //! interrupt sources must be enabled using CANIntEnable(). The interrupt
  705. //! handler being registered must clear the source of the interrupt using
  706. //! CANIntClear().
  707. //!
  708. //! If the application is using a static interrupt vector table stored in
  709. //! flash, then it is not necessary to register the interrupt handler this way.
  710. //! Instead, IntEnable() is used to enable CAN interrupts on the
  711. //! interrupt controller.
  712. //!
  713. //! \sa IntRegister() for important information about registering interrupt
  714. //! handlers.
  715. //!
  716. //! \return None.
  717. //
  718. //*****************************************************************************
  719. void
  720. CANIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
  721. {
  722. uint_fast8_t ui8IntNumber;
  723. //
  724. // Check the arguments.
  725. //
  726. ASSERT(_CANBaseValid(ui32Base));
  727. //
  728. // Get the actual interrupt number for this CAN controller.
  729. //
  730. ui8IntNumber = _CANIntNumberGet(ui32Base);
  731. ASSERT(ui8IntNumber != 0);
  732. //
  733. // Register the interrupt handler.
  734. //
  735. IntRegister(ui8IntNumber, pfnHandler);
  736. //
  737. // Enable the Ethernet interrupt.
  738. //
  739. IntEnable(ui8IntNumber);
  740. }
  741. //*****************************************************************************
  742. //
  743. //! Unregisters an interrupt handler for the CAN controller.
  744. //!
  745. //! \param ui32Base is the base address of the controller.
  746. //!
  747. //! This function unregisters the previously registered interrupt handler and
  748. //! disables the interrupt in the interrupt controller.
  749. //!
  750. //! \sa IntRegister() for important information about registering interrupt
  751. //! handlers.
  752. //!
  753. //! \return None.
  754. //
  755. //*****************************************************************************
  756. void
  757. CANIntUnregister(uint32_t ui32Base)
  758. {
  759. uint_fast8_t ui8IntNumber;
  760. //
  761. // Check the arguments.
  762. //
  763. ASSERT(_CANBaseValid(ui32Base));
  764. //
  765. // Get the actual interrupt number for this CAN controller.
  766. //
  767. ui8IntNumber = _CANIntNumberGet(ui32Base);
  768. ASSERT(ui8IntNumber != 0);
  769. //
  770. // Disable the CAN interrupt.
  771. //
  772. IntDisable(ui8IntNumber);
  773. //
  774. // Register the interrupt handler.
  775. //
  776. IntUnregister(ui8IntNumber);
  777. }
  778. //*****************************************************************************
  779. //
  780. //! Enables individual CAN controller interrupt sources.
  781. //!
  782. //! \param ui32Base is the base address of the CAN controller.
  783. //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
  784. //!
  785. //! This function enables specific interrupt sources of the CAN controller.
  786. //! Only enabled sources cause a processor interrupt.
  787. //!
  788. //! The \e ui32IntFlags parameter is the logical OR of any of the following:
  789. //!
  790. //! - \b CAN_INT_ERROR - a controller error condition has occurred
  791. //! - \b CAN_INT_STATUS - a message transfer has completed, or a bus error has
  792. //! been detected
  793. //! - \b CAN_INT_MASTER - allow CAN controller to generate interrupts
  794. //!
  795. //! In order to generate any interrupts, \b CAN_INT_MASTER must be enabled.
  796. //! Further, for any particular transaction from a message object to generate
  797. //! an interrupt, that message object must have interrupts enabled (see
  798. //! CANMessageSet()). \b CAN_INT_ERROR generates an interrupt if the
  799. //! controller enters the ``bus off'' condition, or if the error counters reach
  800. //! a limit. \b CAN_INT_STATUS generates an interrupt under quite a few
  801. //! status conditions and may provide more interrupts than the application
  802. //! needs to handle. When an interrupt occurs, use CANIntStatus() to determine
  803. //! the cause.
  804. //!
  805. //! \return None.
  806. //
  807. //*****************************************************************************
  808. void
  809. CANIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
  810. {
  811. //
  812. // Check the arguments.
  813. //
  814. ASSERT(_CANBaseValid(ui32Base));
  815. ASSERT((ui32IntFlags & ~(CAN_CTL_EIE | CAN_CTL_SIE | CAN_CTL_IE)) == 0);
  816. //
  817. // Enable the specified interrupts.
  818. //
  819. HWREG(ui32Base + CAN_O_CTL) |= ui32IntFlags;
  820. }
  821. //*****************************************************************************
  822. //
  823. //! Disables individual CAN controller interrupt sources.
  824. //!
  825. //! \param ui32Base is the base address of the CAN controller.
  826. //! \param ui32IntFlags is the bit mask of the interrupt sources to be
  827. //! disabled.
  828. //!
  829. //! Disables the specified CAN controller interrupt sources. Only enabled
  830. //! interrupt sources can cause a processor interrupt.
  831. //!
  832. //! The \e ui32IntFlags parameter has the same definition as in the
  833. //! CANIntEnable() function.
  834. //!
  835. //! \return None.
  836. //
  837. //*****************************************************************************
  838. void
  839. CANIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
  840. {
  841. //
  842. // Check the arguments.
  843. //
  844. ASSERT(_CANBaseValid(ui32Base));
  845. ASSERT((ui32IntFlags & ~(CAN_CTL_EIE | CAN_CTL_SIE | CAN_CTL_IE)) == 0);
  846. //
  847. // Disable the specified interrupts.
  848. //
  849. HWREG(ui32Base + CAN_O_CTL) &= ~ui32IntFlags;
  850. }
  851. //*****************************************************************************
  852. //
  853. //! Returns the current CAN controller interrupt status.
  854. //!
  855. //! \param ui32Base is the base address of the CAN controller.
  856. //! \param eIntStsReg indicates which interrupt status register to read
  857. //!
  858. //! This function returns the value of one of two interrupt status registers.
  859. //! The interrupt status register read is determined by the \e eIntStsReg
  860. //! parameter, which can have one of the following values:
  861. //!
  862. //! - \b CAN_INT_STS_CAUSE - indicates the cause of the interrupt
  863. //! - \b CAN_INT_STS_OBJECT - indicates pending interrupts of all message
  864. //! objects
  865. //!
  866. //! \b CAN_INT_STS_CAUSE returns the value of the controller interrupt register
  867. //! and indicates the cause of the interrupt. The value returned is
  868. //! \b CAN_INT_INTID_STATUS if the cause is a status interrupt. In this case,
  869. //! the status register is read with the CANStatusGet() function.
  870. //! Calling this function to read the status also clears the status
  871. //! interrupt. If the value of the interrupt register is in the range 1-32,
  872. //! then this indicates the number of the highest priority message object that
  873. //! has an interrupt pending. The message object interrupt can be cleared by
  874. //! using the CANIntClear() function, or by reading the message using
  875. //! CANMessageGet() in the case of a received message. The interrupt handler
  876. //! can read the interrupt status again to make sure all pending interrupts are
  877. //! cleared before returning from the interrupt.
  878. //!
  879. //! \b CAN_INT_STS_OBJECT returns a bit mask indicating which message objects
  880. //! have pending interrupts. This value can be used to discover all of the
  881. //! pending interrupts at once, as opposed to repeatedly reading the interrupt
  882. //! register by using \b CAN_INT_STS_CAUSE.
  883. //!
  884. //! \return Returns the value of one of the interrupt status registers.
  885. //
  886. //*****************************************************************************
  887. uint32_t
  888. CANIntStatus(uint32_t ui32Base, tCANIntStsReg eIntStsReg)
  889. {
  890. uint32_t ui32Status;
  891. //
  892. // Check the arguments.
  893. //
  894. ASSERT(_CANBaseValid(ui32Base));
  895. //
  896. // See which status the caller is looking for.
  897. //
  898. switch (eIntStsReg)
  899. {
  900. //
  901. // The caller wants the global interrupt status for the CAN controller
  902. // specified by ui32Base.
  903. //
  904. case CAN_INT_STS_CAUSE:
  905. {
  906. ui32Status = HWREG(ui32Base + CAN_O_INT);
  907. break;
  908. }
  909. //
  910. // The caller wants the current message status interrupt for all
  911. // messages.
  912. //
  913. case CAN_INT_STS_OBJECT:
  914. {
  915. //
  916. // Read and combine both 16 bit values into one 32bit status.
  917. //
  918. ui32Status = (HWREG(ui32Base + CAN_O_MSG1INT) &
  919. CAN_MSG1INT_INTPND_M);
  920. ui32Status |= (HWREG(ui32Base + CAN_O_MSG2INT) << 16);
  921. break;
  922. }
  923. //
  924. // Request was for unknown status so just return 0.
  925. //
  926. default:
  927. {
  928. ui32Status = 0;
  929. break;
  930. }
  931. }
  932. //
  933. // Return the interrupt status value
  934. //
  935. return (ui32Status);
  936. }
  937. //*****************************************************************************
  938. //
  939. //! Clears a CAN interrupt source.
  940. //!
  941. //! \param ui32Base is the base address of the CAN controller.
  942. //! \param ui32IntClr is a value indicating which interrupt source to clear.
  943. //!
  944. //! This function can be used to clear a specific interrupt source. The
  945. //! \e ui32IntClr parameter must be one of the following values:
  946. //!
  947. //! - \b CAN_INT_INTID_STATUS - Clears a status interrupt.
  948. //! - 1-32 - Clears the specified message object interrupt
  949. //!
  950. //! It is not necessary to use this function to clear an interrupt. This
  951. //! function is only used if the application wants to clear an interrupt
  952. //! source without taking the normal interrupt action.
  953. //!
  954. //! Normally, the status interrupt is cleared by reading the controller status
  955. //! using CANStatusGet(). A specific message object interrupt is normally
  956. //! cleared by reading the message object using CANMessageGet().
  957. //!
  958. //! \note Because there is a write buffer in the Cortex-M processor, it may
  959. //! take several clock cycles before the interrupt source is actually cleared.
  960. //! Therefore, it is recommended that the interrupt source be cleared early in
  961. //! the interrupt handler (as opposed to the very last action) to avoid
  962. //! returning from the interrupt handler before the interrupt source is
  963. //! actually cleared. Failure to do so may result in the interrupt handler
  964. //! being immediately reentered (because the interrupt controller still sees
  965. //! the interrupt source asserted).
  966. //!
  967. //! \return None.
  968. //
  969. //*****************************************************************************
  970. void
  971. CANIntClear(uint32_t ui32Base, uint32_t ui32IntClr)
  972. {
  973. //
  974. // Check the arguments.
  975. //
  976. ASSERT(_CANBaseValid(ui32Base));
  977. ASSERT((ui32IntClr == CAN_INT_INTID_STATUS) ||
  978. ((ui32IntClr >= 1) && (ui32IntClr <= 32)));
  979. if (ui32IntClr == CAN_INT_INTID_STATUS)
  980. {
  981. //
  982. // Simply read and discard the status to clear the interrupt.
  983. //
  984. HWREG(ui32Base + CAN_O_STS);
  985. }
  986. else
  987. {
  988. //
  989. // Wait to be sure that this interface is not busy.
  990. //
  991. while (HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  992. {
  993. }
  994. //
  995. // Only change the interrupt pending state by setting only the
  996. // CAN_IF1CMSK_CLRINTPND bit.
  997. //
  998. HWREG(ui32Base + CAN_O_IF1CMSK) = CAN_IF1CMSK_CLRINTPND;
  999. //
  1000. // Send the clear pending interrupt command to the CAN controller.
  1001. //
  1002. HWREG(ui32Base + CAN_O_IF1CRQ) = ui32IntClr & CAN_IF1CRQ_MNUM_M;
  1003. //
  1004. // Wait to be sure that this interface is not busy.
  1005. //
  1006. while (HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1007. {
  1008. }
  1009. }
  1010. }
  1011. //*****************************************************************************
  1012. //
  1013. //! Sets the CAN controller automatic retransmission behavior.
  1014. //!
  1015. //! \param ui32Base is the base address of the CAN controller.
  1016. //! \param bAutoRetry enables automatic retransmission.
  1017. //!
  1018. //! This function enables or disables automatic retransmission of messages with
  1019. //! detected errors. If \e bAutoRetry is \b true, then automatic
  1020. //! retransmission is enabled, otherwise it is disabled.
  1021. //!
  1022. //! \return None.
  1023. //
  1024. //*****************************************************************************
  1025. void
  1026. CANRetrySet(uint32_t ui32Base, bool bAutoRetry)
  1027. {
  1028. uint32_t ui32CtlReg;
  1029. //
  1030. // Check the arguments.
  1031. //
  1032. ASSERT(_CANBaseValid(ui32Base));
  1033. ui32CtlReg = HWREG(ui32Base + CAN_O_CTL);
  1034. //
  1035. // Conditionally set the DAR bit to enable/disable auto-retry.
  1036. //
  1037. if (bAutoRetry)
  1038. {
  1039. //
  1040. // Clearing the DAR bit tells the controller to not disable the
  1041. // auto-retry of messages which were not transmitted or received
  1042. // correctly.
  1043. //
  1044. ui32CtlReg &= ~CAN_CTL_DAR;
  1045. }
  1046. else
  1047. {
  1048. //
  1049. // Setting the DAR bit tells the controller to disable the auto-retry
  1050. // of messages which were not transmitted or received correctly.
  1051. //
  1052. ui32CtlReg |= CAN_CTL_DAR;
  1053. }
  1054. HWREG(ui32Base + CAN_O_CTL) = ui32CtlReg;
  1055. }
  1056. //*****************************************************************************
  1057. //
  1058. //! Returns the current setting for automatic retransmission.
  1059. //!
  1060. //! \param ui32Base is the base address of the CAN controller.
  1061. //!
  1062. //! This function reads the current setting for automatic retransmission in the
  1063. //! CAN controller and returns it to the caller.
  1064. //!
  1065. //! \return Returns \b true if automatic retransmission is enabled, \b false
  1066. //! otherwise.
  1067. //
  1068. //*****************************************************************************
  1069. bool
  1070. CANRetryGet(uint32_t ui32Base)
  1071. {
  1072. //
  1073. // Check the arguments.
  1074. //
  1075. ASSERT(_CANBaseValid(ui32Base));
  1076. //
  1077. // Read the disable automatic retry setting from the CAN controller.
  1078. //
  1079. if (HWREG(ui32Base + CAN_O_CTL) & CAN_CTL_DAR)
  1080. {
  1081. //
  1082. // Automatic data retransmission is not enabled.
  1083. //
  1084. return (false);
  1085. }
  1086. //
  1087. // Automatic data retransmission is enabled.
  1088. //
  1089. return (true);
  1090. }
  1091. //*****************************************************************************
  1092. //
  1093. //! Reads one of the controller status registers.
  1094. //!
  1095. //! \param ui32Base is the base address of the CAN controller.
  1096. //! \param eStatusReg is the status register to read.
  1097. //!
  1098. //! This function reads a status register of the CAN controller and returns it
  1099. //! to the caller.
  1100. //! The different status registers are:
  1101. //!
  1102. //! - \b CAN_STS_CONTROL - the main controller status
  1103. //! - \b CAN_STS_TXREQUEST - bit mask of objects pending transmission
  1104. //! - \b CAN_STS_NEWDAT - bit mask of objects with new data
  1105. //! - \b CAN_STS_MSGVAL - bit mask of objects with valid configuration
  1106. //!
  1107. //! When reading the main controller status register, a pending status
  1108. //! interrupt is cleared. This parameter is used in the interrupt
  1109. //! handler for the CAN controller if the cause is a status interrupt. The
  1110. //! controller status register fields are as follows:
  1111. //!
  1112. //! - \b CAN_STATUS_BUS_OFF - controller is in bus-off condition
  1113. //! - \b CAN_STATUS_EWARN - an error counter has reached a limit of at least 96
  1114. //! - \b CAN_STATUS_EPASS - CAN controller is in the error passive state
  1115. //! - \b CAN_STATUS_RXOK - a message was received successfully (independent of
  1116. //! any message filtering).
  1117. //! - \b CAN_STATUS_TXOK - a message was successfully transmitted
  1118. //! - \b CAN_STATUS_LEC_MSK - mask of last error code bits (3 bits)
  1119. //! - \b CAN_STATUS_LEC_NONE - no error
  1120. //! - \b CAN_STATUS_LEC_STUFF - stuffing error detected
  1121. //! - \b CAN_STATUS_LEC_FORM - a format error occurred in the fixed format part
  1122. //! of a message
  1123. //! - \b CAN_STATUS_LEC_ACK - a transmitted message was not acknowledged
  1124. //! - \b CAN_STATUS_LEC_BIT1 - dominant level detected when trying to send in
  1125. //! recessive mode
  1126. //! - \b CAN_STATUS_LEC_BIT0 - recessive level detected when trying to send in
  1127. //! dominant mode
  1128. //! - \b CAN_STATUS_LEC_CRC - CRC error in received message
  1129. //!
  1130. //! The remaining status registers consist of 32-bit-wide bit maps to the
  1131. //! message objects. They can be used to quickly obtain information about the
  1132. //! status of all the message objects without needing to query each one. They
  1133. //! contain the following information:
  1134. //!
  1135. //! - \b CAN_STS_TXREQUEST - if a message object's TXRQST bit is set, a
  1136. //! transmission is pending on that object. The application can use this
  1137. //! information to determine which objects are still waiting to send a
  1138. //! message.
  1139. //! - \b CAN_STS_NEWDAT - if a message object's NEWDAT bit is set, a new
  1140. //! message has been received in that object, and has not yet been picked up
  1141. //! by the host application
  1142. //! - \b CAN_STS_MSGVAL - if a message object's MSGVAL bit is set, the object
  1143. //! has a valid configuration programmed. The host application can use this
  1144. //! information to determine which message objects are empty/unused.
  1145. //!
  1146. //! \return Returns the value of the status register.
  1147. //
  1148. //*****************************************************************************
  1149. uint32_t
  1150. CANStatusGet(uint32_t ui32Base, tCANStsReg eStatusReg)
  1151. {
  1152. uint32_t ui32Status;
  1153. //
  1154. // Check the arguments.
  1155. //
  1156. ASSERT(_CANBaseValid(ui32Base));
  1157. switch (eStatusReg)
  1158. {
  1159. //
  1160. // Just return the global CAN status register since that is what was
  1161. // requested.
  1162. //
  1163. case CAN_STS_CONTROL:
  1164. {
  1165. ui32Status = HWREG(ui32Base + CAN_O_STS);
  1166. HWREG(ui32Base + CAN_O_STS) = ~(CAN_STS_RXOK | CAN_STS_TXOK |
  1167. CAN_STS_LEC_M);
  1168. break;
  1169. }
  1170. //
  1171. // Combine the Transmit status bits into one 32bit value.
  1172. //
  1173. case CAN_STS_TXREQUEST:
  1174. {
  1175. ui32Status = HWREG(ui32Base + CAN_O_TXRQ1);
  1176. ui32Status |= HWREG(ui32Base + CAN_O_TXRQ2) << 16;
  1177. break;
  1178. }
  1179. //
  1180. // Combine the New Data status bits into one 32bit value.
  1181. //
  1182. case CAN_STS_NEWDAT:
  1183. {
  1184. ui32Status = HWREG(ui32Base + CAN_O_NWDA1);
  1185. ui32Status |= HWREG(ui32Base + CAN_O_NWDA2) << 16;
  1186. break;
  1187. }
  1188. //
  1189. // Combine the Message valid status bits into one 32bit value.
  1190. //
  1191. case CAN_STS_MSGVAL:
  1192. {
  1193. ui32Status = HWREG(ui32Base + CAN_O_MSG1VAL);
  1194. ui32Status |= HWREG(ui32Base + CAN_O_MSG2VAL) << 16;
  1195. break;
  1196. }
  1197. //
  1198. // Unknown CAN status requested so return 0.
  1199. //
  1200. default:
  1201. {
  1202. ui32Status = 0;
  1203. break;
  1204. }
  1205. }
  1206. return (ui32Status);
  1207. }
  1208. //*****************************************************************************
  1209. //
  1210. //! Reads the CAN controller error counter register.
  1211. //!
  1212. //! \param ui32Base is the base address of the CAN controller.
  1213. //! \param pui32RxCount is a pointer to storage for the receive error counter.
  1214. //! \param pui32TxCount is a pointer to storage for the transmit error counter.
  1215. //!
  1216. //! This function reads the error counter register and returns the transmit and
  1217. //! receive error counts to the caller along with a flag indicating if the
  1218. //! controller receive counter has reached the error passive limit. The values
  1219. //! of the receive and transmit error counters are returned through the
  1220. //! pointers provided as parameters.
  1221. //!
  1222. //! After this call, \e *pui32RxCount holds the current receive error count
  1223. //! and \e *pui32TxCount holds the current transmit error count.
  1224. //!
  1225. //! \return Returns \b true if the receive error count has reached the error
  1226. //! passive limit, and \b false if the error count is below the error passive
  1227. //! limit.
  1228. //
  1229. //*****************************************************************************
  1230. bool
  1231. CANErrCntrGet(uint32_t ui32Base, uint32_t *pui32RxCount,
  1232. uint32_t *pui32TxCount)
  1233. {
  1234. uint32_t ui32CANError;
  1235. //
  1236. // Check the arguments.
  1237. //
  1238. ASSERT(_CANBaseValid(ui32Base));
  1239. //
  1240. // Read the current count of transmit/receive errors.
  1241. //
  1242. ui32CANError = HWREG(ui32Base + CAN_O_ERR);
  1243. //
  1244. // Extract the error numbers from the register value.
  1245. //
  1246. *pui32RxCount = (ui32CANError & CAN_ERR_REC_M) >> CAN_ERR_REC_S;
  1247. *pui32TxCount = (ui32CANError & CAN_ERR_TEC_M) >> CAN_ERR_TEC_S;
  1248. if (ui32CANError & CAN_ERR_RP)
  1249. {
  1250. return (true);
  1251. }
  1252. return (false);
  1253. }
  1254. //*****************************************************************************
  1255. //
  1256. //! Configures a message object in the CAN controller.
  1257. //!
  1258. //! \param ui32Base is the base address of the CAN controller.
  1259. //! \param ui32ObjID is the object number to configure (1-32).
  1260. //! \param psMsgObject is a pointer to a structure containing message object
  1261. //! settings.
  1262. //! \param eMsgType indicates the type of message for this object.
  1263. //!
  1264. //! This function is used to configure any one of the 32 message objects in the
  1265. //! CAN controller. A message object can be configured to be any type of CAN
  1266. //! message object as well as to use automatic transmission and reception.
  1267. //! This call also allows the message object to be configured to generate
  1268. //! interrupts on completion of message receipt or transmission. The
  1269. //! message object can also be configured with a filter/mask so that actions
  1270. //! are only taken when a message that meets certain parameters is seen on the
  1271. //! CAN bus.
  1272. //!
  1273. //! The \e eMsgType parameter must be one of the following values:
  1274. //!
  1275. //! - \b MSG_OBJ_TYPE_TX - CAN transmit message object.
  1276. //! - \b MSG_OBJ_TYPE_TX_REMOTE - CAN transmit remote request message object.
  1277. //! - \b MSG_OBJ_TYPE_RX - CAN receive message object.
  1278. //! - \b MSG_OBJ_TYPE_RX_REMOTE - CAN receive remote request message object.
  1279. //! - \b MSG_OBJ_TYPE_RXTX_REMOTE - CAN remote frame receive remote, then
  1280. //! transmit message object.
  1281. //!
  1282. //! The message object pointed to by \e psMsgObject must be populated by the
  1283. //! caller, as follows:
  1284. //!
  1285. //! - \e ui32MsgID - contains the message ID, either 11 or 29 bits.
  1286. //! - \e ui32MsgIDMask - mask of bits from \e ui32MsgID that must match if
  1287. //! identifier filtering is enabled.
  1288. //! - \e ui32Flags
  1289. //! - Set \b MSG_OBJ_TX_INT_ENABLE flag to enable interrupt on transmission.
  1290. //! - Set \b MSG_OBJ_RX_INT_ENABLE flag to enable interrupt on receipt.
  1291. //! - Set \b MSG_OBJ_USE_ID_FILTER flag to enable filtering based on the
  1292. //! identifier mask specified by \e ui32MsgIDMask.
  1293. //! - \e ui32MsgLen - the number of bytes in the message data. This parameter
  1294. //! must be non-zero even for a remote frame; it must match the expected
  1295. //! bytes of data in the responding data frame.
  1296. //! - \e pui8MsgData - points to a buffer containing up to 8 bytes of data for
  1297. //! a data frame.
  1298. //!
  1299. //! \b Example: To send a data frame or remote frame (in response to a remote
  1300. //! request), take the following steps:
  1301. //!
  1302. //! -# Set \e eMsgType to \b MSG_OBJ_TYPE_TX.
  1303. //! -# Set \e psMsgObject->ui32MsgID to the message ID.
  1304. //! -# Set \e psMsgObject->ui32Flags. Make sure to set
  1305. //! \b MSG_OBJ_TX_INT_ENABLE to allow an interrupt to be generated when the
  1306. //! message is sent.
  1307. //! -# Set \e psMsgObject->ui32MsgLen to the number of bytes in the data frame.
  1308. //! -# Set \e psMsgObject->pui8MsgData to point to an array containing the
  1309. //! bytes to send in the message.
  1310. //! -# Call this function with \e ui32ObjID set to one of the 32 object
  1311. //! buffers.
  1312. //!
  1313. //! \b Example: To receive a specific data frame, take the following steps:
  1314. //!
  1315. //! -# Set \e eMsgObjType to \b MSG_OBJ_TYPE_RX.
  1316. //! -# Set \e psMsgObject->ui32MsgID to the full message ID, or a partial mask
  1317. //! to use partial ID matching.
  1318. //! -# Set \e psMsgObject->ui32MsgIDMask bits that are used for masking
  1319. //! during comparison.
  1320. //! -# Set \e psMsgObject->ui32Flags as follows:
  1321. //! - Set \b MSG_OBJ_RX_INT_ENABLE flag to be interrupted when the data
  1322. //! frame is received.
  1323. //! - Set \b MSG_OBJ_USE_ID_FILTER flag to enable identifier-based
  1324. //! filtering.
  1325. //! -# Set \e psMsgObject->ui32MsgLen to the number of bytes in the expected
  1326. //! data frame.
  1327. //! -# The buffer pointed to by \e psMsgObject->pui8MsgData is not used by this
  1328. //! call as no data is present at the time of the call.
  1329. //! -# Call this function with \e ui32ObjID set to one of the 32 object
  1330. //! buffers.
  1331. //!
  1332. //! If you specify a message object buffer that already contains a message
  1333. //! definition, it is overwritten.
  1334. //!
  1335. //! \return None.
  1336. //
  1337. //*****************************************************************************
  1338. void
  1339. CANMessageSet(uint32_t ui32Base, uint32_t ui32ObjID,
  1340. tCANMsgObject *psMsgObject, tMsgObjType eMsgType)
  1341. {
  1342. uint16_t ui16CmdMaskReg;
  1343. uint16_t ui16MaskReg0, ui16MaskReg1;
  1344. uint16_t ui16ArbReg0, ui16ArbReg1;
  1345. uint16_t ui16MsgCtrl;
  1346. bool bTransferData;
  1347. bool bUseExtendedID;
  1348. bTransferData = 0;
  1349. //
  1350. // Check the arguments.
  1351. //
  1352. ASSERT(_CANBaseValid(ui32Base));
  1353. ASSERT((ui32ObjID <= 32) && (ui32ObjID != 0));
  1354. ASSERT((eMsgType == MSG_OBJ_TYPE_TX) ||
  1355. (eMsgType == MSG_OBJ_TYPE_TX_REMOTE) ||
  1356. (eMsgType == MSG_OBJ_TYPE_RX) ||
  1357. (eMsgType == MSG_OBJ_TYPE_RX_REMOTE) ||
  1358. (eMsgType == MSG_OBJ_TYPE_TX_REMOTE) ||
  1359. (eMsgType == MSG_OBJ_TYPE_RXTX_REMOTE));
  1360. //
  1361. // Wait for busy bit to clear
  1362. //
  1363. while (HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1364. {
  1365. }
  1366. //
  1367. // See if we need to use an extended identifier or not.
  1368. //
  1369. if ((psMsgObject->ui32MsgID > CAN_MAX_11BIT_MSG_ID) ||
  1370. (psMsgObject->ui32Flags & MSG_OBJ_EXTENDED_ID))
  1371. {
  1372. bUseExtendedID = 1;
  1373. }
  1374. else
  1375. {
  1376. bUseExtendedID = 0;
  1377. }
  1378. //
  1379. // This is always a write to the Message object as this call is setting a
  1380. // message object. This call always sets all size bits so it sets
  1381. // both data bits. The call uses the CONTROL register to set control
  1382. // bits so this bit needs to be set as well.
  1383. //
  1384. ui16CmdMaskReg = (CAN_IF1CMSK_WRNRD | CAN_IF1CMSK_DATAA |
  1385. CAN_IF1CMSK_DATAB | CAN_IF1CMSK_CONTROL);
  1386. //
  1387. // Initialize the values to a known state before filling them in based on
  1388. // the type of message object that is being configured.
  1389. //
  1390. ui16ArbReg0 = 0;
  1391. ui16ArbReg1 = 0;
  1392. ui16MsgCtrl = 0;
  1393. ui16MaskReg0 = 0;
  1394. ui16MaskReg1 = 0;
  1395. switch (eMsgType)
  1396. {
  1397. //
  1398. // Transmit message object.
  1399. //
  1400. case MSG_OBJ_TYPE_TX:
  1401. {
  1402. //
  1403. // Set the TXRQST bit and the reset the rest of the register.
  1404. //
  1405. ui16MsgCtrl |= CAN_IF1MCTL_TXRQST;
  1406. ui16ArbReg1 = CAN_IF1ARB2_DIR;
  1407. bTransferData = 1;
  1408. break;
  1409. }
  1410. //
  1411. // Transmit remote request message object
  1412. //
  1413. case MSG_OBJ_TYPE_TX_REMOTE:
  1414. {
  1415. //
  1416. // Set the TXRQST bit and the reset the rest of the register.
  1417. //
  1418. ui16MsgCtrl |= CAN_IF1MCTL_TXRQST;
  1419. ui16ArbReg1 = 0;
  1420. break;
  1421. }
  1422. //
  1423. // Receive message object.
  1424. //
  1425. case MSG_OBJ_TYPE_RX:
  1426. {
  1427. //
  1428. // This clears the DIR bit along with everything else. The TXRQST
  1429. // bit was cleared by defaulting ui16MsgCtrl to 0.
  1430. //
  1431. ui16ArbReg1 = 0;
  1432. break;
  1433. }
  1434. //
  1435. // Receive remote request message object.
  1436. //
  1437. case MSG_OBJ_TYPE_RX_REMOTE:
  1438. {
  1439. //
  1440. // The DIR bit is set to one for remote receivers. The TXRQST bit
  1441. // was cleared by defaulting ui16MsgCtrl to 0.
  1442. //
  1443. ui16ArbReg1 = CAN_IF1ARB2_DIR;
  1444. //
  1445. // Set this object so that it only indicates that a remote frame
  1446. // was received and allow for software to handle it by sending back
  1447. // a data frame.
  1448. //
  1449. ui16MsgCtrl = CAN_IF1MCTL_UMASK;
  1450. //
  1451. // Use the full Identifier by default.
  1452. //
  1453. ui16MaskReg0 = 0xffff;
  1454. ui16MaskReg1 = 0x1fff;
  1455. //
  1456. // Make sure to send the mask to the message object.
  1457. //
  1458. ui16CmdMaskReg |= CAN_IF1CMSK_MASK;
  1459. break;
  1460. }
  1461. //
  1462. // Remote frame receive remote, with auto-transmit message object.
  1463. //
  1464. case MSG_OBJ_TYPE_RXTX_REMOTE:
  1465. {
  1466. //
  1467. // Oddly the DIR bit is set to one for remote receivers.
  1468. //
  1469. ui16ArbReg1 = CAN_IF1ARB2_DIR;
  1470. //
  1471. // Set this object to auto answer if a matching identifier is seen.
  1472. //
  1473. ui16MsgCtrl = CAN_IF1MCTL_RMTEN | CAN_IF1MCTL_UMASK;
  1474. //
  1475. // The data to be returned needs to be filled in.
  1476. //
  1477. bTransferData = 1;
  1478. break;
  1479. }
  1480. //
  1481. // This case never happens due to the ASSERT statement at the
  1482. // beginning of this function.
  1483. //
  1484. default:
  1485. {
  1486. return;
  1487. }
  1488. }
  1489. //
  1490. // Configure the Mask Registers.
  1491. //
  1492. if (psMsgObject->ui32Flags & MSG_OBJ_USE_ID_FILTER)
  1493. {
  1494. if (bUseExtendedID)
  1495. {
  1496. //
  1497. // Set the 29 bits of Identifier mask that were requested.
  1498. //
  1499. ui16MaskReg0 = psMsgObject->ui32MsgIDMask & CAN_IF1MSK1_IDMSK_M;
  1500. ui16MaskReg1 = ((psMsgObject->ui32MsgIDMask >> 16) &
  1501. CAN_IF1MSK2_IDMSK_M);
  1502. }
  1503. else
  1504. {
  1505. //
  1506. // Lower 16 bit are unused so set them to zero.
  1507. //
  1508. ui16MaskReg0 = 0;
  1509. //
  1510. // Put the 11 bit Mask Identifier into the upper bits of the field
  1511. // in the register.
  1512. //
  1513. ui16MaskReg1 = ((psMsgObject->ui32MsgIDMask << 2) &
  1514. CAN_IF1MSK2_IDMSK_M);
  1515. }
  1516. }
  1517. //
  1518. // If the caller wants to filter on the extended ID bit then set it.
  1519. //
  1520. if ((psMsgObject->ui32Flags & MSG_OBJ_USE_EXT_FILTER) ==
  1521. MSG_OBJ_USE_EXT_FILTER)
  1522. {
  1523. ui16MaskReg1 |= CAN_IF1MSK2_MXTD;
  1524. }
  1525. //
  1526. // The caller wants to filter on the message direction field.
  1527. //
  1528. if ((psMsgObject->ui32Flags & MSG_OBJ_USE_DIR_FILTER) ==
  1529. MSG_OBJ_USE_DIR_FILTER)
  1530. {
  1531. ui16MaskReg1 |= CAN_IF1MSK2_MDIR;
  1532. }
  1533. if (psMsgObject->ui32Flags &
  1534. (MSG_OBJ_USE_ID_FILTER | MSG_OBJ_USE_DIR_FILTER |
  1535. MSG_OBJ_USE_EXT_FILTER))
  1536. {
  1537. //
  1538. // Set the UMASK bit to enable using the mask register.
  1539. //
  1540. ui16MsgCtrl |= CAN_IF1MCTL_UMASK;
  1541. //
  1542. // Set the MASK bit so that this gets transferred to the Message
  1543. // Object.
  1544. //
  1545. ui16CmdMaskReg |= CAN_IF1CMSK_MASK;
  1546. }
  1547. //
  1548. // Set the Arb bit so that this gets transferred to the Message object.
  1549. //
  1550. ui16CmdMaskReg |= CAN_IF1CMSK_ARB;
  1551. //
  1552. // Configure the Arbitration registers.
  1553. //
  1554. if (bUseExtendedID)
  1555. {
  1556. //
  1557. // Set the 29 bit version of the Identifier for this message object.
  1558. //
  1559. ui16ArbReg0 |= psMsgObject->ui32MsgID & CAN_IF1ARB1_ID_M;
  1560. ui16ArbReg1 |= (psMsgObject->ui32MsgID >> 16) & CAN_IF1ARB2_ID_M;
  1561. //
  1562. // Mark the message as valid and set the extended ID bit.
  1563. //
  1564. ui16ArbReg1 |= CAN_IF1ARB2_MSGVAL | CAN_IF1ARB2_XTD;
  1565. }
  1566. else
  1567. {
  1568. //
  1569. // Set the 11 bit version of the Identifier for this message object.
  1570. // The lower 18 bits are set to zero.
  1571. //
  1572. ui16ArbReg1 |= (psMsgObject->ui32MsgID << 2) & CAN_IF1ARB2_ID_M;
  1573. //
  1574. // Mark the message as valid.
  1575. //
  1576. ui16ArbReg1 |= CAN_IF1ARB2_MSGVAL;
  1577. }
  1578. //
  1579. // Set the data length since this is set for all transfers. This is also a
  1580. // single transfer and not a FIFO transfer so set EOB bit.
  1581. //
  1582. ui16MsgCtrl |= (psMsgObject->ui32MsgLen & CAN_IF1MCTL_DLC_M);
  1583. //
  1584. // Mark this as the last entry if this is not the last entry in a FIFO.
  1585. //
  1586. if ((psMsgObject->ui32Flags & MSG_OBJ_FIFO) == 0)
  1587. {
  1588. ui16MsgCtrl |= CAN_IF1MCTL_EOB;
  1589. }
  1590. //
  1591. // Enable transmit interrupts if they should be enabled.
  1592. //
  1593. if (psMsgObject->ui32Flags & MSG_OBJ_TX_INT_ENABLE)
  1594. {
  1595. ui16MsgCtrl |= CAN_IF1MCTL_TXIE;
  1596. }
  1597. //
  1598. // Enable receive interrupts if they should be enabled.
  1599. //
  1600. if (psMsgObject->ui32Flags & MSG_OBJ_RX_INT_ENABLE)
  1601. {
  1602. ui16MsgCtrl |= CAN_IF1MCTL_RXIE;
  1603. }
  1604. //
  1605. // Write the data out to the CAN Data registers if needed.
  1606. //
  1607. if (bTransferData)
  1608. {
  1609. _CANDataRegWrite(psMsgObject->pui8MsgData,
  1610. (uint32_t *)(ui32Base + CAN_O_IF1DA1),
  1611. psMsgObject->ui32MsgLen);
  1612. }
  1613. //
  1614. // Write out the registers to program the message object.
  1615. //
  1616. HWREG(ui32Base + CAN_O_IF1CMSK) = ui16CmdMaskReg;
  1617. HWREG(ui32Base + CAN_O_IF1MSK1) = ui16MaskReg0;
  1618. HWREG(ui32Base + CAN_O_IF1MSK2) = ui16MaskReg1;
  1619. HWREG(ui32Base + CAN_O_IF1ARB1) = ui16ArbReg0;
  1620. HWREG(ui32Base + CAN_O_IF1ARB2) = ui16ArbReg1;
  1621. HWREG(ui32Base + CAN_O_IF1MCTL) = ui16MsgCtrl;
  1622. //
  1623. // Transfer the message object to the message object specified by
  1624. // ui32ObjID.
  1625. //
  1626. HWREG(ui32Base + CAN_O_IF1CRQ) = ui32ObjID & CAN_IF1CRQ_MNUM_M;
  1627. }
  1628. //*****************************************************************************
  1629. //
  1630. //! Reads a CAN message from one of the message object buffers.
  1631. //!
  1632. //! \param ui32Base is the base address of the CAN controller.
  1633. //! \param ui32ObjID is the object number to read (1-32).
  1634. //! \param psMsgObject points to a structure containing message object fields.
  1635. //! \param bClrPendingInt indicates whether an associated interrupt should be
  1636. //! cleared.
  1637. //!
  1638. //! This function is used to read the contents of one of the 32 message objects
  1639. //! in the CAN controller and return it to the caller. The data returned is
  1640. //! stored in the fields of the caller-supplied structure pointed to by
  1641. //! \e psMsgObject. The data consists of all of the parts of a CAN message,
  1642. //! plus some control and status information.
  1643. //!
  1644. //! Normally, this function is used to read a message object that has received
  1645. //! and stored a CAN message with a certain identifier. However, this function
  1646. //! could also be used to read the contents of a message object in order to
  1647. //! load the fields of the structure in case only part of the structure must
  1648. //! be changed from a previous setting.
  1649. //!
  1650. //! When using CANMessageGet(), all of the same fields of the structure are
  1651. //! populated in the same way as when the CANMessageSet() function is used,
  1652. //! with the following exceptions:
  1653. //!
  1654. //! \e psMsgObject->ui32Flags:
  1655. //!
  1656. //! - \b MSG_OBJ_NEW_DATA indicates if this data is new since the last time it
  1657. //! was read
  1658. //! - \b MSG_OBJ_DATA_LOST indicates that at least one message was received on
  1659. //! this message object and not read by the host before being overwritten.
  1660. //!
  1661. //! \return None.
  1662. //
  1663. //*****************************************************************************
  1664. void
  1665. CANMessageGet(uint32_t ui32Base, uint32_t ui32ObjID,
  1666. tCANMsgObject *psMsgObject, bool bClrPendingInt)
  1667. {
  1668. uint16_t ui16CmdMaskReg;
  1669. uint16_t ui16MaskReg0, ui16MaskReg1;
  1670. uint16_t ui16ArbReg0, ui16ArbReg1;
  1671. uint16_t ui16MsgCtrl;
  1672. //
  1673. // Check the arguments.
  1674. //
  1675. ASSERT(_CANBaseValid(ui32Base));
  1676. ASSERT((ui32ObjID <= 32) && (ui32ObjID != 0));
  1677. //
  1678. // This is always a read to the Message object as this call is setting a
  1679. // message object.
  1680. //
  1681. ui16CmdMaskReg = (CAN_IF1CMSK_DATAA | CAN_IF1CMSK_DATAB |
  1682. CAN_IF1CMSK_CONTROL | CAN_IF1CMSK_MASK |
  1683. CAN_IF1CMSK_ARB);
  1684. //
  1685. // Clear a pending interrupt and new data in a message object.
  1686. //
  1687. if (bClrPendingInt)
  1688. {
  1689. ui16CmdMaskReg |= CAN_IF1CMSK_CLRINTPND;
  1690. }
  1691. //
  1692. // Set up the request for data from the message object.
  1693. //
  1694. HWREG(ui32Base + CAN_O_IF2CMSK) = ui16CmdMaskReg;
  1695. //
  1696. // Transfer the message object to the message object specified by
  1697. // ui32ObjID.
  1698. //
  1699. HWREG(ui32Base + CAN_O_IF2CRQ) = ui32ObjID & CAN_IF1CRQ_MNUM_M;
  1700. //
  1701. // Wait for busy bit to clear
  1702. //
  1703. while (HWREG(ui32Base + CAN_O_IF2CRQ) & CAN_IF1CRQ_BUSY)
  1704. {
  1705. }
  1706. //
  1707. // Read out the IF Registers.
  1708. //
  1709. ui16MaskReg0 = HWREG(ui32Base + CAN_O_IF2MSK1);
  1710. ui16MaskReg1 = HWREG(ui32Base + CAN_O_IF2MSK2);
  1711. ui16ArbReg0 = HWREG(ui32Base + CAN_O_IF2ARB1);
  1712. ui16ArbReg1 = HWREG(ui32Base + CAN_O_IF2ARB2);
  1713. ui16MsgCtrl = HWREG(ui32Base + CAN_O_IF2MCTL);
  1714. psMsgObject->ui32Flags = MSG_OBJ_NO_FLAGS;
  1715. //
  1716. // Determine if this is a remote frame by checking the TXRQST and DIR bits.
  1717. //
  1718. if ((!(ui16MsgCtrl & CAN_IF1MCTL_TXRQST) &&
  1719. (ui16ArbReg1 & CAN_IF1ARB2_DIR)) ||
  1720. ((ui16MsgCtrl & CAN_IF1MCTL_TXRQST) &&
  1721. (!(ui16ArbReg1 & CAN_IF1ARB2_DIR))))
  1722. {
  1723. psMsgObject->ui32Flags |= MSG_OBJ_REMOTE_FRAME;
  1724. }
  1725. //
  1726. // Get the identifier out of the register, the format depends on size of
  1727. // the mask.
  1728. //
  1729. if (ui16ArbReg1 & CAN_IF1ARB2_XTD)
  1730. {
  1731. //
  1732. // Set the 29 bit version of the Identifier for this message object.
  1733. //
  1734. psMsgObject->ui32MsgID = (((ui16ArbReg1 & CAN_IF1ARB2_ID_M) << 16) |
  1735. ui16ArbReg0);
  1736. psMsgObject->ui32Flags |= MSG_OBJ_EXTENDED_ID;
  1737. }
  1738. else
  1739. {
  1740. //
  1741. // The Identifier is an 11 bit value.
  1742. //
  1743. psMsgObject->ui32MsgID = (ui16ArbReg1 & CAN_IF1ARB2_ID_M) >> 2;
  1744. }
  1745. //
  1746. // Indicate that we lost some data.
  1747. //
  1748. if (ui16MsgCtrl & CAN_IF1MCTL_MSGLST)
  1749. {
  1750. psMsgObject->ui32Flags |= MSG_OBJ_DATA_LOST;
  1751. }
  1752. //
  1753. // Set the flag to indicate if ID masking was used.
  1754. //
  1755. if (ui16MsgCtrl & CAN_IF1MCTL_UMASK)
  1756. {
  1757. if (ui16ArbReg1 & CAN_IF1ARB2_XTD)
  1758. {
  1759. //
  1760. // The Identifier Mask is assumed to also be a 29 bit value.
  1761. //
  1762. psMsgObject->ui32MsgIDMask =
  1763. ((ui16MaskReg1 & CAN_IF1MSK2_IDMSK_M) << 16) | ui16MaskReg0;
  1764. //
  1765. // If this is a fully specified Mask and a remote frame then don't
  1766. // set the MSG_OBJ_USE_ID_FILTER because the ID was not really
  1767. // filtered.
  1768. //
  1769. if ((psMsgObject->ui32MsgIDMask != 0x1fffffff) ||
  1770. ((psMsgObject->ui32Flags & MSG_OBJ_REMOTE_FRAME) == 0))
  1771. {
  1772. psMsgObject->ui32Flags |= MSG_OBJ_USE_ID_FILTER;
  1773. }
  1774. }
  1775. else
  1776. {
  1777. //
  1778. // The Identifier Mask is assumed to also be an 11 bit value.
  1779. //
  1780. psMsgObject->ui32MsgIDMask =
  1781. (ui16MaskReg1 & CAN_IF1MSK2_IDMSK_M) >> 2;
  1782. //
  1783. // If this is a fully specified Mask and a remote frame then don't
  1784. // set the MSG_OBJ_USE_ID_FILTER because the ID was not really
  1785. // filtered.
  1786. //
  1787. if ((psMsgObject->ui32MsgIDMask != 0x7ff) ||
  1788. ((psMsgObject->ui32Flags & MSG_OBJ_REMOTE_FRAME) == 0))
  1789. {
  1790. psMsgObject->ui32Flags |= MSG_OBJ_USE_ID_FILTER;
  1791. }
  1792. }
  1793. //
  1794. // Indicate if the extended bit was used in filtering.
  1795. //
  1796. if (ui16MaskReg1 & CAN_IF1MSK2_MXTD)
  1797. {
  1798. psMsgObject->ui32Flags |= MSG_OBJ_USE_EXT_FILTER;
  1799. }
  1800. //
  1801. // Indicate if direction filtering was enabled.
  1802. //
  1803. if (ui16MaskReg1 & CAN_IF1MSK2_MDIR)
  1804. {
  1805. psMsgObject->ui32Flags |= MSG_OBJ_USE_DIR_FILTER;
  1806. }
  1807. }
  1808. //
  1809. // Set the interrupt flags.
  1810. //
  1811. if (ui16MsgCtrl & CAN_IF1MCTL_TXIE)
  1812. {
  1813. psMsgObject->ui32Flags |= MSG_OBJ_TX_INT_ENABLE;
  1814. }
  1815. if (ui16MsgCtrl & CAN_IF1MCTL_RXIE)
  1816. {
  1817. psMsgObject->ui32Flags |= MSG_OBJ_RX_INT_ENABLE;
  1818. }
  1819. //
  1820. // See if there is new data available.
  1821. //
  1822. if (ui16MsgCtrl & CAN_IF1MCTL_NEWDAT)
  1823. {
  1824. //
  1825. // Get the amount of data needed to be read.
  1826. //
  1827. psMsgObject->ui32MsgLen = (ui16MsgCtrl & CAN_IF1MCTL_DLC_M);
  1828. //
  1829. // Don't read any data for a remote frame, there is nothing valid in
  1830. // that buffer anyway.
  1831. //
  1832. if ((psMsgObject->ui32Flags & MSG_OBJ_REMOTE_FRAME) == 0)
  1833. {
  1834. //
  1835. // Read out the data from the CAN registers.
  1836. //
  1837. _CANDataRegRead(psMsgObject->pui8MsgData,
  1838. (uint32_t *)(ui32Base + CAN_O_IF2DA1),
  1839. psMsgObject->ui32MsgLen);
  1840. }
  1841. //
  1842. // Now clear out the new data flag.
  1843. //
  1844. HWREG(ui32Base + CAN_O_IF2CMSK) = CAN_IF1CMSK_NEWDAT;
  1845. //
  1846. // Transfer the message object to the message object specified by
  1847. // ui32ObjID.
  1848. //
  1849. HWREG(ui32Base + CAN_O_IF2CRQ) = ui32ObjID & CAN_IF1CRQ_MNUM_M;
  1850. //
  1851. // Wait for busy bit to clear
  1852. //
  1853. while (HWREG(ui32Base + CAN_O_IF2CRQ) & CAN_IF1CRQ_BUSY)
  1854. {
  1855. }
  1856. //
  1857. // Indicate that there is new data in this message.
  1858. //
  1859. psMsgObject->ui32Flags |= MSG_OBJ_NEW_DATA;
  1860. }
  1861. else
  1862. {
  1863. //
  1864. // Along with the MSG_OBJ_NEW_DATA not being set the amount of data
  1865. // needs to be set to zero if none was available.
  1866. //
  1867. psMsgObject->ui32MsgLen = 0;
  1868. }
  1869. }
  1870. //*****************************************************************************
  1871. //
  1872. //! Clears a message object so that it is no longer used.
  1873. //!
  1874. //! \param ui32Base is the base address of the CAN controller.
  1875. //! \param ui32ObjID is the message object number to disable (1-32).
  1876. //!
  1877. //! This function frees the specified message object from use. Once a message
  1878. //! object has been ``cleared,'' it no longer automatically sends or receives
  1879. //! messages, nor does it generate interrupts.
  1880. //!
  1881. //! \return None.
  1882. //
  1883. //*****************************************************************************
  1884. void
  1885. CANMessageClear(uint32_t ui32Base, uint32_t ui32ObjID)
  1886. {
  1887. //
  1888. // Check the arguments.
  1889. //
  1890. ASSERT(_CANBaseValid(ui32Base));
  1891. ASSERT((ui32ObjID >= 1) && (ui32ObjID <= 32));
  1892. //
  1893. // Wait for busy bit to clear
  1894. //
  1895. while (HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1896. {
  1897. }
  1898. //
  1899. // Clear the message value bit in the arbitration register. This indicates
  1900. // the message is not valid.
  1901. //
  1902. HWREG(ui32Base + CAN_O_IF1CMSK) = CAN_IF1CMSK_WRNRD | CAN_IF1CMSK_ARB;
  1903. HWREG(ui32Base + CAN_O_IF1ARB1) = 0;
  1904. HWREG(ui32Base + CAN_O_IF1ARB2) = 0;
  1905. //
  1906. // Initiate programming the message object
  1907. //
  1908. HWREG(ui32Base + CAN_O_IF1CRQ) = ui32ObjID & CAN_IF1CRQ_MNUM_M;
  1909. }
  1910. //*****************************************************************************
  1911. //
  1912. // Close the Doxygen group.
  1913. //! @}
  1914. //
  1915. //*****************************************************************************