Skip to content

Commit 758553a

Browse files
committed
Add analogRead, touchRead, dacWrite and updated esp-idf
1 parent 48abb79 commit 758553a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4415
-3271
lines changed

cores/esp32/esp32-hal-adc.c

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "esp32-hal-adc.h"
16+
#include "freertos/FreeRTOS.h"
17+
#include "freertos/task.h"
18+
#include "rom/ets_sys.h"
19+
#include "esp_attr.h"
20+
#include "esp_intr.h"
21+
#include "soc/rtc_io_reg.h"
22+
#include "soc/rtc_cntl_reg.h"
23+
#include "soc/sens_reg.h"
24+
25+
static uint8_t __analogAttenuation = 0;//0db
26+
static uint8_t __analogWidth = 3;//12 bits
27+
static uint8_t __analogCycles = 8;
28+
static uint8_t __analogSamples = 0;//1 sample
29+
static uint8_t __analogClockDiv = 1;
30+
31+
void __analogSetWidth(uint8_t bits){
32+
if(bits < 9){
33+
bits = 9;
34+
} else if(bits > 12){
35+
bits = 12;
36+
}
37+
__analogWidth = bits - 9;
38+
SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_SAR1_BIT_WIDTH, __analogWidth, SENS_SAR1_BIT_WIDTH_S);
39+
SET_PERI_REG_BITS(SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_BIT, __analogWidth, SENS_SAR1_SAMPLE_BIT_S);
40+
41+
SET_PERI_REG_BITS(SENS_SAR_START_FORCE_REG, SENS_SAR2_BIT_WIDTH, __analogWidth, SENS_SAR2_BIT_WIDTH_S);
42+
SET_PERI_REG_BITS(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_BIT, __analogWidth, SENS_SAR2_SAMPLE_BIT_S);
43+
}
44+
45+
void __analogSetCycles(uint8_t cycles){
46+
__analogCycles = cycles;
47+
SET_PERI_REG_BITS(SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_CYCLE, __analogCycles, SENS_SAR1_SAMPLE_CYCLE_S);
48+
SET_PERI_REG_BITS(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_CYCLE, __analogCycles, SENS_SAR2_SAMPLE_CYCLE_S);
49+
}
50+
51+
void __analogSetSamples(uint8_t samples){
52+
if(!samples){
53+
return;
54+
}
55+
__analogSamples = samples - 1;
56+
SET_PERI_REG_BITS(SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_NUM, __analogSamples, SENS_SAR1_SAMPLE_NUM_S);
57+
SET_PERI_REG_BITS(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_NUM, __analogSamples, SENS_SAR2_SAMPLE_NUM_S);
58+
}
59+
60+
void __analogSetClockDiv(uint8_t clockDiv){
61+
if(!clockDiv){
62+
return;
63+
}
64+
__analogClockDiv = clockDiv;
65+
SET_PERI_REG_BITS(SENS_SAR_READ_CTRL_REG, SENS_SAR1_CLK_DIV, __analogClockDiv, SENS_SAR1_CLK_DIV_S);
66+
SET_PERI_REG_BITS(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_CLK_DIV, __analogClockDiv, SENS_SAR2_CLK_DIV_S);
67+
}
68+
69+
void __analogSetAttenuation(uint8_t attenuation){
70+
__analogAttenuation = attenuation & 3;
71+
uint32_t att_data = 0;
72+
int i = 8;
73+
while(i--){
74+
att_data |= __analogAttenuation << (i * 2);
75+
}
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);
78+
}
79+
80+
void IRAM_ATTR __analogInit(){
81+
static bool initialized = false;
82+
if(initialized){
83+
return;
84+
}
85+
__analogSetAttenuation(__analogAttenuation);
86+
__analogSetCycles(__analogCycles);
87+
__analogSetSamples(__analogSamples + 1);//in samples
88+
__analogSetClockDiv(__analogClockDiv);
89+
__analogSetWidth(__analogWidth + 9);//in bits
90+
91+
SET_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR1_DATA_INV);
92+
SET_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV);
93+
94+
SET_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_FORCE_M); //SAR ADC1 controller (in RTC) is started by SW
95+
SET_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD_FORCE_M); //SAR ADC1 pad enable bitmap is controlled by SW
96+
SET_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_FORCE_M); //SAR ADC2 controller (in RTC) is started by SW
97+
SET_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD_FORCE_M); //SAR ADC2 pad enable bitmap is controlled by SW
98+
99+
CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_M); //force XPD_SAR=0, use XPD_FSM
100+
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_AMP, 0x2, SENS_FORCE_XPD_AMP_S); //force XPD_AMP=0
101+
102+
CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_CTRL_REG, 0xfff << SENS_AMP_RST_FB_FSM_S); //clear FSM
103+
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT1_REG, SENS_SAR_AMP_WAIT1, 0x1, SENS_SAR_AMP_WAIT1_S);
104+
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT1_REG, SENS_SAR_AMP_WAIT2, 0x1, SENS_SAR_AMP_WAIT2_S);
105+
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_SAR_AMP_WAIT3, 0x1, SENS_SAR_AMP_WAIT3_S);
106+
while (GET_PERI_REG_BITS2(SENS_SAR_SLAVE_ADDR1_REG, 0x7, SENS_MEAS_STATUS_S) != 0); //wait det_fsm==
107+
108+
initialized = true;
109+
}
110+
111+
uint16_t IRAM_ATTR __analogRead(uint8_t pin)
112+
{
113+
int8_t channel = digitalPinToAnalogChannel(pin);
114+
if(channel < 0){
115+
return 0;//not adc pin
116+
}
117+
int8_t pad = digitalPinToTouchChannel(pin);
118+
119+
if(pad >= 0){
120+
uint32_t touch = READ_PERI_REG(SENS_SAR_TOUCH_ENABLE_REG);
121+
if(touch & (1 << pad)){
122+
touch &= ~((1 << (pad + SENS_TOUCH_PAD_OUTEN2_S))
123+
| (1 << (pad + SENS_TOUCH_PAD_OUTEN1_S))
124+
| (1 << (pad + SENS_TOUCH_PAD_WORKEN_S)));
125+
WRITE_PERI_REG(SENS_SAR_TOUCH_ENABLE_REG, touch);
126+
}
127+
} else if(pin == 25){
128+
CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_XPD_DAC | RTC_IO_PDAC1_DAC_XPD_FORCE);//stop dac1
129+
} else if(pin == 26){
130+
CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_XPD_DAC | RTC_IO_PDAC2_DAC_XPD_FORCE);//stop dac2
131+
}
132+
133+
pinMode(pin, ANALOG);
134+
135+
__analogInit();
136+
137+
if(channel > 7){
138+
channel -= 10;
139+
140+
SET_PERI_REG_BITS(SENS_SAR_MEAS_START2_REG, SENS_SAR2_EN_PAD, (1 << channel), SENS_SAR2_EN_PAD_S);
141+
CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M);
142+
SET_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M);
143+
while (GET_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DONE_SAR) == 0) {}; //read done
144+
return GET_PERI_REG_BITS2(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DATA_SAR, SENS_MEAS2_DATA_SAR_S);
145+
}
146+
147+
SET_PERI_REG_BITS(SENS_SAR_MEAS_START1_REG, SENS_SAR1_EN_PAD, (1 << channel), SENS_SAR1_EN_PAD_S);
148+
CLEAR_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M);
149+
SET_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M);
150+
while (GET_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DONE_SAR) == 0) {}; //read done
151+
return GET_PERI_REG_BITS2(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DATA_SAR, SENS_MEAS1_DATA_SAR_S);
152+
}
153+
int __hallRead() //hall sensor without LNA
154+
{
155+
int Sens_Vp0;
156+
int Sens_Vn0;
157+
int Sens_Vp1;
158+
int Sens_Vn1;
159+
160+
pinMode(36, ANALOG);
161+
pinMode(39, ANALOG);
162+
SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_XPD_HALL_FORCE_M); // hall sens force enable
163+
SET_PERI_REG_MASK(RTC_IO_HALL_SENS_REG, RTC_IO_XPD_HALL); // xpd hall
164+
SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_HALL_PHASE_FORCE_M); // phase force
165+
CLEAR_PERI_REG_MASK(RTC_IO_HALL_SENS_REG, RTC_IO_HALL_PHASE); // hall phase
166+
Sens_Vp0 = __analogRead(36);
167+
Sens_Vn0 = __analogRead(39);
168+
SET_PERI_REG_MASK(RTC_IO_HALL_SENS_REG, RTC_IO_HALL_PHASE);
169+
Sens_Vp1 = __analogRead(36);
170+
Sens_Vn1 = __analogRead(39);
171+
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);
172+
CLEAR_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_XPD_HALL_FORCE);
173+
CLEAR_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_HALL_PHASE_FORCE);
174+
return (Sens_Vp1 - Sens_Vp0) - (Sens_Vn1 - Sens_Vn0);
175+
}
176+
177+
extern uint16_t analogRead(uint8_t pin) __attribute__ ((weak, alias("__analogRead")));
178+
extern void analogSetWidth(uint8_t bits) __attribute__ ((weak, alias("__analogSetWidth")));
179+
extern void analogSetCycles(uint8_t cycles) __attribute__ ((weak, alias("__analogSetCycles")));
180+
extern void analogSetSamples(uint8_t samples) __attribute__ ((weak, alias("__analogSetSamples")));
181+
extern void analogSetClockDiv(uint8_t clockDiv) __attribute__ ((weak, alias("__analogSetClockDiv")));
182+
//extern void analogSetAttenuation(uint8_t attenuation) __attribute__ ((weak, alias("__analogSetAttenuation")));
183+
extern int hallRead() __attribute__ ((weak, alias("__hallRead")));

