can.c

#include "can.h"can_trasnmit_message_struct transmit_message;//传入参数为0,则初始化CAN0的GPIO引脚;传入参数为1,则初始化CAN1的GPIO引脚void CanGpioInit(void){/* enable can clock */rcu_periph_clock_enable(RCU_CAN0);rcu_periph_clock_enable(RCU_GPIOD);/* configure CAN0 GPIO, CAN0_TX(PD1) and CAN0_RX(PD0) */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);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);}//初始化CAN和配置过滤器,不过滤任何IDvoid can_networking_init(void){can_parameter_struct can_parameter;can_filter_parameter_struct can_filter;/* initialize CAN structures */can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);/* initialize CAN register */can_deinit(CAN0);/* initialize CAN */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;can_parameter.working_mode = CAN_SILENT_LOOPBACK_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;/* baudrate 1Mbps */can_parameter.prescaler = 5;can_init(CAN0, &can_parameter);/* 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 = 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);//使能CAN0的接收中断nvic_irq_enable(CAN0_RX0_IRQn, 0, 0);can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);}uint8_t can_send_msg(uint32_t id, uint8_t *msg, uint8_t len){uint32_t timeout = 0xFFFF;uint8_t transmit_mailbox = 0;transmit_message.tx_sfid = id;transmit_message.tx_efid = id;transmit_message.tx_ft = CAN_FT_DATA;transmit_message.tx_ff = CAN_FF_STANDARD;transmit_message.tx_dlen = len;for(int i=0;i<len;i++){transmit_message.tx_data[i] = msg[i];}transmit_mailbox = can_message_transmit(CAN0, &transmit_message);/* waiting for transmit completed */timeout = 0xFFFF;while((CAN_TRANSMIT_OK != can_transmit_states(CAN0, transmit_mailbox)) && (0 != timeout)){timeout--;}if(timeout==0){return 1;//发送失败} return 0;//发送成功}

can.h

#ifndef __CAN_H#define __CAN_H#include "head.h"void CanGpioInit(void);void can_networking_init(void);uint8_t can_send_msg(uint32_t id, uint8_t *msg, uint8_t len);extern can_trasnmit_message_struct transmit_message;#endif

中断处理函数

char receive_flag;can_receive_message_struct receive_message;void CAN0_RX0_IRQHandler(void){/* check the receive message */can_message_receive(CAN0, CAN_FIFO0, &receive_message);if((0x7ab == receive_message.rx_sfid)&&(CAN_FF_STANDARD == receive_message.rx_ff) && (8 == receive_message.rx_dlen)){receive_flag = 1;}}

主函数

CanGpioInit();can_networking_init();uint8_t buf[8];buf[0] = 0xA0;buf[1] = 0xA1;buf[2] = 0xA2;buf[3] = 0xA3;buf[4] = 0xA4;buf[5] = 0xA5;buf[6] = 0xA6;buf[7] = 0xA7;while(1){can_send_msg(0x7ab,buf,8);if(1 == receive_flag){receive_flag = 0;for(int i = 0; i < receive_message.rx_dlen; i++){printf(" %02x", receive_message.rx_data[i]);}}}