1 /** 2 ****************************************************************************** 3 * @file HAL_can.h 4 * @author AE Team 5 * @version V1.0.0 6 * @date 28/7/2017 7 * @brief This file contains all the functions prototypes for the BKP firmware 8 * library. 9 ****************************************************************************** 10 * @copy 11 * 12 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 14 * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY 15 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 16 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 17 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 18 * 19 * <h2><center>© COPYRIGHT 2017 MindMotion</center></h2> 20 */ 21 /* Define to prevent recursive inclusion -------------------------------------*/ 22 #ifndef __HAL_CAN_H 23 #define __HAL_CAN_H 24 25 /* Includes ------------------------------------------------------------------*/ 26 #include "HAL_device.h" 27 28 29 /** @defgroup CAN_sleep_constants 30 * @{ 31 */ 32 33 #define CANINITFAILED ((uint8_t)0x00) /* CAN initialization failed */ 34 #define CANINITOK ((uint8_t)0x01) /* CAN initialization ok */ 35 36 /** 37 * @} 38 */ 39 40 41 42 /** @defgroup CAN_sleep_constants 43 * @{ 44 */ 45 46 #define CANSLEEPFAILED ((uint8_t)0x00) /* CAN did not enter the sleep mode */ 47 #define CANSLEEPOK ((uint8_t)0x01) /* CAN entered the sleep mode */ 48 49 /** 50 * @} 51 */ 52 53 54 /** @defgroup CAN_wake_up_constants 55 * @{ 56 */ 57 58 #define CANWAKEUPFAILED ((uint8_t)0x00) /* CAN did not leave the sleep mode */ 59 #define CANWAKEUPOK ((uint8_t)0x01) /* CAN leaved the sleep mode */ 60 61 /** 62 * @} 63 */ 64 65 /** 66 * @brief parasmeter of CAN Mode 67 */ 68 #define CAN_BASICMode ((uint32_t)0x0) 69 #define CAN_PELIMode ((uint32_t)0x80) 70 #define CAN_WorkMode ((uint32_t)0x80) 71 #define CAN_ResetMode ((uint32_t)0x1) 72 #define CAN_ListenOnlyMode ((uint32_t)0x2) 73 #define CAN_SeftTestMode ((uint32_t)0x4) 74 #define CAN_FilterMode_Singal ((uint32_t)0x8) 75 #define CAN_FilterMode_Double ((uint32_t)0xf7) 76 #define CAN_SleepMode ((uint32_t)0x10) 77 78 /** 79 * @} 80 */ 81 82 /** 83 * @brief parasmeter of BASIC CAN interrupt 84 */ 85 #define CAN_IT_RIE ((uint32_t)0x2) 86 #define CAN_IT_TIE ((uint32_t)0x4) 87 #define CAN_IT_EIE ((uint32_t)0x8) 88 #define CAN_IT_OIE ((uint32_t)0x10) 89 90 /** 91 * @} 92 */ 93 94 /** 95 * @brief parasmeter of PELI CAN interrupt 96 */ 97 #define CAN_IT_RI ((uint32_t)0x1) 98 #define CAN_IT_TI ((uint32_t)0x2) 99 #define CAN_IT_EI ((uint32_t)0x4) 100 #define CAN_IT_DOI ((uint32_t)0x8) 101 #define CAN_IT_WUI ((uint32_t)0x10) 102 #define CAN_IT_EPI ((uint32_t)0x20) 103 #define CAN_IT_ALI ((uint32_t)0x40) 104 #define CAN_IT_BEI ((uint32_t)0x80) 105 #define CAN_IT_ALL ((uint32_t)0xff) 106 107 /** 108 * @} 109 */ 110 111 /** 112 * @brief parasmeter of CAN Status 113 */ 114 #define CAN_STATUS_RBS ((uint32_t)0x1) 115 #define CAN_STATUS_DOS ((uint32_t)0x2) 116 #define CAN_STATUS_TBS ((uint32_t)0x4) 117 #define CAN_STATUS_TCS ((uint32_t)0x8) 118 #define CAN_STATUS_RS ((uint32_t)0x10) 119 #define CAN_STATUS_TS ((uint32_t)0x20) 120 #define CAN_STATUS_ES ((uint32_t)0x40) 121 #define CAN_STATUS_BS ((uint32_t)0x80) 122 123 /** 124 * @} 125 */ 126 127 /** 128 * @brief parasmeter of CAN Command register 129 */ 130 #define CAN_TR 0x1 131 #define CAN_AT 0x2 132 #define CAN_RRB 0x4 133 #define CAN_CDO 0x8 134 135 /** 136 * @} 137 */ 138 139 /** 140 * @brief CAN_Basic init structure definition 141 */ 142 typedef struct 143 { 144 uint8_t SJW; 145 uint8_t BRP; 146 FlagStatus SAM; 147 uint8_t TESG2; 148 uint8_t TESG1; 149 FunctionalState GTS; 150 uint8_t CDCLK; 151 uint8_t CLOSE_OPEN_CLK; 152 uint8_t RXINTEN; 153 uint8_t CBP; 154 }CAN_Basic_InitTypeDef; 155 156 /** 157 * @} 158 */ 159 160 /** 161 * @brief CAN_Peli init structure definition 162 */ 163 typedef struct 164 { 165 uint8_t SJW; 166 uint8_t BRP; 167 FlagStatus SAM; 168 uint8_t TESG2; 169 uint8_t TESG1; 170 FunctionalState LOM; 171 FunctionalState STM; 172 FunctionalState SM; 173 FunctionalState SRR; 174 uint32_t EWLR; 175 }CAN_Peli_InitTypeDef; 176 177 /** 178 * @} 179 */ 180 181 /** 182 * @brief CAN_Basic filter init structure definition 183 */ 184 typedef struct 185 { 186 uint8_t CAN_FilterId; /*!< Specifies the filter identification number . 187 This parameter can be a value between 0x00 and 0xFF */ 188 189 uint8_t CAN_FilterMaskId; /*!< Specifies the filter mask number or identification number, 190 This parameter can be a value between 0x00 and 0xFF */ 191 } CAN_Basic_FilterInitTypeDef; 192 193 /** 194 * @} 195 */ 196 197 /** 198 * @brief CAN_Peli filter init structure definition 199 */ 200 typedef struct 201 { 202 uint8_t AFM; 203 uint8_t CAN_FilterId0; /*!< Specifies the filter identification number 204 This parameter can be a value between 0x00 and 0xFF */ 205 uint8_t CAN_FilterId1; 206 uint8_t CAN_FilterId2; 207 uint8_t CAN_FilterId3; 208 209 uint8_t CAN_FilterMaskId0; /*!< Specifies the filter mask number or identification number, 210 This parameter can be a value between 0x00 and 0xFF */ 211 uint8_t CAN_FilterMaskId1; 212 uint8_t CAN_FilterMaskId2; 213 uint8_t CAN_FilterMaskId3; 214 } CAN_Peli_FilterInitTypeDef; 215 216 /** 217 * @} 218 */ 219 220 /** 221 * @brief CAN_Peli transmit frame definition 222 */ 223 typedef enum {DataFrame = 0,RemoteFrame = !DataFrame}TransFrame; 224 225 /** 226 * @} 227 */ 228 229 /** 230 * @brief CAN_Basic Tx message structure definition 231 */ 232 typedef struct 233 { 234 uint8_t IDH; /*!< Specifies the standard high identifier. 235 This parameter can be a value between 0 to 0xFF. */ 236 uint8_t IDL; /*!< Specifies the standard low identifier. 237 This parameter can be a value between 0 to 0x7. */ 238 uint8_t RTR; /*!< Specifies the type of frame for the message that will 239 be transmitted. This parameter can be @TransFrame */ 240 241 uint8_t DLC; /*!< Specifies the length of the frame that will be 242 transmitted. This parameter can be a value between 243 0 to 8 */ 244 245 uint8_t Data[8]; /*!< Contains the data to be transmitted. It ranges from 0 246 to 0xFF. */ 247 } CanBasicTxMsg; 248 249 /** 250 * @} 251 */ 252 253 /** 254 * @brief CAN_Basic Rx message structure definition 255 */ 256 typedef struct 257 { 258 uint16_t ID; /*!< Specifies the standard identifier. 259 This parameter can be a value between 0 to 0x7FF. */ 260 uint8_t RTR; /*!< Specifies the type of frame for the received message. 261 This parameter can be a value of 262 @ref TransFrame */ 263 264 uint8_t DLC; /*!< Specifies the length of the frame that will be received. 265 This parameter can be a value between 0 to 8 */ 266 267 uint8_t Data[8]; /*!< Contains the data to be received. It ranges from 0 to 268 0xFF. */ 269 270 } CanBasicRxMsg; 271 272 /** 273 * @} 274 */ 275 276 /** 277 * @brief CAN_Peli_Tx message structure definition 278 */ 279 typedef struct 280 { 281 uint8_t IDLL; /*!< Specifies the extended identifier. 282 This parameter can be a value between 0 to 0xFF. */ 283 uint8_t IDLH; 284 uint8_t IDHL; 285 uint8_t IDHH; 286 uint8_t FF; /*!< Specifies the type of identifier for the message that 287 will be transmitted. This parameter can be a value 288 of @ref CAN_identifier_type */ 289 290 uint8_t RTR; /*!< Specifies the type of frame for the message that will 291 be transmitted. This parameter can be a value of 292 @ref TransFrame */ 293 294 uint8_t DLC; /*!< Specifies the length of the frame that will be 295 transmitted. This parameter can be a value between 296 0 to 8 */ 297 298 uint8_t Data[8]; /*!< Contains the data to be transmitted. It ranges from 0 299 to 0xFF. */ 300 } CanPeliTxMsg; 301 302 /** 303 * @} 304 */ 305 306 /** 307 * @brief CAN Rx message structure definition 308 */ 309 typedef struct 310 { 311 uint32_t ID; /*!< Specifies the extended identifier. 312 This parameter can be a value between 0 to 0x1FFFFFFF. */ 313 uint8_t FF; /*!< Specifies the type of identifier for the message that 314 will be received. This parameter can be a value of 315 @ref CAN_identifier_type */ 316 317 uint8_t RTR; /*!< Specifies the type of frame for the received message. 318 This parameter can be a value of 319 @ref TransFrame */ 320 321 uint8_t DLC; /*!< Specifies the length of the frame that will be received. 322 This parameter can be a value between 0 to 8 */ 323 324 uint8_t Data[8]; /*!< Contains the data to be received. It ranges from 0 to 325 0xFF. */ 326 327 } CanPeliRxMsg; 328 329 330 331 332 #define CANTXFAILED ((uint8_t)0x00) /* CAN transmission failed */ 333 #define CANTXOK ((uint8_t)0x01) /* CAN transmission succeeded */ 334 #define CANTXPENDING ((uint8_t)0x02) /* CAN transmission pending */ 335 #define CAN_NO_MB ((uint8_t)0x04) /* CAN cell did not provide an empty mailbox */ 336 337 338 /************************ Basic and Peli Work all need function ********************/ 339 340 void CAN_Mode_Cmd(CAN_TypeDef* CANx, uint32_t CAN_MODE); 341 void CAN_ResetMode_Cmd(CAN_TypeDef* CANx,FunctionalState NewState); 342 void CAN_ClearDataOverflow(CAN_TypeDef* CANx); 343 void CAN_ClearITPendingBit(CAN_TypeDef* CANx); 344 345 346 /************************ Basic Work function ********************/ 347 void CAN_DeInit(CAN_TypeDef* CANx); 348 uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_Basic_InitTypeDef* CAN_Basic_InitStruct); 349 void CAN_FilterInit(CAN_Basic_FilterInitTypeDef* CAN_Basic_FilterInitStruct); 350 void CAN_StructInit(CAN_Basic_InitTypeDef* CAN_Basic_InitStruct); 351 void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState Newstate); 352 uint8_t CAN_Transmit(CAN_TypeDef* CANx,CanBasicTxMsg* BasicTxMessage); 353 void CAN_CancelTransmit(CAN_TypeDef* CANx); 354 void CAN_FIFORelease(CAN_TypeDef* CANx); 355 void CAN_Receive(CAN_TypeDef* CANx,CanBasicRxMsg* BasicRxMessage); 356 uint8_t CAN_Sleep(CAN_TypeDef* CANx); 357 uint8_t CAN_WakeUp(CAN_TypeDef* CANx); 358 FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx,uint32_t CAN_FLAG); 359 ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT); 360 361 362 /************************ Peli Work function *********************/ 363 void CAN_Peli_SleepMode_Cmd(FunctionalState NewState); 364 void CAN_Peli_Init(CAN_Peli_InitTypeDef* CAN_InitStruct); 365 void CAN_Peli_StructInit(CAN_Peli_InitTypeDef* CAN_Peli_InitStruct); 366 void CAN_Peli_FilterInit(CAN_Peli_FilterInitTypeDef* CAN_Peli_FilterInitStruct); 367 void CAN_Peli_FilterStructInit(CAN_Peli_FilterInitTypeDef* CAN_Peli_FilterInitStruct); 368 void CAN_Peli_Transmit(CanPeliTxMsg* PeliTxMessage); 369 void CAN_Peli_TransmitRepeat(CanPeliTxMsg* PeliTxMessage); 370 void CAN_Peli_Receive(CanPeliRxMsg* PeliRxMessage); 371 uint32_t CAN_Peli_GetRxFIFOInfo(void); 372 uint8_t CAN_Peli_GetLastErrorCode(void); 373 uint8_t CAN_Peli_GetReceiveErrorCounter(void); 374 uint8_t CAN_Peli_GetLSBTransmitErrorCounter(void); 375 void CAN_Peli_ITConfig(uint32_t CAN_IT, FunctionalState NewState); 376 ITStatus CAN_Peli_GetITStatus(uint32_t CAN_IT); 377 void CAN_AutoCfg_BaudParam(CAN_Peli_InitTypeDef *CAN_Peli_InitStruct,unsigned int SrcClk,unsigned int baud ); 378 #endif /* __HAL_CAN_H */ 379 /** 380 * @} 381 */ 382 383 /** 384 * @} 385 */ 386 387 /** 388 * @} 389 */ 390 391 /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/ 392 393