Skip to content
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

feat(ADC): support DMA by setting DREQs based on ADC FIFO fill level #90

Merged
merged 1 commit into from
Jan 8, 2022
Merged
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
18 changes: 17 additions & 1 deletion src/peripherals/adc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IRQ } from '../irq';
import { RP2040 } from '../rp2040';
import { FIFO } from '../utils/fifo';
import { DREQChannel } from './dma';
import { BasePeripheral, Peripheral } from './peripheral';

const CS = 0x00; // ADC Control and Status
Expand Down Expand Up @@ -96,6 +97,7 @@ export class RPADC extends BasePeripheral implements Peripheral {
};

readonly fifo = new FIFO(4);
readonly dreq = DREQChannel.DREQ_ADC;

// Registers
cs = 0;
Expand Down Expand Up @@ -156,6 +158,17 @@ export class RPADC extends BasePeripheral implements Peripheral {
this.onADCRead(this.activeChannel);
}

private updateDMA() {
if (this.fcs & FCS_DREQ_EN) {
const thres = (this.fcs >> FCS_THRESH_SHIFT) & FCS_THRES_MASK;
if (this.fifo.itemCount >= thres) {
this.rp2040.dma.setDREQ(this.dreq);
} else {
this.rp2040.dma.clearDREQ(this.dreq);
}
}
}

completeADCRead(value: number, error: boolean) {
this.busy = false;
this.result = value;
Expand All @@ -178,6 +191,7 @@ export class RPADC extends BasePeripheral implements Peripheral {
value |= FIFO_ERR;
}
this.fifo.push(value);
this.updateDMA();
this.checkInterrupts();
}
}
Expand Down Expand Up @@ -228,7 +242,9 @@ export class RPADC extends BasePeripheral implements Peripheral {
this.fcs |= FCS_UNDER;
return 0;
} else {
return this.fifo.pull();
const value = this.fifo.pull();
this.updateDMA();
return value;
}
case DIV:
return this.clockDiv;
Expand Down