查看: 1022|回复: 0

投稿 | 基于IAR搭建RA MCU 串口打印

[复制链接]

92

主题

132

帖子

8万

积分

管理员

11111

Rank: 9Rank: 9Rank: 9

积分
84284
QQ
发表于 2022-10-13 22:02:14 | 显示全部楼层 |阅读模式
投稿:欣瑞利科技@Moxair

1.概述
通过RASC创建一个串口工程,具体配置如下,用户可以根据自己定义来配置串口相关的基本参数。

2.创建工程
STEP1:RASC创建工程


STEP2:选芯片型号跟IDE类型


STEP3:选择芯片型号


STEP4:选择无操作系统


STEP5:勾选确认工程


STEP6:配置串口号跟IO口


STEP7:添加串口驱动


STEP8:配置串口


STEP9:配置串口基本属性


STEP10:生成工程


2.代码
STEP11:打开工程


    STEP12:添加代码



    STEP13:具体代码如下

void hal_entry( void )
{    /* TODO: add your own code here */   
fsp_err_t err = R_SCI_UART_Open( &g_uart9_ctrl, &g_uart9_cfg );   
assert( FSP_SUCCESS == err );   
while ( true )   
{        
printf( "Hello RA MCU \r\n" );   
}
#if BSP_TZ_SECURE_BUILD   
/* Enter non-secure code */   
R_BSP_NonSecureEnter();
#endif
}

STEP14:添加头文件跟串口回调函数


    STEP15:具体代码如下


#include "hal_data.h"
#include "stdio.h"
FSP_CPP_HEADER
void R_BSP_WarmStart( bsp_warm_start_event_t event );
FSP_CPP_FOOTER
uint8_t g_transfer_complete = 0;
void g_uart9_cb( uart_callback_args_t* p_args )
{
    /* Handle the UART event */
    switch ( p_args->event )
    {
        /* Received a character */
        case UART_EVENT_RX_CHAR:
        {
            break;
        }
        /* Receive complete */
        case UART_EVENT_RX_COMPLETE:
        {

            break;
        }
        /* Transmit complete */
        case UART_EVENT_TX_COMPLETE:
        {
            g_transfer_complete = 1;
            break;
        }
        default:
        {
        }
    }
}


STEP16:添加打印接口重定向代码,具体如下
/*******************
*
* Copyright 1998-2017 IAR Systems AB.
*
* This is a template implementation of the "__write" function used by
* the standard library.  Replace it with a system-specific
* implementation.
*
* The "__write" function should output "size" number of bytes from
* "buffer" in some application-specific way.  It should return the
* number of characters written, or _LLIO_ERROR on failure.
*
* If "buffer" is zero then __write should perform flushing of
* internal buffers, if any.  In this case "handle" can be -1 to
* indicate that all handles should be flushed.
*
* The template implementation below assumes that the application
* provides the function "MyLowLevelPutchar".  It should return the
* character written, or -1 on failure.
*
********************/

#include <LowLevelIOInterface.h>

#pragma module_name = "?__write"

int MyLowLevelPutchar(int x)
{
  R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&x, 1);
  while(g_transfer_complete == 0);
  g_transfer_complete = 0;
  return x;

}

/*
* If the __write implementation uses internal buffering, uncomment
* the following line to ensure that we are called with "buffer" as 0
* (i.e. flush) when the application terminates.
*/

size_t __write(int handle, const unsigned char * buffer, size_t size)
{
  /* Remove the #if #endif pair to enable the implementation */
#if 1

  size_t nChars = 0;

  if (buffer == 0)
  {
    /*
     * This means that we should flush internal buffers.  Since we
     * don't we just return.  (Remember, "handle" == -1 means that all
     * handles should be flushed.)
     */
    return 0;
  }

  /* This template only writes to "standard out" and "standard err",
   * for all other file handles it returns failure. */
  if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
  {
    return _LLIO_ERROR;
  }

  for (/* Empty */; size != 0; --size)
  {
    if (MyLowLevelPutchar(*buffer++) < 0)
    {
      return _LLIO_ERROR;
    }

    ++nChars;
  }

  return nChars;

#else

  /* Always return error code when implementation is disabled. */
  return _LLIO_ERROR;

#endif

}


3.工具配置
STEP17:工程配置


STEP18:弹出如下窗口


4.结果
STEP19:接上串口工具,打印如下



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

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

本版积分规则

用户排行榜

RA助手

主题: 116帖子:134精华:0

RA_Lance

主题: 92帖子:132精华:9

lugl

主题: 43帖子:132精华:0

xujiwei263

主题: 16帖子:73精华:0

books咦

主题: 11帖子:11精华:2

Juggernaut

主题: 9帖子:95精华:0
快速回复 返回顶部 返回列表