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

  • I have created examples for both GPIO/Timer and GPIOTE sense alternatives: gpio_char_example.zip, gpiote_sense_char_example.zip. Please have a look at these. They are both tested with PCA10028 (nRF51 DK) and PCA10040 (nRF52 DK) boards.

Reply Children
No Data
Related