start firmware: Function definitions for ADC

This commit is contained in:
Mario Hüttel 2020-02-02 00:01:08 +01:00
parent 6da5288d90
commit e091ccf19c
14 changed files with 7525 additions and 26 deletions

View File

@ -1,2 +1,10 @@
*.jdebug *.jdebug
*.jdebug.user *.jdebug.user
reflow-controller.cflags
reflow-controller.config
reflow-controller.creator
reflow-controller.cxxflags
reflow-controller.files
reflow-controller.includes
*.creator.user

View File

@ -3,16 +3,16 @@
#Compiler: arm-none-eabi #Compiler: arm-none-eabi
##################################################################################### #####################################################################################
#Add Files and Folders below######################################################### #Add Files and Folders below#########################################################
CFILES = main.c syscalls.c UART/uart.c cmsis_boot/system_stm32f4xx.c CFILES = main.c syscalls.c uart/uart.c cmsis_boot/system_stm32f4xx.c systick.c
ASFILES = boot/startup_stm32f4xx.S ASFILES = boot/startup_stm32f4xx.S
INCLUDEPATH = -Iboot -Imathlib -Icmsis -Icmsis_boot -IUART INCLUDEPATH = -Icmsis -Iinclude
OBJDIR = obj OBJDIR = obj
target = reflow-controller target = reflow-controller
LIBRARYPATH = -L. -Lmathlib LIBRARYPATH = -L. -Lmathlib
LIBRARIES = # -larm_cortexM4lf_math LIBRARIES = # -larm_cortexM4lf_math
DEFINES = -DSTM32F407xx -DSTM32F4XX -DARM_MATH_CM4 DEFINES = -DSTM32F407xx -DSTM32F4XX -DARM_MATH_CM4 -DHSE_VALUE=8000000UL
mapfile = memory-mapping mapfile = memory-mapping
ifneq ($(VERBOSE),true) ifneq ($(VERBOSE),true)

2
stm-firmware/adc-meas.c Normal file
View File

@ -0,0 +1,2 @@
#include <adc-meas.h>

View File

