查看: 928|回复: 2

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

[复制链接]

38

主题

126

帖子

9358

积分

论坛元老

Rank: 8Rank: 8

积分
9358
发表于 2022-12-10 07:49:48 | 显示全部楼层 |阅读模式
昨天重装了一下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:
#include "bsp_debug_uart.h"/* 调试串口 UART4 初始化 */void bsp_uart_init(void){        fsp_err_t err = FSP_SUCCESS;        err = R_SCI_UART_Open (&g_uart9_ctrl, &g_uart9_cfg);        assert(FSP_SUCCESS == err);}/* 发送完成标志 */volatile bool uart_send_complete_flag = false;/* 串口中断回调 */void user_uart_callback (uart_callback_args_t * p_args){        switch (p_args->event)        {                case UART_EVENT_RX_CHAR:                {                /* 把串口接收到的数据发送回去 */                R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&(p_args->data), 1);                break;                }                case UART_EVENT_TX_COMPLETE:                {                uart_send_complete_flag = true;                break;                }                default:                break;        }}        /* 重定向 printf 输出 */#if defined __GNUC__ && !defined __clang__int _write(int fd, char *pBuffer, int size); //防止编译警告int _write(int fd, char *pBuffer, int size){        (void)fd;        R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)pBuffer, (uint32_t)size);        while(uart_send_complete_flag == false);        uart_send_complete_flag = false;        return size;}#elseint fputc(int ch, FILE *f){        (void)f;        R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&ch, 1);        while(uart_send_complete_flag == false);        uart_send_complete_flag = false;        return ch;}#endif
bsp_debug_uart.h
#ifndef __BSP_UART_H__#define __BSP_UART_H__#include "hal_data.h"#include "stdio.h"void bsp_uart_init(void);#endif
8、然后在keil下新建目录user,并把bsp_debug_uart.c加进去:

9、hal_enty.c主程序如下:
#include "hal_data.h"#include <stdio.h>#include "bsp_debug_uart.h"FSP_CPP_HEADERvoid R_BSP_WarmStart(bsp_warm_start_event_t event);FSP_CPP_FOOTER/*******************************************************************************************************************//** * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function * is called by main() when no RTOS is used. **********************************************************************************************************************/void hal_entry(void){  bsp_uart_init();  char char_i[]="hello e2studio";  int int_i=55;  float float_i=66.20f;  printf("hello RA4\r\n");  while(1)  {    printf("int_i=%d\n",int_i);    printf("float_i=%.2f\n",float_i);    printf("char_i='%s'\n",char_i);    R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);      // NOLINT100->160  }#if BSP_TZ_SECURE_BUILD    /* Enter non-secure code */    R_BSP_NonSecureEnter();#endif}/*******************************************************************************************************************//** * This function is called at various points during the startup process.  This implementation uses the event that is * called right before main() to set up the pins. * * @param[in]  event    Where at in the start up process the code is currently at **********************************************************************************************************************/void R_BSP_WarmStart (bsp_warm_start_event_t event){    if (BSP_WARM_START_RESET == event)    {#if BSP_FEATURE_FLASH_LP_VERSION != 0        /* Enable reading from data flash. */        R_FACI_LP->DFLCTL = 1U;        /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and         * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */#endif    }    if (BSP_WARM_START_POST_C == event)    {        /* C runtime environment and system clocks are setup. */        /* Configure pins. */        R_IOPORT_Open(&g_ioport_ctrl, g_ioport.p_cfg);    }}#if BSP_TZ_SECURE_BUILDBSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable (){}#endif
10、接DAPlink接到SWD、SCK接品上,下载程序就可以了,运行效果如图:
【感受】由于没有钱买jlink10,还是用Keil+DAPlink比较好。学习了前面大佬的作品,自己才能入门。

回复

使用道具 举报

1

主题

3

帖子

379

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
379
发表于 2022-12-15 09:45:55 | 显示全部楼层

111
回复

使用道具 举报

3

主题

195

帖子

1140

积分

金牌会员

Rank: 6Rank: 6

积分
1140
发表于 2023-3-14 11:51:09 | 显示全部楼层

不错,学习一下
回复

使用道具 举报

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

本版积分规则

用户排行榜

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
快速回复 返回顶部 返回列表