I have been using the old William Davy posix port in a linux FreeRTOS simulation and decided to try the newest posix port that is part of the FreeRTOS distribution.
I tried out the latest posix simulation files from the directory portable/ThirdParty/GCC/Posix.
I encountered a few compiler warnings about unused variables, but in my build warnings become errors so it prevents compilation. So I commented them out. Maybe the official posix port should delete the lines with unused variables?
Also when using the posix port with CMSIS, I got a linker error about undefined reference to v_ipsr. This variable used to be defined in the William Davy port that I was using from CMSIS-FreeRTOS-on-Posix-Linux github repo. Edit: Looks like this v_ipsr came from the cmsis_compiler.h file that I obtained from a similar place to the port.c that I obtained (canāt post the link, new users only allowed 2 links), so its probably better I remove this from my port.c and change it to 0 in the cmsis_compiler.h file. Not sure, Iād have to study it more. I included this part in my patch.
Additionally the usage of xTaskGetIdleTaskHandle() was giving me a link error. In other FreeRTOS files its usage is wrapped with an ā#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )ā, so I added that too.
As I am a new user to this forum I am not allowed to upload the file. So I am pasting a patch here:
--- port_old.c 2020-12-10 23:03:06.136862300 -0500
+++ port_new.c 2020-12-10 22:58:54.384621600 -0500
@@ -198,8 +198,10 @@
sigwait( &xSignals, &iSignal );
}
+#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
/* Cancel the Idle task and free its resources */
vPortCancelThread( xTaskGetIdleTaskHandle() );
+#endif
#if ( configUSE_TIMERS == 1 )
/* Cancel the Timer task and free its resources */
vPortCancelThread( xTimerGetTimerDaemonTaskHandle() );
@@ -323,7 +325,7 @@
}
static uint64_t prvStartTimeNs;
-static uint64_t prvTickCount;
+//static uint64_t prvTickCount;
/*
* Setup the systick timer to generate the tick interrupts at the required
@@ -364,7 +366,7 @@
{
Thread_t *pxThreadToSuspend;
Thread_t *pxThreadToResume;
-uint64_t xExpectedTicks;
+//uint64_t xExpectedTicks;
uxCriticalNesting++; /* Signals are blocked in this signal handler. */
@@ -471,7 +473,7 @@
static void prvSuspendSelf( Thread_t *thread )
{
-int iSig;
+//int iSig;
/*
* Suspend this thread by waiting for a pthread_cond_signal event.
@@ -559,3 +561,7 @@
}
/*-----------------------------------------------------------*/
+
+#ifdef FREERTOS_CMSIS
+volatile uint32_t v_ipsr;
+#endif
Thanks @gedeonag. I have tried your change. I am getting a compilation error that I didnāt have before:
../../freertos_lib/posix_sim/port.c: In function āprvSetupSignalsAndSchedulerPolicyā:
/gitworkspace/rbresali/evmb/mc5u/MC5U/freertos_lib/posix_sim/port.c:528:28: error: incompatible type for argument 3 of āpthread_sigmaskā
528 | *&xSchedulerOriginalSignalMask );
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| sigset_t {aka struct <anonymous>}
In file included from /usr/include/signal.h:396,
from ../../freertos_lib/posix_sim/port.c:53:
/usr/include/bits/sigthread.h:32:31: note: expected ā__sigset_t * restrictā {aka āstruct <anonymous> * restrictā} but argument is of type āsigset_tā {aka āstruct <anonymous>ā}
32 | __sigset_t *__restrict __oldmask)__THROW;
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
My /usr/include/bits/sigthread.h line 396 has:
/* Modify the signal mask for the calling thread. The arguments have
the same meaning as for sigprocmask(2). */
extern int pthread_sigmask (int __how,
const __sigset_t *__restrict __newmask,
__sigset_t *__restrict __oldmask)__THROW;
I still have the remaining issue that is CMSIS related.
In CMSIS there is a macro āIS_IRQ()ā that is used to determine if we are in an interrupt context. In CMSIS I find it used in osDelay() function as below:
osStatus_t osDelay (uint32_t ticks) {
osStatus_t stat;
if (IS_IRQ()) {
stat = osErrorISR;
}
else {
stat = osOK;
if (ticks != 0U) {
vTaskDelay(ticks);
}
}
return (stat);
}
I think in the posix port the IS_IRQ() should return ātrueā if we are in an interrupt handler such as the tick handler
How to get this functionality into the posix port?
Is there already some macro available in the posix port to know if we are in an IRQ and I could make the IS_IRQ() in CMSIS utilize it?
For reference:
In the CMSIS-FreeRTOS-on-Posix-Linux github repo (the forum is not letting me post the link) this is solved as follows.
@rtel or moderators, my original posts are flagged as spam, probably because I have some links in them. I am unable to edit the posts to remove the links. Can you unhide the posts? You can see they are not spam, the links are relevant to the issue.
Can you also give me the ability to post links?
EDIT: I figured out how to edit my original posts, I removed the links so that they wonāt be marked as spam.
EDIT2: Looks like I was able to edit because moderators unignored the posts. I still have the links removed, too much time to put them back. Thanks moderators.
@rowbearto, Iāve approved your posts and elevated your user privilege so you should be able to post links without getting put into a review queue. It was triggered because of multiple links to the same domain multiple times.
Sounds like we are making progress. I would be very reluctant to add CMSIS specific features into the POSIX port code. I am not aware of something like IS_IRQ being part of the Posix standard, so if we refer to this in any of our code it would stop working on all other Posix platforms.
The whole benefit of using standard Posix only is that the code will be portable without having to resort to hundreds of #ifdef statements, so that should be avoided at all costs.
@cobusve: I am not asking for IS_IRQ() to be referred to in the posix port.
What I am asking for whether there is information from the posix port that I can use in the my definition of IS_IRQ() which is outside of the posix port.
I would like to be able to know if the port is currently running in an ISR, such as the tick handler. Many FreeRTOS functions canāt run from ISRs, that is why there are āISRā versions of functions.
Specifically, the CMSIS code has a function āosDelay()ā which should not run from an ISR because it is blocking. So it does a check if it is an ISR.
So it would be good if the posix port could set some flag to indicate it is in an ISR like the tick handler. Then if posix port provides this information I can use it in the definition of IS_IRQ().
In my previous post I show code from a port.c that was created that will set some flags upon entry/exit to tick handler, and other signal handlers that can be used for CMSIS to know that it is in an interrupt routine or not.
There is no such thing in the existing port, but I am trying to understand what you are trying to do here so that I can suggest a plan of action because I think this is actually simpler than it sounds unless I am missing something here.
The Posix port only has 1 pseudo-interrupt, it does not support any others, and that interrupt is the tick interrupt, which calls prvSetupTimerInterrupt found in port.c. This means that you could modify the port for your use case by setting a global variable when entering that function, and clearing it when you leave the function. I believe however that you do not need to do this.
The reason I am asking why you need this, is that no other code can run while this ISR is operational, which means that whenever you call IS_IRQ() the return would be 0 in 100% of possible cases in your own code. As a result you could just implement IS_IRQ() as follows and it should be fine
Indeed I use the tick hook to invoke the ISRs that we have in real hardware in the posix simulation environment. So it could be very useful to know from the tick hook. I suppose I could add such a flag in my tick handler, but at the moment Iām still using this other CMSIS/posix simulation where that is built in. So if I moved it into my application code in my tick hook then I would only want that in the new environment using the FreeRTOS official posix simulator, but Iām not ready to make the full switch to the official one as I just learned about it. At this point I am in the exploring phase to switch to the official port.
As the CMSIS environment has this IS_IRQ(), I would like to provide it somehow. On the real hardware it looks at a register that says whether we are in an ISR or not, but that doesnāt exist on the posix sim.
For posix sim it seems that a pseudo-isr like the tick handler uses posix signals. I tried to look up how to test if we are in a signal handler but it seems there is no posix way to do that.
Thank you for pushing the fix. I noticed today that this error also exists in the 202012.00 LTS release, which we would like to switch to for our project. Will fixes like this be pushed to LTS in the future?