test_task_priorities.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Unit tests for FreeRTOS task priority get/set
  3. */
  4. #include <esp_types.h>
  5. #include <stdio.h>
  6. #include <strings.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "unity.h"
  10. #include "test_utils.h"
  11. static void counter_task(void *param)
  12. {
  13. volatile uint32_t *counter = (volatile uint32_t *)param;
  14. while (1) {
  15. (*counter)++;
  16. }
  17. }
  18. TEST_CASE("Get/Set Priorities", "[freertos]")
  19. {
  20. /* Two tasks per processor */
  21. TaskHandle_t tasks[portNUM_PROCESSORS][2] = { 0 };
  22. unsigned volatile counters[portNUM_PROCESSORS][2] = { 0 };
  23. TEST_ASSERT_EQUAL(UNITY_FREERTOS_PRIORITY, uxTaskPriorityGet(NULL));
  24. /* create a matrix of counter tasks on each core */
  25. for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
  26. for (int task = 0; task < 2; task++) {
  27. xTaskCreatePinnedToCore(counter_task, "count", 2048, (void *)&(counters[cpu][task]), UNITY_FREERTOS_PRIORITY - task, &(tasks[cpu][task]), cpu);
  28. }
  29. }
  30. /* check they were created with the expected priorities */
  31. for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
  32. for (int task = 0; task < 2; task++) {
  33. TEST_ASSERT_EQUAL(UNITY_FREERTOS_PRIORITY - task, uxTaskPriorityGet(tasks[cpu][task]));
  34. }
  35. }
  36. vTaskDelay(10);
  37. /* at this point, only the higher priority tasks (first index) should be counting */
  38. for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
  39. TEST_ASSERT_NOT_EQUAL(0, counters[cpu][0]);
  40. TEST_ASSERT_EQUAL(0, counters[cpu][1]);
  41. }
  42. /* swap priorities! */
  43. for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
  44. vTaskPrioritySet(tasks[cpu][0], UNITY_FREERTOS_PRIORITY - 1);
  45. vTaskPrioritySet(tasks[cpu][1], UNITY_FREERTOS_PRIORITY);
  46. }
  47. /* check priorities have swapped... */
  48. for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
  49. TEST_ASSERT_EQUAL(UNITY_FREERTOS_PRIORITY -1, uxTaskPriorityGet(tasks[cpu][0]));
  50. TEST_ASSERT_EQUAL(UNITY_FREERTOS_PRIORITY, uxTaskPriorityGet(tasks[cpu][1]));
  51. }
  52. /* check the tasks which are counting have also swapped now... */
  53. for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
  54. unsigned old_counters[2];
  55. old_counters[0] = counters[cpu][0];
  56. old_counters[1] = counters[cpu][1];
  57. vTaskDelay(10);
  58. TEST_ASSERT_EQUAL(old_counters[0], counters[cpu][0]);
  59. TEST_ASSERT_NOT_EQUAL(old_counters[1], counters[cpu][1]);
  60. }
  61. /* clean up */
  62. for (int cpu = 0; cpu < portNUM_PROCESSORS; cpu++) {
  63. for (int task = 0; task < 2; task++) {
  64. vTaskDelete(tasks[cpu][task]);
  65. }
  66. }
  67. }