application.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-04-16 first version
  9. */
  10. #include <rtthread.h>
  11. #define THREAD_STACK_SIZE 1024
  12. #if 0
  13. struct rt_semaphore sem1, sem2;
  14. static struct rt_thread thread1;
  15. rt_align(4)
  16. static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
  17. static struct rt_thread thread2;
  18. rt_align(4)
  19. static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];
  20. static void thread1_entry(void* parameter)
  21. {
  22. while (1)
  23. {
  24. rt_sem_release(&sem2);
  25. rt_kprintf("thread1..: %s\n", rt_thread_self()->parent.name);
  26. rt_sem_take(&sem1, RT_WAITING_FOREVER);
  27. rt_kprintf("thread1..: %s\n", rt_thread_self()->parent.name);
  28. }
  29. }
  30. static void thread2_entry(void* parameter)
  31. {
  32. while (1)
  33. {
  34. rt_sem_take(&sem2, RT_WAITING_FOREVER);
  35. rt_kprintf("thread2--->: %s\n", rt_thread_self()->parent.name);
  36. rt_sem_release(&sem1);
  37. }
  38. }
  39. /* user application */
  40. int rt_application_init()
  41. {
  42. rt_err_t result;
  43. rt_sem_init(&sem1, "s1", 0, RT_IPC_FLAG_FIFO);
  44. rt_sem_init(&sem2, "s2", 0, RT_IPC_FLAG_FIFO);
  45. result = rt_thread_init(&thread1, "t1", thread1_entry, RT_NULL,
  46. &thread1_stack[0], sizeof(thread1_stack), 10, 10);
  47. if (result == RT_EOK)
  48. rt_thread_startup(&thread1);
  49. result = rt_thread_init(&thread2, "t2", thread2_entry, RT_NULL,
  50. &thread2_stack[0], sizeof(thread2_stack), 18, 10);
  51. if (result == RT_EOK)
  52. rt_thread_startup(&thread2);
  53. return 0;
  54. }
  55. #else
  56. static struct rt_thread thread1;
  57. rt_align(4)
  58. static rt_uint8_t thread1_stack[THREAD_STACK_SIZE];
  59. rt_timer_t ttimer;
  60. static void thread1_entry(void* parameter)
  61. {
  62. rt_uint32_t count = 0;
  63. while (1)
  64. {
  65. rt_kprintf("%s: count = %d\n", rt_thread_self()->parent.name, count ++);
  66. rt_thread_delay(10);
  67. }
  68. }
  69. /* user application */
  70. int rt_application_init()
  71. {
  72. rt_err_t result;
  73. result = rt_thread_init(&thread1, "t1", thread1_entry, RT_NULL,
  74. &thread1_stack[0], sizeof(thread1_stack), 10, 10);
  75. ttimer = &(thread1.thread_timer);
  76. if (result == RT_EOK)
  77. rt_thread_startup(&thread1);
  78. return 0;
  79. }
  80. #endif