[研电赛技术支持] GD32F105RCT6的CAN1通讯只能发不能收,无法进入接收中断

[复制链接]
 楼主| 左左左啊 发表于 2025-6-27 16:38 | 显示全部楼层 |阅读模式
使用GD32F105系列的官方CAN通讯Demo,CAN0可以发可以收,CAN1只能发不能收,查找了好多资料,最后感觉定位在就是滤波器的问题,但是我用keil的debug模式发现无论是CAN0还是CAN1,我更改了滤波器编号,CAN_FW始终不显示我激活了哪个滤波器。这是什么原因,求助求助大佬们,困扰了好久了。下面是官方的demo。
  1. /*!
  2.     \file    main.c
  3.     \brief   communication_among_Devices in normal mode
  4.    
  5.     \version 2024-12-20, V2.5.0, firmware for GD32F10x
  6. */

  7. /*
  8.     Copyright (c) 2024, GigaDevice Semiconductor Inc.

  9.     Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:

  11.     1. Redistributions of source code must retain the above copyright notice, this
  12.        list of conditions and the following disclaimer.
  13.     2. Redistributions in binary form must reproduce the above copyright notice,
  14.        this list of conditions and the following disclaimer in the documentation
  15.        and/or other materials provided with the distribution.
  16.     3. Neither the name of the copyright holder nor the names of its contributors
  17.        may be used to endorse or promote products derived from this software without
  18.        specific prior written permission.

  19.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */

  30. #include "gd32f10x.h"
  31. #include <stdio.h>
  32. #include "gd32f10x_eval.h"

  33. /* select can */
  34. //#define CAN0_USED
  35. #define CAN1_USED

  36. #ifdef  CAN0_USED
  37.     #define CANX CAN0
  38. #else
  39.     #define CANX CAN1
  40. #endif

  41. FlagStatus receive_flag;
  42. uint8_t transmit_number = 0x0;
  43. can_trasnmit_message_struct g_transmit_message;
  44. can_receive_message_struct g_receive_message;
  45.    
  46. void nvic_config(void);
  47. void led_config(void);
  48. void gpio_config(void);
  49. ErrStatus can_networking(void);
  50. void can_networking_init(void);
  51. void delay(void);

  52. /*!
  53.     \brief      main function
  54.     \param[in]  none
  55.     \param[out] none
  56.     \retval     none
  57. */
  58. int main(void)
  59. {
  60.     uint8_t i = 0;
  61.     uint32_t timeout = 0xFFFF;
  62.     uint8_t transmit_mailbox = 0;
  63.    
  64.     receive_flag = RESET;
  65.     /* configure Tamper key */
  66.     gd_eval_key_init(KEY_TAMPER, KEY_MODE_GPIO);
  67.     /* configure GPIO */
  68.     gpio_config();
  69.     /* configure USART */
  70.     gd_eval_com_init(EVAL_COM0);
  71.     /* configure NVIC */
  72.     nvic_config();
  73.     /* configure leds */
  74.     led_config();
  75.     /* set all leds off */
  76.     gd_eval_led_off(LED2);
  77.     gd_eval_led_off(LED3);
  78.     gd_eval_led_off(LED4);
  79.     /* initialize CAN */
  80.     can_networking_init();
  81.    
  82.     /* enable CAN receive FIFO1 not empty interrupt */
  83.     can_interrupt_enable(CANX, CAN_INT_RFNE1);
  84. //     can_interrupt_enable(CAN1, CAN_INT_RFNE1);
  85.     /* initialize transmit message */
  86.     can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &g_transmit_message);
  87.                
  88.     g_transmit_message.tx_sfid = 0x00;
  89.     g_transmit_message.tx_efid = 0xaabb;
  90.     g_transmit_message.tx_ft = CAN_FT_DATA;
  91.     g_transmit_message.tx_ff = CAN_FF_EXTENDED;
  92.     g_transmit_message.tx_dlen = 8;
  93.    
  94.     g_transmit_message.tx_data[0] = 0xA0U;
  95.     g_transmit_message.tx_data[1] = 0xA1U;
  96.     g_transmit_message.tx_data[2] = 0xA2U;
  97.     g_transmit_message.tx_data[3] = 0xA3U;
  98.     g_transmit_message.tx_data[4] = 0xA4U;
  99.     g_transmit_message.tx_data[5] = 0xA5U;
  100.     g_transmit_message.tx_data[6] = 0xA6U;
  101.     g_transmit_message.tx_data[7] = 0xA7U;
  102.    
  103.     printf("please press the Tamper key to transmit data!\r\n");
  104.    
  105.     /* initialize receive message */
  106.     can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &g_receive_message);
  107.    
  108.     while(1) {
  109.         /* waiting for the Tamper key pressed */
  110.         while(0 == gd_eval_key_state_get(KEY_TAMPER)){
  111.             /* if transmit_number is 0x10, set it to 0x00 */
  112.             if(transmit_number == 0x10){
  113.                 transmit_number = 0x00;
  114.             }else{
  115.                 g_transmit_message.tx_data[0] = transmit_number++;
  116.                 printf("\r\n can transmit data:");
  117.                 for(i = 0; i < g_transmit_message.tx_dlen; i++){
  118.                     printf(" %02x", g_transmit_message.tx_data[i]);
  119.                 }
  120.                 /* transmit message */
  121.                 transmit_mailbox = can_message_transmit(CANX, &g_transmit_message);
  122.                 /* waiting for transmit completed */
  123.                 timeout = 0xFFFF;
  124.                 while((CAN_TRANSMIT_OK != can_transmit_states(CANX, transmit_mailbox)) && (0 != timeout)){
  125.                     timeout--;
  126.                 }
  127.                 /* waiting for Tamper key up */
  128.                 while(0 == gd_eval_key_state_get(KEY_TAMPER));
  129.             }
  130.         }
  131.         if(SET == receive_flag) {
  132.             gd_eval_led_toggle(LED2);
  133.             receive_flag = RESET;
  134.             printf("\r\n receive data:");
  135.             for(i = 0; i < g_receive_message.rx_dlen; i++){
  136.                 printf(" %02x", g_receive_message.rx_data[i]);
  137.             }
  138.         }
  139.     }
  140. }

  141. /*!
  142.     \brief      initialize CAN and filter
  143.     \param[in]  can_parameter
  144.       \arg        can_parameter_struct
  145.     \param[in]  can_filter
  146.       \arg        can_filter_parameter_struct
  147.     \param[out] none
  148.     \retval     none
  149. */
  150. void can_networking_init(void)
  151. {
  152.     can_parameter_struct            can_parameter;
  153.     can_filter_parameter_struct     can_filter;
  154.    
  155.     can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
  156.     can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
  157.    
  158.     /* initialize CAN register */
  159.     can_deinit(CANX);
  160.    
  161.     /* initialize CAN */
  162.     can_parameter.time_triggered = DISABLE;
  163.     can_parameter.auto_bus_off_recovery = DISABLE;
  164.     can_parameter.auto_wake_up = DISABLE;
  165.     can_parameter.auto_retrans = DISABLE;
  166.     can_parameter.rec_fifo_overwrite = DISABLE;
  167.     can_parameter.trans_fifo_order = DISABLE;
  168.     can_parameter.working_mode = CAN_NORMAL_MODE;
  169.     can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
  170.     can_parameter.time_segment_1 = CAN_BT_BS1_5TQ;
  171.     can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
  172.     /* baudrate 250kbps */
  173.     can_parameter.prescaler = 24;
  174.     can_init(CANX, &can_parameter);
  175.     /* initialize filter */

  176. #ifdef  CAN0_USED
  177.     /* CAN0 filter number */
  178.     can_filter.filter_number = 0;
  179. #else
  180.     /* CAN1 filter number */
  181.     can_filter.filter_number = 15;
  182. #endif
  183.     /* initialize filter */   
  184.     can_filter.filter_mode = CAN_FILTERMODE_MASK;
  185.     can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  186.     can_filter.filter_list_high = 0x0000U;
  187.     can_filter.filter_list_low = 0x0000U;
  188.     can_filter.filter_mask_high = 0x0000U;
  189.     can_filter.filter_mask_low = 0x0000U;  
  190.     can_filter.filter_fifo_number = CAN_FIFO1;
  191.     can_filter.filter_enable = ENABLE;
  192.     can_filter_init(&can_filter);

  193. }

  194. /*!
  195.     \brief      configure the nested vectored interrupt controller
  196.     \param[in]  none
  197.     \param[out] none
  198.     \retval     none
  199. */
  200. void nvic_config(void)
  201. {
  202. #ifdef  CAN0_USED
  203.     /* configure CAN0 NVIC */
  204.     nvic_irq_enable(CAN0_RX1_IRQn,0,0);
  205. #else
  206.     /* configure CAN1 NVIC */
  207.     nvic_irq_enable(CAN1_RX1_IRQn,0,0);
  208. #endif

  209. }

  210. /*!
  211.     \brief      delay
  212.     \param[in]  none
  213.     \param[out] none
  214.     \retval     none
  215. */
  216. void delay(void)
  217. {
  218.     uint16_t nTime = 0x0000;

  219.     for(nTime = 0; nTime < 0xFFFF; nTime++){
  220.     }
  221. }

  222. /*!
  223.     \brief      configure the leds
  224.     \param[in]  none
  225.     \param[out] none
  226.     \retval     none
  227. */
  228. void led_config(void)
  229. {
  230.     gd_eval_led_init(LED2);
  231.     gd_eval_led_init(LED3);
  232.     gd_eval_led_init(LED4);
  233. }

  234. /*!
  235.     \brief      configure GPIO
  236.     \param[in]  none
  237.     \param[out] none
  238.     \retval     none
  239. */
  240. void gpio_config(void)
  241. {
  242.     /* enable CAN clock */
  243.     rcu_periph_clock_enable(RCU_CAN0);
  244.                 rcu_periph_clock_enable(RCU_CAN1);
  245.           rcu_periph_clock_enable(RCU_GPIOB);
  246.     rcu_periph_clock_enable(RCU_AF);
  247.    
  248.     /* configure CAN0 GPIO */
  249.     gpio_init(GPIOB,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
  250.     gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9);
  251.     gpio_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP,ENABLE);
  252.        
  253.           gpio_init(GPIOB,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_5);
  254.     gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
  255.     gpio_pin_remap_config(GPIO_CAN1_REMAP,ENABLE);
  256. }

  257. #ifdef GD_ECLIPSE_GCC
  258. /* retarget the C library printf function to the USART, in Eclipse GCC environment */
  259. int __io_putchar(int ch)
  260. {
  261.     usart_data_transmit(EVAL_COM0, (uint8_t) ch );
  262.     while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
  263.     return ch;
  264. }
  265. #else
  266. /* retarget the C library printf function to the USART */
  267. int fputc(int ch, FILE *f)
  268. {
  269.     usart_data_transmit(EVAL_COM0, (uint8_t)ch);
  270.     while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
  271.     return ch;
  272. }
  273. #endif /* GD_ECLIPSE_GCC */
