STM32F10x Standard Peripherals Library
3.5.0
|
00001 00023 /* Define to prevent recursive inclusion -------------------------------------*/ 00024 #ifndef __STM32F10x_CAN_H 00025 #define __STM32F10x_CAN_H 00026 00027 #ifdef __cplusplus 00028 extern "C" { 00029 #endif 00030 00031 /* Includes ------------------------------------------------------------------*/ 00032 #include "stm32f10x.h" 00033 00046 #define IS_CAN_ALL_PERIPH(PERIPH) (((PERIPH) == CAN1) || \ 00047 ((PERIPH) == CAN2)) 00048 00053 typedef struct 00054 { 00055 uint16_t CAN_Prescaler; 00058 uint8_t CAN_Mode; 00062 uint8_t CAN_SJW; 00068 uint8_t CAN_BS1; 00072 uint8_t CAN_BS2; 00077 FunctionalState CAN_TTCM; 00081 FunctionalState CAN_ABOM; 00085 FunctionalState CAN_AWUM; 00089 FunctionalState CAN_NART; 00093 FunctionalState CAN_RFLM; 00097 FunctionalState CAN_TXFP; 00100 } CAN_InitTypeDef; 00101 00106 typedef struct 00107 { 00108 uint16_t CAN_FilterIdHigh; 00112 uint16_t CAN_FilterIdLow; 00116 uint16_t CAN_FilterMaskIdHigh; 00121 uint16_t CAN_FilterMaskIdLow; 00126 uint16_t CAN_FilterFIFOAssignment; 00129 uint8_t CAN_FilterNumber; 00131 uint8_t CAN_FilterMode; 00134 uint8_t CAN_FilterScale; 00137 FunctionalState CAN_FilterActivation; 00139 } CAN_FilterInitTypeDef; 00140 00145 typedef struct 00146 { 00147 uint32_t StdId; 00150 uint32_t ExtId; 00153 uint8_t IDE; 00157 uint8_t RTR; 00161 uint8_t DLC; 00165 uint8_t Data[8]; 00167 } CanTxMsg; 00168 00173 typedef struct 00174 { 00175 uint32_t StdId; 00178 uint32_t ExtId; 00181 uint8_t IDE; 00185 uint8_t RTR; 00189 uint8_t DLC; 00192 uint8_t Data[8]; 00195 uint8_t FMI; 00198 } CanRxMsg; 00199 00212 #define CAN_InitStatus_Failed ((uint8_t)0x00) 00213 #define CAN_InitStatus_Success ((uint8_t)0x01) 00223 #define CAN_Mode_Normal ((uint8_t)0x00) 00224 #define CAN_Mode_LoopBack ((uint8_t)0x01) 00225 #define CAN_Mode_Silent ((uint8_t)0x02) 00226 #define CAN_Mode_Silent_LoopBack ((uint8_t)0x03) 00228 #define IS_CAN_MODE(MODE) (((MODE) == CAN_Mode_Normal) || \ 00229 ((MODE) == CAN_Mode_LoopBack)|| \ 00230 ((MODE) == CAN_Mode_Silent) || \ 00231 ((MODE) == CAN_Mode_Silent_LoopBack)) 00232 00241 #define CAN_OperatingMode_Initialization ((uint8_t)0x00) 00242 #define CAN_OperatingMode_Normal ((uint8_t)0x01) 00243 #define CAN_OperatingMode_Sleep ((uint8_t)0x02) 00246 #define IS_CAN_OPERATING_MODE(MODE) (((MODE) == CAN_OperatingMode_Initialization) ||\ 00247 ((MODE) == CAN_OperatingMode_Normal)|| \ 00248 ((MODE) == CAN_OperatingMode_Sleep)) 00249 00258 #define CAN_ModeStatus_Failed ((uint8_t)0x00) 00259 #define CAN_ModeStatus_Success ((uint8_t)!CAN_ModeStatus_Failed) 00270 #define CAN_SJW_1tq ((uint8_t)0x00) 00271 #define CAN_SJW_2tq ((uint8_t)0x01) 00272 #define CAN_SJW_3tq ((uint8_t)0x02) 00273 #define CAN_SJW_4tq ((uint8_t)0x03) 00275 #define IS_CAN_SJW(SJW) (((SJW) == CAN_SJW_1tq) || ((SJW) == CAN_SJW_2tq)|| \ 00276 ((SJW) == CAN_SJW_3tq) || ((SJW) == CAN_SJW_4tq)) 00277 00285 #define CAN_BS1_1tq ((uint8_t)0x00) 00286 #define CAN_BS1_2tq ((uint8_t)0x01) 00287 #define CAN_BS1_3tq ((uint8_t)0x02) 00288 #define CAN_BS1_4tq ((uint8_t)0x03) 00289 #define CAN_BS1_5tq ((uint8_t)0x04) 00290 #define CAN_BS1_6tq ((uint8_t)0x05) 00291 #define CAN_BS1_7tq ((uint8_t)0x06) 00292 #define CAN_BS1_8tq ((uint8_t)0x07) 00293 #define CAN_BS1_9tq ((uint8_t)0x08) 00294 #define CAN_BS1_10tq ((uint8_t)0x09) 00295 #define CAN_BS1_11tq ((uint8_t)0x0A) 00296 #define CAN_BS1_12tq ((uint8_t)0x0B) 00297 #define CAN_BS1_13tq ((uint8_t)0x0C) 00298 #define CAN_BS1_14tq ((uint8_t)0x0D) 00299 #define CAN_BS1_15tq ((uint8_t)0x0E) 00300 #define CAN_BS1_16tq ((uint8_t)0x0F) 00302 #define IS_CAN_BS1(BS1) ((BS1) <= CAN_BS1_16tq) 00303 00311 #define CAN_BS2_1tq ((uint8_t)0x00) 00312 #define CAN_BS2_2tq ((uint8_t)0x01) 00313 #define CAN_BS2_3tq ((uint8_t)0x02) 00314 #define CAN_BS2_4tq ((uint8_t)0x03) 00315 #define CAN_BS2_5tq ((uint8_t)0x04) 00316 #define CAN_BS2_6tq ((uint8_t)0x05) 00317 #define CAN_BS2_7tq ((uint8_t)0x06) 00318 #define CAN_BS2_8tq ((uint8_t)0x07) 00320 #define IS_CAN_BS2(BS2) ((BS2) <= CAN_BS2_8tq) 00321 00330 #define IS_CAN_PRESCALER(PRESCALER) (((PRESCALER) >= 1) && ((PRESCALER) <= 1024)) 00331 00339 #ifndef STM32F10X_CL 00340 #define IS_CAN_FILTER_NUMBER(NUMBER) ((NUMBER) <= 13) 00341 #else 00342 #define IS_CAN_FILTER_NUMBER(NUMBER) ((NUMBER) <= 27) 00343 #endif /* STM32F10X_CL */ 00344 00352 #define CAN_FilterMode_IdMask ((uint8_t)0x00) 00353 #define CAN_FilterMode_IdList ((uint8_t)0x01) 00355 #define IS_CAN_FILTER_MODE(MODE) (((MODE) == CAN_FilterMode_IdMask) || \ 00356 ((MODE) == CAN_FilterMode_IdList)) 00357 00365 #define CAN_FilterScale_16bit ((uint8_t)0x00) 00366 #define CAN_FilterScale_32bit ((uint8_t)0x01) 00368 #define IS_CAN_FILTER_SCALE(SCALE) (((SCALE) == CAN_FilterScale_16bit) || \ 00369 ((SCALE) == CAN_FilterScale_32bit)) 00370 00379 #define CAN_Filter_FIFO0 ((uint8_t)0x00) 00380 #define CAN_Filter_FIFO1 ((uint8_t)0x01) 00381 #define IS_CAN_FILTER_FIFO(FIFO) (((FIFO) == CAN_FilterFIFO0) || \ 00382 ((FIFO) == CAN_FilterFIFO1)) 00383 00390 #define IS_CAN_BANKNUMBER(BANKNUMBER) (((BANKNUMBER) >= 1) && ((BANKNUMBER) <= 27)) 00391 00399 #define IS_CAN_TRANSMITMAILBOX(TRANSMITMAILBOX) ((TRANSMITMAILBOX) <= ((uint8_t)0x02)) 00400 #define IS_CAN_STDID(STDID) ((STDID) <= ((uint32_t)0x7FF)) 00401 #define IS_CAN_EXTID(EXTID) ((EXTID) <= ((uint32_t)0x1FFFFFFF)) 00402 #define IS_CAN_DLC(DLC) ((DLC) <= ((uint8_t)0x08)) 00403 00412 #define CAN_Id_Standard ((uint32_t)0x00000000) 00413 #define CAN_Id_Extended ((uint32_t)0x00000004) 00414 #define IS_CAN_IDTYPE(IDTYPE) (((IDTYPE) == CAN_Id_Standard) || \ 00415 ((IDTYPE) == CAN_Id_Extended)) 00416 00424 #define CAN_RTR_Data ((uint32_t)0x00000000) 00425 #define CAN_RTR_Remote ((uint32_t)0x00000002) 00426 #define IS_CAN_RTR(RTR) (((RTR) == CAN_RTR_Data) || ((RTR) == CAN_RTR_Remote)) 00427 00436 #define CAN_TxStatus_Failed ((uint8_t)0x00) 00437 #define CAN_TxStatus_Ok ((uint8_t)0x01) 00438 #define CAN_TxStatus_Pending ((uint8_t)0x02) 00439 #define CAN_TxStatus_NoMailBox ((uint8_t)0x04) 00449 #define CAN_FIFO0 ((uint8_t)0x00) 00450 #define CAN_FIFO1 ((uint8_t)0x01) 00452 #define IS_CAN_FIFO(FIFO) (((FIFO) == CAN_FIFO0) || ((FIFO) == CAN_FIFO1)) 00453 00462 #define CAN_Sleep_Failed ((uint8_t)0x00) 00463 #define CAN_Sleep_Ok ((uint8_t)0x01) 00473 #define CAN_WakeUp_Failed ((uint8_t)0x00) 00474 #define CAN_WakeUp_Ok ((uint8_t)0x01) 00485 #define CAN_ErrorCode_NoErr ((uint8_t)0x00) 00486 #define CAN_ErrorCode_StuffErr ((uint8_t)0x10) 00487 #define CAN_ErrorCode_FormErr ((uint8_t)0x20) 00488 #define CAN_ErrorCode_ACKErr ((uint8_t)0x30) 00489 #define CAN_ErrorCode_BitRecessiveErr ((uint8_t)0x40) 00490 #define CAN_ErrorCode_BitDominantErr ((uint8_t)0x50) 00491 #define CAN_ErrorCode_CRCErr ((uint8_t)0x60) 00492 #define CAN_ErrorCode_SoftwareSetErr ((uint8_t)0x70) 00502 /* If the flag is 0x3XXXXXXX, it means that it can be used with CAN_GetFlagStatus() 00503 and CAN_ClearFlag() functions. */ 00504 /* If the flag is 0x1XXXXXXX, it means that it can only be used with CAN_GetFlagStatus() function. */ 00505 00506 /* Transmit Flags */ 00507 #define CAN_FLAG_RQCP0 ((uint32_t)0x38000001) 00508 #define CAN_FLAG_RQCP1 ((uint32_t)0x38000100) 00509 #define CAN_FLAG_RQCP2 ((uint32_t)0x38010000) 00511 /* Receive Flags */ 00512 #define CAN_FLAG_FMP0 ((uint32_t)0x12000003) 00513 #define CAN_FLAG_FF0 ((uint32_t)0x32000008) 00514 #define CAN_FLAG_FOV0 ((uint32_t)0x32000010) 00515 #define CAN_FLAG_FMP1 ((uint32_t)0x14000003) 00516 #define CAN_FLAG_FF1 ((uint32_t)0x34000008) 00517 #define CAN_FLAG_FOV1 ((uint32_t)0x34000010) 00519 /* Operating Mode Flags */ 00520 #define CAN_FLAG_WKU ((uint32_t)0x31000008) 00521 #define CAN_FLAG_SLAK ((uint32_t)0x31000012) 00522 /* Note: When SLAK intterupt is disabled (SLKIE=0), no polling on SLAKI is possible. 00523 In this case the SLAK bit can be polled.*/ 00524 00525 /* Error Flags */ 00526 #define CAN_FLAG_EWG ((uint32_t)0x10F00001) 00527 #define CAN_FLAG_EPV ((uint32_t)0x10F00002) 00528 #define CAN_FLAG_BOF ((uint32_t)0x10F00004) 00529 #define CAN_FLAG_LEC ((uint32_t)0x30F00070) 00531 #define IS_CAN_GET_FLAG(FLAG) (((FLAG) == CAN_FLAG_LEC) || ((FLAG) == CAN_FLAG_BOF) || \ 00532 ((FLAG) == CAN_FLAG_EPV) || ((FLAG) == CAN_FLAG_EWG) || \ 00533 ((FLAG) == CAN_FLAG_WKU) || ((FLAG) == CAN_FLAG_FOV0) || \ 00534 ((FLAG) == CAN_FLAG_FF0) || ((FLAG) == CAN_FLAG_FMP0) || \ 00535 ((FLAG) == CAN_FLAG_FOV1) || ((FLAG) == CAN_FLAG_FF1) || \ 00536 ((FLAG) == CAN_FLAG_FMP1) || ((FLAG) == CAN_FLAG_RQCP2) || \ 00537 ((FLAG) == CAN_FLAG_RQCP1)|| ((FLAG) == CAN_FLAG_RQCP0) || \ 00538 ((FLAG) == CAN_FLAG_SLAK )) 00539 00540 #define IS_CAN_CLEAR_FLAG(FLAG)(((FLAG) == CAN_FLAG_LEC) || ((FLAG) == CAN_FLAG_RQCP2) || \ 00541 ((FLAG) == CAN_FLAG_RQCP1) || ((FLAG) == CAN_FLAG_RQCP0) || \ 00542 ((FLAG) == CAN_FLAG_FF0) || ((FLAG) == CAN_FLAG_FOV0) ||\ 00543 ((FLAG) == CAN_FLAG_FF1) || ((FLAG) == CAN_FLAG_FOV1) || \ 00544 ((FLAG) == CAN_FLAG_WKU) || ((FLAG) == CAN_FLAG_SLAK)) 00545 00556 #define CAN_IT_TME ((uint32_t)0x00000001) 00558 /* Receive Interrupts */ 00559 #define CAN_IT_FMP0 ((uint32_t)0x00000002) 00560 #define CAN_IT_FF0 ((uint32_t)0x00000004) 00561 #define CAN_IT_FOV0 ((uint32_t)0x00000008) 00562 #define CAN_IT_FMP1 ((uint32_t)0x00000010) 00563 #define CAN_IT_FF1 ((uint32_t)0x00000020) 00564 #define CAN_IT_FOV1 ((uint32_t)0x00000040) 00566 /* Operating Mode Interrupts */ 00567 #define CAN_IT_WKU ((uint32_t)0x00010000) 00568 #define CAN_IT_SLK ((uint32_t)0x00020000) 00570 /* Error Interrupts */ 00571 #define CAN_IT_EWG ((uint32_t)0x00000100) 00572 #define CAN_IT_EPV ((uint32_t)0x00000200) 00573 #define CAN_IT_BOF ((uint32_t)0x00000400) 00574 #define CAN_IT_LEC ((uint32_t)0x00000800) 00575 #define CAN_IT_ERR ((uint32_t)0x00008000) 00577 /* Flags named as Interrupts : kept only for FW compatibility */ 00578 #define CAN_IT_RQCP0 CAN_IT_TME 00579 #define CAN_IT_RQCP1 CAN_IT_TME 00580 #define CAN_IT_RQCP2 CAN_IT_TME 00581 00582 00583 #define IS_CAN_IT(IT) (((IT) == CAN_IT_TME) || ((IT) == CAN_IT_FMP0) ||\ 00584 ((IT) == CAN_IT_FF0) || ((IT) == CAN_IT_FOV0) ||\ 00585 ((IT) == CAN_IT_FMP1) || ((IT) == CAN_IT_FF1) ||\ 00586 ((IT) == CAN_IT_FOV1) || ((IT) == CAN_IT_EWG) ||\ 00587 ((IT) == CAN_IT_EPV) || ((IT) == CAN_IT_BOF) ||\ 00588 ((IT) == CAN_IT_LEC) || ((IT) == CAN_IT_ERR) ||\ 00589 ((IT) == CAN_IT_WKU) || ((IT) == CAN_IT_SLK)) 00590 00591 #define IS_CAN_CLEAR_IT(IT) (((IT) == CAN_IT_TME) || ((IT) == CAN_IT_FF0) ||\ 00592 ((IT) == CAN_IT_FOV0)|| ((IT) == CAN_IT_FF1) ||\ 00593 ((IT) == CAN_IT_FOV1)|| ((IT) == CAN_IT_EWG) ||\ 00594 ((IT) == CAN_IT_EPV) || ((IT) == CAN_IT_BOF) ||\ 00595 ((IT) == CAN_IT_LEC) || ((IT) == CAN_IT_ERR) ||\ 00596 ((IT) == CAN_IT_WKU) || ((IT) == CAN_IT_SLK)) 00597 00605 #define CANINITFAILED CAN_InitStatus_Failed 00606 #define CANINITOK CAN_InitStatus_Success 00607 #define CAN_FilterFIFO0 CAN_Filter_FIFO0 00608 #define CAN_FilterFIFO1 CAN_Filter_FIFO1 00609 #define CAN_ID_STD CAN_Id_Standard 00610 #define CAN_ID_EXT CAN_Id_Extended 00611 #define CAN_RTR_DATA CAN_RTR_Data 00612 #define CAN_RTR_REMOTE CAN_RTR_Remote 00613 #define CANTXFAILE CAN_TxStatus_Failed 00614 #define CANTXOK CAN_TxStatus_Ok 00615 #define CANTXPENDING CAN_TxStatus_Pending 00616 #define CAN_NO_MB CAN_TxStatus_NoMailBox 00617 #define CANSLEEPFAILED CAN_Sleep_Failed 00618 #define CANSLEEPOK CAN_Sleep_Ok 00619 #define CANWAKEUPFAILED CAN_WakeUp_Failed 00620 #define CANWAKEUPOK CAN_WakeUp_Ok 00621 00641 /* Function used to set the CAN configuration to the default reset state *****/ 00642 void CAN_DeInit(CAN_TypeDef* CANx); 00643 00644 /* Initialization and Configuration functions *********************************/ 00645 uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct); 00646 void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct); 00647 void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct); 00648 void CAN_SlaveStartBank(uint8_t CAN_BankNumber); 00649 void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState); 00650 void CAN_TTComModeCmd(CAN_TypeDef* CANx, FunctionalState NewState); 00651 00652 /* Transmit functions *********************************************************/ 00653 uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage); 00654 uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox); 00655 void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox); 00656 00657 /* Receive functions **********************************************************/ 00658 void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage); 00659 void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber); 00660 uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber); 00661 00662 00663 /* Operation modes functions **************************************************/ 00664 uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode); 00665 uint8_t CAN_Sleep(CAN_TypeDef* CANx); 00666 uint8_t CAN_WakeUp(CAN_TypeDef* CANx); 00667 00668 /* Error management functions *************************************************/ 00669 uint8_t CAN_GetLastErrorCode(CAN_TypeDef* CANx); 00670 uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef* CANx); 00671 uint8_t CAN_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx); 00672 00673 /* Interrupts and flags management functions **********************************/ 00674 void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState); 00675 FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG); 00676 void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG); 00677 ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT); 00678 void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT); 00679 00680 #ifdef __cplusplus 00681 } 00682 #endif 00683 00684 #endif /* __STM32F10x_CAN_H */ 00685 00697 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/