Test program with networking part in RTOS-thread and SD in RTOS-thread
Dependencies: fat mbed mbed-rtos EthernetInterface SDFileSystem
Fork of Network-RTOS by
main.cpp@1:5a71a8a0463b, 2012-07-22 (annotated)
- Committer:
- tuxic
- Date:
- Sun Jul 22 12:51:41 2012 +0000
- Revision:
- 1:5a71a8a0463b
- Parent:
- 0:7a6d4b87941e
Test with network, SD and RTOS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tuxic | 0:7a6d4b87941e | 1 | #include "mbed.h" |
tuxic | 0:7a6d4b87941e | 2 | #include "EthernetInterface.h" |
tuxic | 1:5a71a8a0463b | 3 | #include "SDFileSystem.h" |
tuxic | 1:5a71a8a0463b | 4 | #include "rtos.h" |
tuxic | 0:7a6d4b87941e | 5 | #include "cmsis_os.h" |
tuxic | 0:7a6d4b87941e | 6 | |
tuxic | 0:7a6d4b87941e | 7 | DigitalOut led1(LED1); |
tuxic | 0:7a6d4b87941e | 8 | DigitalOut led2(LED2); |
tuxic | 0:7a6d4b87941e | 9 | DigitalOut led3(LED3); |
tuxic | 0:7a6d4b87941e | 10 | |
tuxic | 0:7a6d4b87941e | 11 | void led2_thread(void const *argument) { |
tuxic | 0:7a6d4b87941e | 12 | while (true) { |
tuxic | 0:7a6d4b87941e | 13 | led2 = !led2; |
tuxic | 0:7a6d4b87941e | 14 | osDelay(1000); |
tuxic | 0:7a6d4b87941e | 15 | } |
tuxic | 0:7a6d4b87941e | 16 | } |
tuxic | 0:7a6d4b87941e | 17 | void net_thread(void const *argument) { |
tuxic | 0:7a6d4b87941e | 18 | EthernetInterface eth; |
tuxic | 0:7a6d4b87941e | 19 | eth.init(); //Use DHCP |
tuxic | 0:7a6d4b87941e | 20 | eth.connect(); |
tuxic | 0:7a6d4b87941e | 21 | printf("IP Address is %s\n", eth.getIPAddress()); |
tuxic | 0:7a6d4b87941e | 22 | TCPSocket sock; |
tuxic | 0:7a6d4b87941e | 23 | while (true) { |
tuxic | 0:7a6d4b87941e | 24 | led3 = !led3; |
tuxic | 0:7a6d4b87941e | 25 | sock.connect("mbed.org", 80); |
tuxic | 0:7a6d4b87941e | 26 | |
tuxic | 0:7a6d4b87941e | 27 | char http_cmd[] = "GET /media/uploads/donatien/hello.txt HTTP/1.1\r\nHost: %s\r\n\r\n"; |
tuxic | 0:7a6d4b87941e | 28 | sock.send(http_cmd, sizeof(http_cmd) - 1, 3000); |
tuxic | 0:7a6d4b87941e | 29 | |
tuxic | 0:7a6d4b87941e | 30 | char in_buf[256]; |
tuxic | 0:7a6d4b87941e | 31 | bool firstIteration = true; |
tuxic | 0:7a6d4b87941e | 32 | int ret; |
tuxic | 0:7a6d4b87941e | 33 | do { |
tuxic | 0:7a6d4b87941e | 34 | ret = sock.receive(in_buf, 255, firstIteration?3000:0); |
tuxic | 0:7a6d4b87941e | 35 | in_buf[ret] = '\0'; |
tuxic | 0:7a6d4b87941e | 36 | |
tuxic | 0:7a6d4b87941e | 37 | printf("Received %d chars from server: %s\n", ret, in_buf); |
tuxic | 0:7a6d4b87941e | 38 | firstIteration = false; |
tuxic | 0:7a6d4b87941e | 39 | } while ( ret > 0 ); |
tuxic | 0:7a6d4b87941e | 40 | |
tuxic | 0:7a6d4b87941e | 41 | sock.close(); |
tuxic | 0:7a6d4b87941e | 42 | |
tuxic | 0:7a6d4b87941e | 43 | //eth.disconnect(); |
tuxic | 0:7a6d4b87941e | 44 | |
tuxic | 0:7a6d4b87941e | 45 | led3 = !led3; |
tuxic | 0:7a6d4b87941e | 46 | osDelay(500); |
tuxic | 0:7a6d4b87941e | 47 | } |
tuxic | 0:7a6d4b87941e | 48 | } |
tuxic | 1:5a71a8a0463b | 49 | void sd_thread(void const *argument) { |
tuxic | 1:5a71a8a0463b | 50 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
tuxic | 1:5a71a8a0463b | 51 | FILE *f = fopen("/sd/out.txt", "w"); |
tuxic | 1:5a71a8a0463b | 52 | for (int i=0; i<30; i++) { |
tuxic | 1:5a71a8a0463b | 53 | fprintf(f, "%d\n", i); |
tuxic | 1:5a71a8a0463b | 54 | printf("%d\n\r", i); |
tuxic | 1:5a71a8a0463b | 55 | } |
tuxic | 1:5a71a8a0463b | 56 | printf("closing\n"); |
tuxic | 1:5a71a8a0463b | 57 | fclose(f); |
tuxic | 1:5a71a8a0463b | 58 | |
tuxic | 1:5a71a8a0463b | 59 | while (true) { |
tuxic | 1:5a71a8a0463b | 60 | led2 = !led2; |
tuxic | 1:5a71a8a0463b | 61 | Thread::wait(1000); |
tuxic | 1:5a71a8a0463b | 62 | } |
tuxic | 1:5a71a8a0463b | 63 | } |
tuxic | 0:7a6d4b87941e | 64 | osThreadDef(led2_thread, osPriorityNormal, DEFAULT_STACK_SIZE); |
tuxic | 0:7a6d4b87941e | 65 | osThreadDef(net_thread, osPriorityNormal, DEFAULT_STACK_SIZE); |
tuxic | 1:5a71a8a0463b | 66 | osThreadDef(sd_thread, osPriorityNormal, (int)(DEFAULT_STACK_SIZE * 2.25)); |
tuxic | 0:7a6d4b87941e | 67 | |
tuxic | 0:7a6d4b87941e | 68 | int main() { |
tuxic | 0:7a6d4b87941e | 69 | osThreadCreate(osThread(led2_thread), NULL); |
tuxic | 0:7a6d4b87941e | 70 | osThreadCreate(osThread(net_thread), NULL); |
tuxic | 1:5a71a8a0463b | 71 | osThreadCreate(osThread(sd_thread), NULL); |
tuxic | 1:5a71a8a0463b | 72 | |
tuxic | 0:7a6d4b87941e | 73 | while (true) { |
tuxic | 0:7a6d4b87941e | 74 | led1 = !led1; |
tuxic | 0:7a6d4b87941e | 75 | osDelay(500); |
tuxic | 0:7a6d4b87941e | 76 | } |
tuxic | 0:7a6d4b87941e | 77 | } |
tuxic | 0:7a6d4b87941e | 78 |