This is sample program for Nucleo L152RE (and F401RE & F411RE) mbed-rtos. You need to modify mbed-src and mbed-rtos before compile it.

Dependencies:   mbed-rtos mbed-src SetRTC

Fork of GR-PEACH_test_on_rtos_works_well by Kenji Arai

Please refer below link.
/users/kenjiArai/notebook/necleo-l152re-rtos-sample-also-for-f401re--f411re-/

Revision:
10:1c0f58b9c048
Parent:
9:de986e74bd93
Child:
13:d0d1da1fae4c
diff -r de986e74bd93 -r 1c0f58b9c048 main.cpp
--- a/main.cpp	Thu Feb 12 07:53:25 2015 +0000
+++ b/main.cpp	Sat May 16 00:43:39 2015 +0000
@@ -1,12 +1,12 @@
 /*
  * mbed Application program for the mbed
- *      Test program for GR-PEACH
+ *      RTOS Test program for Nucleo L152RE
  *
  * Copyright (c) 2014,'15 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created: November  29th, 2014
- *      Revised: Feburary   8th, 2015
+ *      Revised: May       16th, 2015
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -16,84 +16,82 @@
  */
 
 //  Include ---------------------------------------------------------------------------------------
-#include    "mbed.h"
-#include    "rtos.h"
-#include    "L3GD20.h"
-#include    "LIS3DH.h"
-#include    "TextLCD.h"
+#include "mbed.h"
+#include "rtos.h"
+#include "SetRTC.h"     // Own lib. / RTC control
 
 //  Definition ------------------------------------------------------------------------------------
-#define USE_COM         // use Communication with PC(UART)
-#define USE_I2C_LCD
-#define USE_I2C_SENSOR
+#define DO_DEBUG        1
+
+#if DO_DEBUG
+#define DEBUG_LINE      printf("line:%d\r\n", __LINE__);
+#else
+#define DEBUG_LINE      {;}
+#endif
  
 // Com
-#ifdef  USE_COM
-#define BAUD(x)             pcx.baud(x)
-#define GETC(x)             pcx.getc(x)
-#define PUTC(x)             pcx.putc(x)
-#define PRINTF(...)         pcx.printf(__VA_ARGS__)
-#define READABLE(x)         pcx.readable(x)
+#if 1
+#define BAUD(x)         pc.baud(x)
+#define GETC(x)         pc.getc(x)
+#define PUTC(x)         pc.putc(x)
+#define PUTS(x)         pc.puts(x)
+#define PRINTF(...)     pc.printf(__VA_ARGS__)
+#define READABLE(x)     pc.readable(x)
 #else
-#define BAUD(x)             {;}
-#define GETC(x)             {;}
-#define PUTC(x)             {;}
-#define PRINTF(...)         {;}
-#define READABLE(x)         {;}
+#define BAUD(x)         {;}
+#define GETC(x)         {;}
+#define PUTC(x)         {;}
+#define PUTS(x)         {;}
+#define PRINTF(...)     {;}
+#define READABLE(x)     {;}
 #endif
 
+#if 0
 #define TIMEBASE        12000
 #define FIXED_STEPS     100
 
 #define PI              3.1415926536
 #define RAD_TO_DEG      57.29578
-#define TIME_BASE_S     0.02
 #define TIME_BASE_MS    ( TIME_BASE_S * 1000)
 #define RATE            0.1
+#endif
+
+#define TIME_BASE_S     0.02
+
+#define step_one        (0.0625f)
 
 //  Object ----------------------------------------------------------------------------------------
-// LED's
-DigitalOut LEDs[4] = {
-    DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4)
+// LED's !! DON'T USE LED1(PA_5) because PA_5 uses for DAC
+DigitalOut LEDs[3] = {
+    DigitalOut(PC_10), DigitalOut(PC_11), DigitalOut(PC_12)
 };
-// Swiches
-DigitalIn   USER_SWITCH[2] = {
-#if defined(TARGET_RZ_A1H)
-    DigitalIn(P6_0),  DigitalIn(P6_1)
-#elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
-    DigitalIn(PC_13),  DigitalIn(A0)
-#endif
-};
-
 // OS check
