From b2431579e414f837add230e540af226a73ddc8ab Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sat, 2 May 2015 20:26:17 +0000 Subject: [PATCH] git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@7938 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/hal/include/dac.h | 6 +++++ os/hal/ports/STM32/LLD/DACv1/dac_lld.c | 24 ++++++++++++++++-- os/hal/ports/STM32/LLD/DACv1/dac_lld.h | 26 ++++++++++++++++---- os/hal/src/dac.c | 34 ++++++++++++++++++++++++++ testhal/STM32/STM32F4xx/DAC/main.c | 25 ++++++++++++------- 5 files changed, 99 insertions(+), 16 deletions(-) diff --git a/os/hal/include/dac.h b/os/hal/include/dac.h index 9b892bf44..8a23ade4f 100644 --- a/os/hal/include/dac.h +++ b/os/hal/include/dac.h @@ -239,6 +239,12 @@ extern "C" { void dacObjectInit(DACDriver *dacp); void dacStart(DACDriver *dacp, const DACConfig *config); void dacStop(DACDriver *dacp); + void dacPutChannel(DACDriver *dacp, + dacchannel_t channel, + dacsample_t sample); + void dacPutChannelI(DACDriver *dacp, + dacchannel_t channel, + dacsample_t sample); void dacStartConversion(DACDriver *dacp, const DACConversionGroup *grpp, const dacsample_t *samples, size_t depth); void dacStartConversionI(DACDriver *dacp, const DACConversionGroup *grpp, diff --git a/os/hal/ports/STM32/LLD/DACv1/dac_lld.c b/os/hal/ports/STM32/LLD/DACv1/dac_lld.c index 8afa4e505..99ed45ef2 100644 --- a/os/hal/ports/STM32/LLD/DACv1/dac_lld.c +++ b/os/hal/ports/STM32/LLD/DACv1/dac_lld.c @@ -81,7 +81,9 @@ DACDriver DACD4; /* Driver local variables. */ /*===========================================================================*/ -static const DACConfig default_config = {0}; +static const DACConfig default_config = { + datamode: DAC_DHRM_12BIT_RIGHT +}; #if STM32_DAC_USE_DAC1_CH1 == TRUE static const dacparams_t dma1_ch1_params = { @@ -310,6 +312,24 @@ void dac_lld_stop(DACDriver *dacp) { } } +/** + * @brief Outputs a value directly on a DAC channel. + * + * @param[in] dacp pointer to the @p DACDriver object + * @param[in] channel DAC channel number + * @param[in] sample value to be output + * + * @api + */ +void dac_lld_put_channel(DACDriver *dacp, + dacchannel_t channel, + dacsample_t sample) { + + (void)dacp; + (void)channel; + (void)sample; +} + /** * @brief Starts a DAC conversion. * @details Starts an asynchronous conversion operation. @@ -322,7 +342,7 @@ void dac_lld_start_conversion(DACDriver *dacp) { uint32_t cr, dmamode; #if STM32_DAC_DUAL_MODE == FALSE - switch (dacp->grpp->datamode) { + switch (dacp->config->datamode) { /* Sets the DAC data register */ case DAC_DHRM_12BIT_RIGHT: dmaStreamSetPeripheral(dacp->params->dma, &dacp->params->dac->DHR12R1 + diff --git a/os/hal/ports/STM32/LLD/DACv1/dac_lld.h b/os/hal/ports/STM32/LLD/DACv1/dac_lld.h index 36842f295..ddfafc256 100644 --- a/os/hal/ports/STM32/LLD/DACv1/dac_lld.h +++ b/os/hal/ports/STM32/LLD/DACv1/dac_lld.h @@ -226,10 +226,24 @@ #define STM32_DMA_REQUIRED #endif +/** + * @brief Max DAC channels. + */ +#if STM32_DAC_DUAL_MODE == FALSE +#define DAC_MAX_CHANNELS 1 +#else +#define DAC_MAX_CHANNELS 2 +#endif + /*===========================================================================*/ /* Driver data structures and types. */ /*===========================================================================*/ +/** + * @brief Type of a DAC channel index. + */ +typedef uint32_t dacchannel_t; + /** * @brief DAC channel parameters type. */ @@ -336,10 +350,6 @@ typedef struct { */ dacerrorcallback_t error_cb; /* End of the mandatory fields.*/ - /** - * @brief DAC data holding register mode. - */ - dacdhrmode_t datamode; /** * @brief DAC initialization data. * @note This field contains the (not shifted) value to be put into the @@ -354,7 +364,10 @@ typedef struct { */ typedef struct { /* End of the mandatory fields.*/ - uint32_t dummy; + /** + * @brief DAC data holding register mode. + */ + dacdhrmode_t datamode; } DACConfig; /** @@ -433,6 +446,9 @@ extern "C" { void dac_lld_init(void); void dac_lld_start(DACDriver *dacp); void dac_lld_stop(DACDriver *dacp); + void dac_lld_put_channel(DACDriver *dacp, + dacchannel_t channel, + dacsample_t sample); void dac_lld_start_conversion(DACDriver *dacp); void dac_lld_stop_conversion(DACDriver *dacp); #ifdef __cplusplus diff --git a/os/hal/src/dac.c b/os/hal/src/dac.c index a4e0fa672..024547b78 100644 --- a/os/hal/src/dac.c +++ b/os/hal/src/dac.c @@ -130,6 +130,40 @@ void dacStop(DACDriver *dacp) { osalSysUnlock(); } +/** + * @brief Outputs a value directly on a DAC channel. + * + * @param[in] dacp pointer to the @p DACDriver object + * @param[in] channel DAC channel number + * @param[in] sample value to be output + * + * @api + */ +void dacPutChannel(DACDriver *dacp, dacchannel_t channel, dacsample_t sample) { + + osalSysLock(); + dacPutChannelI(dacp, channel, sample); + osalSysUnlock(); +} + +/** + * @brief Outputs a value directly on a DAC channel. + * + * @param[in] dacp pointer to the @p DACDriver object + * @param[in] channel DAC channel number + * @param[in] sample value to be output + * + * @iclass + */ +void dacPutChannelI(DACDriver *dacp, dacchannel_t channel, dacsample_t sample) { + + osalDbgCheckClassI(); + osalDbgCheck(channel < DAC_MAX_CHANNELS); + osalDbgAssert(dacp->state == DAC_READY, "invalid state"); + + dac_lld_put_channel(dacp, channel, sample); +} + /** * @brief Starts a DAC conversion. * @details Starts an asynchronous conversion operation. diff --git a/testhal/STM32/STM32F4xx/DAC/main.c b/testhal/STM32/STM32F4xx/DAC/main.c index d35d42b83..469132559 100644 --- a/testhal/STM32/STM32F4xx/DAC/main.c +++ b/testhal/STM32/STM32F4xx/DAC/main.c @@ -58,17 +58,22 @@ static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = { /* * DAC streaming callback. */ -size_t nx = 0, ny = 0; +size_t nx = 0, ny = 0, nz = 0; static void end_cb1(DACDriver *dacp, const dacsample_t *buffer, size_t n) { (void)dacp; + nz++; if (dac_buffer == buffer) { nx += n; } else { ny += n; } + + if ((nz % 1000) == 0) { + palTogglePad(GPIOD, GPIOD_LED3); + } } /* @@ -82,14 +87,16 @@ static void error_cb1(DACDriver *dacp, dacerror_t err) { chSysHalt("DAC failure"); } -static const DACConversionGroup daccfg1 = { - num_channels: 1, - end_cb: end_cb1, - error_cb: error_cb1, - datamode: DAC_DHRM_12BIT_RIGHT, - trigger: DAC_TRG(0) +static const DACConfig dac1cfg1 = { + datamode: DAC_DHRM_12BIT_RIGHT }; +static const DACConversionGroup dacgrpcfg1 = { + num_channels: 1U, + end_cb: end_cb1, + error_cb: error_cb1, + trigger: DAC_TRG(0) +}; /* * GPT2 configuration. @@ -121,7 +128,7 @@ int main(void) { * by the Reference Manual. */ palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG); - dacStart(&DACD1, NULL); + dacStart(&DACD1, &dac1cfg1); /* * Starting GPT6 driver, it is used for triggering the DAC. @@ -131,7 +138,7 @@ int main(void) { /* * Starting a continuous conversion. */ - dacStartConversion(&DACD1, &daccfg1, dac_buffer, DAC_BUFFER_SIZE); + dacStartConversion(&DACD1, &dacgrpcfg1, dac_buffer, DAC_BUFFER_SIZE); gptStartContinuous(&GPTD6, 2U); /*