Simple test program to get familiar with functionality of MBED RTOS on ST Nucleo-F411RE. Tasks for LED blinking, user button, temperature measurement with DS1620, temperature measurement with internal temperature sensor of ST32F411RE, ultrasonic distance measurement and displaying result on 16x2 TextLCD.

Dependencies:   DS1620_improved TextLCD_improved mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
dzoni
Date:
Fri Dec 18 15:23:21 2015 +0000
Parent:
16:cfb774e3e7fc
Child:
18:be0130c42925
Commit message:
Bugfixes. Works but freezes after some time. Button task requires rewrite (InterruptIn).

Changed in this revision

todo.txt Show annotated file Show diff for this revision Revisions of this file
tsk_button.cpp Show annotated file Show diff for this revision Revisions of this file
tsk_display.cpp Show annotated file Show diff for this revision Revisions of this file
tsk_dist.cpp Show annotated file Show diff for this revision Revisions of this file
tsk_dist.h Show annotated file Show diff for this revision Revisions of this file
tsk_inttemp.cpp Show annotated file Show diff for this revision Revisions of this file
tsk_inttemp.h Show annotated file Show diff for this revision Revisions of this file
tsk_led.cpp Show annotated file Show diff for this revision Revisions of this file
tsk_led.h Show annotated file Show diff for this revision Revisions of this file
tsk_main.cpp Show annotated file Show diff for this revision Revisions of this file
tsk_main.h Show annotated file Show diff for this revision Revisions of this file
tsk_temp.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/todo.txt	Wed Dec 16 22:37:56 2015 +0000
+++ b/todo.txt	Fri Dec 18 15:23:21 2015 +0000
@@ -1,1 +1,2 @@
 1. Change thread principle from Thread::wait() to FSM and its evaluation in given times and signals to particular threads.
+2. Distance measurement: signal + mail, add timeout
--- a/tsk_button.cpp	Wed Dec 16 22:37:56 2015 +0000
+++ b/tsk_button.cpp	Fri Dec 18 15:23:21 2015 +0000
@@ -18,7 +18,7 @@
             led_flash_times.off_time = temp;
         }
         
-        Thread::wait(250);
+        Thread::wait(500);
     }
 }
 
--- a/tsk_display.cpp	Wed Dec 16 22:37:56 2015 +0000
+++ b/tsk_display.cpp	Fri Dec 18 15:23:21 2015 +0000
@@ -3,10 +3,11 @@
 
 #include "TextLCD.h"
 
+#include "tsk_main.h"
 #include "tsk_display.h"
 #include "tsk_temp.h"
+#include "tsk_inttemp.h"
 #include "tsk_dist.h"
-#include "tsk_main.h"
 
 static TextLCD *lcd;
 
@@ -14,10 +15,8 @@
 
     lcd = new TextLCD(PA_8, PA_7, PA_9, PA_1, PB_5, PA_10, TextLCD::LCD16x2);
 
+    wait_ms(250);
     lcd->cls();
-    lcd->printf("Row 1");
-    lcd->locate(0, 1);    
-    lcd->printf("Row 2");
        
     return 1;
 }
@@ -32,14 +31,36 @@
 
 
 void disp_thread(void const *args) {
+    float dist;
+    float temp;
+    float intTemp;
+    uint32_t dist_raw;
+    uint32_t temp_raw;
+    
+    while (true) {
+        mutexDist.lock();
+        dist = dist_data.distance;
+        dist_raw = dist_data.timerTicks;
+        mutexDist.unlock();
 
-    while (true) {
+        mutexTemp.lock();
+        temp = temp_data.temperature;
+        temp_raw = temp_data.temp_raw;
+        mutexTemp.unlock();
+
+        mutexIntTemp.lock();
+        intTemp = int_temp_data.temperature;
+        mutexIntTemp.unlock();
+
+        uiCnt += 2;
+
         lcd->cls();
-        lcd->printf("Raw:%3u % 5.0f mm", temp_data.temp_raw, dist_data.distance);
-//        uiCnt += 2;
+//        lcd->printf("%4.2fmm (%4u)", dist, dist_raw);
+        lcd->printf("%4.2fmm (%4u)", dist, uiCnt);
         lcd->locate(0, 1);    
-        lcd->printf("Float: %3.2f%cC", temp_data.temperature, 0xdf);
+//        lcd->printf("%3.2f%cC (%3u)", temp, 0xdf, temp_raw);
+        lcd->printf("%3.1f%cC (%2.1f)", temp, 0xdf, intTemp);
 
-        Thread::wait(1896);
+        Thread::wait(2000);
     }
 }
\ No newline at end of file
--- a/tsk_dist.cpp	Wed Dec 16 22:37:56 2015 +0000
+++ b/tsk_dist.cpp	Fri Dec 18 15:23:21 2015 +0000
@@ -1,16 +1,26 @@
 #include "mbed.h"
 #include "rtos.h"
 
+#include "tsk_main.h"
 #include "tsk_dist.h"
 
 
-struct dist_data_struct dist_data = { 10000, 10.0f };
-
+struct dist_data_struct dist_data;
 
 static DigitalOut  trigDist(PB_9);
 static InterruptIn echoDist(PA_6);
 
-static Timer   timer;
+static Timer timer;
+
+uint32_t initDist(void const *args) {
+    
+    dist_data.distance = 0.0f;
+    dist_data.timerTicks = 0;
+
+    trigDist = 0;
+    
+    return 1;
+}
 
 static void rising(void) {
     timer.start();
@@ -21,10 +31,12 @@
     echoDist.rise(0);
     echoDist.fall(0);
 
+//    mutexDist.lock();
     dist_data.timerTicks = timer.read_us();
         
     // 340 ms-1
     dist_data.distance = (timer.read()/2.0f - 0.0f)*340.0f*1000.0f;
+//    mutexDist.unlock();
 
     // Vynuluj timer
     timer.reset();
@@ -39,8 +51,10 @@
         trigDist = 0;
     
         if (echoDist != 0) {
+            mutexDist.lock();
             dist_data.timerTicks = 0;
             dist_data.distance = 0.0f;
+            mutexDist.unlock();
         } else {
             // Vynuluj timer
             timer.stop();
@@ -51,6 +65,6 @@
             echoDist.fall(&falling);
         }
         
-        Thread::wait(500);
+        Thread::wait(1000);
     }
 }
