diff options
author | Maximilien Cuony <maximilien@theglu.org> | 2015-12-09 23:36:09 +0100 |
---|---|---|
committer | Maximilien Cuony <maximilien@theglu.org> | 2015-12-09 23:36:09 +0100 |
commit | 88de6ea0637a85ef89849996e350440d1b680ca3 (patch) | |
tree | 5d01102c677677da10b010806b87b835a66d14d6 /src | |
parent | e33c264e29859bd4311faef25394bd8d6058d4be (diff) | |
download | glutte-o-matic-88de6ea0637a85ef89849996e350440d1b680ca3.tar.gz glutte-o-matic-88de6ea0637a85ef89849996e350440d1b680ca3.tar.bz2 glutte-o-matic-88de6ea0637a85ef89849996e350440d1b680ca3.zip |
Temperature measurements
Diffstat (limited to 'src')
-rw-r--r-- | src/bsp/stm32_flash.ld | 1 | ||||
l--------- | src/temperature/FreeRTOS | 1 | ||||
-rw-r--r-- | src/temperature/FreeRTOSConfig.h | 165 | ||||
-rw-r--r-- | src/temperature/Makefile | 130 | ||||
-rw-r--r-- | src/temperature/README.md | 2 | ||||
-rw-r--r-- | src/temperature/bin/.git_keep | 0 | ||||
l--------- | src/temperature/bsp | 1 | ||||
-rw-r--r-- | src/temperature/debug.c | 78 | ||||
-rw-r--r-- | src/temperature/debug.h | 47 | ||||
-rw-r--r-- | src/temperature/main.c | 208 | ||||
-rw-r--r-- | src/temperature/temperature.c | 17 | ||||
-rw-r--r-- | src/temperature/temperature.h | 9 |
12 files changed, 659 insertions, 0 deletions
diff --git a/src/bsp/stm32_flash.ld b/src/bsp/stm32_flash.ld index 8aa36aa..0d09b4c 100644 --- a/src/bsp/stm32_flash.ld +++ b/src/bsp/stm32_flash.ld @@ -113,6 +113,7 @@ SECTIONS . = ALIGN(4); PROVIDE ( end = . ); PROVIDE ( _end = . ); + PROVIDE ( __end__ = . ); . = . + _Min_Heap_Size; . = . + _Min_Stack_Size; . = ALIGN(4); diff --git a/src/temperature/FreeRTOS b/src/temperature/FreeRTOS new file mode 120000 index 0000000..51f4e96 --- /dev/null +++ b/src/temperature/FreeRTOS @@ -0,0 +1 @@ +../FreeRTOS
\ No newline at end of file diff --git a/src/temperature/FreeRTOSConfig.h b/src/temperature/FreeRTOSConfig.h new file mode 100644 index 0000000..11427d8 --- /dev/null +++ b/src/temperature/FreeRTOSConfig.h @@ -0,0 +1,165 @@ +/*
+ FreeRTOS V8.0.0:rc2 - Copyright (C) 2014 Real Time Engineers Ltd.
+ All rights reserved
+
+ VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
+
+ ***************************************************************************
+ * *
+ * FreeRTOS provides completely free yet professionally developed, *
+ * robust, strictly quality controlled, supported, and cross *
+ * platform software that has become a de facto standard. *
+ * *
+ * Help yourself get started quickly and support the FreeRTOS *
+ * project by purchasing a FreeRTOS tutorial book, reference *
+ * manual, or both from: http://www.FreeRTOS.org/Documentation *
+ * *
+ * Thank you! *
+ * *
+ ***************************************************************************
+
+ This file is part of the FreeRTOS distribution.
+
+ FreeRTOS is free software; you can redistribute it and/or modify it under
+ the terms of the GNU General Public License (version 2) as published by the
+ Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
+
+ >>! NOTE: The modification to the GPL is included to allow you to distribute
+ >>! a combined work that includes FreeRTOS without being obliged to provide
+ >>! the source code for proprietary components outside of the FreeRTOS
+ >>! kernel.
+
+ FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. Full license text is available from the following
+ link: http://www.freertos.org/a00114.html
+
+ 1 tab == 4 spaces!
+
+ ***************************************************************************
+ * *
+ * Having a problem? Start by reading the FAQ "My application does *
+ * not run, what could be wrong?" *
+ * *
+ * http://www.FreeRTOS.org/FAQHelp.html *
+ * *
+ ***************************************************************************
+
+ http://www.FreeRTOS.org - Documentation, books, training, latest versions,
+ license and Real Time Engineers Ltd. contact details.
+
+ http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
+ including FreeRTOS+Trace - an indispensable productivity tool, a DOS
+ compatible FAT file system, and our tiny thread aware UDP/IP stack.
+
+ http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
+ Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
+ licenses offer ticketed support, indemnification and middleware.
+
+ http://www.SafeRTOS.com - High Integrity Systems also provide a safety
+ engineered and independently SIL3 certified version for use in safety and
+ mission critical applications that require provable dependability.
+
+ 1 tab == 4 spaces!
+*/
+
+
+#ifndef FREERTOS_CONFIG_H
+#define FREERTOS_CONFIG_H
+
+/*-----------------------------------------------------------
+ * Application specific definitions.
+ *
+ * These definitions should be adjusted for your particular hardware and
+ * application requirements.
+ *
+ * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
+ * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
+ *
+ * See http://www.freertos.org/a00110.html.
+ *----------------------------------------------------------*/
+
+/* Ensure stdint is only used by the compiler, and not the assembler. */
+//#ifdef __ICCARM__
+ #include <stdint.h>
+ extern uint32_t SystemCoreClock;
+//#endif
+
+#define configUSE_PREEMPTION 1
+#define configUSE_IDLE_HOOK 0 // Default: 1
+#define configUSE_TICK_HOOK 0 // Default: 1
+#define configCPU_CLOCK_HZ ( SystemCoreClock )
+#define configTICK_RATE_HZ ( ( portTickType ) 1000 )
+#define configMAX_PRIORITIES ( 5 )
+#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 130 )
+#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 75 * 1024 ) )
+#define configMAX_TASK_NAME_LEN ( 10 )
+#define configUSE_TRACE_FACILITY 1
+#define configUSE_16_BIT_TICKS 0
+#define configIDLE_SHOULD_YIELD 1
+#define configUSE_MUTEXES 1
+#define configQUEUE_REGISTRY_SIZE 8
+#define configCHECK_FOR_STACK_OVERFLOW 2 // Default: 2
+#define configUSE_RECURSIVE_MUTEXES 1
+#define configUSE_MALLOC_FAILED_HOOK 0 // Default: 1
+#define configUSE_APPLICATION_TASK_TAG 0
+#define configUSE_COUNTING_SEMAPHORES 1
+#define configGENERATE_RUN_TIME_STATS 0
+
+/* Co-routine definitions. */
+#define configUSE_CO_ROUTINES 0
+#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
+
+/* Software timer definitions. */
+#define configUSE_TIMERS 1
+#define configTIMER_TASK_PRIORITY ( 2 )
+#define configTIMER_QUEUE_LENGTH 10
+#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 )
+
+/* Set the following definitions to 1 to include the API function, or zero
+to exclude the API function. */
+#define INCLUDE_vTaskPrioritySet 1
+#define INCLUDE_uxTaskPriorityGet 1
+#define INCLUDE_vTaskDelete 1
+#define INCLUDE_vTaskCleanUpResources 1
+#define INCLUDE_vTaskSuspend 1
+#define INCLUDE_vTaskDelayUntil 1
+#define INCLUDE_vTaskDelay 1
+
+/* Cortex-M specific definitions. */
+#ifdef __NVIC_PRIO_BITS
+ /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
+ #define configPRIO_BITS __NVIC_PRIO_BITS
+#else
+ #define configPRIO_BITS 4 /* 15 priority levels */
+#endif
+
+/* The lowest interrupt priority that can be used in a call to a "set priority"
+function. */
+#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0xf
+
+/* The highest interrupt priority that can be used by any interrupt service
+routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
+INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
+PRIORITY THAN THIS! (higher priorities are lower numeric values. */
+#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5
+
+/* Interrupt priorities used by the kernel port layer itself. These are generic
+to all Cortex-M ports, and do not rely on any particular library functions. */
+#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
+/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
+See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
+#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
+
+/* Normal assert() semantics without relying on the provision of an assert.h
+header file. */
+#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
+
+/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
+standard names. */
+#define vPortSVCHandler SVC_Handler
+#define xPortPendSVHandler PendSV_Handler
+#define xPortSysTickHandler SysTick_Handler
+
+#endif /* FREERTOS_CONFIG_H */
+
diff --git a/src/temperature/Makefile b/src/temperature/Makefile new file mode 100644 index 0000000..e29a3ba --- /dev/null +++ b/src/temperature/Makefile @@ -0,0 +1,130 @@ +### +# GNU ARM Embedded Toolchain +CC=arm-none-eabi-gcc +LD=arm-none-eabi-ld +AR=arm-none-eabi-ar +AS=arm-none-eabi-as +CP=arm-none-eabi-objcopy +OD=arm-none-eabi-objdump +SIZE=arm-none-eabi-size + +### +# Directory Structure +BINDIR=bin +SRCDIR=. + +### +# Find source files +ASOURCES=$(shell find -L $(SRCDIR) -name '*.s') +CSOURCES+=$(shell find -L $(SRCDIR) -name '*.c') +# Find header directories +INC=$(shell find -L . -name '*.h' -exec dirname {} \; | uniq) +INCLUDES=$(INC:%=-I%) +# Create object list +OBJECTS=$(ASOURCES:%.s=%.o) +OBJECTS+=$(CSOURCES:%.c=%.o) +# Define output files ELF & IHEX +BINELF=outp.elf +BINHEX=outp.hex + +### +# MCU FLAGS +MCFLAGS=-mcpu=cortex-m4 -mthumb -mlittle-endian \ +-mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mthumb-interwork +# COMPILE FLAGS +DEFS=-DUSE_STDPERIPH_DRIVER -DSTM32F4XX -DARM_MATH_CM4 -D__FPU_PRESENT=1 +CFLAGS =-Wall -ggdb -std=c99 -c $(MCFLAGS) $(DEFS) $(INCLUDES) +# LINKER FLAGS +LDSCRIPT= bsp/stm32_flash.ld +LDFLAGS =-T $(LDSCRIPT) --specs=nosys.specs $(MCFLAGS) -lm +LDFLAGS += -u _printf_float + +### +# Optimizations +OPT?='O1 O2 O3 O4 O6 O7' # O5 disabled by default, because it breaks code + +ifneq ($(findstring release-memopt,$(MAKECMDGOALS)),) +ifneq ($(filter O1,$(OPT)),) +CXXFLAGS+=-fno-exceptions # Uncomment to disable exception handling +DEFS+=-DNO_EXCEPTIONS # The source code has to comply with this rule +endif + +ifneq ($(filter O2,$(OPT)),) +CFLAGS+=-Os # Optimize for size https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html +CXXFLAGS+=-Os +LDFLAGS+=-Os # Optimize for size https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html +endif + +ifneq ($(filter O3,$(OPT)),) +CFLAGS+=-ffunction-sections -fdata-sections # Place each function or data item into its own section in the output file +CXXFLAGS+=-ffunction-sections -fdata-sections # -||- +LDFLAGS+=-Wl,-gc-sections # Remove isolated unused sections +endif + +ifneq ($(filter O4,$(OPT)),) +CFLAGS+=-fno-builtin # Disable C++ exception handling +CXXFLAGS+=-fno-builtin # Disable C++ exception handling +endif + +ifneq ($(filter O5,$(OPT)),) +CFLAGS+=-flto # Enable link time optimization +CXXFLAGS+=-flto # Enable link time optimization +LDFLAGS+=-flto # Enable link time optimization +endif + +ifneq ($(filter O6,$(OPT)),) +CXXFLAGS+=-fno-rtti # Disable type introspection +endif + +ifneq ($(findstring O7,$(OPT)),) +LDFLAGS+=--specs=nano.specs # Use size optimized newlib +endif +endif + +LDFLAGS += --specs=rdimon.specs -lc -lrdimon + +### +# Build Rules +.PHONY: all release release-memopt debug clean + +all: release + +release: $(BINDIR)/$(BINHEX) + +release-memopt: release + +debug: CFLAGS+=-g +debug: LDFLAGS+=-g +debug: release + +$(BINDIR)/$(BINHEX): $(BINDIR)/$(BINELF) + $(CP) -O ihex $< $@ + +$(BINDIR)/$(BINELF): $(OBJECTS) + $(CC) $(LDFLAGS) $(OBJECTS) -o $@ + $(SIZE) $(BINDIR)/$(BINELF) + +%.o: %.c + $(CC) $(CFLAGS) $< -o $@ + +%.o: %.s + $(CC) $(CFLAGS) $< -o $@ + +clean: + rm -f $(OBJECTS) $(BINDIR)/$(BINELF) $(BINDIR)/$(BINHEX) + +# Connect to openocd's gdb server on port 3333 +deploy: $(BINDIR)/$(BINELF) +ifeq ($(wildcard /opt/openocd/bin/openocd),) + /usr/bin/openocd -f /usr/share/openocd/scripts/board/stm32f4discovery.cfg -c "program bin/"$(BINELF)" verify reset" -c "init" -c "reset" -c "exit" +else + /opt/openocd/bin/openocd -f /opt/openocd/share/openocd/scripts/board/stm32f4discovery.cfg -c "program bin/"$(BINELF)" verify reset" -c "init" -c "reset" -c "exit" +endif + +deploy_debug: $(BINDIR)/$(BINELF) +ifeq ($(wildcard /opt/openocd/bin/openocd),) + /usr/bin/openocd -f /usr/share/openocd/scripts/board/stm32f4discovery.cfg -c "program bin/"$(BINELF)" verify reset" -c "init" -c "reset halt" -c "exit" + /usr/bin/openocd -f /usr/share/openocd/scripts/board/stm32f4discovery.cfg -c "init" -c "reset halt" -c "arm semihosting enable" -c "reset" +else + /opt/openocd/bin/openocd -f /opt/openocd/share/openocd/scripts/board/stm32f4discovery.cfg -c "program bin/"$(BINELF)" verify reset" -c "init" -c "reset halt" -c "arm semihosting enable" -c "reset" +endif diff --git a/src/temperature/README.md b/src/temperature/README.md new file mode 100644 index 0000000..f5a17c1 --- /dev/null +++ b/src/temperature/README.md @@ -0,0 +1,2 @@ +An example program that tries to read the +current temperature on the STM32F4DISCOVERY. diff --git a/src/temperature/bin/.git_keep b/src/temperature/bin/.git_keep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/temperature/bin/.git_keep diff --git a/src/temperature/bsp b/src/temperature/bsp new file mode 120000 index 0000000..5d9120a --- /dev/null +++ b/src/temperature/bsp @@ -0,0 +1 @@ +../bsp
\ No newline at end of file diff --git a/src/temperature/debug.c b/src/temperature/debug.c new file mode 100644 index 0000000..ea7473e --- /dev/null +++ b/src/temperature/debug.c @@ -0,0 +1,78 @@ +/* + * 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 <stddef.h> +#include <stdint.h> + +#include "debug.h" + +#define _DEBUG 1 + +#if _DEBUG + +void debug_send_command(int command, void *message) +{ + __asm__ volatile ( + "mov r0, %[cmd];" + "mov r1, %[msg];" + "bkpt #0xAB" + : + : [cmd] "r" (command), [msg] "r" (message) + : "r0", "r1", "memory"); +} + +void put_char(char c) +{ + __asm__ volatile ( + "mov r0, #0x03\n" /* SYS_WRITEC */ + "mov r1, %[msg]\n" + "bkpt #0xAB\n" + : + : [msg] "r" (&c) + : "r0", "r1" + ); +} + +void debug_print(const char* str) +{ + const int std_err = 2; + + int strlen = 0; + const char* s; + s = str; + while (*s) { + strlen++; + s++; + } + + uint32_t m[] = { std_err, (uint32_t)str, strlen }; + debug_send_command(0x05, m); +} + +#else +void debug_send_command(int command, void *message) { } +void put_char(char c) { } +void debug_print(const char* str) { } + +#endif diff --git a/src/temperature/debug.h b/src/temperature/debug.h new file mode 100644 index 0000000..6215e5f --- /dev/null +++ b/src/temperature/debug.h @@ -0,0 +1,47 @@ +/* + * 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. +*/ + +/* Debugging utilities using the ARM semihosting facilities. Warning, it's quite + * slow. + * + * when used with OpenOCD's gdb server, requires the gdb command + * + * monitor arm semihosting enable + */ +#ifndef __DEBUG_H_ +#define __DEBUG_H_ + +/* Example usage for the send_command function +const char *s = "Hello world\n"; +uint32_t m[] = { 2, (uint32_t)s, sizeof(s)/sizeof(char) }; +send_command(0x05, m); +// some interrupt ID + +*/ + +/* Print a string to the OpenOCD console */ +void debug_print(const char* str); + +#endif // __DEBUG_H_ + diff --git a/src/temperature/main.c b/src/temperature/main.c new file mode 100644 index 0000000..9864dab --- /dev/null +++ b/src/temperature/main.c @@ -0,0 +1,208 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <math.h> +#include "stm32f4xx_conf.h" + +/* Kernel includes. */ +#include "FreeRTOS.h" +#include "task.h" +#include "timers.h" +#include "semphr.h" + +#include "temperature.h" + +#include "debug.h" + + +ADC_InitTypeDef ADC_InitStruct; + +ADC_CommonInitTypeDef ADC_CommonInitStruct; + + +// Private variables +volatile uint32_t time_var1, time_var2; + +// Private function prototypes +void init(); + +// Tasks +static void detect_button_press(void *pvParameters); +static void update_temperature(void *pvParameters); + + +void vApplicationStackOverflowHook( TaskHandle_t xTask, + signed char *pcTaskName ) +{ + while (1) {}; +} +extern void initialise_monitor_handles(void); + +int main(void) { + init(); + + xTaskCreate( + detect_button_press, + "TaskButton", + 4*configMINIMAL_STACK_SIZE, + (void*) NULL, + tskIDLE_PRIORITY + 2UL, + NULL); + + xTaskCreate( + update_temperature, + "TaskAudio", + configMINIMAL_STACK_SIZE, + (void*) NULL, + tskIDLE_PRIORITY + 2UL, + NULL); + + /* Start the RTOS Scheduler */ + vTaskStartScheduler(); + + /* HALT */ + while(1); +} + + +static void detect_button_press(void *pvParameters) +{ + GPIO_SetBits(GPIOD, GPIO_Pin_12); + + while (1) { + if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)>0) { + + while (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) > 0) { + vTaskDelay(100 / portTICK_RATE_MS); /* Button Debounce Delay */ + } + + GPIO_ResetBits(GPIOD, GPIO_Pin_12); + GPIO_SetBits(GPIOD, GPIO_Pin_15); + debug_print("Le jeu. Temp is: "); + + char t[32]; + // For debugging purposes only. snprinf has issues with %f + snprintf(t, 32, "%d.%02d", (int)temperature_get(), (int)(temperature_get() * 100.0 - (int)(temperature_get()) * 100.0)); + debug_print(t); + debug_print("\n"); + + GPIO_ResetBits(GPIOD, GPIO_Pin_15); + GPIO_SetBits(GPIOD, GPIO_Pin_12); + + while (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) == 0) { + vTaskDelay(100 / portTICK_RATE_MS); /* Button Debounce Delay */ + } + } + taskYIELD(); + } +} + +static void update_temperature(void *pvParameters) +{ + + while (1) { + + vTaskDelay(1000 / portTICK_RATE_MS); + + GPIO_SetBits(GPIOD, GPIO_Pin_13); + temperature_update(); + GPIO_ResetBits(GPIOD, GPIO_Pin_13); + taskYIELD(); + } +} + + +void init() { + GPIO_InitTypeDef GPIO_InitStructure; + USART_InitTypeDef USART_InitStructure; + // ---------- SysTick timer -------- // + if (SysTick_Config(SystemCoreClock / 1000)) { + // Capture error + while (1){}; + } + + // Enable full access to FPU (Should be done automatically in system_stm32f4xx.c): + //SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); // set CP10 and CP11 Full Access + + // GPIOD Periph clock enable + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); + + // Configure PD12, PD13, PD14 and PD15 in output pushpull mode + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + // Init PushButton + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_Init(GPIOA, &GPIO_InitStructure); + + + // ------ UART ------ // + + // Clock + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); + + // IO + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART1); + GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART1); + + // Conf + USART_InitStructure.USART_BaudRate = 115200; + USART_InitStructure.USART_WordLength = USART_WordLength_8b; + USART_InitStructure.USART_StopBits = USART_StopBits_1; + USART_InitStructure.USART_Parity = USART_Parity_No; + USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; + USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; + USART_Init(USART2, &USART_InitStructure); + + // Enable + USART_Cmd(USART2, ENABLE); + + + // Temperature using ADC + + // Enable ADC clock + RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); + + // Init ADC + ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent; + ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div8; + ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; + ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; + ADC_CommonInit(&ADC_CommonInitStruct); + + ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; + ADC_InitStruct.ADC_ScanConvMode = DISABLE; + ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; + ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; + ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; + ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; + ADC_InitStruct.ADC_NbrOfConversion = 1; + ADC_Init(ADC1, &ADC_InitStruct); + + // Configure ADC1 to use temperture sensor + ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_144Cycles); + + // Enable temperature sensor + ADC_TempSensorVrefintCmd(ENABLE); + + // Enable ADC + ADC_Cmd(ADC1, ENABLE); + +} + diff --git a/src/temperature/temperature.c b/src/temperature/temperature.c new file mode 100644 index 0000000..b9e95e2 --- /dev/null +++ b/src/temperature/temperature.c @@ -0,0 +1,17 @@ +#include "stm32f4xx_conf.h" + +#include "temperature.h" + +float temperature_value = 0; + +void temperature_update() { + ADC_SoftwareStartConv(ADC1); //Start the conversion + + while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); + + temperature_value = ((ADC_GetConversionValue(ADC1) * TEMP_V_BOARD / 0xFFF) - TEMP_V25) / TEMP_AVG_SLOPE + 25.0; +} + +float temperature_get() { + return temperature_value; +} diff --git a/src/temperature/temperature.h b/src/temperature/temperature.h new file mode 100644 index 0000000..ebe8f8b --- /dev/null +++ b/src/temperature/temperature.h @@ -0,0 +1,9 @@ +#define TEMP_V25 0.760 +#define TEMP_AVG_SLOPE 0.0025 +#define TEMP_V_BOARD 3.0 + +// Conpute the current temperature +void temperature_update(); + +// Return the current temperature +float temperature_get(); |