1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

cpu/kinetis: optimization of DAC driver impl.

- use assert() for checking the line parameter
- use 'bit.h' for bitbanding
- simplified code a bit
- unified style of defifining the board configuration
- removed unused configurations form pba-d-01-kw2x and frdm-k64f
This commit is contained in:
Hauke Petersen 2017-06-27 11:30:45 +02:00
parent da4f2f6e6e
commit 3c623f63c3
6 changed files with 40 additions and 79 deletions

View File

@ -120,14 +120,6 @@ static const adc_conf_t adc_config[] = {
#define ADC_NUMOF (sizeof(adc_config) / sizeof(adc_config[0]))
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_CONFIG {}
#define DAC_NUMOF 0
/** @} */
/**
* @name PWM configuration
* @{

View File

@ -114,14 +114,6 @@ static const adc_conf_t adc_config[] = {
#define ADC_NUMOF (sizeof(adc_config) / sizeof(adc_config[0]))
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_CONFIG {}
#define DAC_NUMOF 0
/** @} */
/**
* @name PWM configuration
* @{

View File

@ -172,15 +172,18 @@ static const adc_conf_t adc_config[] = {
/** @} */
/**
* @name DAC configuration
* @name DAC configuration
* @{
*/
static const dac_conf_t dac_config[] = {
{
.dev = DAC0,
.scgc_addr = &SIM->SCGC2,
.scgc_bit = SIM_SCGC2_DAC0_SHIFT
}
};
#define DAC_CONFIG { \
{ DAC0, (uint32_t volatile *)BITBAND_REGADDR(SIM->SCGC2, SIM_SCGC2_DAC0_SHIFT) }, \
}
#define DAC_NUMOF 1
#define DAC_NUMOF (sizeof(dac_config) / sizeof(dac_config[0]))
/** @} */
/**

View File

@ -129,14 +129,6 @@ static const adc_conf_t adc_config[] = {
#define ADC_NUMOF (sizeof(adc_config) / sizeof(adc_config[0]))
/** @} */
/**
* @name DAC configuration
* @{
*/
#define DAC_CONFIG {}
#define DAC_NUMOF 0
/** @} */
/**
* @name PWM configuration
* @{

View File

@ -239,10 +239,9 @@ typedef struct {
* @brief CPU specific DAC configuration
*/
typedef struct {
/** DAC device base pointer */
DAC_Type *dev;
/** Pointer to module clock gate bit in bitband region, use BITBAND_REGADDR() */
uint32_t volatile *clk_gate;
DAC_Type *dev; /**< DAC device base pointer */
volatile uint32_t *scgc_addr; /**< Clock enable register, in SIM module */
uint8_t scgc_bit; /**< Clock enable bit, within the register */
} dac_conf_t;
/**

View File

@ -27,79 +27,62 @@
#include "cpu.h"
#include "bit.h"
#include "assert.h"
#include "periph/dac.h"
#include "periph_conf.h"
static const dac_conf_t dac_config[] = DAC_CONFIG;
/* only compile this file if there are DAC lines defined */
#ifdef DAC_NUMOF
static inline DAC_Type *dev(dac_t line)
{
return dac_config[line].dev;
}
int8_t dac_init(dac_t line)
{
DAC_Type *dac;
if ((unsigned int)line >= DAC_NUMOF) {
if (line >= DAC_NUMOF) {
return DAC_NOLINE;
}
dac = dac_config[line].dev;
/* Enable module clock */
*(dac_config[line].clk_gate) = 1;
bit_set32(dac_config[line].scgc_addr, dac_config[line].scgc_bit);
/* Select VDDA as voltage reference */
dac->C0 = (DAC_C0_DACRFS_MASK);
dev(line)->C0 = (DAC_C0_DACRFS_MASK);
/* Disable DMA and buffering */
dac->C1 = 0;
dac->C2 = 0;
/* Power on */
dac_poweron(line);
dev(line)->C1 = 0;
dev(line)->C2 = 0;
/* Enable the device */
bit_set8(&dac_config[line].dev->C0, DAC_C0_DACEN_SHIFT);
/* Set output value to zero */
dac_set(line, 0);
return DAC_OK;
}
void dac_set(dac_t line, uint16_t value)
{
DAC_Type *dac;
if ((unsigned int)line >= DAC_NUMOF) {
return;
}
dac = dac_config[line].dev;
assert(line < DAC_NUMOF);
/* Scale to 12 bit */
value = value >> ((sizeof(value) * 8) - 12);
value = (value >> 4);
dac->DAT[0].DATH = ((value >> 8) & 0xff);
dac->DAT[0].DATL = (value & 0xff);
}
static inline void _dac_set_power(dac_t line, uint8_t value)
{
DAC_Type *dac;
if ((unsigned int)line >= DAC_NUMOF) {
return;
}
dac = dac_config[line].dev;
if (value) {
bit_set8(&dac->C0, DAC_C0_DACEN_SHIFT);
}
else {
bit_clear8(&dac->C0, DAC_C0_DACEN_SHIFT);
}
dev(line)->DAT[0].DATH = ((value >> 8) & 0xff);
dev(line)->DAT[0].DATL = (value & 0xff);
}
void dac_poweron(dac_t line)
{
_dac_set_power(line, 1);
assert(line < DAC_NUMOF);
bit_set8(&dac_config[line].dev->C0, DAC_C0_DACEN_SHIFT);
}
void dac_poweroff(dac_t line)
{
_dac_set_power(line, 0);
assert(line < DAC_NUMOF);
bit_clear8(&dac_config[line].dev->C0, DAC_C0_DACEN_SHIFT);
}
#endif /* DAC_NUMOF */