can.c 69 KB

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