查看: 1057|回复: 1

【瑞萨RA4系列开发板体验】CAN总线测试

[复制链接]

116

主题

134

帖子

3790

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3790
发表于 2023-2-1 16:01:29 | 显示全部楼层 |阅读模式
【瑞萨RA4系列开发板体验】CAN总线测试
作者:lugl

在前一篇printf的基础上增加CAN的配置,根据@jyaxz 的帖子以及example中can的示例修改而成:
1、在keil中配置好can,然后新建bsp_can.c、bsp_can.h,函数代码如下:
bsp_can.c
  1. #include "bsp_can.h"
  2. #include "bsp_debug_uart.h"
  3. #define RESET_VALUE             (0x00)

  4. /***********************************************************************************************************************
  5. * Private global variables
  6. **********************************************************************************************************************/
  7. /* Flags, set from Callback function */
  8. #define WAIT_TIME                       (5000U)            //wait time value
  9. #define CAN_DESTINATION_MAILBOX_3       (3U)               //destination mail box number
  10. #define CAN_MAILBOX_NUMBER_0            (0U)               //mail box number
  11. #define CAN_FRAME_TRANSMIT_DATA_BYTES   (8U)               //data length
  12. #define ZERO                            (0U)
  13. #define NULL_CHAR                       ('\0')
  14. /***********************************************************************************************************************
  15. * Private global variables
  16. **********************************************************************************************************************/
  17. /* Flags, set from Callback function */
  18. static volatile bool b_can_tx = false;                  //CAN transmission status
  19. static volatile bool b_can_rx = false;                  //CAN receive status
  20. static volatile bool b_can_err = false;                 //CAN error status
  21. /* CAN frames for tx and rx */
  22. static can_frame_t g_can_tx_frame = {0x00};      //CAN transmit frame
  23. static can_frame_t g_can_rx_frame = {0x00};      //CAN receive frame

  24. /***********************************************************************************************************************
  25. * Private local functions
  26. **********************************************************************************************************************/
  27. static void can_deinit(void);
  28. void R_BSP_WarmStart(bsp_warm_start_event_t event);


  29. static void can_deinit(void)
  30. {
  31.     fsp_err_t err = FSP_SUCCESS;
  32.     err = R_CAN_Close(&g_can0_ctrl);
  33.     if (FSP_SUCCESS != err)
  34.     {
  35.         printf("\r\n **CAN Close API failed**");
  36.     }
  37. }

  38. /* 调试串口 UART4 初始化 */
  39. void bsp_can0_init(void)
  40. {
  41.   fsp_err_t err = FSP_SUCCESS;
  42.     /* Initialize CAN module */
  43.     err = R_CAN_Open(&g_can0_ctrl, &g_can0_cfg);
  44.     /* Error trap */
  45.     if(FSP_SUCCESS != err)
  46.     {
  47.         printf("\r\nCAN Open API failed");
  48.         printf(err);
  49.     }
  50. }

  51. /*******************************************************************************************************************//**
  52. * This function is called when an CAN event is occurred and SET the respective flags.
  53. **********************************************************************************************************************/
  54. void can_callback(can_callback_args_t *p_args)
  55. {
  56.     switch (p_args->event)
  57.     {
  58.         case CAN_EVENT_TX_COMPLETE:
  59.         {
  60.             b_can_tx = true;        //set flag bit
  61.             break;
  62.         }

  63.         case CAN_EVENT_RX_COMPLETE:
  64.         {
  65.             b_can_rx = true;
  66.             memcpy(&g_can_rx_frame, &p_args->frame, sizeof(can_frame_t));  //copy the received data to rx_frame
  67.             break;
  68.         }

  69. //        case CAN_EVENT_MAILBOX_MESSAGE_LOST:    //overwrite/overrun error event
  70. //          break;
  71. //        case CAN_EVENT_BUS_RECOVERY:            //Bus recovery error event
  72. //        case CAN_EVENT_ERR_BUS_OFF:             //error Bus Off event
  73. //        case CAN_EVENT_ERR_PASSIVE:             //error passive event
  74. //        case CAN_EVENT_ERR_WARNING:             //error warning event
  75. //        case CAN_EVENT_ERR_BUS_LOCK:            //error bus lock
  76. //        case CAN_EVENT_ERR_CHANNEL:             //error channel
  77. //        case CAN_EVENT_ERR_GLOBAL:              //error global
  78. //        case CAN_EVENT_TX_ABORTED:              //error transmit abort
  79. //        case CAN_EVENT_TX_FIFO_EMPTY:           //error transmit FIFO is empty
  80. //        case CAN_EVENT_FIFO_MESSAGE_LOST:       //error FIFO message lost
  81. //        {
  82. //            b_can_err = true;                   //set flag bit
  83. //            break;
  84. //        }

  85.     }
  86. }

  87. void can_send(void)
  88. {
  89.   fsp_err_t err = FSP_SUCCESS;
  90.   uint32_t time_out = WAIT_TIME;                                      // time out
  91.   fsp_pack_version_t version = {RESET_VALUE};
  92.   uint8_t can_tx_msg[CAN_FRAME_TRANSMIT_DATA_BYTES] =  {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};       //data to be load in tx_frame while transmitting
  93.   uint8_t can_rx_msg[CAN_FRAME_TRANSMIT_DATA_BYTES] =  {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};      //data to be load in rx_frame while acknowledging
  94.   /* Initialize CAN module */
  95.     err = R_CAN_Open(&g_can0_ctrl, &g_can0_cfg);
  96.     /* Error trap */
  97.     if(FSP_SUCCESS != err)
  98.     {
  99.         printf("\r\nCAN Open API failed");
  100.         printf(err);
  101.     }

  102.     printf("\r\nTo start CAN transmission, please enter any key on RTTViewer\r\n");

  103.     g_can_tx_frame.id = CAN_DESTINATION_MAILBOX_3;
  104.     g_can_tx_frame.type = CAN_FRAME_TYPE_DATA;
  105.     g_can_tx_frame.data_length_code = CAN_FRAME_TRANSMIT_DATA_BYTES;
  106.    
  107.     /* copy the tx data frame with TX_MSG */
  108.     memcpy((uint8_t*)&g_can_tx_frame.data[ZERO], (uint8_t*)&can_tx_msg[ZERO], CAN_FRAME_TRANSMIT_DATA_BYTES);
  109.    
  110.     err = R_CAN_Write(&g_can0_ctrl, CAN_MAILBOX_NUMBER_0, &g_can_tx_frame);
  111.      if (FSP_SUCCESS != err)
  112.       {
  113.           printf("\r\n CAN Write API FAILED");
  114.           can_deinit();
  115.           printf(err);
  116.       }

  117.       /* wait for transmit flag bit to set */
  118.       while ((true != b_can_tx) && (--time_out));
  119.       if (RESET_VALUE == time_out)
  120.       {
  121.           printf("CAN transmission failed due to timeout");
  122.           printf(true);
  123.       }
  124.       /* Reset flag bit */
  125.       b_can_tx = false;
  126.       printf("\r\n CAN transmission is successful");
  127.       printf("\r\n To start CAN transmission, please enter any key on RTTViewer\n");
  128.   
  129. }
