Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
dac_dma.c
Go to the documentation of this file.
1#include "dac.h"
2
3#ifdef DAC_USE_DMA
4
5#include <GPDMA_LPC17xx.h>
6#include <LPC17xx.h>
7#include <stdbool.h>
8
9_PRIVATE u8 dma_channel;
10_PRIVATE bool dma_ok;
11
12enum
13{
16 // DAC_CFG_DOUBLEBUF = 0x2,
17
21 DAC_CFG_USE_CNT = 0x4,
22
26 DAC_CFG_USE_DMA = 0x8,
27} DAC_DMAConfig;
28
29void DAC_InitDMA(u32 sample_rate_hz)
30{
31 LPC_DAC->DACCTRL |= (DAC_CFG_USE_DMA | DAC_CFG_USE_CNT);
32 LPC_DAC->DACCNTVAL = sample_rate_hz;
33
34 GPDMA_Initialize();
35 dma_channel = 1;
36 dma_ok = false;
37}
38
39void DAC_DeinitDMA(void)
40{
41 LPC_DAC->DACR = 0;
42 LPC_DAC->DACCTRL &= ~(DAC_CFG_USE_DMA | DAC_CFG_USE_CNT);
43 LPC_DAC->DACCNTVAL = 0;
44 GPDMA_Uninitialize();
45}
46
47// PLAY FUNCTION & PRIVATE STUFF
48
49_PRIVATE void dma_transfer_complete_callback(u32 event)
50{
51 dma_ok = (event & GPDMA_EVENT_TERMINAL_COUNT_REQUEST) != 0;
52 DAC_StopDMA();
53}
54
55void DAC_PlayDMA(const u16 *const pcm_samples, u32 sample_count)
56{
57 if (!pcm_samples || sample_count == 0)
58 return;
59
60 const u32 status = GPDMA_ChannelConfigure(dma_channel, (u32)pcm_samples, (u32)&LPC_DAC->DACR, sample_count,
61 GPDMA_CH_CONTROL_SI | GPDMA_CH_CONTROL_DI | GPDMA_WIDTH_HALFWORD,
62 GPDMA_CONFIG_E, dma_transfer_complete_callback);
63
64 if (status != 0)
65 return;
66
67 if (GPDMA_ChannelEnable(dma_channel) != 0)
68 return;
69}
70
71void DAC_StopDMA(void)
72{
73 GPDMA_ChannelDisable(dma_channel);
74}
75
76#endif
uint8_t u8
Definition types.h:8
#define _PRIVATE
Definition types.h:37
uint16_t u16
Definition types.h:7
uint32_t u32
Definition types.h:6