can.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. //*****************************************************************************
  2. //
  3. // can.c - Driver for the CAN module.
  4. //
  5. // Copyright (c) 2006-2014 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.1.0.12573 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 Ethernet 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. //! \return None.
  988. //
  989. //*****************************************************************************
  990. void
  991. CANIntClear(uint32_t ui32Base, uint32_t ui32IntClr)
  992. {
  993. //
  994. // Check the arguments.
  995. //
  996. ASSERT(_CANBaseValid(ui32Base));
  997. ASSERT((ui32IntClr == CAN_INT_INTID_STATUS) ||
  998. ((ui32IntClr >= 1) && (ui32IntClr <= 32)));
  999. if(ui32IntClr == CAN_INT_INTID_STATUS)
  1000. {
  1001. //
  1002. // Simply read and discard the status to clear the interrupt.
  1003. //
  1004. HWREG(ui32Base + CAN_O_STS);
  1005. }
  1006. else
  1007. {
  1008. //
  1009. // Wait to be sure that this interface is not busy.
  1010. //
  1011. while(HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1012. {
  1013. }
  1014. //
  1015. // Only change the interrupt pending state by setting only the
  1016. // CAN_IF1CMSK_CLRINTPND bit.
  1017. //
  1018. HWREG(ui32Base + CAN_O_IF1CMSK) = CAN_IF1CMSK_CLRINTPND;
  1019. //
  1020. // Send the clear pending interrupt command to the CAN controller.
  1021. //
  1022. HWREG(ui32Base + CAN_O_IF1CRQ) = ui32IntClr & CAN_IF1CRQ_MNUM_M;
  1023. //
  1024. // Wait to be sure that this interface is not busy.
  1025. //
  1026. while(HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1027. {
  1028. }
  1029. }
  1030. }
  1031. //*****************************************************************************
  1032. //
  1033. //! Sets the CAN controller automatic retransmission behavior.
  1034. //!
  1035. //! \param ui32Base is the base address of the CAN controller.
  1036. //! \param bAutoRetry enables automatic retransmission.
  1037. //!
  1038. //! This function enables or disables automatic retransmission of messages with
  1039. //! detected errors. If \e bAutoRetry is \b true, then automatic
  1040. //! retransmission is enabled, otherwise it is disabled.
  1041. //!
  1042. //! \return None.
  1043. //
  1044. //*****************************************************************************
  1045. void
  1046. CANRetrySet(uint32_t ui32Base, bool bAutoRetry)
  1047. {
  1048. uint32_t ui32CtlReg;
  1049. //
  1050. // Check the arguments.
  1051. //
  1052. ASSERT(_CANBaseValid(ui32Base));
  1053. ui32CtlReg = HWREG(ui32Base + CAN_O_CTL);
  1054. //
  1055. // Conditionally set the DAR bit to enable/disable auto-retry.
  1056. //
  1057. if(bAutoRetry)
  1058. {
  1059. //
  1060. // Clearing the DAR bit tells the controller to not disable the
  1061. // auto-retry of messages which were not transmitted or received
  1062. // correctly.
  1063. //
  1064. ui32CtlReg &= ~CAN_CTL_DAR;
  1065. }
  1066. else
  1067. {
  1068. //
  1069. // Setting the DAR bit tells the controller to disable the auto-retry
  1070. // of messages which were not transmitted or received correctly.
  1071. //
  1072. ui32CtlReg |= CAN_CTL_DAR;
  1073. }
  1074. HWREG(ui32Base + CAN_O_CTL) = ui32CtlReg;
  1075. }
  1076. //*****************************************************************************
  1077. //
  1078. //! Returns the current setting for automatic retransmission.
  1079. //!
  1080. //! \param ui32Base is the base address of the CAN controller.
  1081. //!
  1082. //! This function reads the current setting for automatic retransmission in the
  1083. //! CAN controller and returns it to the caller.
  1084. //!
  1085. //! \return Returns \b true if automatic retransmission is enabled, \b false
  1086. //! otherwise.
  1087. //
  1088. //*****************************************************************************
  1089. bool
  1090. CANRetryGet(uint32_t ui32Base)
  1091. {
  1092. //
  1093. // Check the arguments.
  1094. //
  1095. ASSERT(_CANBaseValid(ui32Base));
  1096. //
  1097. // Read the disable automatic retry setting from the CAN controller.
  1098. //
  1099. if(HWREG(ui32Base + CAN_O_CTL) & CAN_CTL_DAR)
  1100. {
  1101. //
  1102. // Automatic data retransmission is not enabled.
  1103. //
  1104. return(false);
  1105. }
  1106. //
  1107. // Automatic data retransmission is enabled.
  1108. //
  1109. return(true);
  1110. }
  1111. //*****************************************************************************
  1112. //
  1113. //! Reads one of the controller status registers.
  1114. //!
  1115. //! \param ui32Base is the base address of the CAN controller.
  1116. //! \param eStatusReg is the status register to read.
  1117. //!
  1118. //! This function reads a status register of the CAN controller and returns it
  1119. //! to the caller.
  1120. //! The different status registers are:
  1121. //!
  1122. //! - \b CAN_STS_CONTROL - the main controller status
  1123. //! - \b CAN_STS_TXREQUEST - bit mask of objects pending transmission
  1124. //! - \b CAN_STS_NEWDAT - bit mask of objects with new data
  1125. //! - \b CAN_STS_MSGVAL - bit mask of objects with valid configuration
  1126. //!
  1127. //! When reading the main controller status register, a pending status
  1128. //! interrupt is cleared. This parameter is used in the interrupt
  1129. //! handler for the CAN controller if the cause is a status interrupt. The
  1130. //! controller status register fields are as follows:
  1131. //!
  1132. //! - \b CAN_STATUS_BUS_OFF - controller is in bus-off condition
  1133. //! - \b CAN_STATUS_EWARN - an error counter has reached a limit of at least 96
  1134. //! - \b CAN_STATUS_EPASS - CAN controller is in the error passive state
  1135. //! - \b CAN_STATUS_RXOK - a message was received successfully (independent of
  1136. //! any message filtering).
  1137. //! - \b CAN_STATUS_TXOK - a message was successfully transmitted
  1138. //! - \b CAN_STATUS_LEC_MSK - mask of last error code bits (3 bits)
  1139. //! - \b CAN_STATUS_LEC_NONE - no error
  1140. //! - \b CAN_STATUS_LEC_STUFF - stuffing error detected
  1141. //! - \b CAN_STATUS_LEC_FORM - a format error occurred in the fixed format part
  1142. //! of a message
  1143. //! - \b CAN_STATUS_LEC_ACK - a transmitted message was not acknowledged
  1144. //! - \b CAN_STATUS_LEC_BIT1 - dominant level detected when trying to send in
  1145. //! recessive mode
  1146. //! - \b CAN_STATUS_LEC_BIT0 - recessive level detected when trying to send in
  1147. //! dominant mode
  1148. //! - \b CAN_STATUS_LEC_CRC - CRC error in received message
  1149. //!
  1150. //! The remaining status registers consist of 32-bit-wide bit maps to the
  1151. //! message objects. They can be used to quickly obtain information about the
  1152. //! status of all the message objects without needing to query each one. They
  1153. //! contain the following information:
  1154. //!
  1155. //! - \b CAN_STS_TXREQUEST - if a message object's TXRQST bit is set, a
  1156. //! transmission is pending on that object. The application can use this
  1157. //! information to determine which objects are still waiting to send a
  1158. //! message.
  1159. //! - \b CAN_STS_NEWDAT - if a message object's NEWDAT bit is set, a new
  1160. //! message has been received in that object, and has not yet been picked up
  1161. //! by the host application
  1162. //! - \b CAN_STS_MSGVAL - if a message object's MSGVAL bit is set, the object
  1163. //! has a valid configuration programmed. The host application can use this
  1164. //! information to determine which message objects are empty/unused.
  1165. //!
  1166. //! \return Returns the value of the status register.
  1167. //
  1168. //*****************************************************************************
  1169. uint32_t
  1170. CANStatusGet(uint32_t ui32Base, tCANStsReg eStatusReg)
  1171. {
  1172. uint32_t ui32Status;
  1173. //
  1174. // Check the arguments.
  1175. //
  1176. ASSERT(_CANBaseValid(ui32Base));
  1177. switch(eStatusReg)
  1178. {
  1179. //
  1180. // Just return the global CAN status register since that is what was
  1181. // requested.
  1182. //
  1183. case CAN_STS_CONTROL:
  1184. {
  1185. ui32Status = HWREG(ui32Base + CAN_O_STS);
  1186. HWREG(ui32Base + CAN_O_STS) = ~(CAN_STS_RXOK | CAN_STS_TXOK |
  1187. CAN_STS_LEC_M);
  1188. break;
  1189. }
  1190. //
  1191. // Combine the Transmit status bits into one 32bit value.
  1192. //
  1193. case CAN_STS_TXREQUEST:
  1194. {
  1195. ui32Status = HWREG(ui32Base + CAN_O_TXRQ1);
  1196. ui32Status |= HWREG(ui32Base + CAN_O_TXRQ2) << 16;
  1197. break;
  1198. }
  1199. //
  1200. // Combine the New Data status bits into one 32bit value.
  1201. //
  1202. case CAN_STS_NEWDAT:
  1203. {
  1204. ui32Status = HWREG(ui32Base + CAN_O_NWDA1);
  1205. ui32Status |= HWREG(ui32Base + CAN_O_NWDA2) << 16;
  1206. break;
  1207. }
  1208. //
  1209. // Combine the Message valid status bits into one 32bit value.
  1210. //
  1211. case CAN_STS_MSGVAL:
  1212. {
  1213. ui32Status = HWREG(ui32Base + CAN_O_MSG1VAL);
  1214. ui32Status |= HWREG(ui32Base + CAN_O_MSG2VAL) << 16;
  1215. break;
  1216. }
  1217. //
  1218. // Unknown CAN status requested so return 0.
  1219. //
  1220. default:
  1221. {
  1222. ui32Status = 0;
  1223. break;
  1224. }
  1225. }
  1226. return(ui32Status);
  1227. }
  1228. //*****************************************************************************
  1229. //
  1230. //! Reads the CAN controller error counter register.
  1231. //!
  1232. //! \param ui32Base is the base address of the CAN controller.
  1233. //! \param pui32RxCount is a pointer to storage for the receive error counter.
  1234. //! \param pui32TxCount is a pointer to storage for the transmit error counter.
  1235. //!
  1236. //! This function reads the error counter register and returns the transmit and
  1237. //! receive error counts to the caller along with a flag indicating if the
  1238. //! controller receive counter has reached the error passive limit. The values
  1239. //! of the receive and transmit error counters are returned through the
  1240. //! pointers provided as parameters.
  1241. //!
  1242. //! After this call, \e *pui32RxCount holds the current receive error count
  1243. //! and \e *pui32TxCount holds the current transmit error count.
  1244. //!
  1245. //! \return Returns \b true if the receive error count has reached the error
  1246. //! passive limit, and \b false if the error count is below the error passive
  1247. //! limit.
  1248. //
  1249. //*****************************************************************************
  1250. bool
  1251. CANErrCntrGet(uint32_t ui32Base, uint32_t *pui32RxCount,
  1252. uint32_t *pui32TxCount)
  1253. {
  1254. uint32_t ui32CANError;
  1255. //
  1256. // Check the arguments.
  1257. //
  1258. ASSERT(_CANBaseValid(ui32Base));
  1259. //
  1260. // Read the current count of transmit/receive errors.
  1261. //
  1262. ui32CANError = HWREG(ui32Base + CAN_O_ERR);
  1263. //
  1264. // Extract the error numbers from the register value.
  1265. //
  1266. *pui32RxCount = (ui32CANError & CAN_ERR_REC_M) >> CAN_ERR_REC_S;
  1267. *pui32TxCount = (ui32CANError & CAN_ERR_TEC_M) >> CAN_ERR_TEC_S;
  1268. if(ui32CANError & CAN_ERR_RP)
  1269. {
  1270. return(true);
  1271. }
  1272. return(false);
  1273. }
  1274. //*****************************************************************************
  1275. //
  1276. //! Configures a message object in the CAN controller.
  1277. //!
  1278. //! \param ui32Base is the base address of the CAN controller.
  1279. //! \param ui32ObjID is the object number to configure (1-32).
  1280. //! \param psMsgObject is a pointer to a structure containing message object
  1281. //! settings.
  1282. //! \param eMsgType indicates the type of message for this object.
  1283. //!
  1284. //! This function is used to configure any one of the 32 message objects in the
  1285. //! CAN controller. A message object can be configured to be any type of CAN
  1286. //! message object as well as to use automatic transmission and reception.
  1287. //! This call also allows the message object to be configured to generate
  1288. //! interrupts on completion of message receipt or transmission. The
  1289. //! message object can also be configured with a filter/mask so that actions
  1290. //! are only taken when a message that meets certain parameters is seen on the
  1291. //! CAN bus.
  1292. //!
  1293. //! The \e eMsgType parameter must be one of the following values:
  1294. //!
  1295. //! - \b MSG_OBJ_TYPE_TX - CAN transmit message object.
  1296. //! - \b MSG_OBJ_TYPE_TX_REMOTE - CAN transmit remote request message object.
  1297. //! - \b MSG_OBJ_TYPE_RX - CAN receive message object.
  1298. //! - \b MSG_OBJ_TYPE_RX_REMOTE - CAN receive remote request message object.
  1299. //! - \b MSG_OBJ_TYPE_RXTX_REMOTE - CAN remote frame receive remote, then
  1300. //! transmit message object.
  1301. //!
  1302. //! The message object pointed to by \e psMsgObject must be populated by the
  1303. //! caller, as follows:
  1304. //!
  1305. //! - \e ui32MsgID - contains the message ID, either 11 or 29 bits.
  1306. //! - \e ui32MsgIDMask - mask of bits from \e ui32MsgID that must match if
  1307. //! identifier filtering is enabled.
  1308. //! - \e ui32Flags
  1309. //! - Set \b MSG_OBJ_TX_INT_ENABLE flag to enable interrupt on transmission.
  1310. //! - Set \b MSG_OBJ_RX_INT_ENABLE flag to enable interrupt on receipt.
  1311. //! - Set \b MSG_OBJ_USE_ID_FILTER flag to enable filtering based on the
  1312. //! identifier mask specified by \e ui32MsgIDMask.
  1313. //! - \e ui32MsgLen - the number of bytes in the message data. This parameter
  1314. //! must be non-zero even for a remote frame; it must match the expected
  1315. //! bytes of data in the responding data frame.
  1316. //! - \e pui8MsgData - points to a buffer containing up to 8 bytes of data for
  1317. //! a data frame.
  1318. //!
  1319. //! \b Example: To send a data frame or remote frame (in response to a remote
  1320. //! request), take the following steps:
  1321. //!
  1322. //! -# Set \e eMsgType to \b MSG_OBJ_TYPE_TX.
  1323. //! -# Set \e psMsgObject->ui32MsgID to the message ID.
  1324. //! -# Set \e psMsgObject->ui32Flags. Make sure to set
  1325. //! \b MSG_OBJ_TX_INT_ENABLE to allow an interrupt to be generated when the
  1326. //! message is sent.
  1327. //! -# Set \e psMsgObject->ui32MsgLen to the number of bytes in the data frame.
  1328. //! -# Set \e psMsgObject->pui8MsgData to point to an array containing the
  1329. //! bytes to send in the message.
  1330. //! -# Call this function with \e ui32ObjID set to one of the 32 object
  1331. //! buffers.
  1332. //!
  1333. //! \b Example: To receive a specific data frame, take the following steps:
  1334. //!
  1335. //! -# Set \e eMsgObjType to \b MSG_OBJ_TYPE_RX.
  1336. //! -# Set \e psMsgObject->ui32MsgID to the full message ID, or a partial mask
  1337. //! to use partial ID matching.
  1338. //! -# Set \e psMsgObject->ui32MsgIDMask bits that are used for masking
  1339. //! during comparison.
  1340. //! -# Set \e psMsgObject->ui32Flags as follows:
  1341. //! - Set \b MSG_OBJ_RX_INT_ENABLE flag to be interrupted when the data
  1342. //! frame is received.
  1343. //! - Set \b MSG_OBJ_USE_ID_FILTER flag to enable identifier-based
  1344. //! filtering.
  1345. //! -# Set \e psMsgObject->ui32MsgLen to the number of bytes in the expected
  1346. //! data frame.
  1347. //! -# The buffer pointed to by \e psMsgObject->pui8MsgData is not used by this
  1348. //! call as no data is present at the time of the call.
  1349. //! -# Call this function with \e ui32ObjID set to one of the 32 object
  1350. //! buffers.
  1351. //!
  1352. //! If you specify a message object buffer that already contains a message
  1353. //! definition, it is overwritten.
  1354. //!
  1355. //! \return None.
  1356. //
  1357. //*****************************************************************************
  1358. void
  1359. CANMessageSet(uint32_t ui32Base, uint32_t ui32ObjID,
  1360. tCANMsgObject *psMsgObject, tMsgObjType eMsgType)
  1361. {
  1362. uint16_t ui16CmdMaskReg;
  1363. uint16_t ui16MaskReg0, ui16MaskReg1;
  1364. uint16_t ui16ArbReg0, ui16ArbReg1;
  1365. uint16_t ui16MsgCtrl;
  1366. bool bTransferData;
  1367. bool bUseExtendedID;
  1368. bTransferData = 0;
  1369. //
  1370. // Check the arguments.
  1371. //
  1372. ASSERT(_CANBaseValid(ui32Base));
  1373. ASSERT((ui32ObjID <= 32) && (ui32ObjID != 0));
  1374. ASSERT((eMsgType == MSG_OBJ_TYPE_TX) ||
  1375. (eMsgType == MSG_OBJ_TYPE_TX_REMOTE) ||
  1376. (eMsgType == MSG_OBJ_TYPE_RX) ||
  1377. (eMsgType == MSG_OBJ_TYPE_RX_REMOTE) ||
  1378. (eMsgType == MSG_OBJ_TYPE_TX_REMOTE) ||
  1379. (eMsgType == MSG_OBJ_TYPE_RXTX_REMOTE));
  1380. //
  1381. // Wait for busy bit to clear
  1382. //
  1383. while(HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1384. {
  1385. }
  1386. //
  1387. // See if we need to use an extended identifier or not.
  1388. //
  1389. if((psMsgObject->ui32MsgID > CAN_MAX_11BIT_MSG_ID) ||
  1390. (psMsgObject->ui32Flags & MSG_OBJ_EXTENDED_ID))
  1391. {
  1392. bUseExtendedID = 1;
  1393. }
  1394. else
  1395. {
  1396. bUseExtendedID = 0;
  1397. }
  1398. //
  1399. // This is always a write to the Message object as this call is setting a
  1400. // message object. This call always sets all size bits so it sets
  1401. // both data bits. The call uses the CONTROL register to set control
  1402. // bits so this bit needs to be set as well.
  1403. //
  1404. ui16CmdMaskReg = (CAN_IF1CMSK_WRNRD | CAN_IF1CMSK_DATAA |
  1405. CAN_IF1CMSK_DATAB | CAN_IF1CMSK_CONTROL);
  1406. //
  1407. // Initialize the values to a known state before filling them in based on
  1408. // the type of message object that is being configured.
  1409. //
  1410. ui16ArbReg0 = 0;
  1411. ui16ArbReg1 = 0;
  1412. ui16MsgCtrl = 0;
  1413. ui16MaskReg0 = 0;
  1414. ui16MaskReg1 = 0;
  1415. switch(eMsgType)
  1416. {
  1417. //
  1418. // Transmit message object.
  1419. //
  1420. case MSG_OBJ_TYPE_TX:
  1421. {
  1422. //
  1423. // Set the TXRQST bit and the reset the rest of the register.
  1424. //
  1425. ui16MsgCtrl |= CAN_IF1MCTL_TXRQST;
  1426. ui16ArbReg1 = CAN_IF1ARB2_DIR;
  1427. bTransferData = 1;
  1428. break;
  1429. }
  1430. //
  1431. // Transmit remote request message object
  1432. //
  1433. case MSG_OBJ_TYPE_TX_REMOTE:
  1434. {
  1435. //
  1436. // Set the TXRQST bit and the reset the rest of the register.
  1437. //
  1438. ui16MsgCtrl |= CAN_IF1MCTL_TXRQST;
  1439. ui16ArbReg1 = 0;
  1440. break;
  1441. }
  1442. //
  1443. // Receive message object.
  1444. //
  1445. case MSG_OBJ_TYPE_RX:
  1446. {
  1447. //
  1448. // This clears the DIR bit along with everything else. The TXRQST
  1449. // bit was cleared by defaulting ui16MsgCtrl to 0.
  1450. //
  1451. ui16ArbReg1 = 0;
  1452. break;
  1453. }
  1454. //
  1455. // Receive remote request message object.
  1456. //
  1457. case MSG_OBJ_TYPE_RX_REMOTE:
  1458. {
  1459. //
  1460. // The DIR bit is set to one for remote receivers. The TXRQST bit
  1461. // was cleared by defaulting ui16MsgCtrl to 0.
  1462. //
  1463. ui16ArbReg1 = CAN_IF1ARB2_DIR;
  1464. //
  1465. // Set this object so that it only indicates that a remote frame
  1466. // was received and allow for software to handle it by sending back
  1467. // a data frame.
  1468. //
  1469. ui16MsgCtrl = CAN_IF1MCTL_UMASK;
  1470. //
  1471. // Use the full Identifier by default.
  1472. //
  1473. ui16MaskReg0 = 0xffff;
  1474. ui16MaskReg1 = 0x1fff;
  1475. //
  1476. // Make sure to send the mask to the message object.
  1477. //
  1478. ui16CmdMaskReg |= CAN_IF1CMSK_MASK;
  1479. break;
  1480. }
  1481. //
  1482. // Remote frame receive remote, with auto-transmit message object.
  1483. //
  1484. case MSG_OBJ_TYPE_RXTX_REMOTE:
  1485. {
  1486. //
  1487. // Oddly the DIR bit is set to one for remote receivers.
  1488. //
  1489. ui16ArbReg1 = CAN_IF1ARB2_DIR;
  1490. //
  1491. // Set this object to auto answer if a matching identifier is seen.
  1492. //
  1493. ui16MsgCtrl = CAN_IF1MCTL_RMTEN | CAN_IF1MCTL_UMASK;
  1494. //
  1495. // The data to be returned needs to be filled in.
  1496. //
  1497. bTransferData = 1;
  1498. break;
  1499. }
  1500. //
  1501. // This case never happens due to the ASSERT statement at the
  1502. // beginning of this function.
  1503. //
  1504. default:
  1505. {
  1506. return;
  1507. }
  1508. }
  1509. //
  1510. // Configure the Mask Registers.
  1511. //
  1512. if(psMsgObject->ui32Flags & MSG_OBJ_USE_ID_FILTER)
  1513. {
  1514. if(bUseExtendedID)
  1515. {
  1516. //
  1517. // Set the 29 bits of Identifier mask that were requested.
  1518. //
  1519. ui16MaskReg0 = psMsgObject->ui32MsgIDMask & CAN_IF1MSK1_IDMSK_M;
  1520. ui16MaskReg1 = ((psMsgObject->ui32MsgIDMask >> 16) &
  1521. CAN_IF1MSK2_IDMSK_M);
  1522. }
  1523. else
  1524. {
  1525. //
  1526. // Lower 16 bit are unused so set them to zero.
  1527. //
  1528. ui16MaskReg0 = 0;
  1529. //
  1530. // Put the 11 bit Mask Identifier into the upper bits of the field
  1531. // in the register.
  1532. //
  1533. ui16MaskReg1 = ((psMsgObject->ui32MsgIDMask << 2) &
  1534. CAN_IF1MSK2_IDMSK_M);
  1535. }
  1536. }
  1537. //
  1538. // If the caller wants to filter on the extended ID bit then set it.
  1539. //
  1540. if((psMsgObject->ui32Flags & MSG_OBJ_USE_EXT_FILTER) ==
  1541. MSG_OBJ_USE_EXT_FILTER)
  1542. {
  1543. ui16MaskReg1 |= CAN_IF1MSK2_MXTD;
  1544. }
  1545. //
  1546. // The caller wants to filter on the message direction field.
  1547. //
  1548. if((psMsgObject->ui32Flags & MSG_OBJ_USE_DIR_FILTER) ==
  1549. MSG_OBJ_USE_DIR_FILTER)
  1550. {
  1551. ui16MaskReg1 |= CAN_IF1MSK2_MDIR;
  1552. }
  1553. if(psMsgObject->ui32Flags &
  1554. (MSG_OBJ_USE_ID_FILTER | MSG_OBJ_USE_DIR_FILTER |
  1555. MSG_OBJ_USE_EXT_FILTER))
  1556. {
  1557. //
  1558. // Set the UMASK bit to enable using the mask register.
  1559. //
  1560. ui16MsgCtrl |= CAN_IF1MCTL_UMASK;
  1561. //
  1562. // Set the MASK bit so that this gets transferred to the Message
  1563. // Object.
  1564. //
  1565. ui16CmdMaskReg |= CAN_IF1CMSK_MASK;
  1566. }
  1567. //
  1568. // Set the Arb bit so that this gets transferred to the Message object.
  1569. //
  1570. ui16CmdMaskReg |= CAN_IF1CMSK_ARB;
  1571. //
  1572. // Configure the Arbitration registers.
  1573. //
  1574. if(bUseExtendedID)
  1575. {
  1576. //
  1577. // Set the 29 bit version of the Identifier for this message object.
  1578. //
  1579. ui16ArbReg0 |= psMsgObject->ui32MsgID & CAN_IF1ARB1_ID_M;
  1580. ui16ArbReg1 |= (psMsgObject->ui32MsgID >> 16) & CAN_IF1ARB2_ID_M;
  1581. //
  1582. // Mark the message as valid and set the extended ID bit.
  1583. //
  1584. ui16ArbReg1 |= CAN_IF1ARB2_MSGVAL | CAN_IF1ARB2_XTD;
  1585. }
  1586. else
  1587. {
  1588. //
  1589. // Set the 11 bit version of the Identifier for this message object.
  1590. // The lower 18 bits are set to zero.
  1591. //
  1592. ui16ArbReg1 |= (psMsgObject->ui32MsgID << 2) & CAN_IF1ARB2_ID_M;
  1593. //
  1594. // Mark the message as valid.
  1595. //
  1596. ui16ArbReg1 |= CAN_IF1ARB2_MSGVAL;
  1597. }
  1598. //
  1599. // Set the data length since this is set for all transfers. This is also a
  1600. // single transfer and not a FIFO transfer so set EOB bit.
  1601. //
  1602. ui16MsgCtrl |= (psMsgObject->ui32MsgLen & CAN_IF1MCTL_DLC_M);
  1603. //
  1604. // Mark this as the last entry if this is not the last entry in a FIFO.
  1605. //
  1606. if((psMsgObject->ui32Flags & MSG_OBJ_FIFO) == 0)
  1607. {
  1608. ui16MsgCtrl |= CAN_IF1MCTL_EOB;
  1609. }
  1610. //
  1611. // Enable transmit interrupts if they should be enabled.
  1612. //
  1613. if(psMsgObject->ui32Flags & MSG_OBJ_TX_INT_ENABLE)
  1614. {
  1615. ui16MsgCtrl |= CAN_IF1MCTL_TXIE;
  1616. }
  1617. //
  1618. // Enable receive interrupts if they should be enabled.
  1619. //
  1620. if(psMsgObject->ui32Flags & MSG_OBJ_RX_INT_ENABLE)
  1621. {
  1622. ui16MsgCtrl |= CAN_IF1MCTL_RXIE;
  1623. }
  1624. //
  1625. // Write the data out to the CAN Data registers if needed.
  1626. //
  1627. if(bTransferData)
  1628. {
  1629. _CANDataRegWrite(psMsgObject->pui8MsgData,
  1630. (uint32_t *)(ui32Base + CAN_O_IF1DA1),
  1631. psMsgObject->ui32MsgLen);
  1632. }
  1633. //
  1634. // Write out the registers to program the message object.
  1635. //
  1636. HWREG(ui32Base + CAN_O_IF1CMSK) = ui16CmdMaskReg;
  1637. HWREG(ui32Base + CAN_O_IF1MSK1) = ui16MaskReg0;
  1638. HWREG(ui32Base + CAN_O_IF1MSK2) = ui16MaskReg1;
  1639. HWREG(ui32Base + CAN_O_IF1ARB1) = ui16ArbReg0;
  1640. HWREG(ui32Base + CAN_O_IF1ARB2) = ui16ArbReg1;
  1641. HWREG(ui32Base + CAN_O_IF1MCTL) = ui16MsgCtrl;
  1642. //
  1643. // Transfer the message object to the message object specified by
  1644. // ui32ObjID.
  1645. //
  1646. HWREG(ui32Base + CAN_O_IF1CRQ) = ui32ObjID & CAN_IF1CRQ_MNUM_M;
  1647. }
  1648. //*****************************************************************************
  1649. //
  1650. //! Reads a CAN message from one of the message object buffers.
  1651. //!
  1652. //! \param ui32Base is the base address of the CAN controller.
  1653. //! \param ui32ObjID is the object number to read (1-32).
  1654. //! \param psMsgObject points to a structure containing message object fields.
  1655. //! \param bClrPendingInt indicates whether an associated interrupt should be
  1656. //! cleared.
  1657. //!
  1658. //! This function is used to read the contents of one of the 32 message objects
  1659. //! in the CAN controller and return it to the caller. The data returned is
  1660. //! stored in the fields of the caller-supplied structure pointed to by
  1661. //! \e psMsgObject. The data consists of all of the parts of a CAN message,
  1662. //! plus some control and status information.
  1663. //!
  1664. //! Normally, this function is used to read a message object that has received
  1665. //! and stored a CAN message with a certain identifier. However, this function
  1666. //! could also be used to read the contents of a message object in order to
  1667. //! load the fields of the structure in case only part of the structure must
  1668. //! be changed from a previous setting.
  1669. //!
  1670. //! When using CANMessageGet(), all of the same fields of the structure are
  1671. //! populated in the same way as when the CANMessageSet() function is used,
  1672. //! with the following exceptions:
  1673. //!
  1674. //! \e psMsgObject->ui32Flags:
  1675. //!
  1676. //! - \b MSG_OBJ_NEW_DATA indicates if this data is new since the last time it
  1677. //! was read
  1678. //! - \b MSG_OBJ_DATA_LOST indicates that at least one message was received on
  1679. //! this message object and not read by the host before being overwritten.
  1680. //!
  1681. //! \return None.
  1682. //
  1683. //*****************************************************************************
  1684. void
  1685. CANMessageGet(uint32_t ui32Base, uint32_t ui32ObjID,
  1686. tCANMsgObject *psMsgObject, bool bClrPendingInt)
  1687. {
  1688. uint16_t ui16CmdMaskReg;
  1689. uint16_t ui16MaskReg0, ui16MaskReg1;
  1690. uint16_t ui16ArbReg0, ui16ArbReg1;
  1691. uint16_t ui16MsgCtrl;
  1692. //
  1693. // Check the arguments.
  1694. //
  1695. ASSERT(_CANBaseValid(ui32Base));
  1696. ASSERT((ui32ObjID <= 32) && (ui32ObjID != 0));
  1697. //
  1698. // This is always a read to the Message object as this call is setting a
  1699. // message object.
  1700. //
  1701. ui16CmdMaskReg = (CAN_IF1CMSK_DATAA | CAN_IF1CMSK_DATAB |
  1702. CAN_IF1CMSK_CONTROL | CAN_IF1CMSK_MASK |
  1703. CAN_IF1CMSK_ARB);
  1704. //
  1705. // Clear a pending interrupt and new data in a message object.
  1706. //
  1707. if(bClrPendingInt)
  1708. {
  1709. ui16CmdMaskReg |= CAN_IF1CMSK_CLRINTPND;
  1710. }
  1711. //
  1712. // Set up the request for data from the message object.
  1713. //
  1714. HWREG(ui32Base + CAN_O_IF2CMSK) = ui16CmdMaskReg;
  1715. //
  1716. // Transfer the message object to the message object specified by
  1717. // ui32ObjID.
  1718. //
  1719. HWREG(ui32Base + CAN_O_IF2CRQ) = ui32ObjID & CAN_IF1CRQ_MNUM_M;
  1720. //
  1721. // Wait for busy bit to clear
  1722. //
  1723. while(HWREG(ui32Base + CAN_O_IF2CRQ) & CAN_IF1CRQ_BUSY)
  1724. {
  1725. }
  1726. //
  1727. // Read out the IF Registers.
  1728. //
  1729. ui16MaskReg0 = HWREG(ui32Base + CAN_O_IF2MSK1);
  1730. ui16MaskReg1 = HWREG(ui32Base + CAN_O_IF2MSK2);
  1731. ui16ArbReg0 = HWREG(ui32Base + CAN_O_IF2ARB1);
  1732. ui16ArbReg1 = HWREG(ui32Base + CAN_O_IF2ARB2);
  1733. ui16MsgCtrl = HWREG(ui32Base + CAN_O_IF2MCTL);
  1734. psMsgObject->ui32Flags = MSG_OBJ_NO_FLAGS;
  1735. //
  1736. // Determine if this is a remote frame by checking the TXRQST and DIR bits.
  1737. //
  1738. if((!(ui16MsgCtrl & CAN_IF1MCTL_TXRQST) &&
  1739. (ui16ArbReg1 & CAN_IF1ARB2_DIR)) ||
  1740. ((ui16MsgCtrl & CAN_IF1MCTL_TXRQST) &&
  1741. (!(ui16ArbReg1 & CAN_IF1ARB2_DIR))))
  1742. {
  1743. psMsgObject->ui32Flags |= MSG_OBJ_REMOTE_FRAME;
  1744. }
  1745. //
  1746. // Get the identifier out of the register, the format depends on size of
  1747. // the mask.
  1748. //
  1749. if(ui16ArbReg1 & CAN_IF1ARB2_XTD)
  1750. {
  1751. //
  1752. // Set the 29 bit version of the Identifier for this message object.
  1753. //
  1754. psMsgObject->ui32MsgID = (((ui16ArbReg1 & CAN_IF1ARB2_ID_M) << 16) |
  1755. ui16ArbReg0);
  1756. psMsgObject->ui32Flags |= MSG_OBJ_EXTENDED_ID;
  1757. }
  1758. else
  1759. {
  1760. //
  1761. // The Identifier is an 11 bit value.
  1762. //
  1763. psMsgObject->ui32MsgID = (ui16ArbReg1 & CAN_IF1ARB2_ID_M) >> 2;
  1764. }
  1765. //
  1766. // Indicate that we lost some data.
  1767. //
  1768. if(ui16MsgCtrl & CAN_IF1MCTL_MSGLST)
  1769. {
  1770. psMsgObject->ui32Flags |= MSG_OBJ_DATA_LOST;
  1771. }
  1772. //
  1773. // Set the flag to indicate if ID masking was used.
  1774. //
  1775. if(ui16MsgCtrl & CAN_IF1MCTL_UMASK)
  1776. {
  1777. if(ui16ArbReg1 & CAN_IF1ARB2_XTD)
  1778. {
  1779. //
  1780. // The Identifier Mask is assumed to also be a 29 bit value.
  1781. //
  1782. psMsgObject->ui32MsgIDMask =
  1783. ((ui16MaskReg1 & CAN_IF1MSK2_IDMSK_M) << 16) | ui16MaskReg0;
  1784. //
  1785. // If this is a fully specified Mask and a remote frame then don't
  1786. // set the MSG_OBJ_USE_ID_FILTER because the ID was not really
  1787. // filtered.
  1788. //
  1789. if((psMsgObject->ui32MsgIDMask != 0x1fffffff) ||
  1790. ((psMsgObject->ui32Flags & MSG_OBJ_REMOTE_FRAME) == 0))
  1791. {
  1792. psMsgObject->ui32Flags |= MSG_OBJ_USE_ID_FILTER;
  1793. }
  1794. }
  1795. else
  1796. {
  1797. //
  1798. // The Identifier Mask is assumed to also be an 11 bit value.
  1799. //
  1800. psMsgObject->ui32MsgIDMask =
  1801. (ui16MaskReg1 & CAN_IF1MSK2_IDMSK_M) >> 2;
  1802. //
  1803. // If this is a fully specified Mask and a remote frame then don't
  1804. // set the MSG_OBJ_USE_ID_FILTER because the ID was not really
  1805. // filtered.
  1806. //
  1807. if((psMsgObject->ui32MsgIDMask != 0x7ff) ||
  1808. ((psMsgObject->ui32Flags & MSG_OBJ_REMOTE_FRAME) == 0))
  1809. {
  1810. psMsgObject->ui32Flags |= MSG_OBJ_USE_ID_FILTER;
  1811. }
  1812. }
  1813. //
  1814. // Indicate if the extended bit was used in filtering.
  1815. //
  1816. if(ui16MaskReg1 & CAN_IF1MSK2_MXTD)
  1817. {
  1818. psMsgObject->ui32Flags |= MSG_OBJ_USE_EXT_FILTER;
  1819. }
  1820. //
  1821. // Indicate if direction filtering was enabled.
  1822. //
  1823. if(ui16MaskReg1 & CAN_IF1MSK2_MDIR)
  1824. {
  1825. psMsgObject->ui32Flags |= MSG_OBJ_USE_DIR_FILTER;
  1826. }
  1827. }
  1828. //
  1829. // Set the interrupt flags.
  1830. //
  1831. if(ui16MsgCtrl & CAN_IF1MCTL_TXIE)
  1832. {
  1833. psMsgObject->ui32Flags |= MSG_OBJ_TX_INT_ENABLE;
  1834. }
  1835. if(ui16MsgCtrl & CAN_IF1MCTL_RXIE)
  1836. {
  1837. psMsgObject->ui32Flags |= MSG_OBJ_RX_INT_ENABLE;
  1838. }
  1839. //
  1840. // See if there is new data available.
  1841. //
  1842. if(ui16MsgCtrl & CAN_IF1MCTL_NEWDAT)
  1843. {
  1844. //
  1845. // Get the amount of data needed to be read.
  1846. //
  1847. psMsgObject->ui32MsgLen = (ui16MsgCtrl & CAN_IF1MCTL_DLC_M);
  1848. //
  1849. // Don't read any data for a remote frame, there is nothing valid in
  1850. // that buffer anyway.
  1851. //
  1852. if((psMsgObject->ui32Flags & MSG_OBJ_REMOTE_FRAME) == 0)
  1853. {
  1854. //
  1855. // Read out the data from the CAN registers.
  1856. //
  1857. _CANDataRegRead(psMsgObject->pui8MsgData,
  1858. (uint32_t *)(ui32Base + CAN_O_IF2DA1),
  1859. psMsgObject->ui32MsgLen);
  1860. }
  1861. //
  1862. // Now clear out the new data flag.
  1863. //
  1864. HWREG(ui32Base + CAN_O_IF2CMSK) = CAN_IF1CMSK_NEWDAT;
  1865. //
  1866. // Transfer the message object to the message object specified by
  1867. // ui32ObjID.
  1868. //
  1869. HWREG(ui32Base + CAN_O_IF2CRQ) = ui32ObjID & CAN_IF1CRQ_MNUM_M;
  1870. //
  1871. // Wait for busy bit to clear
  1872. //
  1873. while(HWREG(ui32Base + CAN_O_IF2CRQ) & CAN_IF1CRQ_BUSY)
  1874. {
  1875. }
  1876. //
  1877. // Indicate that there is new data in this message.
  1878. //
  1879. psMsgObject->ui32Flags |= MSG_OBJ_NEW_DATA;
  1880. }
  1881. else
  1882. {
  1883. //
  1884. // Along with the MSG_OBJ_NEW_DATA not being set the amount of data
  1885. // needs to be set to zero if none was available.
  1886. //
  1887. psMsgObject->ui32MsgLen = 0;
  1888. }
  1889. }
  1890. //*****************************************************************************
  1891. //
  1892. //! Clears a message object so that it is no longer used.
  1893. //!
  1894. //! \param ui32Base is the base address of the CAN controller.
  1895. //! \param ui32ObjID is the message object number to disable (1-32).
  1896. //!
  1897. //! This function frees the specified message object from use. Once a message
  1898. //! object has been ``cleared,'' it no longer automatically sends or receives
  1899. //! messages, nor does it generate interrupts.
  1900. //!
  1901. //! \return None.
  1902. //
  1903. //*****************************************************************************
  1904. void
  1905. CANMessageClear(uint32_t ui32Base, uint32_t ui32ObjID)
  1906. {
  1907. //
  1908. // Check the arguments.
  1909. //
  1910. ASSERT(_CANBaseValid(ui32Base));
  1911. ASSERT((ui32ObjID >= 1) && (ui32ObjID <= 32));
  1912. //
  1913. // Wait for busy bit to clear
  1914. //
  1915. while(HWREG(ui32Base + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1916. {
  1917. }
  1918. //
  1919. // Clear the message value bit in the arbitration register. This indicates
  1920. // the message is not valid.
  1921. //
  1922. HWREG(ui32Base + CAN_O_IF1CMSK) = CAN_IF1CMSK_WRNRD | CAN_IF1CMSK_ARB;
  1923. HWREG(ui32Base + CAN_O_IF1ARB1) = 0;
  1924. HWREG(ui32Base + CAN_O_IF1ARB2) = 0;
  1925. //
  1926. // Initiate programming the message object
  1927. //
  1928. HWREG(ui32Base + CAN_O_IF1CRQ) = ui32ObjID & CAN_IF1CRQ_MNUM_M;
  1929. }
  1930. //*****************************************************************************
  1931. //
  1932. // Close the Doxygen group.
  1933. //! @}
  1934. //
  1935. //*****************************************************************************