: Because in most cases the status change of a GPIO triggers the application to perform some actions. In order to facilitate the NRF51 official to encapsulate the process into Gpiote, full name: The GPIO Tasks and Events (gpiote).
From Gpio level changes to the process of generating interrupt events
1. Gpiote Overview
NRF51 has 32 gpio above, because in most cases the state of the GPIO triggers the application to perform some actions. In order to facilitate the NRF51 official to encapsulate the process into Gpiote, full name: The GPIO Tasks and Events (gpiote). Gpiote provides access to Gpio pin functions using tasks and events. In addition, with this lib you can have multiple modules sharing the same gpiote interrupt, when the Gpiote interrupt, where the fault handler will determine the PIN, and notify the corresponding module of the event.
Figure: Users are being notified of a PIN transition event (user represents each module)
The users of the Gpiote must be configured with the appropriate PIN, unless used for sense purposes (the PIN should be configured to gpio_pin_cnf_sense_disabled for that purpose)
The module needs to specify the level shift at which the PIN will generate interrupt events (level transformation type-from low to high, from high to low, both)
Note
Although the app uses scheduler, the Gptote event handle is directly from the gptote interrupt handle.
2. Initialize the Gpiote module
The API for this module cannot be used before initialization, and it is recommended that app_gpiote_init be used instead of app_gpiote_initit for initialization, since the former is responsible for keeping the required memory for each user (max_users indicates the maximum number of users required. That is, how many modules will register and use the Gpiote module)
1 // Macro to initialize Gpiote module and reserving necessary memory for each of the user. 2 app_gpiote_init (max_users);
Note
Initialize only once!!!
3, Gpiote Register
Each user must register himself to be notified when the Gpio state changes. At the time of registration, user must provide a callback handler to inform a conversion event and the way in which the pin is interested. The 32-bit bitmask is used to represent the 32GPIO pin. User can register an event of interest as a pin from low to high or from high to low.
Figure: GPIO Pin representation using 32-bit bitmask
When the registration succeeds, user is assigned a user id,user need to write down the ID to provide a unique recognition for subsequent requests to the Gpiote module. This unique recognition is an output parameter of the function p_user_id, as follows:
1 //Gpiote User identifier for the example module.2 Staticapp_gpiote_user_id_t m_example_user_id;//Gpiote event handler.3 Static voidExample_gpiote_event_handler (uint32_t event_pins_low_to_high, uint32_t event_pins_high_to_low);4 .5 .6 .7uint32_t Low_to_high_bitmask =0x0000000f;//Bitmask to is notified of transition from low to high for GPIO 0-38uint32_t High_to_low_bitmask =0x0000000e;//Bitmask to is notified of transition from high to low for GPIO 0-29uint32_t retval;retval = App_gpiote_user_register (&m_example_user_id,Ten Low_to_high_bitmask, One High_to_low_bitmask, A Example_gpiote_event_handler); - - if(RetVal! =nrf_success) { the //Failed to register with the user with GPIO module! - } - - . + . -.
PS: by default, Gpiote is lost after initialization. Therefore, there must be a user call app_gpiote_user_enable to enable Gpiote.
The following is a callback function for a user-registered PIN state Change event:
1 //Gpiote event handler.2 voidExample_gpiote_event_handler (uint32_t event_pins_low_to_high, uint32_t event_pins_high_to_low)3 { 4 . 5 . 6 . 7 if(Event_pins_low_to_high &0x00000001) 8 { 9 //GPIO pin 0 transitioned from low to high. Ten //Take necessary action. One } A if(Event_pins_high_to_low &0x00000004) - { - //The GPIO pin 2 transitioned from high to low. the //Take necessary action. - } - - . + . - . +}
4, Enable/disable Gpiote
A registered user can lose or enable the Gpiote module at any time. When the Gpiote is lost, the event will not be received, and after initialization the Gpiote defaults to be lost. The following example shows the deactivation or enable Gpiote module:
1 uint32_t retval;2 3 //Enable Notifications For example the user module which is already registered.4retval =app_gpiote_user_disable (m_example_user_id);5 if(RetVal! =nrf_success)6 { 7 //enabling notifications failed. Take corrective/needed action. 8 .9 .Ten } One A . - . - . the //Enable Notifications For example the user module which is already registered. -retval =app_gpiote_user_enable (m_example_user_id); - if(RetVal! =nrf_success) - { + //enabling notifications failed. Take corrective/needed action. - . + . A}
5. Reading Gpiote State
A registered user can obtain the current state of the Gpios by reading the status information.
1 uint32_t retval;2 uint32_t gpio_pin_state;3 4retval = App_gpiote_pins_state_get (m_example_user_id,&gpio_pin_state);5 if(RetVal! =nrf_success)6 { 7 //Failed to read the state information. Take corrective action.8}Else{9 . Ten . One if(Gpio_pins_state &0x00000006)//Checks if pin One and both are set A { - //Take necessary action - } the . - . -}
Historical related articles
[nRF51822] 3, the New Year is also a summary--graphical NRF51 SDK button handling library and FIFO library
[nRF51822] 4, schematic nRF51 SDK schedule handling library and timer library
@beautifulzzzz 2016-01-01 continue~
[nRF51822] 5, PA screen-detailed nRF51 SDK in the Gpiote (from the Gpio level change to the process of generating the interrupt event detailed)