|
【瑞萨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:
- #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;
- }
- #else
- int 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_HEADER
- void 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_BUILD
- BSP_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比较好。学习了前面大佬的作品,自己才能入门。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|