First SPI/SDcard & UART tests

Dependencies:   SDFileSystem mbed-rtos mbed cmsis_rtos_demo_thread

Fork of cmsis_rtos_demo_thread by Ye Cheng

Committer:
moxondesign
Date:
Mon Sep 29 23:00:24 2014 +0000
Revision:
2:23757aa762a7
Parent:
1:cbda4d713dc8
Child:
3:9fa442c3fd27
initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cnhzcy14 0:618ffeb1dfc5 1 #include "mbed.h"
cnhzcy14 0:618ffeb1dfc5 2 #include "cmsis_os.h"
moxondesign 2:23757aa762a7 3 #include "SDFileSystem.h"
cnhzcy14 0:618ffeb1dfc5 4
moxondesign 2:23757aa762a7 5 DigitalOut led1(D7);
moxondesign 2:23757aa762a7 6 DigitalOut led2(PD_2);
moxondesign 2:23757aa762a7 7 Serial uart1(D8, D2); /* UART1: Modbus */
moxondesign 2:23757aa762a7 8 Serial uart2(D1, D0); /* UART2: pc(USBTX, USBRX) */
moxondesign 2:23757aa762a7 9 /* (i.e.) pc(D1, D0) */
moxondesign 2:23757aa762a7 10 SDFileSystem sd(D11, D12, D13, D10, "sd"); /* MOSI, MISO, SCK, CS */
moxondesign 2:23757aa762a7 11 FILE *fp;
cnhzcy14 0:618ffeb1dfc5 12
moxondesign 2:23757aa762a7 13 void led2_thread(void const *args){
moxondesign 2:23757aa762a7 14 while (true) {
moxondesign 2:23757aa762a7 15 led2 = !led2;
moxondesign 2:23757aa762a7 16 osDelay(1000);
moxondesign 2:23757aa762a7 17 }
moxondesign 2:23757aa762a7 18 }
cnhzcy14 0:618ffeb1dfc5 19
moxondesign 2:23757aa762a7 20 void uart1_thread(const void *args){
moxondesign 2:23757aa762a7 21 uart1.baud(9600);
moxondesign 2:23757aa762a7 22 while(true){
moxondesign 2:23757aa762a7 23 uart1.putc(uart1.getc());
moxondesign 2:23757aa762a7 24 }
moxondesign 2:23757aa762a7 25 }
moxondesign 2:23757aa762a7 26 void uart2_thread(const void *args){
moxondesign 2:23757aa762a7 27 uart2.baud(9600);
moxondesign 2:23757aa762a7 28 while(true){
moxondesign 2:23757aa762a7 29 uart2.putc(uart2.getc());
moxondesign 2:23757aa762a7 30 }
cnhzcy14 0:618ffeb1dfc5 31 }
cnhzcy14 0:618ffeb1dfc5 32
moxondesign 2:23757aa762a7 33 osThreadDef(uart1_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
moxondesign 2:23757aa762a7 34 osThreadDef(uart2_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
moxondesign 2:23757aa762a7 35 osThreadDef(led2_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
cnhzcy14 0:618ffeb1dfc5 36
moxondesign 2:23757aa762a7 37 int main() {
moxondesign 2:23757aa762a7 38 osThreadCreate(osThread(led2_thread), NULL);
moxondesign 2:23757aa762a7 39 osThreadCreate(osThread(uart1_thread), NULL);
cnhzcy14 0:618ffeb1dfc5 40
moxondesign 2:23757aa762a7 41
moxondesign 2:23757aa762a7 42 wait(1);
moxondesign 2:23757aa762a7 43 uart2.printf("\r\n");
moxondesign 2:23757aa762a7 44 wait(1);
moxondesign 2:23757aa762a7 45 uart2.printf("Initializing...\r\n");
moxondesign 2:23757aa762a7 46
moxondesign 2:23757aa762a7 47 fp = fopen("/sd/hello.txt", "r");
moxondesign 2:23757aa762a7 48 if (fp != NULL) {
moxondesign 2:23757aa762a7 49 fclose(fp);
moxondesign 2:23757aa762a7 50 remove("/sd/hello.txt");
moxondesign 2:23757aa762a7 51 uart2.printf("Remove an existing file with the same name\r\n");
cnhzcy14 0:618ffeb1dfc5 52 }
moxondesign 2:23757aa762a7 53
moxondesign 2:23757aa762a7 54 fp = fopen("/sd/hello.txt", "w");
moxondesign 2:23757aa762a7 55 if (fp == NULL) {
moxondesign 2:23757aa762a7 56 uart2.printf("Unable to write the file\r\n");
moxondesign 2:23757aa762a7 57 } else {
moxondesign 2:23757aa762a7 58 fprintf(fp, "mbed SDCard application!");
moxondesign 2:23757aa762a7 59 fclose(fp);
moxondesign 2:23757aa762a7 60 uart2.printf("File successfully written!\r\n");
moxondesign 2:23757aa762a7 61 }
moxondesign 2:23757aa762a7 62
moxondesign 2:23757aa762a7 63 osThreadCreate(osThread(uart2_thread), NULL);
moxondesign 2:23757aa762a7 64
moxondesign 2:23757aa762a7 65 /* spin and spin */
moxondesign 2:23757aa762a7 66 while(true);
cnhzcy14 0:618ffeb1dfc5 67 }