Sharing the SPI bus.

Hi, I’m new to FreeRTOS, I got up and running earlier today.  I’ve just looked at the schematic for our board and realise that the 2 main peripherals on our system are on the same spi bus. We’re using the SAM7S series. Bearing in mind that I will have different tasks handling both peripherals, what’s the best way to attack this? The way I see it is that I create a transmission queue, and when something wants to transmit on spi it adds the message to the queue.  A task then takes the message from the queue (providing it’s not currently transmitting) and transmits it.  Reads from the bus are obviously done in a similar manner as they’re dummy writes. Once the message is fully read, it’s place into the correct read queue for the peripheral and the task that deals with that peripheral awakes and processes the data. Both peripherals can also cause interrupts, so when the interrupt occurs I have to set up the system to do a read. Being new to using FreeRTOS (or any off the shelf RTOS) I’m wondering if I’m thinking in the right way or have I totally got it wrong!  Any ideas or information on how other people have done this would be gratefully accepted. Thanks. Adrian

Sharing the SPI bus.

Some would approach this by having a semaphore to guard access to the bus.  You must first obtain the semaphore before accessing the bus.  This way any task can access it.  Personally I think this is nearly always a bad idea. Take a look at the UART drivers given as examples.  These let any task send data to the UART without the risk of corruption by queueing data to transmit within a function (just a simple function, it does not need to be a task provided you use a scheduler lock around access).   An interrupt can be used to read data from the queue directly. Directing received data to the correct task is a little more complex and requires some decoding of addresses prior to posting onto a queue. Also take a look at the I2C driver that is in the WizNET WEB server demo.  This will give an example of how to send discrete framed messages rather than a single stream of bytes.