Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. /*
  2. * Name:
  3. * Synchronizing tasks with a queue (sending a message)
  4. *
  5. * Description:
  6. * - a FreeRTOS example application for the Silicon Labs
  7. * EFM32 Giant Gecko Starter Kit (STK3700A)
  8. * - There are two tasks. One is sending a message via a queue and the other
  9. * is waiting for this message.
  10. * - The sender task is supposed to send a message (a value incremented by
  11. * one) once in every second
  12. *
  13. * Created by:
  14. * NASZALY Gabor <naszaly@mit.bme.hu>
  15. *
  16. * Last modified on:
  17. * 2016-03-23
  18. */
  19.  
  20. /* --------------------------------------------------------------------------
  21. INCLUDES
  22. -------------------------------------------------------------------------- */
  23.  
  24. // Application specific includes
  25. #include <stdio.h>
  26. #include <retargetserial.h>
  27.  
  28. // FreeRTOS includes
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. // TODO: include header for queue API
  32. #include "queue.h"
  33.  
  34. /* --------------------------------------------------------------------------
  35. DEFINES
  36. -------------------------------------------------------------------------- */
  37.  
  38. #define mainTASK_MESSAGE_SENDER_PRIORITY ( tskIDLE_PRIORITY + 2 )
  39. #define mainTASK_MESSAGE_SENDER_STACK_SIZE configMINIMAL_STACK_SIZE
  40.  
  41. #define mainTASK_MESSAGE_RECEIVER_PRIORITY ( tskIDLE_PRIORITY + 1 )
  42. #define mainTASK_MESSAGE_RECEIVER_STACK_SIZE configMINIMAL_STACK_SIZE
  43.  
  44. /* --------------------------------------------------------------------------
  45. VARIABLES
  46. -------------------------------------------------------------------------- */
  47.  
  48. // A variable storing the number of button presses
  49. uint32_t message = 0;
  50.  
  51. /*
  52. * TODO: define a variable for the queue used to convey the message
  53. * Use type QueueHandle_t
  54. */
  55.  
  56. QueueHandle_t queue;
  57.  
  58. /* --------------------------------------------------------------------------
  59. FUNCTION DECLARATIONS
  60. -------------------------------------------------------------------------- */
  61.  
  62. static void prvTaskMessageSender(void *pvParam);
  63. static void prvTaskMessageReceiver(void *pvParam);
  64.  
  65. /* --------------------------------------------------------------------------
  66. MAIN
  67. -------------------------------------------------------------------------- */
  68.  
  69. int main(void) {
  70. // Initialize starter kit
  71. RETARGET_SerialInit();
  72. RETARGET_SerialCrLf(1);
  73.  
  74. // Initialize FreeRTOS
  75.  
  76. /*
  77. * TODO: create the queue used to convey the messages
  78. * Use function xQueueCreate().
  79. */
  80.  
  81. queue = xQueueCreate(1, sizeof(uint32_t));
  82.  
  83. // Create the message sender task
  84. xTaskCreate(
  85. prvTaskMessageSender,
  86. "Message Sender",
  87. mainTASK_MESSAGE_SENDER_STACK_SIZE,
  88. NULL,
  89. mainTASK_MESSAGE_SENDER_PRIORITY,
  90. NULL);
  91.  
  92. // Create the message receiver task
  93. xTaskCreate(
  94. prvTaskMessageReceiver,
  95. "Message Receiver",
  96. mainTASK_MESSAGE_RECEIVER_STACK_SIZE,
  97. NULL,
  98. mainTASK_MESSAGE_RECEIVER_PRIORITY,
  99. NULL);
  100.  
  101. // Start the scheduler
  102. vTaskStartScheduler();
  103.  
  104. // Prevent compiler warning
  105. return 0;
  106. }
  107.  
  108. /* --------------------------------------------------------------------------
  109. FUNCTION DEFINITIONS
  110. -------------------------------------------------------------------------- */
  111.  
  112. /*
  113. * TODO: This task sends the messages (a number incremented by one each time).
  114. */
  115. static void prvTaskMessageSender(void *pvParam) {
  116.  
  117. while (1) {
  118.  
  119. vTaskDelay(configTICK_RATE_HZ);
  120. /*
  121. * TODO: send the message via the queue
  122. * Use function xQueueSend()
  123. */
  124.  
  125. xQueueSend(queue, (void*) &message, (TickType_t) 100);
  126.  
  127. message = message + 1;
  128.  
  129.  
  130. }
  131. }
  132.  
  133. /*
  134. * This task receives the messages
  135. */
  136. static void prvTaskMessageReceiver(void *pvParam) {
  137. uint32_t local_copy_of_message;
  138.  
  139. while (1) {
  140. /*
  141. * TODO: receive the message from queue
  142. * Use function xQueueReceive()
  143. */
  144.  
  145. xQueueReceive(queue, &local_copy_of_message, (TickType_t) 1000);
  146.  
  147. printf("kapott uzenet: %lu\n", local_copy_of_message);
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement