Skip to content

Commit 34c88ae

Browse files
committed
pbio/drv/adc/adc_ev3: Extend delays for more accurate analog readings
1 parent 23cde1b commit 34c88ae

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/pbio/drv/adc/adc_ev3.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ enum {
5252
//
5353
// NOTE: CSHOLD is *not* set here, so that CS is deasserted between each 16-bit unit
5454
#define MANUAL_ADC_CHANNEL(x) \
55+
(1 << 26) | \
5556
(SPI_SPIDAT1_DFSEL_FORMAT1 << SPI_SPIDAT1_DFSEL_SHIFT) | \
5657
(0 << (SPI_SPIDAT1_CSNR_SHIFT + PBDRV_EV3_SPI0_ADC_CS)) | \
5758
(1 << (SPI_SPIDAT1_CSNR_SHIFT + PBDRV_EV3_SPI0_FLASH_CS)) | \
@@ -186,6 +187,12 @@ void pbdrv_adc_ev3_configure_data_format() {
186187
SPIConfigClkFormat(SOC_SPI_0_REGS, SPI_CLK_POL_LOW | SPI_CLK_OUTOFPHASE, SPI_DATA_FORMAT1);
187188
SPIShiftMsbFirst(SOC_SPI_0_REGS, SPI_DATA_FORMAT1);
188189
SPICharLengthSet(SOC_SPI_0_REGS, 16, SPI_DATA_FORMAT1);
190+
// In order to compensate for analog impedance issues and capacitor charging time,
191+
// we set all SPI delays to the maximum for the ADC. This helps get more accurate readings.
192+
// This includes both this delay (the delay where CS is held inactive),
193+
// as well as the CS-assert-to-clock-start and clock-end-to-CS-deassert delays
194+
// (which are global and set in block_device_ev3.c).
195+
SPIWdelaySet(SOC_SPI_0_REGS, 0x3f << SPI_SPIFMT_WDELAY_SHIFT, SPI_DATA_FORMAT1);
189196
}
190197

191198
void pbdrv_adc_ev3_shut_down_hack() {

lib/pbio/drv/block_device/block_device_ev3.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ static void edma3_set_param(unsigned int slot, EDMA3CCPaRAMEntry_ *p) {
190190

191191
// Helper functions for setting up the high control bits of a data transfer
192192
static inline void spi0_setup_for_flash() {
193+
HWREG(SOC_SPI_0_REGS + SPI_SPIDELAY) = 0;
194+
193195
*(volatile uint16_t *)(SOC_SPI_0_REGS + SPI_SPIDAT1 + 2) =
194196
(1 << (SPI_SPIDAT1_CSHOLD_SHIFT - 16)) |
195197
(SPI_SPIDAT1_DFSEL_FORMAT0 << (SPI_SPIDAT1_DFSEL_SHIFT - 16)) |
@@ -423,6 +425,9 @@ pbio_error_t pbdrv_block_device_ev3_spi_begin_for_adc(const uint32_t *cmds, vola
423425
return PBIO_ERROR_IO;
424426
}
425427

428+
// Max out delays for ADC, see comment in adc_ev3.c
429+
HWREG(SOC_SPI_0_REGS + SPI_SPIDELAY) = (0xff << SPI_SPIDELAY_C2TDELAY_SHIFT) | (0xff << SPI_SPIDELAY_T2CDELAY_SHIFT);
430+
426431
ps.p.srcAddr = (unsigned int)(cmds);
427432
ps.p.destAddr = SOC_SPI_0_REGS + SPI_SPIDAT1;
428433
ps.p.aCnt = sizeof(uint32_t);

0 commit comments

Comments
 (0)