application.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * 2010-05-02 Aozima add led function
  9. */
  10. #include <rtthread.h>
  11. #include <board.h>
  12. #ifdef RT_USING_DFS
  13. /* dfs init */
  14. #include <dfs.h>
  15. /* dfs filesystem:ELM FatFs filesystem init */
  16. #include <dfs_elm.h>
  17. /* dfs Filesystem APIs */
  18. #include <dfs_fs.h>
  19. #endif
  20. #ifdef RT_USING_LWIP
  21. #include <lwip/sys.h>
  22. #include <lwip/api.h>
  23. #include <netif/ethernetif.h>
  24. #endif
  25. #ifdef RT_USING_RTGUI
  26. #include <rtgui/driver.h>
  27. #endif
  28. #ifdef RT_USING_FINSH
  29. #include <shell.h>
  30. #include <finsh.h>
  31. #endif
  32. static int app_init(void);
  33. int main(void)
  34. {
  35. rt_kprintf("Hello RT-Thread!\n");
  36. app_init();
  37. while(1)
  38. {
  39. rt_thread_mdelay(1000);
  40. }
  41. return 0;
  42. }
  43. /* thread phase init */
  44. void rt_init_thread_entry(void *parameter)
  45. {
  46. /* Filesystem Initialization */
  47. #ifdef RT_USING_DFS
  48. {
  49. /* init the device filesystem */
  50. dfs_init();
  51. /* init the elm FAT filesystam*/
  52. elm_init();
  53. /* mount sd card fat partition 1 as root directory */
  54. if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
  55. rt_kprintf("File System initialized!\n");
  56. else
  57. rt_kprintf("File System init failed!\n");
  58. }
  59. #endif
  60. /* LwIP Initialization */
  61. #ifdef RT_USING_LWIP
  62. {
  63. extern void lwip_sys_init(void);
  64. extern void lpc17xx_emac_hw_init(void);
  65. eth_system_device_init();
  66. /* register ethernetif device */
  67. lpc17xx_emac_hw_init();
  68. /* init lwip system */
  69. lwip_sys_init();
  70. rt_kprintf("TCP/IP initialized!\n");
  71. }
  72. #endif
  73. #ifdef RT_USING_RTGUI
  74. {
  75. extern void rtgui_system_server_init(void);
  76. extern void application_init(void);
  77. rt_device_t lcd;
  78. /* init lcd */
  79. rt_hw_lcd_init();
  80. /* find lcd device */
  81. lcd = rt_device_find("lcd");
  82. if (lcd != RT_NULL)
  83. {
  84. /* set lcd device as rtgui graphic driver */
  85. rtgui_graphic_set_device(lcd);
  86. /* init rtgui system server */
  87. rtgui_system_server_init();
  88. /* startup rtgui in demo of RT-Thread/GUI examples */
  89. application_init();
  90. }
  91. }
  92. #endif
  93. #ifdef RT_USING_FINSH
  94. /* initialize finsh */
  95. finsh_system_init();
  96. #endif
  97. }
  98. // init led
  99. #define rt_hw_led_init() LPC_GPIO2->DIR |= 1<<25;
  100. // trun on led n
  101. #define rt_hw_led_on(n) LPC_GPIO2->CLR |= 1<<25;
  102. // trun off led n
  103. #define rt_hw_led_off(n) LPC_GPIO2->SET |= 1<<25;
  104. rt_align(RT_ALIGN_SIZE)
  105. static char thread_led_stack[1024];
  106. struct rt_thread thread_led;
  107. static void rt_thread_entry_led(void* parameter)
  108. {
  109. unsigned int count=0;
  110. rt_hw_led_init();
  111. while (1)
  112. {
  113. /* led on */
  114. #ifndef RT_USING_FINSH
  115. rt_kprintf("led on,count : %d\r\n",count);
  116. #endif
  117. count++;
  118. rt_hw_led_on(1);
  119. /* sleep 0.5 second and switch to other thread */
  120. rt_thread_delay(RT_TICK_PER_SECOND/2);
  121. /* led off */
  122. #ifndef RT_USING_FINSH
  123. rt_kprintf("led off\r\n");
  124. #endif
  125. rt_hw_led_off(1);
  126. rt_thread_delay(RT_TICK_PER_SECOND/2);
  127. }
  128. }
  129. static int app_init(void)
  130. {
  131. rt_thread_t tid;
  132. rt_thread_init(&thread_led,
  133. "led",
  134. rt_thread_entry_led,
  135. RT_NULL,
  136. &thread_led_stack[0],
  137. sizeof(thread_led_stack),11,5);
  138. rt_thread_startup(&thread_led);
  139. tid = rt_thread_create("init",
  140. rt_init_thread_entry, RT_NULL,
  141. 2048, RT_THREAD_PRIORITY_MAX/3, 20);
  142. if (tid != RT_NULL) rt_thread_startup(tid);
  143. return 0;
  144. }
  145. #if defined(RT_USING_RTGUI) && defined(RT_USING_FINSH)
  146. #include <rtgui/rtgui_server.h>
  147. #include <rtgui/event.h>
  148. #include <rtgui/kbddef.h>
  149. #include <finsh.h>
  150. void key(rt_uint32_t key)
  151. {
  152. struct rtgui_event_kbd ekbd;
  153. RTGUI_EVENT_KBD_INIT(&ekbd);
  154. ekbd.mod = RTGUI_KMOD_NONE;
  155. ekbd.unicode = 0;
  156. ekbd.key = key;
  157. ekbd.type = RTGUI_KEYDOWN;
  158. rtgui_server_post_event((struct rtgui_event*)&ekbd, sizeof(ekbd));
  159. rt_thread_delay(2);
  160. ekbd.type = RTGUI_KEYUP;
  161. rtgui_server_post_event((struct rtgui_event*)&ekbd, sizeof(ekbd));
  162. }
  163. FINSH_FUNCTION_EXPORT(key, send a key to gui server);
  164. #endif