查看: 533|回复: 1

【瑞萨RA4系列开发板体验】keil重写printf

[复制链接]

116

主题

133

帖子

3768

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3768
发表于 2023-2-1 12:01:35 | 显示全部楼层 |阅读模式
【瑞萨RA4系列开发板体验】keil重写printf
作者:lugl

昨天重装了一下e2_studio,结果Renesas_Flash_Programmer死活装不上了,安装时提示已经安装,注册表也有一万个安装,删也删不完。考虑还是用回到MDK。参照@ jf_92517703大佬的作品,我这里重新写了一遍printf。
这里配置用SCI9来做debug,这样就不用外接uatr转TTL了。
1、先配置systemDBUG,这样才可以选择SCI9

2、SCI9选择如下图:

3、Stack启用UART

4、修改通道数、回调函数:

5、选择PIN为输出到板载的CH340G

6、最后生成keil工程后,找到目录,打开工程:

7、新建bsp_debug_uart.c、bsp_debug_uart.h(大部分是抄@ jf_92517703)部分做了修改:
bsp_debug_uart.c:
  1. #include "bsp_debug_uart.h"
  2. /* 调试串口 UART4 初始化 */
  3. void bsp_uart_init(void)
  4. {
  5.         fsp_err_t err = FSP_SUCCESS;
  6.         err = R_SCI_UART_Open (&g_uart9_ctrl, &g_uart9_cfg);
  7.         assert(FSP_SUCCESS == err);
  8. }

  9. /* 发送完成标志 */
  10. volatile bool uart_send_complete_flag = false;
  11. /* 串口中断回调 */
  12. void user_uart_callback (uart_callback_args_t * p_args)
  13. {
  14.         switch (p_args->event)
  15.         {
  16.                 case UART_EVENT_RX_CHAR:
  17.                 {
  18.                 /* 把串口接收到的数据发送回去 */
  19.                 R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&(p_args->data), 1);
  20.                 break;
  21.                 }
  22.                 case UART_EVENT_TX_COMPLETE:
  23.                 {
  24.                 uart_send_complete_flag = true;
  25.                 break;
  26.                 }
  27.                 default:
  28.                 break;
  29.         }
  30. }       

  31. /* 重定向 printf 输出 */
  32. #if defined __GNUC__ && !defined __clang__
  33. int _write(int fd, char *pBuffer, int size); //防止编译警告
  34. int _write(int fd, char *pBuffer, int size)
  35. {
  36.         (void)fd;
  37.         R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)pBuffer, (uint32_t)size);
  38.         while(uart_send_complete_flag == false);
  39.         uart_send_complete_flag = false;
  40.         return size;
  41. }
  42. #else
  43. int fputc(int ch, FILE *f)
  44. {
  45.         (void)f;
  46.         R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&ch, 1);
  47.         while(uart_send_complete_flag == false);
  48.         uart_send_complete_flag = false;
  49.         return ch;
  50. }
  51. #endif
复制代码
bsp_debug_uart.h
  1. #ifndef __BSP_UART_H__
  2. #define __BSP_UART_H__

  3. #include "hal_data.h"
  4. #include "stdio.h"
  5. void bsp_uart_init(void);

  6. #endif
复制代码
8、然后在keil下新建目录user,并把bsp_debug_uart.c加进去:

9、hal_enty.c主程序如下:
  1. #include "hal_data.h"
  2. #include <stdio.h>
  3. #include "bsp_debug_uart.h"

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


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

  13.   bsp_uart_init();

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

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

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

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

  48.     if (BSP_WARM_START_POST_C == event)
  49.     {
  50.         /* C runtime environment and system clocks are setup. */

  51.         /* Configure pins. */
  52.         R_IOPORT_Open(&g_ioport_ctrl, g_ioport.p_cfg);
  53.     }
  54. }

  55. #if BSP_TZ_SECURE_BUILD

  56. BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

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

  60. }
  61. #endif
复制代码
10、接DAPlink接到SWD、SCK接品上,下载程序就可以了,运行效果如图:

【感受】由于没有钱买jlink10,还是用Keil+DAPlink比较好。学习了前面大佬的作品,自己才能入门。

本帖子中包含更多资源

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

x
回复

使用道具 举报

3

主题

195

帖子

1241

积分

金牌会员

Rank: 6Rank: 6

积分
1241
发表于 2023-3-14 11:15:43 | 显示全部楼层

不错,学习一下
回复

使用道具 举报

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

本版积分规则

用户排行榜

RA助手

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

RA_Lance

主题: 92帖子:132精华:9

lugl

主题: 38帖子:126精华:0

xujiwei263

主题: 16帖子:73精华:0

books咦

主题: 11帖子:11精华:2

Juggernaut

主题: 9帖子:95精华:0
快速回复 返回顶部 返回列表