配置的代码如下
#include "bxcan.h"
#include "at32f403a_407.h"
#define CAN_RX_TX 1
void CAN_STB_EN(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_mode =GPIO_MODE_OUTPUT ;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_pins = GPIO_PINS_0|GPIO_PINS_1;
gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
gpio_init(GPIOE, &gpio_init_struct);
gpio_bits_reset(GPIOE, GPIO_PINS_0 | GPIO_PINS_1 );
}
void bxcan_init(void)
{
#if CAN_RX_TX
can_base_type can_base_instruct;
can_filter_init_type can_filter_instruct;
can_baudrate_type can_baudrate_struct;
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK,TRUE);
gpio_pin_remap_config(CAN1_GMUX_0011,TRUE);
// nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
gpio_default_para_init(&gpio_init_struct);
/* can tx pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_pins = GPIO_PINS_1;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOD, &gpio_init_struct);
/* can rx pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_0;
gpio_init_struct.gpio_pull = GPIO_PULL_UP;
gpio_init(GPIOD, &gpio_init_struct);
can_default_para_init(&can_base_instruct);
can_base_instruct.mode_selection = CAN_MODE_COMMUNICATE;
can_base_instruct.ttc_enable = FALSE;
can_base_instruct.aebo_enable =FALSE ;
can_base_instruct.aed_enable = TRUE;
can_base_instruct.prsf_enable = FALSE;
can_base_instruct.mdrsel_selection =CAN_DISCARDING_FIRST_RECEIVED;
can_base_instruct.mmssr_selection =CAN_SENDING_BY_ID ;
can_base_init(CAN1,&can_base_instruct);
//500k 60mhz
can_baudrate_struct.baudrate_div = 10;
can_baudrate_struct.bts1_size = CAN_BTS1_8TQ;
can_baudrate_struct.bts2_size = CAN_BTS2_3TQ;
can_baudrate_struct.rsaw_size = CAN_RSAW_3TQ;
can_baudrate_set(CAN1, &can_baudrate_struct);
can_filter_instruct.filter_activate_enable = TRUE;
can_filter_instruct.filter_mode = CAN_FILTER_MODE_ID_MASK;
can_filter_instruct.filter_fifo = CAN_FILTER_FIFO0;
can_filter_instruct.filter_number = 0 ;
can_filter_instruct.filter_bit = CAN_FILTER_32BIT;
can_filter_instruct.filter_id_high =0 ;
can_filter_instruct.filter_id_low = 0;
can_filter_instruct.filter_mask_high = 0;
can_filter_instruct.filter_mask_low = 0;
can_filter_init(CAN1,&can_filter_instruct);
// nvic_irq_enable(CAN1_SE_IRQn, 0x00, 0x00);
nvic_irq_enable(USBFS_L_CAN1_RX0_IRQn, 0, 0);
nvic_irq_enable(CAN1_RX1_IRQn, 0, 0);
can_interrupt_enable(CAN1, CAN_RF0MIEN_INT, TRUE);
// /* error interrupt enable */
// can_interrupt_enable(CAN1, CAN_ETRIEN_INT, TRUE);
// can_interrupt_enable(CAN1, CAN_EOIEN_INT, TRUE);
//
#else
can_base_type can_base_instruct;
can_filter_init_type can_filter_instruct;
can_baudrate_type can_baudrate_struct;
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
// crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK,TRUE);
crm_periph_clock_enable(CRM_CAN2_PERIPH_CLOCK,TRUE);
gpio_pin_remap_config(CAN2_GMUX_0001,TRUE);
gpio_default_para_init(&gpio_init_struct);
//nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
/* can tx pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_pins = GPIO_PINS_6;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOB, &gpio_init_struct);
/* can rx pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_5;
gpio_init_struct.gpio_pull = GPIO_PULL_UP;
gpio_init(GPIOB, &gpio_init_struct);
can_base_instruct.aebo_enable =FALSE ;
can_base_instruct.aed_enable = FALSE;
can_base_instruct.mdrsel_selection =CAN_DISCARDING_FIRST_RECEIVED;
can_base_instruct.mmssr_selection =CAN_SENDING_BY_ID ;
can_base_instruct.mode_selection = CAN_MODE_COMMUNICATE;
can_base_instruct.prsf_enable = FALSE;
can_base_instruct.ttc_enable = FALSE;
can_base_init(CAN2,&can_base_instruct);
can_baudrate_struct.baudrate_div = 10;
can_baudrate_struct.bts1_size = CAN_BTS1_8TQ;
can_baudrate_struct.bts2_size = CAN_BTS2_3TQ;
can_baudrate_struct.rsaw_size = CAN_RSAW_3TQ;
can_baudrate_set(CAN2, &can_baudrate_struct);
can_filter_instruct.filter_activate_enable = TRUE;
can_filter_instruct.filter_bit = CAN_FILTER_32BIT;
can_filter_instruct.filter_fifo = CAN_FILTER_FIFO0;
can_filter_instruct.filter_id_high =0 ;
can_filter_instruct.filter_id_low = 0;
can_filter_instruct.filter_mask_high = 0;
can_filter_instruct.filter_mask_low = 0;
can_filter_instruct.filter_mode = CAN_FILTER_MODE_ID_LIST;
can_filter_instruct.filter_number = 0 ;
can_filter_init(CAN2,&can_filter_instruct);
nvic_irq_enable(CAN2_RX0_IRQn, 0, 0);
can_interrupt_enable(CAN2, CAN_RF0MIEN_INT, TRUE);
#endif
}
void can2_init(void)
{
can_base_type can_base_instruct;
can_filter_init_type can_filter_instruct;
can_baudrate_type can_baudrate_struct;
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
//crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK,TRUE);
crm_periph_clock_enable(CRM_CAN2_PERIPH_CLOCK,TRUE);
gpio_pin_remap_config(CAN2_GMUX_0001,TRUE);
gpio_default_para_init(&gpio_init_struct);
//nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
/* can tx pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_pins = GPIO_PINS_6;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOB, &gpio_init_struct);
/* can rx pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
gpio_init_struct.gpio_pins = GPIO_PINS_5;
gpio_init_struct.gpio_pull = GPIO_PULL_UP;
gpio_init(GPIOB, &gpio_init_struct);
can_base_instruct.aebo_enable =FALSE ;
can_base_instruct.aed_enable = FALSE;
can_base_instruct.mdrsel_selection =CAN_DISCARDING_FIRST_RECEIVED;
can_base_instruct.mmssr_selection =CAN_SENDING_BY_ID ;
can_base_instruct.mode_selection = CAN_MODE_COMMUNICATE;
can_base_instruct.prsf_enable = FALSE;
can_base_instruct.ttc_enable = FALSE;
can_base_init(CAN2,&can_base_instruct);
can_baudrate_struct.baudrate_div = 10;
can_baudrate_struct.bts1_size = CAN_BTS1_8TQ;
can_baudrate_struct.bts2_size = CAN_BTS2_3TQ;
can_baudrate_struct.rsaw_size = CAN_RSAW_3TQ;
can_baudrate_set(CAN2, &can_baudrate_struct);
can_filter_instruct.filter_activate_enable = TRUE;
can_filter_instruct.filter_bit = CAN_FILTER_32BIT;
can_filter_instruct.filter_fifo = CAN_FILTER_FIFO1;
can_filter_instruct.filter_id_high =0 ;
can_filter_instruct.filter_id_low = 0;
can_filter_instruct.filter_mask_high = 0;
can_filter_instruct.filter_mask_low = 0;
can_filter_instruct.filter_mode = CAN_FILTER_MODE_ID_MASK;
can_filter_instruct.filter_number = 1 ;
can_filter_init(CAN2,&can_filter_instruct);
nvic_irq_enable(CAN2_RX0_IRQn, 0, 0);
can_interrupt_enable(CAN2, CAN_RF0MIEN_INT, TRUE);
}
void can_transmit(void)
{
uint8_t transmit_mailbox;
can_tx_message_type tx_message_struct;
tx_message_struct.standard_id = 0x033;
tx_message_struct.extended_id = 0;
tx_message_struct.id_type = CAN_ID_STANDARD;
tx_message_struct.frame_type = CAN_TFT_DATA;
tx_message_struct.dlc = 8;
tx_message_struct.data[0] = 0x88;
tx_message_struct.data[1] = 0x11;
tx_message_struct.data[2] = 0x33;
tx_message_struct.data[3] = 0x44;
tx_message_struct.data[4] = 0x55;
tx_message_struct.data[5] = 0x66;
tx_message_struct.data[6] = 0x77;
tx_message_struct.data[7] = 0x88;
#if CAN_RX_TX
transmit_mailbox = can_message_transmit(CAN1, &tx_message_struct);
while(can_transmit_status_get(CAN1, (can_tx_mailbox_num_type)transmit_mailbox) != CAN_TX_STATUS_SUCCESSFUL);
#else
transmit_mailbox = can_message_transmit(CAN2, &tx_message_struct);
while(can_transmit_status_get(CAN2, (can_tx_mailbox_num_type)transmit_mailbox) != CAN_TX_STATUS_SUCCESSFUL);
#endif
}
void can2_transmit(void)
{
uint8_t transmit_mailbox;
can_tx_message_type tx_message_struct;
tx_message_struct.standard_id = 0x00;
tx_message_struct.extended_id = 0;
tx_message_struct.id_type = CAN_ID_STANDARD;
tx_message_struct.frame_type = CAN_TFT_DATA;
tx_message_struct.dlc = 8;
tx_message_struct.data[0] = 0x99;
tx_message_struct.data[1] = 0x11;
tx_message_struct.data[2] = 0x33;
tx_message_struct.data[3] = 0x44;
tx_message_struct.data[4] = 0x55;
tx_message_struct.data[5] = 0x66;
tx_message_struct.data[6] = 0x77;
tx_message_struct.data[7] = 0x88;
transmit_mailbox = can_message_transmit(CAN2, &tx_message_struct);
while(can_transmit_status_get(CAN2, (can_tx_mailbox_num_type)transmit_mailbox) != CAN_TX_STATUS_SUCCESSFUL);
}
int main(void)
{
system_clock_config();
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
CAN_STB_EN();
delay_init();
can2_init();
bxcan_init();
while(1)
{
can_transmit();
delay_ms(10000);
can2_transmit();
delay_ms(10000);
}
}
|
很好的资源,学习啦~~