STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_can.c
Go to the documentation of this file.
00001 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x_can.h"
00024 #include "stm32f10x_rcc.h"
00025 
00047 /* CAN Master Control Register bits */
00048 
00049 #define MCR_DBF      ((uint32_t)0x00010000) /* software master reset */
00050 
00051 /* CAN Mailbox Transmit Request */
00052 #define TMIDxR_TXRQ  ((uint32_t)0x00000001) /* Transmit mailbox request */
00053 
00054 /* CAN Filter Master Register bits */
00055 #define FMR_FINIT    ((uint32_t)0x00000001) /* Filter init mode */
00056 
00057 /* Time out for INAK bit */
00058 #define INAK_TIMEOUT        ((uint32_t)0x0000FFFF)
00059 /* Time out for SLAK bit */
00060 #define SLAK_TIMEOUT        ((uint32_t)0x0000FFFF)
00061 
00062 
00063 
00064 /* Flags in TSR register */
00065 #define CAN_FLAGS_TSR              ((uint32_t)0x08000000) 
00066 /* Flags in RF1R register */
00067 #define CAN_FLAGS_RF1R             ((uint32_t)0x04000000) 
00068 /* Flags in RF0R register */
00069 #define CAN_FLAGS_RF0R             ((uint32_t)0x02000000) 
00070 /* Flags in MSR register */
00071 #define CAN_FLAGS_MSR              ((uint32_t)0x01000000) 
00072 /* Flags in ESR register */
00073 #define CAN_FLAGS_ESR              ((uint32_t)0x00F00000) 
00074 
00075 /* Mailboxes definition */
00076 #define CAN_TXMAILBOX_0                   ((uint8_t)0x00)
00077 #define CAN_TXMAILBOX_1                   ((uint8_t)0x01)
00078 #define CAN_TXMAILBOX_2                   ((uint8_t)0x02) 
00079 
00080 
00081 
00082 #define CAN_MODE_MASK              ((uint32_t) 0x00000003)
00083 
00107 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit);
00108 
00122 void CAN_DeInit(CAN_TypeDef* CANx)
00123 {
00124   /* Check the parameters */
00125   assert_param(IS_CAN_ALL_PERIPH(CANx));
00126  
00127   if (CANx == CAN1)
00128   {
00129     /* Enable CAN1 reset state */
00130     RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, ENABLE);
00131     /* Release CAN1 from reset state */
00132     RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, DISABLE);
00133   }
00134   else
00135   {  
00136     /* Enable CAN2 reset state */
00137     RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, ENABLE);
00138     /* Release CAN2 from reset state */
00139     RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, DISABLE);
00140   }
00141 }
00142 
00154 uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
00155 {
00156   uint8_t InitStatus = CAN_InitStatus_Failed;
00157   uint32_t wait_ack = 0x00000000;
00158   /* Check the parameters */
00159   assert_param(IS_CAN_ALL_PERIPH(CANx));
00160   assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
00161   assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
00162   assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
00163   assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
00164   assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
00165   assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
00166   assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
00167   assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
00168   assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
00169   assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
00170   assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
00171 
00172   /* Exit from sleep mode */
00173   CANx->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
00174 
00175   /* Request initialisation */
00176   CANx->MCR |= CAN_MCR_INRQ ;
00177 
00178   /* Wait the acknowledge */
00179   while (((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
00180   {
00181     wait_ack++;
00182   }
00183 
00184   /* Check acknowledge */
00185   if ((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
00186   {
00187     InitStatus = CAN_InitStatus_Failed;
00188   }
00189   else 
00190   {
00191     /* Set the time triggered communication mode */
00192     if (CAN_InitStruct->CAN_TTCM == ENABLE)
00193     {
00194       CANx->MCR |= CAN_MCR_TTCM;
00195     }
00196     else
00197     {
00198       CANx->MCR &= ~(uint32_t)CAN_MCR_TTCM;
00199     }
00200 
00201     /* Set the automatic bus-off management */
00202     if (CAN_InitStruct->CAN_ABOM == ENABLE)
00203     {
00204       CANx->MCR |= CAN_MCR_ABOM;
00205     }
00206     else
00207     {
00208       CANx->MCR &= ~(uint32_t)CAN_MCR_ABOM;
00209     }
00210 
00211     /* Set the automatic wake-up mode */
00212     if (CAN_InitStruct->CAN_AWUM == ENABLE)
00213     {
00214       CANx->MCR |= CAN_MCR_AWUM;
00215     }
00216     else
00217     {
00218       CANx->MCR &= ~(uint32_t)CAN_MCR_AWUM;
00219     }
00220 
00221     /* Set the no automatic retransmission */
00222     if (CAN_InitStruct->CAN_NART == ENABLE)
00223     {
00224       CANx->MCR |= CAN_MCR_NART;
00225     }
00226     else
00227     {
00228       CANx->MCR &= ~(uint32_t)CAN_MCR_NART;
00229     }
00230 
00231     /* Set the receive FIFO locked mode */
00232     if (CAN_InitStruct->CAN_RFLM == ENABLE)
00233     {
00234       CANx->MCR |= CAN_MCR_RFLM;
00235     }
00236     else
00237     {
00238       CANx->MCR &= ~(uint32_t)CAN_MCR_RFLM;
00239     }
00240 
00241     /* Set the transmit FIFO priority */
00242     if (CAN_InitStruct->CAN_TXFP == ENABLE)
00243     {
00244       CANx->MCR |= CAN_MCR_TXFP;
00245     }
00246     else
00247     {
00248       CANx->MCR &= ~(uint32_t)CAN_MCR_TXFP;
00249     }
00250 
00251     /* Set the bit timing register */
00252     CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | \
00253                 ((uint32_t)CAN_InitStruct->CAN_SJW << 24) | \
00254                 ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | \
00255                 ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) | \
00256                ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
00257 
00258     /* Request leave initialisation */
00259     CANx->MCR &= ~(uint32_t)CAN_MCR_INRQ;
00260 
00261    /* Wait the acknowledge */
00262    wait_ack = 0;
00263 
00264    while (((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
00265    {
00266      wait_ack++;
00267    }
00268 
00269     /* ...and check acknowledged */
00270     if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
00271     {
00272       InitStatus = CAN_InitStatus_Failed;
00273     }
00274     else
00275     {
00276       InitStatus = CAN_InitStatus_Success ;
00277     }
00278   }
00279 
00280   /* At this step, return the status of initialization */
00281   return InitStatus;
00282 }
00283 
00292 void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
00293 {
00294   uint32_t filter_number_bit_pos = 0;
00295   /* Check the parameters */
00296   assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
00297   assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
00298   assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
00299   assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
00300   assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));
00301 
00302   filter_number_bit_pos = ((uint32_t)1) << CAN_FilterInitStruct->CAN_FilterNumber;
00303 
00304   /* Initialisation mode for the filter */
00305   CAN1->FMR |= FMR_FINIT;
00306 
00307   /* Filter Deactivation */
00308   CAN1->FA1R &= ~(uint32_t)filter_number_bit_pos;
00309 
00310   /* Filter Scale */
00311   if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
00312   {
00313     /* 16-bit scale for the filter */
00314     CAN1->FS1R &= ~(uint32_t)filter_number_bit_pos;
00315 
00316     /* First 16-bit identifier and First 16-bit mask */
00317     /* Or First 16-bit identifier and Second 16-bit identifier */
00318     CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 = 
00319     ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
00320         (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
00321 
00322     /* Second 16-bit identifier and Second 16-bit mask */
00323     /* Or Third 16-bit identifier and Fourth 16-bit identifier */
00324     CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 = 
00325     ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
00326         (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh);
00327   }
00328 
00329   if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
00330   {
00331     /* 32-bit scale for the filter */
00332     CAN1->FS1R |= filter_number_bit_pos;
00333     /* 32-bit identifier or First 32-bit identifier */
00334     CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 = 
00335     ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
00336         (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
00337     /* 32-bit mask or Second 32-bit identifier */
00338     CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 = 
00339     ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
00340         (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow);
00341   }
00342 
00343   /* Filter Mode */
00344   if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
00345   {
00346     /*Id/Mask mode for the filter*/
00347     CAN1->FM1R &= ~(uint32_t)filter_number_bit_pos;
00348   }
00349   else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
00350   {
00351     /*Identifier list mode for the filter*/
00352     CAN1->FM1R |= (uint32_t)filter_number_bit_pos;
00353   }
00354 
00355   /* Filter FIFO assignment */
00356   if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO0)
00357   {
00358     /* FIFO 0 assignation for the filter */
00359     CAN1->FFA1R &= ~(uint32_t)filter_number_bit_pos;
00360   }
00361 
00362   if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO1)
00363   {
00364     /* FIFO 1 assignation for the filter */
00365     CAN1->FFA1R |= (uint32_t)filter_number_bit_pos;
00366   }
00367   
00368   /* Filter activation */
00369   if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
00370   {
00371     CAN1->FA1R |= filter_number_bit_pos;
00372   }
00373 
00374   /* Leave the initialisation mode for the filter */
00375   CAN1->FMR &= ~FMR_FINIT;
00376 }
00377 
00384 void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
00385 {
00386   /* Reset CAN init structure parameters values */
00387   
00388   /* Initialize the time triggered communication mode */
00389   CAN_InitStruct->CAN_TTCM = DISABLE;
00390   
00391   /* Initialize the automatic bus-off management */
00392   CAN_InitStruct->CAN_ABOM = DISABLE;
00393   
00394   /* Initialize the automatic wake-up mode */
00395   CAN_InitStruct->CAN_AWUM = DISABLE;
00396   
00397   /* Initialize the no automatic retransmission */
00398   CAN_InitStruct->CAN_NART = DISABLE;
00399   
00400   /* Initialize the receive FIFO locked mode */
00401   CAN_InitStruct->CAN_RFLM = DISABLE;
00402   
00403   /* Initialize the transmit FIFO priority */
00404   CAN_InitStruct->CAN_TXFP = DISABLE;
00405   
00406   /* Initialize the CAN_Mode member */
00407   CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
00408   
00409   /* Initialize the CAN_SJW member */
00410   CAN_InitStruct->CAN_SJW = CAN_SJW_1tq;
00411   
00412   /* Initialize the CAN_BS1 member */
00413   CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
00414   
00415   /* Initialize the CAN_BS2 member */
00416   CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
00417   
00418   /* Initialize the CAN_Prescaler member */
00419   CAN_InitStruct->CAN_Prescaler = 1;
00420 }
00421 
00428 void CAN_SlaveStartBank(uint8_t CAN_BankNumber) 
00429 {
00430   /* Check the parameters */
00431   assert_param(IS_CAN_BANKNUMBER(CAN_BankNumber));
00432   
00433   /* Enter Initialisation mode for the filter */
00434   CAN1->FMR |= FMR_FINIT;
00435   
00436   /* Select the start slave bank */
00437   CAN1->FMR &= (uint32_t)0xFFFFC0F1 ;
00438   CAN1->FMR |= (uint32_t)(CAN_BankNumber)<<8;
00439   
00440   /* Leave Initialisation mode for the filter */
00441   CAN1->FMR &= ~FMR_FINIT;
00442 }
00443 
00451 void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState)
00452 {
00453   /* Check the parameters */
00454   assert_param(IS_CAN_ALL_PERIPH(CANx));
00455   assert_param(IS_FUNCTIONAL_STATE(NewState));
00456   
00457   if (NewState != DISABLE)
00458   {
00459     /* Enable Debug Freeze  */
00460     CANx->MCR |= MCR_DBF;
00461   }
00462   else
00463   {
00464     /* Disable Debug Freeze */
00465     CANx->MCR &= ~MCR_DBF;
00466   }
00467 }
00468 
00469 
00481 void CAN_TTComModeCmd(CAN_TypeDef* CANx, FunctionalState NewState)
00482 {
00483   /* Check the parameters */
00484   assert_param(IS_CAN_ALL_PERIPH(CANx));
00485   assert_param(IS_FUNCTIONAL_STATE(NewState));
00486   if (NewState != DISABLE)
00487   {
00488     /* Enable the TTCM mode */
00489     CANx->MCR |= CAN_MCR_TTCM;
00490 
00491     /* Set TGT bits */
00492     CANx->sTxMailBox[0].TDTR |= ((uint32_t)CAN_TDT0R_TGT);
00493     CANx->sTxMailBox[1].TDTR |= ((uint32_t)CAN_TDT1R_TGT);
00494     CANx->sTxMailBox[2].TDTR |= ((uint32_t)CAN_TDT2R_TGT);
00495   }
00496   else
00497   {
00498     /* Disable the TTCM mode */
00499     CANx->MCR &= (uint32_t)(~(uint32_t)CAN_MCR_TTCM);
00500 
00501     /* Reset TGT bits */
00502     CANx->sTxMailBox[0].TDTR &= ((uint32_t)~CAN_TDT0R_TGT);
00503     CANx->sTxMailBox[1].TDTR &= ((uint32_t)~CAN_TDT1R_TGT);
00504     CANx->sTxMailBox[2].TDTR &= ((uint32_t)~CAN_TDT2R_TGT);
00505   }
00506 }
00515 uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)
00516 {
00517   uint8_t transmit_mailbox = 0;
00518   /* Check the parameters */
00519   assert_param(IS_CAN_ALL_PERIPH(CANx));
00520   assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
00521   assert_param(IS_CAN_RTR(TxMessage->RTR));
00522   assert_param(IS_CAN_DLC(TxMessage->DLC));
00523 
00524   /* Select one empty transmit mailbox */
00525   if ((CANx->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
00526   {
00527     transmit_mailbox = 0;
00528   }
00529   else if ((CANx->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
00530   {
00531     transmit_mailbox = 1;
00532   }
00533   else if ((CANx->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
00534   {
00535     transmit_mailbox = 2;
00536   }
00537   else
00538   {
00539     transmit_mailbox = CAN_TxStatus_NoMailBox;
00540   }
00541 
00542   if (transmit_mailbox != CAN_TxStatus_NoMailBox)
00543   {
00544     /* Set up the Id */
00545     CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
00546     if (TxMessage->IDE == CAN_Id_Standard)
00547     {
00548       assert_param(IS_CAN_STDID(TxMessage->StdId));  
00549       CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->StdId << 21) | \
00550                                                   TxMessage->RTR);
00551     }
00552     else
00553     {
00554       assert_param(IS_CAN_EXTID(TxMessage->ExtId));
00555       CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->ExtId << 3) | \
00556                                                   TxMessage->IDE | \
00557                                                   TxMessage->RTR);
00558     }
00559     
00560     /* Set up the DLC */
00561     TxMessage->DLC &= (uint8_t)0x0000000F;
00562     CANx->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0xFFFFFFF0;
00563     CANx->sTxMailBox[transmit_mailbox].TDTR |= TxMessage->DLC;
00564 
00565     /* Set up the data field */
00566     CANx->sTxMailBox[transmit_mailbox].TDLR = (((uint32_t)TxMessage->Data[3] << 24) | 
00567                                              ((uint32_t)TxMessage->Data[2] << 16) |
00568                                              ((uint32_t)TxMessage->Data[1] << 8) | 
00569                                              ((uint32_t)TxMessage->Data[0]));
00570     CANx->sTxMailBox[transmit_mailbox].TDHR = (((uint32_t)TxMessage->Data[7] << 24) | 
00571                                              ((uint32_t)TxMessage->Data[6] << 16) |
00572                                              ((uint32_t)TxMessage->Data[5] << 8) |
00573                                              ((uint32_t)TxMessage->Data[4]));
00574     /* Request transmission */
00575     CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
00576   }
00577   return transmit_mailbox;
00578 }
00579 
00589 uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox)
00590 {
00591   uint32_t state = 0;
00592 
00593   /* Check the parameters */
00594   assert_param(IS_CAN_ALL_PERIPH(CANx));
00595   assert_param(IS_CAN_TRANSMITMAILBOX(TransmitMailbox));
00596  
00597   switch (TransmitMailbox)
00598   {
00599     case (CAN_TXMAILBOX_0): 
00600       state =   CANx->TSR &  (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0);
00601       break;
00602     case (CAN_TXMAILBOX_1): 
00603       state =   CANx->TSR &  (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1);
00604       break;
00605     case (CAN_TXMAILBOX_2): 
00606       state =   CANx->TSR &  (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2);
00607       break;
00608     default:
00609       state = CAN_TxStatus_Failed;
00610       break;
00611   }
00612   switch (state)
00613   {
00614       /* transmit pending  */
00615     case (0x0): state = CAN_TxStatus_Pending;
00616       break;
00617       /* transmit failed  */
00618      case (CAN_TSR_RQCP0 | CAN_TSR_TME0): state = CAN_TxStatus_Failed;
00619       break;
00620      case (CAN_TSR_RQCP1 | CAN_TSR_TME1): state = CAN_TxStatus_Failed;
00621       break;
00622      case (CAN_TSR_RQCP2 | CAN_TSR_TME2): state = CAN_TxStatus_Failed;
00623       break;
00624       /* transmit succeeded  */
00625     case (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0):state = CAN_TxStatus_Ok;
00626       break;
00627     case (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1):state = CAN_TxStatus_Ok;
00628       break;
00629     case (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2):state = CAN_TxStatus_Ok;
00630       break;
00631     default: state = CAN_TxStatus_Failed;
00632       break;
00633   }
00634   return (uint8_t) state;
00635 }
00636 
00643 void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox)
00644 {
00645   /* Check the parameters */
00646   assert_param(IS_CAN_ALL_PERIPH(CANx));
00647   assert_param(IS_CAN_TRANSMITMAILBOX(Mailbox));
00648   /* abort transmission */
00649   switch (Mailbox)
00650   {
00651     case (CAN_TXMAILBOX_0): CANx->TSR |= CAN_TSR_ABRQ0;
00652       break;
00653     case (CAN_TXMAILBOX_1): CANx->TSR |= CAN_TSR_ABRQ1;
00654       break;
00655     case (CAN_TXMAILBOX_2): CANx->TSR |= CAN_TSR_ABRQ2;
00656       break;
00657     default:
00658       break;
00659   }
00660 }
00661 
00662 
00671 void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage)
00672 {
00673   /* Check the parameters */
00674   assert_param(IS_CAN_ALL_PERIPH(CANx));
00675   assert_param(IS_CAN_FIFO(FIFONumber));
00676   /* Get the Id */
00677   RxMessage->IDE = (uint8_t)0x04 & CANx->sFIFOMailBox[FIFONumber].RIR;
00678   if (RxMessage->IDE == CAN_Id_Standard)
00679   {
00680     RxMessage->StdId = (uint32_t)0x000007FF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 21);
00681   }
00682   else
00683   {
00684     RxMessage->ExtId = (uint32_t)0x1FFFFFFF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 3);
00685   }
00686   
00687   RxMessage->RTR = (uint8_t)0x02 & CANx->sFIFOMailBox[FIFONumber].RIR;
00688   /* Get the DLC */
00689   RxMessage->DLC = (uint8_t)0x0F & CANx->sFIFOMailBox[FIFONumber].RDTR;
00690   /* Get the FMI */
00691   RxMessage->FMI = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDTR >> 8);
00692   /* Get the data field */
00693   RxMessage->Data[0] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDLR;
00694   RxMessage->Data[1] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 8);
00695   RxMessage->Data[2] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 16);
00696   RxMessage->Data[3] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 24);
00697   RxMessage->Data[4] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDHR;
00698   RxMessage->Data[5] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 8);
00699   RxMessage->Data[6] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 16);
00700   RxMessage->Data[7] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 24);
00701   /* Release the FIFO */
00702   /* Release FIFO0 */
00703   if (FIFONumber == CAN_FIFO0)
00704   {
00705     CANx->RF0R |= CAN_RF0R_RFOM0;
00706   }
00707   /* Release FIFO1 */
00708   else /* FIFONumber == CAN_FIFO1 */
00709   {
00710     CANx->RF1R |= CAN_RF1R_RFOM1;
00711   }
00712 }
00713 
00720 void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber)
00721 {
00722   /* Check the parameters */
00723   assert_param(IS_CAN_ALL_PERIPH(CANx));
00724   assert_param(IS_CAN_FIFO(FIFONumber));
00725   /* Release FIFO0 */
00726   if (FIFONumber == CAN_FIFO0)
00727   {
00728     CANx->RF0R |= CAN_RF0R_RFOM0;
00729   }
00730   /* Release FIFO1 */
00731   else /* FIFONumber == CAN_FIFO1 */
00732   {
00733     CANx->RF1R |= CAN_RF1R_RFOM1;
00734   }
00735 }
00736 
00743 uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber)
00744 {
00745   uint8_t message_pending=0;
00746   /* Check the parameters */
00747   assert_param(IS_CAN_ALL_PERIPH(CANx));
00748   assert_param(IS_CAN_FIFO(FIFONumber));
00749   if (FIFONumber == CAN_FIFO0)
00750   {
00751     message_pending = (uint8_t)(CANx->RF0R&(uint32_t)0x03);
00752   }
00753   else if (FIFONumber == CAN_FIFO1)
00754   {
00755     message_pending = (uint8_t)(CANx->RF1R&(uint32_t)0x03);
00756   }
00757   else
00758   {
00759     message_pending = 0;
00760   }
00761   return message_pending;
00762 }
00763 
00764 
00774 uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode)
00775 {
00776   uint8_t status = CAN_ModeStatus_Failed;
00777   
00778   /* Timeout for INAK or also for SLAK bits*/
00779   uint32_t timeout = INAK_TIMEOUT; 
00780 
00781   /* Check the parameters */
00782   assert_param(IS_CAN_ALL_PERIPH(CANx));
00783   assert_param(IS_CAN_OPERATING_MODE(CAN_OperatingMode));
00784 
00785   if (CAN_OperatingMode == CAN_OperatingMode_Initialization)
00786   {
00787     /* Request initialisation */
00788     CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_SLEEP)) | CAN_MCR_INRQ);
00789 
00790     /* Wait the acknowledge */
00791     while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK) && (timeout != 0))
00792     {
00793       timeout--;
00794     }
00795     if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK)
00796     {
00797       status = CAN_ModeStatus_Failed;
00798     }
00799     else
00800     {
00801       status = CAN_ModeStatus_Success;
00802     }
00803   }
00804   else  if (CAN_OperatingMode == CAN_OperatingMode_Normal)
00805   {
00806     /* Request leave initialisation and sleep mode  and enter Normal mode */
00807     CANx->MCR &= (uint32_t)(~(CAN_MCR_SLEEP|CAN_MCR_INRQ));
00808 
00809     /* Wait the acknowledge */
00810     while (((CANx->MSR & CAN_MODE_MASK) != 0) && (timeout!=0))
00811     {
00812       timeout--;
00813     }
00814     if ((CANx->MSR & CAN_MODE_MASK) != 0)
00815     {
00816       status = CAN_ModeStatus_Failed;
00817     }
00818     else
00819     {
00820       status = CAN_ModeStatus_Success;
00821     }
00822   }
00823   else  if (CAN_OperatingMode == CAN_OperatingMode_Sleep)
00824   {
00825     /* Request Sleep mode */
00826     CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
00827 
00828     /* Wait the acknowledge */
00829     while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK) && (timeout!=0))
00830     {
00831       timeout--;
00832     }
00833     if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK)
00834     {
00835       status = CAN_ModeStatus_Failed;
00836     }
00837     else
00838     {
00839       status = CAN_ModeStatus_Success;
00840     }
00841   }
00842   else
00843   {
00844     status = CAN_ModeStatus_Failed;
00845   }
00846 
00847   return  (uint8_t) status;
00848 }
00849 
00856 uint8_t CAN_Sleep(CAN_TypeDef* CANx)
00857 {
00858   uint8_t sleepstatus = CAN_Sleep_Failed;
00859   
00860   /* Check the parameters */
00861   assert_param(IS_CAN_ALL_PERIPH(CANx));
00862     
00863   /* Request Sleep mode */
00864    CANx->MCR = (((CANx->MCR) & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
00865    
00866   /* Sleep mode status */
00867   if ((CANx->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) == CAN_MSR_SLAK)
00868   {
00869     /* Sleep mode not entered */
00870     sleepstatus =  CAN_Sleep_Ok;
00871   }
00872   /* return sleep mode status */
00873    return (uint8_t)sleepstatus;
00874 }
00875 
00882 uint8_t CAN_WakeUp(CAN_TypeDef* CANx)
00883 {
00884   uint32_t wait_slak = SLAK_TIMEOUT;
00885   uint8_t wakeupstatus = CAN_WakeUp_Failed;
00886   
00887   /* Check the parameters */
00888   assert_param(IS_CAN_ALL_PERIPH(CANx));
00889     
00890   /* Wake up request */
00891   CANx->MCR &= ~(uint32_t)CAN_MCR_SLEEP;
00892     
00893   /* Sleep mode status */
00894   while(((CANx->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)&&(wait_slak!=0x00))
00895   {
00896    wait_slak--;
00897   }
00898   if((CANx->MSR & CAN_MSR_SLAK) != CAN_MSR_SLAK)
00899   {
00900    /* wake up done : Sleep mode exited */
00901     wakeupstatus = CAN_WakeUp_Ok;
00902   }
00903   /* return wakeup status */
00904   return (uint8_t)wakeupstatus;
00905 }
00906 
00907 
00922 uint8_t CAN_GetLastErrorCode(CAN_TypeDef* CANx)
00923 {
00924   uint8_t errorcode=0;
00925   
00926   /* Check the parameters */
00927   assert_param(IS_CAN_ALL_PERIPH(CANx));
00928   
00929   /* Get the error code*/
00930   errorcode = (((uint8_t)CANx->ESR) & (uint8_t)CAN_ESR_LEC);
00931   
00932   /* Return the error code*/
00933   return errorcode;
00934 }
00946 uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef* CANx)
00947 {
00948   uint8_t counter=0;
00949   
00950   /* Check the parameters */
00951   assert_param(IS_CAN_ALL_PERIPH(CANx));
00952   
00953   /* Get the Receive Error Counter*/
00954   counter = (uint8_t)((CANx->ESR & CAN_ESR_REC)>> 24);
00955   
00956   /* Return the Receive Error Counter*/
00957   return counter;
00958 }
00959 
00960 
00966 uint8_t CAN_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx)
00967 {
00968   uint8_t counter=0;
00969   
00970   /* Check the parameters */
00971   assert_param(IS_CAN_ALL_PERIPH(CANx));
00972   
00973   /* Get the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
00974   counter = (uint8_t)((CANx->ESR & CAN_ESR_TEC)>> 16);
00975   
00976   /* Return the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
00977   return counter;
00978 }
00979 
00980 
01003 void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState)
01004 {
01005   /* Check the parameters */
01006   assert_param(IS_CAN_ALL_PERIPH(CANx));
01007   assert_param(IS_CAN_IT(CAN_IT));
01008   assert_param(IS_FUNCTIONAL_STATE(NewState));
01009 
01010   if (NewState != DISABLE)
01011   {
01012     /* Enable the selected CANx interrupt */
01013     CANx->IER |= CAN_IT;
01014   }
01015   else
01016   {
01017     /* Disable the selected CANx interrupt */
01018     CANx->IER &= ~CAN_IT;
01019   }
01020 }
01043 FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
01044 {
01045   FlagStatus bitstatus = RESET;
01046   
01047   /* Check the parameters */
01048   assert_param(IS_CAN_ALL_PERIPH(CANx));
01049   assert_param(IS_CAN_GET_FLAG(CAN_FLAG));
01050   
01051 
01052   if((CAN_FLAG & CAN_FLAGS_ESR) != (uint32_t)RESET)
01053   { 
01054     /* Check the status of the specified CAN flag */
01055     if ((CANx->ESR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
01056     { 
01057       /* CAN_FLAG is set */
01058       bitstatus = SET;
01059     }
01060     else
01061     { 
01062       /* CAN_FLAG is reset */
01063       bitstatus = RESET;
01064     }
01065   }
01066   else if((CAN_FLAG & CAN_FLAGS_MSR) != (uint32_t)RESET)
01067   { 
01068     /* Check the status of the specified CAN flag */
01069     if ((CANx->MSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
01070     { 
01071       /* CAN_FLAG is set */
01072       bitstatus = SET;
01073     }
01074     else
01075     { 
01076       /* CAN_FLAG is reset */
01077       bitstatus = RESET;
01078     }
01079   }
01080   else if((CAN_FLAG & CAN_FLAGS_TSR) != (uint32_t)RESET)
01081   { 
01082     /* Check the status of the specified CAN flag */
01083     if ((CANx->TSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
01084     { 
01085       /* CAN_FLAG is set */
01086       bitstatus = SET;
01087     }
01088     else
01089     { 
01090       /* CAN_FLAG is reset */
01091       bitstatus = RESET;
01092     }
01093   }
01094   else if((CAN_FLAG & CAN_FLAGS_RF0R) != (uint32_t)RESET)
01095   { 
01096     /* Check the status of the specified CAN flag */
01097     if ((CANx->RF0R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
01098     { 
01099       /* CAN_FLAG is set */
01100       bitstatus = SET;
01101     }
01102     else
01103     { 
01104       /* CAN_FLAG is reset */
01105       bitstatus = RESET;
01106     }
01107   }
01108   else /* If(CAN_FLAG & CAN_FLAGS_RF1R != (uint32_t)RESET) */
01109   { 
01110     /* Check the status of the specified CAN flag */
01111     if ((uint32_t)(CANx->RF1R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
01112     { 
01113       /* CAN_FLAG is set */
01114       bitstatus = SET;
01115     }
01116     else
01117     { 
01118       /* CAN_FLAG is reset */
01119       bitstatus = RESET;
01120     }
01121   }
01122   /* Return the CAN_FLAG status */
01123   return  bitstatus;
01124 }
01125 
01143 void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
01144 {
01145   uint32_t flagtmp=0;
01146   /* Check the parameters */
01147   assert_param(IS_CAN_ALL_PERIPH(CANx));
01148   assert_param(IS_CAN_CLEAR_FLAG(CAN_FLAG));
01149   
01150   if (CAN_FLAG == CAN_FLAG_LEC) /* ESR register */
01151   {
01152     /* Clear the selected CAN flags */
01153     CANx->ESR = (uint32_t)RESET;
01154   }
01155   else /* MSR or TSR or RF0R or RF1R */
01156   {
01157     flagtmp = CAN_FLAG & 0x000FFFFF;
01158 
01159     if ((CAN_FLAG & CAN_FLAGS_RF0R)!=(uint32_t)RESET)
01160     {
01161       /* Receive Flags */
01162       CANx->RF0R = (uint32_t)(flagtmp);
01163     }
01164     else if ((CAN_FLAG & CAN_FLAGS_RF1R)!=(uint32_t)RESET)
01165     {
01166       /* Receive Flags */
01167       CANx->RF1R = (uint32_t)(flagtmp);
01168     }
01169     else if ((CAN_FLAG & CAN_FLAGS_TSR)!=(uint32_t)RESET)
01170     {
01171       /* Transmit Flags */
01172       CANx->TSR = (uint32_t)(flagtmp);
01173     }
01174     else /* If((CAN_FLAG & CAN_FLAGS_MSR)!=(uint32_t)RESET) */
01175     {
01176       /* Operating mode Flags */
01177       CANx->MSR = (uint32_t)(flagtmp);
01178     }
01179   }
01180 }
01181 
01203 ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT)
01204 {
01205   ITStatus itstatus = RESET;
01206   /* Check the parameters */
01207   assert_param(IS_CAN_ALL_PERIPH(CANx));
01208   assert_param(IS_CAN_IT(CAN_IT));
01209   
01210   /* check the enable interrupt bit */
01211  if((CANx->IER & CAN_IT) != RESET)
01212  {
01213    /* in case the Interrupt is enabled, .... */
01214     switch (CAN_IT)
01215     {
01216       case CAN_IT_TME:
01217                /* Check CAN_TSR_RQCPx bits */
01218                      itstatus = CheckITStatus(CANx->TSR, CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2);  
01219               break;
01220       case CAN_IT_FMP0:
01221                /* Check CAN_RF0R_FMP0 bit */
01222                      itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FMP0);  
01223               break;
01224       case CAN_IT_FF0:
01225                /* Check CAN_RF0R_FULL0 bit */
01226                itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FULL0);  
01227               break;
01228       case CAN_IT_FOV0:
01229                /* Check CAN_RF0R_FOVR0 bit */
01230                itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FOVR0);  
01231               break;
01232       case CAN_IT_FMP1:
01233                /* Check CAN_RF1R_FMP1 bit */
01234                itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FMP1);  
01235               break;
01236       case CAN_IT_FF1:
01237                /* Check CAN_RF1R_FULL1 bit */
01238                      itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FULL1);  
01239               break;
01240       case CAN_IT_FOV1:
01241                /* Check CAN_RF1R_FOVR1 bit */
01242                      itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FOVR1);  
01243               break;
01244       case CAN_IT_WKU:
01245                /* Check CAN_MSR_WKUI bit */
01246                itstatus = CheckITStatus(CANx->MSR, CAN_MSR_WKUI);  
01247               break;
01248       case CAN_IT_SLK:
01249                /* Check CAN_MSR_SLAKI bit */
01250                      itstatus = CheckITStatus(CANx->MSR, CAN_MSR_SLAKI);  
01251               break;
01252       case CAN_IT_EWG:
01253                /* Check CAN_ESR_EWGF bit */
01254                      itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EWGF);  
01255               break;
01256       case CAN_IT_EPV:
01257                /* Check CAN_ESR_EPVF bit */
01258                      itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EPVF);  
01259               break;
01260       case CAN_IT_BOF:
01261                /* Check CAN_ESR_BOFF bit */
01262                      itstatus = CheckITStatus(CANx->ESR, CAN_ESR_BOFF);  
01263               break;
01264       case CAN_IT_LEC:
01265                /* Check CAN_ESR_LEC bit */
01266                      itstatus = CheckITStatus(CANx->ESR, CAN_ESR_LEC);  
01267               break;
01268       case CAN_IT_ERR:
01269                /* Check CAN_MSR_ERRI bit */ 
01270                itstatus = CheckITStatus(CANx->MSR, CAN_MSR_ERRI); 
01271               break;
01272       default :
01273                /* in case of error, return RESET */
01274               itstatus = RESET;
01275               break;
01276     }
01277   }
01278   else
01279   {
01280    /* in case the Interrupt is not enabled, return RESET */
01281     itstatus  = RESET;
01282   }
01283   
01284   /* Return the CAN_IT status */
01285   return  itstatus;
01286 }
01287 
01306 void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT)
01307 {
01308   /* Check the parameters */
01309   assert_param(IS_CAN_ALL_PERIPH(CANx));
01310   assert_param(IS_CAN_CLEAR_IT(CAN_IT));
01311 
01312   switch (CAN_IT)
01313   {
01314       case CAN_IT_TME:
01315               /* Clear CAN_TSR_RQCPx (rc_w1)*/
01316               CANx->TSR = CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2;  
01317               break;
01318       case CAN_IT_FF0:
01319               /* Clear CAN_RF0R_FULL0 (rc_w1)*/
01320               CANx->RF0R = CAN_RF0R_FULL0; 
01321               break;
01322       case CAN_IT_FOV0:
01323               /* Clear CAN_RF0R_FOVR0 (rc_w1)*/
01324               CANx->RF0R = CAN_RF0R_FOVR0; 
01325               break;
01326       case CAN_IT_FF1:
01327               /* Clear CAN_RF1R_FULL1 (rc_w1)*/
01328               CANx->RF1R = CAN_RF1R_FULL1;  
01329               break;
01330       case CAN_IT_FOV1:
01331               /* Clear CAN_RF1R_FOVR1 (rc_w1)*/
01332               CANx->RF1R = CAN_RF1R_FOVR1; 
01333               break;
01334       case CAN_IT_WKU:
01335               /* Clear CAN_MSR_WKUI (rc_w1)*/
01336               CANx->MSR = CAN_MSR_WKUI;  
01337               break;
01338       case CAN_IT_SLK:
01339               /* Clear CAN_MSR_SLAKI (rc_w1)*/ 
01340               CANx->MSR = CAN_MSR_SLAKI;   
01341               break;
01342       case CAN_IT_EWG:
01343               /* Clear CAN_MSR_ERRI (rc_w1) */
01344               CANx->MSR = CAN_MSR_ERRI;
01345               /* Note : the corresponding Flag is cleared by hardware depending 
01346                         of the CAN Bus status*/ 
01347               break;
01348       case CAN_IT_EPV:
01349               /* Clear CAN_MSR_ERRI (rc_w1) */
01350               CANx->MSR = CAN_MSR_ERRI; 
01351               /* Note : the corresponding Flag is cleared by hardware depending 
01352                         of the CAN Bus status*/
01353               break;
01354       case CAN_IT_BOF:
01355               /* Clear CAN_MSR_ERRI (rc_w1) */ 
01356               CANx->MSR = CAN_MSR_ERRI; 
01357               /* Note : the corresponding Flag is cleared by hardware depending 
01358                         of the CAN Bus status*/
01359               break;
01360       case CAN_IT_LEC:
01361               /*  Clear LEC bits */
01362               CANx->ESR = RESET; 
01363               /* Clear CAN_MSR_ERRI (rc_w1) */
01364               CANx->MSR = CAN_MSR_ERRI; 
01365               break;
01366       case CAN_IT_ERR:
01367               /*Clear LEC bits */
01368               CANx->ESR = RESET; 
01369               /* Clear CAN_MSR_ERRI (rc_w1) */
01370               CANx->MSR = CAN_MSR_ERRI; 
01371               /* Note : BOFF, EPVF and EWGF Flags are cleared by hardware depending 
01372                   of the CAN Bus status*/
01373               break;
01374       default :
01375               break;
01376    }
01377 }
01378 
01385 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit)
01386 {
01387   ITStatus pendingbitstatus = RESET;
01388   
01389   if ((CAN_Reg & It_Bit) != (uint32_t)RESET)
01390   {
01391     /* CAN_IT is set */
01392     pendingbitstatus = SET;
01393   }
01394   else
01395   {
01396     /* CAN_IT is reset */
01397     pendingbitstatus = RESET;
01398   }
01399   return pendingbitstatus;
01400 }
01401 
01402 
01415 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/