GPIOTE :
功能:利用任务(task)和事件(event)对GPIO口进行访问。 可以通过CONFIG[n] registers 对task和event进行配置。 输出Task 可以用来执行以下对管脚的写操作: Set 置1 Clear 清0 Toggle 翻转
利用输入引脚的检测信号(DETECT signal)可以产生以下event: Rising edge 上升沿 Falling edge 下降沿 Any change 任何改变 /*
nrf_gpiote_task_config(uint32_t channel_number,
uint32_t pin_number,
nrf_gpiote_polarity_t polarity,
nrf_gpiote_outinit_t initial_value) 说明:
GPIOTE有4个通道Channel channel_number [0: 3]
31个IO pin可作为通道pin_number[0:30] 即每个IO引脚都有GPIOTE 通道方式:
NRF_GPIOTE_POLARITY_LOTOHI = GPIOTE_CONFIG_POLARITY_LoToHi, ///< Low to high NRF_GPIOTE_POLARITY_HITOLO = GPIOTE_CONFIG_POLARITY_HiToLo, ///< High to low NRF_GPIOTE_POLARITY_TOGGLE = GPIOTE_CONFIG_POLARITY_Toggle///< Toggle } nrf_gpiote_polarity_t;
通道配置后的初始值:
NRF_GPIOTE_INITIAL_VALUE_LOW = GPIOTE_CONFIG_OUTINIT_Low, ///< Low to high NRF_GPIOTE_INITIAL_VALUE_HIGH = GPIOTE_CONFIG_OUTINIT_High ///< High to low
PIN:
? ? ? ? ?
OUT[n] tasks and the IN[n] events输入事件导致输出任务 The tasks can be used for writing to individual pins,
theevents can be generated from changes occurring at the inputs of individual pins.
Every pair of OUT[n] tasks and IN[n]events has one CONFIG[n] register associated with it. When an OUT[n] task or an IN[n] event has been configured to operate on a pin, the pin can only be writtenfrom the GPIOTE module. Attempting to write a pin as a normal GPIO pin will have no effect.当IO配置为GPIOTE后,GPIO操作将无效,直到GPIOTE释放此脚
PORT event
示例分析(): typedef enum {
NRF_GPIOTE_POLARITY_LOTOHI = GPIOTE_CONFIG_POLARITY_LoToHi, ///< Low to high NRF_GPIOTE_POLARITY_HITOLO = GPIOTE_CONFIG_POLARITY_HiToLo, ///< High to low NRF_GPIOTE_POLARITY_TOGGLE = GPIOTE_CONFIG_POLARITY_Toggle ///< Toggle } nrf_gpiote_polarity_t;
typedef enum {
NRF_GPIOTE_INITIAL_VALUE_LOW = GPIOTE_CONFIG_OUTINIT_Low, ///< Low to high NRF_GPIOTE_INITIAL_VALUE_HIGH = GPIOTE_CONFIG_OUTINIT_High ///< High to low } nrf_gpiote_outinit_t;
? nrf_gpiote_task_config(0, PWM_OUTPUT_PIN_NUMBER,
NRF_GPIOTE_POLARITY_LOTOHI,NRF_GPIOTE_INITIAL_VALUE_HIGH);
(0、LED_1、GPIOTE_CONFIG_POLARITY_LoToHi、1):GPIOTE_CONFIG_POLARITY_LoToHi为0 {
/* Check if the output desired is high or low */
if (initial_value == NRF_GPIOTE_INITIAL_VALUE_LOW) {
/* Workaround for the OUTINIT PAN. When nrf_gpiote_task_config() is called a glitch happens
on the GPIO if the GPIO in question is already assigned to GPIOTE and the pin is in the correct state in GPIOTE but not in the OUT register. */
NRF_GPIO->OUTCLR = (1 << pin_number);
/* Configure channel to Pin31, not connected to the pin, and configure as a tasks that will set
it to proper level */
NRF_GPIOTE->CONFIG[channel_number] =
(GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) | (31UL<< GPIOTE_CONFIG_PSEL_Pos) |
(GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos); }
else {
NRF_GPIO->OUTSET = (1 << pin_number);
NRF_GPIOTE->CONFIG[channel_number] =
(GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) | (31UL << GPIOTE_CONFIG_PSEL_Pos) | GPIOTE_CONFIG_POLARITY_LoToHi << GPIOTE_CONFIG_POLARITY_Pos); }
/* Three NOPs are required to make sure configuration is written before setting tasks or getting events */ __NOP(); __NOP(); __NOP();
/* Launch the task to take the GPIOTE channel output to the desired level */ NRF_GPIOTE->TASKS_OUT[channel_number] = 1;
/* Finally configure the channel as the caller expects. If OUTINIT works, the channel is configured properly. If it does not, the channel output inheritance sets the proper level. */
NRF_GPIOTE->CONFIG[channel_number] =
(GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos) | ((uint32_t)pin_number << GPIOTE_CONFIG_PSEL_Pos) | ((uint32_t)polarity << GPIOTE_CONFIG_POLARITY_Pos) | ((uint32_t)initial_value << GPIOTE_CONFIG_OUTINIT_Pos);
/* Three NOPs are required to make sure configuration is written before setting tasks or getting events */ __NOP(); __NOP(); __NOP(); }
初始化:
static void gpiote_init(void) {
NRF_GPIO->OUT = 0x00000000UL; NRF_GPIO->DIRSET = 0x0000FF00UL; NRF_GPIO->DIRCLR = 0x000000FFUL;
nrf_gpio_cfg_input(BUTTON_0, BUTTON_PULL); nrf_gpio_cfg_input(BUTTON_1, BUTTON_PULL); nrf_gpio_cfg_output(PWM_OUTPUT_PIN_NUMBER);
nrf_gpiote_task_config(0, PWM_OUTPUT_PIN_NUMBER, NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_HIGH); }
应用:
if (nrf_gpio_pin_read(BUTTON_1) == 0)
{ nrf_gpiote_task_config(0, PWM_OUTPUT_PIN_NUMBER,
NRF_GPIOTE_POLARITY_LOTOHI,NRF_GPIOTE_INITIAL_VALUE_HIGH); //pwm_set(224UL); //224 }
else if (nrf_gpio_pin_read(BUTTON_0) == 0)
{ nrf_gpiote_task_config(0, PWM_OUTPUT_PIN_NUMBER,
NRF_GPIOTE_POLARITY_TOGGLE, NRF_GPIOTE_INITIAL_VALUE_LOW); pwm_set(10); //32 } else
{ nrf_gpiote_task_config(0,PWM_OUTPUT_PIN_NUMBER, NRF_GPIOTE_POLARITY_HITOLO,NRF_GPIOTE_INITIAL_VALUE_LOW); // pwm_set(1UL); //1 }

