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

描述

 

RA MCU 生态工作室致力于构建一个完善的 RA 生态,这将会是一个好玩有趣的开放社区,也提供各种RA相关问题的解决方法。

 

1.概述      
 

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

2.创建工程      

 

    STEP1:RASC创建工程

mcu

 

    STEP2:选芯片型号跟IDE类型

mcu

 

    STEP3:选择芯片型号

mcu

 

    STEP4:选择无操作系统

mcu

 

   STEP5:勾选确认工程

mcu

 

   STEP6:配置串口号跟IO口

mcu

 

   STEP7:添加串口驱动

mcu

 

  STEP8:配置串口

mcu

 

  STEP9:配置串口基本属性

mcu

 

  STEP10:生成工程

mcu


 

3.代码      

 

  STEP11:打开工程

mcu

 

    STEP12:添加代码

mcu

 

    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:添加头文件跟串口回调函数
 

mcu

 

    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 

#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

}


 

4.工程配置      
 

    STEP17:工程配置

mcu

 

    STEP18:弹出如下窗口

mcu


 

5.结果      

 

    STEP19:接上串口工具,打印如下

mcu


 

投稿:欣瑞利科技@Moxair

 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 相关推荐
  • mcu

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分