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