From a66ca57c01c3ced4c4ada0dc09040e8eb620efaa Mon Sep 17 00:00:00 2001 From: Maximilien Cuony Date: Mon, 18 Apr 2016 20:46:53 +0200 Subject: Temperature --- src/ds18b20/ds18b20.c | 69 ------------------- src/ds18b20/ds18b20.h | 39 ----------- src/ds18b20/tm_stm32f4_onewire.c | 7 +- src/fsm/delay.c | 59 ++++++++++++++++ src/fsm/delay.h | 33 +++++++++ src/fsm/main.c | 27 ++++---- src/fsm/temperature.c | 142 +++++++++++++++++++++++++++++++++++++++ src/fsm/temperature.h | 49 ++++++++++++++ src/fsm/tm_stm32f4_ds18b20.c | 1 + src/fsm/tm_stm32f4_ds18b20.h | 1 + src/fsm/tm_stm32f4_onewire.c | 1 + src/fsm/tm_stm32f4_onewire.h | 1 + 12 files changed, 302 insertions(+), 127 deletions(-) delete mode 100644 src/ds18b20/ds18b20.c delete mode 100644 src/ds18b20/ds18b20.h create mode 100644 src/fsm/delay.c create mode 100644 src/fsm/delay.h create mode 100644 src/fsm/temperature.c create mode 100644 src/fsm/temperature.h create mode 120000 src/fsm/tm_stm32f4_ds18b20.c create mode 120000 src/fsm/tm_stm32f4_ds18b20.h create mode 120000 src/fsm/tm_stm32f4_onewire.c create mode 120000 src/fsm/tm_stm32f4_onewire.h (limited to 'src') diff --git a/src/ds18b20/ds18b20.c b/src/ds18b20/ds18b20.c deleted file mode 100644 index 7470f9d..0000000 --- a/src/ds18b20/ds18b20.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2015 Matthias P. Braendli - * - * 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 "ds18b20.h" -#include "tm_stm32f4_ds18b20.h" -#include "tm_stm32f4_onewire.h" -#include "stm32f4xx.h" -#include "FreeRTOS.h" -#include "task.h" - -static TM_OneWire_t tm_onewire; - -void ds18b20_init() -{ - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); - - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.GPIO_Pin = 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); - - TM_OneWire_Init(&tm_onewire, GPIOA, ONEWIRE_PIN); -} - -int ds18b20_gettemp(float *temperature) -{ - 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 - vTaskDelay(100 / portTICK_RATE_MS); - - status = TM_DS18B20_Read(&tm_onewire, rom_addr, temperature); - } - - return status; -} - diff --git a/src/ds18b20/ds18b20.h b/src/ds18b20/ds18b20.h deleted file mode 100644 index defdaf4..0000000 --- a/src/ds18b20/ds18b20.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2015 Matthias P. Braendli - * - * 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. -*/ - -#pragma once - -/* See pio.txt for PIO allocation details */ - -/* On GPIO A */ -#define ONEWIRE_PIN GPIO_Pin_1 // PA1 - -void ds18b20_init(void); - -/* Measure temperature measured by DS18B20 in degrees C. - * - * Returns 1 on success, 0 on failure - */ -int ds18b20_gettemp(float* temperature); - diff --git a/src/ds18b20/tm_stm32f4_onewire.c b/src/ds18b20/tm_stm32f4_onewire.c index 0890b27..44ec8db 100644 --- a/src/ds18b20/tm_stm32f4_onewire.c +++ b/src/ds18b20/tm_stm32f4_onewire.c @@ -18,8 +18,7 @@ */ #include "tm_stm32f4_onewire.h" -void usart_debug_puts(const char* str); -void delay(const uint64_t us); +void delay_us(uint32_t); static void TM_GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) @@ -62,9 +61,9 @@ TM_GPIO_GetInputPinValue(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) } static void -ONEWIRE_DELAY(int us) +ONEWIRE_DELAY(uint32_t micros) { - delay(us); + delay_us(micros); } void TM_OneWire_Init(TM_OneWire_t* OneWireStruct, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) { 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 +#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 + +#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 -- cgit v1.2.3