uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
demo.c
Go to the documentation of this file.
1 #include <FreeRTOS.h>
2 #include <task.h>
3 #include <semphr.h>
4 #ifdef BOARD_MBED
5 #include <lpc17xx_gpio.h>
6 #endif
7 #include <BoardConsole.h>
8 #include <osdebug.h>
9 #include <stdio.h>
10 #include <fio.h>
11 #include <romfs.h>
12 #include <semifs.h>
13 #include <malloc_wrapper.h>
14 #ifdef HAS_ETHERNET
15 #include <lwip/inet.h>
16 #include <lwip/tcp.h>
17 #include <lwip/ip_frag.h>
18 #include <lwip/netif.h>
19 #include <lwip/init.h>
20 #include <lwip/stats.h>
21 #include <lwip/tcp_impl.h>
22 #include <lwip/timers.h>
23 #include <netif/etharp.h>
24 #include <netif/interface.h>
25 #include <webserver/httpd.h>
26 #include <echo/echo.h>
27 #include <lwip/dhcp.h>
28 #endif
29 
30 #ifdef BOARD_MBED
31 
32 #define LED1_wire 18
33 #define LED2_wire 20
34 #define LED3_wire 21
35 #define LED4_wire 23
36 
37 static void setupLEDs() {
38  GPIO_SetDir(1, (1 << LED1_wire) | (1 << LED2_wire) | (1 << LED3_wire) | (1 << LED4_wire), 1);
39 }
40 
41 static void litLED(int led, int value) {
42  if ((led > 4) || (led < 1))
43  return;
44 
45  switch (led) {
46  case 1: led = 1 << LED1_wire; break;
47  case 2: led = 1 << LED2_wire; break;
48  case 3: led = 1 << LED3_wire; break;
49  case 4: led = 1 << LED4_wire; break;
50  }
51 
52  if (value) {
53  GPIO_SetValue(1, led);
54  } else {
55  GPIO_ClearValue(1, led);
56  }
57 }
58 
59 static void ledTask(void *p) {
60  int n = 0;
61  while (1) {
62  switch ((n++) & 3) {
63  case 0: litLED(1, 1); litLED(2, 0); litLED(3, 0); litLED(4, 0); break;
64  case 1: litLED(1, 0); litLED(2, 1); litLED(3, 0); litLED(4, 0); break;
65  case 2: litLED(1, 0); litLED(2, 0); litLED(3, 1); litLED(4, 0); break;
66  case 3: litLED(1, 0); litLED(2, 0); litLED(3, 0); litLED(4, 1); break;
67  }
68  vTaskDelay(1000);
69  }
70 }
71 #endif
72 
74 
75 #ifdef SHOW_SIMPLE_TASKS
76 static void simpleTask1(void *p) {
77  while (1) {
79  BoardConsolePuts("Task 1");
80  xSemaphoreGive(handle);
81  vTaskDelay(1234);
82  }
83 }
84 
85 static void simpleTask2(void *p) {
86  while (1) {
88  BoardConsolePuts("Task 2");
89  xSemaphoreGive(handle);
90  vTaskDelay(1357);
91  }
92 }
93 #endif
94 
95 #ifdef USE_BAD_TASK
96 static void badTask(void *x) {
97  vTaskDelay(5000);
98  char * p = (char *) 0x10000000;
99  *p = 42;
100 }
101 #endif
102 
103 static const char msg[] = "Hello world - from fwrite!\r\n";
104 
105 extern uint8_t _binary_test_romfs_bin_start[];
106 
107 #ifdef HAS_ETHERNET
108 struct ip_addr board_ip;
109 static struct netif board_netif;
110 static xSemaphoreHandle lwip_sem;
111 
112 #define USE_DHCP
113 
114 static void net_init() {
115  struct ip_addr gw_ip, netmask;
116 #ifdef USE_DHCP
117  IP4_ADDR(&board_ip, 0, 0, 0, 0);
118  IP4_ADDR(&gw_ip, 0, 0, 0, 0);
119  IP4_ADDR(&netmask, 0, 0, 0, 0);
120 #else
121  inet_aton("192.168.1.2", &board_ip.addr);
122  inet_aton("192.168.1.1", &gw_ip.addr);
123  inet_aton("255.255.255.0", &netmask.addr);
124 #endif
125  lwip_init();
126  vSemaphoreCreateBinary(lwip_sem);
127 
128  if (netif_add(&board_netif, &board_ip, &netmask, &gw_ip, NULL, interface_init, ethernet_input) == NULL) {
129  fprintf(stderr, "net_init: netif_add(lpc17xx_if_init) failed.\r\n");
130  return;
131  }
132  netif_set_default(&board_netif);
133 
134 #ifdef USE_DHCP
135  printf("Starting DHCP query\n");
136  dhcp_start(&board_netif);
137 #else
138  netif_set_up(&board_netif);
139 #endif
140 }
141 
142 // for lwip
143 uint32_t sys_now() { return xTaskGetTickCount() * portTICK_RATE_MS; }
144 
145 static void lwip_task(void * p) {
146  net_init();
147  httpd_init(_binary_test_romfs_bin_start);
148  echo_init();
149 
150  uint32_t currentIP = board_netif.ip_addr.addr;
151  while (1) {
152  xSemaphoreTake(lwip_sem, 10 / portTICK_RATE_MS);
153  interface_check_input(&board_netif);
154  sys_check_timeouts();
155  if (board_netif.ip_addr.addr != currentIP) {
156  currentIP = board_netif.ip_addr.addr;
157  printf("got an IP address: %i.%i.%i.%i\n",
158  (board_netif.ip_addr.addr >> 0) & 0xff,
159  (board_netif.ip_addr.addr >> 8) & 0xff,
160  (board_netif.ip_addr.addr >> 16) & 0xff,
161  (board_netif.ip_addr.addr >> 24) & 0xff);
162  }
163  }
164 }
165 
166 void ENET_IRQHandler() {
167  uint32_t status = LPC_EMAC->IntStatus;
168  long woken = pdFALSE;
169  LPC_EMAC->IntClear = status;
170  xSemaphoreGiveFromISR(lwip_sem, &woken);
171  portEND_SWITCHING_ISR(woken);
172 }
173 #endif
174 
175 int main() {
177  FILE * f1, * f2;
178  char buf[32];
179  int c;
180  register_devfs();
181 #ifdef HAS_SEMIFS
182  register_semifs();
183 #endif
184  register_romfs("romfs", _binary_test_romfs_bin_start);
185  handle = xSemaphoreCreateMutex();
186  printf("Hello world - from stdio!\r\n");
187  fflush(stdout);
188  f1 = fopen("/dev/stdout", "w");
189  fprintf(stderr, "f1 = %p\r\n", f1);
190  fwrite(msg, 1, sizeof(msg), f1);
191  f2 = fopen("/romfs/test.txt", "r");
192  c = fread(buf, 1, 32, f2);
193  fwrite(buf, 1, c, f1);
194 #ifdef HAS_SEMIFS
195  FILE * f3 = fopen("/host/TEST.TXT", "r");
196  c = fread(buf, 1, 32, f3);
197  fwrite(buf, 1, c, f1);
198  fclose(f3);
199 #endif
200  fflush(f1);
201  fclose(f1);
202  fclose(f2);
203 #ifdef BOARD_MBED
204  setupLEDs();
205  litLED(1, 0);
206  litLED(2, 0);
207  litLED(3, 0);
208  litLED(4, 0);
209 #endif
210  printf("Test: %f\n", 12.3456f);
211  BoardConsolePuts("Creating simple tasks.");
212 #ifdef SHOW_SIMPLE_TASKS
213  xTaskCreate(simpleTask1, (signed char *) "st1", configMINIMAL_STACK_SIZE, (void *)NULL, tskIDLE_PRIORITY, NULL);
214  xTaskCreate(simpleTask2, (signed char *) "st2", configMINIMAL_STACK_SIZE, (void *)NULL, tskIDLE_PRIORITY, NULL);
215 #endif
216 #ifdef USE_BAD_TASK
217  xTaskCreate(badTask, (signed char *) "bad", configMINIMAL_STACK_SIZE, (void *)NULL, tskIDLE_PRIORITY, NULL);
218 #endif
219 #ifdef BOARD_MBED
220  xTaskCreate(ledTask, (signed char *) "led", configMINIMAL_STACK_SIZE, (void *)NULL, tskIDLE_PRIORITY, NULL);
221 #endif
222 #ifdef HAS_ETHERNET
223  xTaskCreate(lwip_task, (signed char *) "lwip", 1024, (void *) NULL, tskIDLE_PRIORITY | portPRIVILEGE_BIT, NULL);
224 #endif
225  BoardConsolePuts("Scheduler starting.");
227  BoardConsolePuts("Scheduler exitting.");
228  return 0;
229 }