This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2540 OSAL Timer is not accurate

Other Parts Discussed in Thread: CC2540

Hi TI experts,

I did use timer to periodically launch a notification but I found out that the timer is not accurate thought, I set to 250ms and sometimes the interval was over 300ms, sometimes it was less than 200ms.

my code:

// How often (in ms) to read the velocomputer

#define VELOCOMPUTER_READ_PERIOD      250

 

// Setup timer for accelerometer task

      osal_start_timerEx( velocomputerapp_TaskID, VELOCOMPUTER_READ_EVT, VELOCOMPUTER_READ_PERIOD );

    } else {

      // Stop the acceleromter

      osal_stop_timerEx( velocomputerapp_TaskID, VELOCOMPUTER_READ_EVT);

 

// Restart timer

        if (VELOCOMPUTER_READ_PERIOD)

          osal_start_timerEx( velocomputerapp_TaskID, VELOCOMPUTER_READ_EVT, VELOCOMPUTER_READ_PERIOD );

 

// Read velocomputer data

        veloComputerRead(); <<<<<<<< notification will be issued inside here

this is what i got from BTool,

[1] : <Tx> - 03:36:00.516

-Type : 0x01 (Command)

-Opcode : 0xFD12 (ATT_WriteReq)

-Data Length : 0x07 byte(s)

ConnHandle : 0x0000

Signature : 0x00 (No)

Command : 0x00 (No)

AttrHandle : 0x0034

AttrValue : 01

Dump(Tx):

01 12 FD 07 00 00 00 00 34 00 01

 

-----------------------------------------------------------------------------------------------

[2] : <Rx> - 03:36:00.579

-Type : 0x04 (Event)

-EventCode : 0xFF (HCI_LE_ExtEvent)

-Data Length : 0x06 bytes(s)

Event : 0x067F (GAP_HCI_ExtentionCommandStatus)

Status : 0x00 (Success)

OpCode : 0xFD12 (ATT_WriteReq)

DataLength : 0x00

Dump(Rx):

04 FF 06 7F 06 00 12 FD 00

 

-----------------------------------------------------------------------------------------------

[3] : <Rx> - 03:36:00.854

-Type : 0x04 (Event)

-EventCode : 0xFF (HCI_LE_ExtEvent)

-Data Length : 0x06 bytes(s)

Event : 0x0513 (ATT_WriteRsp)

Status : 0x00 (Success)

ConnHandle : 0x0000

EventLen : 0x00

Dump(Rx):

04 FF 06 13 05 00 00 00 00

 

-----------------------------------------------------------------------------------------------

[4] : <Rx> - 03:36:00.953

-Type : 0x04 (Event)

-EventCode : 0xFF (HCI_LE_ExtEvent)

-Data Length : 0x0A bytes(s)

Event : 0x051B (ATT_HandleValueNotification)

Status : 0x00 (Success)

ConnHandle : 0x0000

EventLen : 0x04

AttrHandle : 0x003A

Value : 4A 04

Dump(Rx):

04 FF 0A 1B 05 00 00 00 04 3A 00 4A 04

 

-----------------------------------------------------------------------------------------------

[5] : <Rx> - 03:36:01.254 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

-Type : 0x04 (Event)

-EventCode : 0xFF (HCI_LE_ExtEvent)

-Data Length : 0x0A bytes(s)

Event : 0x051B (ATT_HandleValueNotification)

Status : 0x00 (Success)

ConnHandle : 0x0000

EventLen : 0x04

AttrHandle : 0x003A

Value : 50 E4

Dump(Rx):

04 FF 0A 1B 05 00 00 00 04 3A 00 50 E4

 

-----------------------------------------------------------------------------------------------

[6] : <Rx> - 03:36:01.452 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< only 198ms!

-Type : 0x04 (Event)

-EventCode : 0xFF (HCI_LE_ExtEvent)

-Data Length : 0x0A bytes(s)

Event : 0x051B (ATT_HandleValueNotification)

Status : 0x00 (Success)

ConnHandle : 0x0000

EventLen : 0x04

AttrHandle : 0x003A

Value : 56 24

Dump(Rx):

04 FF 0A 1B 05 00 00 00 04 3A 00 56 24

 

-----------------------------------------------------------------------------------------------

[7] : <Rx> - 03:36:01.759 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

-Type : 0x04 (Event)

-EventCode : 0xFF (HCI_LE_ExtEvent)

-Data Length : 0x0A bytes(s)

Event : 0x051B (ATT_HandleValueNotification)

Status : 0x00 (Success)

ConnHandle : 0x0000

EventLen : 0x04

AttrHandle : 0x003A

Value : 5C 04

Dump(Rx):

04 FF 0A 1B 05 00 00 00 04 3A 00 5C 04

 

-----------------------------------------------------------------------------------------------

[8] : <Rx> - 03:36:01.958 <<<<<<<<<<<<<<< only 199ms here!

-Type : 0x04 (Event)

-EventCode : 0xFF (HCI_LE_ExtEvent)

-Data Length : 0x0A bytes(s)

Event : 0x051B (ATT_HandleValueNotification)

Status : 0x00 (Success)

ConnHandle : 0x0000

EventLen : 0x04

AttrHandle : 0x003A

Value : 62 44

Dump(Rx):

04 FF 0A 1B 05 00 00 00 04 3A 00 62 44 

 

  • A couple comments:

    1. The data is not immediately sent over the air when it is queued up for transmission. In BLE, data is sent at set connection events, and the timing of connection events is determined by the connection interval when the link is established. When a packet is "sent" by the application, it does not actually get sent over the air until the next connection event.

    2. Even with the above point noted, you are correct that the OSAL timer is not necessarily always 100% accurate. This is because when a timer expires, the OSAL will still process any higher priority tasks or interrupts before processing the task set by osal_start_timerEx. This is necessary because when running the BLE stack, the radio and the layers of the stack must be given a higher priority than the application; otherwise the stack would not work.

  • Even if the timer API seems to be removed in CC2540 examples, you can use the timer1 for example to generate a task every milisecond, for example.

    Let me know if you don't know how to do it. It really seems more accurate but I may be wrong.

  • If I disabled interrupts, would there be a way for me to create a timer that could accurately measure 100microseconds?

  • Hi Margo,

    Please post new questions in new posts.

    Because of the task-loop implementation, there will always be jitter if you do the measurement using software reads or schedule events based on OSAL events or set OSAL events in an ISR.

    Feel free to read the CC2540/41 user guide in the section about Timer1 for instance (ch9), to see how you can set this up and trigger interrupts and/or have the timer being triggered by external gpio events to capture external timing.

    Best regards,
    Aslak