f3: wip continues

This commit is contained in:
Karl Palsson 2015-10-28 18:55:56 +00:00
parent 3830ded6e7
commit 26eeb48807
2 changed files with 131 additions and 14 deletions

View file

@ -23,6 +23,14 @@
void adc_power_init(void)
{
/* Some basic ADC config, that we won't touch again */
#if defined(STM32F3)
/* silly f303 */
rcc_periph_clock_enable(RCC_ADC12);
rcc_adc_prescale(RCC_CFGR2_ADCxPRES_PLL_CLK_DIV_1, RCC_CFGR2_ADCxPRES_PLL_CLK_DIV_1);
adc_enable_regulator(ADC1);
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR1_SMP_19DOT5CYC);
adc_set_sample_time(ADC1, ADC_CHANNEL_TEMP, ADC_SMPR1_SMP_181DOT5CYC);
#else
rcc_periph_clock_enable(RCC_ADC1);
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28CYC);
#if 0
@ -35,6 +43,9 @@ void adc_power_init(void)
adc_disable_scan_mode(ADC1);
#endif
#endif
adc_enable_temperature_sensor();
adc_enable_vrefint();
/*
* We're going to setup a timer to run at top speed, so... "fast"
* but we don't actually care about the rate itself. We just
@ -57,6 +68,30 @@ static uint16_t read_adc_naiive(uint8_t channel)
return adc_read_regular(ADC1);
}
static uint16_t compensate_vref(uint16_t adc_count, uint16_t vref_count) {
uint32_t ret = adc_count * ST_VREFINT_CAL;
return (ret / vref_count);
}
static int work_temp(unsigned int ts_v, unsigned int vref) {
// This is important! as the calibration values are give us a slope in mv/C
int raw_temp_mv = compensate_vref(ts_v, vref);
// millivolts gives millcentigrade
float slope = raw_temp_mv - (ST_TSENSE_CAL1_30C * 1.0) / (ST_TSENSE_CAL2_110 * 1.0) - ST_TSENSE_CAL1_30C;
int temp_mc = (110 - 30) * slope + 30.0;
return temp_mc / 10;
}
static float adc_calculate_temp(unsigned int ts_v) {
#if 0
float factor = (110-30) / ((ST_TSENSE_CAL1_30C * 1.0) - (ST_TSENSE_CAL2_110 * 1.0));
return (factor * (ts_v - ST_TSENSE_CAL2_110)) + 30.0;
#else
float factor = (110-30) / ((ST_TSENSE_CAL2_110 * 1.0) - (ST_TSENSE_CAL1_30C * 1.0));
return (factor * (ts_v - ST_TSENSE_CAL1_30C)) + 30.0;
#endif
}
void adc_power_task_up(void) {
TIM_CNT(TIMER) = 0;
adc_power_on(ADC1);
@ -77,12 +112,18 @@ void adc_power_task_up(void) {
unsigned int tconv = TIM_CNT(TIMER);
#else
TIM_CNT(TIMER) = 0;
unsigned int v1 = read_adc_naiive(0);
unsigned int v2 = read_adc_naiive(1);
unsigned int v1 = read_adc_naiive(1);
unsigned int v5 = read_adc_naiive(5);
unsigned int temp_adc = read_adc_naiive(ADC_CHANNEL_TEMP);
unsigned int vref_adc = read_adc_naiive(ADC_CHANNEL_VREF);
// TODO - use vref to adjust temp_adc for non-3v readings!
float temp = adc_calculate_temp(temp_adc);
int tempi = 100 * temp;
unsigned int tconv = TIM_CNT(TIMER);
#endif
printf("ton: %u, tconv: %u, v1: %u, v2: %u\n", td, tconv, v1, v2);
printf("ton: %u, tconv: %u, ch1: %u, ch5: %u\n", td, tconv, v1, v5);
printf("\tTemperature: %f (raw count: %u)\n", temp, temp_adc);
}
void adc_power_task_down()

View file

@ -8,6 +8,7 @@
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/adc.h>
#include <libopencm3/stm32/dac.h>
#include <libopencm3/stm32/flash.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/usart.h>
@ -15,35 +16,110 @@
#include "trace.h"
#include "adc-power.h"
#define LED_DISCO_GREEN_PORT GPIOD
#define LED_DISCO_GREEN_PIN GPIO12
/* f3 pll setup, based on l1/f4*/
typedef struct {
uint8_t pll_mul;
uint8_t pll_div;
uint8_t pll_source;
uint32_t flash_config;
uint8_t hpre;
uint8_t ppre1;
uint8_t ppre2;
uint32_t apb1_frequency;
uint32_t apb2_frequency;
} rcc_clock_scale_t;
static void rcc_clock_setup_pll_f3_special(const rcc_clock_scale_t *clock)
{
/* Turn on the appropriate source for the PLL */
// TODO, some f3's have extra bits here
enum osc my_osc;
if (clock->pll_source == RCC_CFGR_PLLSRC_HSE_PREDIV) {
my_osc = HSE;
} else {
my_osc = HSI;
}
rcc_osc_on(my_osc);
while (!rcc_is_osc_ready(my_osc));
/* Configure flash settings. */
flash_set_ws(clock->flash_config);
/*
* Set prescalers for AHB, ADC, ABP1, ABP2.
* Do this before touching the PLL (TODO: why?).
*/
rcc_set_hpre(clock->hpre);
rcc_set_ppre1(clock->ppre1);
rcc_set_ppre2(clock->ppre2);
rcc_osc_off(PLL);
while (rcc_is_osc_ready(PLL));
rcc_set_pll_source(clock->pll_source);
rcc_set_pll_multiplier(clock->pll_mul);
// TODO - iff pll_div != 0, then maybe we're on a target that
// has the dividers?
/* Enable PLL oscillator and wait for it to stabilize. */
rcc_osc_on(PLL);
while (!rcc_is_osc_ready(PLL));
/* Select PLL as SYSCLK source. */
rcc_set_sysclk_source(RCC_CFGR_SW_PLL);
rcc_wait_for_sysclk_status(PLL);
/* Set the peripheral clock frequencies used. */
rcc_apb1_frequency = clock->apb1_frequency;
rcc_apb2_frequency = clock->apb2_frequency;
}
static void setup_clocks(void)
{
rcc_clock_scale_t clock_full_hse8mhz ={
.pll_mul = RCC_CFGR_PLLMUL_PLL_IN_CLK_X9,
.pll_source = RCC_CFGR_PLLSRC_HSE_PREDIV,
.hpre = RCC_CFGR_HPRE_DIV_NONE,
.ppre1 = RCC_CFGR_PPRE1_DIV_2,
.ppre2 = RCC_CFGR_PPRE2_DIV_NONE,
.flash_config = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY_2WS,
.apb1_frequency = 36000000,
.apb2_frequency = 72000000,
};
rcc_clock_setup_pll_f3_special(&clock_full_hse8mhz);
}
int main(void)
{
int i;
int j = 0;
rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]);
rcc_periph_clock_enable(RCC_GPIOD);
setup_clocks();
/* Board led */
rcc_periph_clock_enable(RCC_GPIOE);
gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO8);
gpio_set(GPIOE, GPIO8);
printf("hi guys!\n");
/* green led for ticking */
gpio_mode_setup(LED_DISCO_GREEN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
LED_DISCO_GREEN_PIN);
rcc_periph_clock_enable(RCC_GPIOA);
gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0);
gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1);
adc_power_init();
for (i = 0; i < 0x1000; i++) { /* need as much as 10 usecs for vreg */
__asm__("NOP");
}
while (1) {
adc_power_task_up();
gpio_toggle(LED_DISCO_GREEN_PORT, LED_DISCO_GREEN_PIN);
gpio_toggle(GPIOE, GPIO8);
for (i = 0; i < 0x1000000; i++) { /* Wait a bit. */
for (i = 0; i < 0x800000; i++) { /* Wait a bit. */
__asm__("NOP");
}
printf("tick...\n");
adc_power_task_down();
gpio_toggle(LED_DISCO_GREEN_PORT, LED_DISCO_GREEN_PIN);
for (i = 0; i < 0x1000000; i++) { /* Wait a bit. */
gpio_toggle(GPIOE, GPIO8);
for (i = 0; i < 0x800000; i++) { /* Wait a bit. */
__asm__("NOP");
}
}