GD32F4xx CAN
8.例子
void can_gpio_config(void)
{
/* enable CAN clock */
rcu_periph_clock_enable(RCU_CAN0);
rcu_periph_clock_enable(RCU_CAN1);
rcu_periph_clock_enable(RCU_GPIOB);
/* configure CAN1 GPIO */
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_5);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_6);
/* configure CAN0 GPIO */
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_8);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_9);
/* configure CAN0 NVIC */
nvic_irq_enable(CAN1_RX0_IRQn,0,0);
/* configure CAN1 NVIC */
nvic_irq_enable(CAN0_RX0_IRQn,1,1);
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INT_RFNE0);
can_interrupt_enable(CAN1, CAN_INT_RFNE0);
}
void vCan0Can1Init(void)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
can_gpio_config();
/* initialize CAN register */
can_deinit(CAN0);
can_deinit(CAN1);
/* initialize CAN parameters */
// can_parameter.time_triggered = DISABLE;
// can_parameter.auto_bus_off_recovery = ENABLE;
// can_parameter.auto_wake_up = DISABLE;
// can_parameter.auto_retrans = ENABLE;
// can_parameter.rec_fifo_overwrite = DISABLE;
// can_parameter.trans_fifo_order = DISABLE;
/* initialize CAN */
can_parameter.time_triggered = DISABLE;
can_parameter.auto_bus_off_recovery = ENABLE;
can_parameter.auto_wake_up = DISABLE;
can_parameter.auto_retrans = DISABLE;
can_parameter.rec_fifo_overwrite = ENABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_7TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
can_parameter.prescaler = 40;
/* initialize CAN */
can_init(CAN0, &can_parameter);
can_init(CAN1, &can_parameter);
/* initialize filter */
can_filter.filter_number=0;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
can_filter.filter_list_high = 0x0000;
can_filter.filter_list_low = 0x0000;
can_filter.filter_mask_high = 0x0000;
can_filter.filter_mask_low = 0x0000;
can_filter.filter_fifo_number = CAN_FIFO0;
can_filter.filter_enable = ENABLE;
can_filter_init(&can_filter);
/* CAN1 filter number */
can_filter.filter_number = 15;
can_filter_init(&can_filter);
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INT_RFNE0);
can_interrupt_enable(CAN1, CAN_INT_RFNE0);
}
1.外设寄存器说明
2.外设库函数说明
3.结构体
4.CAN外设库使用到的各类结构体初始化
can_parameter_struct can_init;
can_struct_para_init (CAN_INIT_STRUCT, &can_init);
5.初始化外设CAN
先决条件 can_struct_para_init()
/* initialize CAN */
can_parameter.time_triggered = DISABLE;
can_parameter.auto_bus_off_recovery = ENABLE;
can_parameter.auto_wake_up = DISABLE;
can_parameter.auto_retrans = DISABLE;
can_parameter.rec_fifo_overwrite = ENABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE; //工作模式
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ; //再同步补偿宽度
can_parameter.time_segment_1 = CAN_BT_BS1_7TQ; //位段1
can_parameter.time_segment_2 = CAN_BT_BS2_2TQ; //位段2
/* baudrate 125k bps */
can_parameter.prescaler = 40; //波特率分频系数
can_init(CAN0, &can_parameter);
6.CAN过滤器初始化
先决条件 can_struct_para_init()
/* initialize filter */
/* CAN0 filter number */
can_filter.filter_number = 0;
/* initialize filter */
can_filter.filter_mode = CAN_FILTERMODE_MASK;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
can_filter.filter_list_high = 0; //(listid>>16)&0xFFFF;
can_filter.filter_list_low = 0; //(listid>>0)&0xFFFF;
can_filter.filter_mask_high = 0; //(maskid>>16)&0xFFFF;
can_filter.filter_mask_low = 0; //(maskid>>0)&0xFFFF;
can_filter.filter_fifo_number = CAN_FIFO0;
can_filter.filter_enable = ENABLE;
can_filter_init(&can_filter);
7.CAN中断使能
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INT_RFNE0);
can_interrupt_enable(CAN1, CAN_INT_RFNE0);
8.例子
void can_gpio_config(void)
{
/* enable CAN clock */
rcu_periph_clock_enable(RCU_CAN0);
rcu_periph_clock_enable(RCU_CAN1);
rcu_periph_clock_enable(RCU_GPIOB);
/* configure CAN1 GPIO */
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_5);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_6);
/* configure CAN0 GPIO */
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_8);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_9);
/* configure CAN0 NVIC */
nvic_irq_enable(CAN1_RX0_IRQn,0,0);
/* configure CAN1 NVIC */
nvic_irq_enable(CAN0_RX0_IRQn,1,1);
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INT_RFNE0);
can_interrupt_enable(CAN1, CAN_INT_RFNE0);
}
void vCan0Can1Init(void)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
can_gpio_config();
/* initialize CAN register */
can_deinit(CAN0);
can_deinit(CAN1);
/* initialize CAN parameters */
// can_parameter.time_triggered = DISABLE;
// can_parameter.auto_bus_off_recovery = ENABLE;
// can_parameter.auto_wake_up = DISABLE;
// can_parameter.auto_retrans = ENABLE;
// can_parameter.rec_fifo_overwrite = DISABLE;
// can_parameter.trans_fifo_order = DISABLE;
/* initialize CAN */
can_parameter.time_triggered = DISABLE;
can_parameter.auto_bus_off_recovery = ENABLE;
can_parameter.auto_wake_up = DISABLE;
can_parameter.auto_retrans = DISABLE;
can_parameter.rec_fifo_overwrite = ENABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_7TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
can_parameter.prescaler = 40;
/* initialize CAN */
can_init(CAN0, &can_parameter);
can_init(CAN1, &can_parameter);
/* initialize filter */
can_filter.filter_number=0;
can_filter.filter_mode = CAN_FILTERMODE_MASK;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
can_filter.filter_list_high = 0x0000;
can_filter.filter_list_low = 0x0000;
can_filter.filter_mask_high = 0x0000;
can_filter.filter_mask_low = 0x0000;
can_filter.filter_fifo_number = CAN_FIFO0;
can_filter.filter_enable = ENABLE;
can_filter_init(&can_filter);
/* CAN1 filter number */
can_filter.filter_number = 15;
can_filter_init(&can_filter);
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INT_RFNE0);
can_interrupt_enable(CAN1, CAN_INT_RFNE0);
}