Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. SemaphoreHandle_t i2c_lock = NULL;
  2.  
  3. // A task that creates a semaphore.
  4. void vATask( void * pvParameters )
  5. {
  6. // Create the semaphore to guard a shared resource.
  7. vSemaphoreCreateBinary( i2c_lock );
  8. }
  9.  
  10. // A task that uses the semaphore.
  11. void vAnotherTask( void * pvParameters )
  12. {
  13. // ... Do other things.
  14.  
  15. if( i2c_lock != NULL )
  16. {
  17. // See if we can obtain the semaphore. If the semaphore is not available
  18. // wait 10 ticks to see if it becomes free.
  19. if( xSemaphoreTake( i2c_lock, ( TickType_t ) 10 ) == pdTRUE )
  20. {
  21. // We were able to obtain the semaphore and can now access the
  22. // shared resource.
  23.  
  24. // ...
  25.  
  26. // We have finished accessing the shared resource. Release the
  27. // semaphore.
  28. xSemaphoreGive( i2c_lock );
  29. }
  30. else
  31. {
  32. // We could not obtain the semaphore and can therefore not access
  33. // the shared resource safely.
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement