From cdd0a0c2e4d3171b008c30728ccab511db040d62 Mon Sep 17 00:00:00 2001 From: Karl Palsson Date: Sun, 11 Sep 2016 23:38:55 +0000 Subject: adc-power: include f1 tests --- tests/adc-power/Makefile.stm32f103-generic | 38 ++++++++++++++++++++++ tests/adc-power/adc-power.c | 38 ++++++++++++++++++++++ tests/adc-power/main-stm32f103-generic.c | 52 ++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 tests/adc-power/Makefile.stm32f103-generic create mode 100644 tests/adc-power/main-stm32f103-generic.c (limited to 'tests') diff --git a/tests/adc-power/Makefile.stm32f103-generic b/tests/adc-power/Makefile.stm32f103-generic new file mode 100644 index 0000000..f71e4b5 --- /dev/null +++ b/tests/adc-power/Makefile.stm32f103-generic @@ -0,0 +1,38 @@ +## +## This file is part of the libopencm3 project. +## +## This library is free software: you can redistribute it and/or modify +## it under the terms of the GNU Lesser General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## This library 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. See the +## GNU Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public License +## along with this library. If not, see . +## + +BOARD = stm32f103-generic +PROJECT = adc-power-$(BOARD) +BUILD_DIR = bin-$(BOARD) + +SHARED_DIR = ../../shared + +CFILES = main-$(BOARD).c +CFILES += adc-power.c +CFILES += trace.c trace_stdio.c + +VPATH += $(SHARED_DIR) + +INCLUDES += $(patsubst %,-I%, . $(SHARED_DIR)) + +OPENCM3_DIR=../../libopencm3 + +DEVICE=stm32f103x8 +#OOCD_INTERFACE = jlink +#OOCD_TARGET = stm32f1x +OOCD_FILE = openocd.stm32f103-generic.cfg +include ../../rules.mk diff --git a/tests/adc-power/adc-power.c b/tests/adc-power/adc-power.c index 4ab5e23..0904bb3 100644 --- a/tests/adc-power/adc-power.c +++ b/tests/adc-power/adc-power.c @@ -23,10 +23,18 @@ #if defined(STM32F0) #define SEPARATE_ADC_SAMPLING 0 #define SAMPLE_TIME_BASIC ADC_SMPR_SMP_239DOT5 // 4usec or more for tempsensor +#define HAS_CALIBRATION 1 +#elif defined(STM32F1) +#define SEPARATE_ADC_SAMPLING 0 +#define SAMPLE_TIME_BASIC ADC_SMPR_SMP_28DOT5CYC // 17usecs or more. >~15cycles at 9Mhz +#define SEPARATE_VREF 0 +#define HAS_CALIBRATION 1 +#define HAS_ROM_CALIBRATION 0 #elif defined(STM32F3) #define SAMPLE_TIME_BASIC ADC_SMPR_SMP_181DOT5CYC #define SAMPLE_TIME_TEMP ADC_SMPR_SMP_601DOT5CYC // 2.2usecs or more #define SAMPLE_TIME_VREF SAMPLE_TIME_TEMP +#define HAS_CALIBRATION 1 #elif defined(STM32F4) #define SAMPLE_TIME_BASIC ADC_SMPR_SMP_28CYC #define SAMPLE_TIME_TEMP ADC_SMPR_SMP_144CYC // 10 usecs or more, in theory needs 840cycles! @@ -42,6 +50,7 @@ #define SAMPLE_TIME_BASIC ADC_SMPR_SMP_247DOT5CYC #define SAMPLE_TIME_TEMP ADC_SMPR_SMP_247DOT5CYC #define SAMPLE_TIME_VREF SAMPLE_TIME_TEMP +#define HAS_CALIBRATION 1 #else #error "no sample time for your target yet?!" #endif @@ -52,6 +61,12 @@ #ifndef SEPARATE_ADC_SAMPLING #define SEPARATE_ADC_SAMPLING 1 #endif +#ifndef HAS_CALIBRATION +#define HAS_CALIBRATION 0 +#endif +#ifndef HAS_ROM_CALIBRATION +#define HAS_ROM_CALIBRATION 1 +#endif void adc_power_init(void) @@ -116,25 +131,48 @@ static uint16_t read_adc_naiive(uint8_t channel) uint8_t channel_array[16]; channel_array[0] = channel; adc_set_regular_sequence(ADC1, 1, channel_array); + // FIXME - use a trigger, see f1 notes! +#if defined (STM32F1) + adc_start_conversion_direct(ADC1); +#else adc_start_conversion_regular(ADC1); +#endif while (!adc_eoc(ADC1)); return adc_read_regular(ADC1); } static float adc_calc_tempf(unsigned int ts_v, unsigned int vref) { +#if (HAS_ROM_CALIBRATION == 1) float adjusted_vtemp = ts_v * ST_VREFINT_CAL * 1.0f / vref * 1.0f; float slope = (110-30) * 1.0f / (ST_TSENSE_CAL2_110C - ST_TSENSE_CAL1_30C) * 1.0f; return slope * (adjusted_vtemp - ST_TSENSE_CAL1_30C) + 30; +#else + return ts_v * 1.0; +#endif } static int adc_calc_tempi(unsigned int ts, unsigned int vref) { +#if (HAS_ROM_CALIBRATION == 1) int adjusted_vtemp = ts * ST_VREFINT_CAL / vref; int slope = (110-30) / (ST_TSENSE_CAL2_110C - ST_TSENSE_CAL1_30C); return slope * (adjusted_vtemp - ST_TSENSE_CAL1_30C) + 30; +#else + return ts * 1.00; +#endif } void adc_power_task_up(void) { TIM_CNT(TIMER) = 0; + // Welcome to f1 world. +#if defined (STM32F1) + adc_power_on(ADC1); + for (int i = 0; i < 0x80000; i++) { /* Wait a bit. */ + __asm__("NOP"); + } +#endif +#if (HAS_CALIBRATION == 1) + adc_calibrate(ADC1); +#endif adc_power_on(ADC1); unsigned int td = TIM_CNT(TIMER); diff --git a/tests/adc-power/main-stm32f103-generic.c b/tests/adc-power/main-stm32f103-generic.c new file mode 100644 index 0000000..815ab16 --- /dev/null +++ b/tests/adc-power/main-stm32f103-generic.c @@ -0,0 +1,52 @@ +/* + * Oct 2015 Karl Palsson + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "trace.h" +#include "adc-power.h" + +#define LED_PORT GPIOC +#define LED_PIN GPIO13 + +int main(void) +{ + int i; + int j = 0; + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + rcc_periph_clock_enable(RCC_GPIOC); + printf("hi guys!\n"); + /* green led for ticking */ + gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, LED_PIN); + + rcc_periph_clock_enable(RCC_GPIOA); + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO0); + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO5); + + + adc_power_init(); + while (1) { + adc_power_task_up(); + gpio_toggle(LED_PORT, LED_PIN); + + for (i = 0; i < 0x80000; i++) { /* Wait a bit. */ + __asm__("NOP"); + } + adc_power_task_down(); + gpio_toggle(LED_PORT, LED_PIN); + for (i = 0; i < 0x80000; i++) { /* Wait a bit. */ + __asm__("NOP"); + } + } + + return 0; +} -- cgit