Skip to content

Commit

Permalink
fix crash on prototype 5
Browse files Browse the repository at this point in the history
  • Loading branch information
corrados committed May 20, 2023
1 parent 3840829 commit 508c87b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions edrumulus_hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,55 @@ void Edrumulus_hardware::capture_samples ( const int number_pads,
// As a workaround, we had to write our own analogRead function.
void Edrumulus_hardware::init_my_analogRead()
{
#ifdef CONFIG_IDF_TARGET_ESP32
// if the GIOP 25/26 are used, we have to set the DAC to 0 to get correct DC offset
// estimates and reduce the number of large spikes
dac_i2s_enable();
dac_output_enable ( DAC_CHANNEL_1 );
dac_output_voltage ( DAC_CHANNEL_1, 0 );
dac_output_disable ( DAC_CHANNEL_1 );
dac_output_enable ( DAC_CHANNEL_2 );
dac_output_voltage ( DAC_CHANNEL_2, 0 );
dac_output_disable ( DAC_CHANNEL_2 );
dac_i2s_disable();

// set attenuation of 11 dB
WRITE_PERI_REG ( SENS_SAR_ATTEN1_REG, 0x0FFFFFFFF );
WRITE_PERI_REG ( SENS_SAR_ATTEN2_REG, 0x0FFFFFFFF );

// set both ADCs to 12 bit resolution using 8 cycles and 1 sample
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_CYCLE, 8, SENS_SAR1_SAMPLE_CYCLE_S ); // cycles
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_CYCLE, 8, SENS_SAR2_SAMPLE_CYCLE_S );
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_NUM, 0, SENS_SAR1_SAMPLE_NUM_S ); // # samples
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_NUM, 0, SENS_SAR2_SAMPLE_NUM_S );
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_CLK_DIV, 1, SENS_SAR1_CLK_DIV_S ); // clock div
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_CLK_DIV, 1, SENS_SAR2_CLK_DIV_S );
SET_PERI_REG_BITS ( SENS_SAR_START_FORCE_REG, SENS_SAR1_BIT_WIDTH, 3, SENS_SAR1_BIT_WIDTH_S ); // width
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_BIT, 3, SENS_SAR1_SAMPLE_BIT_S );
SET_PERI_REG_BITS ( SENS_SAR_START_FORCE_REG, SENS_SAR2_BIT_WIDTH, 3, SENS_SAR2_BIT_WIDTH_S );
SET_PERI_REG_BITS ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_BIT, 3, SENS_SAR2_SAMPLE_BIT_S );

// some other initializations
SET_PERI_REG_MASK ( SENS_SAR_READ_CTRL_REG, SENS_SAR1_DATA_INV );
SET_PERI_REG_MASK ( SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV );
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_FORCE_M ); // SAR ADC1 controller (in RTC) is started by SW
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD_FORCE_M ); // SAR ADC1 pad enable bitmap is controlled by SW
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_FORCE_M ); // SAR ADC2 controller (in RTC) is started by SW
SET_PERI_REG_MASK ( SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD_FORCE_M ); // SAR ADC2 pad enable bitmap is controlled by SW
CLEAR_PERI_REG_MASK ( SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_M ); // force XPD_SAR=0, use XPD_FSM
SET_PERI_REG_BITS ( SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_AMP, 0x2, SENS_FORCE_XPD_AMP_S ); // force XPD_AMP=0
CLEAR_PERI_REG_MASK ( SENS_SAR_MEAS_CTRL_REG, 0xfff << SENS_AMP_RST_FB_FSM_S ); // clear FSM
SET_PERI_REG_BITS ( SENS_SAR_MEAS_WAIT1_REG, SENS_SAR_AMP_WAIT1, 0x1, SENS_SAR_AMP_WAIT1_S );
SET_PERI_REG_BITS ( SENS_SAR_MEAS_WAIT1_REG, SENS_SAR_AMP_WAIT2, 0x1, SENS_SAR_AMP_WAIT2_S );
SET_PERI_REG_BITS ( SENS_SAR_MEAS_WAIT2_REG, SENS_SAR_AMP_WAIT3, 0x1, SENS_SAR_AMP_WAIT3_S );
while ( GET_PERI_REG_BITS2 ( SENS_SAR_SLAVE_ADDR1_REG, 0x7, SENS_MEAS_STATUS_S ) != 0 );

// configure all pins to analog read
for ( int i = 0; i < total_number_inputs; i++ )
{
pinMode ( input_pin[i], ANALOG );
}
#else // CONFIG_IDF_TARGET_ESP32S3
int cur_sample;
for ( int channel = 0; channel < 10; channel++ ) // 10 channels per ADC
{
Expand All @@ -428,6 +477,7 @@ void Edrumulus_hardware::init_my_analogRead()
adc2_get_raw ( static_cast<adc2_channel_t> ( channel ), ADC_WIDTH_BIT_12, &cur_sample );
}
adc_power_on();
#endif
}


Expand Down

0 comments on commit 508c87b

Please sign in to comment.