2 #include <twr_scheduler.h>
3 #include <twr_system.h>
7 #define _TWR_DAC_VOLTAGE_TO_CODE_CONSTANT 19961
45 if (_twr_dac.channel[channel].is_initialized)
50 RCC->APB1ENR |= RCC_APB1ENR_DACEN;
57 _twr_dac.channel[
TWR_DAC_DAC0] = _twr_dac_channel_0_setup_default;
60 DAC->CR |= DAC_CR_EN1;
64 _twr_dac.channel[
TWR_DAC_DAC1] = _twr_dac_channel_1_setup_default;
67 DAC->CR |= DAC_CR_EN2;
70 _twr_dac.channel[channel].is_initialized =
true;
75 if (!_twr_dac.channel[channel].is_initialized)
80 if (_twr_dac.channel[channel].is_in_progress)
88 DAC->CR &= ~DAC_CR_EN1_Msk;
93 DAC->CR &= ~DAC_CR_EN2_Msk;
96 _twr_dac.channel[channel].is_initialized =
false;
100 RCC->APB1ENR &= ~RCC_APB1ENR_DACEN;
106 if (_twr_dac.channel[channel].is_in_progress)
111 uint16_t *dac_value = _twr_dac.channel[channel].dac_register.u16;
117 *_twr_dac.channel[channel].dac_register.u8 = *(uint8_t *) raw;
122 *dac_value = *(uint16_t *) raw;
127 *dac_value = *(
float *) raw * _TWR_DAC_VOLTAGE_TO_CODE_CONSTANT;
139 _twr_dac.channel[channel].event_handler = event_handler;
140 _twr_dac.channel[channel].event_param = event_param;
145 if (_twr_dac.channel[channel].is_in_progress)
182 dac_dma_config->
length = config->length;
186 _twr_dac.channel[channel].sample_rate = config->sample_rate;
195 if (dac_channel_setup->is_in_progress)
200 twr_system_pll_enable();
204 DAC->CR &= ~(DAC_CR_TSEL1_Msk);
207 DAC->CR |= DAC_CR_DMAEN1 | DAC_CR_TEN1;
210 RCC->APB1ENR |= RCC_APB1ENR_TIM6EN;
214 DAC->CR &= ~(DAC_CR_TSEL2_Msk);
217 DAC->CR |= DAC_CR_DMAEN2 | DAC_CR_TEN2 | DAC_CR_TSEL2_0 | DAC_CR_TSEL2_2 | DAC_CR_WAVE1_0;
220 RCC->APB1ENR |= RCC_APB1ENR_TIM7EN;
226 TIM_TypeDef *tim = _twr_dac.channel[channel].tim;
242 tim->EGR = TIM_EGR_UG;
245 tim->CR2 = TIM_CR2_MMS_1;
257 tim->CR1 |= TIM_CR1_CEN;
259 dac_channel_setup->is_in_progress =
true;
268 if (!dac_channel_setup->is_in_progress)
274 dac_channel_setup->tim->CR1 &= ~TIM_CR1_CEN;
280 DAC->CR &= ~(DAC_CR_DMAEN1_Msk | DAC_CR_TEN1_Msk | DAC_CR_TSEL1_Msk);
283 RCC->APB1ENR &= ~RCC_APB1ENR_TIM6EN;
287 DAC->CR &= ~(DAC_CR_DMAEN2_Msk | DAC_CR_TEN2_Msk | DAC_CR_TSEL2_Msk);
290 RCC->APB1ENR &= ~RCC_APB1ENR_TIM7EN;
293 dac_channel_setup->is_in_progress =
false;
295 twr_system_pll_disable();
307 if (dac_channel_setup->event_handler != NULL)
314 if (dac_channel_setup->event_handler != NULL)
316 dac_channel_setup->event_handler(dac_channel,
TWR_DAC_EVENT_DONE, dac_channel_setup->event_param);
332 .is_initialized =
false,
333 .is_in_progress =
false,
334 .event_handler = NULL,
339 .u8 = (
void *)&DAC->DHR8R1,
340 .u16 = (
void *)&DAC->DHR12L1
358 .is_initialized =
false,
359 .is_in_progress =
false,
360 .event_handler = NULL,
365 .u8 = (
void *)&DAC->DHR8R2,
366 .u16 = (
void *)&DAC->DHR12L2
twr_dac_format_t
Raw value format.
void twr_dac_deinit(twr_dac_channel_t channel)
Deitialize DAC channel.
twr_dac_channel_t
DAC channel.
void twr_dac_async_stop(twr_dac_channel_t channel)
Stop asynchronous DAC channel operation.
void twr_dac_init(twr_dac_channel_t channel)
Initialize DAC channel.
twr_dac_event_t
DAC channel event.
bool twr_dac_async_config(twr_dac_channel_t channel, twr_dac_config_t *config)
Configure image of DAC DMA channel.
void twr_dac_set_output(twr_dac_channel_t channel, const void *raw, twr_dac_format_t format)
Set DAC channel output as raw value.
bool twr_dac_async_run(twr_dac_channel_t channel)
Start asynchronous DAC channel operation.
void twr_dac_set_event_handler(twr_dac_channel_t channel, void(*event_handler)(twr_dac_channel_t, twr_dac_event_t, void *), void *event_param)
Set callback function.
twr_dac_sample_rate_t
Sample rate.
@ TWR_DAC_FORMAT_16_BIT
Raw value format is 16-bit.
@ TWR_DAC_FORMAT_VOLTAGE
Raw value format is float.
@ TWR_DAC_FORMAT_8_BIT
Raw value format is 8-bit.
@ TWR_DAC_DAC0
DAC channel DAC0.
@ TWR_DAC_DAC1
DAC channel DAC1.
@ TWR_DAC_EVENT_DONE
Event is done.
@ TWR_DAC_EVENT_HALF_DONE
Event is done.
@ TWR_DAC_DATA_SIZE_8
Data size is 8b.
@ TWR_DAC_DATA_SIZE_16
Data size is 16b.
@ TWR_DAC_MODE_SINGLE
Mode single.
@ TWR_DAC_MODE_CIRCULAR
Mode circular (repeate playing buffer)
@ TWR_DAC_SAMPLE_RATE_16K
Data sample-rate is 16kHz.
@ TWR_DAC_SAMPLE_RATE_8K
Data sample-rate is 8kHz.
void twr_dma_channel_config(twr_dma_channel_t channel, twr_dma_channel_config_t *config)
Configure DMA channel.
void twr_dma_channel_run(twr_dma_channel_t channel)
Start DMA channel.
void twr_dma_set_event_handler(twr_dma_channel_t channel, void(*event_handler)(twr_dma_channel_t, twr_dma_event_t, void *), void *event_param)
Set callback function.
twr_dma_event_t
DMA channel event.
void twr_dma_init(void)
Initialize DMA.
twr_dma_channel_t
DMA channels.
void twr_dma_channel_stop(twr_dma_channel_t channel)
Stop DMA channel.
@ TWR_DMA_REQUEST_9
DMA request 9.
@ TWR_DMA_REQUEST_15
DMA request 15.
@ TWR_DMA_EVENT_DONE
DMA channel event done.
@ TWR_DMA_EVENT_HALF_DONE
DMA channel event half done.
@ TWR_DMA_EVENT_ERROR
DMA channel event error.
@ TWR_DMA_SIZE_2
DMA channel data size 2B.
@ TWR_DMA_SIZE_1
DMA channel data size 1B.
@ TWR_DMA_DIRECTION_TO_PERIPHERAL
DMA channel direction from RAM to peripheral.
@ TWR_DMA_PRIORITY_LOW
DMA channel priority is low.
@ TWR_DMA_CHANNEL_2
DMA channel 2.
@ TWR_DMA_CHANNEL_4
DMA channel 4.
@ TWR_DMA_MODE_CIRCULAR
DMA channel mode circular.
@ TWR_DMA_MODE_STANDARD
DMA channel mode standard.
DMA channel configuration.
twr_dma_mode_t mode
DMA channel mode.
void * address_peripheral
Peripheral address.
twr_dma_size_t data_size_memory
DMA channel memory data size.
size_t length
DMA channel data length.
void * address_memory
RAM memory address.