Skip to content

Fixes and updates. #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 69 additions & 36 deletions examples/Beginner/Waveform_Generator/Waveform_Generator.ino
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
// This example generates different waveforms based on user input on A12/DAC1.

#include <Arduino_AdvancedAnalog.h>
#include <mbed_stats.h>

#define N_SAMPLES (256)
#define DEFAULT_FREQUENCY (16000)
#define N_SAMPLES (64)
#define DEFAULT_FREQUENCY (32000)

AdvancedDAC dac1(A12);
uint8_t SAMPLES_BUFFER[N_SAMPLES];
size_t dac_frequency = DEFAULT_FREQUENCY;

void generate_waveform(int cmd)
{
uint32_t get_current_heap() {
mbed_stats_heap_t heap_stats;
mbed_stats_heap_get(&heap_stats);
return heap_stats.current_size;
}

void print_menu() {
Serial.println();
Serial.println("Enter a command:");
Serial.println("t: Triangle wave");
Serial.println("q: Square wave");
Serial.println("s: Sine wave");
Serial.println("r: Sawtooth wave");
Serial.println("k: stop DAC");
Serial.println("+: Increase frequency");
Serial.println("-: Decrease frequency");
}

void generate_waveform(int cmd) {
static bool dac_started = false;
static size_t dac_frequency = DEFAULT_FREQUENCY;
static size_t starting_heap = get_current_heap();

switch (cmd) {
case 't':
// Triangle wave
Serial.print("Waveform: Triangle ");
for (int i=0; i<N_SAMPLES; i++){
SAMPLES_BUFFER[i] = abs((i % 255) - 127);
for (int i=0, j=N_SAMPLES-1; i<N_SAMPLES; i++, j--){
SAMPLES_BUFFER[i] = abs((i - j) * 256 / N_SAMPLES);
}
break;

case 'q':
// Square wave
Serial.print("Waveform: Square ");
for (int i=0; i<N_SAMPLES; i++){
SAMPLES_BUFFER[i] = (i % 255) < 127 ? 127 : 0;
SAMPLES_BUFFER[i] = i < (N_SAMPLES / 2) ? 0 : 255;
}
break;

Expand All @@ -38,36 +59,59 @@ void generate_waveform(int cmd)

case 'r':
// Sawtooth
Serial.print("Waveform: Sawtooth");
Serial.print("Waveform: Sawtooth ");
for (int i=0; i<N_SAMPLES; i++){
SAMPLES_BUFFER[i] = i;
SAMPLES_BUFFER[i] = i * (256 / N_SAMPLES);
}
break;

case '+':
case '-':
Serial.print("Current frequency: ");
if (cmd == '+' && dac_frequency < 64000) {
dac_frequency *= 2;

if (cmd == '+' && dac_frequency < 128000) {
dac_frequency *= 2;
} else if (cmd == '-' && dac_frequency > 1000) {
dac_frequency /= 2;
dac_frequency /= 2;
} else {
break;
break;
}

// Change frequency.
dac1.frequency(dac_frequency * N_SAMPLES);
break;


case 'k':
dac1.stop();
dac_started = false;
break;

default:
Serial.print("Unknown command ");
Serial.println((char) cmd);
return;
}

Serial.print(dac_frequency/1000);
Serial.println("KHz");

if (cmd == 'k') {
Serial.println("DAC stopped!");
print_menu();
} else {
Serial.print(dac_frequency/1000);
Serial.println("KHz");

if (dac_started == false) {
// Initialize and start the DAC.
if (!dac1.begin(AN_RESOLUTION_8, dac_frequency * N_SAMPLES, N_SAMPLES, 32)) {
Serial.println("Failed to start DAC1 !");
while (1);
}
dac_started = true;
}
}

Serial.print("Used memory: ");
Serial.print(get_current_heap() - starting_heap);
Serial.println(" bytes");
}

void setup() {
Expand All @@ -77,32 +121,21 @@ void setup() {

}


Serial.println("Enter a command:");
Serial.println("t: Triangle wave");
Serial.println("q: Square wave");
Serial.println("s: Sine wave");
Serial.println("r: Sawtooth wave");
Serial.println("+: Increase frequency");
Serial.println("-: Decrease frequency");

// Print list of commands.
print_menu();

// Start generating a sine wave.
generate_waveform('s');

// DAC initialization
if (!dac1.begin(AN_RESOLUTION_8, DEFAULT_FREQUENCY * N_SAMPLES, N_SAMPLES, 32)) {
Serial.println("Failed to start DAC1 !");
while (1);
}
}

void loop() {
if (Serial.available() > 0) {
int cmd = Serial.read();
if (cmd != '\n') {
generate_waveform(cmd);
generate_waveform(cmd);
}
}

if (dac1.available()) {
// Get a free buffer for writing.
SampleBuffer buf = dac1.dequeue();
Expand Down
44 changes: 32 additions & 12 deletions src/AdvancedDAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct dac_descr_t {
TIM_HandleTypeDef tim;
uint32_t tim_trig;
uint32_t resolution;
uint32_t dmaudr_flag;
DMABufferPool<Sample> *pool;
DMABuffer<Sample> *dmabuf[2];
};
Expand All @@ -37,10 +38,10 @@ struct dac_descr_t {
static DAC_HandleTypeDef dac = {0};

static dac_descr_t dac_descr_all[] = {
{&dac, DAC_CHANNEL_1, {DMA1_Stream4, {DMA_REQUEST_DAC1_CH1}}, DMA1_Stream4_IRQn,
{TIM4}, DAC_TRIGGER_T4_TRGO, DAC_ALIGN_12B_R, nullptr, {nullptr, nullptr}},
{&dac, DAC_CHANNEL_2, {DMA1_Stream5, {DMA_REQUEST_DAC1_CH2}}, DMA1_Stream5_IRQn,
{TIM5}, DAC_TRIGGER_T5_TRGO, DAC_ALIGN_12B_R, nullptr, {nullptr, nullptr}},
{&dac, DAC_CHANNEL_1, {DMA1_Stream4, {DMA_REQUEST_DAC1_CH1}}, DMA1_Stream4_IRQn, {TIM4},
DAC_TRIGGER_T4_TRGO, DAC_ALIGN_12B_R, DAC_FLAG_DMAUDR1, nullptr, {nullptr, nullptr}},
{&dac, DAC_CHANNEL_2, {DMA1_Stream5, {DMA_REQUEST_DAC1_CH2}}, DMA1_Stream5_IRQn, {TIM5},
DAC_TRIGGER_T5_TRGO, DAC_ALIGN_12B_R, DAC_FLAG_DMAUDR2, nullptr, {nullptr, nullptr}},
};

static uint32_t DAC_RES_LUT[] = {
Expand Down Expand Up @@ -73,10 +74,12 @@ static dac_descr_t *dac_descr_get(uint32_t channel) {
}

static void dac_descr_deinit(dac_descr_t *descr, bool dealloc_pool) {
if (descr) {
if (descr != nullptr) {
HAL_TIM_Base_Stop(&descr->tim);
HAL_DAC_Stop_DMA(descr->dac, descr->channel);

__HAL_DAC_CLEAR_FLAG(descr->dac, descr->dmaudr_flag);

for (size_t i=0; i<AN_ARRAY_SIZE(descr->dmabuf); i++) {
if (descr->dmabuf[i]) {
descr->dmabuf[i]->release();
Expand All @@ -89,13 +92,17 @@ static void dac_descr_deinit(dac_descr_t *descr, bool dealloc_pool) {
delete descr->pool;
}
descr->pool = nullptr;
} else {
descr->pool->flush();
}

}
}

bool AdvancedDAC::available() {
if (descr != nullptr) {
if (__HAL_DAC_GET_FLAG(descr->dac, descr->dmaudr_flag)) {
dac_descr_deinit(descr, false);
}
return descr->pool->writable();
}
return false;
Expand All @@ -113,11 +120,17 @@ DMABuffer<Sample> &AdvancedDAC::dequeue() {
}

void AdvancedDAC::write(DMABuffer<Sample> &dmabuf) {
static uint32_t buf_count = 0;

if (descr == nullptr) {
return;
}

// Make sure any cached data is flushed.
dmabuf.flush();
descr->pool->enqueue(&dmabuf);

if (descr->dmabuf[0] == nullptr && descr->pool->readable() > 2) {
if (descr->dmabuf[0] == nullptr && (++buf_count % 3) == 0) {
descr->dmabuf[0] = descr->pool->dequeue();
descr->dmabuf[1] = descr->pool->dequeue();

Expand All @@ -126,7 +139,9 @@ void AdvancedDAC::write(DMABuffer<Sample> &dmabuf) {
(uint32_t *) descr->dmabuf[0]->data(), descr->dmabuf[0]->size(), descr->resolution);

// Re/enable DMA double buffer mode.
HAL_NVIC_DisableIRQ(descr->dma_irqn);
hal_dma_enable_dbm(&descr->dma, descr->dmabuf[0]->data(), descr->dmabuf[1]->data());
HAL_NVIC_EnableIRQ(descr->dma_irqn);

// Start trigger timer.
HAL_TIM_Base_Start(&descr->tim);
Expand All @@ -135,7 +150,7 @@ void AdvancedDAC::write(DMABuffer<Sample> &dmabuf) {

int AdvancedDAC::begin(uint32_t resolution, uint32_t frequency, size_t n_samples, size_t n_buffers) {
// Sanity checks.
if (resolution >= AN_ARRAY_SIZE(DAC_RES_LUT) || (descr && descr->pool)) {
if (resolution >= AN_ARRAY_SIZE(DAC_RES_LUT) || descr != nullptr) {
return 0;
}

Expand All @@ -147,13 +162,14 @@ int AdvancedDAC::begin(uint32_t resolution, uint32_t frequency, size_t n_samples

uint32_t function = pinmap_function(dac_pins[0], PinMap_DAC);
descr = dac_descr_get(DAC_CHAN_LUT[STM_PIN_CHANNEL(function) - 1]);
if (descr == nullptr || descr->pool) {
if (descr == nullptr) {
return 0;
}

// Allocate DMA buffer pool.
descr->pool = new DMABufferPool<Sample>(n_samples, n_channels, n_buffers);
if (descr->pool == nullptr) {
descr = nullptr;
return 0;
}
descr->resolution = DAC_RES_LUT[resolution];
Expand All @@ -178,13 +194,16 @@ int AdvancedDAC::begin(uint32_t resolution, uint32_t frequency, size_t n_samples

int AdvancedDAC::stop()
{
dac_descr_deinit(descr, true);
if (descr != nullptr) {
dac_descr_deinit(descr, true);
descr = nullptr;
}
return 1;
}

int AdvancedDAC::frequency(uint32_t const frequency)
{
if (descr && descr->pool) {
if (descr != nullptr) {
// Reconfigure the trigger timer.
dac_descr_deinit(descr, false);
hal_tim_config(&descr->tim, frequency);
Expand All @@ -200,9 +219,10 @@ extern "C" {

void DAC_DMAConvCplt(DMA_HandleTypeDef *dma, uint32_t channel) {
dac_descr_t *descr = dac_descr_get(channel);

// Release the DMA buffer that was just done, allocate a new one,
// and update the next DMA memory address target.
if (descr->pool->readable()) {
if (descr && descr->pool->readable()) {
// NOTE: CT bit is inverted, to get the DMA buffer that's Not currently in use.
size_t ct = ! hal_dma_get_ct(dma);
descr->dmabuf[ct]->release();
Expand Down
Loading