This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Read GPIO pin status through a characteristic.

Hi,

I am new to nrf and trying to make some new services and characteristics for my device. I followed the advertising, services and characteristics tutorial and was successfully able to make a new service and new characteristic.

But now I want to read the status of my D5 pin(P0.28 in nrf51822) using the same characteristics. I tried a lot of things but still not able to do so.

I am not getting what to change and where to change so that the particular GPIO is readable from the characteristic. Please help me. Thanks.

Parents
  • If you followed the characteristics tutorial to the end, you should have a characteristics reporting temperature gathered from the IC at a regular interval. If you want to change the application to report state of a pin on this charactersistics instead, this is the changes you need to make:

    • Define the pin: #define INPUT_PIN 28

    • Configure pin as input in main(): nrf_gpio_cfg_input(INPUT_PIN , NRF_GPIO_PIN_NOPULL);

    • Change timer_timeout_handler() function to read pin state and pass this to the characteristic update function:


    static void timer_timeout_handler(void * p_context)
    {
       int32_t pin_state = 0;
       pin_state = nrf_gpio_pin_read(INPUT_PIN);
       our_temperature_characteristic_update(&m_our_service, &pin_state);
       nrf_gpio_pins_toggle(BSP_LED_2);
    }
    

    The characteristics should now be updated with the pin state on any change.

    If you want the characteristics to be only 1 byte long, and also report the correct pin state on initialization, you also have to change the following:

    • In our_char_add(), change attr_char_value.max_len, attr_char_value.init_len, value and attr_char_value.p_value to:

    attr_char_value.max_len  = 1;
    attr_char_value.init_len = 1;
    uint8_t value            = nrf_gpio_pin_read(INPUT_PIN);
    attr_char_value.p_value  = &value;
    

    • In our_temperature_characteristic_update(), change len to 1.

    Best regards,

    Jørgen

  • You should also be able to do the update of the characteristics using GPIO sensing/GPIOTE. Comment out the content of the timer_timeot_handler() function, add the following GPIOTE handler function:

    static void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        int32_t temperature = 0;
        temperature = nrf_drv_gpiote_in_is_set(INPUT_PIN);
    	our_temperature_characteristic_update(&m_our_service, &temperature);
    }
    

    In main(), add the following:

    nrf_drv_gpiote_init();
    nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    nrf_drv_gpiote_in_init(INPUT_PIN , &config, gpiote_event_handler);
    nrf_drv_gpiote_in_event_enable(INPUT_PIN, true);
    
Reply
  • You should also be able to do the update of the characteristics using GPIO sensing/GPIOTE. Comment out the content of the timer_timeot_handler() function, add the following GPIOTE handler function:

    static void gpiote_event_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        int32_t temperature = 0;
        temperature = nrf_drv_gpiote_in_is_set(INPUT_PIN);
    	our_temperature_characteristic_update(&m_our_service, &temperature);
    }
    

    In main(), add the following:

    nrf_drv_gpiote_init();
    nrf_drv_gpiote_in_config_t config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    nrf_drv_gpiote_in_init(INPUT_PIN , &config, gpiote_event_handler);
    nrf_drv_gpiote_in_event_enable(INPUT_PIN, true);
    
Children
No Data
Related