can.c 69 KB

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