Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years, 4 months ago.
Waiting microsecond in RTOS
Hi!
I am developing a device which has to send a package (using CAN-bUS) every 2.5ms, 7.5ms, and 10ms. If I send the pack in advance or late I lose synchrony and my device blocks. I am using 3 threads (could be 2 ;-)) with osPriorityRealtime and a delay inside a forever-one loop. My problem is that I only can reach delays in milliseconds. I mean, I need a delay of 2.5ms but I only can get 2 or 3ms. I had used wait_us, wait, Thread::wait...but it doesn't work
while(1){ wait_us(2500); //wait(0.0025); can.send_1(); } }
could anybody help me?
1 Answer
5 years, 4 months ago.
Hello Bruno,
Below is a test program to measure delays created using wait_us()
but without sending any CAN message. The results on LPC1768 seem OK (see below). Maybe it can help:
#include "mbed.h" Timer timer1, timer2, timer3; int delay1[20]; int delay2[10]; int delay3[5]; int i1 = 0; int i2 = 0; int i3 = 0; void task1() { timer1.start(); while (1) { timer1.reset(); wait_us(2500); if (i1 < 20) delay1[i1++] = timer1.read_us(); else break; } timer1.stop(); } void task2() { timer2.start(); while (1) { timer2.reset(); wait_us(7500); if (i2 < 6) delay2[i2++] = timer2.read_us(); else break; } timer2.stop(); } int main() { Thread* thread1 = new Thread(osPriorityRealtime, OS_STACK_SIZE); Thread* thread2 = new Thread(osPriorityRealtime, OS_STACK_SIZE); printf("Starting..\r\n"); thread1->start(task1); thread2->start(task2); timer3.start(); while (1) { timer3.reset(); wait_us(10000); if (i3 < 5) delay3[i3++] = timer3.read_us(); else break; } timer3.stop(); printf("-------------\r\n"); for (int i = 0; i < i1; i++) printf("delay1[%d]=%d\r\n", i, delay1[i]); printf("-------------\r\n"); for (int i = 0; i < i2; i++) printf("delay2[%d]=%d\r\n", i, delay2[i]); printf("-------------\r\n"); for (int i = 0; i < i3; i++) printf("delay3[%d]=%d\r\n", i, delay3[i]); }
Results on LPC1768:
Starting.. ------------- delay1[0]=2511 delay1[1]=2511 delay1[2]=2511 delay1[3]=2509 delay1[4]=2512 delay1[5]=2509 delay1[6]=2508 delay1[7]=2509 delay1[8]=2511 delay1[9]=2508 delay1[10]=2511 delay1[11]=2512 delay1[12]=2512 delay1[13]=2508 delay1[14]=2511 delay1[15]=2512 delay1[16]=2511 delay1[17]=2509 delay1[18]=2511 delay1[19]=2508 ------------- delay2[0]=7509 delay2[1]=7510 delay2[2]=7511 delay2[3]=7510 delay2[4]=7511 delay2[5]=7510 ------------- delay3[0]=10012 delay3[1]=10010 delay3[2]=10009 delay3[3]=10010 delay3[4]=10010