本帖最后由 eew_TKwwQ7 于 2023-10-22 22:41 编辑
对于一个开发板来说,编程的第一件事情是控I/O的输出,即点亮一个LED,这个开发板有两个用户编程控制的LED,故就在此检测上点亮LED
一、新建LED配置文件:放置LED配置驱动程序
在做驱动底层需要硬件引脚图:见相关资料:https://bbs.eeworld.com.cn/elecplay/content/6cf59c5f
开发板中LED1、LED2分别对应P207、P206高电平有效,即逻辑为输出“1”LED亮
1)放置新建库文件夹
2)新建led驱动文件在scr文件夹中
3)新建led驱动.h文件在led文件目录中
代码如下:
#ifndef __BSP_LED_H
#define __BSP_LED_H
#include "hal_data.h"
/* LED引脚置低电平 LED灯亮 参考野火[野火] 瑞萨 RA 系列 FSP 库开发实战指南*/
#define LED1_ON R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_07, BSP_IO_LEVEL_LOW)
#define LED2_ON R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_06, BSP_IO_LEVEL_LOW)
/* LED引脚置高电平 LED灯灭 */
#define LED1_OFF R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_07, BSP_IO_LEVEL_HIGH)
#define LED2_OFF R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_02_PIN_06, BSP_IO_LEVEL_HIGH)
/* 使用寄存器来实现 LED灯翻转 */
#define LED1_TOGGLE R_PORT4->PODR ^= 1<<(BSP_IO_PORT_02_PIN_07 & 0xFF)
#define LED2_TOGGLE R_PORT4->PODR ^= 1<<(BSP_IO_PORT_02_PIN_06 & 0xFF)
/* LED初始化函数 */
void LED_Init(void);
#endif
4)新建led驱动.c文件在led文件目录中
代码如下:
#include "bsp_led.h"
/* LED初始化函数 */
void LED_Init(void)
{
/* 初始化配置引脚(这里重复初始化了,可以注释掉) */
R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
}
5)I/O配置:在FSP进行配置
更新配置
二、控制调试LED亮灭
1)在hal_entry.c文件进行添加LED亮灭代码:头文件和程序
#include "hal_data.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
/* 用户头文件包含 */
#include "led/bsp_led.h"
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
LED_Init(); // LED 初始化
while(1)
{
LED1_ON; // LED1亮
LED2_ON; // LED2亮
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS); //延时1秒
LED1_OFF; // LED1灭
LED2_OFF; // LED2灭
R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS); //延时1秒
}
#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
三、配置时钟
四、编译与下载
五、观察LED亮灭效果
WeChat_20231022223947
参考文献:[野火] 瑞萨 RA 系列 FSP 库开发实战指南