Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
hdw-dac.h File Reference

Detailed Description

Design Philosophy

The Digital To Analog Converter (DAC) is used to drive a speaker or headphones. It uses the DAC peripheral. The peripheral can be used a few different ways, but this project uses DMA to output a continuous, arbitrary signal.

This component is initialized by initDac() with a fnDacCallback_t callback which will request DAC samples from the application when required. Sample requests come from the DAC peripheral in an interrupt and are queued to be serviced out of the interrupt. This queue is checked in dacPoll(). Spending time to generate samples in an interrupt isn't a good idea.

Warning
Note that the DAC peripheral and the ADC peripheral (hdw-mic.h) use the same DMA controller, so they cannot both be used at the same time. One must be deinitialize before initializing the other.

Usage

You don't need to call initDac() or deinitDac(). The system does at the appropriate times.

The system will also automatically call dacStart(), though the Swadge mode can later call dacStop() or dacStart() when the DAC needs to be used. Stopping the DAC when not in use can save some processing cycles, but stopping it abruptly may cause unwanted clicks or pops on the speaker.

dacPoll() is called automatically by the system while the DAC is running. By default, samples are requested from sngPlayerFillBuffer(). Swadge modes may override this by providing a non-NULL function pointer for swadgeMode_t.fnDacCb.

Example

void dacCallback(uint8_t* samples, int16_t len)
{
// Generate something sawtooth-ish. It's just an example, gimme a break
for(int16_t idx = 0; idx < len; idx++)
{
samples[idx] = idx;
}
}
int main()
{
// Initialize and start the DAC
initDac(dacCallback);
// Loop forever and poll the DAC.
bool running = true;
while(running)
{
// This will end up calling dacCallback as appropriate
}
// Cleanup
}
void dacStop(void)
Stop the DAC.
Definition hdw-dac.c:140
void dacPoll(void)
Poll the queue to see if any buffers need to be filled with audio samples.
Definition hdw-dac.c:150
void initDac(dac_channel_mask_t channel, gpio_num_t shdn_gpio, fnDacCallback_t cb)
Initialize the DAC.
Definition hdw-dac.c:71
void dacStart(void)
Start the DAC. This will cause samples to be requested from the application.
Definition hdw-dac.c:130

Go to the source code of this file.

Macros

#define DAC_SAMPLE_RATE_HZ   32768
 The sample rate for the DAC.
 
#define DAC_BUF_SIZE   512
 

Typedefs

typedef void(* fnDacCallback_t) (uint8_t *samples, int16_t len)
 A callback which requests DAC samples from the application.
 

Functions

void initDac (dac_channel_mask_t channel, gpio_num_t shdn_gpio, fnDacCallback_t cb)
 Initialize the DAC.
 
void deinitDac (void)
 Deinitialize the DAC and free memory.
 
void dacPoll (void)
 Poll the queue to see if any buffers need to be filled with audio samples.
 
void dacStart (void)
 Start the DAC. This will cause samples to be requested from the application.
 
void dacStop (void)
 Stop the DAC.
 
void setDacShutdown (bool shutdown)
 Set the shutdown state of the DAC.
 

Macro Definition Documentation

◆ DAC_SAMPLE_RATE_HZ

#define DAC_SAMPLE_RATE_HZ   32768

The sample rate for the DAC.

◆ DAC_BUF_SIZE

#define DAC_BUF_SIZE   512

The size of each buffer to fill with DAC samples

Typedef Documentation

◆ fnDacCallback_t

typedef void(* fnDacCallback_t) (uint8_t *samples, int16_t len)

A callback which requests DAC samples from the application.

Parameters
samplesA buffer to fill with 8 bit unsigned DAC samples
lenThe length of the buffer to fill

Function Documentation

◆ initDac()

void initDac ( dac_channel_mask_t channel,
gpio_num_t shdn_gpio,
fnDacCallback_t cb )

Initialize the DAC.

Parameters
channelThe output channel (pin) for the ADC
shdn_gpioThe GPIO that controls the amplifier's shutdown
cbA callback function which will be called to request samples from the application

◆ deinitDac()

void deinitDac ( void )

Deinitialize the DAC and free memory.

◆ dacPoll()

void dacPoll ( void )

Poll the queue to see if any buffers need to be filled with audio samples.

◆ dacStart()

void dacStart ( void )

Start the DAC. This will cause samples to be requested from the application.

◆ dacStop()

void dacStop ( void )

Stop the DAC.

◆ setDacShutdown()

void setDacShutdown ( bool shutdown)

Set the shutdown state of the DAC.

Parameters
shutdowntrue to shut down the DAC, false to enable it