Time Delay in Micro C OS II - c

I have been looking for that, I am using Micro C/OS II Real Time Operating System. I couldn't find a way to create a delay apart from writing nested loops. Any way to do create a delay?

OSTimeDly() will delay/sleep a task for a specified number of ticks. OSTimeDlyHMSM() will delay a specified number of hours, minutes, seconds, milliseconds.

Related

Simulink Clock Synchronisation

In simulink if I run any simulation, it follows an internal clock. I want to run these simulations in real time.
Example: if I use a PWM pulse generator and give it a sample time of 1 second, I expect that it will generate a sample at the end of every one second real-time but the simulink clock moves very very fast (every one second real time corresponds to about 1e6 seconds smulink time). Is there any way to synchronize the simulink clock with the real time clock?
I actually need to give input to hardware at the end of every 2 seconds in a loop and that is why this kind of synchronization is needed.
Firstly note that Simulink is not a real-time environment, so anything you do related to this is not guaranteed to be anything but approximate in the timing achieved.
If your model runs faster than real-time then it can be paused at each time step until clock-time and simulation time are (approximately) equal. This is achieved by writing an S-Function.
There are several examples of doing this. For instance here or here.

Embedded programming. Implementation of real time clock .C programming

I want to implement a real time clock and timer , that prints on screen current time like this. : " HOURS:MINUTES:SECONDS "
Is it safe to use :
While(1){
.....Do sth
sleep(1);
.....Do sth
}
and then
seconds+=1;
For measure of one second pass?
You will have to check whether in your particular embedded system, sleep(1) will sleep the system for 1 second. In many of the embedded boards I have used, sleep takes the argument in milliseconds. So for 1 second sleep you would have to use sleep(1000).
If you are not too worried about accuracy then yes you can use this method. however, this will not be as accurate as you using a timer or an RTC. so for example if you want your system to do something when seconds reaches 30, a better way might be to setup a timer or an RTC alarm (based on what your embedded platform has) to more accurately measure out that time.

how to find scheduling delay for a process in c

I have a C program on linux. During execution of my program, I want to make some decisions if the process is facing scheduling delay above a threshold.
Any suggestion on how I find this statistic ?
P.S.: By scheduling delay I mean time spent by the process waiting to be scheduled i.e. time spent in the scheduler queue.
The time() function allows you to measure the "wall clock" time: http://linux.die.net/man/2/time
On the other side, the clock() function allows you to measure the CPU time used by your process: http://linux.die.net/man/3/clock
By subtracting the two, you can get an approximation of what you asked for.
PS: for more accurate measurements (time has a second resolution) you can use clock_gettime: http://linux.die.net/man/3/clock_gettime
You could set a timer to go off, say every minute, or whatever interval seems appropriate and then gather stats with getrusage() and based on those results (the difference between successive values), you could make your decision then

Calling a C function periodically on OSX

I have a function which calculates a BPM for a track from incoming data packets from a CDJ. Lets say the BPM was 124.45 beats per minute, how would I go about calling a function every 0.482 seconds (i.e. once per beat)? Would it be possible to set up another thread and set a timer?
Maybe have a look at high precision timers, here for which Apple claim 500 micrososecond accuracy which is 0.1% of your 500 (ish) millisecond requirement. You can minimise skew by reading the time at the start of your processing and calculating an offset to the next beat. Also, if you find you are often getting scheduled late, and missing beats, you can sleep for, say, 95% of the time to your next beat so the CPU can schedule something else, and then busy wait for the last few percent so you don't hog the CPU.

Conversion of msec to jiffies

i am using msecs_to_jiffies(msecs) to get delay. I need a delay of 16 ms. But the problem is the function return 1 for input 1-10, 2 for 11-20, 3 for 21-30 and son on. Hence i am unable to set proper delay. I can set delay only in factors of 10 ms. I can't change the HZ value and the function cant sleep also.
Kindly suggest solution to this problem.
Thanks
It seems your system HZ value is set to 100.
If you wish to suspend execution for a period of time in a resolution lower then the system HZ, you need to use high resolution timers (which use nsec resolution, not jiffies) supported in your board and enabled in the kernel. See here for the interface of how to use them: http://lwn.net/Articles/167897/
So, either change the system HZ to 1000 and get a jiffie resolution of 1 msec or use a high resolution timer.
You can't sleep for exactly 16ms. You can sleep for at least 16ms, but not 16ms. That's not the way Linux (or any other desktop OS) works - they're not realtime OSes and they are scheduled in a non-deterministic manner and there's nothing you can do about it.
Whatever you're trying to do, you'll have to go about it another way. With what little info you've provided, all I can say is that what you're trying to do can't be done.

Resources