Handle common memory access using FreeRTOS

Hi, I am having two task vATask and vBTask. vATask – it is handling Modbus TCP communication at every 5 ms vBTask- it is handling CAN communication with a device at every 10 ms I want to access a common memory resources for both the task . I mean i want to receive data from CAN interface(CAN Rx interrupt for receiving data) and put in some CAN Rx buffers when the request come from Modbus TCP then i want to send the requested data from CAN Rx buffers. When some request comes from Modbus TCP to write some data i want to write in CAN Tx buffers and send it to CAN interface. I am worried about the data corruption because both task accesssing the same memory locations.I After reading FreeRTOS documentation i got to know about semaphore and mutex. I am new to RTOS concepts . Can you please guide me what to use(semaphore or mutex) and how can i use in my application. Regards, Savindra Kumar

Handle common memory access using FreeRTOS

The answer really depends on the type of the data. If you are writing data from an interrupt, then reading it from a task, then you have one writer and one reader, which is the simplest case. If you have one writer and one reader and the size of the data is the natural size of the architecture, then you don’t need to use any protection at all. For example, if you are on a 32-bit MCU and the data is 32-bits, then the read will always be atomic (all 32-bits will be read at once) – so you have nothing to worry about. If the data cannot be read atomically, then you do need to concern yourself with avoiding data corruption – for example if the data is a 64-bit type on a 32-bit architecture (so requiring two memory accesses), or if the data is a structure that has more than one member and all the members must be read atomically – then probably the easiest thing to do is perform the read from a critical section (so wrap the read with taskENTERCRITICAL() / taskEXITCRITICAL()).