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

Correct way to use timers

Hi,

I have 4 sensors on my custom board and I'm planning to use BLE. I want to read data from all 4 sensors every 200ms. I'm planning to use app_timer for that and read the data in a callback function or something like that. Is this the way to go?

Also, some of my sensors require a delay for analog to digital conversion and things like that. Those delays are in terms of miliseconds. Do I use nrf_delay_ms() for that or do I use something else? I remember reading somewhere that that kind of delay can mess up softdevice functionality and that it should be used for testing only. Additionally, I'm using retargetted printf for simple UART communication and that seems to mess up the nrf_delay_ms somehow. For example this code does not invert the leds:

    bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS);
    if ((err = uart_init()) != NRF_SUCCESS)
        bsp_board_leds_on();
    printf("### Application started ###\r\n");
    if ((err = twi_init()) != NRF_SUCCESS)
        bsp_board_leds_on();
    //printf("TWI Initialized\r\n");

    while(1)
    {
        for (int i = 0; i < LEDS_NUMBER; i++)
        {
            bsp_board_led_invert(i);
            nrf_delay_ms(500);
        }
    }

but this code does invert the leds (notice that first printf is commented)

    bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS);
    if ((err = uart_init()) != NRF_SUCCESS)
        bsp_board_leds_on();
    //printf("### Application started ###\r\n");
    if ((err = twi_init()) != NRF_SUCCESS)
        bsp_board_leds_on();
    //printf("TWI Initialized\r\n");

    while(1)
    {
        for (int i = 0; i < LEDS_NUMBER; i++)
        {
            bsp_board_led_invert(i);
            nrf_delay_ms(500);
        }
    }

and that's really strange to me. Anyone has any idea why that is?

Thank you for your time,

squeeky

Parents
  • I have 4 sensors on my custom board and I'm planning to use BLE. I want to read data from all 4 sensors every 200ms. I'm planning to use app_timer for that and read the data in a callback function or something like that. Is this the way to go?

    That'll work. 

    Also, some of my sensors require a delay for analog to digital conversion and things like that. Those delays are in terms of miliseconds. Do I use nrf_delay_ms() for that or do I use something else? I remember reading somewhere that that kind of delay can mess up softdevice functionality and that it should be used for testing only.

     Any code that runs at a higher priority than the SoftDevice will block it, though you have to explicitly write your code that way to do it. nrf_delay_xx are completely safe at priorities 6-7 (default interrupt prio in the SDK). 

    Additionally, I'm using retargetted printf for simple UART communication and that seems to mess up the nrf_delay_ms somehow. For example this code does not invert the leds:

     Are you sure that you actually hit your while loop at all? 
    Are you able to print the statement? 

Reply
  • I have 4 sensors on my custom board and I'm planning to use BLE. I want to read data from all 4 sensors every 200ms. I'm planning to use app_timer for that and read the data in a callback function or something like that. Is this the way to go?

    That'll work. 

    Also, some of my sensors require a delay for analog to digital conversion and things like that. Those delays are in terms of miliseconds. Do I use nrf_delay_ms() for that or do I use something else? I remember reading somewhere that that kind of delay can mess up softdevice functionality and that it should be used for testing only.

     Any code that runs at a higher priority than the SoftDevice will block it, though you have to explicitly write your code that way to do it. nrf_delay_xx are completely safe at priorities 6-7 (default interrupt prio in the SDK). 

    Additionally, I'm using retargetted printf for simple UART communication and that seems to mess up the nrf_delay_ms somehow. For example this code does not invert the leds:

     Are you sure that you actually hit your while loop at all? 
    Are you able to print the statement? 

Children
Related