eew_cT3H5d 发表于 2024-8-4 00:07

[原创] 【瑞萨RA8D1评测】第六篇:UART通讯控制LED亮灭

<div class='showpostmsg'> 本帖最后由 eew_cT3H5d 于 2024-8-4 23:02 编辑

<p>先看效果:RA8D1发送&ldquo;HELLO eeworld&rdquo;,同时若接收到串口调试软件发送指令进行亮灭LED灯</p>

<div style="text-align: left;"></div>

<p>&nbsp;</p>

<p>因JLINK自带烧写和调试串口,刚好链接开发板的uart3,所以需要使能开发板的uart3就可以直接通过JLINK的USB直接与电脑进行通讯</p>

<p>系统框图如下:</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>使能串口uart3</p>

<p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>

<p>&nbsp;</p>

<p>核对串口使能引脚</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>配置时钟</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>串口处理函数</p>

<p> &nbsp;</p>

<p>debug_uart3c</p>

<pre>
<code>#include &lt;debug_uart3.h&gt;


void Debug_UART3_Init(void)
{
    fsp_err_t err = FSP_SUCCESS;

    err = R_SCI_B_UART_Open (&amp;g_uart3_ctrl, &amp;g_uart3_cfg);
    assert(FSP_SUCCESS == err);
}

uint8_t uart_send_complete_flag = 0;

void debug_uart3_callback (uart_callback_args_t * p_args)
{
    switch (p_args-&gt;event)
    {
      case UART_EVENT_RX_CHAR:
      {
            switch (p_args-&gt;data)
               {
                  case '1':
                        R_IOPORT_PinWrite(&amp;g_ioport_ctrl, BSP_IO_PORT_10_PIN_01, BSP_IO_LEVEL_LOW);;
                        break;
                  case '2':
                        R_IOPORT_PinWrite(&amp;g_ioport_ctrl, BSP_IO_PORT_10_PIN_01, BSP_IO_LEVEL_HIGH);
                        break;
                  default:
                        break;
                  }
            break;
      }
      case UART_EVENT_TX_COMPLETE:
      {
            uart_send_complete_flag = 1;
            break;
      }

      default:
            break;
    }
}
</code></pre>

<p>&nbsp;</p>

<p>串口头文件</p>

<p> &nbsp;</p>

<p>debug_uart3.h</p>

<pre>
<code>#ifndef _DEBUG_UART3_H
#define        _DEBUG_UART3_H
#include "hal_data.h"
#include "stdio.h"


void Debug_UART3_Init(void);







#endif
</code></pre>

<p>print函数构建</p>

<p> &nbsp;</p>

<p>printf_redirect.c</p>

<pre>
<code>#include "hal_data.h"
#include "stdio.h"
#include &lt;sys/stat.h&gt;
#include &lt;errno.h&gt;
#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(&amp;g_uart3_ctrl, &amp;g_uart3_cfg);
    }

    if (FSP_SUCCESS == err)
    {
          err = R_SCI_B_UART_Write(&amp;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-&gt;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;
}
</code></pre>

<p>主程序hal_entry.c</p>

<p> &nbsp;hal_entry.c</p>

<pre>
<code>#include &quot;hal_data.h&quot;

#include &quot;debug_uart3.h&quot;

fsp_err_t err;

void R_BSP_WarmStart(bsp_warm_start_event_t event);

//extern bsp_leds_t g_bsp_leds;

/*******************************************************************************************************************//**
* <a href="home.php?mod=space&amp;uid=159083" target="_blank">@brief </a>Blinky example application
*
* Blinks all leds at a rate of 1 second using the software delay function provided by the BSP.
*
**********************************************************************************************************************/
void hal_entry (void)
{

    Debug_UART3_Init();
    while (1)
    {
      printf(&quot;HELL eeworld !\r\n&quot;);
      R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS); //延时1秒
    }
}

/*******************************************************************************************************************//**
* 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.
*
* @paramevent    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-&gt;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(&amp;IOPORT_CFG_CTRL, &amp;IOPORT_CFG_NAME);
    }
}
</code></pre>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>程序框架: &nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

eew_cT3H5d 发表于 2024-8-4 23:07

<p>参考来源:https://m.eeworld.com.cn/bbs/forum.php?mod=viewthread&tid=1289361</p>

通途科技 发表于 2024-10-20 20:23

页: [1]
查看完整版本: [原创] 【瑞萨RA8D1评测】第六篇:UART通讯控制LED亮灭