gd32f10x_can.h 18.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
/**
  ******************************************************************************
  * @brief   CAN header file of the firmware library.
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __GD32F10X_CAN_H
#define __GD32F10X_CAN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "gd32f10x.h"

/** @addtogroup GD32F10x_Firmware
  * @{
  */

/** @addtogroup CAN
  * @{
  */

/** @defgroup CAN_Exported_Types
  * @{
  */

/**
  * @brief  CAN Initial Parameters
  */

typedef struct {
    uint16_t CAN_Prescaler;   /*!< Baud rate prescaler.It can be 1 to 1024. */

    uint8_t CAN_Mode;         /*!< Set the CAN operating mode. */

    uint8_t CAN_SJW;          /*!< Set Resynchronization jump width */

    uint8_t CAN_BS1;          /*!< Set time segment 1. */

    uint8_t CAN_BS2;          /*!< Set time segment 2. */

    TypeState CAN_TTC;       /*!< Set time triggered communication mode. */

    TypeState CAN_ABOR;       /*!< Set automatic bus-off recovery. */

    TypeState CAN_AWK;       /*!< Set the automatic wake-up mode. */

    TypeState CAN_ARD;       /*!< Set the automatic retransmission mode. */

    TypeState CAN_RFOD;       /*!< Receive FIFO overwrite mode. */

    TypeState CAN_TFO;       /*!< Set the Transmit FIFO order. */
} CAN_InitPara;

/**
  * @brief  CAN filter Initial Parameters
  */

typedef struct {
    uint16_t CAN_FilterListHigh;         /*!< the filter List number.
                                              If the filter scale is 32-bit,the filter list number is MSBs.
                                              If the filter scale is 16-bit,the filter list number is first one.
                                              This value can take from 0x0000 to 0xFFFF */

    uint16_t CAN_FilterListLow;          /*!< the filter List number.
                                              If the filter scale is 32-bit,the filter list number is LSBs.
                                              If the filter scale is 16-bit,the filter list number is second one.
                                              This value can take from 0x0000 to 0xFFFF */

    uint16_t CAN_FilterMaskListHigh;     /*!< the filter mask number or list number,according to the filter mode.
                                              If the filter scale is 32-bit,the filter mask number or list number is MSBs.
                                              If the filter scale is 16-bit,the filter mask number or list number is first one.
                                              This value can take from 0x0000 to 0xFFFF */

    uint16_t CAN_FilterMaskListLow;      /*!< the filter mask number or list number,according to the filter mode.
                                              If the filter scale is 32-bit,the filter mask number or list number is LSBs.
                                              If the filter scale is 16-bit,the filter mask number or list number is second one.
                                              This value can take from 0x0000 to 0xFFFF */

    uint16_t CAN_FilterFIFOAssociation; /*!< Set the FIFO (0 or 1) which will be associated with the filter. */

    uint8_t CAN_FilterNumber;          /*!< Set the which filter to be initialized. It ranges from 0 to 13 or 0 to 27(in GD32F10X_CL). */

    uint8_t CAN_FilterMode;            /*!< Set the filter mode to be identifier mask or identifier list. */

    uint8_t CAN_FilterScale;           /*!< Set the filter scale. */

    TypeState CAN_FilterWork;    /*!< Set the filter to work or not. */
} CAN_FilterInitPara;

/**
  * @brief  CAN Tx message structure definition
  */

typedef struct {
    uint32_t StdId;  /*!< Set the standard format frame identifier.
                        This value can take from 0 to 0x7FF. */

    uint32_t ExtId;  /*!< Set the extended format frame identifier.
                        This value can take from 0 to 0x1FFFFFFF. */

    uint8_t FF;     /*!< Set the format of Frame , Standard or extended Frame
                        for the message that will be transmitted. */

    uint8_t FT;     /*!< Set the type of frame, Data or remote
                        for the message that will be transmitted. */

    uint8_t DLC;     /*!< Set the length of the frame that will be transmitted.
                        This value can take from 0 to 8 */

    uint8_t Data[8]; /*!< store the data that will be transmitted.
                        This value can take from 0 to 0xFF. */
} CanTxMessage;

/**
  * @brief  CAN Rx message structure definition
  */

