SCI(Serial Communica
tions Interface),意为串行
通信接口,是相对与并行通信的概念,是串行通
信技术的一种总称。包括了 UART,SPI 等串行通信技术。RA6M5 的 SCI 模块是一个有 10 个通
道的异步/同步串行接口。包含如下功能:
• UART
• 8 位时钟同步接口
• 简易 IIC(只能用作主机)
• 简易 SPI
• 智能卡接口(符合 ISO/IEC 7816-3 国际标准)
• 曼彻斯特接口
• 增强的串行接口
另外,SCI0、SCI3~SCI9 有独立的 FIFO 缓冲区。
野火启明板载的串口模块
在配置界面底部点击 “Stack”,加入串口 UART:
配置串口属性
使用 printf 函数时,需要使用到堆,默认情况下堆的大小为 0,因此我们需要修改堆的大小。可
以在 FSP 配置界面中的“BSP”属性栏的“RA Common”中通过修改“Heap size”来设置堆区大
小。
在 e2 stdio 使用printf的时候,需要修改 C 语言项目设置,
到此串口配置完成。
要使用printf 函数,所以我们需要添加一段代码来将 printf 输出重定向到串口
(UART4)。
- /* 重定向 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_uart0_ctrl, (uint8_t *)&ch, 1);
- while(uart_send_complete_flag == false);
- uart_send_complete_flag = false;
- return ch;
- }
- #endif
hal_entry 入口函数
C 语言程序的入口函数 main 函数调用了 hal_entry 函数。我们在 hal_entry 函数里面编写我们的应
用代码。
- #include "hal_data.h"
- #include
- 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;
- /* 重定向 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_uart0_ctrl, (uint8_t *)&ch, 1);
- while(uart_send_complete_flag == false);
- uart_send_complete_flag = false;
- return ch;
- }
- #endif
- void uart0_callback (uart_callback_args_t * p_args)
- {
- if(p_args->event == UART_EVENT_TX_COMPLETE)
- {
- uart_send_complete_flag = true;
- }
- }
- 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("Hello World!.n");
- R_BSP_SoftwareDelay(1000, BSP_DELAY_UNITS_MILLISECONDS);
- }
- #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
输出结果