cores/esp32/esp32-hal-adc.h

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Arduino.h - Main include file for the Arduino SDK
3+
Copyright (c) 2005-2013 Arduino Team. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef MAIN_ESP32_HAL_ADC_H_
21+
#define MAIN_ESP32_HAL_ADC_H_
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
#include "esp32-hal.h"
28+
29+
/*
30+
* Get ADC value for pin
31+
* */
32+
uint16_t analogRead(uint8_t pin);
33+
34+
/*
35+
* Sets the sample bits (9 - 12)
36+
* */
37+
void analogSetWidth(uint8_t bits);
38+
39+
/*
40+
* Set number of cycles per sample
41+
* Default is 8 and seems to do well
42+
* Range is 1 - 255
43+
* */
44+
void analogSetCycles(uint8_t cycles);
45+
46+
/*
47+
* Set number of samples in the range.
48+
* Default is 1
49+
* Range is 1 - 255
50+
* This setting splits the range into
51+
* "samples" pieces, which could look
52+
* like the sensitivity has been multiplied
53+
* that many times
54+
* */
55+
void analogSetSamples(uint8_t samples);
56+
57+
/*
58+
* Set the divider for the ADC clock.
59+
* Default is 1
60+
* Range is 1 - 255
61+
* */
62+
void analogSetClockDiv(uint8_t clockDiv);
63+
64+
/*
65+
* Get value for HALL sensor (without LNA)
66+
* connected to pins 36(SVP) and 39(SVN)
67+
* */
68+
int hallRead();
69+
70+
#ifdef __cplusplus
71+
}
72+
#endif
73+
74+
#endif /* MAIN_ESP32_HAL_ADC_H_ */