-#if defined(TARGET_RZ_A1H)
-DigitalOut task0(P1_8);
-DigitalOut task1(P1_9);
-#elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
-DigitalOut task0(A0);
-DigitalOut task1(A1);
+DigitalOut  task0(PC_2);
+DigitalOut  task1(PC_3);
+// Swiches
+DigitalIn   usr_sw(PC_13);
+// DAC to ADC
+#if defined(TARGET_NUCLEO_L152RE)
+AnalogOut   dac0(PA_4);
+AnalogOut   dac1(PA_5);
 #endif
-
+AnalogIn    adc1(A1);
+AnalogIn    adc3(A3);
+// I2C
+I2C         i2c1(D14,D15);      // SDA, SCL
 // com
-#ifdef USE_COM
-Serial      pcx(USBTX, USBRX);      // Communication with Host
-#endif
-I2C i2c(D14,D15);
-// Gyro
-L3GX_GYRO   gyro(i2c, L3GD20_V_CHIP_ADDR, L3GX_DR_95HZ, L3GX_BW_HI, L3GX_FS_250DPS);
-// Acc
-LIS3DH      acc(i2c, LIS3DH_G_CHIP_ADDR, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_8G);
-#ifdef USE_I2C_LCD
-// LCD
-TextLCD_I2C_N lcd0(&i2c, 0x7c, TextLCD::LCD16x2);  // LCD(Akizuki AQM0802A)
-#endif
+Serial      pc(USBTX, USBRX);   // Communication with Host
 // Mutex
+Mutex       uart_mutex; 
 Mutex       i2c_mutex; 
 
 //  RAM -------------------------------------------------------------------------------------------
 Queue<uint32_t, 2> queue0;
-float fa[3];    // Acc  0:X, 1:Y, 2:Z
-float fg[3];    // Gyro 0:X, 1:Y, 2:Z
+
+float fg[3];
+float fa[3];
+
+float analog[2];
 
 /* Mail */
 typedef struct {
@@ -108,13 +106,11 @@
 uint8_t show_flag1;
 uint32_t count;
 
-#if defined(TARGET_RZ_A1H)
-uint8_t big_data[4096*1024];
-uint32_t big_data_index = 0;
-#endif
-
 //  ROM / Constant data ---------------------------------------------------------------------------
-
+const float out_data[16] = {step_one * 15, step_one * 14, step_one * 13, step_one * 12, step_one * 11,
+                    step_one * 10, step_one *  9, step_one *  8, step_one *  7, step_one *  6,
+                    step_one *  5, step_one *  4, step_one *  3, step_one *  2, step_one *  1, 0};
+            
 //  Function prototypes ---------------------------------------------------------------------------
 
 //  Function prototypes ---------------------------------------------------------------------------
@@ -141,40 +137,32 @@
 }
 
 // Read switch status
