话说这款芯片在评测时的标题就是:雅特力AT-START-F425,USB与CAN应用首选
现在开始对CAN进行基本的使用:
1、在drivers文件夹中新增can.h、can.c:
这里官方给的DEMO的RX 为PB8,TX为PB9,复用配置为MUX4。从CAN的demo中复制IO配置函数:
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] can gpio config
* @param none
* @retval none
*/
static void can_gpio_config(void)
{
gpio_init_type gpio_init_struct;
/* enable the gpio clock */
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
/* configure the can tx, rx 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_8 | GPIO_PINS_9;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOB, &gpio_init_struct);
gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE9, GPIO_MUX_4);
gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE8, GPIO_MUX_4);
}
然后复制can的参数配置:can_configuration 配置CA1 为500K,没有设置ID过滤
/**
* @brief can configiguration.
* @param none
* @retval none
*/
static void can_configuration(void)
{
can_base_type can_base_struct;
can_baudrate_type can_baudrate_struct;
can_filter_init_type can_filter_init_struct;
/* enable the can clock */
crm_periph_clock_enable(CRM_CAN1_PERIPH_CLOCK, TRUE);
/* can base init */
can_default_para_init(&can_base_struct);
can_base_struct.mode_selection = CAN_MODE_COMMUNICATE;
can_base_struct.ttc_enable = FALSE;
can_base_struct.aebo_enable = TRUE;
can_base_struct.aed_enable = TRUE;
can_base_struct.prsf_enable = FALSE;
can_base_struct.mdrsel_selection = CAN_DISCARDING_FIRST_RECEIVED;
can_base_struct.mmssr_selection = CAN_SENDING_BY_ID;
can_base_init(CAN1, &can_base_struct);
/* can baudrate, set boudrate = pclk/(baudrate_div *(1 + bts1_size + bts2_size)) */
can_baudrate_struct.baudrate_div = 16;
can_baudrate_struct.rsaw_size = CAN_RSAW_1TQ;
can_baudrate_struct.bts1_size = CAN_BTS1_8TQ;
can_baudrate_struct.bts2_size = CAN_BTS2_3TQ; //这里配置常用的500K 即96M/(16*(1+8+3)) = 500K
can_baudrate_set(CAN1, &can_baudrate_struct);
/* can filter init */
can_filter_init_struct.filter_activate_enable = TRUE;
can_filter_init_struct.filter_mode = CAN_FILTER_MODE_ID_MASK;
can_filter_init_struct.filter_fifo = CAN_FILTER_FIFO0;
can_filter_init_struct.filter_number = 0;
can_filter_init_struct.filter_bit = CAN_FILTER_32BIT;
can_filter_init_struct.filter_id_high = 0;
can_filter_init_struct.filter_id_low = 0;
can_filter_init_struct.filter_mask_high = 0;
can_filter_init_struct.filter_mask_low = 0;
can_filter_init(CAN1, &can_filter_init_struct);
/* can interrupt config */
nvic_irq_enable(CAN1_IRQn, 0x00, 0x00);
can_interrupt_enable(CAN1, CAN_RF0MIEN_INT, TRUE);
can_interrupt_enable(CAN1, CAN_ETRIEN_INT, TRUE);
can_interrupt_enable(CAN1, CAN_EOIEN_INT, TRUE);
}
复制发送函数:发送ID设置为标准ID,ID = 0x400 发送8位数据
/**
* @brief can transmit data
* @param none
* @retval none
*/
void can_transmit_data(void)
{
uint8_t transmit_mailbox;
can_tx_message_type tx_message_struct;
tx_message_struct.standard_id = 0x400;
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] = 0x11;
tx_message_struct.data[1] = 0x22;
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(CAN1, &tx_message_struct);
while(can_transmit_status_get(CAN1, (can_tx_mailbox_num_type)transmit_mailbox) != CAN_TX_STATUS_SUCCESSFUL);
}
配置can1中断接收函数:
/**
* @brief can1 interrupt function
* @param none
* @retval none
*/
void CAN1_IRQHandler(void)
{
__IO uint32_t err_index = 0;
can_rx_message_type rx_message_struct;
if(can_flag_get(CAN1,CAN_ETR_FLAG) != RESET)
{
err_index = CAN1->ests & 0x70;
can_flag_clear(CAN1, CAN_ETR_FLAG);
if(err_index == 0x00000010)
{
can_reset(CAN1);
can_configuration();
}
}
if(can_flag_get(CAN1,CAN_RF0MN_FLAG) != RESET)
{
can_message_receive(CAN1, CAN_RX_FIFO0, &rx_message_struct);
if(rx_message_struct.standard_id == 0x400)
{
如果是接收到的ID 是0x400则显示接收到相对的ID,后期可以根据这个来作更进一步功能。
LCD_ShowString(10, 240, "recv 0x400 ", BLUE,WHITE, 32, 0);
}
else
{
LCD_ShowString(10, 240, "recv not my ID", BLUE,WHITE, 32, 0);
}
}
}
在主任务函中初始化CAN1,接上CAN分析仪,配置为500K速率,接收到如下信息:
堆积木到现在CAN通信已经评测通过。明天再调试与STM32、N32G45的三方通信。
引用: Jacktang 发表于 2022-3-30 13:38 下面是调试与STM32、N32G45的三方通信的测评应该更加顺利
谢谢,你的作品也是写得很精彩的,大家共同进步,谢谢!