test_task.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Demo: task
  3. *
  4. * This demo creates two tasks. Task 1 is created dynamically and task 2 is created statically.
  5. * Task 2 increments a number than delays for 1 second.
  6. * After task 2 runs for 10 iterations, task 1 deletes task 2, then deletes itself.
  7. *
  8. */
  9. #include <FreeRTOS.h>
  10. #include <task.h>
  11. #define TASK_PRIORITY (FINSH_THREAD_PRIORITY + 1)
  12. static TaskHandle_t TaskHandle1 = NULL;
  13. static TaskHandle_t TaskHandle2 = NULL;
  14. static StaticTask_t xTaskBuffer;
  15. StackType_t xStack[configMINIMAL_STACK_SIZE * 2];
  16. static BaseType_t num = 0;
  17. static void vTask1Code(void * pvParameters)
  18. {
  19. while (1)
  20. {
  21. vTaskDelay(pdMS_TO_TICKS(1000));
  22. rt_kprintf("Task 1 running\n");
  23. if (num > 10)
  24. {
  25. rt_kprintf("Delete task 2\n");
  26. vTaskDelete(TaskHandle2);
  27. rt_kprintf("Delete task 1\n");
  28. vTaskDelete(NULL);
  29. rt_kprintf("Should not reach here\n");
  30. }
  31. }
  32. }
  33. static void vTask2Code(void * pvParameters)
  34. {
  35. while (1)
  36. {
  37. vTaskDelay(pdMS_TO_TICKS(1000));
  38. num += 1;
  39. rt_kprintf("Task 2 running, num = %d\n",num);
  40. }
  41. }
  42. int task_sample(void)
  43. {
  44. xTaskCreate(vTask1Code, "Task1", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, &TaskHandle1);
  45. if (TaskHandle1 == NULL)
  46. {
  47. rt_kprintf("Create task 1 failed\n");
  48. return -1;
  49. }
  50. TaskHandle2 = xTaskCreateStatic(vTask2Code, "Task2", configMINIMAL_STACK_SIZE * 2, NULL, TASK_PRIORITY, xStack, &xTaskBuffer);
  51. if (TaskHandle2 == NULL)
  52. {
  53. rt_kprintf("Create task 2 failed\n");
  54. return -1;
  55. }
  56. return 0;
  57. }
  58. MSH_CMD_EXPORT(task_sample, task sample);