From fc1d3c9b0c2bea78e15ec873c9aca4710722b755 Mon Sep 17 00:00:00 2001 From: mxmxmx Date: Thu, 30 Aug 2018 07:15:22 +0200 Subject: [PATCH] [ADC] DMA --- software/C2M_X/C2M_ADC.cpp | 18 +++++++++++------- software/C2M_X/C2M_ADC.h | 7 +++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/software/C2M_X/C2M_ADC.cpp b/software/C2M_X/C2M_ADC.cpp index 87b32e0..83a1bd4 100644 --- a/software/C2M_X/C2M_ADC.cpp +++ b/software/C2M_X/C2M_ADC.cpp @@ -11,9 +11,9 @@ namespace C2M { /*static*/ uint32_t ADC::smoothed_[ADC_CHANNEL_LAST]; /*static*/ volatile bool ADC::ready_; -constexpr uint16_t ADC::SCA_CHANNEL_NUM[DMA_NUM_CH]; -DMAChannel* dma0 = new DMAChannel(false); -DMAChannel* dma1 = new DMAChannel(false); +constexpr uint16_t ADC::SCA_CHANNEL_ID[DMA_NUM_CH]; // ADCx_SCA register channel numbers +DMAChannel* dma0 = new DMAChannel(false); // dma0 channel, fills adcbuffer_0 +DMAChannel* dma1 = new DMAChannel(false); // dma1 channel, updates ADC0_SC1A which holds the channel/pin IDs DMAMEM static volatile uint16_t __attribute__((aligned(DMA_BUF_SIZE+0))) adcbuffer_0[DMA_BUF_SIZE]; /*static*/ void ADC::Init(CalibrationData *calibration_data) { @@ -42,7 +42,7 @@ void ADC::DMA_ISR() { /* * * DMA/ADC à la https://forum.pjrc.com/threads/30171-Reconfigure-ADC-via-a-DMA-transfer-to-allow-multiple-Channel-Acquisition - * basically, this sets up two DMA channels and cycles through the 10 (effectively: 16) adc channels once (until the buffer is full), resets, and so on; dma1 advances SCA_CHANNEL_NUM + * basically, this sets up two DMA channels and cycles through the 10 (effectively: 16) adc channels once (until the buffer is full), resets, and so on; dma1 advances SCA_CHANNEL_ID * somewhat like https://www.nxp.com/docs/en/application-note/AN4590.pdf but w/o the PDB. * */ @@ -66,7 +66,7 @@ void ADC::Init_DMA() { dma0->attachInterrupt(DMA_ISR); dma1->begin(true); // allocate the DMA channel - dma1->TCD->SADDR = &ADC::SCA_CHANNEL_NUM[0]; + dma1->TCD->SADDR = &ADC::SCA_CHANNEL_ID[0]; dma1->TCD->SOFF = 2; // source increment each transfer (n bytes) dma1->TCD->ATTR = 0x101; dma1->TCD->SLAST = - DMA_NUM_CH*2; // num ADC0 samples * 2 @@ -88,12 +88,16 @@ void ADC::Init_DMA() { if (ADC::ready_) { ADC::ready_ = false; - /* starts at adcbuffer_0[1] because of weird offset; because there's 16 samples in the buffer, 11-15 are the pitch inputs again: */ + /* + * starts collecting results at adcbuffer_0[1] because of weird offset (--> discard adcbuffer_0[0]). + * there's 16 samples in the buffer, 1-5 / 11-15 are the pitch inputs: + */ update((adcbuffer_0[1] + adcbuffer_0[11])/2); update((adcbuffer_0[2] + adcbuffer_0[12])/2); update((adcbuffer_0[3] + adcbuffer_0[13])/2); update((adcbuffer_0[4] + adcbuffer_0[14])/2); - update((adcbuffer_0[5] + adcbuffer_0[15])/2); + update((adcbuffer_0[5] + adcbuffer_0[15])/2); + /* 6-10 are the velocity inputs: */ update(adcbuffer_0[6]); update(adcbuffer_0[7]); update(adcbuffer_0[8]); diff --git a/software/C2M_X/C2M_ADC.h b/software/C2M_X/C2M_ADC.h index ccefb0c..0610e65 100644 --- a/software/C2M_X/C2M_ADC.h +++ b/software/C2M_X/C2M_ADC.h @@ -23,7 +23,7 @@ enum ADC_CHANNEL { }; #define DMA_BUF_SIZE 16 -#define DMA_NUM_CH 16 // 16 because we need some power of 2 (see SCA_CHANNEL_NUM[] below) +#define DMA_NUM_CH 16 // 16 because we need some power of 2 (see SCA_CHANNEL_ID[] below) namespace C2M { @@ -100,13 +100,12 @@ class ADC { static uint32_t smoothed_[ADC_CHANNEL_LAST]; /* - * below: channel ids for the ADCx_SCA register: we have 10 inputs, which isn't ideal ... so we pad the array - * (and start at 0x4C because of weird offset.) + * below: channel ids for the ADCx_SCA register: we have 10 inputs, which isn't ideal ... so we pad the array * pitch inputs 1-5: A5 = 0x4C; A4 = 0x4D; A3 = 0x49; A2 = 0x48; A0 = 0x45 * velocity inputs 1-5: A9 = 0x44; A8 = 0x4F; A7 = 0x47; A6 = 0x46; A1 = 0x4E */ - static constexpr uint16_t SCA_CHANNEL_NUM[] = { 0x4C, 0x4D, 0x49, 0x48, 0x45, 0x44, 0x4F, 0x47, 0x46, 0x4E, 0x4C, 0x4D, 0x49, 0x48, 0x45, 0x44 }; + static constexpr uint16_t SCA_CHANNEL_ID[DMA_NUM_CH] = { 0x4C, 0x4D, 0x49, 0x48, 0x45, 0x44, 0x4F, 0x47, 0x46, 0x4E, 0x4C, 0x4D, 0x49, 0x48, 0x45, 0x44 }; }; };