CAN (Controller area network) контроллер процессора Cortex-M3


Инициализацию CAN-контроллера можно выполнить с помощью функции стандартной библиотеки (stm32f10x_can.h):

uint8_t  CAN_Init (CAN_TypeDef*  CANx,  CAN_InitTypeDef*  CAN_InitStruct);

где: CANx - интерфейс CAN1;

  CAN_InitStruct - структура с параметрами инициализации:


typedef struct
{
  uint16_t CAN_Prescaler;   /* Specifies the length of a time quantum. 
                                 It ranges from 1 to 1024. */
  
  uint8_t CAN_Mode;         /* Specifies the CAN operating mode.
                                 This parameter can be a value of 
                                @ref CAN_operating_mode */

  uint8_t CAN_SJW;          /* Specifies the maximum number of time quanta 
                                 the CAN hardware is allowed to lengthen or 
                                 shorten a bit to perform resynchronization.
                                 This parameter can be a value of 
                                 @ref CAN_synchronisation_jump_width */

  uint8_t CAN_BS1;          /* Specifies the number of time quanta in Bit 
                                 Segment 1. This parameter can be a value of 
                                 @ref CAN_time_quantum_in_bit_segment_1 */

  uint8_t CAN_BS2;          /* Specifies the number of time quanta in Bit 
                                 Segment 2.
                                 This parameter can be a value of 
                                 @ref CAN_time_quantum_in_bit_segment_2 */
  
  FunctionalState CAN_TTCM; /* Enable or disable the time triggered 
                                 communication mode. This parameter can be set 
                                 either to ENABLE or DISABLE. */
  
  FunctionalState CAN_ABOM;  /* Enable or disable the automatic bus-off 
                                  management. This parameter can be set either 
                                  to ENABLE or DISABLE. */

  FunctionalState CAN_AWUM;  /* Enable or disable the automatic wake-up mode. 
                                  This parameter can be set either to ENABLE or 
                                  DISABLE. */

  FunctionalState CAN_NART;  /* Enable or disable the no-automatic 
                                  retransmission mode. This parameter can be 
                                  set either to ENABLE or DISABLE. */

  FunctionalState CAN_RFLM;  /* Enable or disable the Receive FIFO Locked mode.
                                  This parameter can be set either to ENABLE 
                                  or DISABLE. */

  FunctionalState CAN_TXFP;  /* Enable or disable the transmit FIFO priority.
                                  This parameter can be set either to ENABLE 
                                  or DISABLE. */
} CAN_InitTypeDef;

CAN_Mode - режим работы CAN-контроллера:

#define CAN_Mode_Normal             ((uint8_t)0x00)  /* normal mode */
#define CAN_Mode_LoopBack           ((uint8_t)0x01)  /* loopback mode */
#define CAN_Mode_Silent             ((uint8_t)0x02)  /* silent mode */
#define CAN_Mode_Silent_LoopBack    ((uint8_t)0x03)  /* loopback combined with silent mode */

CAN_SJW - кол-во изменяемых квантов для синхронизации:

#define CAN_SJW_1tq     ((uint8_t)0x00)   /* 1 time quantum */
#define CAN_SJW_2tq     ((uint8_t)0x01)   /* 2 time quantum */
#define CAN_SJW_3tq     ((uint8_t)0x02)   /* 3 time quantum */
#define CAN_SJW_4tq     ((uint8_t)0x03)   /* 4 time quantum */

CAN_BS1 - кол-во квантов первого сегмента минус один:

#define CAN_BS1_1tq      ((uint8_t)0x00)   /* 1 time quantum */
#define CAN_BS1_2tq      ((uint8_t)0x01)   /* 2 time quantum */
#define CAN_BS1_3tq      ((uint8_t)0x02)   /* 3 time quantum */
#define CAN_BS1_4tq      ((uint8_t)0x03)   /* 4 time quantum */
#define CAN_BS1_5tq      ((uint8_t)0x04)   /* 5 time quantum */
#define CAN_BS1_6tq      ((uint8_t)0x05)   /* 6 time quantum */
#define CAN_BS1_7tq      ((uint8_t)0x06)   /* 7 time quantum */
#define CAN_BS1_8tq      ((uint8_t)0x07)   /* 8 time quantum */
#define CAN_BS1_9tq      ((uint8_t)0x08)   /* 9 time quantum */
#define CAN_BS1_10tq     ((uint8_t)0x09)   /* 10 time quantum */
#define CAN_BS1_11tq     ((uint8_t)0x0A)   /* 11 time quantum */
#define CAN_BS1_12tq     ((uint8_t)0x0B)   /* 12 time quantum */
#define CAN_BS1_13tq     ((uint8_t)0x0C)   /* 13 time quantum */
#define CAN_BS1_14tq     ((uint8_t)0x0D)   /* 14 time quantum */
#define CAN_BS1_15tq     ((uint8_t)0x0E)   /* 15 time quantum */
#define CAN_BS1_16tq     ((uint8_t)0x0F)   /* 16 time quantum */

CAN_BS2 - кол-во квантов второго сегмента минус один:

#define CAN_BS2_1tq      ((uint8_t)0x00)   /* 1 time quantum */
#define CAN_BS2_2tq      ((uint8_t)0x01)   /* 2 time quantum */
#define CAN_BS2_3tq      ((uint8_t)0x02)   /* 3 time quantum */
#define CAN_BS2_4tq      ((uint8_t)0x03)   /* 4 time quantum */
#define CAN_BS2_5tq      ((uint8_t)0x04)   /* 5 time quantum */
#define CAN_BS2_6tq      ((uint8_t)0x05)   /* 6 time quantum */
#define CAN_BS2_7tq      ((uint8_t)0x06)   /* 7 time quantum */
#define CAN_BS2_8tq      ((uint8_t)0x07)   /* 8 time quantum */