现象描述
基于light_switch例程,按照新的GPIO配置IO后不能生效
具体细节
例程中GPIO的分配为P0口的13、14、15、16,现需要改为
原例程中4个LED分别如下
#define LED_1 NRF_GPIO_PIN_MAP(0,13)
#define LED_2 NRF_GPIO_PIN_MAP(0,14)
#define LED_3 NRF_GPIO_PIN_MAP(0,2)
#define LED_4 NRF_GPIO_PIN_MAP(0,16)
现改为P1口的6、11、2、5口
#define LED_1 NRF_GPIO_PIN_MAP(1,11)
#define LED_2 NRF_GPIO_PIN_MAP(1,6)
#define LED_3 NRF_GPIO_PIN_MAP(1,2)
#define LED_4 NRF_GPIO_PIN_MAP(1,5)
改为后发现未生效,针对此现象进行了代码追寻
代码分析
GPIO驱动发现有问题,只能使用Port0不能使用port1,代码如下
NRF_GPIO->OUTCLR = (1 << pin);
#ifndef NRF_GPIO
#define NRF_GPIO NRF_P0
#endif
#ifndef NRF_GPIO_BASE
#define NRF_GPIO_BASE NRF_P0_BASE
#endif
改为根据pin脚号来自动区分Port0还是Port1,更新驱动后如下
nrf_gpio_pin_write(pin, value);
NRF_GPIO_Type * reg = nrf_gpio_pin_port_decode(&pin_number);
nrf_gpio_port_out_set(reg, 1UL << pin_number);
修改后仍不能控制,发现仍有一个问题,初始化也是有局限性的,只能是从小到大且为连续的pin脚,代码如下
for (uint32_t i = LED_START; i <= LED_STOP; ++i)
{
NRF_GPIO->PIN_CNF[i] = LED_PIN_CONFIG;
NRF_GPIO->OUTSET = 1UL << i;
}
修改通用初始化代码如下:
/* 管脚输出配置,管脚的删减同步更新管脚个数 */
static const uint8_t gpio_out_list[4] = { LED_1, LED_2, LED_3, LED_4 };
for (uint32_t i = 0; i < 4; i++)
{
nrf_gpio_cfg_output(gpio_out_list[i]);
}
结果
修改驱动后,可以自如控制GPIO