can.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  1. //*****************************************************************************
  2. //
  3. // can.c - Driver for the CAN module.
  4. //
  5. // Copyright (c) 2006-2010 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 6459 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 int iDelay;
  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(iDelay = 0; iDelay < CAN_RW_DELAY; iDelay++)
  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 int iDelay;
  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(iDelay = 0; iDelay < CAN_RW_DELAY; iDelay++)
  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, int iSize)
  334. {
  335. int iIdx;
  336. unsigned long ulValue;
  337. //
  338. // Loop always copies 1 or 2 bytes per iteration.
  339. //
  340. for(iIdx = 0; iIdx < iSize; )
  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[iIdx++];
  347. //
  348. // Only write the second byte if needed otherwise it will be zero.
  349. //
  350. if(iIdx < iSize)
  351. {
  352. ulValue |= (pucData[iIdx++] << 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, int iSize)
  385. {
  386. int iIdx;
  387. unsigned long ulValue;
  388. //
  389. // Loop always copies 1 or 2 bytes per iteration.
  390. //
  391. for(iIdx = 0; iIdx < iSize; )
  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[iIdx++] = (unsigned char)ulValue;
  402. //
  403. // Only read the second byte if needed.
  404. //
  405. if(iIdx < iSize)
  406. {
  407. pucData[iIdx++] = (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. int iMsg;
  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(iMsg = 1; iMsg <= 32; iMsg++)
  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, iMsg);
  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(iMsg = 1; iMsg <= 32; iMsg++)
  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, iMsg);
  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 int uBitReg;
  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. uBitReg = CANRegRead(ulBase + CAN_O_BIT);
  588. //
  589. // Set the phase 2 segment.
  590. //
  591. pClkParms->uPhase2Seg =
  592. ((uBitReg & CAN_BIT_TSEG2_M) >> CAN_BIT_TSEG2_S) + 1;
  593. //
  594. // Set the phase 1 segment.
  595. //
  596. pClkParms->uSyncPropPhase1Seg =
  597. ((uBitReg & CAN_BIT_TSEG1_M) >> CAN_BIT_TSEG1_S) + 1;
  598. //
  599. // Set the synchronous jump width.
  600. //
  601. pClkParms->uSJW = ((uBitReg & CAN_BIT_SJW_M) >> CAN_BIT_SJW_S) + 1;
  602. //
  603. // Set the pre-divider for the CAN bus bit clock.
  604. //
  605. pClkParms->uQuantumPrescaler =
  606. ((uBitReg & 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->uSyncPropPhase1Seg parameter. Phase Buffer 2 is determined
  755. //! from the \e pClkParms->uPhase2Seg parameter. These two parameters, along
  756. //! with \e pClkParms->uSJW are based in units of bit time quanta. The actual
  757. //! quantum time is determined by the \e pClkParms->uQuantumPrescaler 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 = uSyncPropPhase1Seg + uPhase2Seg + 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 uSyncPropPhase1Seg + \e uPhase2Seg + 1) * (\e uQuantumPrescaler))
  772. //!
  773. //! This means that with \e uSyncPropPhase1Seg = 4, \e uPhase2Seg = 1,
  774. //! \e uQuantumPrescaler = 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 int uBitReg;
  788. unsigned int uSavedInit;
  789. //
  790. // Check the arguments.
  791. //
  792. ASSERT(CANBaseValid(ulBase));
  793. ASSERT(pClkParms != 0);
  794. //
  795. // The phase 1 segment must be in the range from 2 to 16.
  796. //
  797. ASSERT((pClkParms->uSyncPropPhase1Seg >= 2) &&
  798. (pClkParms->uSyncPropPhase1Seg <= 16));
  799. //
  800. // The phase 2 segment must be in the range from 1 to 8.
  801. //
  802. ASSERT((pClkParms->uPhase2Seg >= 1) && (pClkParms->uPhase2Seg <= 8));
  803. //
  804. // The synchronous jump windows must be in the range from 1 to 4.
  805. //
  806. ASSERT((pClkParms->uSJW >= 1) && (pClkParms->uSJW <= 4));
  807. //
  808. // The CAN clock pre-divider must be in the range from 1 to 1024.
  809. //
  810. ASSERT((pClkParms->uQuantumPrescaler <= 1024) &&
  811. (pClkParms->uQuantumPrescaler >= 1));
  812. //
  813. // To set the bit timing register, the controller must be placed in init
  814. // mode (if not already), and also configuration change bit enabled. State
  815. // of the init bit should be saved so it can be restored at the end.
  816. //
  817. uSavedInit = CANRegRead(ulBase + CAN_O_CTL);
  818. CANRegWrite(ulBase + CAN_O_CTL, uSavedInit | CAN_CTL_INIT | CAN_CTL_CCE);
  819. //
  820. // Set the bit fields of the bit timing register according to the parms.
  821. //
  822. uBitReg = (((pClkParms->uPhase2Seg - 1) << CAN_BIT_TSEG2_S) &
  823. CAN_BIT_TSEG2_M);
  824. uBitReg |= (((pClkParms->uSyncPropPhase1Seg - 1) << CAN_BIT_TSEG1_S) &
  825. CAN_BIT_TSEG1_M);
  826. uBitReg |= ((pClkParms->uSJW - 1) << CAN_BIT_SJW_S) & CAN_BIT_SJW_M;
  827. uBitReg |= (pClkParms->uQuantumPrescaler - 1) & CAN_BIT_BRP_M;
  828. CANRegWrite(ulBase + CAN_O_BIT, uBitReg);
  829. //
  830. // Set the divider upper bits in the extension register.
  831. //
  832. CANRegWrite(ulBase + CAN_O_BRPE,
  833. ((pClkParms->uQuantumPrescaler - 1) >> 6) & CAN_BRPE_BRPE_M);
  834. //
  835. // Clear the config change bit, and restore the init bit.
  836. //
  837. uSavedInit &= ~CAN_CTL_CCE;
  838. //
  839. // If Init was not set before, then clear it.
  840. //
  841. if(uSavedInit & CAN_CTL_INIT)
  842. {
  843. uSavedInit &= ~CAN_CTL_INIT;
  844. }
  845. CANRegWrite(ulBase + CAN_O_CTL, uSavedInit);
  846. }
  847. //*****************************************************************************
  848. //
  849. //! Registers an interrupt handler for the CAN controller.
  850. //!
  851. //! \param ulBase is the base address of the CAN controller.
  852. //! \param pfnHandler is a pointer to the function to be called when the
  853. //! enabled CAN interrupts occur.
  854. //!
  855. //! This function registers the interrupt handler in the interrupt vector
  856. //! table, and enables CAN interrupts on the interrupt controller; specific CAN
  857. //! interrupt sources must be enabled using CANIntEnable(). The interrupt
  858. //! handler being registered must clear the source of the interrupt using
  859. //! CANIntClear().
  860. //!
  861. //! If the application is using a static interrupt vector table stored in
  862. //! flash, then it is not necessary to register the interrupt handler this way.
  863. //! Instead, IntEnable() should be used to enable CAN interrupts on the
  864. //! interrupt controller.
  865. //!
  866. //! \sa IntRegister() for important information about registering interrupt
  867. //! handlers.
  868. //!
  869. //! \return None.
  870. //
  871. //*****************************************************************************
  872. void
  873. CANIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
  874. {
  875. unsigned long ulIntNumber;
  876. //
  877. // Check the arguments.
  878. //
  879. ASSERT(CANBaseValid(ulBase));
  880. //
  881. // Get the actual interrupt number for this CAN controller.
  882. //
  883. ulIntNumber = CANIntNumberGet(ulBase);
  884. //
  885. // Register the interrupt handler.
  886. //
  887. IntRegister(ulIntNumber, pfnHandler);
  888. //
  889. // Enable the Ethernet interrupt.
  890. //
  891. IntEnable(ulIntNumber);
  892. }
  893. //*****************************************************************************
  894. //
  895. //! Unregisters an interrupt handler for the CAN controller.
  896. //!
  897. //! \param ulBase is the base address of the controller.
  898. //!
  899. //! This function unregisters the previously registered interrupt handler and
  900. //! disables the interrupt on the interrupt controller.
  901. //!
  902. //! \sa IntRegister() for important information about registering interrupt
  903. //! handlers.
  904. //!
  905. //! \return None.
  906. //
  907. //*****************************************************************************
  908. void
  909. CANIntUnregister(unsigned long ulBase)
  910. {
  911. unsigned long ulIntNumber;
  912. //
  913. // Check the arguments.
  914. //
  915. ASSERT(CANBaseValid(ulBase));
  916. //
  917. // Get the actual interrupt number for this CAN controller.
  918. //
  919. ulIntNumber = CANIntNumberGet(ulBase);
  920. //
  921. // Register the interrupt handler.
  922. //
  923. IntUnregister(ulIntNumber);
  924. //
  925. // Disable the CAN interrupt.
  926. //
  927. IntDisable(ulIntNumber);
  928. }
  929. //*****************************************************************************
  930. //
  931. //! Enables individual CAN controller interrupt sources.
  932. //!
  933. //! \param ulBase is the base address of the CAN controller.
  934. //! \param ulIntFlags is the bit mask of the interrupt sources to be enabled.
  935. //!
  936. //! Enables specific interrupt sources of the CAN controller. Only enabled
  937. //! sources will cause a processor interrupt.
  938. //!
  939. //! The \e ulIntFlags parameter is the logical OR of any of the following:
  940. //!
  941. //! - \b CAN_INT_ERROR - a controller error condition has occurred
  942. //! - \b CAN_INT_STATUS - a message transfer has completed, or a bus error has
  943. //! been detected
  944. //! - \b CAN_INT_MASTER - allow CAN controller to generate interrupts
  945. //!
  946. //! In order to generate any interrupts, \b CAN_INT_MASTER must be enabled.
  947. //! Further, for any particular transaction from a message object to generate
  948. //! an interrupt, that message object must have interrupts enabled (see
  949. //! CANMessageSet()). \b CAN_INT_ERROR will generate an interrupt if the
  950. //! controller enters the ``bus off'' condition, or if the error counters reach
  951. //! a limit. \b CAN_INT_STATUS will generate an interrupt under quite a few
  952. //! status conditions and may provide more interrupts than the application
  953. //! needs to handle. When an interrupt occurs, use CANIntStatus() to determine
  954. //! the cause.
  955. //!
  956. //! \return None.
  957. //
  958. //*****************************************************************************
  959. void
  960. CANIntEnable(unsigned long ulBase, unsigned long ulIntFlags)
  961. {
  962. //
  963. // Check the arguments.
  964. //
  965. ASSERT(CANBaseValid(ulBase));
  966. ASSERT((ulIntFlags & ~(CAN_CTL_EIE | CAN_CTL_SIE | CAN_CTL_IE)) == 0);
  967. //
  968. // Enable the specified interrupts.
  969. //
  970. CANRegWrite(ulBase + CAN_O_CTL,
  971. CANRegRead(ulBase + CAN_O_CTL) | ulIntFlags);
  972. }
  973. //*****************************************************************************
  974. //
  975. //! Disables individual CAN controller interrupt sources.
  976. //!
  977. //! \param ulBase is the base address of the CAN controller.
  978. //! \param ulIntFlags is the bit mask of the interrupt sources to be disabled.
  979. //!
  980. //! Disables the specified CAN controller interrupt sources. Only enabled
  981. //! interrupt sources can cause a processor interrupt.
  982. //!
  983. //! The \e ulIntFlags parameter has the same definition as in the
  984. //! CANIntEnable() function.
  985. //!
  986. //! \return None.
  987. //
  988. //*****************************************************************************
  989. void
  990. CANIntDisable(unsigned long ulBase, unsigned long ulIntFlags)
  991. {
  992. //
  993. // Check the arguments.
  994. //
  995. ASSERT(CANBaseValid(ulBase));
  996. ASSERT((ulIntFlags & ~(CAN_CTL_EIE | CAN_CTL_SIE | CAN_CTL_IE)) == 0);
  997. //
  998. // Disable the specified interrupts.
  999. //
  1000. CANRegWrite(ulBase + CAN_O_CTL,
  1001. CANRegRead(ulBase + CAN_O_CTL) & ~(ulIntFlags));
  1002. }
  1003. //*****************************************************************************
  1004. //
  1005. //! Returns the current CAN controller interrupt status.
  1006. //!
  1007. //! \param ulBase is the base address of the CAN controller.
  1008. //! \param eIntStsReg indicates which interrupt status register to read
  1009. //!
  1010. //! Returns the value of one of two interrupt status registers. The interrupt
  1011. //! status register read is determined by the \e eIntStsReg parameter, which
  1012. //! can have one of the following values:
  1013. //!
  1014. //! - \b CAN_INT_STS_CAUSE - indicates the cause of the interrupt
  1015. //! - \b CAN_INT_STS_OBJECT - indicates pending interrupts of all message
  1016. //! objects
  1017. //!
  1018. //! \b CAN_INT_STS_CAUSE returns the value of the controller interrupt register
  1019. //! and indicates the cause of the interrupt. It will be a value of
  1020. //! \b CAN_INT_INTID_STATUS if the cause is a status interrupt. In this case,
  1021. //! the status register should be read with the CANStatusGet() function.
  1022. //! Calling this function to read the status will also clear the status
  1023. //! interrupt. If the value of the interrupt register is in the range 1-32,
  1024. //! then this indicates the number of the highest priority message object that
  1025. //! has an interrupt pending. The message object interrupt can be cleared by
  1026. //! using the CANIntClear() function, or by reading the message using
  1027. //! CANMessageGet() in the case of a received message. The interrupt handler
  1028. //! can read the interrupt status again to make sure all pending interrupts are
  1029. //! cleared before returning from the interrupt.
  1030. //!
  1031. //! \b CAN_INT_STS_OBJECT returns a bit mask indicating which message objects
  1032. //! have pending interrupts. This can be used to discover all of the pending
  1033. //! interrupts at once, as opposed to repeatedly reading the interrupt register
  1034. //! by using \b CAN_INT_STS_CAUSE.
  1035. //!
  1036. //! \return Returns the value of one of the interrupt status registers.
  1037. //
  1038. //*****************************************************************************
  1039. unsigned long
  1040. CANIntStatus(unsigned long ulBase, tCANIntStsReg eIntStsReg)
  1041. {
  1042. unsigned long ulStatus;
  1043. //
  1044. // Check the arguments.
  1045. //
  1046. ASSERT(CANBaseValid(ulBase));
  1047. //
  1048. // See which status the caller is looking for.
  1049. //
  1050. switch(eIntStsReg)
  1051. {
  1052. //
  1053. // The caller wants the global interrupt status for the CAN controller
  1054. // specified by ulBase.
  1055. //
  1056. case CAN_INT_STS_CAUSE:
  1057. {
  1058. ulStatus = CANRegRead(ulBase + CAN_O_INT);
  1059. break;
  1060. }
  1061. //
  1062. // The caller wants the current message status interrupt for all
  1063. // messages.
  1064. //
  1065. case CAN_INT_STS_OBJECT:
  1066. {
  1067. //
  1068. // Read and combine both 16 bit values into one 32bit status.
  1069. //
  1070. ulStatus = (CANRegRead(ulBase + CAN_O_MSG1INT) &
  1071. CAN_MSG1INT_INTPND_M);
  1072. ulStatus |= (CANRegRead(ulBase + CAN_O_MSG2INT) << 16);
  1073. break;
  1074. }
  1075. //
  1076. // Request was for unknown status so just return 0.
  1077. //
  1078. default:
  1079. {
  1080. ulStatus = 0;
  1081. break;
  1082. }
  1083. }
  1084. //
  1085. // Return the interrupt status value
  1086. //
  1087. return(ulStatus);
  1088. }
  1089. //*****************************************************************************
  1090. //
  1091. //! Clears a CAN interrupt source.
  1092. //!
  1093. //! \param ulBase is the base address of the CAN controller.
  1094. //! \param ulIntClr is a value indicating which interrupt source to clear.
  1095. //!
  1096. //! This function can be used to clear a specific interrupt source. The
  1097. //! \e ulIntClr parameter should be one of the following values:
  1098. //!
  1099. //! - \b CAN_INT_INTID_STATUS - Clears a status interrupt.
  1100. //! - 1-32 - Clears the specified message object interrupt
  1101. //!
  1102. //! It is not necessary to use this function to clear an interrupt. This
  1103. //! should only be used if the application wants to clear an interrupt source
  1104. //! without taking the normal interrupt action.
  1105. //!
  1106. //! Normally, the status interrupt is cleared by reading the controller status
  1107. //! using CANStatusGet(). A specific message object interrupt is normally
  1108. //! cleared by reading the message object using CANMessageGet().
  1109. //!
  1110. //! \note Since there is a write buffer in the Cortex-M3 processor, it may take
  1111. //! several clock cycles before the interrupt source is actually cleared.
  1112. //! Therefore, it is recommended that the interrupt source be cleared early in
  1113. //! the interrupt handler (as opposed to the very last action) to avoid
  1114. //! returning from the interrupt handler before the interrupt source is
  1115. //! actually cleared. Failure to do so may result in the interrupt handler
  1116. //! being immediately reentered (since NVIC still sees the interrupt source
  1117. //! asserted).
  1118. //!
  1119. //! \return None.
  1120. //
  1121. //*****************************************************************************
  1122. void
  1123. CANIntClear(unsigned long ulBase, unsigned long ulIntClr)
  1124. {
  1125. //
  1126. // Check the arguments.
  1127. //
  1128. ASSERT(CANBaseValid(ulBase));
  1129. ASSERT((ulIntClr == CAN_INT_INTID_STATUS) ||
  1130. ((ulIntClr>=1) && (ulIntClr <=32)));
  1131. if(ulIntClr == CAN_INT_INTID_STATUS)
  1132. {
  1133. //
  1134. // Simply read and discard the status to clear the interrupt.
  1135. //
  1136. CANRegRead(ulBase + CAN_O_STS);
  1137. }
  1138. else
  1139. {
  1140. //
  1141. // Wait to be sure that this interface is not busy.
  1142. //
  1143. while(CANRegRead(ulBase + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1144. {
  1145. }
  1146. //
  1147. // Only change the interrupt pending state by setting only the
  1148. // CAN_IF1CMSK_CLRINTPND bit.
  1149. //
  1150. CANRegWrite(ulBase + CAN_O_IF1CMSK, CAN_IF1CMSK_CLRINTPND);
  1151. //
  1152. // Send the clear pending interrupt command to the CAN controller.
  1153. //
  1154. CANRegWrite(ulBase + CAN_O_IF1CRQ, ulIntClr & CAN_IF1CRQ_MNUM_M);
  1155. //
  1156. // Wait to be sure that this interface is not busy.
  1157. //
  1158. while(CANRegRead(ulBase + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1159. {
  1160. }
  1161. }
  1162. }
  1163. //*****************************************************************************
  1164. //
  1165. //! Sets the CAN controller automatic retransmission behavior.
  1166. //!
  1167. //! \param ulBase is the base address of the CAN controller.
  1168. //! \param bAutoRetry enables automatic retransmission.
  1169. //!
  1170. //! Enables or disables automatic retransmission of messages with detected
  1171. //! errors. If \e bAutoRetry is \b true, then automatic retransmission is
  1172. //! enabled, otherwise it is disabled.
  1173. //!
  1174. //! \return None.
  1175. //
  1176. //*****************************************************************************
  1177. void
  1178. CANRetrySet(unsigned long ulBase, tBoolean bAutoRetry)
  1179. {
  1180. unsigned long ulCtlReg;
  1181. //
  1182. // Check the arguments.
  1183. //
  1184. ASSERT(CANBaseValid(ulBase));
  1185. ulCtlReg = CANRegRead(ulBase + CAN_O_CTL);
  1186. //
  1187. // Conditionally set the DAR bit to enable/disable auto-retry.
  1188. //
  1189. if(bAutoRetry)
  1190. {
  1191. //
  1192. // Clearing the DAR bit tells the controller to not disable the
  1193. // auto-retry of messages which were not transmitted or received
  1194. // correctly.
  1195. //
  1196. ulCtlReg &= ~CAN_CTL_DAR;
  1197. }
  1198. else
  1199. {
  1200. //
  1201. // Setting the DAR bit tells the controller to disable the auto-retry
  1202. // of messages which were not transmitted or received correctly.
  1203. //
  1204. ulCtlReg |= CAN_CTL_DAR;
  1205. }
  1206. CANRegWrite(ulBase + CAN_O_CTL, ulCtlReg);
  1207. }
  1208. //*****************************************************************************
  1209. //
  1210. //! Returns the current setting for automatic retransmission.
  1211. //!
  1212. //! \param ulBase is the base address of the CAN controller.
  1213. //!
  1214. //! Reads the current setting for the automatic retransmission in the CAN
  1215. //! controller and returns it to the caller.
  1216. //!
  1217. //! \return Returns \b true if automatic retransmission is enabled, \b false
  1218. //! otherwise.
  1219. //
  1220. //*****************************************************************************
  1221. tBoolean
  1222. CANRetryGet(unsigned long ulBase)
  1223. {
  1224. //
  1225. // Check the arguments.
  1226. //
  1227. ASSERT(CANBaseValid(ulBase));
  1228. //
  1229. // Read the disable automatic retry setting from the CAN controller.
  1230. //
  1231. if(CANRegRead(ulBase + CAN_O_CTL) & CAN_CTL_DAR)
  1232. {
  1233. //
  1234. // Automatic data retransmission is not enabled.
  1235. //
  1236. return(false);
  1237. }
  1238. //
  1239. // Automatic data retransmission is enabled.
  1240. //
  1241. return(true);
  1242. }
  1243. //*****************************************************************************
  1244. //
  1245. //! Reads one of the controller status registers.
  1246. //!
  1247. //! \param ulBase is the base address of the CAN controller.
  1248. //! \param eStatusReg is the status register to read.
  1249. //!
  1250. //! Reads a status register of the CAN controller and returns it 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 will be cleared. This should be used in the interrupt handler
  1260. //! for the CAN controller if the cause is a status interrupt. The controller
  1261. //! 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 are 32-bit bit maps to the message objects.
  1282. //! They can be used to quickly obtain information about the status of all the
  1283. //! message objects without needing to query each one. They contain the
  1284. //! following information:
  1285. //!
  1286. //! - \b CAN_STS_TXREQUEST - if a message object's TxRequest bit is set, that
  1287. //! means that a transmission is pending on that object. The application can
  1288. //! use this to determine which objects are still waiting to send a message.
  1289. //! - \b CAN_STS_NEWDAT - if a message object's NewDat bit is set, that means
  1290. //! that a new message has been received in that object, and has not yet been
  1291. //! picked up by the host application
  1292. //! - \b CAN_STS_MSGVAL - if a message object's MsgVal bit is set, that means
  1293. //! it has a valid configuration programmed. The host application can use this
  1294. //! to determine which message objects are empty/unused.
  1295. //!
  1296. //! \return Returns the value of the status register.
  1297. //
  1298. //*****************************************************************************
  1299. unsigned long
  1300. CANStatusGet(unsigned long ulBase, tCANStsReg eStatusReg)
  1301. {
  1302. unsigned long ulStatus;
  1303. //
  1304. // Check the arguments.
  1305. //
  1306. ASSERT(CANBaseValid(ulBase));
  1307. switch(eStatusReg)
  1308. {
  1309. //
  1310. // Just return the global CAN status register since that is what was
  1311. // requested.
  1312. //
  1313. case CAN_STS_CONTROL:
  1314. {
  1315. ulStatus = CANRegRead(ulBase + CAN_O_STS);
  1316. CANRegWrite(ulBase + CAN_O_STS,
  1317. ~(CAN_STS_RXOK | CAN_STS_TXOK | CAN_STS_LEC_M));
  1318. break;
  1319. }
  1320. //
  1321. // Combine the Transmit status bits into one 32bit value.
  1322. //
  1323. case CAN_STS_TXREQUEST:
  1324. {
  1325. ulStatus = CANRegRead(ulBase + CAN_O_TXRQ1);
  1326. ulStatus |= CANRegRead(ulBase + CAN_O_TXRQ2) << 16;
  1327. break;
  1328. }
  1329. //
  1330. // Combine the New Data status bits into one 32bit value.
  1331. //
  1332. case CAN_STS_NEWDAT:
  1333. {
  1334. ulStatus = CANRegRead(ulBase + CAN_O_NWDA1);
  1335. ulStatus |= CANRegRead(ulBase + CAN_O_NWDA2) << 16;
  1336. break;
  1337. }
  1338. //
  1339. // Combine the Message valid status bits into one 32bit value.
  1340. //
  1341. case CAN_STS_MSGVAL:
  1342. {
  1343. ulStatus = CANRegRead(ulBase + CAN_O_MSG1VAL);
  1344. ulStatus |= CANRegRead(ulBase + CAN_O_MSG2VAL) << 16;
  1345. break;
  1346. }
  1347. //
  1348. // Unknown CAN status requested so return 0.
  1349. //
  1350. default:
  1351. {
  1352. ulStatus = 0;
  1353. break;
  1354. }
  1355. }
  1356. return(ulStatus);
  1357. }
  1358. //*****************************************************************************
  1359. //
  1360. //! Reads the CAN controller error counter register.
  1361. //!
  1362. //! \param ulBase is the base address of the CAN controller.
  1363. //! \param pulRxCount is a pointer to storage for the receive error counter.
  1364. //! \param pulTxCount is a pointer to storage for the transmit error counter.
  1365. //!
  1366. //! Reads the error counter register and returns the transmit and receive error
  1367. //! counts to the caller along with a flag indicating if the controller receive
  1368. //! counter has reached the error passive limit. The values of the receive and
  1369. //! transmit error counters are returned through the pointers provided as
  1370. //! parameters.
  1371. //!
  1372. //! After this call, \e *pulRxCount will hold the current receive error count
  1373. //! and \e *pulTxCount will hold the current transmit error count.
  1374. //!
  1375. //! \return Returns \b true if the receive error count has reached the error
  1376. //! passive limit, and \b false if the error count is below the error passive
  1377. //! limit.
  1378. //
  1379. //*****************************************************************************
  1380. tBoolean
  1381. CANErrCntrGet(unsigned long ulBase, unsigned long *pulRxCount,
  1382. unsigned long *pulTxCount)
  1383. {
  1384. unsigned long ulCANError;
  1385. //
  1386. // Check the arguments.
  1387. //
  1388. ASSERT(CANBaseValid(ulBase));
  1389. //
  1390. // Read the current count of transmit/receive errors.
  1391. //
  1392. ulCANError = CANRegRead(ulBase + CAN_O_ERR);
  1393. //
  1394. // Extract the error numbers from the register value.
  1395. //
  1396. *pulRxCount = (ulCANError & CAN_ERR_REC_M) >> CAN_ERR_REC_S;
  1397. *pulTxCount = (ulCANError & CAN_ERR_TEC_M) >> CAN_ERR_TEC_S;
  1398. if(ulCANError & CAN_ERR_RP)
  1399. {
  1400. return(true);
  1401. }
  1402. return(false);
  1403. }
  1404. //*****************************************************************************
  1405. //
  1406. //! Configures a message object in the CAN controller.
  1407. //!
  1408. //! \param ulBase is the base address of the CAN controller.
  1409. //! \param ulObjID is the object number to configure (1-32).
  1410. //! \param pMsgObject is a pointer to a structure containing message object
  1411. //! settings.
  1412. //! \param eMsgType indicates the type of message for this object.
  1413. //!
  1414. //! This function is used to configure any one of the 32 message objects in the
  1415. //! CAN controller. A message object can be configured as any type of CAN
  1416. //! message object as well as several options for automatic transmission and
  1417. //! reception. This call also allows the message object to be configured to
  1418. //! generate interrupts on completion of message receipt or transmission. The
  1419. //! message object can also be configured with a filter/mask so that actions
  1420. //! are only taken when a message that meets certain parameters is seen on the
  1421. //! CAN bus.
  1422. //!
  1423. //! The \e eMsgType parameter must be one of the following values:
  1424. //!
  1425. //! - \b MSG_OBJ_TYPE_TX - CAN transmit message object.
  1426. //! - \b MSG_OBJ_TYPE_TX_REMOTE - CAN transmit remote request message object.
  1427. //! - \b MSG_OBJ_TYPE_RX - CAN receive message object.
  1428. //! - \b MSG_OBJ_TYPE_RX_REMOTE - CAN receive remote request message object.
  1429. //! - \b MSG_OBJ_TYPE_RXTX_REMOTE - CAN remote frame receive remote, then
  1430. //! transmit message object.
  1431. //!
  1432. //! The message object pointed to by \e pMsgObject must be populated by the
  1433. //! caller, as follows:
  1434. //!
  1435. //! - \e ulMsgID - contains the message ID, either 11 or 29 bits.
  1436. //! - \e ulMsgIDMask - mask of bits from \e ulMsgID that must match if
  1437. //! identifier filtering is enabled.
  1438. //! - \e ulFlags
  1439. //! - Set \b MSG_OBJ_TX_INT_ENABLE flag to enable interrupt on transmission.
  1440. //! - Set \b MSG_OBJ_RX_INT_ENABLE flag to enable interrupt on receipt.
  1441. //! - Set \b MSG_OBJ_USE_ID_FILTER flag to enable filtering based on the
  1442. //! identifier mask specified by \e ulMsgIDMask.
  1443. //! - \e ulMsgLen - the number of bytes in the message data. This should be
  1444. //! non-zero even for a remote frame; it should match the expected bytes of the
  1445. //! data responding data frame.
  1446. //! - \e pucMsgData - points to a buffer containing up to 8 bytes of data for a
  1447. //! data frame.
  1448. //!
  1449. //! \b Example: To send a data frame or remote frame(in response to a remote
  1450. //! request), take the following steps:
  1451. //!
  1452. //! -# Set \e eMsgType to \b MSG_OBJ_TYPE_TX.
  1453. //! -# Set \e pMsgObject->ulMsgID to the message ID.
  1454. //! -# Set \e pMsgObject->ulFlags. Make sure to set \b MSG_OBJ_TX_INT_ENABLE to
  1455. //! allow an interrupt to be generated when the message is sent.
  1456. //! -# Set \e pMsgObject->ulMsgLen to the number of bytes in the data frame.
  1457. //! -# Set \e pMsgObject->pucMsgData to point to an array containing the bytes
  1458. //! to send in the message.
  1459. //! -# Call this function with \e ulObjID set to one of the 32 object buffers.
  1460. //!
  1461. //! \b Example: To receive a specific data frame, take the following steps:
  1462. //!
  1463. //! -# Set \e eMsgObjType to \b MSG_OBJ_TYPE_RX.
  1464. //! -# Set \e pMsgObject->ulMsgID to the full message ID, or a partial mask to
  1465. //! use partial ID matching.
  1466. //! -# Set \e pMsgObject->ulMsgIDMask bits that should be used for masking
  1467. //! during comparison.
  1468. //! -# Set \e pMsgObject->ulFlags as follows:
  1469. //! - Set \b MSG_OBJ_RX_INT_ENABLE flag to be interrupted when the data frame
  1470. //! is received.
  1471. //! - Set \b MSG_OBJ_USE_ID_FILTER flag to enable identifier based filtering.
  1472. //! -# Set \e pMsgObject->ulMsgLen to the number of bytes in the expected data
  1473. //! frame.
  1474. //! -# The buffer pointed to by \e pMsgObject->pucMsgData is not used by this
  1475. //! call as no data is present at the time of the call.
  1476. //! -# Call this function with \e ulObjID set to one of the 32 object buffers.
  1477. //!
  1478. //! If you specify a message object buffer that already contains a message
  1479. //! definition, it will be overwritten.
  1480. //!
  1481. //! \return None.
  1482. //
  1483. //*****************************************************************************
  1484. void
  1485. CANMessageSet(unsigned long ulBase, unsigned long ulObjID,
  1486. tCANMsgObject *pMsgObject, tMsgObjType eMsgType)
  1487. {
  1488. unsigned short usCmdMaskReg;
  1489. unsigned short usMaskReg0, usMaskReg1;
  1490. unsigned short usArbReg0, usArbReg1;
  1491. unsigned short usMsgCtrl;
  1492. tBoolean bTransferData;
  1493. tBoolean bUseExtendedID;
  1494. bTransferData = 0;
  1495. //
  1496. // Check the arguments.
  1497. //
  1498. ASSERT(CANBaseValid(ulBase));
  1499. ASSERT((ulObjID <= 32) && (ulObjID != 0));
  1500. ASSERT((eMsgType == MSG_OBJ_TYPE_TX) ||
  1501. (eMsgType == MSG_OBJ_TYPE_TX_REMOTE) ||
  1502. (eMsgType == MSG_OBJ_TYPE_RX) ||
  1503. (eMsgType == MSG_OBJ_TYPE_RX_REMOTE) ||
  1504. (eMsgType == MSG_OBJ_TYPE_TX_REMOTE) ||
  1505. (eMsgType == MSG_OBJ_TYPE_RXTX_REMOTE));
  1506. //
  1507. // Wait for busy bit to clear
  1508. //
  1509. while(CANRegRead(ulBase + CAN_O_IF1CRQ) & CAN_IF1CRQ_BUSY)
  1510. {
  1511. }
  1512. //
  1513. // See if we need to use an extended identifier or not.
  1514. //
  1515. if((pMsgObject->ulMsgID > CAN_MAX_11BIT_MSG_ID) ||
  1516. (pMsgObject->ulFlags & MSG_OBJ_EXTENDED_ID))
  1517. {
  1518. bUseExtendedID = 1;
  1519. }
  1520. else
  1521. {
  1522. bUseExtendedID = 0;
  1523. }
  1524. //
  1525. // This is always a write to the Message object as this call is setting a
  1526. // message object. This call will also always set all size bits so it sets
  1527. // both data bits. The call will use the CONTROL register to set control
  1528. // bits so this bit needs to be set as well.
  1529. //
  1530. usCmdMaskReg = (CAN_IF1CMSK_WRNRD | CAN_IF1CMSK_DATAA | CAN_IF1CMSK_DATAB |
  1531. CAN_IF1CMSK_CONTROL);
  1532. //
  1533. // Initialize the values to a known state before filling them in based on
  1534. // the type of message object that is being configured.
  1535. //
  1536. usArbReg0 = 0;
  1537. usArbReg1 = 0;
  1538. usMsgCtrl = 0;
  1539. usMaskReg0 = 0;
  1540. usMaskReg1 = 0;
  1541. switch(eMsgType)
  1542. {
  1543. //
  1544. // Transmit message object.
  1545. //
  1546. case MSG_OBJ_TYPE_TX:
  1547. {
  1548. //
  1549. // Set the TXRQST bit and the reset the rest of the register.
  1550. //
  1551. usMsgCtrl |= CAN_IF1MCTL_TXRQST;
  1552. usArbReg1 = CAN_IF1ARB2_DIR;
  1553. bTransferData = 1;
  1554. break;
  1555. }
  1556. //
  1557. // Transmit remote request message object
  1558. //
  1559. case MSG_OBJ_TYPE_TX_REMOTE:
  1560. {
  1561. //
  1562. // Set the TXRQST bit and the reset the rest of the register.
  1563. //
  1564. usMsgCtrl |= CAN_IF1MCTL_TXRQST;
  1565. usArbReg1 = 0;
  1566. break;
  1567. }
  1568. //
  1569. // Receive message object.
  1570. //
  1571. case MSG_OBJ_TYPE_RX:
  1572. {
  1573. //
  1574. // This clears the DIR bit along with everything else. The TXRQST
  1575. // bit was cleared by defaulting usMsgCtrl to 0.
  1576. //
  1577. usArbReg1 = 0;
  1578. break;
  1579. }
  1580. //
  1581. // Receive remote request message object.
  1582. //
  1583. case MSG_OBJ_TYPE_RX_REMOTE:
  1584. {
  1585. //
  1586. // The DIR bit is set to one for remote receivers. The TXRQST bit
  1587. // was cleared by defaulting usMsgCtrl to 0.
  1588. //
  1589. usArbReg1 = CAN_IF1ARB2_DIR;
  1590. //
  1591. // Set this object so that it only indicates that a remote frame
  1592. // was received and allow for software to handle it by sending back
  1593. // a data frame.
  1594. //
  1595. usMsgCtrl = CAN_IF1MCTL_UMASK;
  1596. //
  1597. // Use the full Identifier by default.
  1598. //
  1599. usMaskReg0 = 0xffff;
  1600. usMaskReg1 = 0x1fff;
  1601. //
  1602. // Make sure to send the mask to the message object.
  1603. //
  1604. usCmdMaskReg |= CAN_IF1CMSK_MASK;
  1605. break;
  1606. }
  1607. //
  1608. // Remote frame receive remote, with auto-transmit message object.
  1609. //
  1610. case MSG_OBJ_TYPE_RXTX_REMOTE:
  1611. {
  1612. //
  1613. // Oddly the DIR bit is set to one for remote receivers.
  1614. //
  1615. usArbReg1 = CAN_IF1ARB2_DIR;
  1616. //
  1617. // Set this object to auto answer if a matching identifier is seen.
  1618. //
  1619. usMsgCtrl = CAN_IF1MCTL_RMTEN | CAN_IF1MCTL_UMASK;
  1620. //
  1621. // The data to be returned needs to be filled in.
  1622. //
  1623. bTransferData = 1;
  1624. break;
  1625. }
  1626. //
  1627. // This case should never happen due to the ASSERT statement at the
  1628. // beginning of this function.
  1629. //
  1630. default:
  1631. {
  1632. return;
  1633. }
  1634. }
  1635. //
  1636. // Configure the Mask Registers.
  1637. //
  1638. if(pMsgObject->ulFlags & MSG_OBJ_USE_ID_FILTER)
  1639. {
  1640. if(bUseExtendedID)
  1641. {
  1642. //
  1643. // Set the 29 bits of Identifier mask that were requested.
  1644. //
  1645. usMaskReg0 = pMsgObject->ulMsgIDMask & CAN_IF1MSK1_IDMSK_M;
  1646. usMaskReg1 = ((pMsgObject->ulMsgIDMask >> 16) &
  1647. CAN_IF1MSK2_IDMSK_M);
  1648. }
  1649. else
  1650. {
  1651. //
  1652. // Lower 16 bit are unused so set them to zero.
  1653. //
  1654. usMaskReg0 = 0;
  1655. //
  1656. // Put the 11 bit Mask Identifier into the upper bits of the field
  1657. // in the register.
  1658. //
  1659. usMaskReg1 = ((pMsgObject->ulMsgIDMask << 2) &
  1660. CAN_IF1MSK2_IDMSK_M);
  1661. }
  1662. }
  1663. //
  1664. // If the caller wants to filter on the extended ID bit then set it.
  1665. //
  1666. if((pMsgObject->ulFlags & MSG_OBJ_USE_EXT_FILTER) ==
  1667. MSG_OBJ_USE_EXT_FILTER)
  1668. {
  1669. usMaskReg1 |= CAN_IF1MSK2_MXTD;
  1670. }
  1671. //
  1672. // The caller wants to filter on the message direction field.
  1673. //
  1674. if((pMsgObject->ulFlags & MSG_OBJ_USE_DIR_FILTER) ==
  1675. MSG_OBJ_USE_DIR_FILTER)
  1676. {
  1677. usMaskReg1 |= CAN_IF1MSK2_MDIR;
  1678. }
  1679. if(pMsgObject->ulFlags & (MSG_OBJ_USE_ID_FILTER | MSG_OBJ_USE_DIR_FILTER |
  1680. MSG_OBJ_USE_EXT_FILTER))
  1681. {
  1682. //
  1683. // Set the UMASK bit to enable using the mask register.
  1684. //
  1685. usMsgCtrl |= CAN_IF1MCTL_UMASK;
  1686. //
  1687. // Set the MASK bit so that this gets transferred to the Message Object.
  1688. //
  1689. usCmdMaskReg |= CAN_IF1CMSK_MASK;
  1690. }
  1691. //
  1692. // Set the Arb bit so that this gets transferred to the Message object.
  1693. //
  1694. usCmdMaskReg |= CAN_IF1CMSK_ARB;
  1695. //
  1696. // Configure the Arbitration registers.
  1697. //
  1698. if(bUseExtendedID)
  1699. {
  1700. //
  1701. // Set the 29 bit version of the Identifier for this message object.
  1702. //
  1703. usArbReg0 |= pMsgObject->ulMsgID & CAN_IF1ARB1_ID_M;
  1704. usArbReg1 |= (pMsgObject->ulMsgID >> 16) & CAN_IF1ARB2_ID_M;
  1705. //
  1706. // Mark the message as valid and set the extended ID bit.
  1707. //
  1708. usArbReg1 |= CAN_IF1ARB2_MSGVAL | CAN_IF1ARB2_XTD;
  1709. }
  1710. else
  1711. {
  1712. //
  1713. // Set the 11 bit version of the Identifier for this message object.
  1714. // The lower 18 bits are set to zero.
  1715. //
  1716. usArbReg1 |= (pMsgObject->ulMsgID << 2) & CAN_IF1ARB2_ID_M;
  1717. //
  1718. // Mark the message as valid.
  1719. //
  1720. usArbReg1 |= CAN_IF1ARB2_MSGVAL;
  1721. }
  1722. //
  1723. // Set the data length since this is set for all transfers. This is also a
  1724. // single transfer and not a FIFO transfer so set EOB bit.
  1725. //
  1726. usMsgCtrl |= (pMsgObject->ulMsgLen & CAN_IF1MCTL_DLC_M);
  1727. //
  1728. // Mark this as the last entry if this is not the last entry in a FIFO.
  1729. //
  1730. if((pMsgObject->ulFlags & MSG_OBJ_FIFO) == 0)
  1731. {
  1732. usMsgCtrl |= CAN_IF1MCTL_EOB;
  1733. }
  1734. //
  1735. // Enable transmit interrupts if they should be enabled.
  1736. //
  1737. if(pMsgObject->ulFlags & MSG_OBJ_TX_INT_ENABLE)
  1738. {
  1739. usMsgCtrl |= CAN_IF1MCTL_TXIE;
  1740. }
  1741. //
  1742. // Enable receive interrupts if they should be enabled.
  1743. //
  1744. if(pMsgObject->ulFlags & MSG_OBJ_RX_INT_ENABLE)
  1745. {
  1746. usMsgCtrl |= CAN_IF1MCTL_RXIE;
  1747. }
  1748. //
  1749. // Write the data out to the CAN Data registers if needed.
  1750. //
  1751. if(bTransferData)
  1752. {
  1753. CANDataRegWrite(pMsgObject->pucMsgData,
  1754. (unsigned long *)(ulBase + CAN_O_IF1DA1),
  1755. pMsgObject->ulMsgLen);
  1756. }
  1757. //
  1758. // Write out the registers to program the message object.
  1759. //
  1760. CANRegWrite(ulBase + CAN_O_IF1CMSK, usCmdMaskReg);
  1761. CANRegWrite(ulBase + CAN_O_IF1MSK1, usMaskReg0);
  1762. CANRegWrite(ulBase + CAN_O_IF1MSK2, usMaskReg1);
  1763. CANRegWrite(ulBase + CAN_O_IF1ARB1, usArbReg0);
  1764. CANRegWrite(ulBase + CAN_O_IF1ARB2, usArbReg1);
  1765. CANRegWrite(ulBase + CAN_O_IF1MCTL, usMsgCtrl);
  1766. //
  1767. // Transfer the message object to the message object specifiec by ulObjID.
  1768. //
  1769. CANRegWrite(ulBase + CAN_O_IF1CRQ, ulObjID & CAN_IF1CRQ_MNUM_M);
  1770. return;
  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 is used to read a message object that has received and stored
  1789. //! a CAN message with a certain identifier. However, this could also be used
  1790. //! to read the contents of a message object in order to load the fields of the
  1791. //! structure in case only part of the structure needs to be changed from a
  1792. //! 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 is new data 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 will no longer automatically send or
  2019. //! receive messages, or 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. //*****************************************************************************