# Semaphore?

**URL:** <https://forums.freertos.org/t/semaphore/4733>\
**Category:** Kernel\
**Created:** [February 15, 2007, 2:42pm UTC](https://forums.freertos.org/t/semaphore/4733 "2007-02-15T14:42:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 15, 2007, 2:42pm UTC](https://forums.freertos.org/t/semaphore/4733/1 "2007-02-15T14:42:07Z")

</div>

**nobody** wrote on [Thursday, February 15, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/cf7c210e/#d7f1):

Hi,

I am using a Semaphore to ensure that two tasks are not accessing the same SPI port at the same time. But I am not sure if it is okay to it in this way.

TaskOne()&nbsp; // priority 0

{  
for (;😉  
{  
&nbsp;&nbsp;&nbsp; xSemaphoreTake( xSemaphoreSPI0, portMAX\_DELAY&nbsp; );

&nbsp;&nbsp;&nbsp; ReadSPI0();

&nbsp;&nbsp;&nbsp; xSemaphoreGive( xSemaphoreSPI0 );

&nbsp;&nbsp;&nbsp; vTaskDelay(1000);  
}  
}

TaskTwo() // priority 1

{  
&nbsp;&nbsp; xSemaphoreTake( xSemaphoreSPI0, portMAX\_DELAY&nbsp; );

&nbsp;&nbsp;&nbsp; ReadSPI0();

&nbsp;&nbsp;&nbsp; xSemaphoreGive( xSemaphoreSPI0 );

&nbsp;&nbsp;&nbsp; vTaskDelay(2000);  
}

}

What happens if TaskTwo gets ready to run, but the semaphore is obtained by TaskOne, does TaskOne then get time to finish?

Best regards,

Frank Andersen

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 15, 2007, 3:04pm UTC](https://forums.freertos.org/t/semaphore/4733/2 "2007-02-15T15:04:33Z")

</div>

**[embeddedc](https://sourceforge.net/u/embeddedc/)** wrote on [Thursday, February 15, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/cf7c210e/#d7f1/db46):

Presuming that the semphore is only given once successfully taken then your code will be ok.&nbsp; An alternative is to have one task that controls the SPI port, and any other task that wants to send or receive send a message to the SPI controlling task with the info to be sent/received.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 15, 2007, 3:06pm UTC](https://forums.freertos.org/t/semaphore/4733/3 "2007-02-15T15:06:35Z")

</div>

**[embeddedc](https://sourceforge.net/u/embeddedc/)** wrote on [Thursday, February 15, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/cf7c210e/#d7f1/db46/f4dd):

That did not read very well, try again.&nbsp; An alternative is to have one task that controls the SPI port.&nbsp; If another task want to send data on the SPI port it sends the data to the task in charge of the SPI rather than directly to the SPI peripheral itself.&nbsp; The message can be a structure, with for example the message destination, length and data bytes.&nbsp; The message can be send to the controlling task on a queue to ensure serial access is maintained.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 19, 2007, 10:26am UTC](https://forums.freertos.org/t/semaphore/4733/4 "2007-02-19T10:26:25Z")

</div>

**nobody** wrote on [Monday, February 19, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/cf7c210e/#d7f1/7ce7):

Well, it does not work.

Could someone please come with a working example, where to tasks are sharing a resource, controlled by a semaphore?

Best regards,

Frank Andersen

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 12, 2007, 2:27pm UTC](https://forums.freertos.org/t/semaphore/4733/5 "2007-03-12T14:27:24Z")

</div>

**nobody** wrote on [Monday, March 12, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/cf7c210e/#d7f1/09d0):

{  
if ( xSemaphoreTake( xSemaphoreSPI0, portMAX\_DELAY )!= pdFALSE);  
{  
&nbsp;&nbsp; ReadSPI0();

&nbsp;&nbsp; xSemaphoreGive( xSemaphoreSPI0 );  
}  
vTaskDelay(1000);  
}
