application.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * File : application.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2014, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2014-07-13 xiaonong port for lpc43xx
  13. */
  14. #include <rtthread.h>
  15. #include <board.h>
  16. #include <rtdevice.h>
  17. #include "drv_led.h"
  18. #ifdef RT_USING_FINSH
  19. #include <finsh.h>
  20. #include <shell.h>
  21. #endif
  22. #ifdef RT_USING_LOGTRACE
  23. #include <log_trace.h>
  24. #endif
  25. #ifdef RT_USING_VBUS
  26. #include <vbus.h>
  27. #endif
  28. /* thread phase init */
  29. void rt_init_thread_entry(void *parameter)
  30. {
  31. #ifdef RT_USING_LOGTRACE
  32. log_trace_init();
  33. log_trace_set_device(RT_CONSOLE_DEVICE_NAME);
  34. #endif
  35. #ifdef RT_USING_FINSH
  36. /* initialize finsh */
  37. finsh_system_init();
  38. finsh_set_device(RT_CONSOLE_DEVICE_NAME);
  39. #endif
  40. #ifdef RT_USING_VBUS
  41. rt_vbus_do_init();
  42. #endif
  43. }
  44. /*the led thread*/
  45. ALIGN(RT_ALIGN_SIZE)
  46. static rt_uint8_t led_stack[1024];
  47. static struct rt_thread led_thread;
  48. static void led_thread_entry(void *parameter)
  49. {
  50. rt_device_t led_dev;
  51. rt_device_t vbus_dev;
  52. rt_err_t err;
  53. rt_led_hw_init();
  54. led_dev = rt_device_find("led");
  55. if (led_dev == RT_NULL)
  56. {
  57. rt_kprintf("can not find the led device\n");
  58. return;
  59. }
  60. vbus_dev = rt_device_find("vecho");
  61. if (vbus_dev == RT_NULL)
  62. {
  63. rt_kprintf("can not find the vbus device\n");
  64. return;
  65. }
  66. err = rt_device_open(vbus_dev, RT_DEVICE_OFLAG_RDWR);
  67. if (err != RT_EOK)
  68. {
  69. rt_kprintf("open vbus failed: %d\n", err);
  70. return;
  71. }
  72. while (1)
  73. {
  74. rt_uint8_t led_value;
  75. int len;
  76. len = rt_device_read(vbus_dev, 0, &led_value, sizeof(led_value));
  77. if (len <= 0)
  78. {
  79. rt_kprintf("vbus read err: %d, %d\n", len, rt_get_errno());
  80. }
  81. led_dev->write(led_dev, 1, &led_value, sizeof(led_value));
  82. }
  83. }
  84. int rt_application_init(void)
  85. {
  86. rt_thread_t tid;
  87. rt_err_t result;
  88. tid = rt_thread_create("init",
  89. rt_init_thread_entry, RT_NULL,
  90. 2048, 3, 20);
  91. if (tid != RT_NULL)
  92. rt_thread_startup(tid);
  93. /* init led thread */
  94. result = rt_thread_init(&led_thread, "led",
  95. led_thread_entry, RT_NULL,
  96. (rt_uint8_t *)&led_stack[0], sizeof(led_stack),
  97. 20, 5);
  98. if (result == RT_EOK)
  99. {
  100. rt_thread_startup(&led_thread);
  101. }
  102. return 0;
  103. }