test_queue_isr.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-24 yangjie the first version
  9. */
  10. /*
  11. * Demo: queue
  12. * This demo creates a periodic hard timer which sends data to a queue when timeout
  13. * A thread blocks to receive from the queue
  14. *
  15. */
  16. #include <rtthread.h>
  17. #include <FreeRTOS.h>
  18. #include <queue.h>
  19. #define THREAD_PRIORITY (FINSH_THREAD_PRIORITY + 1)
  20. #define THREAD_TIMESLICE 5
  21. #define QUEUE_LENGTH 1
  22. #define ITEM_SIZE sizeof( uint32_t )
  23. static QueueHandle_t xQueue = NULL;
  24. static rt_timer_t timer1;
  25. #ifdef rt_align
  26. rt_align(RT_ALIGN_SIZE)
  27. #else
  28. ALIGN(RT_ALIGN_SIZE)
  29. #endif
  30. static char thread1_stack[1024];
  31. static struct rt_thread thread1;
  32. static void rt_thread1_entry(void *parameter)
  33. {
  34. BaseType_t xReturn;
  35. uint32_t num = 0;
  36. while (1)
  37. {
  38. xReturn = xQueueReceive(xQueue, &num, portMAX_DELAY);
  39. if (xReturn != pdPASS)
  40. {
  41. rt_kprintf("Task 1 receive from queue failed\n");
  42. }
  43. else
  44. {
  45. rt_kprintf("Task 1 receive data %d from queue\n", num);
  46. }
  47. if (num >= 100)
  48. {
  49. return;
  50. }
  51. }
  52. }
  53. static void timeout(void *parameter)
  54. {
  55. static uint32_t num = 0;
  56. rt_kprintf("timer isr send data %d to queue\n", num);
  57. xQueueSendToBackFromISR(xQueue, &num, NULL);
  58. num += 1;
  59. }
  60. int queue_isr()
  61. {
  62. xQueue = xQueueCreate(QUEUE_LENGTH, ITEM_SIZE);
  63. if (xQueue == NULL)
  64. {
  65. rt_kprintf("create queue failed.\n");
  66. return -1;
  67. }
  68. rt_thread_init(&thread1,
  69. "thread1",
  70. rt_thread1_entry,
  71. RT_NULL,
  72. &thread1_stack[0],
  73. sizeof(thread1_stack),
  74. THREAD_PRIORITY, THREAD_TIMESLICE);
  75. rt_thread_startup(&thread1);
  76. /* Create a hard timer with period of 5 seconds */
  77. timer1 = rt_timer_create("timer1", timeout, RT_NULL, rt_tick_from_millisecond(5000), RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER);
  78. if (timer1 != RT_NULL)
  79. {
  80. rt_timer_start(timer1);
  81. }
  82. return 0;
  83. }
  84. MSH_CMD_EXPORT(queue_isr, queue sample);