test_task_priorities.c 2.7 KB

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