Skip to content

Commit

Permalink
[ADC] DMA
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmxmx committed Aug 30, 2018
1 parent d1c7ff8 commit fc1d3c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
18 changes: 11 additions & 7 deletions software/C2M_X/C2M_ADC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
*
*/
Expand All @@ -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
Expand All @@ -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<ADC_CHANNEL_1_1>((adcbuffer_0[1] + adcbuffer_0[11])/2);
update<ADC_CHANNEL_2_1>((adcbuffer_0[2] + adcbuffer_0[12])/2);
update<ADC_CHANNEL_3_1>((adcbuffer_0[3] + adcbuffer_0[13])/2);
update<ADC_CHANNEL_4_1>((adcbuffer_0[4] + adcbuffer_0[14])/2);
update<ADC_CHANNEL_5_1>((adcbuffer_0[5] + adcbuffer_0[15])/2);
update<ADC_CHANNEL_5_1>((adcbuffer_0[5] + adcbuffer_0[15])/2);
/* 6-10 are the velocity inputs: */
update<ADC_CHANNEL_1_2>(adcbuffer_0[6]);
update<ADC_CHANNEL_2_2>(adcbuffer_0[7]);
update<ADC_CHANNEL_3_2>(adcbuffer_0[8]);
Expand Down
7 changes: 3 additions & 4 deletions software/C2M_X/C2M_ADC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 };
};

};
Expand Down

0 comments on commit fc1d3c9

Please sign in to comment.