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

Adding a timer to the ble_app_uart example which triggers ble_nus_data_send()

Hello,

I have two boards. A nRF52840DK as central and a BMD-300-EVAL (contains a nRF52832 chip) as peripheral. I flashed ble_app_uart_c on the central and ble_app_uart on the peripheral. The examples works fine. I tried to edit the ble_app_uart example on the peripheral. My goal is to have a timer on the peripheral, which calls the ble_nus_data_send() function with 200 bytes of data every 40ms. 

This is the code I added to the ble_app_uart example:

static uint32_t counter = 0;
static uint8_t array[200];
static bool waited_first_time = false;

void my_timer_init (void)
{
    NRF_TIMER4->MODE = TIMER_MODE_MODE_Timer;
    NRF_TIMER4->BITMODE =  TIMER_BITMODE_BITMODE_16Bit;
    NRF_TIMER4->PRESCALER = 4;
    NRF_TIMER4->CC[0] = 1000;
    NRF_TIMER4->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;
    NRF_TIMER4->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos;
    NVIC_EnableIRQ(TIMER4_IRQn);
    NRF_TIMER4->TASKS_START = 1;
}

void TIMER4_IRQHandler(void)
{    
    ret_code_t err_code;
    uint16_t length_array = 200;

  if(NRF_TIMER4->EVENTS_COMPARE[0])
  {
      NRF_TIMER4->EVENTS_COMPARE[0] = 0;

      counter++;

      if (counter >= 2000) // Wait 2 Sec. before start sending
      {
          counter = 0;
          waited_first_time = true;
      }

      if (counter >= 40 && waited_first_time == true)
      {
          counter = 0;
          err_code = ble_nus_data_send(&m_nus, array, &length_array, m_conn_handle);
          if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))
          {
              APP_ERROR_CHECK(err_code);
          }
      }
  }
}

void init_test_array(void)
{
    uint16_t i;
    for (i = 0; i < 200; i++)
    {
        array[i] = 33;
    }
}

int main(void)
{
    //Added after advertising_start()
    init_test_array();
    my_timer_init();
}

Here is my problem: My program stops immediatly when it tries to exectute ble_nus_data_send(...). The next line doesent get executed after that: 

if ((err_code != NRF_ERROR_INVALID_STATE) && (err_code != NRF_ERROR_NOT_FOUND))

So it is not the APP_ERROR_CHECK which causes the error, it is the call of ble_nus_data_send(...) itself.

I set "DEBUG" in the preprocessor defines, but I dont get a proper error message. I only have the messages from the call stack:

 

So here are my questions:

1. What is the issue and how can I fix this issue?

2. Is there a better way to implement this?

Thank you very much in advance.

Parents Reply Children
No Data
Related