typedef struct {
    uint32_t StdId;  /*!< Set the standard format frame identifier.
                        This value can take from 0 to 0x7FF. */

    uint32_t ExtId;  /*!< Set the extended format frame identifier.
                        This value can take from 0 to 0x1FFFFFFF. */

    uint8_t FF;     /*!< Set the format of Frame , Standard or extended Frame
                        for the message that will be received. */

    uint8_t FT;     /*!< Set the type of frame, Data or remote
                        for the message that will be received. */

    uint8_t DLC;     /*!< Set the length of the frame that will be received.
                        This value can take from 0 to 8 */

    uint8_t Data[8]; /*!< store the data that will be received.
                        This value can take from 0 to 0xFF. */

    uint8_t FI;     /*!< The index of the filter by which the frame is passed..
                        This value can take from 0 to 0xFF */
} CanRxMessage;

/**
  * @}
  */

/** @defgroup CAN_Exported_Constants
  * @{
  */

/** @defgroup CAN_sleep_constants
  * @{
  */
#define CAN_INITSTATE_FAILED              ((uint8_t)0x00) /*!< CAN initialization failed */
#define CAN_INITSTATE_SUCCESS             ((uint8_t)0x01) /*!< CAN initialization OK */

/**
  * @}
  */

/** @defgroup CAN_Communication_Mode
  * @{
  */
#define CAN_MODE_NORMAL             ((uint8_t)0x00)  /*!< Normal communication mode */
#define CAN_MODE_LOOPBACK           ((uint8_t)0x01)  /*!< Loopback communication mode */
#define CAN_MODE_SILENT             ((uint8_t)0x02)  /*!< Silent communication mode */
#define CAN_MODE_SILENT_LOOPBACK    ((uint8_t)0x03)  /*!< Loopback and silent communication mode */

/**
  * @}
  */


/**
  * @defgroup CAN_Working_Mode
  * @{
  */
#define CAN_WORKINGMODE_INITIAL         ((uint8_t)0x00) /*!< Initial working mode */
#define CAN_WORKINGMODE_NORMAL          ((uint8_t)0x01) /*!< Normal working mode */
#define CAN_WORKINGMODE_SLEEP           ((uint8_t)0x02) /*!< Sleep working mode */

/**
  * @}
  */


/**
  * @defgroup CAN_Mode_State
  * @{
  */
#define CAN_MODESTATE_FAILED    ((uint8_t)0x00)                    /*!< CAN entering the specified mode failed */
#define CAN_MODESTATE_SUCCESS   ((uint8_t)!CAN_MODESTATE_FAILED)   /*!< CAN entering the specified mode Succeed */

/**
  * @}
  */

/** @defgroup CAN_Synchronisation_Jump_Width
  * @{
  */
#define CAN_SJW_1TQ                 ((uint8_t)0x00)  /*!< 1 time quanta */
#define CAN_SJW_2TQ                 ((uint8_t)0x01)  /*!< 2 time quanta */
#define CAN_SJW_3TQ                 ((uint8_t)0x02)  /*!< 3 time quanta */
#define CAN_SJW_4TQ                 ((uint8_t)0x03)  /*!< 4 time quanta */

/**
  * @}
  */

/** @defgroup CAN_Time_Quanta_In_Bit_Segment_1
  * @{
  */
#define CAN_BS1_1TQ                 ((uint8_t)0x00)  /*!< 1 time quanta */
#define CAN_BS1_2TQ                 ((uint8_t)0x01)  /*!< 2 time quanta */
#define CAN_BS1_3TQ                 ((uint8_t)0x02)  /*!< 3 time quanta */
#define CAN_BS1_4TQ                 ((uint8_t)0x03)  /*!< 4 time quanta */
#define CAN_BS1_5TQ                 ((uint8_t)0x04)  /*!< 5 time quanta */
#define CAN_BS1_6TQ                 ((uint8_t)0x05)  /*!< 6 time quanta */
#define CAN_BS1_7TQ                 ((uint8_t)0x06)  /*!< 7 time quanta */
#define CAN_BS1_8TQ                 ((uint8_t)0x07)  /*!< 8 time quanta */
#define CAN_BS1_9TQ                 ((uint8_t)0x08)  /*!< 9 time quanta */
#define CAN_BS1_10TQ                ((uint8_t)0x09)  /*!< 10 time quanta */
#define CAN_BS1_11TQ                ((uint8_t)0x0A)  /*!< 11 time quanta */
#define CAN_BS1_12TQ                ((uint8_t)0x0B)  /*!< 12 time quanta */
#define CAN_BS1_13TQ                ((uint8_t)0x0C)  /*!< 13 time quanta */
#define CAN_BS1_14TQ                ((uint8_t)0x0D)  /*!< 14 time quanta */
#define CAN_BS1_15TQ                ((uint8_t)0x0E)  /*!< 15 time quanta */
#define CAN_BS1_16TQ                ((uint8_t)0x0F)  /*!< 16 time quanta */

