diff --git a/boards/frdm-k22f/include/periph_conf.h b/boards/frdm-k22f/include/periph_conf.h index ad2640456e..28c53ae9b6 100644 --- a/boards/frdm-k22f/include/periph_conf.h +++ b/boards/frdm-k22f/include/periph_conf.h @@ -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 * @{ diff --git a/boards/frdm-k64f/include/periph_conf.h b/boards/frdm-k64f/include/periph_conf.h index 297a7ef388..1405ccf925 100644 --- a/boards/frdm-k64f/include/periph_conf.h +++ b/boards/frdm-k64f/include/periph_conf.h @@ -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 * @{ diff --git a/boards/mulle/include/periph_conf.h b/boards/mulle/include/periph_conf.h index 3a7aeb4f09..fecdf2930e 100644 --- a/boards/mulle/include/periph_conf.h +++ b/boards/mulle/include/periph_conf.h @@ -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])) /** @} */ /** diff --git a/boards/pba-d-01-kw2x/include/periph_conf.h b/boards/pba-d-01-kw2x/include/periph_conf.h index 403f0245c0..5dfd2d1a79 100644 --- a/boards/pba-d-01-kw2x/include/periph_conf.h +++ b/boards/pba-d-01-kw2x/include/periph_conf.h @@ -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 * @{ diff --git a/cpu/kinetis_common/include/periph_cpu.h b/cpu/kinetis_common/include/periph_cpu.h index 30094b0606..428cfc8c0e 100644 --- a/cpu/kinetis_common/include/periph_cpu.h +++ b/cpu/kinetis_common/include/periph_cpu.h @@ -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; /** diff --git a/cpu/kinetis_common/periph/dac.c b/cpu/kinetis_common/periph/dac.c index d8127e9652..b8034a6a64 100644 --- a/cpu/kinetis_common/periph/dac.c +++ b/cpu/kinetis_common/periph/dac.c @@ -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 */