中断的回调函数如下:
  1. void CAN0_RX1_IRQHandler(void)
  2. {
  3.     /* check the receive message */
  4.     can_message_receive(CAN0, CAN_FIFO1, &g_receive_message);
  5. //    if((0xaabb == g_receive_message.rx_efid)&&(CAN_FF_EXTENDED == g_receive_message.rx_ff) && (8 == g_receive_message.rx_dlen)){
  6.         receive_flag = SET;
  7. //    }
  8. }

  9. /*!
  10.     \brief      this function handles CAN1 RX0 exception
  11.     \param[in]  none
  12.     \param[out] none
  13.     \retval     none
  14. */
  15. void CAN1_RX1_IRQHandler(void)
  16. {
  17. //    printf("get!!\r\n");
  18.     /* check the receive message */
  19.     can_message_receive(CAN1, CAN_FIFO1, &g_receive_message);
  20. //    if((0xaabb == g_receive_message.rx_efid)&&(CAN_FF_EXTENDED == g_receive_message.rx_ff) && (8 == g_receive_message.rx_dlen)){
  21.         receive_flag = SET;
  22. //    }
  23. }
使用debug模式看了CAN_FW中激活的滤波器,发现没有打√的,是不是就代表没激活,导致我进不去中断。图片如图
12106685e581cc48fa.png
9634685e581d9f2dd.png
FIFO1的相应中断也开了,能接收到数据
98546685e58627bae6.png
各位大佬,请求指点迷津。




评论

大佬们求支招  发表于 2025-6-28 11:03
 楼主| 左左左啊 发表于 2025-7-1 16:04 | 显示全部楼层
问题已解决,是启动文件没有配置好
liangzidefannao 发表于 2025-8-19 17:21 | 显示全部楼层
左左左啊 发表于 2025-7-1 16:04
问题已解决,是启动文件没有配置好

问下是启动文件哪里需要配置呀
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部