-int read_sw(uint8_t n){
-    if (USER_SWITCH[n] == 0){   return 1;
-    } else {                    return 0;}
+int read_sw(){
+    if (usr_sw.read()){
+        return 0;
+    } else {
+        return 1;
+    }
 }
 
 // Monitor program
 void monitor(void const *args){
-    Thread::wait(1000);
+    Thread::wait(100);
     while (true){
+DEBUG_LINE
         mon();
     }
 }
 
 void watch_time (void const *args) {
-    uint32_t i = 0;
     time_t seconds;
     char buf[64];
-#if 0
-    struct tm t;
 
-    t.tm_year       = 15 + 100;
-    t.tm_mon        = 2 - 1;
-    t.tm_mday       = 7;
-    t.tm_hour       = 22;
-    t.tm_min        = 21;
-    t.tm_sec        = 20;
-    seconds = mktime(&t);
-    set_time(seconds);
-#endif
     while (true) {
         seconds = time(NULL);
         strftime(buf, 40, "%B %d,'%y, %H:%M:%S", localtime(&seconds));
         if (show_flag1){
-            printf("[TASK1] %s, No:%5d, Ticker:%10u\r\n",buf, i++,  us_ticker_read());
+//            printf("[TASK1] %s, No:%5d, Ticker:%10u\r\n",buf, i++,  us_ticker_read());
         }
         Thread::wait(1000);
     }
@@ -186,107 +174,136 @@
 }
 
 // Update sensor data
-void update_angle(void const *args){
+void update_sensor(void const *args){ // No need to connect I2C line
+    char cmd[2];
+    uint8_t i = 0;
+
+DEBUG_LINE
+    osDelay(1000);
+    i2c1.frequency(50000);
     while (true) {
         osEvent evt = queue0.get();
         // ---->lock
         i2c_mutex.lock();
-#ifdef USE_I2C_SENSOR
-        // read acceleration data from sensor
-        acc.read_data(fa);
-        // read gyroscope data from sensor
-        gyro.read_data(fg);
-#else
-        fa[0] = fa[1] = fa[2] = 1.11f;
-        fg[0] = fg[1] = fg[2] = 1.11f;
-#endif
+        cmd[0] = 0x55;
+        cmd[1] = 0xaa;
+        i2c1.write(0xa4, cmd, 2);
+        if (i2c1.read(0xa4, cmd, 2)){
+            fa[0] = fa[1] = fa[2] = 1.11f + (++i / 100);
+            fg[0] = fg[1] = fg[2] = 2.22f + (++i / 100);
+        } else {
+            fa[0] = fa[1] = fa[2] = -0.01f - (++i / 100);
+            fg[0] = fg[1] = fg[2] =  0.01f + (++i / 100);
+        }
         // <----unlock
         i2c_mutex.unlock();
         // debug
         task0 = !task0;
+        osDelay(1000);
     }
 }
 
 // Update sensor data
-void display(void const *args){
-    uint32_t n = 0;
-
+void adc(void const *args){
     while (true) {
-#ifdef USE_I2C_LCD
-        i2c_mutex.lock();
-        lcd0.locate(0, 0);    // 1st line top
-        lcd0.printf(" G=%4.1f ", sqrt(fa[0]*fa[0] + fa[1]*fa[1] + fa[2]*fa[2]));
-        lcd0.locate(0, 1);    // 2nd line top
-        lcd0.printf("%8d",n++);
-        i2c_mutex.unlock();
+#if defined(TARGET_NUCLEO_L152RE)
+        analog[0] = adc1.read();
+        analog[1] = adc3.read();
+#else
+        analog[0] = 1.2f;
+        analog[1] = 3.4f;
 #endif
-        Thread::wait(500);
-        // debug
+        osDelay(500);   // miliseconds
         task1 = !task1;
     }
 }
 
+// Update sensor data
+void pwm_and_dac(void const *args){
+    uint8_t i = 0;
+    while (true) {
+#if defined(TARGET_NUCLEO_L152RE)
+        dac0.write(out_data[i & 0x0f]);
+        dac1.write(out_data[i & 0x0f]);
+#endif
+        osDelay(500);   // miliseconds
+        i++;
+    }
+}
+
 // Thread definition
-osThreadDef(update_angle, osPriorityNormal, 1024);
+osThreadDef(update_sensor, osPriorityNormal, 1024);
 osThreadDef(monitor, osPriorityNormal, 1024);
-osThreadDef(display, osPriorityAboveNormal, 1024);
 osThreadDef(watch_time, osPriorityNormal, 1024);
+osThreadDef(adc, osPriorityAboveNormal, 1024);
+osThreadDef(pwm_and_dac, osPriorityAboveNormal, 1024);
 
 int main(void) {
-    PRINTF("\r\nstep1\r\n");
-    
+    osDelay(1000);  // wait 1sec
+//    PUTS("\x1b[2J\x1b[H");
+    PUTS("\r\nCreated on "__DATE__ " " __TIME__ " (UTC)\r\n""\r\n");
+DEBUG_LINE
+    if (SetRTC(0) == 1) {
+        PRINTF("Use External CLK (Good for RTC)\r\n");
+    } else {
+        PRINTF("Use Internal CLK (Bad for RTC)\r\n");
+    }
+DEBUG_LINE
+    time_enter_mode();
+DEBUG_LINE
     RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0);
     RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1);
     RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2);
-    RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3);
-
-    PRINTF("step2\r\n");    
+DEBUG_LINE   
     led_1_timer.start(2000);
     led_2_timer.start(1000);
     led_3_timer.start(500);
