Integrating freeRTOS to an existing project - c

I'am attempting to integrate freeRTOS to my application that run on AT32UC3A0512. I downloaded a freeRTOS project example for EVK1100 (it Supports the AT32UC3A) and try to include the kernel source files, so my application hierarchy looks like :
src
|ASF
|main.c
|freertos
|freertos-7.0.0
|source
the main looks like
int main()
{
char str[5];
uint8_t num;
enable_Timer();
enable_Timer_Interrupt();
sprintf (str, %03d, num);
while(1)
{
// Wait the timer interrupt to happen
}
}
Now by just excluding the freertos folder from my project, the timer interrupt are fired and all works well,
However if i include the freertos folder (no inclusion nor call to freertos sources is done) the timer interrupt are not fired.
It seems crazy but i don't know how the behaviour of sprintf has changed this way and also i don't see the relation between sprintf and the interrupt controller

The RTOS and your application might be using the same timer interrupt. Check RTOS port.c file to find which timer is it using for timer tick. CHeck your interrupt function and port.c interrupt function definition.

Related

How can I determin if execution takes place in thread mode or if an exception is active? (ARMv7-A architecture)

I am using FreeRTOS on an ARM Cortex A9 CPU und I'm desperately trying to find out if it is possible to determin if the processor is executing a normal thread or an interrupt service routine. It is implemented in V7-a architecture.
I found some promising reference hinting the ICSR register (-> VECTACTIVE bits), but this only exist in the cortex M family. Is there a comparable register in the A family as well? I tried to read out the processor modes in the current processor status register (CPSR), but when read during an ISR I saw that the mode bits indicate supervisor mode rather than IRQ or FIQ mode.
Looks a lot like there is no way to determine in which state the processor is, but I wanted to ask anyway, maybe I missed something...
The processor has a pl390 General Interrupt Controller. Maybe it is possible to determine the if an interrupt has been triggered by reading some of it's registers?
If anybody can give me a clue I would be very greatfull!
Edit1:
The IRQ Handler of FreeRTOS switches the processor to Superviser mode:
And subsequently switches back to system mode:
Can I just check if the processor is in supervisor mode and assume that this means that the execution takes place in an ISR, or are there other situations where the kernel may switches to supervisor mode, without being in an ISR?
Edit2:
On request I'll add an overal background description of the solution that I want to achieve in the first place, by solving the problem of knowing the current execution context.
I'm writing a set of libraries for the CortexA9 and FreeRTOS that will access periphery. Amongst others I want to implement a library for the available HW timer from the processor's periphery.
In order to secure the access to the HW and to avoid multiple tasks trying to access the HW resource simultaneously I added Mutex Semaphores to the timer library implementation. The first thing the lib function does on call is to try to gain the Mutex. If it fails the function returns an error, otherwise it continouses its execution.
Lets focus on the function that starts the timer:
static ret_val_e TmrStart(tmr_ctrl_t * pCtrl)
{
ret_val_e retVal = RET_ERR_DEF;
BaseType_t retVal_os = pdFAIL;
XTtcPs * pHwTmrInstance = (XTtcPs *) pCtrl->pHwTmrInstance;
//Check status of driver
if(pCtrl == NULL)
{
return RET_ERR_TMR_CTRL_REF;
}else if(!pCtrl->bInitialized )
{
return RET_ERR_TMR_UNINITIALIZED;
}else
{
retVal_os = xSemaphoreTake(pCtrl->osSemMux_Tmr, INSTANCE_BUSY_ACCESS_DELAY_TICKS);
if(retVal_os != pdPASS)
{
return RET_ERR_OS_SEM_MUX;
}
}
//This function starts the timer
XTtcPs_Start(pHwTmrInstance);
(...)
Sometimes it can be helpful to start the timer directly inside an ISR. The problem that appears is that while the rest of function would support it, the SemaphoreTake() call MUST be changed to SemaphoreTakeFromISR() - moreover no wait ticks are supported when called from ISR in order to avoid a blocking ISR.
In order to achieve code that is suitable for both execution modes (thread mode and IRQ mode) we would need to change the function to first check the execution state and based on that invokes either SemaphoreTake() or SemaphoreTakeFromISR() before proceeding to access the HW.
That's the context of my question. As mentioned in the comments I do not want to implement this by adding a parameter that must be supplied by the user on every call which tells the function if it's been called from a thread or an ISR, as I want to keep the API as slim as possible.
I could take FreeRTOS approch and implement a copy of the TmrStart() function with the name TmrStartFromISR() which contains the the ISR specific calls to FreeRTOS's system resources. But I rather avoid that either as duplicating all my functions makes the code overall harder to maintain.
So determining the execution state by reading out some processor registers would be the only way that I can think of. But apparently the A9 does not supply this information easily unfortunately, unlike the M3 for example.
Another approch that just came to my mind could be to set a global variable in the assembler code of FreeRTOS that handles exeptions. In the portSAVE_CONTEXT it could be set and in the portRESTORE_CONTEXT it could be reset.
The downside of this solution is that the library then would not work with the official A9 port of FreeRTOS which does not sound good either. Moreover you could get problems with race conditions if the variable is changed right after it has been checked by the lib function, but I guess this would also be a problem when reading the state from a processor registers directly... Probably one would need to enclose this check in a critical section that prevents interrupts for a short period of time.
If somebody sees some other solutions that I did not think of please do not hesitate to bring them up.
Also please feel free to discuss the solutions I brought up so far.
I'd just like to find the best way to do it.
Thanks!
On a Cortex-A processor, when an interrupt handler is triggered, the processor enters IRQ mode, with interrupts disabled. This is reflected in the state field of CPSR. IRQ mode is not suitable to receive nested interrupts, because if a second interrupt happened, the return address for the first interrupt would be overwritten. So, if an interrupt handler ever needs to re-enable interrupts, it must switch to supervisor mode first.
Generally, one of the first thing that an operating system's interrupt handler does is to switch to supervisor mode. By the time the code reaches a particular driver, the processor is in supervisor mode. So the behavior you're observing is perfectly normal.
A FreeRTOS interrupt handler is a C function. It runs with interrupts enabled, in supervisor mode. If you want to know whether your code is running in the context of an interrupt handler, never call the interrupt handler function directly, and when it calls auxiliary functions that care, pass a variable that indicates who the caller is.
void code_that_wants_to_know_who_called_it(int context) {
if (context != 0)
// called from an interrupt handler
else
// called from outside an interrupt handler
}
void my_handler1(void) {
code_that_wants_to_know_who_called_it(1);
}
void my_handler2(void) {
code_that_wants_to_know_who_called_it(1);
}
int main(void) {
Install_Interrupt(EVENT1, my_handler1);
Install_Interrupt(EVENT2, my_handler1);
code_that_wants_to_know_who_called_it(0);
}

Request timer interrupts in userspace program?

Is it possible (preferably in an OS-independent fashion, although I happen to be using Windows) to request a timer interrupt in a userspace program?
Here is some pseudo-code which may illustrate what kind of functionality I'm looking for:
#include <time_library_x.h> //For setHandler() and set_timer_for_ms()
void timerInterruptHandler() {
update_something();
set_timer_for_ms(50);
}
void main() {
setHandler(timerInterruptHandler);
set_timer_for_ms(50);
while(1) {
very_boring_data_collection();
}
}
Anyone know what "library x" is?
In Windows, there is Timers object to create Timer object and register callback function.
In Linux using SIGALRM and setitimer is common to implement timer things, but I have not seen such libraries which integrates those for mult-platform use.
Windows Timers Example : https://msdn.microsoft.com/en-us/library/windows/desktop/ms644901(v=vs.85).aspx

FreeRTOS task not resuming

I'm using an ARM Cortex-M4 microcontroller to develop an application with FreeRTOS.
For exact timing, I want use an interrupt based timer. The interrupt has an appropriate priority so it should be able to call the FreeRTOS API. The ISR is called periodically and should wake up a task as in the given code:
/* This function is executed by the task I'd like to resume */
void hello_task() {
while (1) {
vTaskSuspend(task);
printf("Tick\n");
}
}
/* The ISR is called by an interrupt about 200 times per second */
void Timer_IRQHandler() {
CLEAR_INTERRUPT_FLAG();
xTaskResumeFromISR(task);
}
The ISR is executed correctly but the task doesn't resume afterwards.
Does anyone have an explaination for this behavior?
Thank you!
Read the documentation for xTaskResumeFromISR(). It tells you not to do what you are doing.
Direct to task notifications offer the best (most light weight and efficient) method of doing what you describe. There is a worked example on the following page: http://www.freertos.org/RTOS_Task_Notification_As_Counting_Semaphore.html

ARM Cortex M4: test from unpriviledged mode if inside interrupt

I am working with the following CPU: Stellaris LM4F120H5QR Microcontroller. This CPU contains a MPU and I want to utalize this thing. But a lot of registers are no longer accessable when inside unpriviliged mode and I cannot seem to find a register which indicates that the system is inside an interrupt and is readable from unpriviliged mode.
I need this because there is code that might take a different route when called from an interrupt. If I do a wrong check from unprivileged mode, the system will immedeatly jump to access fault.
So how can I check if a function is called from an interrupt in a way that will not create a fault when called from unprivileged mode?
According to ARM's documentation the CONTROL and ISR registers might be just what you need.
So, using the CMSIS prototypes, the code might look like:
if (__get_IPSR() || !(__get_CONTROL() & 0x1))
{
/* Privilged code */
}
else
{
/* Unprivileged code */
}
As far as I know, reading those should be allowed even to a user thread.

Timer tick implementation in c

I am doing some embedded system work and have a couple of question. I have a timer function which fires every 100ms. I now need to get the actual time once the system has started .
Currently my code is something like this:
struct timer
{
uint8_t millis_100;
uint8_t minute_455;
}
void tick()
{
//fires evry 100ms
timer_task.millis_100++;
}
However I am confused if this is the right approach since I will need to check if millis_100 overflowed to 0 and then increment it inside the ISR routine. If I need more than 455*2^8-1 then I would need to put another if statement in the ISR. IS this how system ticks are used to make software timers? Or is there a more elegant solution?
If you are worried about the timer overflowing, why not use an unsigned long variable ??
Check the timer implementation in linux kernel timer.c, timer.h
For more details, read the section The Timer API in LDD3/ch07

Resources