can.c 71 KB

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