Data Share multiple thread write and read


I have “once more” question about data sharing between threads. My specific problem

I need to control motor in real time but using additionally a lot different libraries functionalities which are in separate threads.

So threads

  1. Default bus -> communication (can bus)
  2. Motor thread
  3. Sensors threads
  4. and etc

everything is crossed, data also could be needed in the same thread in different functions

I have decided to use one data structure

 struct Parameters {
bool PowerOn;
uint8_t ControlType;
float ControlValue;
bool Calibrate;
uint8_t PoleNumber;
uint8_t FOCModulation;
float VEL_P;
float VEL_I;
float VEL_U_RAMP;
float VEL_FILTER_Tf;
float Angle;
uint8_t Orientation;
uint8_t NodeID;

Why 1 structure ?
All threads shares data
for example if I need to control an motor speed

  1. I am read/write from EEPROM default data. reading to a variable
  2. Motor control thread in real time checks this data as quick as possible to smooth motor work
  3. communication thread sending to user current speed value
  4. Communication thread must write a new value to the variable if user changes it
  5. if user decided to store Values in EEPROM also must be performed this function in real time

so we have multiple read and write operations and I have no luck to control everything.

I need some advanced help with it.

I am using STM32F303, STMCubeIDE, C++ … so CMSIS
I tried semaphores, eventsbits, …

Maybe to separate whole structure to smaller one ?
Maybe to use somehow memory pools ?

I will be glad to get some examples to test possible solutions or advise.

Are you controlling the motor directly, if so, what is the required cycle time?
If you are not controlling the motor directly, but receiving the motor status and sending motor commands over the CAN bus, what is the required cycle time?

Writing to an EEPROM is always going to disrupt your cycle times unless you do it from a task that is not blocking the motor control interrupts and/or control task. If you can, do that using a DMA or other such low processor overhead mechanism.


Not direct. Motor thread uses PWM generated signals. PWM signal is about 24 KHz. So only timers generating PWM signals and such signals modulation mathematics directly influence cycle time. Any other delays will disrupt PWM signal timing. Howerver at each cycle I have additional structure checking for the data change.
So for this case It was used evetsbit to say if are data is changed and only in this case to read a new variable, for example to read a new speed value.
Yes EEPROM is a mountain comparing times needed for the motor control and writing to memory. So it’s is used only at beginning to read default values while nothng more is activated and to write data only after user push the GUI button. DMA is used from the begining.
Practically it’s working but time to crash variating from 20 min to 8 hours. Without data sharing, structure and with a fixed values it’s worked without crashes for a few times for a few days until I just power down it.