1.测试环境
硬件:nrf5340开发板
ncs版本:2.5.2
2.移植
2.1 CMakeLists.txt添加源文件
target_sources(app PRIVATE src/main.c src/bsp_led.c)
2.2 bsp_led.h
/**
* @file bsp_led.h
* @author wfagly
* @brief 基于安富莱电子(www.armfly.com)例子修改
* @version 1.0.1
* @date 2024-03-09
*
* @copyright Copyright (c) 2024
*
*/
#ifndef __BSP_LED_H
#define __BSP_LED_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <zephyr/kernel.h>
#define BSP_LED_OFF 0U
#define BSP_LED_ON 1U
/**
* @brief open led func.
*
* @param _no led number
*/
void bsp_LedOn(uint8_t _no);
/**
* @brief close led func.
*
* @param _no led number
*/
void bsp_LedOff(uint8_t _no);
/**
* @brief
*
* @param _no led number
* @return int 0-fail
*/
int bsp_LedToggle(uint8_t _no);
/**
* @brief Obtain LED status.
*
* @param _no led number
* @return uint8_t 1-on, 0-off
*/
uint8_t bsp_IsLedOn(uint8_t _no);
/**
* @brief led init func. default led off.
*
* @return int 0-error, 1-success
*/
int bsp_InitLed(void);
#ifdef __cplusplus
}
#endif
#endif //__BSP_LED_H
2.3 bsp_led.c
/**
* @file bsp_led.c
* @author wfagly
* @brief
* @version 1.0.1
* @date 2024-03-09
*
* @copyright Copyright (c) 2024
*
*/
#include "bsp_led.h"
#include <zephyr/drivers/gpio.h>
#define LED_LIST_COUT 4
/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)
#define LED1_NODE DT_ALIAS(led1)
#define LED2_NODE DT_ALIAS(led2)
#define LED3_NODE DT_ALIAS(led3)
/*
* A build error on this line means your board is unsupported.
* See the sample documentation for information on how to fix this.
*/
static const struct gpio_dt_spec led[LED_LIST_COUT] =
{
GPIO_DT_SPEC_GET(LED0_NODE, gpios),
GPIO_DT_SPEC_GET(LED1_NODE, gpios),
GPIO_DT_SPEC_GET(LED2_NODE, gpios),
GPIO_DT_SPEC_GET(LED3_NODE, gpios),
};
void bsp_LedOn(uint8_t _no)
{
gpio_pin_set_dt(&led[_no], BSP_LED_ON);
}
void bsp_LedOff(uint8_t _no)
{
gpio_pin_set_dt(&led[_no], BSP_LED_OFF);
}
int bsp_LedToggle(uint8_t _no)
{
int ret = gpio_pin_toggle_dt(&led[_no]);
if (ret < 0)
{
return 0;
}
}
uint8_t bsp_IsLedOn(uint8_t _no)
{
return (uint8_t)gpio_pin_get_dt(&led[_no]);
}
int bsp_InitLed(void)
{
int ret;
for (uint8_t i = 0; i < LED_LIST_COUT; i++)
{
if (!gpio_is_ready_dt(&led[i]))
{
return 0;
}
ret = gpio_pin_configure_dt(&led[i], GPIO_OUTPUT_ACTIVE);
if (ret < 0)
{
return 0;
}
gpio_pin_set_dt(&led[i], BSP_LED_OFF);
}
return 1;
}