@ -0,0 +1,97 @@
#ifndef __ADCMEAS_H__
#define __ADCMEAS_H__
#include <stdbool.h>
#include <stdint.h>
#define ADC_PT1000_FILTER_WEIGHT 0.005f
#define ADC_PT1000_LOWER_WATCHDOG 100
#define ADC_PT1000_HIGHER_WATCHDOG 4000
enum adc_p1000_error {ADC_PT1000_NO_ERR= 0, ADC_PT1000_WATCHDOG_ERROR=-1, ADC_PT1000_OVERFLOW=2};
/**
* @brief This function sets up the ADC measurement fo the external PT1000 temperature sensor
*
* Used peripherals:
* - Timer 2 for sampling control
* - ADC1
*
* The filter weight \f$\alpha\f$ is configured for @ref ADC_PT1000_FILTER_WEIGHT
*
*/
void adc_pt1000_setup_meas();
/**
* @brief Set moving average filter parameters
*
* The sampled resistance value is filtered with an exponential average filter
* specified by following difference equation:
*
* \f$ y[n] = (1-\alpha)y[n-1] + \alpha x[n] \f$
*
* @param alpha
*/
void adc_pt1000_set_moving_average_filter_param(float alpha);
/**
* @brief Set the calibration data for the PT1000 measurement
*
* The resulting resistance reading is
* \f$R_{corrected} = \sigma R_{raw} + O\f$
*
* @param offset Offset \f$O\f$
* @param sensitivity_deviation Sensitivity Deviation \f$\sigma\f$ after offset correction
*/
void adc_pt1000_set_resistance_calibration(float offset, float sensitivity_deviation, bool active);
/**
* @brief Get the state and values of the resistance calibration
* @param offset Offset
* @param sensitivity_deviation Sensitivity deviation
* @param active Active state of the correction
*/
void adc_pt1000_get_resistance_calibration(float *offset, float *sensitivity_deviation, bool *active);
/**
* @brief Get the current reistance value
*
* If the reistance calibration is enabled, this function applies the calculations of the raw reistance reading and
* returns the corrected value.
*
* If an ADC error is set, the status is negative. If the ADC is in streaming mode, the reistance reading is disabled and
* the status is 1. The status is 2 during the first measurements with a given filter setting. Technically, the resistance value is
* correct but the filter is not stable yet.
* Use adc_pt1000_check_error to check the error and reinitialize the ADC.
*
*
* @param[out] resistance Resistance output in Ohms
* @return Status
*/
int adc_pt1000_get_current_resistance(float *resistance);
/**
* @brief Stream the raw ADC data to an array in memory.
*
* Streaming is done using DMA2 Stream0.
* This function is used for gathering fullspeed sampling data for external interfaces or calibration
* During streaming, the adc_pt1000_get_current_resistance() function will return an error
*
* @param adc_array Array to stream data to
* @param length Amount of data points to be measured
* @param flag_to_set This flag is set to 1 once the data has been measured and is transferred. A negative value indicates an error
* @return 0 if measurement could be started
*/
int adc_pt1000_stream_raw_value_to_memory(uint16_t *adc_array, uint32_t length, volatile uint8_t *flag_to_set);
void adc_pt1000_convert_raw_value_array_to_resistance(float *resistance_dest, uint16_t *raw_source, uint32_t count);
/**
* @brief Check if the ADC measurement experienced any kind of error (DMA, Analog Watchdog, etc...)
*
* In case of an error, it may be necessary to call adc_pt1000_setup_meas() again in order to recover from the error
*/
enum adc_p1000_error adc_pt1000_check_error();
#endif // __ADCMEAS_H__

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,43 @@
#ifndef __SYSTICK_H__
#define __SYSTICK_H__
#include <stdint.h>
/**
* @brief Reload value for the systick timer.
*
* This value has to be configured to set the systick to a one milliscond tick interval
* The default value is 168000, which results in a 1ms tick for 168 MHz CPU speed
*/
#define SYSTICK_RELOAD (168000UL)
/**
* @brief Variable used by the systick_wait_ms function
*/
extern volatile uint32_t wait_tick_ms;
/**
* @brief Systemclock in milliseconds.
*
* This value must not be reset during the whole runtime.
*
*/
extern volatile uint64_t global_tick_ms;
/**
* @brief Setup the Systick timer to generate a 1 ms tick
*/
void systick_setup(void);
/**
* @brief Wait for x milliseconds
*
* This function is not reentrant and must not be called from an interrupt
*
* @warning Do not use in interrupt context
* @param ms wait time in ms
*/
void systick_wait_ms(uint32_t ms);
#endif /* __SYSTICK_H__ */

View File