--- a/tsk_dist.h	Wed Dec 16 22:37:56 2015 +0000
+++ b/tsk_dist.h	Fri Dec 18 15:23:21 2015 +0000
@@ -8,6 +8,7 @@
 
 extern struct dist_data_struct dist_data;
 
+extern uint32_t initDist(void const *args);
 extern void dist_thread(void const *args);
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tsk_inttemp.cpp	Fri Dec 18 15:23:21 2015 +0000
@@ -0,0 +1,73 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "stm32f411xe.h"
+
+#include "tsk_main.h"
+#include "tsk_inttemp.h"
+
+
+struct int_temp_data_struct int_temp_data = { 0 };
+
+
+uint32_t initIntTemp(void const *args) {
+    
+    int_temp_data.temperature = 0.0f;
+    
+    //enable ADC1 clock
+    SET_BIT(RCC->APB2ENR, RCC_APB2ENR_ADC1EN);
+
+    // ADC on
+    SET_BIT(ADC1->CR2, ADC_CR2_ADON);
+    CLEAR_BIT(ADC1->CR2, ADC_CR2_CONT);
+    
+    // Temp sensor on
+    CLEAR_BIT(ADC->CCR, ADC_CCR_VBATE);
+    SET_BIT(ADC->CCR, ADC_CCR_TSVREFE);
+    
+    CLEAR_BIT(ADC1->CR1, ADC_CR1_RES);
+    
+    return 1;
+}
+
+static uint16_t getADCTemp() {
+    // Set channel
+    ADC1->SQR1 &= ~ADC_SQR1_L;  // Count = 1
+    ADC1->SQR3 |= ADC_SQR3_SQ1 & 18;
+
+    // Start conversion
+    SET_BIT(ADC1->CR2, ADC_CR2_SWSTART);
+    
+    // Wait for completion
+    while (!READ_BIT(ADC1->SR, ADC_SR_EOC))
+        ;
+        
+    // Return result
+    return READ_REG(ADC1->DR);
+}
+
+
+void inttemp_thread(void const *args) {
+
+    const float     V25 = 943.3212f;// when V25=1.41V at ref 3.3V (0,76V)
+    const float     Avg_Slope = 3.1030303f; //when avg_slope=4.3mV/C at ref 3.3V  (2.5mV/C)
+    const uint16_t  bufferSize = 4;
+
+    float       temp;
+    uint16_t    i;
+    uint16_t    uiCnt = 0;
+
+    
+    while (true) {
+        for (i = 0, temp = 0.0f; i < bufferSize; i++) {
+            temp += ((float)getADCTemp() - V25) / Avg_Slope + 48.2f;
+            Thread::wait(100);
+        }
+        temp /= (float)bufferSize;
+     
+        mutexIntTemp.lock();
+        int_temp_data.temperature = temp;
+        mutexIntTemp.unlock();
+        
+        Thread::wait(5000);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tsk_inttemp.h	Fri Dec 18 15:23:21 2015 +0000
@@ -0,0 +1,13 @@
+#ifndef TSK_INTTEMP_H
+#define TSK_INTTEMP_H
+
+struct int_temp_data_struct {
+    float       temperature;
+};
+
+extern struct int_temp_data_struct int_temp_data;
+
+extern uint32_t initIntTemp(void const *args);
+extern void inttemp_thread(void const *args);
+
+#endif
--- a/tsk_led.cpp	Wed Dec 16 22:37:56 2015 +0000
+++ b/tsk_led.cpp	Fri Dec 18 15:23:21 2015 +0000
@@ -4,11 +4,18 @@
 #include "tsk_led.h"
 
 
-struct led_flash_times_struct led_flash_times = { 100, 2500 };
+struct led_flash_times_struct led_flash_times;
 
 
 static DigitalOut  led2(LED2);
 
+uint32_t initLed(void const *args) {
+    
+    led_flash_times.on_time = 100;
+    led_flash_times.off_time = 2500;
+   
+    return 1;
+}
 
 void led_thread(void const *args) {
 
--- a/tsk_led.h	Wed Dec 16 22:37:56 2015 +0000
+++ b/tsk_led.h	Fri Dec 18 15:23:21 2015 +0000
@@ -8,6 +8,7 @@
 
 extern struct led_flash_times_struct led_flash_times;
 
+extern uint32_t initLed(void const *args);
 extern void led_thread(void const *args);
 
 #endif
--- a/tsk_main.cpp	Wed Dec 16 22:37:56 2015 +0000
+++ b/tsk_main.cpp	Fri Dec 18 15:23:21 2015 +0000
@@ -6,11 +6,14 @@
 #include "tsk_dist.h"
 #include "tsk_led.h"
 #include "tsk_temp.h"
+#include "tsk_inttemp.h"
 
 #define TASKS_NUMBER    (sizeof(taskList)/sizeof(taskList[0]))
 
 uint32_t uiCnt = 0;
-
+Mutex mutexDist;
+Mutex mutexTemp;
+Mutex mutexIntTemp;
 
 struct task_definition_struct {
     void(*task)(void const *args);
@@ -26,11 +29,12 @@
 };
 
 static struct task_definition_struct taskList[] = {
-    { dist_thread,   NULL,            NULL, NULL, osPriorityNormal, 0, NULL, 0 },
-    { temp_thread,   &initDS1620Temp, NULL, NULL, osPriorityLow,    0, NULL, 0 },
-    { button_thread, NULL,            NULL, NULL, osPriorityNormal, 0, NULL, 0 },
-    { led_thread,    NULL,            NULL, NULL, osPriorityNormal, 0, NULL, 0 },
-    { disp_thread,   &initDisplay,    NULL, NULL, osPriorityNormal, 0, NULL, 0 },
+//    { dist_thread,    &initDist,       NULL, NULL, osPriorityNormal, 100, NULL, 0 },
+    { temp_thread,    &initDS1620Temp, NULL, NULL, osPriorityNormal, 100, NULL, 0 },
+    { inttemp_thread, &initIntTemp,    NULL, NULL, osPriorityNormal, 100, NULL, 0 },
+//    { button_thread,  NULL,            NULL, NULL, osPriorityNormal, 0, NULL, 0 },
+    { led_thread,     &initLed,        NULL, NULL, osPriorityNormal, 100, NULL, 0 },
+    { disp_thread,    &initDisplay,    NULL, NULL, osPriorityNormal, 100, NULL, 0 },
 };
 
 static uint32_t initTasks(void) {
@@ -60,8 +64,6 @@
 
 int main() {
     
-    Thread::wait(1000);
-
     initTasks();
 
     Thread::wait(osWaitForever);
--- a/tsk_main.h	Wed Dec 16 22:37:56 2015 +0000
+++ b/tsk_main.h	Fri Dec 18 15:23:21 2015 +0000
@@ -1,6 +1,10 @@
 #ifndef TSK_MAIN_H
 #define TSK_MAIN_H
 
+extern Mutex mutexDist;
+extern Mutex mutexTemp;
+extern Mutex mutexIntTemp;
+
 extern uint32_t uiCnt;
 
 #endif
--- a/tsk_temp.cpp	Wed Dec 16 22:37:56 2015 +0000
+++ b/tsk_temp.cpp	Fri Dec 18 15:23:21 2015 +0000
@@ -3,17 +3,21 @@
 
 #include "DS1620.h"
 
+#include "tsk_main.h"
 #include "tsk_temp.h"
 
 
-struct temp_data_struct temp_data = { 20, 10.0f };
+struct temp_data_struct temp_data;
 
 static  DS1620 ds1620Sensor(PB_4, PB_10, PB_3);
 
 
 uint32_t initDS1620Temp(void const *args) {
     
-    ds1620Sensor.setSerialClockFrequency(freq500k);
+    temp_data.temperature = 10.0f;
+    temp_data.temp_raw = 10;
+    
+    ds1620Sensor.setSerialClockFrequency(freq250k);
     
     if ((ds1620Sensor.readConfig() & 0x03) != 0x03) {
         ds1620Sensor.writeConfig(0x03);
@@ -28,6 +32,9 @@
 
 void temp_thread(void const *args) {
 
+    float temp;
+    uint32_t temp_raw;
+    
     while (true) {
         ds1620Sensor.startConversion();
     
@@ -36,7 +43,14 @@
         while (!(ds1620Sensor.readConfig() & 0x80))
             Thread::wait(10);
                 
-        temp_data.temperature = ds1620Sensor.getHighResolutionTemperature();
-        temp_data.temp_raw = ds1620Sensor.readTemperatureRaw();
+        temp = ds1620Sensor.getHighResolutionTemperature();
+        temp_raw = ds1620Sensor.readTemperatureRaw();
+
+        mutexTemp.lock();
+        temp_data.temperature = temp;
+        temp_data.temp_raw = temp_raw;
+        mutexTemp.unlock();
+        
+        Thread::wait(1250);
     }
 }