cpu/stm32f4: reworked the ADC driver

This commit is contained in:
Hauke Petersen 2015-12-07 21:32:01 +01:00
parent 5b57bc80b0
commit fc7c25c95e
3 changed files with 141 additions and 139 deletions

View File

@ -25,6 +25,15 @@
extern "C" { extern "C" {
#endif #endif
/**
* @brief Available number of ADC devices
*/
#if defined(CPU_MODEL_STM32F401RE)
#define ADC_DEVS (1U)
#elif defined(CPU_MODEL_STM32F407VG) || defined(CPU_MODEL_STM32F415RG)
#define ADC_DEVS (3U)
#endif
/** /**
* @brief Overwrite the default gpio_t type definition * @brief Overwrite the default gpio_t type definition
* @{ * @{
@ -43,6 +52,30 @@ typedef uint32_t gpio_t;
*/ */
#define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 10)) | y) #define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 10)) | y)
/**
* @brief declare needed generic SPI functions
* @{
*/
#define PERIPH_SPI_NEEDS_TRANSFER_BYTES
#define PERIPH_SPI_NEEDS_TRANSFER_REG
#define PERIPH_SPI_NEEDS_TRANSFER_REGS
/** @} */
/**
* @brief Override the ADC resolution configuration
* @{
*/
#define HAVE_ADC_RES_T
typedef enum {
ADC_RES_6BIT = 0x03000000, /**< ADC resolution: 6 bit */
ADC_RES_8BIT = 0x02000000, /**< ADC resolution: 8 bit */
ADC_RES_10BIT = 0x01000000, /**< ADC resolution: 10 bit */
ADC_RES_12BIT = 0x00000000, /**< ADC resolution: 12 bit */
ADC_RES_14BIT = 1, /**< ADC resolution: 14 bit (not supported) */
ADC_RES_16BIT = 2 /**< ADC resolution: 16 bit (not supported)*/
} adc_res_t;
/** @} */
/** /**
* @brief Available ports on the STM32F4 family * @brief Available ports on the STM32F4 family
*/ */
@ -95,6 +128,16 @@ typedef struct {
} uart_conf_t; } uart_conf_t;
/** @} */ /** @} */
/**
* @brief ADC channel configuration data
*/
typedef struct {
gpio_t pin; /**< pin connected to the channel */
uint8_t dev; /**< ADCx - 1 device used for the channel */
uint8_t chan; /**< CPU ADC channel connected to the pin */
uint8_t rcc; /**< bit in the RCC APB2 enable register */
} adc_conf_t;
/** /**
* @brief Configure the alternate function for the given pin * @brief Configure the alternate function for the given pin
* *
@ -105,6 +148,13 @@ typedef struct {
*/ */
void gpio_init_af(gpio_t pin, gpio_af_t af); void gpio_init_af(gpio_t pin, gpio_af_t af);
/**
* @brief Configure the given pin to be used as ADC input
*
* @param[in] pin pin to configure
*/
void gpio_init_analog(gpio_t pin);
/** /**
* @brief Power on the DMA device the given stream belongs to * @brief Power on the DMA device the given stream belongs to
* *

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2014 Freie Universität Berlin * Copyright (C) 2014-2016 Freie Universität Berlin
* *
* This file is subject to the terms and conditions of the GNU Lesser General * This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more * Public License v2.1. See the file LICENSE in the top level directory for more
@ -19,167 +19,111 @@
*/ */
#include "cpu.h" #include "cpu.h"
#include "mutex.h"
#include "periph/adc.h" #include "periph/adc.h"
#include "periph_conf.h" #include "periph_conf.h"
/* guard in case that no ADC device is defined */ /**
#if ADC_NUMOF * @brief Maximum allowed ADC clock speed
*/
#define MAX_ADC_SPEED (12000000U)
typedef struct { /**
int max_value; * @brief Load the ADC configuration
} adc_config_t; * @{
*/
#ifdef ADC_CONFIG
static const adc_conf_t adc_config[] = ADC_CONFIG;
#else
static const adc_conf_t adc_config[] = {};
#endif
adc_config_t adc_config[ADC_NUMOF]; /**
* @brief Allocate locks for all three available ADC devices
*/
static mutex_t locks[] = {
#if ADC_DEVS > 1
MUTEX_INIT,
#endif
#if ADC_DEVS > 2
MUTEX_INIT,
#endif
MUTEX_INIT
};
int adc_init(adc_t dev, adc_precision_t precision) static inline ADC_TypeDef *dev(adc_t line)
{ {
ADC_TypeDef *adc = 0; return (ADC_TypeDef *)(ADC1_BASE + (adc_config[line].dev << 8));
}
adc_poweron(dev); static inline void prep(adc_t line)
{
mutex_lock(&locks[adc_config[line].dev]);
RCC->APB2ENR |= (RCC_APB2ENR_ADC1EN << adc_config[line].dev);
}
switch (dev) { static inline void done(adc_t line)
#if ADC_0_EN {
case ADC_0: RCC->APB2ENR &= ~(RCC_APB2ENR_ADC1EN << adc_config[line].dev);
adc = ADC_0_DEV; mutex_unlock(&locks[adc_config[line].dev]);
ADC_0_PORT_CLKEN(); }
ADC_0_PORT->MODER |= (3 << (ADC_0_CH0_PIN * 2) | 3 << (ADC_0_CH1_PIN * 2));
break; int adc_init(adc_t line)
#endif {
#if ADC_1_EN uint32_t clk_div = 2;
case ADC_1:
adc = ADC_1_DEV; /* check if the line is valid */
ADC_1_PORT_CLKEN(); if (line >= ADC_NUMOF) {
ADC_1_PORT->MODER |= (3 << (ADC_1_CH0_PIN * 2) | 3 << (ADC_1_CH1_PIN * 2));
break;
#endif
default:
return -1; return -1;
} }
/* reset control registers */ /* lock and power-on the device */
adc->CR1 = 0; prep(line);
adc->CR2 = 0;
adc->SQR1 = 0;
/* set precision */ /* configure the pin */
gpio_init_analog(adc_config[line].pin);
switch (precision) { /* set sequence length to 1 conversion and enable the ADC device */
case ADC_RES_6BIT: dev(line)->SQR1 = 0;
adc->CR1 |= ADC_CR1_RES_0 | ADC_CR1_RES_1; dev(line)->CR2 = ADC_CR2_ADON;
adc_config[dev].max_value = 0x3f; /* set clock prescaler to get the maximal possible ADC clock value */
break; for (clk_div = 2; clk_div < 8; clk_div += 2) {
case ADC_RES_8BIT: if ((CLOCK_CORECLOCK / clk_div) <= MAX_ADC_SPEED) {
adc->CR1 |= ADC_CR1_RES_1;
adc_config[dev].max_value = 0xff;
break;
case ADC_RES_10BIT:
adc->CR1 |= ADC_CR1_RES_0;
adc_config[dev].max_value = 0x3ff;
break;
case ADC_RES_12BIT:
adc_config[dev].max_value = 0xfff;
break;
case ADC_RES_14BIT:
case ADC_RES_16BIT:
adc_poweroff(dev);
return -1;
break; break;
} }
}
ADC->CCR = ((clk_div / 2) - 1) << 16;
/* set clock prescaler */ /* free the device again */
ADC->CCR = (3 << 16); /* ADC clock = 10,5MHz */ done(line);
/* enable the ADC module */
adc->CR2 |= ADC_CR2_ADON;
return 0; return 0;
} }
int adc_sample(adc_t dev, int channel) int adc_sample(adc_t line, adc_res_t res)
{ {
ADC_TypeDef *adc = 0; int sample;
switch (dev) { /* check if resolution is applicable */
#if ADC_0_EN if (res < 0xff) {
case ADC_0:
adc = ADC_0_DEV;
switch (channel) {
case 0:
adc->SQR3 = ADC_0_CH0 & 0x1f;
break;
case 1:
adc->SQR3 = ADC_0_CH1 & 0x1f;
break;
default:
return -1; return -1;
} }
break;
#endif
#if ADC_1_EN
case ADC_1:
adc = ADC_1_DEV;
switch (channel) {
case 0:
adc->SQR3 = ADC_1_CH0 & 0x1f;
break;
case 1:
adc->SQR3 = ADC_1_CH1 & 0x1f;
break;
default:
return -1;
}
break;
#endif
}
/* start single conversion */ /* lock and power on the ADC device */
adc->CR2 |= ADC_CR2_SWSTART; prep(line);
/* wait until conversion is complete */
while (!(adc->SR & ADC_SR_EOC)) {}
/* read and return result */
return (int)adc->DR;
}
void adc_poweron(adc_t dev) /* wait for any ongoing conversions to finish */
{ while (dev(line)->SR & ADC_SR_STRT) {}
switch (dev) { /* set resolution and conversion channel */
#if ADC_0_EN dev(line)->CR1 = res;
case ADC_0: dev(line)->SQR3 = adc_config[line].chan;
ADC_0_CLKEN(); /* start conversion and wait for results */
break; dev(line)->CR2 |= ADC_CR2_SWSTART;
#endif while (!(dev(line)->SR & ADC_SR_EOC)) {}
#if ADC_1_EN /* finally read sample and reset the STRT bit in the status register */
case ADC_1: sample = (int)dev(line)->DR;
ADC_1_CLKEN(); dev(line)->SR &= ~ADC_SR_STRT;
break;
#endif
}
}
void adc_poweroff(adc_t dev) /* power off and unlock device again */
{ done(line);
switch (dev) {
#if ADC_0_EN
case ADC_0:
ADC_0_CLKDIS();
break;
#endif
#if ADC_1_EN
case ADC_1:
ADC_1_CLKDIS();
break;
#endif
}
}
int adc_map(adc_t dev, int value, int min, int max) return sample;
{
return (int)adc_mapf(dev, value, (float)min, (float)max);
} }
float adc_mapf(adc_t dev, int value, float min, float max)
{
return ((max - min) / ((float)adc_config[dev].max_value)) * value;
}
#endif /* ADC_NUMOF */

View File

@ -148,6 +148,14 @@ void gpio_init_af(gpio_t pin, gpio_af_t af)
port->AFR[(pin_num > 7) ? 1 : 0] |= (af << ((pin_num & 0x07) * 4)); port->AFR[(pin_num > 7) ? 1 : 0] |= (af << ((pin_num & 0x07) * 4));
} }
void gpio_init_analog(gpio_t pin)
{
/* enable clock */
RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOAEN << _port_num(pin));
/* set to analog mode */
_port(pin)->MODER |= (0x3 << (2 * _pin_num(pin)));
}
void gpio_irq_enable(gpio_t pin) void gpio_irq_enable(gpio_t pin)
{ {
EXTI->IMR |= (1 << _pin_num(pin)); EXTI->IMR |= (1 << _pin_num(pin));