cores/esp32/esp32-hal-dac.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "esp32-hal-dac.h"
16+
#include "freertos/FreeRTOS.h"
17+
#include "freertos/task.h"
18+
#include "rom/ets_sys.h"
19+
#include "esp_attr.h"
20+
#include "esp_intr.h"
21+
#include "soc/rtc_io_reg.h"
22+
#include "soc/rtc_cntl_reg.h"
23+
#include "soc/sens_reg.h"
24+
25+
void IRAM_ATTR __dacWrite(uint8_t pin, uint8_t value)
26+
{
27+
if(pin < 25 || pin > 26){
28+
return;//not dac pin
29+
}
30+
pinMode(pin, ANALOG);
31+
uint8_t channel = pin - 25;
32+
33+
34+
//Disable Tone
35+
CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_SW_TONE_EN);
36+
37+
if (channel) {
38+
//Disable Channel Tone
39+
CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN2_M);
40+
//Set the Dac value
41+
SET_PERI_REG_BITS(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_DAC, value, RTC_IO_PDAC2_DAC_S); //dac_output
42+
//Channel output enable
43+
SET_PERI_REG_MASK(RTC_IO_PAD_DAC2_REG, RTC_IO_PDAC2_XPD_DAC | RTC_IO_PDAC2_DAC_XPD_FORCE);
44+
} else {
45+
//Disable Channel Tone
46+
CLEAR_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN1_M);
47+
//Set the Dac value
48+
SET_PERI_REG_BITS(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_DAC, value, RTC_IO_PDAC1_DAC_S); //dac_output
49+
//Channel output enable
50+
SET_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_XPD_DAC | RTC_IO_PDAC1_DAC_XPD_FORCE);
51+
}
52+
}
53+
54+
extern void dacWrite(uint8_t pin, uint8_t value) __attribute__ ((weak, alias("__dacWrite")));

cores/esp32/esp32-hal-dac.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Arduino.h - Main include file for the Arduino SDK
3+
Copyright (c) 2005-2013 Arduino Team. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef MAIN_ESP32_HAL_DAC_H_
21+
#define MAIN_ESP32_HAL_DAC_H_
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
#include "esp32-hal.h"
28+
29+
void dacWrite(uint8_t pin, uint8_t value);
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif
34+
35+
#endif /* MAIN_ESP32_HAL_DAC_H_ */

0 commit comments

Comments
 (0)