I am trying to implement a simple matrix 4X4 keypad. I have an issue in enabling gpiote events. I disable gpiote events when there is a row event and i scan the keys by turining off/on columns and reading the row states. Then i re-enable them. However as soon as i enable there is a new event firing (probably because button is still physically closed) but i set the event lotohi so i'd still expect it to not fire..
Any suggestion is welcome thanks!
static const uint8_t column_to_pin_map[4] = { (uint8_t)(KBC_1_PIN), (uint8_t)(KBC_2_PIN), (uint8_t)(KBC_3_PIN), (uint8_t)(KBC_4_PIN), }; static const uint8_t row_to_pin_map[4] = { (uint8_t)(KBR_1_PIN), (uint8_t)(KBR_2_PIN), (uint8_t)(KBR_3_PIN), (uint8_t)(KBR_4_PIN), }; static void scan_keyboard_for(uint8_t row_pin){ uint8_t column = NULL; uint8_t row = NULL; disable_row_events(); for(uint8_t c = 0; c < 4;c++){ nrfx_gpiote_out_clear(column_to_pin_map[c]); nrf_delay_us(PROPAGATION_DELAY); if(!nrfx_gpiote_in_is_set(row_pin)){ column = 1 + c; break; } } if(KBR_1_PIN == row_pin) row = 1; if(KBR_2_PIN == row_pin) row = 2; if(KBR_3_PIN == row_pin) row = 3; if(KBR_4_PIN == row_pin) row = 4; NRF_LOG_INFO("COLUMN: %d ROW: %d", column, row); nrf_delay_us(PROPAGATION_DELAY); nrfx_gpiote_out_set(KBC_1_PIN); nrfx_gpiote_out_set(KBC_2_PIN); nrfx_gpiote_out_set(KBC_3_PIN); nrfx_gpiote_out_set(KBC_4_PIN); enable_row_events(); //return column; } static void gpiote_in_event_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { scan_keyboard_for(pin); } static void enable_row_events(){ nrfx_gpiote_in_event_enable( KBR_1_PIN, true ); nrfx_gpiote_in_event_enable( KBR_2_PIN, true ); nrfx_gpiote_in_event_enable( KBR_3_PIN, true ); nrfx_gpiote_in_event_enable( KBR_4_PIN, true ); } static void disable_row_events(){ nrfx_gpiote_in_event_disable( KBR_1_PIN ); nrfx_gpiote_in_event_disable( KBR_2_PIN ); nrfx_gpiote_in_event_disable( KBR_3_PIN ); nrfx_gpiote_in_event_disable( KBR_4_PIN ); } void init_keyboard(){ if(!nrfx_gpiote_is_init()) { APP_ERROR_CHECK(nrfx_gpiote_init()); } nrfx_gpiote_out_config_t config_out = NRFX_GPIOTE_CONFIG_OUT_SIMPLE(true); // true ==> init high nrfx_gpiote_in_config_t config_in = NRFX_GPIOTE_CONFIG_IN_SENSE_LOTOHI(false);//NRFX_GPIOTE_CONFIG_IN_SENSE_TOGGLE(false); // false ==> hi accu = false config_in.pull = NRF_GPIO_PIN_PULLDOWN; APP_ERROR_CHECK(nrfx_gpiote_out_init(KBC_1_PIN, &config_out )); APP_ERROR_CHECK(nrfx_gpiote_out_init(KBC_2_PIN, &config_out )); APP_ERROR_CHECK(nrfx_gpiote_out_init(KBC_3_PIN, &config_out )); APP_ERROR_CHECK(nrfx_gpiote_out_init(KBC_4_PIN, &config_out )); APP_ERROR_CHECK(nrfx_gpiote_in_init(KBR_1_PIN, &config_in, gpiote_in_event_handler)); APP_ERROR_CHECK(nrfx_gpiote_in_init(KBR_2_PIN, &config_in, gpiote_in_event_handler)); APP_ERROR_CHECK(nrfx_gpiote_in_init(KBR_3_PIN, &config_in, gpiote_in_event_handler)); APP_ERROR_CHECK(nrfx_gpiote_in_init(KBR_4_PIN, &config_in, gpiote_in_event_handler)); enable_row_events(); NRF_LOG_INFO("keyboard init done"); }