1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

cpu/stm32: unified and cleaned up DAC driver

- removed neccessity to define empty `DAC_NUMOF 0` for each STM
  base board
- adapted all board configs to this
- joined stm32f2 to use common DAC driver
- improved code of DAC driver
This commit is contained in:
Hauke Petersen 2017-06-27 12:45:50 +02:00
parent 49c1725d98
commit 73ede74cd8
54 changed files with 99 additions and 481 deletions

View File

@ -158,14 +158,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -110,13 +110,6 @@ static const uart_conf_t uart_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -66,13 +66,6 @@ extern "C" {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name Timer configuration
* @{

View File

@ -128,13 +128,6 @@ static const uart_conf_t uart_config[] = {
#define UART_NUMOF (sizeof(uart_config) / sizeof(uart_config[0]))
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name Real time counter configuration
* @{

View File

@ -49,13 +49,6 @@ extern "C" {
#define CLOCK_APB1 (CLOCK_CORECLOCK / 1)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name Timer configuration
* @{

View File

@ -66,13 +66,6 @@ extern "C" {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name Timer configuration
* @{

View File

@ -110,18 +110,15 @@ static const pwm_conf_t pwm_config[] = {
/** @} */
/**
* @name DAC configuration
*
* We need to define the following fields:
* PIN, DAC channel
* @name DAC configuration
* @{
*/
#define DAC_CONFIG { \
{GPIO_PIN(PORT_A, 4), 0}, \
{GPIO_PIN(PORT_A, 5), 1} \
}
static const dac_conf_t dac_config[] = {
{ .pin = GPIO_PIN(PORT_A, 4), .chan = 0 },
{ .pin = GPIO_PIN(PORT_A, 5), .chan = 1 }
};
#define DAC_NUMOF (2)
#define DAC_NUMOF (sizeof(dac_config) / sizeof(dac_config[0]))
/** @} */
/**

View File

@ -141,13 +141,6 @@ static const pwm_conf_t pwm_config[] = {
#define ADC_NUMOF (6)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -153,13 +153,6 @@ static const pwm_conf_t pwm_config[] = {
#define ADC_NUMOF (6)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -201,13 +201,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (6)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -203,12 +203,6 @@ static const pwm_conf_t pwm_config[] = {
#define ADC_NUMOF (6)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}

View File

@ -66,13 +66,6 @@ extern "C" {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name Timer configuration
* @{

View File

@ -56,13 +56,6 @@ extern "C" {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name Timer configuration
* @{

View File

@ -47,13 +47,6 @@ extern "C" {
#define CLOCK_APB1 (CLOCK_CORECLOCK / 2)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name Timer configuration
* @{

View File

@ -46,20 +46,6 @@ extern "C" {
#define CLOCK_APB1 (CLOCK_CORECLOCK / 2)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name Timer configuration
* @{

View File

@ -251,13 +251,6 @@ static const spi_conf_t spi_config[] = {
}
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -200,13 +200,6 @@ static const spi_conf_t spi_config[] = {
}
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -246,13 +246,6 @@ static const spi_conf_t spi_config[] = {
}
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -275,13 +275,6 @@ static const spi_conf_t spi_config[] = {
}
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -170,14 +170,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -181,14 +181,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name I2C configuration
* @{

View File

@ -236,14 +236,15 @@ static const i2c_conf_t i2c_config[] = {
* @name DAC configuration
* @{
*/
#define DAC_CONFIG { \
{ GPIO_PIN(PORT_A, 4), 1}, \
{ GPIO_PIN(PORT_A, 5), 2}, \
}
static const dac_conf_t dac_config[] = {
{ .pin = GPIO_PIN(PORT_A, 4), .chan = 0 },
{ .pin = GPIO_PIN(PORT_A, 5), .chan = 1 }
};
#define DAC_NUMOF (2U)
#define DAC_NUMOF (sizeof(dac_config) / sizeof(dac_config[0]))
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -227,13 +227,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTT configuration
*

View File

@ -259,14 +259,6 @@ static const spi_conf_t spi_config[] = {
{GPIO_PIN(PORT_C, 0), 1, 0} \
}
#define ADC_NUMOF (2)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**

View File

@ -208,13 +208,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -246,13 +246,6 @@ static const spi_conf_t spi_config[] = {
}
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -246,14 +246,6 @@ static const spi_conf_t spi_config[] = {
}
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -244,13 +244,6 @@ static const spi_conf_t spi_config[] = {
}
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -230,13 +230,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -132,13 +132,6 @@ static const uart_conf_t uart_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -132,13 +132,6 @@ static const uart_conf_t uart_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -189,13 +189,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (5)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -199,13 +199,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (5)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -181,13 +181,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -156,13 +156,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -190,13 +190,6 @@ static const spi_conf_t spi_config[] = {
#define ADC_NUMOF (0U)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0U)
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -237,17 +237,18 @@ static const i2c_conf_t i2c_config[] = {
/** @} */
/**
* @name DAC configuration
* @name DAC configuration
* @{
*/
#define DAC_CONFIG { \
{ GPIO_PIN(PORT_A, 4), 1}, \
{ GPIO_PIN(PORT_A, 5), 2}, \
}
static const dac_conf_t dac_config[] = {
{ .pin = GPIO_PIN(PORT_A, 4), .chan = 0 },
{ .pin = GPIO_PIN(PORT_A, 5), .chan = 1 }
};
#define DAC_NUMOF (2)
#define DAC_NUMOF (sizeof(dac_config) / sizeof(dac_config[0]))
/** @} */
#ifdef __cplusplus
}
#endif

View File

@ -66,13 +66,6 @@ extern "C" {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name Timer configuration
* @{

View File

@ -59,13 +59,6 @@
#define CLOCK_APB1 (CLOCK_CORECLOCK / 2)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name ADC configuration
* @{

View File

@ -112,13 +112,6 @@ static const uart_conf_t uart_config[] = {
#define ADC_NUMOF (6)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name SPI configuration
* @{

View File

@ -49,11 +49,11 @@ extern "C" {
* @name DAC configuration
* @{
*/
#define DAC_CONFIG { \
{ GPIO_PIN(PORT_A, 4), 0, 0 }, \
}
static const dac_conf_t dac_config[] = {
{ .pin = GPIO_PIN(PORT_A, 4), .chan = 0 }
};
#define DAC_NUMOF (1)
#define DAC_NUMOF (sizeof(dac_config) / sizeof(dac_config[0]))
/** @} */
/**

View File

@ -137,17 +137,14 @@ static const uart_conf_t uart_config[] = {
/**
* @name DAC configuration
*
* We need to define the following fields:
* PIN, DAC channel
* @{
*/
#define DAC_CONFIG { \
{ GPIO_PIN(PORT_A, 4), 0 }, \
{ GPIO_PIN(PORT_A, 5), 1 }, \
}
static const dac_conf_t dac_config[] = {
{ .pin = GPIO_PIN(PORT_A, 4), .chan = 0 },
{ .pin = GPIO_PIN(PORT_A, 5), .chan = 1 }
};
#define DAC_NUMOF (2)
#define DAC_NUMOF (sizeof(dac_config) / sizeof(dac_config[0]))
/** @} */
/**

View File

@ -100,13 +100,6 @@ static const uart_conf_t uart_config[] = {
#define ADC_NUMOF (0)
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_NUMOF (0)
/** @} */
/**
* @name RTC configuration
* @{

View File

@ -190,6 +190,14 @@ typedef enum {
#endif /* ndef DOXYGEN */
#endif /* ndef CPU_FAM_STM32F1 */
/**
* @brief DAC line configuration data
*/
typedef struct {
gpio_t pin; /**< pin connected to the line */
uint8_t chan; /**< DAC device used for this line */
} dac_conf_t;
/**
* @brief Timer configuration
*/

View File

@ -9,7 +9,7 @@
/**
* @ingroup cpu_cortexm_common
* @ingroup drivers_periph_adc
* @ingroup drivers_periph_dac
* @{
*
* @file
@ -22,22 +22,37 @@
*/
#include "cpu.h"
#include "assert.h"
#include "periph/dac.h"
#include "periph_conf.h"
/* only compile this, if the CPU has a DAC */
#if (defined(DAC) || defined(DAC1)) && defined(DAC_CONFIG)
#ifdef DAC_NUMOF
#ifdef DAC2
#define _DAC(line) (dac_config[line].dac ? DAC2 : DAC1)
/* DAC channel enable bits */
#ifdef DAC_CR_EN2
#define EN_MASK (DAC_CR_EN1 | DAC_CR_EN2)
#else
#define _DAC(line) DAC
#define EN_MASK (DAC_CR_EN1)
#endif
/**
* @brief Get the DAC configuration from the board config
*/
static const dac_conf_t dac_config[] = DAC_CONFIG;
/* get RCC bit */
#ifdef RCC_APB1ENR_DAC1EN
#define RCC_BIT (RCC_APB1ENR_DAC1EN)
#else
#define RCC_BIT (RCC_APB1ENR_DACEN)
#endif
/* deduct DAC device from given line channel */
static inline DAC_TypeDef *dev(dac_t line)
{
#if defined(DAC2)
return (dac_config[line].chan > 1) ? DAC2 : DAC1;
#elif defined (DAC1)
return DAC1;
#else
return DAC;
#endif
}
int8_t dac_init(dac_t line)
{
@ -47,46 +62,65 @@ int8_t dac_init(dac_t line)
/* configure pin */
gpio_init_analog(dac_config[line].pin);
/* enable the DAC's clock */
#if defined(DAC2)
periph_clk_en(APB1, dac_config[line].dac ?
RCC_APB1ENR_DAC2EN : RCC_APB1ENR_DAC1EN);
#elif defined(DAC1)
periph_clk_en(APB1, RCC_APB1ENR_DAC1EN);
#else
periph_clk_en(APB1, RCC_APB1ENR_DACEN);
#endif
/* reset output and enable the line's channel */
dac_set(line, 0);
dac_poweron(line);
dac_set(line, 0);
return DAC_OK;
}
void dac_set(dac_t line, uint16_t value)
{
value = (value >> 4); /* scale to 12-bit */
assert(line < DAC_NUMOF);
/* scale set value to 12-bit */
value = (value >> 4);
#ifdef DAC_DHR12R2_DACC2DHR
if (dac_config[line].chan) {
_DAC(line)->DHR12R2 = value;
if (dac_config[line].chan & 0x01) {
dev(line)->DHR12R2 = value;
}
else {
_DAC(line)->DHR12R1 = value;
dev(line)->DHR12R1 = value;
}
#else
(void) line;
_DAC(line)->DHR12R1 = value;
dev(line)->DHR12R1 = value;
#endif
}
void dac_poweron(dac_t line)
{
DAC->CR |= (1 << (16 * dac_config[line].chan));
assert(line < DAC_NUMOF);
/* enable the DAC's clock */
#if defined(DAC2)
periph_clk_en(APB1, (dac_config[line].chan > 1) ?
RCC_APB1ENR_DAC2EN : RCC_APB1ENR_DAC1EN);
#else
periph_clk_en(APB1, RCC_BIT);
#endif
/* enable corresponding DAC channel */
dev(line)->CR |= (1 << (16 * (dac_config[line].chan & 0x01)));
}
void dac_poweroff(dac_t line)
{
DAC->CR &= ~(1 << (16 * dac_config[line].chan));
assert(line < DAC_NUMOF);
/* disable corresponding channel */
dev(line)->CR &= ~(1 << (16 * (dac_config[line].chan & 0x01)));
/* disable the DAC's clock in case no channel is active anymore */
if (!(dev(line)->CR & EN_MASK)) {
#if defined(DAC2)
periph_clk_dis(APB1, (dac_config[line].chan > 1) ?
RCC_APB1ENR_DAC2EN : RCC_APB1ENR_DAC1EN);
#else
periph_clk_dis(APB1, RCC_BIT);
#endif
}
}
#else

View File

@ -62,14 +62,6 @@ typedef struct {
uint8_t chan; /**< internal channel the pin is connected to */
} adc_conf_t;
/**
* @brief DAC line configuration data
*/
typedef struct {
gpio_t pin; /**< pin connected to the line */
uint8_t chan; /**< DAC device used for this line */
} dac_conf_t;
#ifdef __cplusplus
}
#endif

View File

@ -129,14 +129,6 @@ typedef struct {
uint8_t chan; /**< CPU ADC channel connected to the pin */
} adc_conf_t;
/**
* @brief DAC line configuration data
*/
typedef struct {
gpio_t pin; /**< pin connected to the line */
uint8_t chan; /**< DAC device used for this line */
} dac_conf_t;
#ifdef __cplusplus
}
#endif

View File

@ -71,14 +71,6 @@ typedef enum {
} adc_res_t;
/** @} */
/**
* @brief DAC line configuration data
*/
typedef struct {
gpio_t pin; /**< pin connected to the line */
uint8_t chan; /**< DAC device used for this line */
} dac_conf_t;
/**
* @brief Power on the DMA device the given stream belongs to
*

View File

@ -1,77 +0,0 @@
/*
* Copyright (C) 2015 Engineering-Spirit
*
* 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
* details.
*/
/**
* @ingroup cpu_stm32f2
* @ingroup drivers_periph_dac
* @{
*
* @file
* @brief Low-level DAC driver implementation
*
* @author Nick v. IJzendoorn <nijzendoorn@engineering-spirit.nl>
*
* @}
*/
#include "cpu.h"
#include "periph/dac.h"
#include "periph_conf.h"
/* guard in case that no DAC device is defined */
#if DAC_NUMOF
/**
* @brief Get the DAC configuration from the board (if configured)
* @{
*/
#ifdef DAC_CONFIG
static const dac_conf_t dac_config[] = DAC_CONFIG;
#else
static const dac_conf_t dac_config[] = {};
#endif
/** @} */
int8_t dac_init(dac_t line)
{
if (line >= DAC_NUMOF) {
return DAC_NOLINE;
}
/* configure pin */
gpio_init_analog(dac_config[line].pin);
/* enable the DAC's clock */
periph_clk_en(APB1, RCC_APB1ENR_DACEN);
/* reset output and enable the line's channel */
dac_set(line, 0);
dac_poweron(line);
return DAC_OK;
}
void dac_set(dac_t line, uint16_t value)
{
value = (value >> 4); /* scale to 12-bit */
if (dac_config[line].chan) {
DAC->DHR12R2 = value;
}
else {
DAC->DHR12R1 = value;
}
}
void dac_poweron(dac_t line)
{
DAC->CR |= (1 << (16 * dac_config[line].chan));
}
void dac_poweroff(dac_t line)
{
DAC->CR &= ~(1 << (16 * dac_config[line].chan));
}
#endif /* DAC_NUMOF */

View File

@ -39,15 +39,6 @@ enum {
PORT_H = 7, /**< port H */
};
/**
* @brief DAC line configuration support
*/
typedef struct {
gpio_t pin; /**< pin connected to the line */
uint8_t dac; /**< The DAC to use, 0 or 1 */
uint8_t chan; /**< DAC device used for this line */
} dac_conf_t;
#ifdef __cplusplus
}
#endif

View File

@ -75,14 +75,6 @@ typedef struct {
uint8_t chan; /**< CPU ADC channel connected to the pin */
} adc_conf_t;
/**
* @brief DAC line configuration data
*/
typedef struct {
gpio_t pin; /**< pin connected to the line */
uint8_t chan; /**< DAC device used for this line */
} dac_conf_t;
/**
* @brief Power on the DMA device the given stream belongs to
*

View File

@ -65,14 +65,6 @@ typedef struct {
uint8_t chan; /**< internal channel the pin is connected to */
} adc_conf_t;
/**
* @brief DAC line configuration data
*/
typedef struct {
gpio_t pin; /**< pin connected to the line */
uint8_t chan; /**< DAC device used for this line */
} dac_conf_t;
/**
* @brief I2C configuration data structure
*/

View File

@ -41,15 +41,6 @@ enum {
PORT_H = 5, /**< port H */
};
/**
* @brief DAC line configuration data
*/
typedef struct {
gpio_t pin; /**< pin connected to the line */
uint8_t chan; /**< DAC device used for this line */
} dac_conf_t;
/**
* @brief ADC channel configuration data
*/

View File

@ -65,14 +65,6 @@ typedef struct {
uint8_t chan; /**< internal channel the pin is connected to */
} adc_conf_t;
/**
* @brief DAC line configuration data
*/
typedef struct {
gpio_t pin; /**< pin connected to the line */
uint8_t chan; /**< DAC device used for this line */
} dac_conf_t;
#ifdef __cplusplus
}
#endif