FreeRTOS and Atmel SAM4S SPI with several CS's

goldie13 wrote on Monday, December 16, 2013:

Hi,

I am using the freertos SPI API from ASF. In the freertos spi_write/spi_read packets, the API uses the DMA and also gives the ability to pass a semaphore to be notified on transfer completion.
So, if using several tasks and each task can communicate via the same SPI but on different CS, I need to modify the freertos spi_read/spi_write functions so it will first acquire the semaphores, then change the CS with “spi_set_peripheral_chip_select_value”, and then configuring the PDC… right?
This is because it is not one atom operation, and I don’t want that one task will change the CS and before it will transmit, it will switch to another task that will, again chge to a different CS, then it will switch back to the first task that will start transmitting, but now to the wrong CS…
Is this correct?

Thank you,
Assaf

rtel wrote on Monday, December 16, 2013:

I just looked at the demos that come with the driver and they appear to be setting the CS lines manually. The driver has two semaphores/mutexes, one to obtain access to the port and one to be notified of when the operation on the port is complete - is that the case? If so then it is only the semaphore used to get access to the port that prevents you from using the driver unmodified I think, and I would try to avoid modifying the driver.

You could provide your own access mutex so the operation would be as follows.

  1. Create an access mutex in your application.
  2. Open the SPI port using the driver but don't configure it to use an RX mutex, as you already have one in your application.
  3. When a task wants access to the SPI have it first obtain the access mutex within your application, rather than in the driver.
  4. Once access is obtained, set the CS as you require, then use the driver again. You can provide a notification mutex if you wish - just make sure that when the transfer is complete your application gives back the access mutex.

Regards.