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

Why time delay is happening after loop ? Please help after many days fail to resolve it.

I have 2 message order by sequence  and between each of them have delay 2s. for Example 

How should it work

A1BBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDXX

Delay 2s

A2BBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDXX

How it work now

A1BBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDXX 

A2BBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDXX

Delay 2s

Why from A1 to A2 it skip the time delay ??My code  

void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    uint32_t       err_code;
   uint16_t length = (uint16_t)index;


    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') ||
                (data_array[index - 1] == '\r') ||
                (index >= m_ble_nus_max_data_len))
            {
                if (index > 1)
                {

                    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                    do
                    {
                     
                        err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                        if ((err_code != NRF_ERROR_INVALID_STATE) &&
                            (err_code != NRF_ERROR_RESOURCES) &&
                            (err_code != NRF_ERROR_NOT_FOUND))
                        {
                            APP_ERROR_CHECK(err_code);
                        }
                    } while (err_code == NRF_ERROR_RESOURCES);
      

                      }
                      
                      //null
              else{

                    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                     int i;

                    do
                    {
                     
                       char buf[42];
                        for (i = 0; i < 2; i++) {
                      snprintf(buf,42, "A%dBBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDXX", i);         
                      // puts string into buffer
                       printf("%s\n", buf); // outputs so you can see it

                         
                           uint16_t length2 = sizeof(buf);
      
                                   //dalam function ni problem APP_TIMER_MODE_REPEATED sebab dia akan reset send 3 kali
                          send_some_data_uart_init();


                           err_code = ble_nus_data_send(&m_nus,buf, &length2, m_conn_handle);
                          NRF_SUCCESS;
               
                                 if ((err_code != NRF_ERROR_INVALID_STATE) &&
                                                            (err_code != NRF_ERROR_RESOURCES) &&
                                                            (err_code != NRF_ERROR_NOT_FOUND))
                                                        {
                                                            APP_ERROR_CHECK(err_code);
                                                        }


                       }
                      
                      
                         
                      
                       
                    } while (err_code == NRF_ERROR_RESOURCES );



                             }

            
                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}

Parents
  • Maybe try to call printf from main() and not an interrupt context? Your code snippet doesn't contain any delay from what I can see.

    Kenneth

  •  send_some_data_uart_init(); this function is the function of the timer. The delay timer is work but why it skip the loop ?

    void send_some_data_uart_init(void)
    {
    
      ret_code_t err_code;
    
    
    //    app_timer_create(&send_some_uart_data,APP_TIMER_MODE_REPEATED, uart_event_handle);
       err_code= app_timer_start(send_some_uart_data, APP_TIMER_TICKS(5000), NULL);
         APP_ERROR_CHECK(err_code);
    }
    
    static void timers_init(void)
    {
       ret_code_t    err_code;
    
         err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    
         err_code=app_timer_create(&send_some_uart_data,APP_TIMER_MODE_REPEATED, uart_event_handle);
    
        APP_ERROR_CHECK(err_code);
    
    }
    
    

Reply
  •  send_some_data_uart_init(); this function is the function of the timer. The delay timer is work but why it skip the loop ?

    void send_some_data_uart_init(void)
    {
    
      ret_code_t err_code;
    
    
    //    app_timer_create(&send_some_uart_data,APP_TIMER_MODE_REPEATED, uart_event_handle);
       err_code= app_timer_start(send_some_uart_data, APP_TIMER_TICKS(5000), NULL);
         APP_ERROR_CHECK(err_code);
    }
    
    static void timers_init(void)
    {
       ret_code_t    err_code;
    
         err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    
         err_code=app_timer_create(&send_some_uart_data,APP_TIMER_MODE_REPEATED, uart_event_handle);
    
        APP_ERROR_CHECK(err_code);
    
    }
    
    

Children
No Data
Related