/**
  * @}
  */

/** @defgroup CAN_Time_Quanta_In_Bit_Segment_2
  * @{
  */
#define CAN_BS2_1TQ                 ((uint8_t)0x00)  /*!< 1 time quanta */
#define CAN_BS2_2TQ                 ((uint8_t)0x01)  /*!< 2 time quanta */
#define CAN_BS2_3TQ                 ((uint8_t)0x02)  /*!< 3 time quanta */
#define CAN_BS2_4TQ                 ((uint8_t)0x03)  /*!< 4 time quanta */
#define CAN_BS2_5TQ                 ((uint8_t)0x04)  /*!< 5 time quanta */
#define CAN_BS2_6TQ                 ((uint8_t)0x05)  /*!< 6 time quanta */
#define CAN_BS2_7TQ                 ((uint8_t)0x06)  /*!< 7 time quanta */
#define CAN_BS2_8TQ                 ((uint8_t)0x07)  /*!< 8 time quanta */

/**
  * @}
  */


/** @defgroup CAN_Filter_Mode
  * @{
  */
#define CAN_FILTERMODE_MASK            ((uint8_t)0x00)  /*!< mask mode */
#define CAN_FILTERMODE_LIST            ((uint8_t)0x01)  /*!< List mode */

/**
  * @}
  */

/** @defgroup CAN_Filter_Scale
  * @{
  */
#define CAN_FILTERSCALE_16BIT       ((uint8_t)0x00) /*!< Two 16-bit filters */
#define CAN_FILTERSCALE_32BIT       ((uint8_t)0x01) /*!< One 32-bit filter */

/**
  * @}
  */

/** @defgroup CAN_Filter_FIFO
  * @{
  */
#define CAN_FILTER_FIFO0             ((uint8_t)0x00)  /*!< Filter associated with FIFO0 */
#define CAN_FILTER_FIFO1             ((uint8_t)0x01)  /*!< Filter associated with FIFO1 */

/**
  * @}
  */

/** @defgroup CAN_Format_Frame
  * @{
  */
#define CAN_FF_STANDARD             ((uint32_t)0x00000000)  /*!< Standard format frame */
#define CAN_FF_EXTENDED             ((uint32_t)0x00000004)  /*!< Extended format frame */

/**
  * @}
  */

/** @defgroup CAN_Frame_Type
  * @{
  */
#define CAN_FT_DATA                ((uint32_t)0x00000000)  /*!< Data frame */
#define CAN_FT_REMOTE              ((uint32_t)0x00000002)  /*!< Remote frame */

/**
  * @}
  */

/** @defgroup CAN_Transmit_State
  * @{
  */
#define CAN_TXSTATE_FAILED         ((uint8_t)0x00)/*!< CAN transmitted failure */
#define CAN_TXSTATE_OK             ((uint8_t)0x01) /*!< CAN transmitted success */
#define CAN_TXSTATE_PENDING        ((uint8_t)0x02) /*!< CAN transmitted pending */
#define CAN_TXSTATE_NOMAILBOX      ((uint8_t)0x04) /*!< No empty mailbox to be used for CAN */

/**
  * @}
  */

/** @defgroup CAN_Receive_FIFO_Number_Constants
  * @{
  */
#define CAN_FIFO0                 ((uint8_t)0x00) /*!< Use CAN FIFO 0 to receive */
#define CAN_FIFO1                 ((uint8_t)0x01) /*!< Use CAN FIFO 1 to receive */

/**
  * @}
  */

/** @defgroup CAN_Sleep_Constants
  * @{
  */
#define CAN_SLEEP_FAILED     ((uint8_t)0x00) /*!< CAN entered the sleep mode failed */
#define CAN_SLEEP_OK         ((uint8_t)0x01) /*!< CAN entered the sleep mode succeeded */

/**
  * @}
  */

/** @defgroup CAN_Wake_Wp_Constants
  * @{
  */
#define CAN_WAKEUP_FAILED        ((uint8_t)0x00) /*!< CAN leaved the sleep mode failed */
#define CAN_WAKEUP_OK            ((uint8_t)0x01) /*!< CAN leaved the sleep mode succeeded */

/**
  * @}
  */

/**
  * @defgroup   CAN_Error_Type_Constants
  * @{
  */
