First SPI/SDcard & UART tests
Dependencies: SDFileSystem mbed-rtos mbed cmsis_rtos_demo_thread
Fork of cmsis_rtos_demo_thread by
main.cpp@5:3409eb01a539, 2015-04-07 (annotated)
- Committer:
- moxondesign
- Date:
- Tue Apr 07 00:32:11 2015 +0000
- Revision:
- 5:3409eb01a539
- Parent:
- 3:9fa442c3fd27
L152 SPI/SDcard test, Uart test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cnhzcy14 | 0:618ffeb1dfc5 | 1 | #include "mbed.h" |
moxondesign | 3:9fa442c3fd27 | 2 | #include "rtos.h" |
cnhzcy14 | 0:618ffeb1dfc5 | 3 | #include "cmsis_os.h" |
moxondesign | 2:23757aa762a7 | 4 | #include "SDFileSystem.h" |
cnhzcy14 | 0:618ffeb1dfc5 | 5 | |
moxondesign | 2:23757aa762a7 | 6 | DigitalOut led1(D7); |
moxondesign | 3:9fa442c3fd27 | 7 | DigitalOut led2(D9); |
moxondesign | 3:9fa442c3fd27 | 8 | Serial uart1(D8, D2); /* UART1: Modbus */ |
moxondesign | 3:9fa442c3fd27 | 9 | Serial uart2(D1, D0); /* UART2: pc(USBTX, USBRX) */ |
moxondesign | 3:9fa442c3fd27 | 10 | /* (i.e.) pc(D1, D0) */ |
moxondesign | 3:9fa442c3fd27 | 11 | I2C i2c(I2C_SDA, I2C_SCL); /* I2C1: */ |
moxondesign | 3:9fa442c3fd27 | 12 | |
moxondesign | 3:9fa442c3fd27 | 13 | SDFileSystem sd(D11, D12, D13, D10, "sd"); /* SPI1: MOSI, MISO, SCK, CS */ |
moxondesign | 2:23757aa762a7 | 14 | FILE *fp; |
cnhzcy14 | 0:618ffeb1dfc5 | 15 | |
moxondesign | 3:9fa442c3fd27 | 16 | AnalogIn ain_0(A0); |
moxondesign | 3:9fa442c3fd27 | 17 | AnalogIn ain_1(A1); |
moxondesign | 3:9fa442c3fd27 | 18 | AnalogIn ain_2(A2); |
moxondesign | 3:9fa442c3fd27 | 19 | AnalogIn ain_3(A3); |
moxondesign | 3:9fa442c3fd27 | 20 | |
moxondesign | 3:9fa442c3fd27 | 21 | InterruptIn user_sw_1(USER_BUTTON); |
moxondesign | 3:9fa442c3fd27 | 22 | |
moxondesign | 3:9fa442c3fd27 | 23 | /* N.B. - add to the FRU... */ |
moxondesign | 3:9fa442c3fd27 | 24 | //const char key[] = "your_api_key"; // Replace with your API key |
moxondesign | 3:9fa442c3fd27 | 25 | //const char feed[] = "your_feed_id"; // Replace with your Feed ID |
moxondesign | 3:9fa442c3fd27 | 26 | //const char stream[] = "your_stream_name"; // Replace with your stream name |
moxondesign | 3:9fa442c3fd27 | 27 | //char name[] = "your_device_location_name"; // Name of current location of datasource |
moxondesign | 3:9fa442c3fd27 | 28 | //double latitude = 30.3748076; |
moxondesign | 3:9fa442c3fd27 | 29 | //double longitude = -97.7386896; // You can also read those values from a GPS |
moxondesign | 3:9fa442c3fd27 | 30 | //double elevation = 400.00; |
moxondesign | 3:9fa442c3fd27 | 31 | |
moxondesign | 2:23757aa762a7 | 32 | void led2_thread(void const *args){ |
moxondesign | 2:23757aa762a7 | 33 | while (true) { |
moxondesign | 3:9fa442c3fd27 | 34 | led1 = led2; |
moxondesign | 2:23757aa762a7 | 35 | led2 = !led2; |
moxondesign | 2:23757aa762a7 | 36 | osDelay(1000); |
moxondesign | 2:23757aa762a7 | 37 | } |
moxondesign | 2:23757aa762a7 | 38 | } |
cnhzcy14 | 0:618ffeb1dfc5 | 39 | |
moxondesign | 2:23757aa762a7 | 40 | void uart1_thread(const void *args){ |
moxondesign | 2:23757aa762a7 | 41 | uart1.baud(9600); |
moxondesign | 2:23757aa762a7 | 42 | while(true){ |
moxondesign | 2:23757aa762a7 | 43 | uart1.putc(uart1.getc()); |
moxondesign | 2:23757aa762a7 | 44 | } |
moxondesign | 2:23757aa762a7 | 45 | } |
moxondesign | 2:23757aa762a7 | 46 | void uart2_thread(const void *args){ |
moxondesign | 2:23757aa762a7 | 47 | uart2.baud(9600); |
moxondesign | 2:23757aa762a7 | 48 | while(true){ |
moxondesign | 2:23757aa762a7 | 49 | uart2.putc(uart2.getc()); |
moxondesign | 2:23757aa762a7 | 50 | } |
cnhzcy14 | 0:618ffeb1dfc5 | 51 | } |
cnhzcy14 | 0:618ffeb1dfc5 | 52 | |
moxondesign | 2:23757aa762a7 | 53 | osThreadDef(uart1_thread, osPriorityNormal, DEFAULT_STACK_SIZE); |
moxondesign | 2:23757aa762a7 | 54 | osThreadDef(uart2_thread, osPriorityNormal, DEFAULT_STACK_SIZE); |
moxondesign | 2:23757aa762a7 | 55 | osThreadDef(led2_thread, osPriorityNormal, DEFAULT_STACK_SIZE); |
cnhzcy14 | 0:618ffeb1dfc5 | 56 | |
moxondesign | 2:23757aa762a7 | 57 | int main() { |
moxondesign | 3:9fa442c3fd27 | 58 | |
moxondesign | 3:9fa442c3fd27 | 59 | /* Set RTC time to 16 December 2013 10:05:23 UTC */ |
moxondesign | 3:9fa442c3fd27 | 60 | set_time(1387188323); |
moxondesign | 3:9fa442c3fd27 | 61 | /* Time as seconds since January 1, 1970 */ |
moxondesign | 3:9fa442c3fd27 | 62 | time_t seconds = time(NULL); /* */ |
moxondesign | 3:9fa442c3fd27 | 63 | |
moxondesign | 2:23757aa762a7 | 64 | osThreadCreate(osThread(led2_thread), NULL); |
moxondesign | 2:23757aa762a7 | 65 | osThreadCreate(osThread(uart1_thread), NULL); |
moxondesign | 3:9fa442c3fd27 | 66 | wait(1); |
moxondesign | 2:23757aa762a7 | 67 | |
moxondesign | 2:23757aa762a7 | 68 | uart2.printf("\r\n"); |
moxondesign | 2:23757aa762a7 | 69 | wait(1); |
moxondesign | 2:23757aa762a7 | 70 | uart2.printf("Initializing...\r\n"); |
moxondesign | 2:23757aa762a7 | 71 | |
moxondesign | 2:23757aa762a7 | 72 | fp = fopen("/sd/hello.txt", "r"); |
moxondesign | 2:23757aa762a7 | 73 | if (fp != NULL) { |
moxondesign | 2:23757aa762a7 | 74 | fclose(fp); |
moxondesign | 2:23757aa762a7 | 75 | remove("/sd/hello.txt"); |
moxondesign | 2:23757aa762a7 | 76 | uart2.printf("Remove an existing file with the same name\r\n"); |
cnhzcy14 | 0:618ffeb1dfc5 | 77 | } |
moxondesign | 2:23757aa762a7 | 78 | |
moxondesign | 2:23757aa762a7 | 79 | fp = fopen("/sd/hello.txt", "w"); |
moxondesign | 2:23757aa762a7 | 80 | if (fp == NULL) { |
moxondesign | 2:23757aa762a7 | 81 | uart2.printf("Unable to write the file\r\n"); |
moxondesign | 2:23757aa762a7 | 82 | } else { |
moxondesign | 2:23757aa762a7 | 83 | fprintf(fp, "mbed SDCard application!"); |
moxondesign | 2:23757aa762a7 | 84 | fclose(fp); |
moxondesign | 2:23757aa762a7 | 85 | uart2.printf("File successfully written!\r\n"); |
moxondesign | 2:23757aa762a7 | 86 | } |
moxondesign | 2:23757aa762a7 | 87 | |
moxondesign | 2:23757aa762a7 | 88 | osThreadCreate(osThread(uart2_thread), NULL); |
moxondesign | 3:9fa442c3fd27 | 89 | |
moxondesign | 3:9fa442c3fd27 | 90 | Thread::wait(osWaitForever); /* this is better than an infinite loop... */ |
moxondesign | 2:23757aa762a7 | 91 | /* spin and spin */ |
moxondesign | 3:9fa442c3fd27 | 92 | // while(true); |
cnhzcy14 | 0:618ffeb1dfc5 | 93 | } |