#include <stdint.h>
#define SENS_SAR_READ_CTRL_REG 0x3FF48800
#define SENS_SAR_ATTEN1_REG 0x3FF48834
#define SENS_SAR_MEAS_START1_REG 0x3FF48854
#define SENS_ULP_CP_SLEEP_CYC0_REG 0x3FF48818
#define SENS_SAR_START_FORCE_REG 0x3FF4882C
#define WRITE_REG(addr, val) (*(volatile uint32_t *)(addr) = (val))
#define READ_REG(addr) (*(volatile uint32_t *)(addr))
void delay1(volatile uint32_t i) {
for(uint32_t x=0; x<i*100000; x++);
}
void configure_adc1() {
// Step 2: Set attenuation for ADC1 channel 0
uint32_t atten_reg = READ_REG(SENS_SAR_ATTEN1_REG);
atten_reg &= ~(0x3<<0);// Clear attenuation for ADC1 channel 0
atten_reg |= (0x2<<0);// Set attenuation to 6 dB
WRITE_REG(SENS_SAR_ATTEN1_REG, atten_reg);
}
void start_adc1_conversion() {
// Step 3: Start conversion
uint32_t start_force = READ_REG(SENS_SAR_START_FORCE_REG);
start_force &= ~(1<<23)|(0x3<<0);
start_force |= (0x3<<0);
uint32_t start_reg = READ_REG(SENS_SAR_MEAS_START1_REG);
start_reg &= ~((1<<18)|(1 << 17)|(1<<31)|(0x7FF<<19));
start_reg |= (1<<31)|(1 << 17)|(1<<18)|(0x1<<19);
WRITE_REG(SENS_SAR_MEAS_START1_REG, start_reg);
// Step 4: Wait for conversion to complete
while (!(READ_REG(SENS_SAR_MEAS_START1_REG) & (1 << 16)));
}
uint16_t read_adc1_data() {
// Read conversion result
uint32_t data_reg = READ_REG(SENS_SAR_MEAS_START1_REG);
return (uint16_t)(data_reg & 0xFFF);
}
void setup() {
Serial.begin(115200);
configure_adc1();
}
void loop() {
start_adc1_conversion();
uint16_t adc_value = read_adc1_data();
Serial.println(adc_value);
delay1(10);
}