-    led_4_timer.start(250);
-
-    PRINTF("step3\r\n");    
+DEBUG_LINE     
     Thread thread(send_thread);
-
-    PRINTF("step4\r\n");
-
+DEBUG_LINE
     // IRQ
     Ticker ticker0;
     Ticker ticker1;
     ticker0.attach(queue_isr0, TIME_BASE_S);
-
- 
-    PRINTF("step5\r\n");
+DEBUG_LINE
+#if 0
+/// Create a thread and add it to Active Threads and set it to state READY
+osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument) {
+  if (__exceptional_mode()) return NULL;           // Not allowed in ISR
+  if ((__get_mode() != MODE_USR) && (os_running == 0)) {
+    // Privileged and not running
+    return   svcThreadCreate(thread_def, argument);
+  } else {
+    return __svcThreadCreate(thread_def, argument);
+  }
+}
+#endif
     // Starts threads
-    if (osThreadCreate(osThread(display), NULL) == NULL){
-        PRINTF("ERROR4\r\n");
+    if (osThreadCreate(osThread(monitor), NULL) == NULL){
+        PRINTF("ERROR/monitor() thread\r\n");
     }
-    if (osThreadCreate(osThread(monitor), NULL) == NULL){
-        PRINTF("ERROR3\r\n");
+DEBUG_LINE
+    if (osThreadCreate(osThread(update_sensor), NULL) == NULL){
+        PRINTF("ERROR/update_sensor() thread\r\n");
     }
-    if (osThreadCreate(osThread(update_angle), NULL) == NULL){
-        PRINTF("ERROR1\r\n");
-    }
+DEBUG_LINE
     if (osThreadCreate(osThread(watch_time), NULL) == NULL){
-        printf("ERROR5\r\n");
+        printf("ERROR/watch_time() thread\r\n");
+    }
+DEBUG_LINE
+    if (osThreadCreate(osThread(adc), NULL) == NULL){
+        PRINTF("ERROR/print4com() therad\r\n");
     }
-    // I2C LCD
-#ifdef USE_I2C_LCD
-    // ---->lock
-    i2c_mutex.lock();
-    lcd0.locate(0, 0);    // 1st line top
-    lcd0.printf("I2C test");
-    lcd0.locate(0, 1);    // 2nd line top
-    lcd0.puts(" JH1PJL ");
-    lcd0.setContrast(0x14);
-    // <----unlock
-    i2c_mutex.unlock();
-#endif
+DEBUG_LINE
+    if (osThreadCreate(osThread(pwm_and_dac), NULL) == NULL){
+        PRINTF("ERROR/print4com() therad\r\n");
+    }
+DEBUG_LINE
     count = 0;
-    PRINTF("step6\r\n");
+DEBUG_LINE
     while (true) {
         osEvent evt = mail_box.get();
+//DEBUG_LINE
         if (evt.status == osEventMail) {
             mail_t *mail = (mail_t*)evt.value.p;
             if (show_flag0){
@@ -298,23 +315,16 @@
             }
             mail_box.free(mail);
         }
-#if defined(TARGET_RZ_A1H)
-        for (uint32_t i = 0; i < 100; i++){
-            big_data[big_data_index] = ++big_data_index;
-            if (big_data_index == (4096 * 1024)){
-                big_data_index = 0;
-            }
-        }
-#endif
     }
 }
 
+// rtos error routine
 void mbed_die(void) {
     PRINTF("Error, came from os_error()!\r\n");
     gpio_t led_1; gpio_init_out(&led_1, LED1);
-    gpio_t led_2; gpio_init_out(&led_2, LED2);
-    gpio_t led_3; gpio_init_out(&led_3, LED3);
-    gpio_t led_4; gpio_init_out(&led_4, LED4);
+    gpio_t led_2; gpio_init_out(&led_2, PC_10);
+    gpio_t led_3; gpio_init_out(&led_3, PC_11);
+    gpio_t led_4; gpio_init_out(&led_4, PC_12);
 
     while (1) {
         gpio_write(&led_1, 1);