![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test program for BoB3 breakout board. Refer to my Notebook page for further details and contact info.
main.cpp@0:788d8ebe64b5, 2010-04-06 (annotated)
- Committer:
- paulg
- Date:
- Tue Apr 06 17:08:41 2010 +0000
- Revision:
- 0:788d8ebe64b5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
paulg | 0:788d8ebe64b5 | 1 | /* Test program for BoB3 breakout board |
paulg | 0:788d8ebe64b5 | 2 | * Paul Griffith |
paulg | 0:788d8ebe64b5 | 3 | * Last edit: 29 Jan 2010 |
paulg | 0:788d8ebe64b5 | 4 | */ |
paulg | 0:788d8ebe64b5 | 5 | |
paulg | 0:788d8ebe64b5 | 6 | #include "mbed.h" |
paulg | 0:788d8ebe64b5 | 7 | #include "SDFileSystem.h" |
paulg | 0:788d8ebe64b5 | 8 | #include "lwip/opt.h" |
paulg | 0:788d8ebe64b5 | 9 | #include "lwip/stats.h" |
paulg | 0:788d8ebe64b5 | 10 | #include "lwip/sys.h" |
paulg | 0:788d8ebe64b5 | 11 | #include "lwip/pbuf.h" |
paulg | 0:788d8ebe64b5 | 12 | #include "lwip/udp.h" |
paulg | 0:788d8ebe64b5 | 13 | #include "lwip/tcp.h" |
paulg | 0:788d8ebe64b5 | 14 | #include "lwip/dns.h" |
paulg | 0:788d8ebe64b5 | 15 | #include "lwip/dhcp.h" |
paulg | 0:788d8ebe64b5 | 16 | #include "lwip/init.h" |
paulg | 0:788d8ebe64b5 | 17 | #include "lwip/netif.h" |
paulg | 0:788d8ebe64b5 | 18 | #include "netif/etharp.h" |
paulg | 0:788d8ebe64b5 | 19 | #include "netif/loopif.h" |
paulg | 0:788d8ebe64b5 | 20 | #include "device.h" |
paulg | 0:788d8ebe64b5 | 21 | |
paulg | 0:788d8ebe64b5 | 22 | |
paulg | 0:788d8ebe64b5 | 23 | void printhelps(void); |
paulg | 0:788d8ebe64b5 | 24 | void listdir(void); |
paulg | 0:788d8ebe64b5 | 25 | void printfile(char *); |
paulg | 0:788d8ebe64b5 | 26 | void bad_args(void); |
paulg | 0:788d8ebe64b5 | 27 | int exist(char *); |
paulg | 0:788d8ebe64b5 | 28 | int eth_test(void); |
paulg | 0:788d8ebe64b5 | 29 | |
paulg | 0:788d8ebe64b5 | 30 | |
paulg | 0:788d8ebe64b5 | 31 | Serial pc(USBTX, USBRX); |
paulg | 0:788d8ebe64b5 | 32 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
paulg | 0:788d8ebe64b5 | 33 | DigitalOut led1(p25), led2(p26), led3(p27), led4(p28), led5(p29), led6(p30); |
paulg | 0:788d8ebe64b5 | 34 | |
paulg | 0:788d8ebe64b5 | 35 | int main() { |
paulg | 0:788d8ebe64b5 | 36 | char buf[120], *cp; |
paulg | 0:788d8ebe64b5 | 37 | char arg1[50], arg2[50], arg3[50], filename[20]; |
paulg | 0:788d8ebe64b5 | 38 | int argc, cnt = 0, i, j, k, l; |
paulg | 0:788d8ebe64b5 | 39 | FILE *fp; |
paulg | 0:788d8ebe64b5 | 40 | time_t seconds; |
paulg | 0:788d8ebe64b5 | 41 | struct tm t; |
paulg | 0:788d8ebe64b5 | 42 | |
paulg | 0:788d8ebe64b5 | 43 | |
paulg | 0:788d8ebe64b5 | 44 | printf("BoB3 test program\n"); |
paulg | 0:788d8ebe64b5 | 45 | printf("Type ? for help\n"); |
paulg | 0:788d8ebe64b5 | 46 | |
paulg | 0:788d8ebe64b5 | 47 | while (1) { |
paulg | 0:788d8ebe64b5 | 48 | printf("> "); |
paulg | 0:788d8ebe64b5 | 49 | cp = buf; |
paulg | 0:788d8ebe64b5 | 50 | while ( (*cp++ = putchar(getchar())) != '\r') ; //get a line of input |
paulg | 0:788d8ebe64b5 | 51 | *cp = '\0'; //terminate buffer |
paulg | 0:788d8ebe64b5 | 52 | printf("\n"); |
paulg | 0:788d8ebe64b5 | 53 | argc = sscanf(buf, "%s%s%s", arg1, arg2, arg3); //extract command and arguments |
paulg | 0:788d8ebe64b5 | 54 | if (argc < 1) |
paulg | 0:788d8ebe64b5 | 55 | continue; |
paulg | 0:788d8ebe64b5 | 56 | switch (arg1[0]) { |
paulg | 0:788d8ebe64b5 | 57 | |
paulg | 0:788d8ebe64b5 | 58 | case 'c': //create file on uSD card |
paulg | 0:788d8ebe64b5 | 59 | if (argc != 2) { |
paulg | 0:788d8ebe64b5 | 60 | bad_args(); |
paulg | 0:788d8ebe64b5 | 61 | break; |
paulg | 0:788d8ebe64b5 | 62 | } |
paulg | 0:788d8ebe64b5 | 63 | sprintf(filename, "/sd/%s", arg2); |
paulg | 0:788d8ebe64b5 | 64 | if (exist(filename) == 1) { |
paulg | 0:788d8ebe64b5 | 65 | printf("File already exists\n"); |
paulg | 0:788d8ebe64b5 | 66 | break; |
paulg | 0:788d8ebe64b5 | 67 | } |
paulg | 0:788d8ebe64b5 | 68 | fp = fopen(filename, "w"); |
paulg | 0:788d8ebe64b5 | 69 | if (fp == 0) { |
paulg | 0:788d8ebe64b5 | 70 | printf("Unable to create file\n"); |
paulg | 0:788d8ebe64b5 | 71 | } else { |
paulg | 0:788d8ebe64b5 | 72 | printf("File created\n"); |
paulg | 0:788d8ebe64b5 | 73 | seconds = time(NULL); |
paulg | 0:788d8ebe64b5 | 74 | fprintf(fp, "Hello world, the time is %s\n", ctime(&seconds)); |
paulg | 0:788d8ebe64b5 | 75 | fclose(fp); |
paulg | 0:788d8ebe64b5 | 76 | } |
paulg | 0:788d8ebe64b5 | 77 | break; |
paulg | 0:788d8ebe64b5 | 78 | |
paulg | 0:788d8ebe64b5 | 79 | case 'd': //delete file on uSD card |
paulg | 0:788d8ebe64b5 | 80 | if (argc != 2) { |
paulg | 0:788d8ebe64b5 | 81 | bad_args(); |
paulg | 0:788d8ebe64b5 | 82 | break; |
paulg | 0:788d8ebe64b5 | 83 | } |
paulg | 0:788d8ebe64b5 | 84 | sprintf(filename, "/sd/%s", arg2); |
paulg | 0:788d8ebe64b5 | 85 | if (exist(filename) == 0) |
paulg | 0:788d8ebe64b5 | 86 | printf("File does not exist\n"); |
paulg | 0:788d8ebe64b5 | 87 | else { |
paulg | 0:788d8ebe64b5 | 88 | remove(filename); |
paulg | 0:788d8ebe64b5 | 89 | printf("File deleted\n"); |
paulg | 0:788d8ebe64b5 | 90 | } |
paulg | 0:788d8ebe64b5 | 91 | break; |
paulg | 0:788d8ebe64b5 | 92 | |
paulg | 0:788d8ebe64b5 | 93 | case 'e': //test Ethernet status LEDS |
paulg | 0:788d8ebe64b5 | 94 | if (argc == 1) |
paulg | 0:788d8ebe64b5 | 95 | i = 0; |
paulg | 0:788d8ebe64b5 | 96 | else |
paulg | 0:788d8ebe64b5 | 97 | sscanf(arg2, "%d", &i); |
paulg | 0:788d8ebe64b5 | 98 | switch (i) { |
paulg | 0:788d8ebe64b5 | 99 | case 0: |
paulg | 0:788d8ebe64b5 | 100 | led1 = 1; |
paulg | 0:788d8ebe64b5 | 101 | wait(1.0); |
paulg | 0:788d8ebe64b5 | 102 | led1 = 0; |
paulg | 0:788d8ebe64b5 | 103 | wait(1.0); |
paulg | 0:788d8ebe64b5 | 104 | led2 = 1; |
paulg | 0:788d8ebe64b5 | 105 | wait(1.0); |
paulg | 0:788d8ebe64b5 | 106 | led2 = 0; |
paulg | 0:788d8ebe64b5 | 107 | break; |
paulg | 0:788d8ebe64b5 | 108 | |
paulg | 0:788d8ebe64b5 | 109 | case 1: |
paulg | 0:788d8ebe64b5 | 110 | led3 = 1; |
paulg | 0:788d8ebe64b5 | 111 | wait(1.0); |
paulg | 0:788d8ebe64b5 | 112 | led3 = 0; |
paulg | 0:788d8ebe64b5 | 113 | wait(1.0); |
paulg | 0:788d8ebe64b5 | 114 | led4 = 1; |
paulg | 0:788d8ebe64b5 | 115 | wait(1.0); |
paulg | 0:788d8ebe64b5 | 116 | led4 = 0; |
paulg | 0:788d8ebe64b5 | 117 | break; |
paulg | 0:788d8ebe64b5 | 118 | |
paulg | 0:788d8ebe64b5 | 119 | case 2: |
paulg | 0:788d8ebe64b5 | 120 | led5 = 1; |
paulg | 0:788d8ebe64b5 | 121 | wait(1.0); |
paulg | 0:788d8ebe64b5 | 122 | led5 = 0; |
paulg | 0:788d8ebe64b5 | 123 | wait(1.0); |
paulg | 0:788d8ebe64b5 | 124 | led6 = 1; |
paulg | 0:788d8ebe64b5 | 125 | wait(1.0); |
paulg | 0:788d8ebe64b5 | 126 | led6 = 0; |
paulg | 0:788d8ebe64b5 | 127 | break; |
paulg | 0:788d8ebe64b5 | 128 | } |
paulg | 0:788d8ebe64b5 | 129 | break; |
paulg | 0:788d8ebe64b5 | 130 | |
paulg | 0:788d8ebe64b5 | 131 | case 'E': //run Ethernet test |
paulg | 0:788d8ebe64b5 | 132 | printf("Reset mbed to exit Ethernet test\n"); |
paulg | 0:788d8ebe64b5 | 133 | i = eth_test(); |
paulg | 0:788d8ebe64b5 | 134 | printf("Ethernet test returned %d\n", i); |
paulg | 0:788d8ebe64b5 | 135 | break; |
paulg | 0:788d8ebe64b5 | 136 | |
paulg | 0:788d8ebe64b5 | 137 | case 'l': //list uSD card directory |
paulg | 0:788d8ebe64b5 | 138 | listdir(); |
paulg | 0:788d8ebe64b5 | 139 | break; |
paulg | 0:788d8ebe64b5 | 140 | |
paulg | 0:788d8ebe64b5 | 141 | case 'p': //print uSD card file |
paulg | 0:788d8ebe64b5 | 142 | if (argc != 2) { |
paulg | 0:788d8ebe64b5 | 143 | bad_args(); |
paulg | 0:788d8ebe64b5 | 144 | break; |
paulg | 0:788d8ebe64b5 | 145 | } |
paulg | 0:788d8ebe64b5 | 146 | sprintf(filename, "/sd/%s", arg2); |
paulg | 0:788d8ebe64b5 | 147 | if (exist(filename) == 0) |
paulg | 0:788d8ebe64b5 | 148 | printf("File does not exist\n"); |
paulg | 0:788d8ebe64b5 | 149 | else |
paulg | 0:788d8ebe64b5 | 150 | printfile(filename); |
paulg | 0:788d8ebe64b5 | 151 | break; |
paulg | 0:788d8ebe64b5 | 152 | |
paulg | 0:788d8ebe64b5 | 153 | case 'q': //quit |
paulg | 0:788d8ebe64b5 | 154 | exit(1); |
paulg | 0:788d8ebe64b5 | 155 | break; |
paulg | 0:788d8ebe64b5 | 156 | |
paulg | 0:788d8ebe64b5 | 157 | case 't': //print current time and date |
paulg | 0:788d8ebe64b5 | 158 | seconds = time(NULL); |
paulg | 0:788d8ebe64b5 | 159 | printf("Current time = %s\n", ctime(&seconds)); |
paulg | 0:788d8ebe64b5 | 160 | break; |
paulg | 0:788d8ebe64b5 | 161 | |
paulg | 0:788d8ebe64b5 | 162 | case 'T': //set time and date |
paulg | 0:788d8ebe64b5 | 163 | sscanf(buf, "%c %d %d %d %d %d", &i, &t.tm_hour, &t.tm_min, &t.tm_mday, |
paulg | 0:788d8ebe64b5 | 164 | &t.tm_mon, &t.tm_year); |
paulg | 0:788d8ebe64b5 | 165 | // adjust for tm structure required values |
paulg | 0:788d8ebe64b5 | 166 | t.tm_year = t.tm_year - 1900; |
paulg | 0:788d8ebe64b5 | 167 | t.tm_mon = t.tm_mon - 1; |
paulg | 0:788d8ebe64b5 | 168 | t.tm_sec = 0; |
paulg | 0:788d8ebe64b5 | 169 | // printf("tm = %d %d %d %d %d %d %d\n", t.tm_hour, t.tm_min, t.tm_sec, t.tm_mday, t.tm_mon, t.tm_year); |
paulg | 0:788d8ebe64b5 | 170 | // set the time |
paulg | 0:788d8ebe64b5 | 171 | set_time(mktime(&t)); |
paulg | 0:788d8ebe64b5 | 172 | break; |
paulg | 0:788d8ebe64b5 | 173 | |
paulg | 0:788d8ebe64b5 | 174 | case 'U': //run USB test |
paulg | 0:788d8ebe64b5 | 175 | printf("Any ideas folks?\n"); |
paulg | 0:788d8ebe64b5 | 176 | break; |
paulg | 0:788d8ebe64b5 | 177 | |
paulg | 0:788d8ebe64b5 | 178 | case '?': //print help |
paulg | 0:788d8ebe64b5 | 179 | printhelps(); |
paulg | 0:788d8ebe64b5 | 180 | break; |
paulg | 0:788d8ebe64b5 | 181 | |
paulg | 0:788d8ebe64b5 | 182 | default: |
paulg | 0:788d8ebe64b5 | 183 | printf("?? Unknown command\n"); |
paulg | 0:788d8ebe64b5 | 184 | break; |
paulg | 0:788d8ebe64b5 | 185 | } |
paulg | 0:788d8ebe64b5 | 186 | } |
paulg | 0:788d8ebe64b5 | 187 | } |
paulg | 0:788d8ebe64b5 | 188 | |
paulg | 0:788d8ebe64b5 | 189 | //Command functions |
paulg | 0:788d8ebe64b5 | 190 | |
paulg | 0:788d8ebe64b5 | 191 | void printhelps(void) { |
paulg | 0:788d8ebe64b5 | 192 | printf("Command summary:\n"); |
paulg | 0:788d8ebe64b5 | 193 | printf("c filename create a file on uSD card\n"); |
paulg | 0:788d8ebe64b5 | 194 | printf("d filename delete a file on uSD card\n"); |
paulg | 0:788d8ebe64b5 | 195 | printf("e pins test Ethernet status LEDs (pins = 0-2)\n"); |
paulg | 0:788d8ebe64b5 | 196 | printf("E run Ethernet test (reset mbed to exit)\n"); |
paulg | 0:788d8ebe64b5 | 197 | printf("l list directory of uSD card\n"); |
paulg | 0:788d8ebe64b5 | 198 | printf("p filename print file on uSD card to stdout\n"); |
paulg | 0:788d8ebe64b5 | 199 | printf("q quit\n"); |
paulg | 0:788d8ebe64b5 | 200 | printf("t print current time and date\n"); |
paulg | 0:788d8ebe64b5 | 201 | printf("T h m d m y set RTC time and date\n"); |
paulg | 0:788d8ebe64b5 | 202 | printf("U run USB device test\n"); |
paulg | 0:788d8ebe64b5 | 203 | printf("? print help\n"); |
paulg | 0:788d8ebe64b5 | 204 | } |
paulg | 0:788d8ebe64b5 | 205 | |
paulg | 0:788d8ebe64b5 | 206 | void listdir(void) { |
paulg | 0:788d8ebe64b5 | 207 | DIR *d; |
paulg | 0:788d8ebe64b5 | 208 | struct dirent *p; |
paulg | 0:788d8ebe64b5 | 209 | |
paulg | 0:788d8ebe64b5 | 210 | d = opendir("/sd"); |
paulg | 0:788d8ebe64b5 | 211 | if (d != NULL) { |
paulg | 0:788d8ebe64b5 | 212 | while ((p = readdir(d)) != NULL) { |
paulg | 0:788d8ebe64b5 | 213 | printf(" - %s\n", p->d_name); |
paulg | 0:788d8ebe64b5 | 214 | } |
paulg | 0:788d8ebe64b5 | 215 | } else { |
paulg | 0:788d8ebe64b5 | 216 | printf("Could not open directory!\n"); |
paulg | 0:788d8ebe64b5 | 217 | } |
paulg | 0:788d8ebe64b5 | 218 | closedir(d); |
paulg | 0:788d8ebe64b5 | 219 | } |
paulg | 0:788d8ebe64b5 | 220 | |
paulg | 0:788d8ebe64b5 | 221 | void printfile(char *name) { |
paulg | 0:788d8ebe64b5 | 222 | char buf[80], *cp; |
paulg | 0:788d8ebe64b5 | 223 | FILE *fp; |
paulg | 0:788d8ebe64b5 | 224 | |
paulg | 0:788d8ebe64b5 | 225 | fp = fopen(name, "r"); |
paulg | 0:788d8ebe64b5 | 226 | while (1) { |
paulg | 0:788d8ebe64b5 | 227 | cp = fgets(buf, sizeof(buf), fp); |
paulg | 0:788d8ebe64b5 | 228 | if (cp == NULL) |
paulg | 0:788d8ebe64b5 | 229 | break; |
paulg | 0:788d8ebe64b5 | 230 | if ( (cp = strchr(buf, '\n')) ) |
paulg | 0:788d8ebe64b5 | 231 | *cp = '\0'; //trim trailing \n because |
paulg | 0:788d8ebe64b5 | 232 | puts(buf); //puts will convert terminator to \n |
paulg | 0:788d8ebe64b5 | 233 | } |
paulg | 0:788d8ebe64b5 | 234 | fclose(fp); |
paulg | 0:788d8ebe64b5 | 235 | } |
paulg | 0:788d8ebe64b5 | 236 | |
paulg | 0:788d8ebe64b5 | 237 | //Support functions |
paulg | 0:788d8ebe64b5 | 238 | |
paulg | 0:788d8ebe64b5 | 239 | void bad_args(void) { |
paulg | 0:788d8ebe64b5 | 240 | printf("?? Bad arguments\n"); |
paulg | 0:788d8ebe64b5 | 241 | } |
paulg | 0:788d8ebe64b5 | 242 | |
paulg | 0:788d8ebe64b5 | 243 | int exist(char *name) { |
paulg | 0:788d8ebe64b5 | 244 | FILE *fp; |
paulg | 0:788d8ebe64b5 | 245 | |
paulg | 0:788d8ebe64b5 | 246 | fp = fopen(name, "r"); |
paulg | 0:788d8ebe64b5 | 247 | if (fp != NULL) |
paulg | 0:788d8ebe64b5 | 248 | fclose(fp); |
paulg | 0:788d8ebe64b5 | 249 | return ((fp == NULL) ? 0 : 1); |
paulg | 0:788d8ebe64b5 | 250 | } |
paulg | 0:788d8ebe64b5 | 251 | |
paulg | 0:788d8ebe64b5 | 252 | |
paulg | 0:788d8ebe64b5 | 253 | //Ethernet test stuff (based on Michael Wei's Ethernet Tester program) |
paulg | 0:788d8ebe64b5 | 254 | |
paulg | 0:788d8ebe64b5 | 255 | Ethernet ethernet; |
paulg | 0:788d8ebe64b5 | 256 | DigitalOut ledLink(p25); |
paulg | 0:788d8ebe64b5 | 257 | DigitalOut ledSpeed(p26); |
paulg | 0:788d8ebe64b5 | 258 | DigitalOut ledStage0(LED1); |
paulg | 0:788d8ebe64b5 | 259 | DigitalOut ledStage1(LED2); |
paulg | 0:788d8ebe64b5 | 260 | DigitalOut ledStage2(LED3); |
paulg | 0:788d8ebe64b5 | 261 | DigitalOut ledTCP80(LED4); |
paulg | 0:788d8ebe64b5 | 262 | |
paulg | 0:788d8ebe64b5 | 263 | DigitalIn phyLink(P1_25); |
paulg | 0:788d8ebe64b5 | 264 | DigitalIn phySpeed(P1_26); |
paulg | 0:788d8ebe64b5 | 265 | |
paulg | 0:788d8ebe64b5 | 266 | volatile char stage = 0; |
paulg | 0:788d8ebe64b5 | 267 | |
paulg | 0:788d8ebe64b5 | 268 | Ticker stage_blinker; |
paulg | 0:788d8ebe64b5 | 269 | |
paulg | 0:788d8ebe64b5 | 270 | struct netif netif_data; |
paulg | 0:788d8ebe64b5 | 271 | |
paulg | 0:788d8ebe64b5 | 272 | const char testPage[] = "HTTP/1.1 200 OK\r\n" |
paulg | 0:788d8ebe64b5 | 273 | "Content-Type: text/html\r\n" |
paulg | 0:788d8ebe64b5 | 274 | "Connection: Close\r\n\r\n" |
paulg | 0:788d8ebe64b5 | 275 | "<html>" |
paulg | 0:788d8ebe64b5 | 276 | "<head>" |
paulg | 0:788d8ebe64b5 | 277 | "<title>mbed test page</title>" |
paulg | 0:788d8ebe64b5 | 278 | "<style type='text/css'>" |
paulg | 0:788d8ebe64b5 | 279 | "body{font-family:'Arial, sans-serif', sans-serif;font-size:.8em;background-color:#fff;}" |
paulg | 0:788d8ebe64b5 | 280 | "</style>" |
paulg | 0:788d8ebe64b5 | 281 | "</head>" |
paulg | 0:788d8ebe64b5 | 282 | "<body>%s</body></html>\r\n\r\n"; |
paulg | 0:788d8ebe64b5 | 283 | |
paulg | 0:788d8ebe64b5 | 284 | char buffer[1024]; |
paulg | 0:788d8ebe64b5 | 285 | char temp[1024]; |
paulg | 0:788d8ebe64b5 | 286 | |
paulg | 0:788d8ebe64b5 | 287 | err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { |
paulg | 0:788d8ebe64b5 | 288 | struct netif *netif = &netif_data; |
paulg | 0:788d8ebe64b5 | 289 | ledTCP80 = true; |
paulg | 0:788d8ebe64b5 | 290 | printf("TCP callback from %d.%d.%d.%d\r\n", ip4_addr1(&(pcb->remote_ip)),ip4_addr2(&(pcb->remote_ip)),ip4_addr3(&(pcb->remote_ip)),ip4_addr4(&(pcb->remote_ip))); |
paulg | 0:788d8ebe64b5 | 291 | char *data; |
paulg | 0:788d8ebe64b5 | 292 | /* Check if status is ok and data is arrived. */ |
paulg | 0:788d8ebe64b5 | 293 | if (err == ERR_OK && p != NULL) { |
paulg | 0:788d8ebe64b5 | 294 | /* Inform TCP that we have taken the data. */ |
paulg | 0:788d8ebe64b5 | 295 | tcp_recved(pcb, p->tot_len); |
paulg | 0:788d8ebe64b5 | 296 | data = static_cast<char *>(p->payload); |
paulg | 0:788d8ebe64b5 | 297 | /* If the data is a GET request we can handle it. */ |
paulg | 0:788d8ebe64b5 | 298 | if (strncmp(data, "GET ", 4) == 0) { |
paulg | 0:788d8ebe64b5 | 299 | printf("Handling GET request...\r\n"); |
paulg | 0:788d8ebe64b5 | 300 | printf("Request:\r\n%s\r\n", data); |
paulg | 0:788d8ebe64b5 | 301 | |
paulg | 0:788d8ebe64b5 | 302 | //generate the test page |
paulg | 0:788d8ebe64b5 | 303 | time_t seconds = time(NULL); |
paulg | 0:788d8ebe64b5 | 304 | sprintf(temp, "<h1>Congratulations!</h1>If you can see this page, your mbed is working properly." |
paulg | 0:788d8ebe64b5 | 305 | "<h2>mbed Configuration</h2>" |
paulg | 0:788d8ebe64b5 | 306 | "mbed RTC time:%s<br/>" |
paulg | 0:788d8ebe64b5 | 307 | "mbed HW address: %02x:%02x:%02x:%02x:%02x:%02x<br/>" |
paulg | 0:788d8ebe64b5 | 308 | "mbed IP Address: %s<br/>", |
paulg | 0:788d8ebe64b5 | 309 | ctime(&seconds), |
paulg | 0:788d8ebe64b5 | 310 | (char*) netif->hwaddr[0], |
paulg | 0:788d8ebe64b5 | 311 | (char*) netif->hwaddr[1], |
paulg | 0:788d8ebe64b5 | 312 | (char*) netif->hwaddr[2], |
paulg | 0:788d8ebe64b5 | 313 | (char*) netif->hwaddr[3], |
paulg | 0:788d8ebe64b5 | 314 | (char*) netif->hwaddr[4], |
paulg | 0:788d8ebe64b5 | 315 | (char*) netif->hwaddr[5], |
paulg | 0:788d8ebe64b5 | 316 | inet_ntoa(*(struct in_addr*)&(netif->ip_addr)) |
paulg | 0:788d8ebe64b5 | 317 | ); |
paulg | 0:788d8ebe64b5 | 318 | sprintf(buffer, testPage, temp); |
paulg | 0:788d8ebe64b5 | 319 | if (tcp_write(pcb, (void *)buffer, strlen(buffer), 1) == ERR_OK) { |
paulg | 0:788d8ebe64b5 | 320 | tcp_output(pcb); |
paulg | 0:788d8ebe64b5 | 321 | printf("Closing connection...\r\n"); |
paulg | 0:788d8ebe64b5 | 322 | tcp_close(pcb); |
paulg | 0:788d8ebe64b5 | 323 | } |
paulg | 0:788d8ebe64b5 | 324 | } |
paulg | 0:788d8ebe64b5 | 325 | else |
paulg | 0:788d8ebe64b5 | 326 | { |
paulg | 0:788d8ebe64b5 | 327 | printf("Non GET request...\r\nRequest:\r\n%s\r\n", data); |
paulg | 0:788d8ebe64b5 | 328 | } |
paulg | 0:788d8ebe64b5 | 329 | |
paulg | 0:788d8ebe64b5 | 330 | pbuf_free(p); |
paulg | 0:788d8ebe64b5 | 331 | } |
paulg | 0:788d8ebe64b5 | 332 | |
paulg | 0:788d8ebe64b5 | 333 | else { |
paulg | 0:788d8ebe64b5 | 334 | /* No data arrived */ |
paulg | 0:788d8ebe64b5 | 335 | /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */ |
paulg | 0:788d8ebe64b5 | 336 | /* We have to cleanup and destroy out TCPConnection. */ |
paulg | 0:788d8ebe64b5 | 337 | printf("Connection closed by client.\r\n"); |
paulg | 0:788d8ebe64b5 | 338 | pbuf_free(p); |
paulg | 0:788d8ebe64b5 | 339 | } |
paulg | 0:788d8ebe64b5 | 340 | /* Don't panic! Everything is fine. */ |
paulg | 0:788d8ebe64b5 | 341 | ledTCP80 = false; |
paulg | 0:788d8ebe64b5 | 342 | return ERR_OK; |
paulg | 0:788d8ebe64b5 | 343 | } |
paulg | 0:788d8ebe64b5 | 344 | /* Accept an incomming call on the registered port */ |
paulg | 0:788d8ebe64b5 | 345 | err_t accept_callback(void *arg, struct tcp_pcb *npcb, err_t err) { |
paulg | 0:788d8ebe64b5 | 346 | LWIP_UNUSED_ARG(arg); |
paulg | 0:788d8ebe64b5 | 347 | /* Subscribe a receive callback function */ |
paulg | 0:788d8ebe64b5 | 348 | tcp_recv(npcb, &recv_callback); |
paulg | 0:788d8ebe64b5 | 349 | /* Don't panic! Everything is fine. */ |
paulg | 0:788d8ebe64b5 | 350 | return ERR_OK; |
paulg | 0:788d8ebe64b5 | 351 | } |
paulg | 0:788d8ebe64b5 | 352 | |
paulg | 0:788d8ebe64b5 | 353 | void stageblinker() |
paulg | 0:788d8ebe64b5 | 354 | { |
paulg | 0:788d8ebe64b5 | 355 | switch (stage) |
paulg | 0:788d8ebe64b5 | 356 | { |
paulg | 0:788d8ebe64b5 | 357 | case 0: |
paulg | 0:788d8ebe64b5 | 358 | ledStage0 = !ledStage0; |
paulg | 0:788d8ebe64b5 | 359 | ledStage1 = false; |
paulg | 0:788d8ebe64b5 | 360 | ledStage2 = false; |
paulg | 0:788d8ebe64b5 | 361 | break; |
paulg | 0:788d8ebe64b5 | 362 | case 1: |
paulg | 0:788d8ebe64b5 | 363 | ledStage0 = true; |
paulg | 0:788d8ebe64b5 | 364 | ledStage1 = !ledStage1; |
paulg | 0:788d8ebe64b5 | 365 | ledStage2 = false; |
paulg | 0:788d8ebe64b5 | 366 | break; |
paulg | 0:788d8ebe64b5 | 367 | case 2: |
paulg | 0:788d8ebe64b5 | 368 | ledStage0 = true; |
paulg | 0:788d8ebe64b5 | 369 | ledStage1 = true; |
paulg | 0:788d8ebe64b5 | 370 | ledStage2 = true; |
paulg | 0:788d8ebe64b5 | 371 | stage_blinker.detach(); |
paulg | 0:788d8ebe64b5 | 372 | break; |
paulg | 0:788d8ebe64b5 | 373 | } |
paulg | 0:788d8ebe64b5 | 374 | } |
paulg | 0:788d8ebe64b5 | 375 | |
paulg | 0:788d8ebe64b5 | 376 | int eth_test() { |
paulg | 0:788d8ebe64b5 | 377 | printf("mBed Ethernet Tester 1.0\r\nStarting Up...\r\n"); |
paulg | 0:788d8ebe64b5 | 378 | stage = 0; |
paulg | 0:788d8ebe64b5 | 379 | struct netif *netif = &netif_data; |
paulg | 0:788d8ebe64b5 | 380 | struct ip_addr ipaddr; |
paulg | 0:788d8ebe64b5 | 381 | struct ip_addr netmask; |
paulg | 0:788d8ebe64b5 | 382 | struct ip_addr gateway; |
paulg | 0:788d8ebe64b5 | 383 | Ticker tickFast, tickSlow, tickARP, eth_tick, dns_tick, dhcp_coarse, dhcp_fine; |
paulg | 0:788d8ebe64b5 | 384 | stage_blinker.attach_us(&stageblinker, 1000*500); |
paulg | 0:788d8ebe64b5 | 385 | |
paulg | 0:788d8ebe64b5 | 386 | char *hostname = "my-mbed"; |
paulg | 0:788d8ebe64b5 | 387 | |
paulg | 0:788d8ebe64b5 | 388 | printf("Configuring device for DHCP...\r\n"); |
paulg | 0:788d8ebe64b5 | 389 | /* Start Network with DHCP */ |
paulg | 0:788d8ebe64b5 | 390 | IP4_ADDR(&netmask, 255,255,255,255); |
paulg | 0:788d8ebe64b5 | 391 | IP4_ADDR(&gateway, 0,0,0,0); |
paulg | 0:788d8ebe64b5 | 392 | IP4_ADDR(&ipaddr, 0,0,0,0); |
paulg | 0:788d8ebe64b5 | 393 | /* Initialise after configuration */ |
paulg | 0:788d8ebe64b5 | 394 | lwip_init(); |
paulg | 0:788d8ebe64b5 | 395 | netif->hwaddr_len = ETHARP_HWADDR_LEN; |
paulg | 0:788d8ebe64b5 | 396 | device_address((char *)netif->hwaddr); |
paulg | 0:788d8ebe64b5 | 397 | netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input); |
paulg | 0:788d8ebe64b5 | 398 | netif->hostname = hostname; |
paulg | 0:788d8ebe64b5 | 399 | netif_set_default(netif); |
paulg | 0:788d8ebe64b5 | 400 | dhcp_start(netif); // <-- Use DHCP |
paulg | 0:788d8ebe64b5 | 401 | |
paulg | 0:788d8ebe64b5 | 402 | /* Initialise all needed timers */ |
paulg | 0:788d8ebe64b5 | 403 | tickARP.attach_us( ðarp_tmr, ARP_TMR_INTERVAL * 1000); |
paulg | 0:788d8ebe64b5 | 404 | tickFast.attach_us(&tcp_fasttmr, TCP_FAST_INTERVAL * 1000); |
paulg | 0:788d8ebe64b5 | 405 | tickSlow.attach_us(&tcp_slowtmr, TCP_SLOW_INTERVAL * 1000); |
paulg | 0:788d8ebe64b5 | 406 | dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000); |
paulg | 0:788d8ebe64b5 | 407 | dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000); |
paulg | 0:788d8ebe64b5 | 408 | dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); |
paulg | 0:788d8ebe64b5 | 409 | stage = 1; |
paulg | 0:788d8ebe64b5 | 410 | while (!netif_is_up(netif)) { |
paulg | 0:788d8ebe64b5 | 411 | ledLink = ethernet.link(); |
paulg | 0:788d8ebe64b5 | 412 | ledSpeed = !phySpeed; |
paulg | 0:788d8ebe64b5 | 413 | device_poll(); |
paulg | 0:788d8ebe64b5 | 414 | } |
paulg | 0:788d8ebe64b5 | 415 | |
paulg | 0:788d8ebe64b5 | 416 | /* |
paulg | 0:788d8ebe64b5 | 417 | while (!(netif->dhcp->state == DHCP_BOUND || netif->dhcp->state == DHCP_PERMANENT)) |
paulg | 0:788d8ebe64b5 | 418 | { |
paulg | 0:788d8ebe64b5 | 419 | ledLink = ethernet.link(); |
paulg | 0:788d8ebe64b5 | 420 | device_poll(); |
paulg | 0:788d8ebe64b5 | 421 | //printf("Waiting for DHCP response, state = %d\r\n", netif->dhcp->state); |
paulg | 0:788d8ebe64b5 | 422 | //wait_ms(100); |
paulg | 0:788d8ebe64b5 | 423 | } |
paulg | 0:788d8ebe64b5 | 424 | */ |
paulg | 0:788d8ebe64b5 | 425 | stage = 2; |
paulg | 0:788d8ebe64b5 | 426 | printf("Interface is up, local IP is %s\r\n", |
paulg | 0:788d8ebe64b5 | 427 | inet_ntoa(*(struct in_addr*)&(netif->ip_addr))); |
paulg | 0:788d8ebe64b5 | 428 | |
paulg | 0:788d8ebe64b5 | 429 | printf("Starting Web Server...\r\n"); |
paulg | 0:788d8ebe64b5 | 430 | |
paulg | 0:788d8ebe64b5 | 431 | /* Bind a function to a tcp port */ |
paulg | 0:788d8ebe64b5 | 432 | struct tcp_pcb *pcb = tcp_new(); |
paulg | 0:788d8ebe64b5 | 433 | if (tcp_bind(pcb, IP_ADDR_ANY, 80) == ERR_OK) { |
paulg | 0:788d8ebe64b5 | 434 | pcb = tcp_listen(pcb); |
paulg | 0:788d8ebe64b5 | 435 | tcp_accept(pcb, &accept_callback); |
paulg | 0:788d8ebe64b5 | 436 | } |
paulg | 0:788d8ebe64b5 | 437 | |
paulg | 0:788d8ebe64b5 | 438 | printf("Waiting for connection...\r\n"); |
paulg | 0:788d8ebe64b5 | 439 | while(1) { |
paulg | 0:788d8ebe64b5 | 440 | device_poll(); |
paulg | 0:788d8ebe64b5 | 441 | ledLink = ethernet.link(); |
paulg | 0:788d8ebe64b5 | 442 | ledSpeed = !phySpeed; |
paulg | 0:788d8ebe64b5 | 443 | } |
paulg | 0:788d8ebe64b5 | 444 | } |