复制代码
bsp_can.h:
  1. #ifndef __BSP_CAN_H__
  2. #define __BSP_CAN_H__

  3. #include "hal_data.h"
  4. #include "stdio.h"



  5. void bsp_can0_init(void);
  6. void can_send(void);

  7. #endif
复制代码
修如hal_enty.c如下:
  1. #include "hal_data.h"
  2. #include <stdio.h>
  3. #include "bsp_debug_uart.h"
  4. #include "bsp_can.h"

  5. FSP_CPP_HEADER
  6. void R_BSP_WarmStart(bsp_warm_start_event_t event);
  7. FSP_CPP_FOOTER


  8. /*******************************************************************************************************************//**
  9. * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
  10. * is called by main() when no RTOS is used.
  11. **********************************************************************************************************************/
  12. void hal_entry(void)
  13. {

  14.   bsp_uart_init();
  15.   bsp_can0_init();

  16.   char char_i[]="hello e2studio";
  17.   int int_i=55;
  18.   float float_i=66.20f;
  19.   printf("hello RA4\r\n");
  20.   while(1)
  21.   {
  22.     can_send();
  23.     printf("int_i=%d\n",int_i);
  24.     printf("float_i=%.2f\n",float_i);
  25.     printf("char_i='%s'\n",char_i);
  26.     R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);
  27.       // NOLINT100->160
  28.   }
  29. #if BSP_TZ_SECURE_BUILD
  30.     /* Enter non-secure code */
  31.     R_BSP_NonSecureEnter();
  32. #endif
  33. }

  34. /*******************************************************************************************************************//**
  35. * This function is called at various points during the startup process.  This implementation uses the event that is
  36. * called right before main() to set up the pins.
  37. *
  38. * @param[in]  event    Where at in the start up process the code is currently at
  39. **********************************************************************************************************************/
  40. void R_BSP_WarmStart (bsp_warm_start_event_t event)
  41. {
  42.     if (BSP_WARM_START_RESET == event)
  43.     {
  44. #if BSP_FEATURE_FLASH_LP_VERSION != 0

  45.         /* Enable reading from data flash. */
  46.         R_FACI_LP->DFLCTL = 1U;

  47.         /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
  48.          * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
  49. #endif
  50.     }

  51.     if (BSP_WARM_START_POST_C == event)
  52.     {
  53.         /* C runtime environment and system clocks are setup. */

  54.         /* Configure pins. */
  55.         R_IOPORT_Open(&g_ioport_ctrl, g_ioport.p_cfg);
  56.     }
  57. }

  58. #if BSP_TZ_SECURE_BUILD

  59. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

  60. /* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
  61. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
  62. {

  63. }
  64. #endif
复制代码
下载后,用TTL转CAN与CAN分析仪接好,效果如下:


【注】本来上面截了好多图的,一不小心按了一下返回,结果都没有了,最好只能把代码帖出来。
下一步,与迪文屏进行can通信,展示数据。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

3

主题

195

帖子

1178

积分

金牌会员

Rank: 6Rank: 6

积分
1178
发表于 2023-3-14 11:14:41 | 显示全部楼层

不错,学习一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

用户排行榜

RA助手

主题: 116帖子:134精华:0

RA_Lance

主题: 93帖子:136精华:9

lugl

主题: 52帖子:150精华:1

xujiwei263

主题: 18帖子:86精华:0

Juggernaut

主题: 12帖子:118精华:0

books咦

主题: 11帖子:11精华:2
快速回复 返回顶部 返回列表