Skip to content

Commit c65013b

Browse files
Merge branch 'develop'
2 parents c03be96 + 426ab81 commit c65013b

File tree

23 files changed

+299
-1330
lines changed

23 files changed

+299
-1330
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@
329329
path = Sming/Libraries/nanopb/nanopb
330330
url = https://github.com/nanopb/nanopb.git
331331
ignore = dirty
332+
[submodule "Libraries.OneWire"]
333+
path = Sming/Libraries/OneWire
334+
url = https://github.com/PaulStoffregen/OneWire.git
335+
ignore = dirty
332336
[submodule "Libraries.RapidXML"]
333337
path = Sming/Libraries/RapidXML
334338
url = https://github.com/mikee47/Sming-RapidXML

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ You can also try Sming without installing anything locally. We have an [interact
2929

3030
The purpose of Sming is to simplify the creation of embedded applications. The documentation will help you get started in no time.
3131

32-
- [**Documentation for version 6.1.0**](https://sming.readthedocs.io/en/stable) - current stable version.
32+
- [**Documentation for version 6.2.0**](https://sming.readthedocs.io/en/stable) - current stable version.
3333
- [Documentation for latest](https://sming.readthedocs.io/en/latest) - development version.
3434

3535
## Releases
3636

3737
### Stable
3838

39-
- [Sming V6.1.0](https://github.com/SmingHub/Sming/releases/tag/6.1.0) - great new features, performance and stability improvements.
39+
- [Sming V6.2.0](https://github.com/SmingHub/Sming/releases/tag/6.2.0) - great new features, performance and stability improvements.
4040

4141
### Development
4242

Sming/Arch/Esp32/Components/esp32/src/tasks.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,37 @@ void sming_task_loop(void*)
2626
tcpip_callback_msg* callbackMessage;
2727
volatile bool eventQueueFlag;
2828

29+
/**
30+
* @brief Process all messages in the Sming event queue
31+
* Called in the context of the tcpip message thread.
32+
*/
2933
void tcpip_message_handler(void*)
3034
{
3135
eventQueueFlag = false;
36+
// Process only waiting messages (not newly posted ones) so tcpip doesn't starve
37+
auto messageCount = uxQueueMessagesWaiting(eventQueue);
3238
os_event_t evt;
33-
while(xQueueReceive(eventQueue, &evt, 0) == pdTRUE) {
39+
while(messageCount-- && xQueueReceive(eventQueue, &evt, 0) == pdTRUE) {
3440
taskCallback(&evt);
3541
}
3642
}
3743

44+
/**
45+
* @brief Kick the tcpip message handling thread so we get a callback
46+
* @retval bool Woken flag, true if a message was queued, false for no action
47+
*/
48+
bool tcpip_kick_thread()
49+
{
50+
// If queue isn't empty and we haven't already asked for a tcpip callback, do that now
51+
if(xQueueIsQueueEmptyFromISR(eventQueue) == pdFALSE && !eventQueueFlag) {
52+
eventQueueFlag = true;
53+
auto err = tcpip_callbackmsg_trycallback_fromisr(callbackMessage);
54+
return (err == ERR_NEED_SCHED);
55+
}
56+
57+
return false;
58+
}
59+
3860
#endif
3961

4062
} // namespace
@@ -82,6 +104,9 @@ void start_sming_task_loop()
82104

83105
callbackMessage = tcpip_callbackmsg_new(tcpip_callback_fn(tcpip_message_handler), nullptr);
84106

107+
// There may be messages already queued
108+
tcpip_kick_thread();
109+
85110
#endif
86111
}
87112

@@ -103,14 +128,7 @@ bool IRAM_ATTR system_os_post(uint8_t prio, os_signal_t sig, os_param_t par)
103128
// Message loop not yet active
104129
return true;
105130
}
106-
// If queue isn't empty and we haven't already asked for a tcpip callback, do that now
107-
if(xQueueIsQueueEmptyFromISR(eventQueue) == pdFALSE && !eventQueueFlag) {
108-
eventQueueFlag = true;
109-
auto err = tcpip_callbackmsg_trycallback_fromisr(callbackMessage);
110-
woken = (err == ERR_NEED_SCHED);
111-
} else {
112-
woken = false;
113-
}
131+
woken = tcpip_kick_thread();
114132
#endif
115133

116134
portYIELD_FROM_ISR_ARG(woken);

Sming/Arch/Esp32/Core/Digital.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include <esp_clk.h>
1414
#include <hal/gpio_ll.h>
1515
#include <driver/rtc_io.h>
16+
#include <driver/adc.h>
17+
#include <bitset>
18+
#include <debug_progmem.h>
1619
#if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
1720
#include <hal/rtc_io_ll.h>
1821
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 2, 0)
@@ -21,12 +24,21 @@
2124
#endif
2225
#endif
2326

27+
namespace
28+
{
29+
adc_oneshot_unit_handle_t adcUnitHandles[SOC_ADC_PERIPH_NUM];
30+
std::bitset<GPIO_PIN_COUNT> adcInitFlags;
31+
} // namespace
32+
2433
void pinMode(uint16_t pin, uint8_t mode)
2534
{
2635
if(pin >= GPIO_PIN_COUNT) {
2736
return; // Bad pin
2837
}
2938

39+
// Next call to `analogRead` needs to re-initialise
40+
adcInitFlags[pin] = 0;
41+
3042
auto gpio = gpio_num_t(pin);
3143

3244
#if SOC_RTCIO_INPUT_OUTPUT_SUPPORTED
@@ -146,3 +158,38 @@ unsigned long pulseIn(uint16_t pin, uint8_t state, unsigned long timeout)
146158
WAIT_FOR_PIN_STATE(!state);
147159
return clockCyclesToMicroseconds(esp_get_ccount() - pulse_start_cycle_count);
148160
}
161+
162+
uint16_t analogRead(uint16_t pin)
163+
{
164+
adc_unit_t unit_id;
165+
adc_channel_t channel;
166+
esp_err_t err = adc_oneshot_io_to_channel(pin, &unit_id, &channel);
167+
if(err != ESP_OK) {
168+
debug_e("Pin %u is not ADC pin!", pin);
169+
return 0;
170+
}
171+
172+
// Initialise unit
173+
auto& adc_handle = adcUnitHandles[unit_id];
174+
if(!adc_handle) {
175+
adc_oneshot_unit_init_cfg_t init_config{
176+
.unit_id = unit_id,
177+
};
178+
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle));
179+
}
180+
181+
// Initialise channel
182+
if(!adcInitFlags[pin]) {
183+
adc_oneshot_chan_cfg_t channel_config{
184+
.atten = ADC_ATTEN_DB_0,
185+
.bitwidth = ADC_BITWIDTH_DEFAULT,
186+
};
187+
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, channel, &channel_config));
188+
adcInitFlags[pin] = 1;
189+
}
190+
191+
int rawSampleValue{0};
192+
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, channel, &rawSampleValue));
193+
194+
return rawSampleValue;
195+
}

