先申明,本栏目用的都是GD32F470芯片240M,软件用的是keil,编写用的是C++(其实和C没有区别).,
下面这个图是F450,F470比较新,还没出手册。
can的 相关知识我就不科普了。
IO口配置:
根据你自己板子上的IO口来配置,
我的can是PD0和PD1。
/* enable can clock */
rcu_periph_clock_enable(RCU_CAN0);
// rcu_periph_clock_enable(RCU_CAN1);
rcu_periph_clock_enable(RCU_GPIOD);
/* configure CAN0 GPIO */
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0);
gpio_af_set(GPIOD, GPIO_AF_9, GPIO_PIN_0);
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
gpio_af_set(GPIOD, GPIO_AF_9, GPIO_PIN_1);
CAN波特率计算.
举个例子:我要配置成500K.
那么我的tpclk1 =60M.
从这个图可以看出,但是我的是F470,是60M。
那么:
1.tq = (1/60M )* 12=(1/5)us ,或者这么理解 1/(60M/12),那么12就是分频的意思。
2.𝑡𝑆𝑌𝑁𝐶_𝑆𝐸𝐺 为 1 tq(一般固定为1)
𝑡𝐵𝑆1 + 𝑡𝐵𝑆2凑出一个9 * tq就行.
我这里𝑡𝐵𝑆1 = 5 ,𝑡𝐵𝑆2=4.
所以:(1+ 5 + 4 ) tq = 10 * (1/5)us = 2us
- 𝐵𝑎𝑢𝑑𝑅𝑎𝑡e = 1 / 2us = 0.5 *10 ^6 hz = 500 000hz = 500K.
Can配置:
代码:
下面列举了各种波特率的配置.
/Can时钟是60M
//波特率 = (60M /(1 + ntsg1 + ntsg2) / presc) ,单位是M
void Can_Class::Can_Init(u8 baudrate_Index)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
uint8_t ntsg1=0,ntsg2=0;
uint16_t presc=0;
Can_GPIO_Init();
if(baudrate_Index==1)
can_Baudrate =20000;
else if(baudrate_Index==2)
can_Baudrate =50000;
else if(baudrate_Index==3)
can_Baudrate =100000;
else if(baudrate_Index==4)
can_Baudrate =125000;
else if(baudrate_Index==5)
can_Baudrate =250000;
else if(baudrate_Index==6)
can_Baudrate =500000;
else if(baudrate_Index==7)
can_Baudrate =1000000;
else
can_Baudrate =500000;
//根据波特率的不同,设置不同参数
if(can_Baudrate==20000)
{
presc=300;
ntsg1=CAN_BT_BS1_5TQ;
ntsg2=CAN_BT_BS2_4TQ;
}
else if(can_Baudrate==50000)
{
presc=120;
ntsg1=CAN_BT_BS1_5TQ;
ntsg2=CAN_BT_BS2_4TQ;
}
else if(can_Baudrate==100000)
{
presc=60;
ntsg1=CAN_BT_BS1_5TQ;
ntsg2=CAN_BT_BS2_4TQ;
}
else if(can_Baudrate==125000)
{
presc=48;
ntsg1=CAN_BT_BS1_5TQ;
ntsg2=CAN_BT_BS2_4TQ;
}
else if(can_Baudrate==250000)
{
presc=24;
ntsg1=CAN_BT_BS1_5TQ;
ntsg2=CAN_BT_BS2_4TQ;
}
else if(can_Baudrate==500000)
{
presc=12;
ntsg1=CAN_BT_BS1_5TQ;
ntsg2=CAN_BT_BS2_4TQ;
}
else if(can_Baudrate==1000000)
{
presc=6;
ntsg1=CAN_BT_BS1_5TQ;
ntsg2=CAN_BT_BS2_4TQ;
}
else//默认500K
{
presc=12;
ntsg1=CAN_BT_BS1_5TQ;
ntsg2=CAN_BT_BS2_4TQ;
}
/* initialize CAN register */
can_deinit(can_index);
/* 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 = DISABLE;//禁用接收 FIFO 满时覆盖0:使能接收 FIFO 满时覆盖
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 = ntsg1;
can_parameter.time_segment_2 = ntsg2;
can_parameter.prescaler = presc;
can_init(can_index, &can_parameter);
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 = 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);
/* enable CAN receive FIFO0 not empty interrupt */
can_interrupt_enable(can_index, CAN_INTEN_RFNEIE0);
nvic_irq_enable(CAN0_RX0_IRQn,0,1);
}
can 寄存器以及库函数的配置细节和坑:
可以看到寄存器的位域值是+1的。
所以如果是自己填写bs1和bs2数据时而不用自带库的宏定义的话,要减少1.
注意:
分频系数不需要手动减1,因为库函数里面帮我们减1了。。(波特率 的公式要加1的都要),
库的宏定义代码:
可以看到减了1
#define CAN_BT_BS1_4TQ ((uint8_t)0x03U) /*!< 4 time quanta */
#define CAN_BT_BS1_5TQ ((uint8_t)0x04U) /*!< 5 time quanta */
然后是分频系数:在init函数里面-1,不需要手动减1.
总结:还是用库的宏定义把。
滤波ID:
没弄,默认全部接受。后续可能会搞。
发送函数:
发送的 是数据帧和标准帧
uint8_t Can_Class::Can_Send_Data(uint32_t ID,uint8_t* msg,uint32_t len)
{
uint8_t mail_Index=0;
uint8_t i=0;
UNS32 Canopen_Wait_Send_Succeed_time=0;
can_trasnmit_message_struct transmit_message;
transmit_message.tx_sfid = ID;
transmit_message.tx_efid = 0x00;
transmit_message.tx_ft = CAN_FT_DATA; //数据帧
transmit_message.tx_ff = CAN_FF_STANDARD; //标准帧
transmit_message.tx_dlen = len;
for(i=0;i<len; i++)
{
transmit_message.tx_data[i] = msg[i];
}
mail_Index = can_message_transmit(can_index, &transmit_message);
mail_Index = can_message_transmit(CAN0, &transmit_message);
Canopen_Wait_Send_Succeed_time = lwTickCount;
while(can_transmit_states(CAN0,mail_Index)!=CAN_TRANSMIT_OK)
{
if(lwTickCount>(Canopen_Wait_Send_Succeed_time + 2))
{
return 1;
}
}
return 0;
}
}
can_message_transmit是判断有没有空邮箱,有的话就发送出去。
can_transmit_states是判断该邮箱的状态。
中断函数:
我的是接收邮箱为不空就中断,然后有数据就读取出来。
处理的是canfasival的代码
注意:
不需要清中断,读取了就会硬件清零。
extern "C"void CAN0_RX0_IRQHandler(void)
{
Message RxMSG = Message_Initializer;
//这只是判断是否打开了中断,不用can_interrupt_flag_clear,
//can_interrupt_flag_clear这个函数是用来清状态寄存器 (CAN_STAT)的,不是接收中断的.
can_receive_message_struct receive_message;
/* check the receive message */
//释放一次 FIFO0 中的数据
//RFD0该位被置 1, 将释放 FIFO0 中的一帧数据。
//FIFO 释放相应的数据空间后,该位被清 0。硬件清零
can_message_receive(CAN0, CAN_FIFO0, &receive_message);
/************Canfestival需要的********************************************/
RxMSG.cob_id = receive_message.rx_sfid;
RxMSG.len = receive_message.rx_dlen;
if( receive_message.rx_ft == CAN_FT_REMOTE )
{
RxMSG.rtr = 1;
}
else
{
RxMSG.rtr = 0;
}
mymemcpy(RxMSG.data, receive_message.rx_data, RxMSG.len);
canDispatch(&TestSlave_Data, &(RxMSG)); //canopen报文分析
}
库函数的最后会释放。
void can_message_receive(uint32_t can_periph, uint8_t fifo_number, can_receive_message_struct *receive_message)
{
/* get the frame format */
receive_message->rx_ff = (uint8_t)(CAN_RFIFOMI_FF & CAN_RFIFOMI(can_periph, fifo_number));
if(CAN_FF_STANDARD == receive_message->rx_ff) {
/* get standard identifier */
receive_message->rx_sfid = (uint32_t)(GET_RFIFOMI_SFID(CAN_RFIFOMI(can_periph, fifo_number)));
} else {
/* get extended identifier */
receive_message->rx_efid = (uint32_t)(GET_RFIFOMI_EFID(CAN_RFIFOMI(can_periph, fifo_number)));
}
/* get frame type */
receive_message->rx_ft = (uint8_t)(CAN_RFIFOMI_FT & CAN_RFIFOMI(can_periph, fifo_number));
/* filtering index */
receive_message->rx_fi = (uint8_t)(GET_RFIFOMP_FI(CAN_RFIFOMP(can_periph, fifo_number)));
/* get receive data length */
receive_message->rx_dlen = (uint8_t)(GET_RFIFOMP_DLENC(CAN_RFIFOMP(can_periph, fifo_number)));
/* receive data */
receive_message -> rx_data[0] = (uint8_t)(GET_RFIFOMDATA0_DB0(CAN_RFIFOMDATA0(can_periph, fifo_number)));
receive_message -> rx_data[1] = (uint8_t)(GET_RFIFOMDATA0_DB1(CAN_RFIFOMDATA0(can_periph, fifo_number)));
receive_message -> rx_data[2] = (uint8_t)(GET_RFIFOMDATA0_DB2(CAN_RFIFOMDATA0(can_periph, fifo_number)));
receive_message -> rx_data[3] = (uint8_t)(GET_RFIFOMDATA0_DB3(CAN_RFIFOMDATA0(can_periph, fifo_number)));
receive_message -> rx_data[4] = (uint8_t)(GET_RFIFOMDATA1_DB4(CAN_RFIFOMDATA1(can_periph, fifo_number)));
receive_message -> rx_data[5] = (uint8_t)(GET_RFIFOMDATA1_DB5(CAN_RFIFOMDATA1(can_periph, fifo_number)));
receive_message -> rx_data[6] = (uint8_t)(GET_RFIFOMDATA1_DB6(CAN_RFIFOMDATA1(can_periph, fifo_number)));
receive_message -> rx_data[7] = (uint8_t)(GET_RFIFOMDATA1_DB7(CAN_RFIFOMDATA1(can_periph, fifo_number)));
/* release FIFO */
if(CAN_FIFO0 == fifo_number) {
CAN_RFIFO0(can_periph) |= CAN_RFIFO0_RFD0;
} else {
CAN_RFIFO1(can_periph) |= CAN_RFIFO1_RFD1;
}
}