1 /******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
2 * File Name : 75x_can.c
3 * Author : MCD Application Team
4 * Date First Issued : 03/10/2006
5 * Description : This file provides all the CAN software functions.
6 ********************************************************************************
10 ********************************************************************************
11 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
13 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
14 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
15 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
16 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *******************************************************************************/
19 /* Includes ------------------------------------------------------------------*/
23 /* Private typedef -----------------------------------------------------------*/
24 /* Private define ------------------------------------------------------------*/
25 /* Private macro -------------------------------------------------------------*/
26 /*----------------------------------------------------------------------------*/
27 /* Macro Name : xxx_ID_MSK, xxx_ID_ARB */
28 /* Description : Form the Mask and Arbitration registers value to filter */
29 /* a range of identifiers or a fixed identifier, for standard*/
30 /* and extended IDs */
31 /*----------------------------------------------------------------------------*/
32 #define RANGE_ID_MSK(range_start, range_end) (~((range_end) - (range_start)))
33 #define RANGE_ID_ARB(range_start, range_end) ((range_start) & (range_end))
35 #define FIXED_ID_MSK(id) RANGE_ID_MSK((id), (id))
36 #define FIXED_ID_ARB(id) RANGE_ID_ARB((id), (id))
38 #define STD_RANGE_ID_MSK(range_start, range_end) ((u16)((RANGE_ID_MSK((range_start), (range_end)) & 0x7FF) << 2))
39 #define STD_RANGE_ID_ARB(range_start, range_end) ((u16)(RANGE_ID_ARB((range_start), (range_end)) << 2))
41 #define STD_FIXED_ID_MSK(id) ((u16)((FIXED_ID_MSK(id) & 0x7FF) << 2))
42 #define STD_FIXED_ID_ARB(id) ((u16)(FIXED_ID_ARB(id) << 2))
44 #define EXT_RANGE_ID_MSK_L(range_start, range_end) ((u16)(RANGE_ID_MSK((range_start), (range_end)) >> 11))
45 #define EXT_RANGE_ID_MSK_H(range_start, range_end) ((u16)(STD_RANGE_ID_MSK((range_start), (range_end)) | ((RANGE_ID_MSK((range_start), (range_end)) >> 27) & 0x03)))
46 #define EXT_RANGE_ID_ARB_L(range_start, range_end) ((u16)(RANGE_ID_ARB((range_start), (range_end)) >> 11))
47 #define EXT_RANGE_ID_ARB_H(range_start, range_end) ((u16)(STD_RANGE_ID_ARB((range_start), (range_end)) | ((RANGE_ID_ARB((range_start), (range_end)) >> 27) & 0x03)))
49 #define EXT_FIXED_ID_MSK_L(id) ((u16)(FIXED_ID_MSK(id) >> 11))
50 #define EXT_FIXED_ID_MSK_H(id) ((u16)(STD_FIXED_ID_MSK(id) | ((FIXED_ID_MSK(id) >> 27) & 0x03)))
51 #define EXT_FIXED_ID_ARB_L(id) ((u16)(FIXED_ID_ARB(id) >> 11))
52 #define EXT_FIXED_ID_ARB_H(id) ((u16)(STD_FIXED_ID_ARB(id) | ((FIXED_ID_ARB(id) >> 27) & 0x03)))
54 /* macro to format the timing register value from the timing parameters*/
55 #define CAN_TIMING(tseg1, tseg2, sjw, brp) ((((tseg2-1) & 0x07) << 12) | (((tseg1-1) & 0x0F) << 8) | (((sjw-1) & 0x03) << 6) | ((brp-1) & 0x3F))
57 /* Private variables ---------------------------------------------------------*/
58 /* array of pre-defined timing parameters for standard bitrates*/
59 u16 CanTimings[] = { /* value bitrate NTQ TSEG1 TSEG2 SJW BRP */
60 CAN_TIMING(11, 4, 4, 5), /* 0x3AC4 100 kbit/s 16 11 4 4 5 */
61 CAN_TIMING(11, 4, 4, 4), /* 0x3AC3 125 kbit/s 16 11 4 4 4 */
62 CAN_TIMING( 4, 3, 3, 4), /* 0x2383 250 kbit/s 8 4 3 3 4 */
63 CAN_TIMING(13, 2, 1, 1), /* 0x1C00 500 kbit/s 16 13 2 1 1 */
64 CAN_TIMING( 4, 3, 1, 1), /* 0x2300 1 Mbit/s 8 4 3 1 1 */
67 /* Private function prototypes -----------------------------------------------*/
68 static u32 GetFreeIF(void);
69 /* Private functions ---------------------------------------------------------*/
71 /*******************************************************************************
72 * Function Name : CAN_DeInit
73 * Description : Deinitializes the CAN peripheral registers to their default
78 *******************************************************************************/
79 void CAN_DeInit (void)
81 /* Reset the CAN registers values*/
82 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_CAN,ENABLE);
83 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_CAN,DISABLE);
86 /*******************************************************************************
87 * Function Name : CAN_Init
88 * Description : Initializes the CAN peripheral according to the specified
89 * parameters in the CAN_InitStruct.
90 * Input : CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
91 * contains the configuration information for the CAN peripheral.
94 *******************************************************************************/
95 void CAN_Init(CAN_InitTypeDef* CAN_InitStruct)
97 CAN_EnterInitMode(CAN_CR_CCE | CAN_InitStruct->CAN_ConfigParameters);
98 CAN_SetBitrate(CAN_InitStruct->CAN_Bitrate);
103 /*******************************************************************************
104 * Function Name : CAN_StructInit
105 * Description : Fills each CAN_InitStruct member with its reset value.
106 * Input : CAN_InitStruct : pointer to a CAN_InitTypeDef structure which
107 * will be initialized.
110 *******************************************************************************/
111 void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
113 /* Reset CAN init structure parameters values */
114 CAN_InitStruct->CAN_ConfigParameters = 0x0;
115 CAN_InitStruct->CAN_Bitrate = 0x2301;
118 /*******************************************************************************
119 * Function Name : CAN_SetBitrate
120 * Description : Setups a standard CAN bitrate.
121 * Input : bitrate: specifies the bit rate.
124 *******************************************************************************/
125 void CAN_SetBitrate(u32 bitrate)
127 CAN->BTR = CanTimings[bitrate]; /* write the predefined timing value */
128 CAN->BRPR = 0; /* clear the Extended Baud Rate Prescaler */
131 /*******************************************************************************
132 * Function Name : CAN_SetTiming
133 * Description : Setups the CAN timing with specific parameters
134 * Input : - tseg1: specifies Time Segment before the sample point.
135 * This parameter must be a number between 1 and 16.
136 * - tseg2: Time Segment after the sample point. This parameter
137 * must be a number between 1 and 8.
138 * - sjw: Synchronisation Jump Width. This parameter must be
139 * a number between 1 and 4.
140 * - brp: Baud Rate Prescaler. This parameter must be a number
141 * between 1 and 1024.
144 *******************************************************************************/
145 void CAN_SetTiming(u32 tseg1, u32 tseg2, u32 sjw, u32 brp)
147 CAN->BTR = CAN_TIMING(tseg1, tseg2, sjw, brp);
148 CAN->BRPR = ((brp-1) >> 6) & 0x0F;
151 /*******************************************************************************
152 * Function Name : GetFreeIF
153 * Description : Searchs the first free message interface, starting from 0.
156 * Return : A free message interface number (0 or 1) if found, else 2
157 *******************************************************************************/
158 static u32 GetFreeIF(void)
160 if ((CAN->sMsgObj[0].CRR & CAN_CRR_BUSY) == 0)
162 else if ((CAN->sMsgObj[1].CRR & CAN_CRR_BUSY) == 0)
168 /*******************************************************************************
169 * Function Name : CAN_SetUnusedMsgObj
170 * Description : Configures the message object as unused
171 * Input : msgobj: specifies the Message object number, from 0 to 31.
173 * Return : An ErrorStatus enumuration value:
174 * - SUCCESS: Interface to treat the message
175 * - ERROR: No interface found to treat the message
176 *******************************************************************************/
177 ErrorStatus CAN_SetUnusedMsgObj(u32 msgobj)
181 if ((msg_if = GetFreeIF()) == 2)
186 CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
193 CAN->sMsgObj[msg_if].M1R = 0;
194 CAN->sMsgObj[msg_if].M2R = 0;
196 CAN->sMsgObj[msg_if].A1R = 0;
197 CAN->sMsgObj[msg_if].A2R = 0;
199 CAN->sMsgObj[msg_if].MCR = 0;
201 CAN->sMsgObj[msg_if].DA1R = 0;
202 CAN->sMsgObj[msg_if].DA2R = 0;
203 CAN->sMsgObj[msg_if].DB1R = 0;
204 CAN->sMsgObj[msg_if].DB2R = 0;
206 CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
211 /*******************************************************************************
212 * Function Name : CAN_SetTxMsgObj
213 * Description : Configures the message object as TX.
214 * Input : - msgobj: specifies the Message object number, from 0 to 31.
215 * - idType: specifies the identifier type of the frames that
216 * will be transmitted using this message object.
217 * This parameter can be one of the following values:
218 * - CAN_STD_ID (standard ID, 11-bit)
219 * - CAN_EXT_ID (extended ID, 29-bit)
221 * Return : An ErrorStatus enumuration value:
222 * - SUCCESS: Interface to treat the message
223 * - ERROR: No interface found to treat the message
224 *******************************************************************************/
225 ErrorStatus CAN_SetTxMsgObj(u32 msgobj, u32 idType)
229 if ((msg_if = GetFreeIF()) == 2)
234 CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
241 CAN->sMsgObj[msg_if].M1R = 0;
242 CAN->sMsgObj[msg_if].A1R = 0;
244 if (idType == CAN_STD_ID)
246 CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR;
247 CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR;
251 CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR | CAN_M2R_MXTD;
252 CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR | CAN_A2R_XTD;
255 CAN->sMsgObj[msg_if].MCR = CAN_MCR_TXIE | CAN_MCR_EOB;
257 CAN->sMsgObj[msg_if].DA1R = 0;
258 CAN->sMsgObj[msg_if].DA2R = 0;
259 CAN->sMsgObj[msg_if].DB1R = 0;
260 CAN->sMsgObj[msg_if].DB2R = 0;
262 CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
267 /*******************************************************************************
268 * Function Name : CAN_SetRxMsgObj
269 * Description : Configures the message object as RX.
270 * Input : - msgobj: specifies the Message object number, from 0 to 31.
271 * - idType: specifies the identifier type of the frames that
272 * will be transmitted using this message object.
273 * This parameter can be one of the following values:
274 * - CAN_STD_ID (standard ID, 11-bit)
275 * - CAN_EXT_ID (extended ID, 29-bit)
276 * - idLow: specifies the low part of the identifier range used
277 * for acceptance filtering.
278 * - idHigh: specifies the high part of the identifier range
279 * used for acceptance filtering.
280 * - singleOrFifoLast: specifies the end-of-buffer indicator.
281 * This parameter can be one of the following values:
282 * - TRUE: for a single receive object or a FIFO receive
283 * object that is the last one of the FIFO.
284 * - FALSE: for a FIFO receive object that is not the
287 * Return : An ErrorStatus enumuration value:
288 * - SUCCESS: Interface to treat the message
289 * - ERROR: No interface found to treat the message
290 *******************************************************************************/
291 ErrorStatus CAN_SetRxMsgObj(u32 msgobj, u32 idType, u32 idLow, u32 idHigh, bool singleOrFifoLast)
295 if ((msg_if = GetFreeIF()) == 2)
300 CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
307 if (idType == CAN_STD_ID)
309 CAN->sMsgObj[msg_if].M1R = 0;
310 CAN->sMsgObj[msg_if].M2R = STD_RANGE_ID_MSK(idLow, idHigh);
312 CAN->sMsgObj[msg_if].A1R = 0;
313 CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | STD_RANGE_ID_ARB(idLow, idHigh);
317 CAN->sMsgObj[msg_if].M1R = EXT_RANGE_ID_MSK_L(idLow, idHigh);
318 CAN->sMsgObj[msg_if].M2R = CAN_M2R_MXTD | EXT_RANGE_ID_MSK_H(idLow, idHigh);
320 CAN->sMsgObj[msg_if].A1R = EXT_RANGE_ID_ARB_L(idLow, idHigh);
321 CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_XTD | EXT_RANGE_ID_ARB_H(idLow, idHigh);
324 CAN->sMsgObj[msg_if].MCR = CAN_MCR_RXIE | CAN_MCR_UMASK | (singleOrFifoLast ? CAN_MCR_EOB : 0);
326 CAN->sMsgObj[msg_if].DA1R = 0;
327 CAN->sMsgObj[msg_if].DA2R = 0;
328 CAN->sMsgObj[msg_if].DB1R = 0;
329 CAN->sMsgObj[msg_if].DB2R = 0;
331 CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
336 /*******************************************************************************
337 * Function Name : CAN_InvalidateAllMsgObj
338 * Description : Configures all the message objects as unused.
342 *******************************************************************************/
343 void CAN_InvalidateAllMsgObj(void)
346 for (i = 0; i < 32; i++)
347 CAN_SetUnusedMsgObj(i);
351 /*******************************************************************************
352 * Function Name : CAN_ReleaseMessage
353 * Description : Releases the message object
354 * Input : - msgobj: specifies the Message object number, from 0 to 31.
356 * Return : An ErrorStatus enumuration value:
357 * - SUCCESS: Interface to treat the message
358 * - ERROR: No interface found to treat the message
359 *******************************************************************************/
360 ErrorStatus CAN_ReleaseMessage(u32 msgobj)
364 if ((msg_if = GetFreeIF()) == 2)
369 CAN->sMsgObj[msg_if].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
370 CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
375 /*******************************************************************************
376 * Function Name : CAN_SendMessage
377 * Description : Start transmission of a message
378 * Input : - msgobj: specifies the Message object number, from 0 to 31.
379 * : - pCanMsg: pointer to the message structure containing data
382 * Return : An ErrorStatus enumuration value:
383 * - SUCCESS: Transmission OK
384 * - ERROR: No transmission
385 *******************************************************************************/
386 ErrorStatus CAN_SendMessage(u32 msgobj, canmsg* pCanMsg)
388 if (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
393 CAN->SR &= ~CAN_SR_TXOK;
395 /* read the Arbitration and Message Control*/
396 CAN->sMsgObj[0].CMR = CAN_CMR_ARB | CAN_CMR_CONTROL;
398 CAN->sMsgObj[0].CRR = 1 + msgobj;
400 if (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
405 /* update the contents needed for transmission*/
406 CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
412 if ((CAN->sMsgObj[0].A2R & CAN_A2R_XTD) == 0)
415 CAN->sMsgObj[0].A1R = 0;
416 CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
421 CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
422 CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
425 CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFEF0) | CAN_MCR_NEWDAT | CAN_MCR_TXRQST | pCanMsg->Dlc;
427 CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1]<<8) | pCanMsg->Data[0];
428 CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3]<<8) | pCanMsg->Data[2];
429 CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5]<<8) | pCanMsg->Data[4];
430 CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7]<<8) | pCanMsg->Data[6];
432 CAN->sMsgObj[0].CRR = 1 + msgobj;
437 /*******************************************************************************
438 * Function Name : CAN_ReceiveMessage
439 * Description : Gets the message, if received.
440 * Input : - msgobj: specifies the Message object number, from 0 to 31.
441 * - release: specifies the message release indicator.
442 * This parameter can be one of the following values:
443 * - TRUE: the message object is released when getting
445 * - FALSE: the message object is not released.
446 * - pCanMsg: pointer to the message structure where received
449 * Return : An ErrorStatus enumuration value:
450 * - SUCCESS: Reception OK
451 * - ERROR: No message pending
452 *******************************************************************************/
453 ErrorStatus CAN_ReceiveMessage(u32 msgobj, bool release, canmsg* pCanMsg)
455 if (!CAN_IsMessageWaiting(msgobj))
460 CAN->SR &= ~CAN_SR_RXOK;
462 /* read the message contents*/
463 CAN->sMsgObj[1].CMR = CAN_CMR_MASK
467 | (release ? CAN_CMR_TXRQSTNEWDAT : 0)
471 CAN->sMsgObj[1].CRR = 1 + msgobj;
473 if (CAN->sMsgObj[1].CRR & CAN_CRR_BUSY)
478 if ((CAN->sMsgObj[1].A2R & CAN_A2R_XTD) == 0)
481 pCanMsg->IdType = CAN_STD_ID;
482 pCanMsg->Id = (CAN->sMsgObj[1].A2R >> 2) & 0x07FF;
487 pCanMsg->IdType = CAN_EXT_ID;
488 pCanMsg->Id = ((CAN->sMsgObj[1].A2R >> 2) & 0x07FF);
489 pCanMsg->Id |= ((u32)CAN->sMsgObj[1].A1R << 11);
490 pCanMsg->Id |= (((u32)CAN->sMsgObj[1].A2R & 0x0003) << 27);
493 pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;
495 pCanMsg->Data[0] = (u8) CAN->sMsgObj[1].DA1R;
496 pCanMsg->Data[1] = (u8)(CAN->sMsgObj[1].DA1R >> 8);
497 pCanMsg->Data[2] = (u8) CAN->sMsgObj[1].DA2R;
498 pCanMsg->Data[3] = (u8)(CAN->sMsgObj[1].DA2R >> 8);
499 pCanMsg->Data[4] = (u8) CAN->sMsgObj[1].DB1R;
500 pCanMsg->Data[5] = (u8)(CAN->sMsgObj[1].DB1R >> 8);
501 pCanMsg->Data[6] = (u8) CAN->sMsgObj[1].DB2R;
502 pCanMsg->Data[7] = (u8)(CAN->sMsgObj[1].DB2R >> 8);
507 /*******************************************************************************
508 * Function Name : CAN_WaitEndOfTx
509 * Description : Waits until current transmission is finished.
512 * Return : An ErrorStatus enumuration value:
513 * - SUCCESS: Transmission ended
514 * - ERROR: Transmission did not occur yet
515 *******************************************************************************/
516 ErrorStatus CAN_WaitEndOfTx(void)
518 if ((CAN->SR & CAN_SR_TXOK) == 0)
522 CAN->SR &= ~CAN_SR_TXOK;
527 /*******************************************************************************
528 * Function Name : CAN_BasicSendMessage
529 * Description : Starts transmission of a message in BASIC mode. This mode
530 * does not use the message RAM.
531 * Input : pCanMsg: Pointer to the message structure containing data to
534 * Return : An ErrorStatus enumuration value:
535 * - SUCCESS: Transmission OK
536 * - ERROR: No transmission
537 *******************************************************************************/
538 ErrorStatus CAN_BasicSendMessage(canmsg* pCanMsg)
540 /* clear NewDat bit in IF2 to detect next reception*/
541 CAN->sMsgObj[1].MCR &= ~CAN_MCR_NEWDAT;
543 CAN->SR &= ~CAN_SR_TXOK;
544 CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
550 if (pCanMsg->IdType == CAN_STD_ID)
553 CAN->sMsgObj[0].A1R = 0;
554 CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000) | STD_FIXED_ID_ARB(pCanMsg->Id);
559 CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
560 CAN->sMsgObj[0].A2R = ((CAN->sMsgObj[0].A2R) & 0xE000) | EXT_FIXED_ID_ARB_H(pCanMsg->Id);
563 CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFCF0) | pCanMsg->Dlc;
565 CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1]<<8) | pCanMsg->Data[0];
566 CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3]<<8) | pCanMsg->Data[2];
567 CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5]<<8) | pCanMsg->Data[4];
568 CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7]<<8) | pCanMsg->Data[6];
570 /* request transmission*/
571 if (CAN->sMsgObj[0].CRR == CAN_CRR_BUSY )
579 /*******************************************************************************
580 * Function Name : CAN_BasicReceiveMessage
581 * Description : Gets the message in BASIC mode, if received. This mode does
582 * not use the message RAM.
583 * Input : pCanMsg: pointer to the message structure where message is copied.
585 * Return : An ErrorStatus enumuration value:
586 * - SUCCESS: Reception OK
587 * - ERROR: No message pending
588 *******************************************************************************/
589 ErrorStatus CAN_BasicReceiveMessage(canmsg* pCanMsg)
591 if ((CAN->sMsgObj[1].MCR & CAN_MCR_NEWDAT) == 0)
596 CAN->SR &= ~CAN_SR_RXOK;
598 CAN->sMsgObj[1].CMR = CAN_CMR_ARB
603 if ((CAN->sMsgObj[1].A2R & CAN_A2R_XTD) == 0)
606 pCanMsg->IdType = CAN_STD_ID;
607 pCanMsg->Id = (CAN->sMsgObj[1].A2R >> 2) & 0x07FF;
612 pCanMsg->IdType = CAN_EXT_ID;
613 pCanMsg->Id = ((CAN->sMsgObj[1].A2R >> 2) & 0x07FF);
614 pCanMsg->Id |= ((u32)CAN->sMsgObj[1].A1R << 11);
615 pCanMsg->Id |= (((u32)CAN->sMsgObj[1].A2R & 0x0003) << 27);
618 pCanMsg->Dlc = CAN->sMsgObj[1].MCR & 0x0F;
620 pCanMsg->Data[0] = (u8) CAN->sMsgObj[1].DA1R;
621 pCanMsg->Data[1] = (u8)(CAN->sMsgObj[1].DA1R >> 8);
622 pCanMsg->Data[2] = (u8) CAN->sMsgObj[1].DA2R;
623 pCanMsg->Data[3] = (u8)(CAN->sMsgObj[1].DA2R >> 8);
624 pCanMsg->Data[4] = (u8) CAN->sMsgObj[1].DB1R;
625 pCanMsg->Data[5] = (u8)(CAN->sMsgObj[1].DB1R >> 8);
626 pCanMsg->Data[6] = (u8) CAN->sMsgObj[1].DB2R;
627 pCanMsg->Data[7] = (u8)(CAN->sMsgObj[1].DB2R >> 8);
632 /*******************************************************************************
633 * Function Name : CAN_EnterInitMode
634 * Description : Switchs the CAN into initialization mode. This function must
635 * be used in conjunction with CAN_LeaveInitMode().
636 * Input : InitMask: specifies the CAN configuration in normal mode.
639 *******************************************************************************/
640 void CAN_EnterInitMode(u8 InitMask)
642 CAN->CR = InitMask | CAN_CR_INIT;
643 CAN->SR = 0; /* reset the status*/
646 /*******************************************************************************
647 * Function Name : CAN_LeaveInitMode
648 * Description : Leaves the initialization mode (switch into normal mode).
649 * This function must be used in conjunction with CAN_EnterInitMode().
653 *******************************************************************************/
654 void CAN_LeaveInitMode(void)
656 CAN->CR &= ~(CAN_CR_INIT | CAN_CR_CCE);
659 /*******************************************************************************
660 * Function Name : CAN_EnterTestMode
661 * Description : Switchs the CAN into test mode. This function must be used in
662 * conjunction with CAN_LeaveTestMode().
663 * Input : TestMask: specifies the configuration in test modes.
666 *******************************************************************************/
667 void CAN_EnterTestMode(u8 TestMask)
669 CAN->CR |= CAN_CR_TEST;
670 CAN->TESTR |= TestMask;
673 /*******************************************************************************
674 * Function Name : CAN_LeaveTestMode
675 * Description : Leaves the current test mode (switch into normal mode).
676 * This function must be used in conjunction with CAN_EnterTestMode().
680 *******************************************************************************/
681 void CAN_LeaveTestMode(void)
683 CAN->CR |= CAN_CR_TEST;
684 CAN->TESTR &= ~(CAN_TESTR_LBACK | CAN_TESTR_SILENT | CAN_TESTR_BASIC);
685 CAN->CR &= ~CAN_CR_TEST;
688 /*******************************************************************************
689 * Function Name : CAN_ReleaseTxMessage
690 * Description : Releases the transmit message object.
691 * Input : - msgobj: specifies the Message object number, from 0 to 31.
694 *******************************************************************************/
695 void CAN_ReleaseTxMessage(u32 msgobj)
697 CAN->sMsgObj[0].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
698 CAN->sMsgObj[0].CRR = 1 + msgobj;
701 /*******************************************************************************
702 * Function Name : CAN_ReleaseRxMessage
703 * Description : Releases the receive message object.
704 * Input : - msgobj: specifies the Message object number, from 0 to 31.
707 *******************************************************************************/
708 void CAN_ReleaseRxMessage(u32 msgobj)
710 CAN->sMsgObj[1].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQSTNEWDAT;
711 CAN->sMsgObj[1].CRR = 1 + msgobj;
714 /*******************************************************************************
715 * Function Name : CAN_IsMessageWaiting
716 * Description : Tests the waiting status of a received message.
717 * Input : - msgobj: specifies the Message object number, from 0 to 31.
719 * Return : A non-zero value if the corresponding message object has
720 * received a message waiting to be copied, else 0.
721 *******************************************************************************/
722 u32 CAN_IsMessageWaiting(u32 msgobj)
724 return (msgobj < 16 ? CAN->ND1R & (1 << msgobj) : CAN->ND2R & (1 << (msgobj-16)));
727 /*******************************************************************************
728 * Function Name : CAN_IsTransmitRequested
729 * Description : Tests the request status of a transmitted message.
730 * Input : - msgobj: specifies the Message object number, from 0 to 31.
732 * Return : A non-zero value if the corresponding message is requested
733 * to transmit, else 0.
734 *******************************************************************************/
735 u32 CAN_IsTransmitRequested(u32 msgobj)
737 return (msgobj < 16 ? CAN->TXR1R & (1 << msgobj) : CAN->TXR2R & (1 << (msgobj-16)));
740 /*******************************************************************************
741 * Function Name : CAN_IsInterruptPending
742 * Description : Tests the interrupt status of a message object.
743 * Input : - msgobj: specifies the Message object number, from 0 to 31.
745 * Return : A non-zero value if the corresponding message has an
746 * interrupt pending, else 0.
747 *******************************************************************************/
748 u32 CAN_IsInterruptPending(u32 msgobj)
750 return (msgobj < 16 ? CAN->IP1R & (1 << msgobj) : CAN->IP2R & (1 << (msgobj-16)));
753 /*******************************************************************************
754 * Function Name : CAN_IsObjectValid
755 * Description : Tests the validity of a message object (ready to use).
756 * Input : - msgobj: specifies the Message object number, from 0 to 31.
758 * Return : A non-zero value if the corresponding message object is
760 *******************************************************************************/
761 u32 CAN_IsObjectValid(u32 msgobj)
763 return (msgobj < 16 ? CAN->MV1R & (1 << msgobj) : CAN->MV2R & (1 << (msgobj-16)));
765 /******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/