**打开 e² studio 软件,依次点击 **文件
- 新建
- 瑞萨 C/C++ 项目
- Renesas RA
**依次进行工程命名,路径设置,FSP版本,目标开发板选择,芯片型号选择 **R7FA2E1A72DFL
,工具链选择 GNU ARM Embedded
**进入 FSP 配置界面,选择 **Stacks
- New Stack
- Connectivity
- UART (r_sci_uart)
串口通信属性设置,选中新建的 Stack ,进入属性栏,修改相关参数
**波特率 115200,回调函数定义为 **user_uart_callback
.
串口通信引脚定义,在图形化配置界面,右键 P101 和 P100 分别选择对应的串口发送和接收定义选项。
若 Pins 引脚定义存在冲突,则定位至冲突位置,将其操作模式修改为 Custom 自定义即可。
**检查设置完成,点击 **Generate Project Content
将配置参数编译至工程文件。
**使用 **R_SCI_UART_Open()
函数进行配置,开启和初始化 UART;
fsp_err_t err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
assert(FSP_SUCCESS == err);
**发送完毕后使用 **UART_EVENT_TX_COMPLETE
函数进行判断;
volatile bool uart_send_complete_flag = false;
void user_uart_callback (uart_callback_args_t * p_args)
{
if(p_args->event == UART_EVENT_TX_COMPLETE)
{
uart_send_complete_flag = true;
}
}
**使用 **R_SCI_UART_Write()
函数进行串口数据输出;
unsigned char buff[]="RA E2STUDIO";
uint8_t buff_len = strlen(buff);
err = R_SCI_UART_Write(&g_uart0_ctrl, buff, buff_len);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
**使用 **sprintf
函数输出字符串,函数声明
int sprintf(char *string, char *format [,argument,...]);
sprintf
的具体使用
sprintf(send_buff, "\nHello World!.\n");
uint8_t len = strlen(send_buff);
err = R_SCI_UART_Write(&g_uart0_ctrl, send_buff, len);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
memset(send_buff, '\0', sizeof(100));
**此外还可以使用 **printf
函数实现串口打印。
设置 e² studio 堆栈
重定向 printf 设置
**打开 **项目
- C/C++ Project Settings
界面,
**依次进入 **C/C++ 构建
- 设置
- 工具设置
- GNU Arm Cross C Linker
- Miscellaneous
,勾选 Use float with nano printf (-u _printf_float)
选项以便使用浮点。
printf 输出重定向至串口
**注意添加头文件 **#include <stdio.h>
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
串口打印输出函数定义
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;i<size;i++)
{
__io_putchar(*pBuffer++);
}
return size;
}
printf 输出
int int_i=55;
float float_i=66.20f;
char char_i[]="hello e2studio";
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);
}
#include "hal_data.h"
#include <stdio.h>
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
fsp_err_t err = FSP_SUCCESS;
unsigned char send_buff[100];
volatile bool uart_send_complete_flag = false;
void user_uart_callback (uart_callback_args_t * p_args)
{
if(p_args->event == UART_EVENT_TX_COMPLETE)
{
uart_send_complete_flag = true;
}
}
#ifdef __GNUC__ // UART redirection
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;i<size;i++)
{
__io_putchar(*pBuffer++);
}
return size;
}
void hal_entry(void)
{
/* TODO: add your own code here */
/* Open the transfer instance with initial configuration. */
err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
assert(FSP_SUCCESS == err);
unsigned char buff[]="RA E2STUDIO";
uint8_t buff_len = strlen(buff);
err = R_SCI_UART_Write(&g_uart0_ctrl, buff, buff_len);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
sprintf(send_buff, "\nHello World!.\n");
uint8_t len = strlen(send_buff);
err = R_SCI_UART_Write(&g_uart0_ctrl, send_buff, len);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
memset(send_buff, '\0', sizeof(100));
int int_i=55;
float float_i=66.20f;
char char_i[]="hello e2studio";
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
}
**在代码文件中添加库函数可通过 **拖拽 方式:
**点击选择项目根目录中的 **Developer Assistance
- HAL/Common
目录中的目标库函数,拖拽至代码文件中即可实现快速配置。
**若仅测试串口打印效果,则仅需包含写入函数 **R_SCI_UART_Write
即可,代码如下
#include "hal_data.h"
#include <stdio.h>
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
fsp_err_t err = FSP_SUCCESS;
/* Callback function */
volatile bool uart_send_complete_flag = false;
void user_uart_callback(uart_callback_args_t *p_args)
{
/* TODO: add your own code here */
if(p_args->event == UART_EVENT_TX_COMPLETE)
{
uart_send_complete_flag = true;
}
}
uint8_t t[3] = {0xaa,0xbb,0xcc};
void hal_entry(void)
{
/* TODO: add your own code here */
/* Open the transfer instance with initial configuration. */
err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
assert(FSP_SUCCESS == err);
while(1)
{
err = R_SCI_UART_Write(&g_uart0_ctrl, t, 3);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
效果如下
**进一步使用 **sprintf
函数,代码如下
unsigned char send_buff[100];
void hal_entry(void)
{
/* TODO: add your own code here */
/* Open the transfer instance with initial configuration. */
err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
assert(FSP_SUCCESS == err);
while(1)
{
sprintf(send_buff, "Hello World!\n");
uint8_t len = strlen(send_buff);
err = R_SCI_UART_Write(&g_uart0_ctrl, send_buff, len);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
memset(send_buff, '\0', sizeof(100));
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
效果如下
**注意回调函数的使用,头文件 **#include <stdio.h>
的添加
#include <stdio.h>
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;i<size;i++)
{
__io_putchar(*pBuffer++);
}
return size;
}
void hal_entry(void)
{
/* TODO: add your own code here */
/* Open the transfer instance with initial configuration. */
//err = R_SCI_UART_Open(&g_uart0_ctrl, &g_uart0_cfg);
//assert(FSP_SUCCESS == err);
while(1)
{
/*--------- printf -----------*/
int int_i = 55;
float float_i = 66.20f;
char char_i[] = "Hello e2studio";
printf("int_i=%d\n",int_i);
printf("float_i=%.2f\n",float_i);
printf("char_i='%s'\n",char_i);
R_BSP_SoftwareDelay(300, BSP_DELAY_UNITS_MILLISECONDS);
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
效果如下
运行出错,始终没有找到错误原因,请各位路过的大神指点……
参考: .
**这里采用生成 **hex 固件并连接 PWLink2 烧录器,配合 PowerWriter 软件烧录固件。
1.硬件连接,将烧录器与板载 SWD 接口对应连接,注意二者的串口线 RXD 与 TXD 交叉连接;
**2.打开 PowerWriter 软件,选择目标芯片型号 **Renesas
- RA2E1
- R7FA2E1A7xx
点击应用设置,注意到左下角显示目标芯片已连接;
**3.进入 Code Flash 标签页面,点击 **添加固件
选择目标 hex 文件路径并 应用固件
,点击 自动
即可将 hex 下载至芯片;
**4.点击软件上的 **串口
快捷按钮,打开串口发送与接收界面,配置相关参数,打开串口,此时 SWD 连接自动断开;
**5.短按板载 **RESET
按键使芯片复位即可运行程序,接收芯片发送的十六进制代码 AA BB CC
.
更多回帖