#申请原创# @21小跑堂 @21小跑堂 @21小跑堂
往期回顾
【CPKCOR-RA8D1B核心板】1、板卡基本信息
【CPKCOR-RA8D1B核心板】2、搭建开发环境
【CPKCOR-RA8D1B核心板】3、离不开的点灯——GPIO输出操作
【CPKCOR-RA8D1B核心板】4、另一种闪烁——非阻塞式闪烁
【CPKCOR-RA8D1B核心板】5、不一样的点灯系列——点呼吸灯
随着我们使用的功能越来越多,有一些装填判断已经不是那么直观了,比如一些复杂功能的步骤都是很多的,那么如果最后的结果和我们的设想不符,到底问题是出在哪里,这个就不好定位,这个时候串口打印功能就非常适合我们的使用,在一些关键地方加上打印可以直观的进行进程的展示。
本核心板具有调试串口功能,也就是说通过调试连接后实际上虚拟串口也形成了,供电上可以看到:
看一下原理图:
实际上对应MCU的Uart3。
串口通讯(Serial Communication)是一种设备间非常常用的串行通讯方式,因为它简单便捷,因此大部分电子设备都支持该通讯方式,电子工程师在调试设备时也经常使用该通讯方式输出调试信息。 在计算机科学里,大部分复杂的问题都可以通过分层来简化。如芯片被分为内核层和片上外设。对于通讯协议, 我们也以分层的方式来理解,最基本的是把它分为物理层和协议层。物理层规定通讯系统中具有机械、电子功能部分的特性,确保原始数据在物理媒体的传输。协议层主要规定通讯逻辑,统一收发双方的数据打包、解包标准。
SCI(Serial Communications Interface),意为串行通信接口, 是相对与并行通信的概念,是串行通信技术的一种总称,包括了UART,SPI等串行通信技术。
我们在FSP中新建一个串口之前先配置一下引脚:
串口3属于SCI3中的一个分支,然后根据实际使用的引脚进行配置,然后新建串口功能模块:
“属性”窗口中配置 名字(name)、通道(Channel)、回调函数(Callback)名字即可, 引脚(Pins)、波特率(Baud Rate)等其他的属性按照默认的配置即可:
Name
| 名字,根据读者需求设置即可。
| Channel
| 通道,根据SCI号设置即可,例如实验使用SCI4,则这里配置为通道4。
| Data Bits
| 每个字(word)的比特(bit)数,默认为8bits
| Parity
| 校验模式,可选择“Odd”奇校验,“Even”偶校验或“None”无校验。
| Stop Bits
| 停止位,可选1或2bit。
| Baud Rate
| 波特率
| Baud Rate Modulation
| 波特率调制,通过调整时钟周期,以减少申请波特率与实际波特率之间的误差。
| Max Error(%)
| 计算波特率时允许的最大百分比误差。
| Callback
| 回调函数的名字,根据读者需求设置即可。
| Receive Interrupt Priority
| 接收中断优先级
| Transmit Data Empty Interrupt Priority
| 发送数据空中断优先级
| Transmit End Interrupt Priority
| 发送完成中断优先级
| Error Interrupt Priority
| 错误中断优先级
| 以上是一些常见的参数的含义,可以根据自己的要求进行修改;
串口打印实际上使用的就是printf函数,这个函数需要使用到堆,默认情况下堆的大小为0,因此我们需要修改堆的大小。 可以在 FSP 配置界面中的“BSP”属性栏的“RA Common”中通过修改“Heap size”来设置堆区大小。 这里需要设置为 8 的整数倍,推荐至少为4K(0x1000):
注意使用SCI的时候要在“Clocks”里面开启SCI的时钟;
这个时候我们就需要进行串口的初始化的调用来启动串口了:
fsp_err_t err = FSP_SUCCESS;
err = R_SCI_B_UART_Open (&g_uart3_ctrl, &g_uart3_cfg);
assert(FSP_SUCCESS == err);
然后就是发送程序的处理,这里有两种方式可以进行数据的发送,可以直接使用 R_SCI_B_UART_Write 函数来将字符串输出到串口:
fsp_err_t R_SCI_B_UART_Write (uart_ctrl_t * const p_api_ctrl, uint8_t const * const p_src, uint32_t const bytes)
参数 p_src 指向要写入的字符串首地址;
参数 bytes 为传入的要写入的字符的数目。
这个在进行复杂多样的串口数据几乎是比较方便和灵活的,不过我们在使用 R_SCI_B_UART_Write 函数需要注意的一些事项:
若使用了 R_SCI_B_UART_Write() 来发送数据, 在数据发送完成之后会导致 uart_send_complete_flag 这个标志位被置位, 因此程序在调用 R_SCI_UART_Write 函数之后需要等待 uart_send_complete_flag 标志位被置位, 然后将该标志位清零。否则当连续调用 R_SCI_UART_Write 函数时可能导致发送数据丢失。
如果只是进行一下字符串的传输,建议printf函数将数据发送到串口,很多情况下使用 printf 函数那样方便,需要添加一段代码来将 printf 输出重定向到串口:
<article data-content="[{"type":"block","id":"zDfd-1728915508213","name":"code","data":{"wrap":false,"tokensPerLine":[],"language":"","theme":"default"},"nodes":[{"type":"block","id":"bIWF-1728915508212","name":"code-line","data":{},"nodes":[{"type":"text","id":"4Q7R-1728915508211","leaves":[{"text":"#if defined __GNUC__ && !defined __clang__","marks":[]}]}]},{"type":"block","id":"0KnP-1728915532904","name":"code-line","data":{},"nodes":[{"type":"text","id":"WQKg-1728915532903","leaves":[{"text":"int _write(int fd, char *pBuffer, int size); //防止编译警告","marks":[]}]}]},{"type":"block","id":"vQWD-1728915532906","name":"code-line","data":{},"nodes":[{"type":"text","id":"Ddjc-1728915532905","leaves":[{"text":"int _write(int fd, char *pBuffer, int size)","marks":[]}]}]},{"type":"block","id":"17dM-1728915532908","name":"code-line","data":{},"nodes":[{"type":"text","id":"iqi2-1728915532907","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"1HkB-1728915532910","name":"code-line","data":{},"nodes":[{"type":"text","id":"TERp-1728915532909","leaves":[{"text":" (void)fd;","marks":[]}]}]},{"type":"block","id":"Utw0-1728915532912","name":"code-line","data":{},"nodes":[{"type":"text","id":"gmf2-1728915532911","leaves":[{"text":" R_SCI_UART_Write(&g_uart3_ctrl, (uint8_t *)pBuffer, (uint32_t)size);","marks":[]}]}]},{"type":"block","id":"6LmN-1728915532914","name":"code-line","data":{},"nodes":[{"type":"text","id":"Kk6v-1728915532913","leaves":[{"text":" while(uart_send_complete_flag == false);","marks":[]}]}]},{"type":"block","id":"w2dI-1728915532916","name":"code-line","data":{},"nodes":[{"type":"text","id":"oJfV-1728915532915","leaves":[{"text":" uart_send_complete_flag = false;","marks":[]}]}]},{"type":"block","id":"q5V3-1728915532918","name":"code-line","data":{},"nodes":[{"type":"text","id":"4Hwp-1728915532917","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"wmzF-1728915532920","name":"code-line","data":{},"nodes":[{"type":"text","id":"sU5f-1728915532919","leaves":[{"text":" return size;","marks":[]}]}]},{"type":"block","id":"QBj3-1728915532922","name":"code-line","data":{},"nodes":[{"type":"text","id":"VHSl-1728915532921","leaves":[{"text":"}","marks":[]}]}]},{"type":"block","id":"Dh9s-1728915532924","name":"code-line","data":{},"nodes":[{"type":"text","id":"oDeV-1728915532923","leaves":[{"text":"#else","marks":[]}]}]},{"type":"block","id":"Fy2Z-1728915532926","name":"code-line","data":{},"nodes":[{"type":"text","id":"IpFa-1728915532925","leaves":[{"text":"int fputc(int ch, FILE *f)","marks":[]}]}]},{"type":"block","id":"dXGp-1728915532928","name":"code-line","data":{},"nodes":[{"type":"text","id":"AdRN-1728915532927","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"4lSr-1728915532930","name":"code-line","data":{},"nodes":[{"type":"text","id":"zY96-1728915532929","leaves":[{"text":" (void)f;","marks":[]}]}]},{"type":"block","id":"yIBk-1728915532932","name":"code-line","data":{},"nodes":[{"type":"text","id":"2SL3-1728915532931","leaves":[{"text":" R_SCI_B_UART_Write(&g_uart4_ctrl, (uint8_t *)&ch, 1);","marks":[]}]}]},{"type":"block","id":"oslL-1728915532934","name":"code-line","data":{},"nodes":[{"type":"text","id":"Pn2g-1728915532933","leaves":[{"text":" while(uart_send_complete_flag == false);","marks":[]}]}]},{"type":"block","id":"EbfE-1728915532937","name":"code-line","data":{},"nodes":[{"type":"text","id":"WKoN-1728915532936","leaves":[{"text":" uart_send_complete_flag = false;","marks":[]}]}]},{"type":"block","id":"ICQ0-1728915532939","name":"code-line","data":{},"nodes":[{"type":"text","id":"OuDi-1728915532938","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"uIKw-1728915532941","name":"code-line","data":{},"nodes":[{"type":"text","id":"R9Su-1728915532940","leaves":[{"text":" return ch;","marks":[]}]}]},{"type":"block","id":"RtF0-1728915532943","name":"code-line","data":{},"nodes":[{"type":"text","id":"nIK5-1728915532942","leaves":[{"text":"}","marks":[]}]}]},{"type":"block","id":"KPYT-1728915532945","name":"code-line","data":{},"nodes":[{"type":"text","id":"Fdk2-1728915532944","leaves":[{"text":"#endif","marks":[]}]}]}],"state":{}}]"><div yne-bulb-block="code" data-theme="default" style="white-space-collapse: preserve;" data-language="">#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_uart3_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_B_UART_Write(&g_uart4_ctrl, (uint8_t *)&ch, 1);
while(uart_send_complete_flag == false);
uart_send_complete_flag = false;
return ch;
}
#endif</div></article>
然后还要在下图位置打开:
还有一个比较容易忽略的一点就是,不要忘记调用#include"stdio.h"。
在配置的时候我们定义的了一个Callback函数,这个函数是实际上就是我们串口中断的回调函数,我们需要重写一下:
<article data-content="[{"type":"block","id":"vLoq-1728915691229","name":"code","data":{"wrap":false,"tokensPerLine":[],"language":"","theme":"default"},"nodes":[{"type":"block","id":"7gqp-1728915691228","name":"code-line","data":{},"nodes":[{"type":"text","id":"G5rW-1728915691227","leaves":[{"text":"volatile bool uart_send_complete_flag = false;","marks":[]}]}]},{"type":"block","id":"Mvcz-1728915692112","name":"code-line","data":{},"nodes":[{"type":"text","id":"gg3C-1728915692111","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"3YSn-1728915692114","name":"code-line","data":{},"nodes":[{"type":"text","id":"L65r-1728915692113","leaves":[{"text":"/* 串口中断回调 */","marks":[]}]}]},{"type":"block","id":"bmuL-1728915692116","name":"code-line","data":{},"nodes":[{"type":"text","id":"XPAH-1728915692115","leaves":[{"text":"void g_uart3_Callback (uart_callback_args_t * p_args){","marks":[]}]}]},{"type":"block","id":"x6sO-1728915692118","name":"code-line","data":{},"nodes":[{"type":"text","id":"FUxc-1728915692117","leaves":[{"text":" switch (p_args->event){","marks":[]}]}]},{"type":"block","id":"iSDq-1728915692120","name":"code-line","data":{},"nodes":[{"type":"text","id":"k5gA-1728915692119","leaves":[{"text":" case UART_EVENT_RX_CHAR:{","marks":[]}]}]},{"type":"block","id":"shsw-1728915692122","name":"code-line","data":{},"nodes":[{"type":"text","id":"Zaex-1728915692121","leaves":[{"text":" /* 把串口接收到的数据发送回去 */","marks":[]}]}]},{"type":"block","id":"EV99-1728915692124","name":"code-line","data":{},"nodes":[{"type":"text","id":"1Xqy-1728915692123","leaves":[{"text":" R_SCI_B_UART_Write(&g_uart3_ctrl, (uint8_t *)&(p_args->data), 1);","marks":[]}]}]},{"type":"block","id":"jcbO-1728915692126","name":"code-line","data":{},"nodes":[{"type":"text","id":"abO8-1728915692125","leaves":[{"text":" break;","marks":[]}]}]},{"type":"block","id":"0X71-1728915692128","name":"code-line","data":{},"nodes":[{"type":"text","id":"pGVR-1728915692127","leaves":[{"text":" }","marks":[]}]}]},{"type":"block","id":"dWld-1728915692130","name":"code-line","data":{},"nodes":[{"type":"text","id":"GzD1-1728915692129","leaves":[{"text":" case UART_EVENT_TX_COMPLETE:{","marks":[]}]}]},{"type":"block","id":"b5Gv-1728915692132","name":"code-line","data":{},"nodes":[{"type":"text","id":"GPsC-1728915692131","leaves":[{"text":" uart_send_complete_flag = true;","marks":[]}]}]},{"type":"block","id":"UtBe-1728915692134","name":"code-line","data":{},"nodes":[{"type":"text","id":"lcbd-1728915692133","leaves":[{"text":" break;","marks":[]}]}]},{"type":"block","id":"di7k-1728915692136","name":"code-line","data":{},"nodes":[{"type":"text","id":"RGxd-1728915692135","leaves":[{"text":" }","marks":[]}]}]},{"type":"block","id":"dOve-1728915692138","name":"code-line","data":{},"nodes":[{"type":"text","id":"x8a6-1728915692137","leaves":[{"text":" default:","marks":[]}]}]},{"type":"block","id":"HEG8-1728915692140","name":"code-line","data":{},"nodes":[{"type":"text","id":"tkWy-1728915692139","leaves":[{"text":" break;","marks":[]}]}]},{"type":"block","id":"pPmy-1728915692142","name":"code-line","data":{},"nodes":[{"type":"text","id":"mLA6-1728915692141","leaves":[{"text":" }","marks":[]}]}]},{"type":"block","id":"k97g-1728915692144","name":"code-line","data":{},"nodes":[{"type":"text","id":"rhD2-1728915692143","leaves":[{"text":"}","marks":[]}]}]}],"state":{}}]"><div yne-bulb-block="code" data-theme="default" style="white-space-collapse: preserve;" data-language="">volatile bool uart_send_complete_flag = false;
/* 串口中断回调 */
void g_uart3_Callback (uart_callback_args_t * p_args){
switch (p_args->event){
case UART_EVENT_RX_CHAR:{
/* 把串口接收到的数据发送回去 */
R_SCI_B_UART_Write(&g_uart3_ctrl, (uint8_t *)&(p_args->data), 1);
break;
}
case UART_EVENT_TX_COMPLETE:{
uart_send_complete_flag = true;
break;
}
default:
break;
}
}</div></article><p> default:</p><p> break;</p><p> }</p><p>}</p>
可以看到这里简单的写了一个回传函数,和中断发送的处理。
接下来我们编译一下发现使用printf的时候提示了如下的错误:
这其实就是一个标准库的使用问题,就目前的使用来说不管是使用STM32CubeIDE还是目前的e2studio,这类最新的基于GCC编译的,貌似都升级了,导致标准库的一些函数无法使用(不兼容),而printf还离不开,最新的IAR也是有这样的一个问题,目前貌似只有keil依然可以直接使用标准库,直接使用printf重定向就可以,而STM32CubeIDE和e2studio需要重写_write,上面的报错说明还有一些基础函数需要定义,为此我们创建一个“printf_redirect.c”,把printf重定向和相关支持函数都放到这里:
<article data-content="[{"type":"block","id":"WqiP-1728916513752","name":"code","data":{"wrap":false,"tokensPerLine":[],"language":"","theme":"default"},"nodes":[{"type":"block","id":"avTc-1728916513751","name":"code-line","data":{},"nodes":[{"type":"text","id":"naYE-1728916513750","leaves":[{"text":"#include \"hal_data.h\"","marks":[]}]}]},{"type":"block","id":"0l5V-1728916526584","name":"code-line","data":{},"nodes":[{"type":"text","id":"M4gi-1728916526583","leaves":[{"text":"#include \"stdio.h\"","marks":[]}]}]},{"type":"block","id":"xdfp-1728916526586","name":"code-line","data":{},"nodes":[{"type":"text","id":"TOyY-1728916526585","leaves":[{"text":"#include <sys/stat.h>","marks":[]}]}]},{"type":"block","id":"nOhd-1728916526588","name":"code-line","data":{},"nodes":[{"type":"text","id":"YXmy-1728916526587","leaves":[{"text":"#include <errno.h>","marks":[]}]}]},{"type":"block","id":"UE3i-1728916526590","name":"code-line","data":{},"nodes":[{"type":"text","id":"Dc0X-1728916526589","leaves":[{"text":"#undef errno","marks":[]}]}]},{"type":"block","id":"KqsE-1728916526592","name":"code-line","data":{},"nodes":[{"type":"text","id":"xJz5-1728916526591","leaves":[{"text":"extern int errno;","marks":[]}]}]},{"type":"block","id":"UUST-1728916526594","name":"code-line","data":{},"nodes":[{"type":"text","id":"7uqr-1728916526593","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"5Lg5-1728916526596","name":"code-line","data":{},"nodes":[{"type":"text","id":"Kb3A-1728916526595","leaves":[{"text":"int _write(int file, char *ptr, int len);","marks":[]}]}]},{"type":"block","id":"3ETm-1728916526598","name":"code-line","data":{},"nodes":[{"type":"text","id":"6tUD-1728916526597","leaves":[{"text":"int _close(int file);","marks":[]}]}]},{"type":"block","id":"nLTK-1728916526600","name":"code-line","data":{},"nodes":[{"type":"text","id":"N1Y8-1728916526599","leaves":[{"text":"int _fstat(int file, struct stat *st);","marks":[]}]}]},{"type":"block","id":"I2bU-1728916526602","name":"code-line","data":{},"nodes":[{"type":"text","id":"NPCH-1728916526601","leaves":[{"text":"int _isatty(int file);","marks":[]}]}]},{"type":"block","id":"m1ph-1728916526604","name":"code-line","data":{},"nodes":[{"type":"text","id":"0wyl-1728916526603","leaves":[{"text":"int _read(int file, char *ptr, int len);","marks":[]}]}]},{"type":"block","id":"BWtA-1728916526606","name":"code-line","data":{},"nodes":[{"type":"text","id":"Yquz-1728916526605","leaves":[{"text":"int _lseek(int file, int ptr, int dir);","marks":[]}]}]},{"type":"block","id":"jPah-1728916526608","name":"code-line","data":{},"nodes":[{"type":"text","id":"dPf7-1728916526607","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"amfp-1728916526610","name":"code-line","data":{},"nodes":[{"type":"text","id":"0O54-1728916526609","leaves":[{"text":"#define DEBUG_SERIAL_TIMEOUT 2000/portTICK_PERIOD_MS","marks":[]}]}]},{"type":"block","id":"SDXy-1728916526612","name":"code-line","data":{},"nodes":[{"type":"text","id":"3rNz-1728916526611","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"umvg-1728916526614","name":"code-line","data":{},"nodes":[{"type":"text","id":"mC2u-1728916526613","leaves":[{"text":"extern uint8_t uart_send_complete_flag;","marks":[]}]}]},{"type":"block","id":"rm7C-1728916526616","name":"code-line","data":{},"nodes":[{"type":"text","id":"6nVo-1728916526615","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"I9ci-1728916526618","name":"code-line","data":{},"nodes":[{"type":"text","id":"zxqk-1728916526617","leaves":[{"text":"int _write(int file, char *ptr, int len)","marks":[]}]}]},{"type":"block","id":"MWj5-1728916526620","name":"code-line","data":{},"nodes":[{"type":"text","id":"OSkX-1728916526619","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"r5kp-1728916526622","name":"code-line","data":{},"nodes":[{"type":"text","id":"BXUK-1728916526621","leaves":[{"text":" fsp_err_t err = FSP_SUCCESS;","marks":[]}]}]},{"type":"block","id":"mumW-1728916526624","name":"code-line","data":{},"nodes":[{"type":"text","id":"ZNkb-1728916526623","leaves":[{"text":" FSP_PARAMETER_NOT_USED(file);","marks":[]}]}]},{"type":"block","id":"s2lc-1728916526626","name":"code-line","data":{},"nodes":[{"type":"text","id":"Roqy-1728916526625","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"q2CQ-1728916526628","name":"code-line","data":{},"nodes":[{"type":"text","id":"SH49-1728916526627","leaves":[{"text":" static bool uart_open = false;","marks":[]}]}]},{"type":"block","id":"d3G4-1728916526630","name":"code-line","data":{},"nodes":[{"type":"text","id":"mYBj-1728916526629","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"N5Su-1728916526632","name":"code-line","data":{},"nodes":[{"type":"text","id":"wTAH-1728916526631","leaves":[{"text":" if (false == uart_open)","marks":[]}]}]},{"type":"block","id":"6ksn-1728916526634","name":"code-line","data":{},"nodes":[{"type":"text","id":"EqGz-1728916526633","leaves":[{"text":" {","marks":[]}]}]},{"type":"block","id":"UlCD-1728916526636","name":"code-line","data":{},"nodes":[{"type":"text","id":"3IHQ-1728916526635","leaves":[{"text":" err = R_SCI_B_UART_Open(&g_uart3_ctrl, &g_uart3_cfg);","marks":[]}]}]},{"type":"block","id":"eUpb-1728916526638","name":"code-line","data":{},"nodes":[{"type":"text","id":"IuXs-1728916526637","leaves":[{"text":" }","marks":[]}]}]},{"type":"block","id":"TSdn-1728916526640","name":"code-line","data":{},"nodes":[{"type":"text","id":"JqkC-1728916526639","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"EHdy-1728916526642","name":"code-line","data":{},"nodes":[{"type":"text","id":"WRc6-1728916526641","leaves":[{"text":" if (FSP_SUCCESS == err)","marks":[]}]}]},{"type":"block","id":"NCJ3-1728916526644","name":"code-line","data":{},"nodes":[{"type":"text","id":"U4a0-1728916526643","leaves":[{"text":" {","marks":[]}]}]},{"type":"block","id":"8gM5-1728916526646","name":"code-line","data":{},"nodes":[{"type":"text","id":"JH6N-1728916526645","leaves":[{"text":" err = R_SCI_B_UART_Write(&g_uart3_ctrl, (uint8_t *)ptr, (uint32_t)len);","marks":[]}]}]},{"type":"block","id":"Ksv9-1728916526648","name":"code-line","data":{},"nodes":[{"type":"text","id":"xhVE-1728916526647","leaves":[{"text":" }","marks":[]}]}]},{"type":"block","id":"4mg8-1728916526650","name":"code-line","data":{},"nodes":[{"type":"text","id":"f5Ji-1728916526649","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"GDsn-1728916526652","name":"code-line","data":{},"nodes":[{"type":"text","id":"xzQo-1728916526651","leaves":[{"text":" if (FSP_SUCCESS != err)","marks":[]}]}]},{"type":"block","id":"xufW-1728916526654","name":"code-line","data":{},"nodes":[{"type":"text","id":"UxgR-1728916526653","leaves":[{"text":" {","marks":[]}]}]},{"type":"block","id":"Iecm-1728916526656","name":"code-line","data":{},"nodes":[{"type":"text","id":"bI9z-1728916526655","leaves":[{"text":" len = -1;","marks":[]}]}]},{"type":"block","id":"I43R-1728916526658","name":"code-line","data":{},"nodes":[{"type":"text","id":"2VY1-1728916526657","leaves":[{"text":" }","marks":[]}]}]},{"type":"block","id":"HzB7-1728916526660","name":"code-line","data":{},"nodes":[{"type":"text","id":"fync-1728916526659","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"vLnk-1728916526662","name":"code-line","data":{},"nodes":[{"type":"text","id":"GYJI-1728916526661","leaves":[{"text":" while(uart_send_complete_flag == 0)","marks":[]}]}]},{"type":"block","id":"9TfF-1728916526664","name":"code-line","data":{},"nodes":[{"type":"text","id":"1Y9C-1728916526663","leaves":[{"text":" {","marks":[]}]}]},{"type":"block","id":"i5IN-1728916526666","name":"code-line","data":{},"nodes":[{"type":"text","id":"ctgx-1728916526665","leaves":[{"text":" R_BSP_SoftwareDelay(10, BSP_DELAY_UNITS_MILLISECONDS);","marks":[]}]}]},{"type":"block","id":"TcUR-1728916526668","name":"code-line","data":{},"nodes":[{"type":"text","id":"hM6l-1728916526667","leaves":[{"text":" }","marks":[]}]}]},{"type":"block","id":"DvGz-1728916526670","name":"code-line","data":{},"nodes":[{"type":"text","id":"zCgG-1728916526669","leaves":[{"text":" uart_send_complete_flag = 0;","marks":[]}]}]},{"type":"block","id":"g8Gr-1728916526672","name":"code-line","data":{},"nodes":[{"type":"text","id":"RH1q-1728916526671","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"p3Ow-1728916526674","name":"code-line","data":{},"nodes":[{"type":"text","id":"6EFV-1728916526673","leaves":[{"text":" return len;","marks":[]}]}]},{"type":"block","id":"4rPG-1728916526676","name":"code-line","data":{},"nodes":[{"type":"text","id":"NNzk-1728916526675","leaves":[{"text":"}","marks":[]}]}]},{"type":"block","id":"I2K5-1728916526678","name":"code-line","data":{},"nodes":[{"type":"text","id":"dIZ1-1728916526677","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"d5Va-1728916526680","name":"code-line","data":{},"nodes":[{"type":"text","id":"cxv6-1728916526679","leaves":[{"text":"int _close(int file)","marks":[]}]}]},{"type":"block","id":"mTnI-1728916526682","name":"code-line","data":{},"nodes":[{"type":"text","id":"qEVb-1728916526681","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"nrdo-1728916526684","name":"code-line","data":{},"nodes":[{"type":"text","id":"0Epc-1728916526683","leaves":[{"text":" FSP_PARAMETER_NOT_USED(file);","marks":[]}]}]},{"type":"block","id":"6Zl8-1728916526686","name":"code-line","data":{},"nodes":[{"type":"text","id":"zs04-1728916526685","leaves":[{"text":" return -1;","marks":[]}]}]},{"type":"block","id":"I5bv-1728916526688","name":"code-line","data":{},"nodes":[{"type":"text","id":"WfYr-1728916526687","leaves":[{"text":"}","marks":[]}]}]},{"type":"block","id":"WwyF-1728916526690","name":"code-line","data":{},"nodes":[{"type":"text","id":"KOZE-1728916526689","leaves":[{"text":"int _fstat(int file, struct stat *st)","marks":[]}]}]},{"type":"block","id":"blpS-1728916526692","name":"code-line","data":{},"nodes":[{"type":"text","id":"lMdt-1728916526691","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"DR9w-1728916526695","name":"code-line","data":{},"nodes":[{"type":"text","id":"ipfS-1728916526694","leaves":[{"text":" FSP_PARAMETER_NOT_USED(file);","marks":[]}]}]},{"type":"block","id":"A4Y1-1728916526697","name":"code-line","data":{},"nodes":[{"type":"text","id":"TGcv-1728916526696","leaves":[{"text":" st->st_mode = S_IFCHR;","marks":[]}]}]},{"type":"block","id":"SO0o-1728916526699","name":"code-line","data":{},"nodes":[{"type":"text","id":"hoFV-1728916526698","leaves":[{"text":" return 0;","marks":[]}]}]},{"type":"block","id":"hqFf-1728916526701","name":"code-line","data":{},"nodes":[{"type":"text","id":"TMIG-1728916526700","leaves":[{"text":"}","marks":[]}]}]},{"type":"block","id":"Nt69-1728916526703","name":"code-line","data":{},"nodes":[{"type":"text","id":"cr1d-1728916526702","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"JHbg-1728916526705","name":"code-line","data":{},"nodes":[{"type":"text","id":"Xkh0-1728916526704","leaves":[{"text":"int _isatty(int file)","marks":[]}]}]},{"type":"block","id":"aaFB-1728916526707","name":"code-line","data":{},"nodes":[{"type":"text","id":"176b-1728916526706","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"kpqD-1728916526709","name":"code-line","data":{},"nodes":[{"type":"text","id":"qkBn-1728916526708","leaves":[{"text":" FSP_PARAMETER_NOT_USED(file);","marks":[]}]}]},{"type":"block","id":"wD7t-1728916526711","name":"code-line","data":{},"nodes":[{"type":"text","id":"R1mc-1728916526710","leaves":[{"text":" return 1;","marks":[]}]}]},{"type":"block","id":"uvi3-1728916526713","name":"code-line","data":{},"nodes":[{"type":"text","id":"axVc-1728916526712","leaves":[{"text":"}","marks":[]}]}]},{"type":"block","id":"yF7n-1728916526715","name":"code-line","data":{},"nodes":[{"type":"text","id":"RCy6-1728916526714","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"nmwM-1728916526717","name":"code-line","data":{},"nodes":[{"type":"text","id":"zYrV-1728916526716","leaves":[{"text":"int _lseek(int file, int ptr, int dir)","marks":[]}]}]},{"type":"block","id":"Po5k-1728916526719","name":"code-line","data":{},"nodes":[{"type":"text","id":"EzVD-1728916526718","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"MNGK-1728916526721","name":"code-line","data":{},"nodes":[{"type":"text","id":"rjGS-1728916526720","leaves":[{"text":" FSP_PARAMETER_NOT_USED(file);","marks":[]}]}]},{"type":"block","id":"ok1y-1728916526723","name":"code-line","data":{},"nodes":[{"type":"text","id":"pPNo-1728916526722","leaves":[{"text":" FSP_PARAMETER_NOT_USED(ptr);","marks":[]}]}]},{"type":"block","id":"EX0A-1728916526725","name":"code-line","data":{},"nodes":[{"type":"text","id":"3MIj-1728916526724","leaves":[{"text":" FSP_PARAMETER_NOT_USED(dir);","marks":[]}]}]},{"type":"block","id":"UZ6v-1728916526727","name":"code-line","data":{},"nodes":[{"type":"text","id":"2Rjl-1728916526726","leaves":[{"text":" return 0;","marks":[]}]}]},{"type":"block","id":"Pnnk-1728916526729","name":"code-line","data":{},"nodes":[{"type":"text","id":"MER4-1728916526728","leaves":[{"text":"}","marks":[]}]}]},{"type":"block","id":"ZHhd-1728916526731","name":"code-line","data":{},"nodes":[{"type":"text","id":"1zBc-1728916526730","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"nlFk-1728916526733","name":"code-line","data":{},"nodes":[{"type":"text","id":"Jbpc-1728916526732","leaves":[{"text":"int _read(int file, char *ptr, int len)","marks":[]}]}]},{"type":"block","id":"v6Hi-1728916526735","name":"code-line","data":{},"nodes":[{"type":"text","id":"tE9U-1728916526734","leaves":[{"text":"{","marks":[]}]}]},{"type":"block","id":"RTqw-1728916526737","name":"code-line","data":{},"nodes":[{"type":"text","id":"KPnt-1728916526736","leaves":[{"text":" FSP_PARAMETER_NOT_USED(file);","marks":[]}]}]},{"type":"block","id":"PDal-1728916526739","name":"code-line","data":{},"nodes":[{"type":"text","id":"l2Q9-1728916526738","leaves":[{"text":" FSP_PARAMETER_NOT_USED(ptr);","marks":[]}]}]},{"type":"block","id":"kqgL-1728916526741","name":"code-line","data":{},"nodes":[{"type":"text","id":"3rUf-1728916526740","leaves":[{"text":" FSP_PARAMETER_NOT_USED(len);","marks":[]}]}]},{"type":"block","id":"LnBn-1728916526743","name":"code-line","data":{},"nodes":[{"type":"text","id":"viop-1728916526742","leaves":[{"text":" return 0;","marks":[]}]}]},{"type":"block","id":"EvCB-1728916526745","name":"code-line","data":{},"nodes":[{"type":"text","id":"gEtk-1728916526744","leaves":[{"text":"}","marks":[]}]}]}],"state":{}}]"><div yne-bulb-block="code" data-theme="default" style="white-space-collapse: preserve;" data-language="">#include "hal_data.h"
#include "stdio.h"
#include <sys/stat.h>
#include <errno.h>
#undef errno
extern int errno;
int _write(int file, char *ptr, int len);
int _close(int file);
int _fstat(int file, struct stat *st);
int _isatty(int file);
int _read(int file, char *ptr, int len);
int _lseek(int file, int ptr, int dir);
#define DEBUG_SERIAL_TIMEOUT 2000/portTICK_PERIOD_MS
extern uint8_t uart_send_complete_flag;
int _write(int file, char *ptr, int len)
{
fsp_err_t err = FSP_SUCCESS;
FSP_PARAMETER_NOT_USED(file);
static bool uart_open = false;
if (false == uart_open)
{
err = R_SCI_B_UART_Open(&g_uart3_ctrl, &g_uart3_cfg);
}
if (FSP_SUCCESS == err)
{
err = R_SCI_B_UART_Write(&g_uart3_ctrl, (uint8_t *)ptr, (uint32_t)len);
}
if (FSP_SUCCESS != err)
{
len = -1;
}
while(uart_send_complete_flag == 0)
{
R_BSP_SoftwareDelay(10, BSP_DELAY_UNITS_MILLISECONDS);
}
uart_send_complete_flag = 0;
return len;
}
int _close(int file)
{
FSP_PARAMETER_NOT_USED(file);
return -1;
}
int _fstat(int file, struct stat *st)
{
FSP_PARAMETER_NOT_USED(file);
st->st_mode = S_IFCHR;
return 0;
}
int _isatty(int file)
{
FSP_PARAMETER_NOT_USED(file);
return 1;
}
int _lseek(int file, int ptr, int dir)
{
FSP_PARAMETER_NOT_USED(file);
FSP_PARAMETER_NOT_USED(ptr);
FSP_PARAMETER_NOT_USED(dir);
return 0;
}
int _read(int file, char *ptr, int len)
{
FSP_PARAMETER_NOT_USED(file);
FSP_PARAMETER_NOT_USED(ptr);
FSP_PARAMETER_NOT_USED(len);
return 0;
}</div></article>
准备工作以及一些报错问题都解决了,接下来我们可以编译下载了
可以看到串口数据收发正常,在初始化完成后加了一个printf也打印成功,这里就实现了串口通信的基本功能。
|