8000 Merge branch 'master' into me-no-dev · maxosprojects/arduino-esp32@4d2c31f · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d2c31f

Browse files
committed
Merge branch 'master' into me-no-dev
2 parents 7d843cb + e92634a commit 4d2c31f

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Arduino core for ESP32 WiFi chip
22

3+
## Need help or have a question? Join the chat at [![https://gitter.im/espressif/arduino-esp32](https://badges.gitter.im/espressif/arduino-esp32.svg)](https://gitter.im/espressif/arduino-esp32?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4+
35
- [Development Status](#development-status)
46
- Installing options:
57
+ [Using Arduino IDE](#using-arduino-ide)
@@ -83,44 +85,43 @@ Linux 32/64, Linux ARM (like Raspberry Pi, BeagleBone, CubieBoard).
8385
- Download and install [esp-idf](https://github.com/espressif/esp-idf)
8486
- Create blank idf project (from one of the examples)
8587
- in the project folder, create a folder called components and clone this repository inside
86-
88+
8789
```bash
8890
mkdir -p components && \
8991
cd components && \
9092
git clone https://github.com/espressif/arduino-esp32.git arduino && \
91-
cd.. && \
93+
cd .. && \
9294
make menuconfig
9395
```
9496
- ```make menuconfig``` has some Arduino options
9597
- "Autostart Arduino setup and loop on boot"
9698
- If you enable this options, your main.cpp should be formated like any other sketch
97-
99+
98100
```arduino
99101
//file: main.cpp
100102
#include "Arduino.h"
101-
103+
102104
void setup(){
103105
Serial.begin(115200);
104106
}
105-
107+
106108
void loop(){
107109
Serial.println("loop");
108110
delay(1000);
109111
}
110112
```
111113
- Else you need to implement ```app_main()``` and call ```initArduino();``` in it.
112-
114+
113115
Keep in mind that setup() and loop() will not be called in this case
114-
116+
115117
```arduino
116118
//file: main.cpp
117119
#include "Arduino.h"
118-
extern "C" void initArduino();
119-
120+
120121
extern "C" void app_main()
121122
{
122123
initArduino();
123-
pinMode(4, OUPUT);
124+
pinMode(4, OUTPUT);
124125
digitalWrite(4, HIGH);
125126
//do your own thing
126127
}

cores/esp32/esp32-hal-adc.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
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
2626
static uint8_t __analogWidth = 3;//12 bits
2727
static uint8_t __analogCycles = 8;
2828
static 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

8081
void 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+
111127
uint16_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+
153170
int __hallRead() //hall sensor without LNA
154171
{
155172
int Sens_Vp0;
@@ -179,5 +196,6 @@ extern void analogSetWidth(uint8_t bits) __attribute__ ((weak, alias("__analogSe
179196
extern void analogSetCycles(uint8_t cycles) __attribute__ ((weak, alias("__analogSetCycles")));
180197
extern void analogSetSamples(uint8_t samples) __attribute__ ((weak, alias("__analogSetSamples")));
181198
extern 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")));
183201
extern int hallRead() __attribute__ ((weak, alias("__hallRead")));

cores/esp32/esp32-hal-adc.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ extern "C" {
2626

2727
#include "esp32-hal.h"
2828

29+
typedef enum {
30+
ADC_0db,
31+
ADC_2_5db,
32+
ADC_6db,
33+
ADC_11db
34+
} adc_attenuation_t;
35+
2936
/*
3037
* Get ADC value for pin
3138
* */
3239
uint16_t analogRead(uint8_t pin);
3340

3441
/*
35-
* Sets the sample bits (9 - 12)
42+
* Sets the sample bits
43+
* Default is 12bit (0 - 4095)
44+
* Range is 9 - 12
3645
* */
3746
void analogSetWidth(uint8_t bits);
3847

@@ -61,6 +70,18 @@ void analogSetSamples(uint8_t samples);
6170
* */
6271
void analogSetClockDiv(uint8_t clockDiv);
6372

73+
/*
74+
* Set the attenuation for all channels
75+
* Default is 11db
76+
* */
77+
void analogSetAttenuation(adc_attenuation_t attenuation);
78+
79+
/*
80+
* Set the attenuation for particular pin
81+
* Default is 11db
82+
* */
83+
void analogSetPinAttenuation(uint8_t pin, adc_attenuation_t attenuation);
84+
6485
/*
6586
* Get value for HALL sensor (without LNA)
6687
* connected to pins 36(SVP) and 39(SVN)

0 commit comments

Comments
 (0)
0