#define CAN_ERRORTYPE_NOERR           ((uint8_t)0x00) /*!< No Error */
#define CAN_ERRORTYPE_STUFFERR        ((uint8_t)0x10) /*!< Stuff Error */
#define CAN_ERRORTYPE_FORMERR         ((uint8_t)0x20) /*!< Form Error */
#define CAN_ERRORTYPE_ACKERR          ((uint8_t)0x30) /*!< Acknowledgment Error */
#define CAN_ERRORTYPE_BITRECESSIVEERR ((uint8_t)0x40) /*!< Bit Recessive Error */
#define CAN_ERRORTYPE_BITDOMINANTERR  ((uint8_t)0x50) /*!< Bit Dominant Error */
#define CAN_ERRORTYPE_CRCERR          ((uint8_t)0x60) /*!< CRC Error  */
#define CAN_ERRORTYPE_SOFTWARESETERR  ((uint8_t)0x70) /*!< Set by software */

/**
  * @}
  */

/** @defgroup CAN_flags
  * @{
  */
/* The flag value of 0x3XXXXXXX is used with CAN_GetBitState()and CAN_ClearBitState() functions. */
/* The flag value of 0x1XXXXXXX is only used with CAN_GetBitState() function.  */

/* Transmit Flags */
#define CAN_FLAG_MTF0              ((uint32_t)0x38000001) /*!< Mailbox 0 transmit finished Flag */
#define CAN_FLAG_MTF1              ((uint32_t)0x38000100) /*!< Mailbox 1 transmit finished Flag */
#define CAN_FLAG_MTF2              ((uint32_t)0x38010000) /*!< Mailbox 2 transmit finished Flag */

/* Receive Flags */
#define CAN_FLAG_RFL0              ((uint32_t)0x12000003) /*!< the length of the receive FIFO0 Flag */
#define CAN_FLAG_RFF0              ((uint32_t)0x32000008) /*!< Receive FIFO 0 full Flag */
#define CAN_FLAG_RFO0              ((uint32_t)0x32000010) /*!< Receive FIFO 0 overfull  Flag */
#define CAN_FLAG_RFL1              ((uint32_t)0x14000003) /*!< the length of the receive FIFO1 Flag */
#define CAN_FLAG_RFF1              ((uint32_t)0x34000008) /*!< Receive FIFO 1 full Flag */
#define CAN_FLAG_RFO1              ((uint32_t)0x34000010) /*!< Receive FIFO 1 overfull Flag */

/* Working Mode Flags, in CAN status register */
#define CAN_FLAG_WU                ((uint32_t)0x31000008) /*!< Wake up Flag */
#define CAN_FLAG_SLP               ((uint32_t)0x31000012) /*!< Sleep working state Flag */

/* Error Flags, in CAN error register  */
#define CAN_FLAG_WE                ((uint32_t)0x10F00001) /*!< Warning error Flag */
#define CAN_FLAG_PE                ((uint32_t)0x10F00002) /*!< Passive error Flag */
#define CAN_FLAG_BOE               ((uint32_t)0x10F00004) /*!< Bus-off error Flag */
#define CAN_FLAG_ET                ((uint32_t)0x30F00070) /*!< Error type Flag  */

/**
  * @}
  */


/** @defgroup CAN_interrupts
  * @{
  */
#define CAN_INT_TME                  ((uint32_t)0x00000001) /*!< Transmit mailbox empty Interrupt*/

/* Receive Interrupts */
#define CAN_INT_RFNE0                ((uint32_t)0x00000002) /*!< FIFO 0 not empty interrupt */
#define CAN_INT_RFF0                 ((uint32_t)0x00000004) /*!< FIFO 0 full Interrupt*/
#define CAN_INT_RFO0                 ((uint32_t)0x00000008) /*!< FIFO 0 overrun Interrupt*/
#define CAN_INT_RFNE1                ((uint32_t)0x00000010) /*!< FIFO 1 not empty interrupt*/
#define CAN_INT_RFF1                 ((uint32_t)0x00000020) /*!< FIFO 1 full Interrupt*/
#define CAN_INT_RFO1                 ((uint32_t)0x00000040) /*!< FIFO 1 overfull Interrupt*/

/* Working Mode Interrupts */
#define CAN_INT_WU                   ((uint32_t)0x00010000) /*!< Wake-up Interrupt*/
#define CAN_INT_SLP                  ((uint32_t)0x00020000) /*!< Sleep Interrupt*/

