Skip to content

Commit c30012a

Browse files
committed
Add Kconfig for IDF and option to disable HAL mutexes
IDF Options: - Autostart Arduino (implements app_main) - Disable HAL locks - Set HAL debug level - Auto-connect STA if configured (else will connect after WiFi.begin())
1 parent c82699a commit c30012a

11 files changed

+174
-22
lines changed

Diff for: Kconfig

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
menu "Arduino Configuration"
2+
3+
config AUTOSTART_ARDUINO
4+
bool "Autostart Arduino setup and loop on boot"
5+
default "n"
6+
help
7+
Enabling this option will implement app_main and start Arduino.
8+
All you need to implement in your main.cpp is setup() and loop()
9+
and include Arduino.h
10+
If disabled, you can call initArduino() to run any preparations
11+
required by the framework
12+
13+
config DISABLE_HAL_LOCKS
14+
bool "Disable mutex locks for HAL"
15+
default "n"
16+
help
17+
Enabling this option will run all hardware abstraction without locks.
18+
While communication with external hardware will be faster, you need to
19+
make sure that there is no option to use the same bus from another thread
20+
or interrupt at the same time. Option is best used with Arduino enabled
21+
and code implemented only in setup/loop and Arduino callbacks
22+
23+
menu "Debug Log Configuration"
24+
choice ARDUHAL_LOG_DEFAULT_LEVEL
25+
bool "Default log level"
26+
default ARDUHAL_LOG_DEFAULT_LEVEL_ERROR
27+
help
28+
Specify how much output to see in logs by default.
29+
30+
config ARDUHAL_LOG_DEFAULT_LEVEL_NONE
31+
bool "No output"
32+
config ARDUHAL_LOG_DEFAULT_LEVEL_ERROR
33+
bool "Error"
34+
config ARDUHAL_LOG_DEFAULT_LEVEL_WARN
35+
bool "Warning"
36+
config ARDUHAL_LOG_DEFAULT_LEVEL_INFO
37+
bool "Info"
38+
config ARDUHAL_LOG_DEFAULT_LEVEL_DEBUG
39+
bool "Debug"
40+
config ARDUHAL_LOG_DEFAULT_LEVEL_VERBOSE
41+
bool "Verbose"
42+
endchoice
43+
44+
config ARDUHAL_LOG_DEFAULT_LEVEL
45+
int
46+
default 0 if ARDUHAL_LOG_DEFAULT_LEVEL_NONE
47+
default 1 if ARDUHAL_LOG_DEFAULT_LEVEL_ERROR
48+
default 2 if ARDUHAL_LOG_DEFAULT_LEVEL_WARN
49+
default 3 if ARDUHAL_LOG_DEFAULT_LEVEL_INFO
50+
default 4 if ARDUHAL_LOG_DEFAULT_LEVEL_DEBUG
51+
default 5 if ARDUHAL_LOG_DEFAULT_LEVEL_VERBOSE
52+
53+
config ARDUHAL_LOG_COLORS
54+
bool "Use ANSI terminal colors in log output"
55+
default "n"
56+
help
57+
Enable ANSI terminal color codes in bootloader output.
58+
In order to view these, your terminal program must support ANSI color codes.
59+
60+
endmenu
61+
62+
config AUTOCONNECT_WIFI
63+
bool "Autoconnect WiFi on boot"
64+
default "n"
65+
depends on AUTOSTART_ARDUINO
66+
help
67+
If enabled, WiFi will connect to the last used SSID (if station was enabled),
68+
else connection will be started only after calling WiFi.begin(ssid, password)
69+
70+
endmenu

Diff for: component.mk

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
COMPONENT_ADD_INCLUDEDIRS := cores/esp32 variants/esp32 libraries/WiFi/src libraries/SPI/src libraries/Wire/src
22
COMPONENT_PRIV_INCLUDEDIRS := cores/esp32/libb64
33
COMPONENT_SRCDIRS := cores/esp32/libb64 cores/esp32 variants/esp32 libraries/WiFi/src libraries/SPI/src libraries/Wire/src
4-
include $(IDF_PATH)/make/component_common.mk
54
CXXFLAGS += -fno-rtti

Diff for: cores/esp32/esp32-hal-i2c.c

+14
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030

3131
struct i2c_struct_t {
3232
i2c_dev_t * dev;
33+
#if !CONFIG_DISABLE_HAL_LOCKS
3334
xSemaphoreHandle lock;
35+
#endif
3436
uint8_t num;
3537
};
3638

