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