application.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_init.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. /* thread phase init */
  33. void rt_init_thread_entry(void *parameter)
  34. {
  35. /* Filesystem Initialization */
  36. #ifdef RT_USING_DFS
  37. {
  38. /* init the device filesystem */
  39. dfs_init();
  40. /* init the elm FAT filesystam*/
  41. elm_init();
  42. /* mount sd card fat partition 1 as root directory */
  43. if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
  44. rt_kprintf("File System initialized!\n");
  45. else
  46. rt_kprintf("File System init failed!\n");
  47. }
  48. #endif
  49. /* LwIP Initialization */
  50. #ifdef RT_USING_LWIP
  51. {
  52. extern void lwip_sys_init(void);
  53. extern void lpc17xx_emac_hw_init(void);
  54. eth_system_device_init();
  55. /* register ethernetif device */
  56. lpc17xx_emac_hw_init();
  57. /* init all device */
  58. rt_device_init_all();
  59. /* init lwip system */
  60. lwip_sys_init();
  61. rt_kprintf("TCP/IP initialized!\n");
  62. }
  63. #endif
  64. #ifdef RT_USING_RTGUI
  65. {
  66. extern void rtgui_system_server_init(void);
  67. extern void application_init(void);
  68. rt_device_t lcd;
  69. /* init lcd */
  70. rt_hw_lcd_init();
  71. /* re-init device driver */
  72. rt_device_init_all();
  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. }
  87. // init led
  88. #define rt_hw_led_init() LPC_GPIO2->DIR |= 1<<25;
  89. // trun on led n
  90. #define rt_hw_led_on(n) LPC_GPIO2->CLR |= 1<<25;
  91. // trun off led n
  92. #define rt_hw_led_off(n) LPC_GPIO2->SET |= 1<<25;
  93. ALIGN(RT_ALIGN_SIZE)
  94. static char thread_led_stack[1024];
  95. struct rt_thread thread_led;
  96. static void rt_thread_entry_led(void* parameter)
  97. {
  98. unsigned int count=0;
  99. rt_hw_led_init();
  100. while (1)
  101. {
  102. /* led on */
  103. #ifndef RT_USING_FINSH
  104. rt_kprintf("led on,count : %d\r\n",count);
  105. #endif
  106. count++;
  107. rt_hw_led_on(1);
  108. /* sleep 0.5 second and switch to other thread */
  109. rt_thread_delay(RT_TICK_PER_SECOND/2);
  110. /* led off */
  111. #ifndef RT_USING_FINSH
  112. rt_kprintf("led off\r\n");
  113. #endif
  114. rt_hw_led_off(1);
  115. rt_thread_delay(RT_TICK_PER_SECOND/2);
  116. }
  117. }
  118. int rt_application_init(void)
  119. {
  120. rt_thread_t tid;
  121. rt_thread_init(&thread_led,
  122. "led",
  123. rt_thread_entry_led,
  124. RT_NULL,
  125. &thread_led_stack[0],
  126. sizeof(thread_led_stack),11,5);
  127. rt_thread_startup(&thread_led);
  128. tid = rt_thread_create("init",
  129. rt_init_thread_entry, RT_NULL,
  130. 2048, RT_THREAD_PRIORITY_MAX/3, 20);
  131. if (tid != RT_NULL) rt_thread_startup(tid);
  132. return 0;
  133. }
  134. #if defined(RT_USING_RTGUI) && defined(RT_USING_FINSH)
  135. #include <rtgui/rtgui_server.h>
  136. #include <rtgui/event.h>
  137. #include <rtgui/kbddef.h>
  138. #include <finsh.h>
  139. void key(rt_uint32_t key)
  140. {
  141. struct rtgui_event_kbd ekbd;
  142. RTGUI_EVENT_KBD_INIT(&ekbd);
  143. ekbd.mod = RTGUI_KMOD_NONE;
  144. ekbd.unicode = 0;
  145. ekbd.key = key;
  146. ekbd.type = RTGUI_KEYDOWN;
  147. rtgui_server_post_event((struct rtgui_event*)&ekbd, sizeof(ekbd));
  148. rt_thread_delay(2);
  149. ekbd.type = RTGUI_KEYUP;
  150. rtgui_server_post_event((struct rtgui_event*)&ekbd, sizeof(ekbd));
  151. }
  152. FINSH_FUNCTION_EXPORT(key, send a key to gui server);
  153. #endif