2014-07-26 09:24:53 +00:00
|
|
|
/*
|
2015-01-11 13:56:55 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
|
2014-07-26 09:24:53 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file STM32/DACv1/dac_lld.c
|
|
|
|
* @brief STM32 DAC subsystem low level driver source.
|
|
|
|
*
|
|
|
|
* @addtogroup DAC
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#if HAL_USE_DAC || defined(__DOXYGEN__)
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
/* Because ST headers naming inconsistencies.*/
|
2014-07-26 09:24:53 +00:00
|
|
|
#if !defined(DAC1)
|
|
|
|
#define DAC1 DAC
|
|
|
|
#endif
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
#define DAC1_CH1_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_DAC1_CH1_DMA_STREAM, \
|
|
|
|
STM32_DAC1_CH1_DMA_CHN)
|
2014-07-26 09:24:53 +00:00
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
#define DAC1_CH2_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_DAC1_CH2_DMA_STREAM, \
|
|
|
|
STM32_DAC1_CH2_DMA_CHN)
|
|
|
|
|
|
|
|
#define DAC2_CH1_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_DAC2_CH1_DMA_STREAM, \
|
|
|
|
STM32_DAC2_CH1_DMA_CHN)
|
|
|
|
|
|
|
|
#define DAC2_CH2_DMA_CHANNEL \
|
|
|
|
STM32_DMA_GETCHANNEL(STM32_DAC2_CH2_DMA_STREAM, \
|
|
|
|
STM32_DAC2_CH2_DMA_CHN)
|
|
|
|
|
2015-05-02 09:11:13 +00:00
|
|
|
#define CHANNEL_DATA_OFFSET 12U
|
2014-07-26 09:24:53 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
/** @brief DAC1 CH1 driver identifier.*/
|
|
|
|
#if STM32_DAC_USE_DAC1_CH1 || defined(__DOXYGEN__)
|
2014-07-26 09:24:53 +00:00
|
|
|
DACDriver DACD1;
|
|
|
|
#endif
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
/** @brief DAC1 CH2 driver identifier.*/
|
|
|
|
#if (STM32_DAC_USE_DAC1_CH2 && !STM32_DAC_DUAL_MODE) || defined(__DOXYGEN__)
|
2014-07-26 09:24:53 +00:00
|
|
|
DACDriver DACD2;
|
|
|
|
#endif
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
/** @brief DAC2 CH1 driver identifier.*/
|
|
|
|
#if STM32_DAC_USE_DAC2_CH1 || defined(__DOXYGEN__)
|
2014-07-26 09:24:53 +00:00
|
|
|
DACDriver DACD3;
|
|
|
|
#endif
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
/** @brief DAC2 CH2 driver identifier.*/
|
|
|
|
#if (STM32_DAC_USE_DAC2_CH2 && !STM32_DAC_DUAL_MODE) || defined(__DOXYGEN__)
|
|
|
|
DACDriver DACD4;
|
|
|
|
#endif
|
|
|
|
|
2014-07-26 09:24:53 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2015-05-02 09:11:13 +00:00
|
|
|
#if STM32_DAC_USE_DAC1_CH1 == TRUE
|
|
|
|
static const dacparams_t dma1_ch1_params = {
|
|
|
|
dac: DAC1,
|
|
|
|
dataoffset: 0U,
|
|
|
|
regshift: 0U,
|
|
|
|
regmask: 0xFFFF0000U,
|
|
|
|
dma: STM32_DMA_STREAM(STM32_DAC1_CH1_DMA_STREAM),
|
|
|
|
dmamode: STM32_DMA_CR_CHSEL(DAC1_CH1_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_DAC1_CH1_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_DIR_M2P |
|
|
|
|
STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE | STM32_DMA_CR_HTIE |
|
|
|
|
STM32_DMA_CR_TCIE,
|
|
|
|
dmairqprio: STM32_DAC1_CH1_IRQ_PRIORITY
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if STM32_DAC_USE_DAC1_CH2 == TRUE
|
|
|
|
static const dacparams_t dma1_ch2_params = {
|
|
|
|
dac: DAC1,
|
|
|
|
dataoffset: CHANNEL_DATA_OFFSET,
|
|
|
|
regshift: 16U,
|
|
|
|
regmask: 0x0000FFFFU,
|
|
|
|
dma: STM32_DMA_STREAM(STM32_DAC1_CH2_DMA_STREAM),
|
|
|
|
dmamode: STM32_DMA_CR_CHSEL(DAC1_CH2_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_DAC1_CH2_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_DIR_M2P |
|
|
|
|
STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE | STM32_DMA_CR_HTIE |
|
|
|
|
STM32_DMA_CR_TCIE,
|
|
|
|
dmairqprio: STM32_DAC1_CH2_IRQ_PRIORITY
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if STM32_DAC_USE_DAC2_CH1 == TRUE
|
|
|
|
static const dacparams_t dma2_ch1_params = {
|
|
|
|
dac: DAC2,
|
|
|
|
dataoffset: 0U,
|
|
|
|
regshift: 0U,
|
|
|
|
regmask: 0xFFFF0000U,
|
|
|
|
dma: STM32_DMA_STREAM(STM32_DAC2_CH1_DMA_STREAM),
|
|
|
|
dmamode: STM32_DMA_CR_CHSEL(DAC2_CH1_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_DAC2_CH1_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_DIR_M2P |
|
|
|
|
STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE | STM32_DMA_CR_HTIE |
|
|
|
|
STM32_DMA_CR_TCIE,
|
|
|
|
dmairqprio: STM32_DAC2_CH1_IRQ_PRIORITY
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if STM32_DAC_USE_DAC2_CH2 == TRUE
|
|
|
|
static const dacparams_t dma1_ch2_params = {
|
|
|
|
dac: DAC2,
|
|
|
|
dataoffset: CHANNEL_DATA_OFFSET,
|
|
|
|
regshift: 16U,
|
|
|
|
regmask: 0x0000FFFFU,
|
|
|
|
dma: STM32_DMA_STREAM(STM32_DAC2_CH2_DMA_STREAM),
|
|
|
|
dmamode: STM32_DMA_CR_CHSEL(DAC2_CH2_DMA_CHANNEL) |
|
|
|
|
STM32_DMA_CR_PL(STM32_DAC2_CH2_DMA_PRIORITY) |
|
|
|
|
STM32_DMA_CR_MINC | STM32_DMA_CR_CIRC | STM32_DMA_CR_DIR_M2P |
|
|
|
|
STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE | STM32_DMA_CR_HTIE |
|
|
|
|
STM32_DMA_CR_TCIE,
|
|
|
|
dmairqprio: STM32_DAC2_CH2_IRQ_PRIORITY
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2014-07-26 09:24:53 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Shared end/half-of-tx service routine.
|
|
|
|
*
|
|
|
|
* @param[in] dacp pointer to the @p DACDriver object
|
|
|
|
* @param[in] flags pre-shifted content of the ISR register
|
|
|
|
*/
|
|
|
|
static void dac_lld_serve_tx_interrupt(DACDriver *dacp, uint32_t flags) {
|
|
|
|
|
|
|
|
if ((flags & (STM32_DMA_ISR_TEIF | STM32_DMA_ISR_DMEIF)) != 0) {
|
|
|
|
/* DMA errors handling.*/
|
2015-05-01 16:03:03 +00:00
|
|
|
_dac_isr_error_code(dacp, DAC_ERR_DMAFAILURE);
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((flags & STM32_DMA_ISR_HTIF) != 0) {
|
|
|
|
/* Half transfer processing.*/
|
2015-05-01 16:03:03 +00:00
|
|
|
_dac_isr_half_code(dacp);
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
|
|
|
if ((flags & STM32_DMA_ISR_TCIF) != 0) {
|
|
|
|
/* Transfer complete processing.*/
|
2015-05-01 16:03:03 +00:00
|
|
|
_dac_isr_full_code(dacp);
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Low level DAC driver initialization.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void dac_lld_init(void) {
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
#if STM32_DAC_USE_DAC1_CH1
|
2014-07-26 09:24:53 +00:00
|
|
|
dacObjectInit(&DACD1);
|
2015-05-02 09:11:13 +00:00
|
|
|
DACD1.params = &dma1_ch1_params;
|
2014-07-26 09:24:53 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
#if STM32_DAC_USE_DAC1_CH2
|
2014-07-26 09:24:53 +00:00
|
|
|
dacObjectInit(&DACD2);
|
2015-05-02 09:11:13 +00:00
|
|
|
DACD2.params = &dma1_ch2_params;
|
2014-07-26 09:24:53 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
#if STM32_DAC_USE_DAC2_CH1
|
2014-07-26 09:24:53 +00:00
|
|
|
dacObjectInit(&DACD3);
|
2015-05-02 09:11:13 +00:00
|
|
|
DACD3.params = &dma2_ch1_params;
|
2015-05-01 16:03:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if STM32_DAC_USE_DAC2_CH2
|
|
|
|
dacObjectInit(&DACD4);
|
2015-05-02 09:11:13 +00:00
|
|
|
DACD4.params = &dma2_ch2_params;
|
2014-07-26 09:24:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Configures and activates the DAC peripheral.
|
|
|
|
*
|
|
|
|
* @param[in] dacp pointer to the @p DACDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void dac_lld_start(DACDriver *dacp) {
|
2015-05-01 16:03:03 +00:00
|
|
|
|
2015-05-02 09:11:13 +00:00
|
|
|
/* If the driver is in DAC_STOP state then a full initialization is
|
|
|
|
required.*/
|
|
|
|
if (dacp->state == DAC_STOP) {
|
|
|
|
/* Enabling the clock source.*/
|
|
|
|
#if STM32_DAC_USE_DAC1_CH1
|
|
|
|
if (&DACD1 == dacp) {
|
|
|
|
rccEnableDAC1(false);
|
|
|
|
}
|
2014-07-26 09:24:53 +00:00
|
|
|
#endif
|
2015-05-01 16:03:03 +00:00
|
|
|
|
|
|
|
#if STM32_DAC_USE_DAC1_CH2
|
2014-07-26 09:24:53 +00:00
|
|
|
if (&DACD2 == dacp) {
|
2015-05-02 09:11:13 +00:00
|
|
|
rccEnableDAC1(false);
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-05-01 16:03:03 +00:00
|
|
|
|
|
|
|
#if STM32_DAC_USE_DAC2_CH1
|
2014-07-26 09:24:53 +00:00
|
|
|
if (&DACD3 == dacp) {
|
2015-05-02 09:11:13 +00:00
|
|
|
rccEnableDAC2(false);
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-05-01 16:03:03 +00:00
|
|
|
|
|
|
|
#if STM32_DAC_USE_DAC2_CH2
|
|
|
|
if (&DACD3 == dacp) {
|
2015-05-02 09:11:13 +00:00
|
|
|
rccEnableDAC2(false);
|
2015-05-01 16:03:03 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-02 12:28:09 +00:00
|
|
|
/* Enabling DAC in SW triggering mode initially, initializing data to
|
|
|
|
zero.*/
|
2015-05-01 16:03:03 +00:00
|
|
|
#if STM32_DAC_DUAL_MODE == FALSE
|
2015-05-02 12:28:09 +00:00
|
|
|
dacp->params->dac->CR &= dacp->params->regmask;
|
|
|
|
dacp->params->dac->CR |= DAC_CR_EN1 << dacp->params->regshift;
|
2015-05-03 09:04:25 +00:00
|
|
|
dac_lld_put_channel(dacp, 0U, dacp->config->init);
|
2015-05-01 16:03:03 +00:00
|
|
|
#else
|
2015-05-03 12:49:42 +00:00
|
|
|
if ((dacp->config->datamode == DAC_DHRM_12BIT_RIGHT_DUAL) ||
|
|
|
|
(dacp->config->datamode == DAC_DHRM_12BIT_LEFT_DUAL) ||
|
|
|
|
(dacp->config->datamode == DAC_DHRM_8BIT_RIGHT_DUAL)) {
|
|
|
|
dacp->params->dac->CR = DAC_CR_EN2 | DAC_CR_EN1;
|
|
|
|
dac_lld_put_channel(dacp, 1U, dacp->config->init);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dacp->params->dac->CR = DAC_CR_EN1;
|
|
|
|
}
|
2015-05-03 09:04:25 +00:00
|
|
|
dac_lld_put_channel(dacp, 0U, dacp->config->init);
|
2014-07-26 09:24:53 +00:00
|
|
|
#endif
|
2015-05-02 12:28:09 +00:00
|
|
|
}
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Deactivates the DAC peripheral.
|
|
|
|
*
|
|
|
|
* @param[in] dacp pointer to the @p DACDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void dac_lld_stop(DACDriver *dacp) {
|
|
|
|
|
|
|
|
/* If in ready state then disables the DAC clock.*/
|
|
|
|
if (dacp->state == DAC_READY) {
|
|
|
|
|
2015-05-02 12:28:09 +00:00
|
|
|
/* Disabling DAC.*/
|
|
|
|
dacp->params->dac->CR &= dacp->params->regmask;
|
|
|
|
|
2015-05-01 16:03:03 +00:00
|
|
|
#if STM32_DAC_USE_DAC1_CH1
|
2014-07-26 09:24:53 +00:00
|
|
|
if (&DACD1 == dacp) {
|
2015-05-02 09:11:13 +00:00
|
|
|
if ((dacp->params->dac->CR & DAC_CR_EN2) == 0U) {
|
2015-05-01 16:03:03 +00:00
|
|
|
rccDisableDAC1(false);
|
|
|
|
}
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
2015-05-01 16:03:03 +00:00
|
|
|
|
|
|
|
#if STM32_DAC_USE_DAC1_CH2
|
2014-07-26 09:24:53 +00:00
|
|
|
if (&DACD2 == dacp) {
|
2015-05-02 09:11:13 +00:00
|
|
|
if ((dacp->params->dac->CR & DAC_CR_EN1) == 0U) {
|
2015-05-01 16:03:03 +00:00
|
|
|
rccDisableDAC1(false);
|
|
|
|
}
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
2015-05-01 16:03:03 +00:00
|
|
|
#endif
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 20:26:17 +00:00
|
|
|
/**
|
|
|
|
* @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) {
|
|
|
|
|
2015-05-03 08:51:07 +00:00
|
|
|
switch (dacp->config->datamode) {
|
|
|
|
case DAC_DHRM_12BIT_RIGHT:
|
2015-05-03 12:49:42 +00:00
|
|
|
case DAC_DHRM_12BIT_RIGHT_DUAL:
|
2015-05-03 08:51:07 +00:00
|
|
|
if (channel == 0U) {
|
|
|
|
dacp->params->dac->DHR12R1 = (uint32_t)sample;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dacp->params->dac->DHR12R2 = (uint32_t)sample;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DAC_DHRM_12BIT_LEFT:
|
2015-05-03 12:49:42 +00:00
|
|
|
case DAC_DHRM_12BIT_LEFT_DUAL:
|
2015-05-03 08:51:07 +00:00
|
|
|
if (channel == 0U) {
|
|
|
|
dacp->params->dac->DHR12L1 = (uint32_t)sample;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dacp->params->dac->DHR12L2 = (uint32_t)sample;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DAC_DHRM_8BIT_RIGHT:
|
2015-05-03 12:49:42 +00:00
|
|
|
case DAC_DHRM_8BIT_RIGHT_DUAL:
|
2015-05-03 08:51:07 +00:00
|
|
|
if (channel == 0U) {
|
|
|
|
dacp->params->dac->DHR8R1 = (uint32_t)sample;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dacp->params->dac->DHR8R2 = (uint32_t)sample;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
chDbgAssert(false, "unexpected DAC mode");
|
|
|
|
break;
|
|
|
|
}
|
2015-05-02 20:26:17 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 09:24:53 +00:00
|
|
|
/**
|
2015-05-01 16:03:03 +00:00
|
|
|
* @brief Starts a DAC conversion.
|
|
|
|
* @details Starts an asynchronous conversion operation.
|
2015-05-03 12:49:42 +00:00
|
|
|
* @note In @p DAC_DHRM_8BIT_RIGHT mode the parameters passed to the
|
|
|
|
* callback are wrong because two samples are packed in a single
|
|
|
|
* dacsample_t element. This will not be corrected, do not rely
|
|
|
|
* on those parameters.
|
|
|
|
* @note In @p DAC_DHRM_8BIT_RIGHT_DUAL mode two samples are treated
|
|
|
|
* as a single 16 bits sample and packed into a single dacsample_t
|
|
|
|
* element. The num_channels must be set to one in the group
|
|
|
|
* conversion configuration structure.
|
2014-07-26 09:24:53 +00:00
|
|
|
*
|
|
|
|
* @param[in] dacp pointer to the @p DACDriver object
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void dac_lld_start_conversion(DACDriver *dacp) {
|
2015-05-03 12:49:42 +00:00
|
|
|
uint32_t n, cr, dmamode;
|
|
|
|
|
|
|
|
/* Number of DMA operations per buffer.*/
|
|
|
|
n = dacp->depth * dacp->grpp->num_channels;
|
2015-05-02 09:11:13 +00:00
|
|
|
|
2015-05-03 08:51:07 +00:00
|
|
|
/* Allocating the DMA channel.*/
|
|
|
|
bool b = dmaStreamAllocate(dacp->params->dma, dacp->params->dmairqprio,
|
|
|
|
(stm32_dmaisr_t)dac_lld_serve_tx_interrupt,
|
|
|
|
(void *)dacp);
|
|
|
|
osalDbgAssert(!b, "stream already allocated");
|
|
|
|
|
2015-05-03 12:49:42 +00:00
|
|
|
/* DMA settings depend on the chosed DAC mode.*/
|
2015-05-02 20:26:17 +00:00
|
|
|
switch (dacp->config->datamode) {
|
2015-05-02 09:11:13 +00:00
|
|
|
/* Sets the DAC data register */
|
|
|
|
case DAC_DHRM_12BIT_RIGHT:
|
2015-05-03 12:49:42 +00:00
|
|
|
osalDbgAssert(dacp->grpp->num_channels == 1, "invalid number of channels");
|
|
|
|
|
2015-05-02 09:11:13 +00:00
|
|
|
dmaStreamSetPeripheral(dacp->params->dma, &dacp->params->dac->DHR12R1 +
|
|
|
|
dacp->params->dataoffset);
|
|
|
|
dmamode = dacp->params->dmamode |
|
|
|
|
STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD;
|
|
|
|
break;
|
|
|
|
case DAC_DHRM_12BIT_LEFT:
|
2015-05-03 12:49:42 +00:00
|
|
|
osalDbgAssert(dacp->grpp->num_channels == 1, "invalid number of channels");
|
|
|
|
|
2015-05-02 09:11:13 +00:00
|
|
|
dmaStreamSetPeripheral(dacp->params->dma, &dacp->params->dac->DHR12L1 +
|
|
|
|
dacp->params->dataoffset);
|
|
|
|
dmamode = dacp->params->dmamode |
|
|
|
|
STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD;
|
|
|
|
break;
|
|
|
|
case DAC_DHRM_8BIT_RIGHT:
|
2015-05-03 12:49:42 +00:00
|
|
|
osalDbgAssert(dacp->grpp->num_channels == 1, "invalid number of channels");
|
|
|
|
|
2015-05-02 09:11:13 +00:00
|
|
|
dmaStreamSetPeripheral(dacp->params->dma, &dacp->params->dac->DHR8R1 +
|
|
|
|
dacp->params->dataoffset);
|
|
|
|
dmamode = dacp->params->dmamode |
|
|
|
|
STM32_DMA_CR_PSIZE_BYTE | STM32_DMA_CR_MSIZE_BYTE;
|
2015-05-03 12:49:42 +00:00
|
|
|
|
|
|
|
/* In this mode the size of the buffer is halved because two samples
|
|
|
|
packed in a single dacsample_t element.*/
|
|
|
|
n = (n + 1) / 2;
|
|
|
|
break;
|
|
|
|
#if STM32_DAC_DUAL_MODE == TRUE
|
|
|
|
case DAC_DHRM_12BIT_RIGHT_DUAL:
|
|
|
|
osalDbgAssert(dacp->grpp->num_channels == 2, "invalid number of channels");
|
|
|
|
|
|
|
|
dmaStreamSetPeripheral(dacp->params->dma, &dacp->params->dac->DHR12RD);
|
|
|
|
dmamode = dacp->params->dmamode |
|
|
|
|
STM32_DMA_CR_PSIZE_WORD | STM32_DMA_CR_MSIZE_WORD;
|
|
|
|
break;
|
|
|
|
case DAC_DHRM_12BIT_LEFT_DUAL:
|
|
|
|
osalDbgAssert(dacp->grpp->num_channels == 2, "invalid number of channels");
|
|
|
|
|
|
|
|
dmaStreamSetPeripheral(dacp->params->dma, &dacp->params->dac->DHR12LD);
|
|
|
|
dmamode = dacp->params->dmamode |
|
|
|
|
STM32_DMA_CR_PSIZE_WORD | STM32_DMA_CR_MSIZE_WORD;
|
2015-05-02 09:11:13 +00:00
|
|
|
break;
|
2015-05-03 12:49:42 +00:00
|
|
|
case DAC_DHRM_8BIT_RIGHT_DUAL:
|
|
|
|
osalDbgAssert(dacp->grpp->num_channels == 1, "invalid number of channels");
|
|
|
|
|
|
|
|
dmaStreamSetPeripheral(dacp->params->dma, &dacp->params->dac->DHR8RD);
|
|
|
|
dmamode = dacp->params->dmamode |
|
|
|
|
STM32_DMA_CR_PSIZE_HWORD | STM32_DMA_CR_MSIZE_HWORD;
|
|
|
|
break;
|
|
|
|
#endif
|
2015-05-02 09:11:13 +00:00
|
|
|
default:
|
|
|
|
chDbgAssert(false, "unexpected DAC mode");
|
|
|
|
break;
|
|
|
|
}
|
2015-05-01 16:03:03 +00:00
|
|
|
|
2015-05-02 09:11:13 +00:00
|
|
|
dmaStreamSetMemory0(dacp->params->dma, dacp->samples);
|
2015-05-03 12:49:42 +00:00
|
|
|
dmaStreamSetTransactionSize(dacp->params->dma, n);
|
2015-05-02 09:11:13 +00:00
|
|
|
dmaStreamSetMode(dacp->params->dma, dmamode |
|
2015-05-01 16:03:03 +00:00
|
|
|
STM32_DMA_CR_DMEIE | STM32_DMA_CR_TEIE |
|
|
|
|
STM32_DMA_CR_HTIE | STM32_DMA_CR_TCIE);
|
2015-05-02 09:11:13 +00:00
|
|
|
dmaStreamEnable(dacp->params->dma);
|
|
|
|
|
|
|
|
/* DAC configuration.*/
|
|
|
|
#if STM32_DAC_DUAL_MODE == FALSE
|
2015-05-02 12:28:09 +00:00
|
|
|
cr = DAC_CR_DMAEN1 | (dacp->grpp->trigger << 3) | DAC_CR_TEN1 | DAC_CR_EN1;
|
|
|
|
dacp->params->dac->CR &= dacp->params->regmask;
|
|
|
|
dacp->params->dac->CR |= cr << dacp->params->regshift;
|
2015-05-02 09:11:13 +00:00
|
|
|
#else
|
2015-05-03 12:49:42 +00:00
|
|
|
dacp->params->dac->CR = 0;
|
|
|
|
cr = DAC_CR_DMAEN1 | (dacp->grpp->trigger << 3) | DAC_CR_TEN1 | DAC_CR_EN1
|
|
|
|
| (dacp->grpp->trigger << 19) | DAC_CR_TEN2 | DAC_CR_EN2;
|
|
|
|
dacp->params->dac->CR = cr;
|
2015-05-02 09:11:13 +00:00
|
|
|
#endif
|
2015-05-01 16:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stops an ongoing conversion.
|
|
|
|
* @details This function stops the currently ongoing conversion and returns
|
|
|
|
* the driver in the @p DAC_READY state. If there was no conversion
|
|
|
|
* being processed then the function does nothing.
|
|
|
|
*
|
|
|
|
* @param[in] dacp pointer to the @p DACDriver object
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
|
|
|
void dac_lld_stop_conversion(DACDriver *dacp) {
|
|
|
|
|
2015-05-03 08:51:07 +00:00
|
|
|
/* DMA channel disabled and released.*/
|
2015-05-02 09:11:13 +00:00
|
|
|
dmaStreamDisable(dacp->params->dma);
|
2015-05-03 08:51:07 +00:00
|
|
|
dmaStreamRelease(dacp->params->dma);
|
|
|
|
|
2015-05-02 09:11:13 +00:00
|
|
|
#if STM32_DAC_DUAL_MODE == FALSE
|
2015-05-02 20:34:22 +00:00
|
|
|
dacp->params->dac->CR &= dacp->params->regmask;
|
2015-05-02 12:28:09 +00:00
|
|
|
dacp->params->dac->CR |= DAC_CR_EN1 << dacp->params->regshift;
|
2015-05-02 09:11:13 +00:00
|
|
|
#else
|
2015-05-03 12:49:42 +00:00
|
|
|
if ((dacp->config->datamode == DAC_DHRM_12BIT_RIGHT_DUAL) ||
|
|
|
|
(dacp->config->datamode == DAC_DHRM_12BIT_LEFT_DUAL) ||
|
|
|
|
(dacp->config->datamode == DAC_DHRM_8BIT_RIGHT_DUAL)) {
|
|
|
|
dacp->params->dac->CR = DAC_CR_EN2 | DAC_CR_EN1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dacp->params->dac->CR = DAC_CR_EN1;
|
|
|
|
}
|
2015-05-02 09:11:13 +00:00
|
|
|
#endif
|
2014-07-26 09:24:53 +00:00
|
|
|
}
|
2015-05-01 16:03:03 +00:00
|
|
|
|
2014-07-26 09:24:53 +00:00
|
|
|
#endif /* HAL_USE_DAC */
|
|
|
|
|
|
|
|
/** @} */
|