Unverified Commit 4ef39130 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

adc.startSampling: Use DMA to populate buffer

parent fc390815
Loading
Loading
Loading
Loading
Loading
+7 −15
Original line number Diff line number Diff line
@@ -13,14 +13,10 @@ class ADC {
	private:
		ADC(ADC const &copy);

		uint16_t bufsize;
		uint16_t bufpos;
		uint16_t *buf16;
		uint8_t *buf8;
		volatile bool bufferFull;

	public:
		ADC() : bufsize(0), bufpos(0), buf16(NULL), buf8(NULL), bufferFull(false) {}
		ADC() : bufferFull(false) {}

		float getTemp();
		float getVCC();
@@ -28,7 +24,12 @@ class ADC {
		void startSampling(uint16_t *buf, uint16_t bufsize);
		void stopSampling();

		inline bool doneSampling()
		inline void setBufferFull()
		{
			bufferFull = true;
		}

		inline bool isBufferFull()
		{
			if (bufferFull) {
				bufferFull = false;
@@ -36,15 +37,6 @@ class ADC {
			}
			return false;
		}

		inline void storeReading(uint16_t value)
		{
			buf16[bufpos++] = value;
			bufpos %= bufsize;
			if (bufpos == 0) {
				bufferFull = true;
			}
		}
};

extern ADC adc;
+14 −14
Original line number Diff line number Diff line
@@ -152,16 +152,16 @@ void ADC::startSampling(uint16_t *buf, uint16_t bufsize)
	P3SEL0 |= BIT0;
	P3SEL1 |= BIT0;

	// raise an interrupt after each sample
	ADC12IER0 = ADC12IE1;
	ADC12IFGR0 = ADC12IFG1;

	// clear pending interrupts
	ADC12IV = 0;

	buf16 = buf;
	this->bufsize = bufsize;

	// Use DMA Channel 0 to automatically copy readings to buf
	DMACTL0 = DMA0TSEL__DMA0TRIG26;
	DMACTL4 = DMARMWDIS;
	DMA0CTL = DMADSTINCR_3;
	DMA0SA = ADC12_B_BASE + OFS_ADC12MEM1;
	DMA0DA = (uintptr_t)buf;
	DMA0SZ = bufsize;
	DMA0CTL |= DMAEN | DMAIE;

	// start sampling
	ADC12CTL0 |= ADC12ENC | ADC12SC;
}

@@ -174,11 +174,11 @@ void ADC::stopSampling()
ADC adc;

#ifndef __acweaving
__attribute__((interrupt(ADC12_VECTOR))) void handle_adc_irq()
__attribute__((interrupt(DMA_VECTOR))) void handle_dma_irq()
{
	if (ADC12IV == 0x00e) {
		// ADC12MEM1
		adc.storeReading(ADC12MEM1);
	if (DMAIV == 0x02) {
		adc.setBufferFull();
		DMA0CTL |= DMAEN;
	}
}
#endif