Analysis of GPIOTE developed by nRF52832

1. GPIOTE principle

1. Concept
1) GPIO of nRF52832 can only be used as general input and output enable. If it cannot be interrupted when it is used as an input, this effect needs to be achieved through GPIOTE;
2) GPIOTE (GPIO tasks and events), yes Introduce tasks and events on the basis of GPIO;
3) Operate IO through tasks and events, tasks are for output, it can make IO output different actions, and events are for input, IO state changes will set the event register, Thereby generating an event interrupt.

2. Principle
1) GPIOTE of nRF52832 has 8 channels in total, each channel can be assigned to a pin, and the assigned pin can be configured as task mode or event mode;
2) Each channel of GPIOTE has SET, CLR and OUT Task, in which SET task makes the pin output high level, CLR task makes the pin output low level, OUT task can perform set, clear and toggle operations on the pin through configuration;
3) GPIOTE events for each channel Can be configured into three input states: rising edge, falling edge and any level transition;
4) tasks and events are configured through the CONFIG [n] (n = 0 ~ 7) register, each CONFIG [n] register can be configured with one Corresponding number OUT [n] (n = 0 ~ 7) task register and IN [n] event register;
5) When a pin is configured through the CONFIG register is controlled by the SET, CLR and OUT task register or IN event register, the The pin can only be written by the GPIOTE module, normal GPIO write is invalid;
6) When conflicting tasks are triggered at the same time in the same GPIOTE channel, the execution level of these tasks from high to low is: OUT-> the CLR -> the SET;
. 7) GPIOTE addition to eight channels, further comprising a thing PORT , PORT events are events generated by a plurality of pins GPIO DETECT signal.

2. GPIOTE register (n = 0 ~ 7)

1.
OUT task register OUT [n]: write to the pin specified by CONFIG [n] .PSEL, the pin action is determined by the configuration in CONFIG [n] .POLARITY

2. TASKS_SET task register
TASKS_SET [n]: write to the pin specified by CONFIG [n] .PSEL, the pin action is to output high level

3. TASKS_CLR task register
TASKS_CLR [n]: write to the pin specified by CONFIG [n] .PSEL, the pin action is output low

4. EVENT_IN event register
EVENT_IN [n]: CONFIG [n]. Event generated by the pin specified by PSEL

5. General register
1) INTENSET: enable interrupt
2) INTENCLR: disable interrupt
3) CONFIG [n]: configuration of OUT [n], SET [n], CLR [n] and IN [n]

Three, programming analysis

SDK version: nRF5_SDK_15.2.0_9412b96

1. GPIOTE output process
1) Initialize GPIOTE module
2) Initialize GPIOTE output pin
3) Whether to enable task mode

2. GPIOTE input process
1) Initialize GPIOTE module
2) Configure the pin as GPIOTE input
3) Enable event mode

3. Related library functions
// header file: nrf_drv_gpiote.h
. 1) ret_code_t nrf_drv_gpiote_init (void)
Function: Initialization Module GPIOTE
2) ret_code_t nrf_drv_gpiote_out_init (nrf_drv_gpiote_pin_t pin,
nrf_drv_gpiote_out_config_t const * p_config) Function: Initialization GPIOTE output pin, an output pin In the initial setting, the action of the pin (low level to high level, high level to low level or toggle state) and initialization state (high level or low level)
3) void nrf_drv_gpiote_out_uninit (nrf_drv_gpiote_pin_t pin)
Function: release GPIOTE output pin
4) void nrfx_gpiote_out_task_enable (nrfx_drv_gpiote_pin_t pin)
function: enable GPIOTE output pin task mode
5) void nrfx_gpiote_out_task_disable (nrfx_drv_gpiote_pin_t pin)
pin
drive task output mode: disable GPIOTE output pin function Foot function
1> nrf_drv_gpiote_set_task_trigger (nrf_drv_gpiote_pin_t pin)
function: receive the trigger GPIOTE SET task, after triggering, the corresponding pin outputs high level
2> nrf_drv_gpiote_clr_task_trigger (nrf_drv_gpiote_pin_t pin)
function: GPIOTE CLR task is received after triggering, the corresponding pin outputs low level after triggering
3> nrf_drv_gpiote_out_task_trigger (nrf_drv_gpiote_pin_t pin)
OUT pin : after triggering the task, the GP pin is triggered after receiving the task Output state flip

7) ret_code_t nrf_drv_gpiote_in_init (nrf_drv_gpiote_pin_t pin,
nrf_drv_gpiote_in_config_t const * p_config, nrf_drv_gpiote_evt_handler_t evt_handler) Function: Initialization GPIOTE input pin,
. 8) void nrfx_gpiote_in_event_enable (nrfx_drv_gpiote_pin_t PIN, the INT_ENABLE BOOL)
Function: Enable input pin GPIOTE

4. Code writing
1) GPIOTE output process example

#include "boards.h"
#include "nrf_delay.h"
#include "nrf_drv_gpiote.h"

int main(void)
{
	ret_code_t err_code;
	//初始化GPIOTE模块
	err_code = nrf_drv_gpiote_init();
	APP_ERROR_CHECK(err_code);
	//定义GPIOTE输出初始化结构体并赋值
	nrf_drv_gpiote_out_config_t config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(true);
	//初始化GPIOTE输出引脚
	err_code = nrf_drv_gpiote_out_init(LED_1,&config);
	APP_ERROR_CHECK(err_code);
	//使能任务模式
	nrf_drv_gpiote_out_task_enable(LED_1);
	
	while(true)
	{	//触发任务
		nrf_drv_gpiote_out_task_trigger(LED_1);
		nrf_delay_ms(150);
	}
	return 0;
}

2) GPIOTE input process example

#include "boards.h"
#include "nrf_drv_gpiote.h"

//事件回调函数
void in_pin_handler(nrf_drv_gpiote_pin_t pin,nrf_gpiote_polarity_t action)
{
	if (pin == BUTTON_1)
		nrf_gpio_pin_toggle(LED_1);
	
	if (action == NRF_GPIOTE_POLARITY_HITOLO)
		nrf_gpio_pin_toggle(LED_2);
	else if (action == NRF_GPIOTE_POLARITY_LOTOHI)
		nrf_gpio_pin_toggle(LED_3);
	else if (action == NRF_GPIOTE_POLARITY_TOGGLE)
		nrf_gpio_pin_toggle(LED_4);
}

int main(void)
{
	ret_code_t err_code;
	//初始化开发板上的LED灯
	bsp_board_init(BSP_INIT_LEDS);
	//初始化GPIOTE模块
	err_code = nrf_drv_gpiote_init();
	APP_ERROR_CHECK(err_code);
	//配置下降沿产生事件
	//nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
	//配置上升沿产生事件,false表示
	nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(false);//true表示高精度,false表示低精度
	//配置任意电平变化产生事件
	//nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
	//配置引脚为内部上拉
	config.pull = NRF_GPIO_PIN_PULLUP;
	//配置引脚为GPIOTE输入
	err_code = nrf_drv_gpiote_in_init(BUTTON_1,&config,in_pin_handler);
	APP_ERROR_CHECK(err_code);
	//使能事件模式
	nrf_drv_gpiote_in_event_enable(BUTTON_1,true);
	while(true)
	{
	
	}
	return 0;
}

 

Published 81 original articles · 21 praises · 30,000+ views

Guess you like

Origin blog.csdn.net/qq_33575901/article/details/90041554