test_queue_isr.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 25
  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. ALIGN(RT_ALIGN_SIZE)
  26. static char thread1_stack[1024];
  27. static struct rt_thread thread1;
  28. static void rt_thread1_entry(void *parameter)
  29. {
  30. BaseType_t xReturn;
  31. uint32_t num = 0;
  32. while (1)
  33. {
  34. xReturn = xQueueReceive(xQueue, &num, portMAX_DELAY);
  35. if (xReturn != pdPASS)
  36. {
  37. rt_kprintf("Task 1 receive from queue failed\n");
  38. }
  39. else
  40. {
  41. rt_kprintf("Task 1 receive data %d from queue\n", num);
  42. }
  43. if (num >= 100)
  44. {
  45. return;
  46. }
  47. }
  48. }
  49. static void timeout(void *parameter)
  50. {
  51. static uint32_t num = 0;
  52. rt_kprintf("timer isr send data %d to queue\n", num);
  53. xQueueSendToBackFromISR(xQueue, &num, NULL);
  54. num += 1;
  55. }
  56. int queue_isr()
  57. {
  58. xQueue = xQueueCreate(QUEUE_LENGTH, ITEM_SIZE);
  59. if (xQueue == NULL)
  60. {
  61. rt_kprintf("create queue failed.\n");
  62. return -1;
  63. }
  64. rt_thread_init(&thread1,
  65. "thread1",
  66. rt_thread1_entry,
  67. RT_NULL,
  68. &thread1_stack[0],
  69. sizeof(thread1_stack),
  70. THREAD_PRIORITY, THREAD_TIMESLICE);
  71. rt_thread_startup(&thread1);
  72. /* Create a hard timer with period of 5 seconds */
  73. timer1 = rt_timer_create("timer1", timeout, RT_NULL, rt_tick_from_millisecond(5000), RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_HARD_TIMER);
  74. if (timer1 != RT_NULL)
  75. {
  76. rt_timer_start(timer1);
  77. }
  78. return 0;
  79. }
  80. MSH_CMD_EXPORT(queue_isr, queue sample);