Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SemaphoreHandle_t i2c_lock = NULL;
- // A task that creates a semaphore.
- void vATask( void * pvParameters )
- {
- // Create the semaphore to guard a shared resource.
- vSemaphoreCreateBinary( i2c_lock );
- }
- // A task that uses the semaphore.
- void vAnotherTask( void * pvParameters )
- {
- // ... Do other things.
- if( i2c_lock != NULL )
- {
- // See if we can obtain the semaphore. If the semaphore is not available
- // wait 10 ticks to see if it becomes free.
- if( xSemaphoreTake( i2c_lock, ( TickType_t ) 10 ) == pdTRUE )
- {
- // We were able to obtain the semaphore and can now access the
- // shared resource.
- // ...
- // We have finished accessing the shared resource. Release the
- // semaphore.
- xSemaphoreGive( i2c_lock );
- }
- else
- {
- // We could not obtain the semaphore and can therefore not access
- // the shared resource safely.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement