aboutsummaryrefslogtreecommitdiffstats
path: root/src/fsm
diff options
context:
space:
mode:
authorMaximilien Cuony <maximilien@theglu.org>2016-04-18 20:46:53 +0200
committerMaximilien Cuony <maximilien@theglu.org>2016-04-18 20:46:53 +0200
commita66ca57c01c3ced4c4ada0dc09040e8eb620efaa (patch)
tree0f260e1b2036f3847ef8e4d4aea621cd382ec749 /src/fsm
parent5927d922ee95f3e3eeed99302b25094aeec1d622 (diff)
downloadglutte-o-matic-a66ca57c01c3ced4c4ada0dc09040e8eb620efaa.tar.gz
glutte-o-matic-a66ca57c01c3ced4c4ada0dc09040e8eb620efaa.tar.bz2
glutte-o-matic-a66ca57c01c3ced4c4ada0dc09040e8eb620efaa.zip
Temperature
Diffstat (limited to 'src/fsm')
-rw-r--r--src/fsm/delay.c59
-rw-r--r--src/fsm/delay.h33
-rw-r--r--src/fsm/main.c27
-rw-r--r--src/fsm/temperature.c142
-rw-r--r--src/fsm/temperature.h49
l---------src/fsm/tm_stm32f4_ds18b20.c1
l---------src/fsm/tm_stm32f4_ds18b20.h1
l---------src/fsm/tm_stm32f4_onewire.c1
l---------src/fsm/tm_stm32f4_onewire.h1
9 files changed, 299 insertions, 15 deletions
diff --git a/src/fsm/delay.c b/src/fsm/delay.c
new file mode 100644
index 0000000..8c9d24c
--- /dev/null
+++ b/src/fsm/delay.c
@@ -0,0 +1,59 @@
+
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Matthias P. Braendli, Maximilien Cuony
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+
+#include <stdint.h>
+#include "stm32f4xx_conf.h"
+#include "stm32f4xx.h"
+
+#ifndef __DELAY_H
+#define __DELAY_H
+
+
+uint32_t delay_multiplier = 1;
+
+void delay_us(uint32_t micros) {
+
+ micros = micros * delay_multiplier - 10;
+ while (micros--);
+}
+
+void delay_ms(uint32_t millis) {
+
+ for (int i = 0; i < 1000; i++) {
+ delay_us(millis);
+ }
+}
+
+void delay_init() {
+
+ RCC_ClocksTypeDef RCC_Clocks;
+ RCC_GetClocksFreq(&RCC_Clocks);
+
+ delay_multiplier = RCC_Clocks.HCLK_Frequency / 4000000;
+
+}
+
+#endif
diff --git a/src/fsm/delay.h b/src/fsm/delay.h
new file mode 100644
index 0000000..73ed669
--- /dev/null
+++ b/src/fsm/delay.h
@@ -0,0 +1,33 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Matthias P. Braendli, Maximilien Cuony
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+
+// High-precisions delay (with approximations)
+
+
+// Thoses functions works only if interupts are disabled
+void delay_us(uint32_t micros);
+void delay_ms(uint32_t millis);
+
+void delay_init();
diff --git a/src/fsm/main.c b/src/fsm/main.c
index b7da4e6..94c15e0 100644
--- a/src/fsm/main.c
+++ b/src/fsm/main.c
@@ -41,9 +41,8 @@
#include "fsm.h"
#include "common.h"
#include "usart.h"
-#if DS18B20_ENABLED
-#include "ds18b20.h"
-#endif
+#include "delay.h"
+#include "temperature.h"
#include "vc.h"
#define GPIOD_BOARD_LED_GREEN GPIO_Pin_12
@@ -75,6 +74,7 @@ void vApplicationStackOverflowHook( TaskHandle_t xTask,
int main(void) {
init();
+ delay_init();
usart_init();
usart_debug_puts("\r\n******* glutt-o-matique version " GIT_VERSION " *******\r\n");
@@ -123,10 +123,8 @@ static void launcher_task(void *pvParameters)
usart_debug_puts("GPS init\r\n");
gps_init();
-#if DS18B20_ENABLED
usart_debug_puts("DS18B20 init\r\n");
- ds18b20_init();
-#endif
+ temperature_init();
usart_debug_puts("TaskButton init\r\n");
@@ -219,15 +217,14 @@ static void detect_button_press(void *pvParameters)
last_pin_high_count != pin_high_count) {
tm_trigger = 1;
usart_debug_puts("Bouton bleu\r\n");
-#if DS18B20_ENABLED
- float temp = 0.0f;
- if (ds18b20_gettemp(&temp)) {
- usart_debug("Temperature %d\r\n", temp);
- }
-#else
- if (0) {}
-#endif
- else {
+
+ if (temperature_valid()) {
+
+ float temp = temperature_get();
+
+ usart_debug("Temperature %f\r\n", temp);
+
+ } else {
usart_debug_puts("No temp\r\n");
}
}
diff --git a/src/fsm/temperature.c b/src/fsm/temperature.c
new file mode 100644
index 0000000..84c537e
--- /dev/null
+++ b/src/fsm/temperature.c
@@ -0,0 +1,142 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Matthias P. Braendli, Maximilien Cuony
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+#include "stm32f4xx_conf.h"
+#include "stm32f4xx.h"
+#include "FreeRTOS.h"
+#include "FreeRTOSConfig.h"
+#include "task.h"
+#include "common.h"
+#include "tm_stm32f4_ds18b20.h"
+#include "tm_stm32f4_onewire.h"
+#include "temperature.h"
+#include "delay.h"
+
+
+float _temperature_last_value;
+int _temperature_valid;
+
+const TickType_t _temperature_delay = 60000 / portTICK_PERIOD_MS; // 60s
+
+static TM_OneWire_t tm_onewire;
+
+
+void ds18b20_init() {
+ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
+
+ GPIO_InitTypeDef GPIO_InitStructure;
+ GPIO_InitStructure.GPIO_Pin = TEMPERATURE_ONEWIRE_PIN;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
+ GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
+ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
+ GPIO_Init(GPIOA, &GPIO_InitStructure);
+
+ vTaskSuspendAll();
+ TM_OneWire_Init(&tm_onewire, GPIOA, TEMPERATURE_ONEWIRE_PIN);
+ xTaskResumeAll();
+}
+
+int ds18b20_gettemp_one(float *temperature) {
+
+ vTaskSuspendAll();
+
+ uint8_t rom_addr[8];
+
+ TM_DS18B20_StartAll(&tm_onewire);
+ int status = TM_OneWire_First(&tm_onewire);
+ if (status) {
+ //Save ROM number from device
+ TM_OneWire_GetFullROM(&tm_onewire, rom_addr);
+ TM_DS18B20_Start(&tm_onewire, rom_addr);
+
+ // The sensor needs time to do the conversion
+ xTaskResumeAll();
+ delay_ms(100);
+ vTaskSuspendAll();
+ status = TM_DS18B20_Read(&tm_onewire, rom_addr, temperature);
+ }
+
+ xTaskResumeAll();
+
+ return status;
+}
+
+int ds18b20_gettemp(float *temperature) {
+ int status;
+ for (int i = 0; i < 10; i++) {
+ status = ds18b20_gettemp_one(temperature);
+
+ if (status) {
+ break;
+ }
+ delay_ms(5);
+ }
+ return status;
+}
+
+
+static void temperature_task(void *pvParameters) {
+
+ while (1) {
+
+ if (!_temperature_valid) {
+ ds18b20_init();
+ }
+
+ if (ds18b20_gettemp(&_temperature_last_value)) {
+ _temperature_valid = 1;
+ } else {
+ _temperature_valid = 0;
+ }
+
+ vTaskDelay(_temperature_delay);
+
+ }
+}
+
+void temperature_init() {
+
+ xTaskCreate(
+ temperature_task,
+ "TaskTemperature",
+ 4*configMINIMAL_STACK_SIZE,
+ (void*) NULL,
+ tskIDLE_PRIORITY + 2UL,
+ NULL);
+}
+
+// Return the current temperature
+float temperature_get() {
+ if (_temperature_valid) {
+ return _temperature_last_value;
+ } else {
+ return 0.0f;
+ }
+}
+
+// Return 1 if the temperature is valid
+int temperature_valid() {
+ return _temperature_valid;
+}
diff --git a/src/fsm/temperature.h b/src/fsm/temperature.h
new file mode 100644
index 0000000..56145f2
--- /dev/null
+++ b/src/fsm/temperature.h
@@ -0,0 +1,49 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Matthias P. Braendli, Maximilien Cuony
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+*/
+
+#include <stdint.h>
+
+#ifndef __TEMPERATURE_H
+#define __TEMPERATURE_H
+
+/* Setup DS18B20 and report temperature */
+
+/* On wire connection: PA1
+ */
+
+
+#define TEMPERATURE_ONEWIRE_PIN GPIO_Pin_1
+
+
+// Setup communication and GPS receiver
+void temperature_init();
+
+// Return 1 if the temperature is valid
+int temperature_valid();
+
+// Get current temperature
+float temperature_get();
+
+#endif // __TEMPERATURE_H
+
diff --git a/src/fsm/tm_stm32f4_ds18b20.c b/src/fsm/tm_stm32f4_ds18b20.c
new file mode 120000
index 0000000..1dca5f5
--- /dev/null
+++ b/src/fsm/tm_stm32f4_ds18b20.c
@@ -0,0 +1 @@
+../ds18b20/tm_stm32f4_ds18b20.c \ No newline at end of file
diff --git a/src/fsm/tm_stm32f4_ds18b20.h b/src/fsm/tm_stm32f4_ds18b20.h
new file mode 120000
index 0000000..6b8bae8
--- /dev/null
+++ b/src/fsm/tm_stm32f4_ds18b20.h
@@ -0,0 +1 @@
+../ds18b20/tm_stm32f4_ds18b20.h \ No newline at end of file
diff --git a/src/fsm/tm_stm32f4_onewire.c b/src/fsm/tm_stm32f4_onewire.c
new file mode 120000
index 0000000..21e6c82
--- /dev/null
+++ b/src/fsm/tm_stm32f4_onewire.c
@@ -0,0 +1 @@
+../ds18b20/tm_stm32f4_onewire.c \ No newline at end of file
diff --git a/src/fsm/tm_stm32f4_onewire.h b/src/fsm/tm_stm32f4_onewire.h
new file mode 120000
index 0000000..1779648
--- /dev/null
+++ b/src/fsm/tm_stm32f4_onewire.h
@@ -0,0 +1 @@
+../ds18b20/tm_stm32f4_onewire.h \ No newline at end of file