@@ -42,13 +44,23 @@ enum {
4244
I2C_CMD_END
4345
};
4446

47+
#if CONFIG_DISABLE_HAL_LOCKS
48+
#define I2C_MUTEX_LOCK()
49+
#define I2C_MUTEX_UNLOCK()
50+
51+
static i2c_t _i2c_bus_array[2] = {
52+
{(volatile i2c_dev_t *)(DR_REG_I2C_EXT_BASE), 0},
53+
{(volatile i2c_dev_t *)(DR_REG_I2C1_EXT_BASE), 1}
54+
};
55+
#else
4556
#define I2C_MUTEX_LOCK() do {} while (xSemaphoreTake(i2c->lock, portMAX_DELAY) != pdPASS)
4657
#define I2C_MUTEX_UNLOCK() xSemaphoreGive(i2c->lock)
4758

4859
static i2c_t _i2c_bus_array[2] = {
4960
{(volatile i2c_dev_t *)(DR_REG_I2C_EXT_BASE), NULL, 0},
5061
{(volatile i2c_dev_t *)(DR_REG_I2C1_EXT_BASE), NULL, 1}
5162
};
63+
#endif
5264

5365
i2c_err_t i2cAttachSCL(i2c_t * i2c, int8_t scl)
5466
{
@@ -353,12 +365,14 @@ i2c_t * i2cInit(uint8_t i2c_num, uint16_t slave_addr, bool addr_10bit_en)
353365

354366
i2c_t * i2c = &_i2c_bus_array[i2c_num];
355367

368+
#if !CONFIG_DISABLE_HAL_LOCKS
356369
if(i2c->lock == NULL){
357370
i2c->lock = xSemaphoreCreateMutex();
358371
if(i2c->lock == NULL) {
359372
return NULL;
360373
}
361374
}
375+
#endif
362376

363377
if(i2c_num == 0) {
364378
SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG,DPORT_I2C_EXT0_CLK_EN);

Diff for: cores/esp32/esp32-hal-ledc.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
#include "soc/ledc_reg.h"
2323
#include "soc/ledc_struct.h"
2424

25-
xSemaphoreHandle _ledc_sys_lock;
26-
27-
25+
#if CONFIG_DISABLE_HAL_LOCKS
26+
#define LEDC_MUTEX_LOCK()
27+
#define LEDC_MUTEX_UNLOCK()
28+
#else
2829
#define LEDC_MUTEX_LOCK() do {} while (xSemaphoreTake(_ledc_sys_lock, portMAX_DELAY) != pdPASS)
2930
#define LEDC_MUTEX_UNLOCK() xSemaphoreGive(_ledc_sys_lock)
31+
xSemaphoreHandle _ledc_sys_lock;
32+
#endif
3033

3134
/*
3235
* LEDC Chan to Group/Channel/Timer Mapping
@@ -59,7 +62,9 @@ void ledcSetupTimer(uint8_t chan, uint32_t div_num, uint8_t bit_num, bool apb_cl
5962
SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_LEDC_CLK_EN);
6063
CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_LEDC_RST);
6164
ledc_dev->conf.apb_clk_sel = 1;//LS use apb clock
65+
#if !CONFIG_DISABLE_HAL_LOCKS
6266
_ledc_sys_lock = xSemaphoreCreateMutex();
67+
#endif
6368
}
6469
LEDC_MUTEX_LOCK();
6570
ledc_dev->timer_group[group].timer[timer].conf.div_num = div_num;//18 bit (10.8) This register is used to configure parameter for divider in timer the least significant eight bits represent the decimal part.

Diff for: cores/esp32/esp32-hal-misc.c

+15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ void delayMicroseconds(uint32_t us)
4747
}
4848
}
4949

50+
void initVariant() __attribute__((weak));
51+
void initVariant() {}
52+
53+
void init() __attribute__((weak));
54+
void init() {}
55+
56+
void initWiFi() __attribute__((weak));
57+
void initWiFi() {}
58+
59+
void initArduino(){
60+
init();
61+
initVariant();
62+
initWiFi();
63+
}
64+
5065
//used by hal log
5166
const char * IRAM_ATTR pathToFileName(const char * path){
5267
size_t i = 0;

Diff for: cores/esp32/esp32-hal-sd.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
#include "soc/gpio_sd_reg.h"
2222
#include "soc/gpio_sd_struct.h"
2323

24-
xSemaphoreHandle _sd_sys_lock;
2524

25+
#if CONFIG_DISABLE_HAL_LOCKS
26+
#define SD_MUTEX_LOCK()
27+
#define SD_MUTEX_UNLOCK()
28+
#else
2629
#define SD_MUTEX_LOCK() do {} while (xSemaphoreTake(_sd_sys_lock, portMAX_DELAY) != pdPASS)
2730
#define SD_MUTEX_UNLOCK() xSemaphoreGive(_sd_sys_lock)
28-
31+
xSemaphoreHandle _sd_sys_lock;
32+
#endif
2933

3034
uint32_t sdSetup(uint8_t channel, uint32_t freq) //chan 0-7 freq 1220-312500
3135
{
@@ -35,7 +39,9 @@ uint32_t sdSetup(uint8_t channel, uint32_t freq) //chan 0-7 freq 1220-312500
3539
static bool tHasStarted = false;
3640
if(!tHasStarted) {
3741
tHasStarted = true;
42+
#if !CONFIG_DISABLE_HAL_LOCKS
3843
_sd_sys_lock = xSemaphoreCreateMutex();
44+
#endif
3945
}
4046
gpio_sd_dev_t * gpio_sd_dev = (volatile gpio_sd_dev_t *)(DR_REG_GPIO_SD_BASE);
4147
uint32_t prescale = (10000000/(freq*32)) - 1;

Diff for: cores/esp32/esp32-hal-spi.c

+16
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,23 @@
4141

4242
struct spi_struct_t {
4343
spi_dev_t * dev;
44+
#if !CONFIG_DISABLE_HAL_LOCKS
4445
xSemaphoreHandle lock;
46+
#endif
4547
uint8_t num;
4648
};
4749

50+
#if CONFIG_DISABLE_HAL_LOCKS
51+
#define SPI_MUTEX_LOCK()
52+
#define SPI_MUTEX_UNLOCK()
53+
54+
static spi_t _spi_bus_array[4] = {
55+
{(volatile spi_dev_t *)(DR_REG_SPI0_BASE), 0},
56+
{(volatile spi_dev_t *)(DR_REG_SPI1_BASE), 1},
57+
{(volatile spi_dev_t *)(DR_REG_SPI2_BASE), 2},
58+
{(volatile spi_dev_t *)(DR_REG_SPI3_BASE), 3}
59+
};
60+
#else
4861
#define SPI_MUTEX_LOCK() do {} while (xSemaphoreTake(spi->lock, portMAX_DELAY) != pdPASS)
4962
#define SPI_MUTEX_UNLOCK() xSemaphoreGive(spi->lock)
5063

@@ -54,6 +67,7 @@ static spi_t _spi_bus_array[4] = {
5467
{(volatile spi_dev_t *)(DR_REG_SPI2_BASE), NULL, 2},
5568
{(volatile spi_dev_t *)(DR_REG_SPI3_BASE), NULL, 3}
5669
};
70+
#endif
5771

5872
void spiAttachSCK(spi_t * spi, int8_t sck)
5973
{
@@ -383,12 +397,14 @@ spi_t * spiStartBus(uint8_t spi_num, uint32_t clockDiv, uint8_t dataMode, uint8_
383397

384398
spi_t * spi = &_spi_bus_array[spi_num];
385399

400+
#if !CONFIG_DISABLE_HAL_LOCKS
386401
if(spi->lock == NULL){
387402
spi->lock = xSemaphoreCreateMutex();
388403
if(spi->lock == NULL) {
389404
return NULL;
390405
}
391406
}
407+
#endif
392408

393409
if(spi_num == HSPI) {
394410
SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_SPI_CLK_EN_1);

Diff for: cores/esp32/esp32-hal-uart.c

+19
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,23 @@ static int s_uart_debug_nr = 0;
4040

4141
struct uart_struct_t {
4242
uart_dev_t * dev;
43+
#if !CONFIG_DISABLE_HAL_LOCKS
4344
xSemaphoreHandle lock;
45+
#endif
4446
uint8_t num;
4547
xQueueHandle queue;
4648
};
4749

50+
#if CONFIG_DISABLE_HAL_LOCKS
51+
#define UART_MUTEX_LOCK()
52+
#define UART_MUTEX_UNLOCK()
53+
54+
static uart_t _uart_bus_array[3] = {
55+
{(volatile uart_dev_t *)(DR_REG_UART_BASE), 0, NULL},
56+
{(volatile uart_dev_t *)(DR_REG_UART1_BASE), 1, NULL},
57+
{(volatile uart_dev_t *)(DR_REG_UART2_BASE), 2, NULL}
58+
};
59+
#else
4860
#define UART_MUTEX_LOCK() do {} while (xSemaphoreTake(uart->lock, portMAX_DELAY) != pdPASS)
4961
#define UART_MUTEX_UNLOCK() xSemaphoreGive(uart->lock)
5062

@@ -53,6 +65,7 @@ static uart_t _uart_bus_array[3] = {
5365
{(volatile uart_dev_t *)(DR_REG_UART1_BASE), NULL, 1, NULL},
5466
{(volatile uart_dev_t *)(DR_REG_UART2_BASE), NULL, 2, NULL}
5567
};
68+
#endif
5669

5770
static void IRAM_ATTR _uart_isr(void *arg)
5871
{
@@ -163,12 +176,14 @@ uart_t* uartBegin(uint8_t uart_nr, uint32_t baudrate, uint32_t config, int8_t rx
163176

164177
uart_t* uart = &_uart_bus_array[uart_nr];
165178

179+
#if !CONFIG_DISABLE_HAL_LOCKS
166180
if(uart->lock == NULL) {
167181
uart->lock = xSemaphoreCreateMutex();
168182
if(uart->lock == NULL) {
169183
return NULL;
170184
}
171185
}
186+
#endif
172187

173188
if(queueLen && uart->queue == NULL) {
174189
uart->queue = xQueueCreate(queueLen, sizeof(uint8_t)); //initialize the queue
@@ -379,13 +394,17 @@ int log_printf(const char *format, ...)
379394
}
380395
}
381396
vsnprintf(temp, len+1, format, arg);
397+
#if !CONFIG_DISABLE_HAL_LOCKS
382398
if(_uart_bus_array[s_uart_debug_nr].lock){
383399
while (xSemaphoreTake(_uart_bus_array[s_uart_debug_nr].lock, portMAX_DELAY) != pdPASS);
384400
ets_printf("%s", temp);
385401
xSemaphoreGive(_uart_bus_array[s_uart_debug_nr].lock);
386402
} else {
387403
ets_printf("%s", temp);
388404
}
405+
#else
406+
ets_printf("%s", temp);
407+
#endif
389408
va_end(arg);
390409
if(len > 64){
391410
free(temp);

Diff for: cores/esp32/esp32-hal.h

+12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ extern "C" {
3333
#include <string.h>
3434
#include <math.h>
3535

36+
#ifndef CONFIG_DISABLE_HAL_LOCKS
37+
#define CONFIG_DISABLE_HAL_LOCKS 0
38+
#endif
39+
40+
#ifndef CONFIG_AUTOSTART_ARDUINO
41+
#define CONFIG_AUTOSTART_ARDUINO 1
42+
#endif
43+
3644
//forward declaration from freertos/portmacro.h
3745
void vPortYield( void );
3846
#define yield() vPortYield()
@@ -56,6 +64,10 @@ uint32_t millis();
5664
void delay(uint32_t);
5765
void delayMicroseconds(uint32_t us);
5866

67+
#if !CONFIG_AUTOSTART_ARDUINO
68+
void initArduino();
69+
#endif
70+
5971
#ifdef __cplusplus
6072
}
6173
#endif

Diff for: cores/esp32/main.cpp

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
#include "freertos/FreeRTOS.h"
22
#include "freertos/task.h"
3+
#include "esp32-hal.h"
34

4-
void initVariant() __attribute__((weak));
5-
void initVariant() {}
5+
#if CONFIG_AUTOSTART_ARDUINO
66

7-
void init() __attribute__((weak));
8-
void init() {}
7+
extern "C" void initArduino();
8+
extern void loop();
9+
extern void setup();
910

1011
void startWiFi() __attribute__((weak));
1112
void startWiFi() {}
1213

13-
void initWiFi() __attribute__((weak));
14-
void initWiFi() {}
15-
16-
extern void loop();
17-
extern void setup();
18-
1914
void loopTask(void *pvParameters)
2015
{
2116
bool setup_done = false;
@@ -31,9 +26,8 @@ void loopTask(void *pvParameters)
3126

3227
extern "C" void app_main()
3328
{
34-
init();
35-
initVariant();
36-
initWiFi();
29+
initArduino();
3730
xTaskCreatePinnedToCore(loopTask, "loopTask", 4096, NULL, 1, NULL, 1);
3831
}
3932

33+
#endif

0 commit comments

Comments
 (0)