First SPI/SDcard & UART tests

Dependencies:   SDFileSystem mbed-rtos mbed cmsis_rtos_demo_thread

Fork of cmsis_rtos_demo_thread by Ye Cheng

Revision:
2:23757aa762a7
Parent:
1:cbda4d713dc8
Child:
3:9fa442c3fd27
--- a/main.cpp	Sun Jan 06 15:03:57 2013 +0000
+++ b/main.cpp	Mon Sep 29 23:00:24 2014 +0000
@@ -1,99 +1,67 @@
-/******************************************************************************
-* DESCRIPTION:
-*   A "hello world" CMSIS RTOS program which demonstrates another safe way
-*   to pass arguments to threads during thread creation.  In this case,
-*   a structure is used to pass multiple arguments.
-* AUTHOR: Blaise Barney
-* MODIFICATOR: (Simon) CHENG Ye
-* LAST REVISED: 06/JAN/2013
-******************************************************************************/
-
 #include "mbed.h"
 #include "cmsis_os.h"
-
-#define NUM_THREADS 8
-
-Serial debug(USBTX, USBRX);     // For mbed debug by serial port.
-char *messages[NUM_THREADS];
+#include "SDFileSystem.h"
 
-struct thread_data
-{
-    int thread_id;
-    int  sum;
-    char *message;
-};
-struct thread_data thread_data_array[NUM_THREADS];
+DigitalOut led1(D7);
+DigitalOut led2(PD_2);
+Serial uart1(D8, D2);                        /* UART1: Modbus           */
+Serial uart2(D1, D0);                        /* UART2: pc(USBTX, USBRX) */
+                                             /* (i.e.) pc(D1, D0)       */
+SDFileSystem sd(D11, D12, D13, D10, "sd");   /* MOSI, MISO, SCK, CS     */
+FILE *fp;
 
-void PrintHello(void const *threadarg)
-{
-    int taskid, sum;
-    char *hello_msg;
-    struct thread_data *my_data;
-    osThreadId id;
+void led2_thread(void const *args){
+    while (true) {
+        led2 = !led2;
+        osDelay(1000);
+    }
+}
 
-    osDelay(1000);
-    my_data = (struct thread_data *) threadarg;
-    taskid = my_data->thread_id;
-    sum = my_data->sum;
-    hello_msg = my_data->message;
-    printf("Thread %d: %s  Sum=%d\n", taskid, hello_msg, sum);
-    id = osThreadGetId();
-    osThreadTerminate(id);
+void uart1_thread(const void *args){
+    uart1.baud(9600);
+    while(true){
+        uart1.putc(uart1.getc());
+    }
+}
+void uart2_thread(const void *args){
+    uart2.baud(9600);
+    while(true){
+        uart2.putc(uart2.getc());
+    }
 }
 
-void t0(void const *argument) {PrintHello(argument);}
-osThreadDef(t0, osPriorityNormal, DEFAULT_STACK_SIZE);
-
-void t1(void const *argument) {PrintHello(argument);}
-osThreadDef(t1, osPriorityNormal, DEFAULT_STACK_SIZE);
-
-void t2(void const *argument) {PrintHello(argument);}
-osThreadDef(t2, osPriorityNormal, DEFAULT_STACK_SIZE);
-
-void t3(void const *argument) {PrintHello(argument);}
-osThreadDef(t3, osPriorityNormal, DEFAULT_STACK_SIZE);
+osThreadDef(uart1_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
+osThreadDef(uart2_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
+osThreadDef(led2_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
 
-void t4(void const *argument) {PrintHello(argument);}
-osThreadDef(t4, osPriorityNormal, DEFAULT_STACK_SIZE);
-
-void t5(void const *argument) {PrintHello(argument);}
-osThreadDef(t5, osPriorityNormal, DEFAULT_STACK_SIZE);
-
-void t6(void const *argument) {PrintHello(argument);}
-osThreadDef(t6, osPriorityNormal, DEFAULT_STACK_SIZE);
-
-void t7(void const *argument) {PrintHello(argument);}
-osThreadDef(t7, osPriorityNormal, DEFAULT_STACK_SIZE);
+int main() {
+    osThreadCreate(osThread(led2_thread), NULL);
+    osThreadCreate(osThread(uart1_thread), NULL);
 
-int main() 
-{
-    debug.baud(57600);
-    int t, sum;
-
-    sum=0;
-    messages[0] = "English: Hello World!";
-    messages[1] = "French: Bonjour, le monde!";
-    messages[2] = "Spanish: Hola al mundo";
-    messages[3] = "Klingon: Nuq neH!";
-    messages[4] = "German: Guten Tag, Welt!";
-    messages[5] = "Russian: Zdravstvytye, mir!";
-    messages[6] = "Japan: Sekai e konnichiwa!";
-    messages[7] = "Latin: Orbis, te saluto!";
-
-    for(t=0;t<NUM_THREADS;t++) 
-    {
-        sum = sum + t;
-        thread_data_array[t].thread_id = t;
-        thread_data_array[t].sum = sum;
-        thread_data_array[t].message = messages[t];
-        printf("Creating thread %d\n", t);
-        if(t==0) osThreadCreate(osThread(t0), (void *)&thread_data_array[t]);
-        if(t==1) osThreadCreate(osThread(t1), (void *)&thread_data_array[t]);
-        if(t==2) osThreadCreate(osThread(t2), (void *)&thread_data_array[t]);
-        if(t==3) osThreadCreate(osThread(t3), (void *)&thread_data_array[t]);
-        if(t==4) osThreadCreate(osThread(t4), (void *)&thread_data_array[t]);
-        if(t==5) osThreadCreate(osThread(t5), (void *)&thread_data_array[t]);
-        if(t==6) osThreadCreate(osThread(t6), (void *)&thread_data_array[t]);
-        if(t==7) osThreadCreate(osThread(t7), (void *)&thread_data_array[t]);
+    
+    wait(1);
+    uart2.printf("\r\n");
+    wait(1);
+    uart2.printf("Initializing...\r\n");
+    
+    fp = fopen("/sd/hello.txt", "r");
+    if (fp != NULL) {
+        fclose(fp);
+        remove("/sd/hello.txt");
+        uart2.printf("Remove an existing file with the same name\r\n");
     }
+    
+    fp = fopen("/sd/hello.txt", "w");
+    if (fp == NULL) {
+        uart2.printf("Unable to write the file\r\n");
+    } else {
+        fprintf(fp, "mbed SDCard application!");
+        fclose(fp);
+        uart2.printf("File successfully written!\r\n");
+    }
+    
+    osThreadCreate(osThread(uart2_thread), NULL);
+        
+    /* spin and spin */    
+    while(true);
 }