123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(APP_GPIOTE)
- #include "app_gpiote.h"
- #include "nrf_bitmask.h"
- #define MODULE_INITIALIZED (mp_users != NULL)
- #if (GPIO_COUNT == 1)
- #define MAX_PIN_NUMBER 32
- #elif (GPIO_COUNT == 2)
- #define MAX_PIN_NUMBER (32 + P1_PIN_NUM)
- #else
- #error "Not supported."
- #endif
- typedef struct
- {
- uint32_t pins_mask[GPIO_COUNT];
- uint32_t pins_low_to_high_mask[GPIO_COUNT];
- uint32_t pins_high_to_low_mask[GPIO_COUNT];
- uint32_t sense_high_pins[GPIO_COUNT];
- app_gpiote_event_handler_t event_handler;
- bool enabled;
- } gpiote_user_t;
- STATIC_ASSERT(sizeof(gpiote_user_t) <= GPIOTE_USER_NODE_SIZE);
- STATIC_ASSERT(sizeof(gpiote_user_t) % 4 == 0);
- static uint8_t m_user_array_size;
- static uint8_t m_user_count;
- static gpiote_user_t * mp_users = NULL;
- static uint32_t m_pins[GPIO_COUNT];
- static uint32_t m_last_pins_state[GPIO_COUNT];
- void gpiote_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
- {
- int i;
- uint32_t pin_mask[GPIO_COUNT] = {0};
- uint32_t empty_pin_mask[GPIO_COUNT] = {0};
- nrf_bitmask_bit_set(pin, pin_mask);
- bool hitolo = nrf_bitmask_bit_is_set(pin, m_last_pins_state);
- nrf_gpio_ports_read(0, GPIO_COUNT, m_last_pins_state);
- for (i = 0; i < m_user_count; i++)
- {
- if (mp_users[i].enabled && nrf_bitmask_bit_is_set(pin, mp_users[i].pins_mask))
- {
- if (
- nrf_bitmask_bit_is_set(pin, mp_users[i].pins_high_to_low_mask)
- && hitolo)
- {
- mp_users[i].event_handler(empty_pin_mask,pin_mask);
- }
- else if (
- nrf_bitmask_bit_is_set(pin, mp_users[i].pins_low_to_high_mask)
- && !hitolo)
- {
- mp_users[i].event_handler(pin_mask,empty_pin_mask);
- }
- }
- }
- }
- uint32_t app_gpiote_init(uint8_t max_users, void * p_buffer)
- {
- uint32_t ret_code = NRF_SUCCESS;
- if (p_buffer == NULL)
- {
- return NRF_ERROR_INVALID_PARAM;
- }
-
- if (!is_word_aligned(p_buffer))
- {
- return NRF_ERROR_INVALID_PARAM;
- }
-
- mp_users = (gpiote_user_t *)p_buffer;
- m_user_array_size = max_users;
- m_user_count = 0;
- memset(m_pins,0, sizeof(m_pins));
- memset(mp_users, 0, m_user_array_size * sizeof(gpiote_user_t));
- if (nrf_drv_gpiote_is_init()==false)
- {
- ret_code = nrf_drv_gpiote_init();
- }
- return ret_code;
- }
- uint32_t app_gpiote_user_register(app_gpiote_user_id_t * p_user_id,
- uint32_t const * p_pins_low_to_high_mask,
- uint32_t const * p_pins_high_to_low_mask,
- app_gpiote_event_handler_t event_handler)
- {
- uint32_t user_pin_mask[GPIO_COUNT];
- ASSERT(event_handler != NULL);
-
- VERIFY_MODULE_INITIALIZED();
- if (m_user_count >= m_user_array_size)
- {
- return NRF_ERROR_NO_MEM;
- }
- nrf_bitmask_masks_or(p_pins_low_to_high_mask, p_pins_high_to_low_mask,
- user_pin_mask, sizeof(user_pin_mask));
-
- mp_users[m_user_count].enabled = false;
- mp_users[m_user_count].event_handler = event_handler;
- memcpy(mp_users[m_user_count].pins_mask,
- user_pin_mask,
- sizeof(mp_users[m_user_count].pins_mask));
- memcpy(mp_users[m_user_count].pins_low_to_high_mask,
- p_pins_low_to_high_mask,
- sizeof(mp_users[m_user_count].pins_low_to_high_mask));
- memcpy(mp_users[m_user_count].pins_high_to_low_mask,
- p_pins_high_to_low_mask,
- sizeof(mp_users[m_user_count].pins_high_to_low_mask));
- uint32_t i;
- const nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);
- uint32_t num_of_pins = MAX_PIN_NUMBER;
- for (i = 0; i < num_of_pins; i++)
- {
- if (nrf_bitmask_bit_is_set(i, user_pin_mask) &&
- !nrf_bitmask_bit_is_set(i, m_pins))
- {
- uint32_t ret_val = nrf_drv_gpiote_in_init(i, &config, gpiote_handler);
- VERIFY_SUCCESS(ret_val);
- nrf_bitmask_bit_set(i, m_pins);
- }
- }
-
- *p_user_id = m_user_count++;
- return NRF_SUCCESS;
- }
- uint32_t app_gpiote_user_register_ex(app_gpiote_user_id_t * p_user_id,
- app_gpiote_user_pin_config_t const * p_pins_config,
- size_t pin_count,
- app_gpiote_event_handler_t event_handler)
- {
- ASSERT(event_handler != NULL);
-
- VERIFY_MODULE_INITIALIZED();
- if (m_user_count >= m_user_array_size)
- {
- return NRF_ERROR_NO_MEM;
- }
-
- gpiote_user_t * p_user = &mp_users[m_user_count];
- p_user->enabled = false;
- memset(p_user, 0, sizeof(gpiote_user_t));
- mp_users[m_user_count].event_handler = event_handler;
- for (; pin_count != 0; --pin_count, ++p_pins_config)
- {
- nrfx_gpiote_pin_t pin = (nrfx_gpiote_pin_t)p_pins_config->pin_number;
- const nrf_drv_gpiote_in_config_t config = GPIOTE_RAW_CONFIG_IN_SENSE_TOGGLE(false);
- if (!nrf_bitmask_bit_is_set(pin, m_pins))
- {
- uint32_t ret_val = nrf_drv_gpiote_in_init(pin, &config, gpiote_handler);
- VERIFY_SUCCESS(ret_val);
- nrf_bitmask_bit_set(pin, m_pins);
- }
-
- if ((p_pins_config->sense == NRF_GPIOTE_POLARITY_LOTOHI) ||
- (p_pins_config->sense == NRF_GPIOTE_POLARITY_TOGGLE))
- {
- nrf_bitmask_bit_set(pin, p_user->pins_low_to_high_mask);
- }
- if ((p_pins_config->sense == NRF_GPIOTE_POLARITY_HITOLO) ||
- (p_pins_config->sense == NRF_GPIOTE_POLARITY_TOGGLE))
- {
- nrf_bitmask_bit_set(pin, p_user->pins_high_to_low_mask);
- }
-
- }
-
- nrf_bitmask_masks_or(
- p_user->pins_low_to_high_mask,
- p_user->pins_high_to_low_mask,
- p_user->pins_mask,
- sizeof(p_user->pins_mask));
-
- *p_user_id = m_user_count++;
- return NRF_SUCCESS;
- }
- __STATIC_INLINE uint32_t error_check(app_gpiote_user_id_t user_id)
- {
-
- VERIFY_MODULE_INITIALIZED();
- if (user_id >= m_user_count)
- {
- return NRF_ERROR_INVALID_PARAM;
- }
- return NRF_SUCCESS;
- }
- static void pin_event_enable(uint32_t pin, bool enable)
- {
- uint32_t i;
- bool enabled = false;
-
- for (i = 0; i < m_user_count; i++)
- {
- if (mp_users[i].enabled &&
- nrf_bitmask_bit_is_set(pin, mp_users[i].pins_mask))
- {
- enabled = true;
- break;
- }
- }
- if (!enabled)
- {
- if (enable)
- {
- nrf_gpio_ports_read(0, GPIO_COUNT, m_last_pins_state);
- nrf_drv_gpiote_in_event_enable(pin, true);
- }
- else
- {
- nrf_drv_gpiote_in_event_disable(pin);
- }
- }
- }
- static uint32_t user_enable(app_gpiote_user_id_t user_id, bool enable)
- {
- uint32_t ret_code = error_check(user_id);
- if (ret_code == NRF_SUCCESS)
- {
- uint32_t i;
- for (i = 0; i < MAX_PIN_NUMBER; i++)
- {
- if (nrf_bitmask_bit_is_set(i, mp_users[user_id].pins_mask))
- {
- pin_event_enable(i, enable);
- }
- }
- }
- return ret_code;
- }
- uint32_t app_gpiote_user_enable(app_gpiote_user_id_t user_id)
- {
- uint32_t ret_code = NRF_SUCCESS;
- if (mp_users[user_id].enabled == false)
- {
- ret_code = user_enable(user_id, true);
- VERIFY_SUCCESS(ret_code);
- mp_users[user_id].enabled = true;
- return ret_code;
- }
- else
- {
- return ret_code;
- }
- }
- uint32_t app_gpiote_user_disable(app_gpiote_user_id_t user_id)
- {
- uint32_t ret_code = NRF_SUCCESS;
- if (mp_users[user_id].enabled)
- {
- mp_users[user_id].enabled = false;
- ret_code = user_enable(user_id, false);
- }
- return ret_code;
- }
- uint32_t app_gpiote_pins_state_get(app_gpiote_user_id_t user_id, uint32_t * p_pins)
- {
- gpiote_user_t * p_user;
- uint32_t ret_code = error_check(user_id);
- if (ret_code == NRF_SUCCESS)
- {
- p_user = &mp_users[user_id];
- nrf_gpio_ports_read(0, GPIO_COUNT, p_pins);
- nrf_bitmask_masks_and(p_pins, p_user->pins_mask, p_pins, sizeof(p_user->pins_mask));
- }
- return ret_code;
- }
- #endif
|