-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp_helper.ino
196 lines (168 loc) · 4.75 KB
/
esp_helper.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "header/esp_helper.h"
#include "rom/ets_sys.h"
#include "soc/rtc_cntl_reg.h"
#include "soc/sens_reg.h"
#include "esp_sntp.h"
#include "esp32-hal.h"
#include "lwip/apps/sntp.h"
#include "esp_netif.h"
#include "header/adc_lut.h"
#include <esp_adc_cal.h>
float read_esp32_temperature()
{
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S);
SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 10, SENS_TSENS_CLK_DIV_S);
CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE);
SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
ets_delay_us(100);
SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
ets_delay_us(5);
float temp_f = (float)GET_PERI_REG_BITS2(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT, SENS_TSENS_OUT_S);
float temp_c = (temp_f - 32) / 1.8;
return temp_c;
}
static enum psa_voltmeter_resolution adc_resolution = PSA_VOLTMETER_INVALID;
static int R1_val, R2_val;
static float divide_value;
void ntp_config(
const char *server1,
const char *server2,
const char *server3)
{
esp_netif_init();
if (sntp_enabled())
{
sntp_stop();
}
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, (char *)server1);
sntp_setservername(1, (char *)server2);
sntp_setservername(2, (char *)server3);
sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
Serial.println("Starting sntp");
sntp_init();
sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
// sntp_sync_time(tm);
// sntp_sync_time
}
void set_time_zone(long offset, int dst_offset)
{
char cst[17] = {0};
char cdt[17] = "DST";
char tz[33] = {0};
if (offset % 3600)
{
sprintf(cst, "UTC%ld:%02u:%02u", offset / 3600, abs((offset % 3600) / 60), abs(offset % 60));
}
else
{
sprintf(cst, "UTC%ld", offset / 3600);
}
if (dst_offset != 3600)
{
long tz_dst = offset - dst_offset;
if (tz_dst % 3600)
{
sprintf(cdt, "DST%ld:%02u:%02u", tz_dst / 3600, abs((tz_dst % 3600) / 60), abs(tz_dst % 60));
}
else
{
sprintf(cdt, "DST%ld", tz_dst / 3600);
}
}
sprintf(tz, "%s%s", cst, cdt);
setenv("TZ", tz, 1);
tzset();
}
/**
* @brief Read the analog input voltage with voltage divider resistiors R1 and R2.
* Returns voltage in milivolts.
*
* Call this if the ADC is configured with 11 bit resolution.
*
* @param pin Input GPIO pin
* @param R1 R1 value in ohms
* @param R2 R2 value in ohms
* @return uint16_t voltage in milivolts
*/
static uint16_t read_voltmeter_11_bit(uint8_t pin, int R1, int R2)
{
uint16_t raw_value = analogRead(pin) * 2;
if (raw_value > 4095)
{
raw_value = 4095;
}
uint16_t value = ADC_LUT[raw_value];
uint32_t vout = (value * 3333) / 4096;
uint16_t vin = (float)vout / (R2 / (R1 + R2));
return vin;
}
/**
* @brief Read the analog input voltage with voltage divider resistiors R1 and R2.
* Returns voltage in milivolts.
*
* Call this if the ADC is configured with 12 bit resolution.
*
* @param pin Input GPIO pin
* @param R1 R1 value in ohms
* @param R2 R2 value in ohms
* @return uint16_t voltage in milivolts
*/
static uint16_t read_voltmeter_12_bit(uint8_t pin, int R1, int R2)
{
uint16_t raw_value = analogRead(pin);
if (raw_value > 4095)
{
raw_value = 4095;
}
uint16_t value = ADC_LUT[raw_value];
uint32_t vout = (value * 3333) / 4096;
uint16_t vin = (float)vout / (R2 / (R1 + R2));
return vin;
}
enum psa_status init_voltmeter(uint8_t pin, enum psa_voltmeter_resolution resolution)
{
adc1_channel_t channelNum;
switch (pin)
{
case (36):
channelNum = ADC1_CHANNEL_0;
break;
case (39):
channelNum = ADC1_CHANNEL_3;
break;
case (34):
channelNum = ADC1_CHANNEL_6;
break;
case (35):
channelNum = ADC1_CHANNEL_7;
break;
case (32):
channelNum = ADC1_CHANNEL_4;
break;
case (33):
channelNum = ADC1_CHANNEL_5;
break;
default:
return PSA_ERROR;
}
switch (resolution)
{
case PSA_VOLTMETER_11_BIT:
analogReadResolution(11);
adc1_config_channel_atten(channelNum, ADC_ATTEN_DB_11);
adc_resolution = PSA_VOLTMETER_11_BIT;
break;
case PSA_VOLTMETER_12_BIT:
analogReadResolution(12);
adc1_config_channel_atten(channelNum, ADC_ATTEN_DB_11);
adc_resolution = PSA_VOLTMETER_12_BIT;
break;
default:
return PSA_ERROR;
break;
}
return PSA_OK;
}