can.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  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 8264 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, this function returns the
  143. //! corresponding interrupt 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 automatically transmits any pending frames, and processes 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 no longer automatically processes 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. //! Sets the CAN bit timing values to a nominal setting based on a desired
  612. //! 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 sets the CAN bit timing for the bit rate passed in the
  619. //! \e ulBitRate parameter based on the \e ulSourceClock parameter. Because 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 works 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. //! Because not all bit rates can be matched exactly, the bit rate is set to
  628. //! the 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, is 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 is 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. //! Thus with \e ulSyncPropPhase1Seg = 4, \e ulPhase2Seg = 1,
  774. //! \e ulQuantumPrescaler = 2 and an 8 MHz CAN clock, the bit rate is
  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 in 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. //! This function enables specific interrupt sources of the CAN controller.
  936. //! Only enabled sources 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 generates 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. //! This function returns the value of one of two interrupt status registers.
  1010. //! The interrupt status register read is determined by the \e eIntStsReg
  1011. //! parameter, which 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. The value returned is
  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 also clears 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 value can be used to discover all of the
  1032. //! pending interrupts at once, as opposed to repeatedly reading the interrupt
  1033. //! register 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. //! function should only be used if the application wants to clear an interrupt
  1103. //! source 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-M 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. //! This function enables or disables automatic retransmission of messages with
  1170. //! detected errors. If \e bAutoRetry is \b true, then automatic retransmission
  1171. //! is 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. //! This function reads the current setting for 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. //! This function reads a status register of the CAN controller and returns it
  1250. //! to the caller.
  1251. //! The different status registers are:
  1252. //!
  1253. //! - \b CAN_STS_CONTROL - the main controller status
  1254. //! - \b CAN_STS_TXREQUEST - bit mask of objects pending transmission
  1255. //! - \b CAN_STS_NEWDAT - bit mask of objects with new data
  1256. //! - \b CAN_STS_MSGVAL - bit mask of objects with valid configuration
  1257. //!
  1258. //! When reading the main controller status register, a pending status
  1259. //! interrupt is cleared. This parameter should be used in the interrupt
  1260. //! handler for the CAN controller if the cause is a status interrupt. The
  1261. //! controller status register fields are as follows:
  1262. //!
  1263. //! - \b CAN_STATUS_BUS_OFF - controller is in bus-off condition
  1264. //! - \b CAN_STATUS_EWARN - an error counter has reached a limit of at least 96
  1265. //! - \b CAN_STATUS_EPASS - CAN controller is in the error passive state
  1266. //! - \b CAN_STATUS_RXOK - a message was received successfully (independent of
  1267. //! any message filtering).
  1268. //! - \b CAN_STATUS_TXOK - a message was successfully transmitted
  1269. //! - \b CAN_STATUS_LEC_MSK - mask of last error code bits (3 bits)
  1270. //! - \b CAN_STATUS_LEC_NONE - no error
  1271. //! - \b CAN_STATUS_LEC_STUFF - stuffing error detected
  1272. //! - \b CAN_STATUS_LEC_FORM - a format error occurred in the fixed format part
  1273. //! of a message
  1274. //! - \b CAN_STATUS_LEC_ACK - a transmitted message was not acknowledged
  1275. //! - \b CAN_STATUS_LEC_BIT1 - dominant level detected when trying to send in
  1276. //! recessive mode
  1277. //! - \b CAN_STATUS_LEC_BIT0 - recessive level detected when trying to send in
  1278. //! dominant mode
  1279. //! - \b CAN_STATUS_LEC_CRC - CRC error in received message
  1280. //!
  1281. //! The remaining status registers consist of 32-bit-wide bit maps to the
  1282. //! message objects. They can be used to quickly obtain information about the
  1283. //! status of all the message objects without needing to query each one. They
  1284. //! contain the following information:
  1285. //!
  1286. //! - \b CAN_STS_TXREQUEST - if a message object's TXRQST bit is set, a
  1287. //! transmission is pending on that object. The application can use this
  1288. //! information to determine which objects are still waiting to send a
  1289. //! message.
  1290. //! - \b CAN_STS_NEWDAT - if a message object's NEWDAT bit is set, a new
  1291. //! message has been received in that object, and has not yet been picked up
  1292. //! by the host application
  1293. //! - \b CAN_STS_MSGVAL - if a message object's MSGVAL bit is set, the object
  1294. //! has a valid configuration programmed. The host application can use this
  1295. //! information to determine which message objects are empty/unused.
  1296. //!
  1297. //! \return Returns the value of the status register.
  1298. //
  1299. //*****************************************************************************
  1300. unsigned long
  1301. CANStatusGet(unsigned long ulBase, tCANStsReg eStatusReg)
  1302. {
  1303. unsigned long ulStatus;
  1304. //
  1305. // Check the arguments.
  1306. //
  1307. ASSERT(CANBaseValid(ulBase));
  1308. switch(eStatusReg)
  1309. {
  1310. //
  1311. // Just return the global CAN status register since that is what was
  1312. // requested.
  1313. //
  1314. case CAN_STS_CONTROL:
  1315. {
  1316. ulStatus = CANRegRead(ulBase + CAN_O_STS);
  1317. CANRegWrite(ulBase + CAN_O_STS,
  1318. ~(CAN_STS_RXOK | CAN_STS_TXOK | CAN_STS_LEC_M));
  1319. break;
  1320. }
  1321. //
  1322. // Combine the Transmit status bits into one 32bit value.
  1323. //
  1324. case CAN_STS_TXREQUEST:
  1325. {
  1326. ulStatus = CANRegRead(ulBase + CAN_O_TXRQ1);
  1327. ulStatus |= CANRegRead(ulBase + CAN_O_TXRQ2) << 16;
  1328. break;
  1329. }
  1330. //
  1331. // Combine the New Data status bits into one 32bit value.
  1332. //
  1333. case CAN_STS_NEWDAT:
  1334. {
  1335. ulStatus = CANRegRead(ulBase + CAN_O_NWDA1);
  1336. ulStatus |= CANRegRead(ulBase + CAN_O_NWDA2) << 16;
  1337. break;
  1338. }
  1339. //
  1340. // Combine the Message valid status bits into one 32bit value.
  1341. //
  1342. case CAN_STS_MSGVAL:
  1343. {
  1344. ulStatus = CANRegRead(ulBase + CAN_O_MSG1VAL);
  1345. ulStatus |= CANRegRead(ulBase + CAN_O_MSG2VAL) << 16;
  1346. break;
  1347. }
  1348. //
  1349. // Unknown CAN status requested so return 0.
  1350. //
  1351. default:
  1352. {
  1353. ulStatus = 0;
  1354. break;
  1355. }
  1356. }
  1357. return(ulStatus);
  1358. }
  1359. //*****************************************************************************
  1360. //
  1361. //! Reads the CAN controller error counter register.
  1362. //!
  1363. //! \param ulBase is the base address of the CAN controller.
  1364. //! \param pulRxCount is a pointer to storage for the receive error counter.
  1365. //! \param pulTxCount is a pointer to storage for the transmit error counter.
  1366. //!
  1367. //! This function reads the error counter register and returns the transmit and
  1368. //! receive error counts to the caller along with a flag indicating if the
  1369. //! controller receive counter has reached the error passive limit. The values
  1370. //! of the receive and transmit error counters are returned through the pointers
  1371. //! provided as parameters.
  1372. //!
  1373. //! After this call, \e *pulRxCount holds the current receive error count
  1374. //! and \e *pulTxCount holds the current transmit error count.
  1375. //!
  1376. //! \return Returns \b true if the receive error count has reached the error
  1377. //! passive limit, and \b false if the error count is below the error passive
  1378. //! limit.
  1379. //
  1380. //*****************************************************************************
  1381. tBoolean
  1382. CANErrCntrGet(unsigned long ulBase, unsigned long *pulRxCount,
  1383. unsigned long *pulTxCount)
  1384. {
  1385. unsigned long ulCANError;
  1386. //
  1387. // Check the arguments.
  1388. //
  1389. ASSERT(CANBaseValid(ulBase));
  1390. //
  1391. // Read the current count of transmit/receive errors.
  1392. //
  1393. ulCANError = CANRegRead(ulBase + CAN_O_ERR);
  1394. //
  1395. // Extract the error numbers from the register value.
  1396. //
  1397. *pulRxCount = (ulCANError & CAN_ERR_REC_M) >> CAN_ERR_REC_S;
  1398. *pulTxCount = (ulCANError & CAN_ERR_TEC_M) >> CAN_ERR_TEC_S;
  1399. if(ulCANError & CAN_ERR_RP)
  1400. {
  1401. return(true);
  1402. }
  1403. return(false);
  1404. }
  1405. //*****************************************************************************
  1406. //
  1407. //! Configures a message object in the CAN controller.
  1408. //!
  1409. //! \param ulBase is the base address of the CAN controller.
  1410. //! \param ulObjID is the object number to configure (1-32).
  1411. //! \param pMsgObject is a pointer to a structure containing message object
  1412. //! settings.
  1413. //! \param eMsgType indicates the type of message for this object.
  1414. //!
  1415. //! This function is used to configure any one of the 32 message objects in the
  1416. //! CAN controller. A message object can be configured to be any type of CAN
  1417. //! message object as well as to use automatic transmission and reception.
  1418. //! This call also allows the message object to be configured to generate
  1419. //! interrupts on completion of message receipt or transmission. The
  1420. //! message object can also be configured with a filter/mask so that actions
  1421. //! are only taken when a message that meets certain parameters is seen on the
  1422. //! CAN bus.
  1423. //!
  1424. //! The \e eMsgType parameter must be one of the following values:
  1425. //!
  1426. //! - \b MSG_OBJ_TYPE_TX - CAN transmit message object.
  1427. //! - \b MSG_OBJ_TYPE_TX_REMOTE - CAN transmit remote request message object.
  1428. //! - \b MSG_OBJ_TYPE_RX - CAN receive message object.
  1429. //! - \b MSG_OBJ_TYPE_RX_REMOTE - CAN receive remote request message object.
  1430. //! - \b MSG_OBJ_TYPE_RXTX_REMOTE - CAN remote frame receive remote, then
  1431. //! transmit message object.
  1432. //!
  1433. //! The message object pointed to by \e pMsgObject must be populated by the
  1434. //! caller, as follows:
  1435. //!
  1436. //! - \e ulMsgID - contains the message ID, either 11 or 29 bits.
  1437. //! - \e ulMsgIDMask - mask of bits from \e ulMsgID that must match if
  1438. //! identifier filtering is enabled.
  1439. //! - \e ulFlags
  1440. //! - Set \b MSG_OBJ_TX_INT_ENABLE flag to enable interrupt on transmission.
  1441. //! - Set \b MSG_OBJ_RX_INT_ENABLE flag to enable interrupt on receipt.
  1442. //! - Set \b MSG_OBJ_USE_ID_FILTER flag to enable filtering based on the
  1443. //! identifier mask specified by \e ulMsgIDMask.
  1444. //! - \e ulMsgLen - the number of bytes in the message data. This parameter
  1445. //! should be non-zero even for a remote frame; it should match the expected
  1446. //! bytes of data in the responding data frame.
  1447. //! - \e pucMsgData - points to a buffer containing up to 8 bytes of data for a
  1448. //! data frame.
  1449. //!
  1450. //! \b Example: To send a data frame or remote frame (in response to a remote
  1451. //! request), take the following steps:
  1452. //!
  1453. //! -# Set \e eMsgType to \b MSG_OBJ_TYPE_TX.
  1454. //! -# Set \e pMsgObject->ulMsgID to the message ID.
  1455. //! -# Set \e pMsgObject->ulFlags. Make sure to set \b MSG_OBJ_TX_INT_ENABLE to
  1456. //! allow an interrupt to be generated when the message is sent.
  1457. //! -# Set \e pMsgObject->ulMsgLen to the number of bytes in the data frame.
  1458. //! -# Set \e pMsgObject->pucMsgData to point to an array containing the bytes
  1459. //! to send in the message.
  1460. //! -# Call this function with \e ulObjID set to one of the 32 object buffers.
  1461. //!
  1462. //! \b Example: To receive a specific data frame, take the following steps:
  1463. //!
  1464. //! -# Set \e eMsgObjType to \b MSG_OBJ_TYPE_RX.
  1465. //! -# Set \e pMsgObject->ulMsgID to the full message ID, or a partial mask to
  1466. //! use partial ID matching.
  1467. //! -# Set \e pMsgObject->ulMsgIDMask bits that should be used for masking
  1468. //! during comparison.
  1469. //! -# Set \e pMsgObject->ulFlags as follows:
  1470. //! - Set \b MSG_OBJ_RX_INT_ENABLE flag to be interrupted when the data frame
  1471. //! is received.
  1472. //! - Set \b MSG_OBJ_USE_ID_FILTER flag to enable identifier-based filtering.
  1473. //! -# Set \e pMsgObject->ulMsgLen to the number of bytes in the expected data
  1474. //! frame.
  1475. //! -# The buffer pointed to by \e pMsgObject->pucMsgData is not used by this
  1476. //! call as no data is present at the time of the call.
  1477. //! -# Call this function with \e ulObjID set to one of the 32 object buffers.
  1478. //!
  1479. //! If you specify a message object buffer that already contains a message
  1480. //! definition, it is overwritten.
  1481. //!
  1482. //! \return None.
  1483. //
  1484. //*****************************************************************************
  1485. void
  1486. CANMessageSet(unsigned long ulBase, unsigned long ulObjID,
  1487. tCANMsgObject *pMsgObject, tMsgObjType eMsgType)
  1488. {
  1489. unsigned short usCmdMaskReg;
  1490. unsigned short usMaskReg0, usMaskReg1;
  1491. unsigned short usArbReg0, usArbReg1;
  1492. unsigned short usMsgCtrl;
  1493. tBoolean bTransferData;
  1494. tBoolean bUseExtendedID;
  1495. bTransferData = 0;
  1496. //
  1497. // Check the arguments.
  1498. //
  1499. ASSERT(CANBaseValid(ulBase));
  1500. ASSERT((ulObjID <= 32) && (ulObjID != 0));
  1501. ASSERT((eMsgType == MSG_OBJ_TYPE_TX) ||
  1502. (eMsgType == MSG_OBJ_TYPE_TX_REMOTE) ||
  1503. (eMsgType == MSG_OBJ_TYPE_RX) ||
  1504. (eMsgType == MSG_OBJ_TYPE_RX_REMOTE) ||
  1505. (eMsgType == MSG_OBJ_TYPE_TX_REMOTE) ||
  1506. (eMsgType == MSG_OBJ_TYPE_RXTX_REMOTE));
  1507. //
  1508. // Wait for busy bit to clear
  1509. //
  1510. while(CANRegRead(ulBase + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1511. {
  1512. }
  1513. //
  1514. // See if we need to use an extended identifier or not.
  1515. //
  1516. if((pMsgObject->ulMsgID > CAN_MAX_11BIT_MSG_ID) ||
  1517. (pMsgObject->ulFlags & MSG_OBJ_EXTENDED_ID))
  1518. {
  1519. bUseExtendedID = 1;
  1520. }
  1521. else
  1522. {
  1523. bUseExtendedID = 0;
  1524. }
  1525. //
  1526. // This is always a write to the Message object as this call is setting a
  1527. // message object. This call will also always set all size bits so it sets
  1528. // both data bits. The call will use the CONTROL register to set control
  1529. // bits so this bit needs to be set as well.
  1530. //
  1531. usCmdMaskReg = (CAN_IF1CMSK_WRNRD | CAN_IF1CMSK_DATAA | CAN_IF1CMSK_DATAB |
  1532. CAN_IF1CMSK_CONTROL);
  1533. //
  1534. // Initialize the values to a known state before filling them in based on
  1535. // the type of message object that is being configured.
  1536. //
  1537. usArbReg0 = 0;
  1538. usArbReg1 = 0;
  1539. usMsgCtrl = 0;
  1540. usMaskReg0 = 0;
  1541. usMaskReg1 = 0;
  1542. switch(eMsgType)
  1543. {
  1544. //
  1545. // Transmit message object.
  1546. //
  1547. case MSG_OBJ_TYPE_TX:
  1548. {
  1549. //
  1550. // Set the TXRQST bit and the reset the rest of the register.
  1551. //
  1552. usMsgCtrl |= CAN_IF1MCTL_TXRQST;
  1553. usArbReg1 = CAN_IF1ARB2_DIR;
  1554. bTransferData = 1;
  1555. break;
  1556. }
  1557. //
  1558. // Transmit remote request message object
  1559. //
  1560. case MSG_OBJ_TYPE_TX_REMOTE:
  1561. {
  1562. //
  1563. // Set the TXRQST bit and the reset the rest of the register.
  1564. //
  1565. usMsgCtrl |= CAN_IF1MCTL_TXRQST;
  1566. usArbReg1 = 0;
  1567. break;
  1568. }
  1569. //
  1570. // Receive message object.
  1571. //
  1572. case MSG_OBJ_TYPE_RX:
  1573. {
  1574. //
  1575. // This clears the DIR bit along with everything else. The TXRQST
  1576. // bit was cleared by defaulting usMsgCtrl to 0.
  1577. //
  1578. usArbReg1 = 0;
  1579. break;
  1580. }
  1581. //
  1582. // Receive remote request message object.
  1583. //
  1584. case MSG_OBJ_TYPE_RX_REMOTE:
  1585. {
  1586. //
  1587. // The DIR bit is set to one for remote receivers. The TXRQST bit
  1588. // was cleared by defaulting usMsgCtrl to 0.
  1589. //
  1590. usArbReg1 = CAN_IF1ARB2_DIR;
  1591. //
  1592. // Set this object so that it only indicates that a remote frame
  1593. // was received and allow for software to handle it by sending back
  1594. // a data frame.
  1595. //
  1596. usMsgCtrl = CAN_IF1MCTL_UMASK;
  1597. //
  1598. // Use the full Identifier by default.
  1599. //
  1600. usMaskReg0 = 0xffff;
  1601. usMaskReg1 = 0x1fff;
  1602. //
  1603. // Make sure to send the mask to the message object.
  1604. //
  1605. usCmdMaskReg |= CAN_IF1CMSK_MASK;
  1606. break;
  1607. }
  1608. //
  1609. // Remote frame receive remote, with auto-transmit message object.
  1610. //
  1611. case MSG_OBJ_TYPE_RXTX_REMOTE:
  1612. {
  1613. //
  1614. // Oddly the DIR bit is set to one for remote receivers.
  1615. //
  1616. usArbReg1 = CAN_IF1ARB2_DIR;
  1617. //
  1618. // Set this object to auto answer if a matching identifier is seen.
  1619. //
  1620. usMsgCtrl = CAN_IF1MCTL_RMTEN | CAN_IF1MCTL_UMASK;
  1621. //
  1622. // The data to be returned needs to be filled in.
  1623. //
  1624. bTransferData = 1;
  1625. break;
  1626. }
  1627. //
  1628. // This case should never happen due to the ASSERT statement at the
  1629. // beginning of this function.
  1630. //
  1631. default:
  1632. {
  1633. return;
  1634. }
  1635. }
  1636. //
  1637. // Configure the Mask Registers.
  1638. //
  1639. if(pMsgObject->ulFlags & MSG_OBJ_USE_ID_FILTER)
  1640. {
  1641. if(bUseExtendedID)
  1642. {
  1643. //
  1644. // Set the 29 bits of Identifier mask that were requested.
  1645. //
  1646. usMaskReg0 = pMsgObject->ulMsgIDMask & CAN_IF1MSK1_IDMSK_M;
  1647. usMaskReg1 = ((pMsgObject->ulMsgIDMask >> 16) &
  1648. CAN_IF1MSK2_IDMSK_M);
  1649. }
  1650. else
  1651. {
  1652. //
  1653. // Lower 16 bit are unused so set them to zero.
  1654. //
  1655. usMaskReg0 = 0;
  1656. //
  1657. // Put the 11 bit Mask Identifier into the upper bits of the field
  1658. // in the register.
  1659. //
  1660. usMaskReg1 = ((pMsgObject->ulMsgIDMask << 2) &
  1661. CAN_IF1MSK2_IDMSK_M);
  1662. }
  1663. }
  1664. //
  1665. // If the caller wants to filter on the extended ID bit then set it.
  1666. //
  1667. if((pMsgObject->ulFlags & MSG_OBJ_USE_EXT_FILTER) ==
  1668. MSG_OBJ_USE_EXT_FILTER)
  1669. {
  1670. usMaskReg1 |= CAN_IF1MSK2_MXTD;
  1671. }
  1672. //
  1673. // The caller wants to filter on the message direction field.
  1674. //
  1675. if((pMsgObject->ulFlags & MSG_OBJ_USE_DIR_FILTER) ==
  1676. MSG_OBJ_USE_DIR_FILTER)
  1677. {
  1678. usMaskReg1 |= CAN_IF1MSK2_MDIR;
  1679. }
  1680. if(pMsgObject->ulFlags & (MSG_OBJ_USE_ID_FILTER | MSG_OBJ_USE_DIR_FILTER |
  1681. MSG_OBJ_USE_EXT_FILTER))
  1682. {
  1683. //
  1684. // Set the UMASK bit to enable using the mask register.
  1685. //
  1686. usMsgCtrl |= CAN_IF1MCTL_UMASK;
  1687. //
  1688. // Set the MASK bit so that this gets transferred to the Message Object.
  1689. //
  1690. usCmdMaskReg |= CAN_IF1CMSK_MASK;
  1691. }
  1692. //
  1693. // Set the Arb bit so that this gets transferred to the Message object.
  1694. //
  1695. usCmdMaskReg |= CAN_IF1CMSK_ARB;
  1696. //
  1697. // Configure the Arbitration registers.
  1698. //
  1699. if(bUseExtendedID)
  1700. {
  1701. //
  1702. // Set the 29 bit version of the Identifier for this message object.
  1703. //
  1704. usArbReg0 |= pMsgObject->ulMsgID & CAN_IF1ARB1_ID_M;
  1705. usArbReg1 |= (pMsgObject->ulMsgID >> 16) & CAN_IF1ARB2_ID_M;
  1706. //
  1707. // Mark the message as valid and set the extended ID bit.
  1708. //
  1709. usArbReg1 |= CAN_IF1ARB2_MSGVAL | CAN_IF1ARB2_XTD;
  1710. }
  1711. else
  1712. {
  1713. //
  1714. // Set the 11 bit version of the Identifier for this message object.
  1715. // The lower 18 bits are set to zero.
  1716. //
  1717. usArbReg1 |= (pMsgObject->ulMsgID << 2) & CAN_IF1ARB2_ID_M;
  1718. //
  1719. // Mark the message as valid.
  1720. //
  1721. usArbReg1 |= CAN_IF1ARB2_MSGVAL;
  1722. }
  1723. //
  1724. // Set the data length since this is set for all transfers. This is also a
  1725. // single transfer and not a FIFO transfer so set EOB bit.
  1726. //
  1727. usMsgCtrl |= (pMsgObject->ulMsgLen & CAN_IF1MCTL_DLC_M);
  1728. //
  1729. // Mark this as the last entry if this is not the last entry in a FIFO.
  1730. //
  1731. if((pMsgObject->ulFlags & MSG_OBJ_FIFO) == 0)
  1732. {
  1733. usMsgCtrl |= CAN_IF1MCTL_EOB;
  1734. }
  1735. //
  1736. // Enable transmit interrupts if they should be enabled.
  1737. //
  1738. if(pMsgObject->ulFlags & MSG_OBJ_TX_INT_ENABLE)
  1739. {
  1740. usMsgCtrl |= CAN_IF1MCTL_TXIE;
  1741. }
  1742. //
  1743. // Enable receive interrupts if they should be enabled.
  1744. //
  1745. if(pMsgObject->ulFlags & MSG_OBJ_RX_INT_ENABLE)
  1746. {
  1747. usMsgCtrl |= CAN_IF1MCTL_RXIE;
  1748. }
  1749. //
  1750. // Write the data out to the CAN Data registers if needed.
  1751. //
  1752. if(bTransferData)
  1753. {
  1754. CANDataRegWrite(pMsgObject->pucMsgData,
  1755. (unsigned long *)(ulBase + CAN_O_IF1DA1),
  1756. pMsgObject->ulMsgLen);
  1757. }
  1758. //
  1759. // Write out the registers to program the message object.
  1760. //
  1761. CANRegWrite(ulBase + CAN_O_IF1CMSK, usCmdMaskReg);
  1762. CANRegWrite(ulBase + CAN_O_IF1MSK1, usMaskReg0);
  1763. CANRegWrite(ulBase + CAN_O_IF1MSK2, usMaskReg1);
  1764. CANRegWrite(ulBase + CAN_O_IF1ARB1, usArbReg0);
  1765. CANRegWrite(ulBase + CAN_O_IF1ARB2, usArbReg1);
  1766. CANRegWrite(ulBase + CAN_O_IF1MCTL, usMsgCtrl);
  1767. //
  1768. // Transfer the message object to the message object specified by ulObjID.
  1769. //
  1770. CANRegWrite(ulBase + CAN_O_IF1CRQ, ulObjID & CAN_IF1CRQ_MNUM_M);
  1771. }
  1772. //*****************************************************************************
  1773. //
  1774. //! Reads a CAN message from one of the message object buffers.
  1775. //!
  1776. //! \param ulBase is the base address of the CAN controller.
  1777. //! \param ulObjID is the object number to read (1-32).
  1778. //! \param pMsgObject points to a structure containing message object fields.
  1779. //! \param bClrPendingInt indicates whether an associated interrupt should be
  1780. //! cleared.
  1781. //!
  1782. //! This function is used to read the contents of one of the 32 message objects
  1783. //! in the CAN controller and return it to the caller. The data returned is
  1784. //! stored in the fields of the caller-supplied structure pointed to by
  1785. //! \e pMsgObject. The data consists of all of the parts of a CAN message,
  1786. //! plus some control and status information.
  1787. //!
  1788. //! Normally, this function is used to read a message object that has received
  1789. //! and stored a CAN message with a certain identifier. However, this function
  1790. //! could also be used to read the contents of a message object in order to
  1791. //! load the fields of the structure in case only part of the structure must
  1792. //! be changed from a previous setting.
  1793. //!
  1794. //! When using CANMessageGet(), all of the same fields of the structure are
  1795. //! populated in the same way as when the CANMessageSet() function is used,
  1796. //! with the following exceptions:
  1797. //!
  1798. //! \e pMsgObject->ulFlags:
  1799. //!
  1800. //! - \b MSG_OBJ_NEW_DATA indicates if this data is new since the last time it
  1801. //! was read
  1802. //! - \b MSG_OBJ_DATA_LOST indicates that at least one message was received on
  1803. //! this message object and not read by the host before being overwritten.
  1804. //!
  1805. //! \return None.
  1806. //
  1807. //*****************************************************************************
  1808. void
  1809. CANMessageGet(unsigned long ulBase, unsigned long ulObjID,
  1810. tCANMsgObject *pMsgObject, tBoolean bClrPendingInt)
  1811. {
  1812. unsigned short usCmdMaskReg;
  1813. unsigned short usMaskReg0, usMaskReg1;
  1814. unsigned short usArbReg0, usArbReg1;
  1815. unsigned short usMsgCtrl;
  1816. //
  1817. // Check the arguments.
  1818. //
  1819. ASSERT(CANBaseValid(ulBase));
  1820. ASSERT((ulObjID <= 32) && (ulObjID != 0));
  1821. //
  1822. // This is always a read to the Message object as this call is setting a
  1823. // message object.
  1824. //
  1825. usCmdMaskReg = (CAN_IF1CMSK_DATAA | CAN_IF1CMSK_DATAB |
  1826. CAN_IF1CMSK_CONTROL | CAN_IF1CMSK_MASK | CAN_IF1CMSK_ARB);
  1827. //
  1828. // Clear a pending interrupt and new data in a message object.
  1829. //
  1830. if(bClrPendingInt)
  1831. {
  1832. usCmdMaskReg |= CAN_IF1CMSK_CLRINTPND;
  1833. }
  1834. //
  1835. // Set up the request for data from the message object.
  1836. //
  1837. CANRegWrite(ulBase + CAN_O_IF2CMSK, usCmdMaskReg);
  1838. //
  1839. // Transfer the message object to the message object specified by ulObjID.
  1840. //
  1841. CANRegWrite(ulBase + CAN_O_IF2CRQ, ulObjID & CAN_IF1CRQ_MNUM_M);
  1842. //
  1843. // Wait for busy bit to clear
  1844. //
  1845. while(CANRegRead(ulBase + CAN_O_IF2CRQ) & CAN_IF1CRQ_BUSY)
  1846. {
  1847. }
  1848. //
  1849. // Read out the IF Registers.
  1850. //
  1851. usMaskReg0 = CANRegRead(ulBase + CAN_O_IF2MSK1);
  1852. usMaskReg1 = CANRegRead(ulBase + CAN_O_IF2MSK2);
  1853. usArbReg0 = CANRegRead(ulBase + CAN_O_IF2ARB1);
  1854. usArbReg1 = CANRegRead(ulBase + CAN_O_IF2ARB2);
  1855. usMsgCtrl = CANRegRead(ulBase + CAN_O_IF2MCTL);
  1856. pMsgObject->ulFlags = MSG_OBJ_NO_FLAGS;
  1857. //
  1858. // Determine if this is a remote frame by checking the TXRQST and DIR bits.
  1859. //
  1860. if((!(usMsgCtrl & CAN_IF1MCTL_TXRQST) && (usArbReg1 & CAN_IF1ARB2_DIR)) ||
  1861. ((usMsgCtrl & CAN_IF1MCTL_TXRQST) && (!(usArbReg1 & CAN_IF1ARB2_DIR))))
  1862. {
  1863. pMsgObject->ulFlags |= MSG_OBJ_REMOTE_FRAME;
  1864. }
  1865. //
  1866. // Get the identifier out of the register, the format depends on size of
  1867. // the mask.
  1868. //
  1869. if(usArbReg1 & CAN_IF1ARB2_XTD)
  1870. {
  1871. //
  1872. // Set the 29 bit version of the Identifier for this message object.
  1873. //
  1874. pMsgObject->ulMsgID = ((usArbReg1 & CAN_IF1ARB2_ID_M) << 16) |
  1875. usArbReg0;
  1876. pMsgObject->ulFlags |= MSG_OBJ_EXTENDED_ID;
  1877. }
  1878. else
  1879. {
  1880. //
  1881. // The Identifier is an 11 bit value.
  1882. //
  1883. pMsgObject->ulMsgID = (usArbReg1 & CAN_IF1ARB2_ID_M) >> 2;
  1884. }
  1885. //
  1886. // Indicate that we lost some data.
  1887. //
  1888. if(usMsgCtrl & CAN_IF1MCTL_MSGLST)
  1889. {
  1890. pMsgObject->ulFlags |= MSG_OBJ_DATA_LOST;
  1891. }
  1892. //
  1893. // Set the flag to indicate if ID masking was used.
  1894. //
  1895. if(usMsgCtrl & CAN_IF1MCTL_UMASK)
  1896. {
  1897. if(usArbReg1 & CAN_IF1ARB2_XTD)
  1898. {
  1899. //
  1900. // The Identifier Mask is assumed to also be a 29 bit value.
  1901. //
  1902. pMsgObject->ulMsgIDMask =
  1903. ((usMaskReg1 & CAN_IF1MSK2_IDMSK_M) << 16) | usMaskReg0;
  1904. //
  1905. // If this is a fully specified Mask and a remote frame then don't
  1906. // set the MSG_OBJ_USE_ID_FILTER because the ID was not really
  1907. // filtered.
  1908. //
  1909. if((pMsgObject->ulMsgIDMask != 0x1fffffff) ||
  1910. ((pMsgObject->ulFlags & MSG_OBJ_REMOTE_FRAME) == 0))
  1911. {
  1912. pMsgObject->ulFlags |= MSG_OBJ_USE_ID_FILTER;
  1913. }
  1914. }
  1915. else
  1916. {
  1917. //
  1918. // The Identifier Mask is assumed to also be an 11 bit value.
  1919. //
  1920. pMsgObject->ulMsgIDMask = ((usMaskReg1 & CAN_IF1MSK2_IDMSK_M) >>
  1921. 2);
  1922. //
  1923. // If this is a fully specified Mask and a remote frame then don't
  1924. // set the MSG_OBJ_USE_ID_FILTER because the ID was not really
  1925. // filtered.
  1926. //
  1927. if((pMsgObject->ulMsgIDMask != 0x7ff) ||
  1928. ((pMsgObject->ulFlags & MSG_OBJ_REMOTE_FRAME) == 0))
  1929. {
  1930. pMsgObject->ulFlags |= MSG_OBJ_USE_ID_FILTER;
  1931. }
  1932. }
  1933. //
  1934. // Indicate if the extended bit was used in filtering.
  1935. //
  1936. if(usMaskReg1 & CAN_IF1MSK2_MXTD)
  1937. {
  1938. pMsgObject->ulFlags |= MSG_OBJ_USE_EXT_FILTER;
  1939. }
  1940. //
  1941. // Indicate if direction filtering was enabled.
  1942. //
  1943. if(usMaskReg1 & CAN_IF1MSK2_MDIR)
  1944. {
  1945. pMsgObject->ulFlags |= MSG_OBJ_USE_DIR_FILTER;
  1946. }
  1947. }
  1948. //
  1949. // Set the interrupt flags.
  1950. //
  1951. if(usMsgCtrl & CAN_IF1MCTL_TXIE)
  1952. {
  1953. pMsgObject->ulFlags |= MSG_OBJ_TX_INT_ENABLE;
  1954. }
  1955. if(usMsgCtrl & CAN_IF1MCTL_RXIE)
  1956. {
  1957. pMsgObject->ulFlags |= MSG_OBJ_RX_INT_ENABLE;
  1958. }
  1959. //
  1960. // See if there is new data available.
  1961. //
  1962. if(usMsgCtrl & CAN_IF1MCTL_NEWDAT)
  1963. {
  1964. //
  1965. // Get the amount of data needed to be read.
  1966. //
  1967. pMsgObject->ulMsgLen = (usMsgCtrl & CAN_IF1MCTL_DLC_M);
  1968. //
  1969. // Don't read any data for a remote frame, there is nothing valid in
  1970. // that buffer anyway.
  1971. //
  1972. if((pMsgObject->ulFlags & MSG_OBJ_REMOTE_FRAME) == 0)
  1973. {
  1974. //
  1975. // Read out the data from the CAN registers.
  1976. //
  1977. CANDataRegRead(pMsgObject->pucMsgData,
  1978. (unsigned long *)(ulBase + CAN_O_IF2DA1),
  1979. pMsgObject->ulMsgLen);
  1980. }
  1981. //
  1982. // Now clear out the new data flag.
  1983. //
  1984. CANRegWrite(ulBase + CAN_O_IF2CMSK, CAN_IF1CMSK_NEWDAT);
  1985. //
  1986. // Transfer the message object to the message object specified by
  1987. // ulObjID.
  1988. //
  1989. CANRegWrite(ulBase + CAN_O_IF2CRQ, ulObjID & CAN_IF1CRQ_MNUM_M);
  1990. //
  1991. // Wait for busy bit to clear
  1992. //
  1993. while(CANRegRead(ulBase + CAN_O_IF2CRQ) & CAN_IF1CRQ_BUSY)
  1994. {
  1995. }
  1996. //
  1997. // Indicate that there is new data in this message.
  1998. //
  1999. pMsgObject->ulFlags |= MSG_OBJ_NEW_DATA;
  2000. }
  2001. else
  2002. {
  2003. //
  2004. // Along with the MSG_OBJ_NEW_DATA not being set the amount of data
  2005. // needs to be set to zero if none was available.
  2006. //
  2007. pMsgObject->ulMsgLen = 0;
  2008. }
  2009. }
  2010. //*****************************************************************************
  2011. //
  2012. //! Clears a message object so that it is no longer used.
  2013. //!
  2014. //! \param ulBase is the base address of the CAN controller.
  2015. //! \param ulObjID is the message object number to disable (1-32).
  2016. //!
  2017. //! This function frees the specified message object from use. Once a message
  2018. //! object has been ``cleared,'' it no longer automatically sends or
  2019. //! receives messages, nor does it generate interrupts.
  2020. //!
  2021. //! \return None.
  2022. //
  2023. //*****************************************************************************
  2024. void
  2025. CANMessageClear(unsigned long ulBase, unsigned long ulObjID)
  2026. {
  2027. //
  2028. // Check the arguments.
  2029. //
  2030. ASSERT(CANBaseValid(ulBase));
  2031. ASSERT((ulObjID >= 1) && (ulObjID <= 32));
  2032. //
  2033. // Wait for busy bit to clear
  2034. //
  2035. while(CANRegRead(ulBase + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  2036. {
  2037. }
  2038. //
  2039. // Clear the message value bit in the arbitration register. This indicates
  2040. // the message is not valid.
  2041. //
  2042. CANRegWrite(ulBase + CAN_O_IF1CMSK, CAN_IF1CMSK_WRNRD | CAN_IF1CMSK_ARB);
  2043. CANRegWrite(ulBase + CAN_O_IF1ARB1, 0);
  2044. CANRegWrite(ulBase + CAN_O_IF1ARB2, 0);
  2045. //
  2046. // Initiate programming the message object
  2047. //
  2048. CANRegWrite(ulBase + CAN_O_IF1CRQ, ulObjID & CAN_IF1CRQ_MNUM_M);
  2049. }
  2050. //*****************************************************************************
  2051. //
  2052. // Close the Doxygen group.
  2053. //! @}
  2054. //
  2055. //*****************************************************************************