@ -5,10 +5,11 @@
* Author: mari * Author: mari
*/ */
#include <stm32f4xx.h> #include <stm32f4xx.h>
#include <systick.h>
//#include <arm_math.h> //#include <arm_math.h>
#include <system_stm32f4xx.h> #include <system_stm32f4xx.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#define OUTPUT(pin) (0b01 << (pin * 2)) #define OUTPUT(pin) (0b01 << (pin * 2))
#define ANALOG(pin) (0x03 << (pin * 2)) #define ANALOG(pin) (0x03 << (pin * 2))
@ -34,9 +35,14 @@ void DMA2_Stream0_IRQHandler()
DMA2->LIFCR = lisr; DMA2->LIFCR = lisr;
if (lisr & DMA_LISR_TCIF0) { if (lisr & DMA_LISR_TCIF0) {
if (new_data)
new_data = 2;
new_data = 1; new_data = 1;
sample_count++; sample_count++;
GPIOB->ODR ^= (1<<3);
} }
} }
void setup_dma(void *dest, size_t size) void setup_dma(void *dest, size_t size)
@ -63,11 +69,16 @@ float vdd_calculated = 3.3f;
float vbat_lf_corr; float vbat_lf_corr;
static void setup_timers(void)
{
}
int main() { int main() {
struct adc_conversions working; struct adc_conversions working;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
@ -75,7 +86,6 @@ int main() {
GPIOB->MODER = OUTPUT(2) | OUTPUT(3); GPIOB->MODER = OUTPUT(2) | OUTPUT(3);
GPIOA->MODER |= ANALOG(2); GPIOA->MODER |= ANALOG(2);
GPIOB->ODR |= (1<<2); GPIOB->ODR |= (1<<2);
SysTick_Config(8*1680000);
ADC1->SMPR2 = (7U<<(3*2)); ADC1->SMPR2 = (7U<<(3*2));
ADC1->SMPR1 = (7U<<18) | (7U<<21) | (7U<<24); ADC1->SMPR1 = (7U<<18) | (7U<<21) | (7U<<24);
@ -88,21 +98,24 @@ int main() {
//while(1); //while(1);
systick_setup();
setup_dma(&adc_results, 3); setup_dma(&adc_results, 3);
ADC1->CR2 |= ADC_CR2_SWSTART; ADC1->CR2 |= ADC_CR2_SWSTART;
while(1) { while(1) {
if (!new_data) if (!new_data) {
continue; continue;
}
new_data = 0;
memcpy(&working, &adc_results, sizeof(adc_results)); memcpy(&working, &adc_results, sizeof(adc_results));
new_data = 0;
//ref_lf = 0.995f * ref_lf + 0.005f * (float)working.ref_raw;
//vdd_calculated = ((1.21f * 4096)/ ref_lf);
ref_lf = 0.995f * ref_lf + 0.005f * (float)working.ref_raw; //temp_lf_corr = 0.99f * temp_lf_corr + 0.01 * (float)working.temp_raw * vdd_calculated / 2.495f;
vdd_calculated = ((1.21f * 4096)/ ref_lf);
temp_lf_corr = 0.99f * temp_lf_corr + 0.01 * (float)working.temp_raw * vdd_calculated / 2.495f;
ext_lf_corr = 0.995f * ext_lf_corr + 0.005f * (float)working.pa2_raw / 4096 * 2500.0f; // * vdd_calculated / 2.495f; ext_lf_corr = 0.995f * ext_lf_corr + 0.005f * (float)working.pa2_raw / 4096 * 2500.0f; // * vdd_calculated / 2.495f;
//vbat_lf_corr = 0.99 * vbat_lf_corr + 0.01 * (float)working.vbat_raw / 4096 * vdd_calculated * 2.0f; //vbat_lf_corr = 0.99 * vbat_lf_corr + 0.01 * (float)working.vbat_raw / 4096 * vdd_calculated * 2.0f;
@ -110,7 +123,4 @@ int main() {
} }
void SysTick_Handler()
{
GPIOB->ODR ^= (1<<2) | (1<<3);
}

View File

@ -1,11 +1,4 @@
/* #include <uart/uart.h>
* syscalls.c
*
* Created on: Dec 14, 2014
* Author: shino-chan
*/
#include "uart.h"
char* _sbrk(int incr) { char* _sbrk(int incr) {
extern char heap_low; // Defined by the linker extern char heap_low; // Defined by the linker

40
stm-firmware/systick.c Normal file
View File

@ -0,0 +1,40 @@
#include <systick.h>
#include <stm32f4xx.h>
#include <core_cm4.h>
volatile uint32_t wait_tick_ms;
volatile uint64_t global_tick_ms;
void systick_setup(void)
{
/* Setup Systick for 1ms tick @ 168 MHz Clock Speed */
SysTick_Config(SYSTICK_RELOAD);
}
/**
* @brief Wait for x milliseconds
*
* This function is not reentrant and must not be called from an interrupt
*
* @warning Do not use in interrupt context
* @param ms wait time in ms
*/
void systick_wait_ms(uint32_t ms)
{
wait_tick_ms = 0UL;
while (wait_tick_ms < ms);
}
/**
* @brief Interrupt Handler for SysTick
*
* This handler is called every millisecond
*
* @warning For calling cyclic functions use separate timers/flags and don't spoil this function
*/
void SysTick_Handler()
{
/* Increase tick counters */
wait_tick_ms++;
global_tick_ms++;
}

View File

@ -8,7 +8,7 @@
//PA2 => TX //PA2 => TX
//PA3 => RX //PA3 => RX
//Alternate Function 7 //Alternate Function 7
#include "uart.h" #include <uart/uart.h>
#include <stm32f4xx.h> #include <stm32f4xx.h>
void initUART() { void initUART() {