/* Error Interrupts */
#define CAN_INT_WE                   ((uint32_t)0x00000100) /*!< Warning error Interrupt*/
#define CAN_INT_PE                   ((uint32_t)0x00000200) /*!< Passive error Interrupt*/
#define CAN_INT_BOE                  ((uint32_t)0x00000400) /*!< Bus-off error Interrupt*/
#define CAN_INT_ET                   ((uint32_t)0x00000800) /*!< error type Interrupt*/
#define CAN_INT_ERR                  ((uint32_t)0x00008000) /*!< Error Interrupt*/

#define CAN_INT_MTF0   CAN_INT_TME
#define CAN_INT_MTF1   CAN_INT_TME
#define CAN_INT_MTF2   CAN_INT_TME

/**
  * @}
  */

/** @defgroup CAN_Legacy
  * @{
  */
#define CANINITFAILED               CAN_INITSTATE_FAILED
#define CANINITOK                   CAN_INITSTATE_SUCCESS
#define CAN_FILTERFIFO0             CAN_FILTER_FIFO0
#define CAN_FILTERFIFO1             CAN_FILTER_FIFO1
#define CAN_FF_STD                  CAN_FF_STANDARD
#define CAN_FF_EXT                  CAN_FF_EXTENDED
//#define CAN_FT_DATA                 CAN_FT_DATA
//#define CAN_FT_REMOTE               CAN_FT_REMOTE
#define CANTXFAILE                  CAN_TXSTATE_FAILED
#define CANTXOK                     CAN_TXSTATE_OK
#define CANTXPENDING                CAN_TXSTATE_PENDING
#define CAN_NO_MB                   CAN_TXSTATE_NOMAILBOX
#define CANSLEEPFAILED              CAN_SLEEP_FAILED
#define CANSLEEPOK                  CAN_SLEEP_OK
#define CANWAKEUPFAILED             CAN_WAKEUP_FAILED
#define CANWAKEUPOK                 CAN_WAKEUP_OK

/**
  * @}
  */

/**
  * @}
  */


/** @defgroup CAN_Exported_Functions
  * @{
  */
void CAN_DeInit(CAN_TypeDef *CANx);
uint8_t CAN_Init(CAN_TypeDef *CANx, CAN_InitPara *CAN_InitParaStruct);
void CAN_FilterInit(CAN_FilterInitPara *CAN_FilterInitParaStruct);
void CAN_StructInit(CAN_InitPara *CAN_InitParaStruct);
void CAN_HeaderBank(uint8_t CAN_HeaderBankNumber);
void CAN_DebugFreeze(CAN_TypeDef *CANx, TypeState NewValue);
void CAN_TimeTrigComMode_Enable(CAN_TypeDef *CANx, TypeState NewValue);

/* Transmit functions */
uint8_t CAN_Transmit(CAN_TypeDef *CANx, CanTxMessage *TxMessage);
uint8_t CAN_TransmitState(CAN_TypeDef *CANx, uint8_t TransmitMailbox);
void CAN_StopTransmit(CAN_TypeDef *CANx, uint8_t Mailbox);

/* Receive functions */
void CAN_Receive(CAN_TypeDef *CANx, uint8_t FIFONumber, CanRxMessage *RxMessage);
void CAN_FIFODequeue(CAN_TypeDef *CANx, uint8_t FIFONumber);
uint8_t CAN_MessageLength(CAN_TypeDef *CANx, uint8_t FIFONumber);


/* Working modes functions */
uint8_t CAN_WorkingMode(CAN_TypeDef *CANx, uint8_t CAN_WorkingMode);
uint8_t CAN_EnterSleep(CAN_TypeDef *CANx);
uint8_t CAN_WakeUp(CAN_TypeDef *CANx);

/* Error management functions */
uint8_t CAN_GetErrorType(CAN_TypeDef *CANx);
uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef *CANx);
uint8_t CAN_GetTransmitErrorCounter(CAN_TypeDef *CANx);

/* Interrupts and flags management functions */
void CAN_INTConfig(CAN_TypeDef *CANx, uint32_t CAN_INT, TypeState NewValue);
TypeState CAN_GetBitState(CAN_TypeDef *CANx, uint32_t CAN_FLAG);
void CAN_ClearBitState(CAN_TypeDef *CANx, uint32_t CAN_FLAG);
TypeState CAN_GetIntBitState(CAN_TypeDef *CANx, uint32_t CAN_INT);
void CAN_ClearIntBitState(CAN_TypeDef *CANx, uint32_t CAN_INT);
static TypeState CheckINTState(uint32_t CAN_Reg, uint32_t Int_Bit);

#ifdef __cplusplus
}
#endif

#endif /* __GD32F10x_CAN_H */
/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */