2222#include "soc/rtc_cntl_reg.h"
2323#include "soc/sens_reg.h"
2424
25- static uint8_t __analogAttenuation = 0 ;//0db
25+ static uint8_t __analogAttenuation = 3 ;//11db
2626static uint8_t __analogWidth = 3 ;//12 bits
2727static uint8_t __analogCycles = 8 ;
2828static uint8_t __analogSamples = 0 ;//1 sample
@@ -66,22 +66,24 @@ void __analogSetClockDiv(uint8_t clockDiv){
6666 SET_PERI_REG_BITS (SENS_SAR_READ_CTRL2_REG , SENS_SAR2_CLK_DIV , __analogClockDiv , SENS_SAR2_CLK_DIV_S );
6767}
6868
69- void __analogSetAttenuation (uint8_t attenuation ){
69+ void __analogSetAttenuation (adc_attenuation_t attenuation )
70+ {
7071 __analogAttenuation = attenuation & 3 ;
7172 uint32_t att_data = 0 ;
72- int i = 8 ;
73+ int i = 10 ;
7374 while (i -- ){
7475 att_data |= __analogAttenuation << (i * 2 );
7576 }
76- SET_PERI_REG_BITS ( SENS_SAR_MEAS_START1_REG , SENS_MEAS1_DATA_SAR , att_data , SENS_MEAS1_DATA_SAR_S );
77- SET_PERI_REG_BITS ( SENS_SAR_MEAS_START2_REG , SENS_MEAS2_DATA_SAR , att_data , SENS_MEAS2_DATA_SAR_S );
77+ WRITE_PERI_REG ( SENS_SAR_ATTEN1_REG , att_data & 0xFFFF ); //ADC1 has 8 channels
78+ WRITE_PERI_REG ( SENS_SAR_ATTEN2_REG , att_data );
7879}
7980
8081void IRAM_ATTR __analogInit (){
8182 static bool initialized = false;
8283 if (initialized ){
8384 return ;
8485 }
86+
8587 __analogSetAttenuation (__analogAttenuation );
8688 __analogSetCycles (__analogCycles );
8789 __analogSetSamples (__analogSamples + 1 );//in samples
@@ -108,6 +110,20 @@ void IRAM_ATTR __analogInit(){
108110 initialized = true;
109111}
110112
113+ void __analogSetPinAttenuation (uint8_t pin , adc_attenuation_t attenuation )
114+ {
115+ int8_t channel = digitalPinToAnalogChannel (pin );
116+ if (channel < 0 || attenuation > 3 ){
117+ return ;
118+ }
119+ __analogInit ();
120+ if (channel > 7 ){
121+ SET_PERI_REG_BITS (SENS_SAR_ATTEN2_REG , 3 , attenuation , ((channel - 10 ) * 2 ));
122+ } else {
123+ SET_PERI_REG_BITS (SENS_SAR_ATTEN1_REG , 3 , attenuation , (channel * 2 ));
124+ }
125+ }
126+
111127uint16_t IRAM_ATTR __analogRead (uint8_t pin )
112128{
113129 int8_t channel = digitalPinToAnalogChannel (pin );
@@ -150,6 +166,7 @@ uint16_t IRAM_ATTR __analogRead(uint8_t pin)
150166 while (GET_PERI_REG_MASK (SENS_SAR_MEAS_START1_REG , SENS_MEAS1_DONE_SAR ) == 0 ) {}; //read done
151167 return GET_PERI_REG_BITS2 (SENS_SAR_MEAS_START1_REG , SENS_MEAS1_DATA_SAR , SENS_MEAS1_DATA_SAR_S );
152168}
169+
153170int __hallRead () //hall sensor without LNA
154171{
155172 int Sens_Vp0 ;
@@ -179,5 +196,6 @@ extern void analogSetWidth(uint8_t bits) __attribute__ ((weak, alias("__analogSe
179196extern void analogSetCycles (uint8_t cycles ) __attribute__ ((weak , alias ("__analogSetCycles" )));
180197extern void analogSetSamples (uint8_t samples ) __attribute__ ((weak , alias ("__analogSetSamples" )));
181198extern void analogSetClockDiv (uint8_t clockDiv ) __attribute__ ((weak , alias ("__analogSetClockDiv" )));
182- //extern void analogSetAttenuation(uint8_t attenuation) __attribute__ ((weak, alias("__analogSetAttenuation")));
199+ extern void analogSetAttenuation (adc_attenuation_t attenuation ) __attribute__ ((weak , alias ("__analogSetAttenuation" )));
200+ extern void analogSetPinAttenuation (uint8_t pin , adc_attenuation_t attenuation ) __attribute__ ((weak , alias ("__analogSetPinAttenuation" )));
183201extern int hallRead () __attribute__ ((weak , alias ("__hallRead" )));
0 commit comments