Thursday, October 11, 2012
Wednesday, October 10, 2012
3. MQX Events. Event Handling, Building an Event based application
Events
Events can be used to synchronize tasks. Just to make it
simpler, you can imagine an event a bit in the 32 bit integer number (mostly, that is the word size of a MQX RTOS).
So this is how it would happen:
a) Somebody-1 would be waiting for this bit to be set. Until
this bit is set that somebody would be stuck.
b) And Somebody-2 would set that bit so that somebody-1 can
proceed further.
So in crude and very layman sense, events in RTOS are
nothing but setting/clearing one bit.
Now let’s add few more technicalities to it.
Somebody-1 above normally would be tasks which would wait
for something. And sombebody-2 could be another Task or ISR.
In fact many embedded applications work this way, ISR gets triggered
and as ISR handlers should be very small, they normal set an event and exit. So
it serves both purposed ISR finishes quickly and also your application logic of
waiting gets out of ISR. (in fact this is the most widely approach of designing ISR/task based applications).
Why not use Setting/Clearing of bits than Events? What extra ROTS event handling adds apart from setting
clearing bits?
a)
First and for most, whenever
RTOS calls are setting/clearing event bit that part of kernel code is 100%
atomic and safe. I mean, as done for other parts of kernel, when updating (here
setting/clearing bit) interrupts are disabled.
(Imagine you try to mimic the events and you are about to clear the bit and suddenly and ISR comes which tries to set the same bit. Well, that would be it for your logic.)
If you have access to MQX source it would be fun exercise to trace the implementation of set/get event.
(Imagine you try to mimic the events and you are about to clear the bit and suddenly and ISR comes which tries to set the same bit. Well, that would be it for your logic.)
If you have access to MQX source it would be fun exercise to trace the implementation of set/get event.
b)
Option to wait for single
event (one event) or many events in single call.
_event_wait_all -> if you choose this one your wait call would be blocked until all the events(bits) you specified in the mask are set.
_event_wait_any -> if any of the events(bit) specified in the mask is triggered.
_event_wait_all -> if you choose this one your wait call would be blocked until all the events(bits) you specified in the mask are set.
_event_wait_any -> if any of the events(bit) specified in the mask is triggered.
c)
RTOS has data structure called run queue and blocked
queue. So event wait call need not be a infinite for loop. If you
are implementing the events try to think how would be your WaitEvent call. As you don't have those data structure it would have to be infinite loop.
RTOS has that(run, blocked queue) info, so when it hits a event wait call, it puts that task to blocked list. When it receives the event it checks the blocked queue and puts that task to ready queue.
Infinite loop are big NO in embedded systems. As in that case, CPU would be performing loop operation and consuming power.
RTOS has that(run, blocked queue) info, so when it hits a event wait call, it puts that task to blocked list. When it receives the event it checks the blocked queue and puts that task to ready queue.
Infinite loop are big NO in embedded systems. As in that case, CPU would be performing loop operation and consuming power.
d)
Option to unblock all the
tasks. Suppose 3 tasks are waiting for an event, and event is received. There in an
option in MQX which would automatically clear the event as soon as it arrives
and unblock all the tasks.
e)
You can also pass timer parameter to the wait event calls, means you will only be blocked for x mill
secs.
BTW,
MQX comes with two type of event handling, one with lightweight event
handling and second one is (let’s say) normal event handling. Lightweight APIs
are targeted to single processor systems while other one can handle more than
one processor. Remember, in first MQX post when I said MQX can handle more than one
processors.
PS: I intentionally have avoided to put the API names, thinking behind this is concepts are more important than the API names.
Tuesday, October 9, 2012
2. Starting MQX, Scheduling and Managing Tasks
I talked about the Task templates in my last post.
The lower the priority value of the task the higher the priority. You can remember this easily by knowing that if you create a task with priority 0, this task will run will interrupts disabled.
There is one more thing you should remember, MQX creates one ready queue for each task up to lowest priority. This creation is done by reading task template at the boot up time. So, you cannot create a task having lower priority than the lowest of task template.
If (hopefully) you are aware of any OS task creation APIs, well this ones is not that much different. Only names would change so I am skipping typing the obvious here.
Scheduling of Tasks
I won't be able to discuss the topic without describing various states of a task in MQX.
There are three states
1) Blocked : task is not ready because its blocked on some condition to occur.
2) Ready : task is ready to become active but its not active. This is because its same or lower priority than the active task.
3) Active : Task is running.
Task Creation APIs
Nothing much different than any other OS. You can create a task by api called '_task_create', you could also create a task which would be in blocked state by api named '_task_create_blocked'. Blocked task can be made ready by _task_ready().
There are other APIs too to get task id, get creator task id, get error code id, getting/setting exit handler, restarting task, setting error code etc.
Its worth mentioning error code here. Whenever something bad happens to a task it given an error code. If task is allowed to run even after error it may enter to some other error state. But the first error code is not overwritten, this is just to give clear indication to the user whats the original error.
Terminating Task
A task can terminate itself or it can be terminated by other task. Whenever a task is terminated its children are NOT killed. Whenever a task terminates, MQX frees it all resources like dynamic allocated memory blocks and partition blocks, lightweight semaphore, message queues, mutex etc.
Task Scheduling
Three policies are provided. Please note all are preemptive.
i) FIFO
ii) Round Robin
Same as FIFO but it has additional constraint that each round robin task a maximum amount of time slice during which it can be active.
You do need to set the time slice for this type of scheduling.
iii) Task Queues
Look for this space will scribe about it later.
Read about MQX Events. Event Handling, Building an Event based application here
The lower the priority value of the task the higher the priority. You can remember this easily by knowing that if you create a task with priority 0, this task will run will interrupts disabled.
There is one more thing you should remember, MQX creates one ready queue for each task up to lowest priority. This creation is done by reading task template at the boot up time. So, you cannot create a task having lower priority than the lowest of task template.
If (hopefully) you are aware of any OS task creation APIs, well this ones is not that much different. Only names would change so I am skipping typing the obvious here.
Scheduling of Tasks
I won't be able to discuss the topic without describing various states of a task in MQX.
There are three states
1) Blocked : task is not ready because its blocked on some condition to occur.
2) Ready : task is ready to become active but its not active. This is because its same or lower priority than the active task.
3) Active : Task is running.
Task Creation APIs
Nothing much different than any other OS. You can create a task by api called '_task_create', you could also create a task which would be in blocked state by api named '_task_create_blocked'. Blocked task can be made ready by _task_ready().
There are other APIs too to get task id, get creator task id, get error code id, getting/setting exit handler, restarting task, setting error code etc.
Its worth mentioning error code here. Whenever something bad happens to a task it given an error code. If task is allowed to run even after error it may enter to some other error state. But the first error code is not overwritten, this is just to give clear indication to the user whats the original error.
Terminating Task
A task can terminate itself or it can be terminated by other task. Whenever a task is terminated its children are NOT killed. Whenever a task terminates, MQX frees it all resources like dynamic allocated memory blocks and partition blocks, lightweight semaphore, message queues, mutex etc.
Task Scheduling
Three policies are provided. Please note all are preemptive.
i) FIFO
Its default policy. Next task to run here would be that
which is waiting for longest. The active task runs until anyone of flowing occurs
- -Active task voluntarily relinquishes
the processor because it calls a blocking function.
- -Higher priority interrupt
occurs
- -A task having higher
priority becomes ready
ii) Round Robin
Same as FIFO but it has additional constraint that each round robin task a maximum amount of time slice during which it can be active.
You do need to set the time slice for this type of scheduling.
iii) Task Queues
Look for this space will scribe about it later.
Read about MQX Events. Event Handling, Building an Event based application here
Monday, October 8, 2012
Saturday, October 6, 2012
1. MQX RTOS - Introduction
In next few days, I would scribe what I know about MQX. The notes can be used by the newbies. So here we go....
Whats MQX
MQX is a real time operating system from ARC and is designed for uni processor, multi processor and distributed processors. MQX provide you runtime library of functions which can be used by multitasking real-time applications.
There are certain services which are marked as core which would anyway would come with MQX, while others can be added as per your requirements.
Core services/components include automatic task creation, task scheduling, lightweight semaphores, task errors etc.
You can always add toppings like watchdogs, exception handling, message queues, event handling, semaphores, interrupts, kernel log etc.
How you would initialize the MQX/Where in my code MQX is getting started
Well, if you are working on project which is using MQX rots, you search for the function call named _mqx.
This function 'mqx' would take few very important arguments which would tell how would MQX would behave for you. Some of this would include
a)Number of processors in you system (remember when i told it can work for multiprocessor systems as well)
b)RAM start/end address for kernel
c)stack area for Interrupts
d) address of task template, if you fill this template, these tasks would be created by the MQX after initializations. Lots to guess but, this task template would provide information about the task stack size, priority, name, default time slice etc.
e)Max hardware interrupt level. This is important filed, keep this in mind, will come back to this later.
Task template creates tasks once RTOS initialization has finished, but user is allowed to create the task at run time. You can also provide an optional exception handler or exit function. MQX would call them appropriately
AOM - Automated Overlay Manager
This feature is really useful when your target system has limited main memory and large external memory. You can use the Overlays to put the 'different functions and data to the same memory location in the RAM at different point of times'. What else you want isn't that cool.
An overlay can consist of a function, a group of function or data. When a overlay is loaded in the RAM, it may overwrite the existing overlay.
Overlays are not part of standard C language and developer must write their over libs or rely on vendor tools like Automated Overlay Manger. Well, you know it now, MQX has already done that for you.
To a make a function overlay, function name must be prefixed by _Overlay(). This call would create a distinctive overlay for that function.You can also create a overlay group by giving group id as _Overlap(GroupID).
You need to specify the size of overlay memory in linker configurations (I would come back to it later).
Events
Bit based synchronization mechanism used by tasks and interrupts.
Timers
Timers provide periodic execution of an application function. MQX supports oneshot timers (they expire once) and periodic timers (they expire repeatedly at a given interval). You can set timers to start at a specified time or after a specified duration.
When you set a timer, you specify the notification function that Timer Task calls when the timer expires. The notification function can be used to synchronize tasks by sending messages, setting events, or using one of the other MQX synchronization mechanisms.
Watchdogs
Watchdogs are an option component that lets you detect task starvation and deadlock conditions at the task level.
Interrupt and exception handling
MQX services all hardware interrupts within a range that the BSP defines and saves a minimum context for the active task. MQX supports fully nested interrupts. Once inside an interrupt service routine (ISR), an application can re-enable any interrupt level. To further reduce interrupt latencies, MQX defers task rescheduling until after all ISRs have run. In addition, MQX reschedules only if a new task has been made ready by an ISR. To reduce stack size, MQX supports a separate interrupt stack.
An ISR is not a task; it is a small, high-speed routine that reacts quickly to hardware interrupts. An ISR is usually written in C. Its duties include resetting the device, getting its data, and signaling the appropriate task. An ISR can be used to signal a task with any non-blocking MQX functions.
There are few other components as well but what I discussed above are the core ones. Will come back to them at appropriate times.
Read about Scheduling Tasks and Starting MQX here.
Whats MQX
MQX is a real time operating system from ARC and is designed for uni processor, multi processor and distributed processors. MQX provide you runtime library of functions which can be used by multitasking real-time applications.
There are certain services which are marked as core which would anyway would come with MQX, while others can be added as per your requirements.
Core services/components include automatic task creation, task scheduling, lightweight semaphores, task errors etc.
You can always add toppings like watchdogs, exception handling, message queues, event handling, semaphores, interrupts, kernel log etc.
How you would initialize the MQX/Where in my code MQX is getting started
Well, if you are working on project which is using MQX rots, you search for the function call named _mqx.
This function 'mqx' would take few very important arguments which would tell how would MQX would behave for you. Some of this would include
a)Number of processors in you system (remember when i told it can work for multiprocessor systems as well)
b)RAM start/end address for kernel
c)stack area for Interrupts
d) address of task template, if you fill this template, these tasks would be created by the MQX after initializations. Lots to guess but, this task template would provide information about the task stack size, priority, name, default time slice etc.
e)Max hardware interrupt level. This is important filed, keep this in mind, will come back to this later.
Task template creates tasks once RTOS initialization has finished, but user is allowed to create the task at run time. You can also provide an optional exception handler or exit function. MQX would call them appropriately
AOM - Automated Overlay Manager
This feature is really useful when your target system has limited main memory and large external memory. You can use the Overlays to put the 'different functions and data to the same memory location in the RAM at different point of times'. What else you want isn't that cool.
An overlay can consist of a function, a group of function or data. When a overlay is loaded in the RAM, it may overwrite the existing overlay.
Overlays are not part of standard C language and developer must write their over libs or rely on vendor tools like Automated Overlay Manger. Well, you know it now, MQX has already done that for you.
To a make a function overlay, function name must be prefixed by _Overlay(). This call would create a distinctive overlay for that function.You can also create a overlay group by giving group id as _Overlap(GroupID).
You need to specify the size of overlay memory in linker configurations (I would come back to it later).
Events
Bit based synchronization mechanism used by tasks and interrupts.
Timers
Timers provide periodic execution of an application function. MQX supports oneshot timers (they expire once) and periodic timers (they expire repeatedly at a given interval). You can set timers to start at a specified time or after a specified duration.
When you set a timer, you specify the notification function that Timer Task calls when the timer expires. The notification function can be used to synchronize tasks by sending messages, setting events, or using one of the other MQX synchronization mechanisms.
Watchdogs
Watchdogs are an option component that lets you detect task starvation and deadlock conditions at the task level.
Interrupt and exception handling
MQX services all hardware interrupts within a range that the BSP defines and saves a minimum context for the active task. MQX supports fully nested interrupts. Once inside an interrupt service routine (ISR), an application can re-enable any interrupt level. To further reduce interrupt latencies, MQX defers task rescheduling until after all ISRs have run. In addition, MQX reschedules only if a new task has been made ready by an ISR. To reduce stack size, MQX supports a separate interrupt stack.
An ISR is not a task; it is a small, high-speed routine that reacts quickly to hardware interrupts. An ISR is usually written in C. Its duties include resetting the device, getting its data, and signaling the appropriate task. An ISR can be used to signal a task with any non-blocking MQX functions.
There are few other components as well but what I discussed above are the core ones. Will come back to them at appropriate times.
Read about Scheduling Tasks and Starting MQX here.
Sunday, April 29, 2012
Cracking Bitwise Operators, Important tricks Bitwise operator
In interviews embedded engineers and Driver developers are often asked various bitwise questions.
You can never predict what interviewer will ask, but if you have few tricks in mind you can crack the questions easily. I am putting few of stuff here.
Assume num is the unsigned integer.
1) Extract a certain bit, say bit p from the number
bit = num & 1<
2) Clear bit p in a number
num = num &(~(1<
Core concept : ~(1<
num = num & (1<
4) A number XORed with itself results zero, if you XOR number again with itself(3 times) it will again form the original number. Remember this is very important property.
5) Suppose you want to get n bits from the number starting from position p (This is core logic of many difficult bitwise questions)
result = (num>>p) & ((1<
Core Concept : (1<
Keep looking this space will add more.
You can never predict what interviewer will ask, but if you have few tricks in mind you can crack the questions easily. I am putting few of stuff here.
Assume num is the unsigned integer.
1) Extract a certain bit, say bit p from the number
bit = num & 1<
2) Clear bit p in a number
num = num &(~(1<
Core concept : ~(1<
3) Set a bit p in a number
num = num & (1<
4) A number XORed with itself results zero, if you XOR number again with itself(3 times) it will again form the original number. Remember this is very important property.
5) Suppose you want to get n bits from the number starting from position p (This is core logic of many difficult bitwise questions)
result = (num>>p) & ((1<
Core Concept : (1<
Keep looking this space will add more.
Subscribe to:
Posts (Atom)