/* CAN1 interface */
can_trasnmit_message_struct can1_transmit_message; //can0 rx message
can_receive_message_struct can1_receive_message; //can0 rx message
uint8_t can1_receive_flag = 0;
static void bsp_can1_gpio_config(void)
{
/* enable CAN1 clock */
rcu_periph_clock_enable(RCU_CAN1);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_AF);
/* configure CAN1 GPIO */
gpio_init(GPIOB, GPIO_MODE_IPU,GPIO_OSPEED_50MHZ, GPIO_PIN_12);
gpio_init(GPIOB, GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_13);
gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_15);
gpio_bit_write(GPIOC, GPIO_PIN_15, RESET);
// gpio_pin_remap_config(GPIO_CAN1_REMAP,ENABLE);
}
/*!
\brief initialize can1 interface
\param[in] none
\param[out] none
\retval none
*/
void bsp_can1_init(void)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
bsp_can1_gpio_config();
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
/* initialize CAN register */
can_deinit(CAN1);
/* initialize CAN parameters */
can_parameter.time_triggered = DISABLE;
can_parameter.auto_bus_off_recovery = DISABLE;
can_parameter.auto_wake_up = DISABLE;
can_parameter.auto_retrans = DISABLE;
can_parameter.rec_fifo_overwrite = DISABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
/* configure CAN0 baud rate 1Mbps */
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 = 18;
/* initialize CAN */
can_init(CAN1, &can_parameter);
/* initialize filter */
/* configure filter mode */
can_filter.filter_mode = CAN_FILTERMODE_MASK;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
/* configure filter ID, only the 0xaabb expand ID can be received */
can_filter.filter_list_high = (uint16_t)(0xAACC >> 13U);
can_filter.filter_list_low = (uint16_t)(0xAACC << 3U);
/* configure filter mask */
can_filter.filter_mask_high = 0x0000;
can_filter.filter_mask_low = 0x0000;
/* select receiver fifo */
can_filter.filter_fifo_number = CAN_FIFO0;
can_filter.filter_number = 15;
can_filter.filter_enable = ENABLE;
can_filter_init(CAN1, &can_filter);
/* configure CAN1 NVIC */
nvic_irq_enable(CAN1_RX0_IRQn, 1, 0);
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN1, CAN_INT_RFNE0);
}
void CAN1_RX0_IRQHandler(void)
{
gpio_bit_set(GPIOC, GPIO_PIN_1);
/* check the receive message */
can_message_receive(CAN1, CAN_FIFO0, &can1_receive_message);
can1_receive_flag = 1;
}
|