application.c 4.0 KB

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