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

eew_cT3H5d   2024-8-4 00:07 楼主

先看效果:RA8D1发送“HELLO eeworld”,同时若接收到串口调试软件发送指令进行亮灭LED灯

串口控制LED.gif 点击上图查看Gif动图

 

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

系统框图如下:

image.png  

 

使能串口uart3

image.png        

 

核对串口使能引脚

image.png  

 

配置时钟

image.png  

 

串口处理函数

image.png  

debug_uart3c

#include <debug_uart3.h>


void Debug_UART3_Init(void)
{
    fsp_err_t err = FSP_SUCCESS;

    err = R_SCI_B_UART_Open (&g_uart3_ctrl, &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->event)
    {
        case UART_EVENT_RX_CHAR:
        {
            switch (p_args->data)
                 {
                    case '1':
                        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_10_PIN_01, BSP_IO_LEVEL_LOW);;
                        break;
                    case '2':
                        R_IOPORT_PinWrite(&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;
    }
}

 

串口头文件

image.png  

debug_uart3.h

#ifndef _DEBUG_UART3_H
#define	_DEBUG_UART3_H
#include "hal_data.h"
#include "stdio.h"


void Debug_UART3_Init(void);







#endif

print函数构建

image.png  

printf_redirect.c

#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;
}

主程序hal_entry.c

image.png  hal_entry.c

#include "hal_data.h"

#include "debug_uart3.h"

fsp_err_t err;

void R_BSP_WarmStart(bsp_warm_start_event_t event);

//extern bsp_leds_t g_bsp_leds;

/*******************************************************************************************************************//**
 * @brief   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("HELL eeworld !\r\n");
        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.
 *
 * @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(&IOPORT_CFG_CTRL, &IOPORT_CFG_NAME);
    }
}

 

 

程序框架: image.png  

 

 

 

本帖最后由 eew_cT3H5d 于 2024-8-4 23:02 编辑

回复评论 (2)

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

点赞  2024-8-4 23:07

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

点赞  2024-10-20 20:23
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复