Sming/Arch/Esp32/Core/adc.cpp

Lines changed: 0 additions & 113 deletions
This file was deleted.

Sming/Arch/Esp32/Core/pins_arduino.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#pragma once
1414

15+
#include <soc/adc_channel.h>
16+
1517
#define EXTERNAL_NUM_INTERRUPTS 16
1618
#define NUM_DIGITAL_PINS 40
1719
#define NUM_ANALOG_INPUTS 16
@@ -20,7 +22,7 @@
2022
#define digitalPinToInterrupt(p) (((p) < 40) ? (p) : -1)
2123
#define digitalPinHasPWM(p) (p < 34)
2224

23-
constexpr uint8_t A0{36};
25+
constexpr uint8_t A0 = ADC1_CHANNEL_0_GPIO_NUM;
2426

2527
#define GPIO_REG_TYPE uint32_t
2628

Sming/Arch/Esp8266/Core/Digital.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,11 @@ unsigned long pulseIn(uint16_t pin, uint8_t state, unsigned long timeout)
192192

193193
return cycleTimer.elapsedTime().as<NanoTime::Microseconds>();
194194
}
195+
196+
uint16_t analogRead(uint16_t pin)
197+
{
198+
if(pin == A0)
199+
return system_adc_read();
200+
else
201+
return -1; // Not supported
202+
}

Sming/Arch/Esp8266/Core/adc.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

Sming/Arch/Rp2040/Core/Digital.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,22 @@
1414
#include <esp_systemapi.h>
1515
#include <hardware/gpio.h>
1616
#include <Platform/Timers.h>
17+
#include <hardware/adc.h>
18+
#include <bitset>
19+
20+
#define PIN_ADC0 26
21+
#define PIN_ADC1 27
22+
#define PIN_ADC2 28
23+
#define PIN_ADC3 29
24+
#define PIN_TEMP 30 // Not a GPIO
25+
#define ADC_TEMP 4
1726

1827
namespace
1928
{
2029
constexpr uint32_t PIN_COUNT = NUM_BANK0_GPIOS;
2130

31+
std::bitset<NUM_ADC_CHANNELS> adcInitFlags;
32+
2233
#define CHECK_PIN(pin_number, ...) \
2334
if(pin_number >= PIN_COUNT) { \
2435
SYSTEM_ERROR("ERROR: Illegal pin in %s (%d)", __FUNCTION__, pin_number); \
@@ -58,6 +69,12 @@ void pinMode(uint16_t pin, uint8_t mode)
5869
SYSTEM_ERROR("ERROR: Illegal pinMode mode (%d)", mode);
5970
return;
6071
}
72+
73+
if(pin >= PIN_ADC0 && pin <= PIN_TEMP) {
74+
// Next call to `analogRead` needs to re-initialise
75+
uint8_t channel = pin - PIN_ADC0;
76+
adcInitFlags[channel] = 0;
77+
}
6178
}
6279

6380
bool isInputPin(uint16_t pin)
@@ -130,3 +147,27 @@ unsigned long pulseIn(uint16_t pin, uint8_t state, unsigned long timeout)
130147

131148
return timer.elapsedTicks();
132149
}
150+
151+
uint16_t analogRead(uint16_t pin)
152+
{
153+
if(pin < PIN_ADC0 || pin > PIN_TEMP) {
154+
// Not an analogue pin
155+
return 0;
156+
}
157+
158+
uint8_t channel = pin - PIN_ADC0;
159+
if((adc_hw->cs & ADC_CS_EN_BITS) == 0) {
160+
adc_init();
161+
}
162+
if(!adcInitFlags[channel]) {
163+
if(channel == ADC_TEMP) {
164+
adc_set_temp_sensor_enabled(true);
165+
} else {
166+
adc_gpio_init(pin);
167+
}
168+
adcInitFlags[channel] = true;
169+
}
170+
171+
adc_select_input(channel);
172+
return adc_read();
173+
}

0 commit comments

Comments
 (0)