can.c 73 KB

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