Advertisement
RobertBerger

FreeRTOS UseInputVal

Apr 27th, 2022
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. xSemaphoreHandle xMutexUseInputVal;
  2.  
  3. ...
  4.  
  5. xMutexUseInputVal = xSemaphoreCreateMutex();
  6. if (xMutexUseInputVal == NULL) {
  7. printf("Error creating xMutexUseInputVal function: %s, line %d\n",
  8. __FUNCTION__, __LINE__);
  9. }
  10.  
  11.  
  12. int GetUseInputVal(void) {
  13. int localUseInputVal;
  14.  
  15. if (xMutexUseInputVal != NULL) {
  16. /* See if we can obtain the semaphore.
  17. * If the semaphore is not available wait 10 ticks to see
  18. * if it becomes free.
  19. */
  20. if (xSemaphoreTake( xMutexUseInputVal, ( portTickType ) 10 ) == pdTRUE) {
  21. /* We were able to obtain the semaphore and can now
  22. * access the shared resource.
  23. */
  24. localUseInputVal = UseInputVal;
  25.  
  26. /* We have finished accessing the shared resource.
  27. * Release the mutex.
  28. */
  29. xSemaphoreGive( xMutexUseInputVal );
  30. return (localUseInputVal);
  31. } else {
  32. /* We could not obtain the semaphore and can therefore
  33. * not access the shared resource safely.
  34. */
  35. printf("Error with xMutexUseInputVal function: %s, line %d\n",
  36. __FUNCTION__, __LINE__);
  37. /* we skip further error handling */
  38. }
  39. }
  40. } /* GetUseInputVal */
  41.  
  42. void SetUseInputVal(int val) {
  43. if (xMutexUseInputVal != NULL) {
  44. /* See if we can obtain the semaphore.
  45. * If the semaphore is not available wait 10 ticks to see
  46. * if it becomes free.
  47. */
  48. if (xSemaphoreTake( xMutexUseInputVal, ( portTickType ) 10 ) == pdTRUE) {
  49. /* We were able to obtain the semaphore and can now
  50. * access the shared resource.
  51. */
  52. UseInputVal = val;
  53.  
  54. /* We have finished accessing the shared resource.
  55. * Release the mutex.
  56. */
  57. xSemaphoreGive( xMutexUseInputVal );
  58. } else {
  59. /* We could not obtain the semaphore and can therefore
  60. * not access the shared resource safely.
  61. */
  62. printf("Error with xMutexUseInputVal function: %s, line %d\n",
  63. __FUNCTION__, __LINE__);
  64. /* we skip further error handling */
  65. }
  66. }
  67. } /* SetUseInputVal */
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement