打印
[AT32F403/403A]

AT32F403A CAN问题

[复制链接]
1348|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
春风知意|  楼主 | 2023-5-12 10:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
关于CAN1和CAN2无法同时使用的问题,单独使用CAN1或者CAN2是可以用示波器看到波形的,先初始化CAN2再初始化CAN2或者两个顺序反过来先初始化CAN1,再初始化CAN2都会出现CAN2无法使用

使用特权

评论回复
评论
xu@xupt 2023-9-17 08:44 回复TA
很好的资源,学习啦~~ 
沙发
春风知意|  楼主 | 2023-5-12 10:44 | 只看该作者
配置的代码如下
#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);

   }
     
}


使用特权

评论回复
板凳
春风知意|  楼主 | 2023-5-12 10:46 | 只看该作者
本帖最后由 春风知意 于 2023-5-12 10:47 编辑

两个初始化函数我都单独测试了,示波器是有波形的。

使用特权

评论回复
地板
hoop| | 2023-5-12 11:04 | 只看该作者
本帖最后由 muyichuan2012 于 2023-5-12 18:07 编辑

可以跟如下路径代码对比看看,哪里存在差异
https://www.arterytek.com/download/FAQ/FAQ0102_%E5%8F%8CCAN%E9%85%8D%E7%BD%AE_V2.0.0.pdf

使用特权

评论回复
5
春风知意|  楼主 | 2023-5-14 00:28 | 只看该作者
hoop 发表于 2023-5-12 11:04
可以跟如下路径代码对比看看,哪里存在差异
https://www.arterytek.com/download/FAQ/FAQ0102_%E5%8F%8CCAN ...

好的,谢谢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

11

主题

41

帖子

0

粉丝