2024-05-02 08:53 AM
Hi all,
I currently working on a project using TouchGFX, multiple tasks are using functions from <math.h> which are not re-entrant, so I have them mutex protected. The drivers using these functions are written in C.
Does anyone know if the TouchGFX library uses any non re-entrant library functions? Do I need to be careful with anything I use in the application?
Kind Regards,
Anthony
Solved! Go to Solution.
2024-05-04 02:58 PM
These should all be thread and interrupt safe on ARM processors. May be need a different library?
2024-05-04 12:16 PM - edited 2024-05-04 12:40 PM
What do you think is the relationship between TouchGFX and <math.h>
You have one FreeRTOS task with which the TouchGFX layer is started.
what is the role of this mutex?!?
These are functions that do not use a shared resource
Use the MPV.
In most projects I've done, there are several functions in the Screen classes that update variables on the display. Everything else is in the model
2024-05-04 01:39 PM
Which functions in math.h ?
I can see string.h perhaps strtok() it would be better to use strsep()
2024-05-04 02:11 PM
I have multiple other tasks using the maths functions, not just the TouchGFX task.
Some functions in <math.h> are #defined out by _REENT_ONLY, such as sqrt(). So if I want to use this function from multiple tasks safely I need to make sure access to this function is mutex protected.
I do use MVP, just that some of the calculations I am doing in the other tasks so that the UI task doesn't have to handle everything.
I was curious as to whether the renderer core library makes use of these maths functions, but source code is not provided, maybe there is another way to find out.
From <math.h>
/* Reentrant ANSI C functions. */
#ifndef __math_68881
extern double atan (double);
extern double cos (double);
extern double sin (double);
extern double tan (double);
extern double tanh (double);
extern double frexp (double, int *);
extern double modf (double, double *);
extern double ceil (double);
extern double fabs (double);
extern double floor (double);
#endif /* ! defined (__math_68881) */
/* Non reentrant ANSI C functions. */
#ifndef _REENT_ONLY
#ifndef __math_68881
extern double acos (double);
extern double asin (double);
extern double atan2 (double, double);
extern double cosh (double);
extern double sinh (double);
extern double exp (double);
extern double ldexp (double, int);
extern double log (double);
extern double log10 (double);
extern double pow (double, double);
extern double sqrt (double);
extern double fmod (double, double);
#endif /* ! defined (__math_68881) */
#endif /* ! defined (_REENT_ONLY) */
2024-05-04 02:19 PM
Yes, I avoid strtok() and use a function similar to what you suggest.
Maths functions include sqrt, atan2, cosh, sinh, and some of the other hyperbolic functions, but I noticed the issue through two tasks both using sqrt().
2024-05-04 02:58 PM
These should all be thread and interrupt safe on ARM processors. May be need a different library?
2024-05-05 12:51 AM
Interesting, I did also read that (ARM website somewhere).
Probably best to set up an experiment and test whether the problem is related to those functions or some other issue is at play.
2024-05-05 02:13 AM
Sorry, I haven't had a similar problem. For me, the only solution is a wrapper with mutexes
2024-05-05 03:42 AM
The routines and the task swapping should push and save MCU and FPU context.
If the FPU context isn't saved all the math using it will be compromised, by interrupts or task changes.
Build without the FPU, so it uses the software math library.
2024-05-05 03:58 AM
I will double check this, I am currently building with FPU.
At some point I noticed my project didn't have ENABLE_FPU (CubeMX freertos option), but this is now enabled, I should remove the mutexes and see if it now works with this defined.
It sounds very much like the FPU context not being saved, so will investigate based on that.
Thanks.