Skip to content

Commit 559fb85

Browse files
committed
Update for Arduino-ESP32 3.x API and improve ESC init
Refactor buzzer and vibration motor initialization to use the new ledcAttach API from Arduino-ESP32 3.x. Update BLE characteristic callbacks to use String instead of std::string for compatibility. Improve ESC initialization by deferring throttle setup until after the first CAN process and add a readiness check. Update watchdog initialization to use the new esp_task_wdt_init config structure. Minor fix to pin initialization order in setup.
1 parent 6d8ae6a commit 559fb85

File tree

6 files changed

+37
-24
lines changed

6 files changed

+37
-24
lines changed

platformio.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ lib_ignore =
2121

2222

2323
[env:OpenPPG-CESP32S3-CAN-SP140]
24-
platform = espressif32@6.11.0
24+
platform = https://github.com/pioarduino/platform-espressif32.git#develop
2525
board = m5stack-stamps3
2626
framework = arduino
2727
src_folder = sp140
@@ -35,6 +35,7 @@ build_flags =
3535
-D LV_CONF_INCLUDE_SIMPLE
3636
-I inc/sp140/lvgl
3737
-D LV_LVGL_H_INCLUDE_SIMPLE
38+
-D USBSerial=Serial
3839

3940
build_type = debug
4041
debug_speed = 12000

src/sp140/buzzer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ static bool buzzerInitialized = false;
2929
* @return Returns true if initialization was successful, false otherwise
3030
*/
3131
bool initBuzz() {
32-
// Setup LEDC channel for buzzer
33-
ledcSetup(BUZZER_PWM_CHANNEL, BUZZER_PWM_FREQUENCY, BUZZER_PWM_RESOLUTION);
34-
ledcAttachPin(board_config.buzzer_pin, BUZZER_PWM_CHANNEL);
32+
// Setup LEDC channel for buzzer (Arduino-ESP32 3.x API)
33+
// Channels are implicitly allocated; attach pin to channel and set frequency/resolution
34+
ledcAttach(board_config.buzzer_pin, BUZZER_PWM_FREQUENCY, BUZZER_PWM_RESOLUTION);
3535
buzzerInitialized = true;
3636
return true;
3737
}
@@ -43,9 +43,9 @@ void startTone(uint16_t frequency) {
4343
if (!buzzerInitialized || !ENABLE_BUZZ) return;
4444

4545
// Change the frequency for this channel
46-
ledcChangeFrequency(BUZZER_PWM_CHANNEL, frequency, BUZZER_PWM_RESOLUTION);
46+
ledcWriteTone(board_config.buzzer_pin, frequency);
4747
// Set 50% duty cycle (square wave)
48-
ledcWrite(BUZZER_PWM_CHANNEL, 128);
48+
ledcWrite(board_config.buzzer_pin, 128);
4949
}
5050

5151
/**
@@ -55,7 +55,7 @@ void stopTone() {
5555
if (!buzzerInitialized) return;
5656

5757
// Set duty cycle to 0 to stop the tone
58-
ledcWrite(BUZZER_PWM_CHANNEL, 0);
58+
ledcWrite(board_config.buzzer_pin, 0);
5959
}
6060

6161
/**

src/sp140/esc.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static CanardAdapter adapter;
1515
static uint8_t memory_pool[1024] __attribute__((aligned(8)));
1616
static SineEsc esc(adapter);
1717
static unsigned long lastSuccessfulCommTimeMs = 0; // Store millis() time of last successful ESC comm
18+
static bool escReady = false;
1819

1920

2021
STR_ESC_TELEMETRY_140 escTelemetryData = {
@@ -36,14 +37,13 @@ void initESC() {
3637
}
3738

3839
adapter.begin(memory_pool, sizeof(memory_pool));
39-
adapter.setLocalNodeId(LOCAL_NODE_ID);
4040
esc.begin(0x20); // Default ID for the ESC
41+
adapter.setLocalNodeId(LOCAL_NODE_ID);
4142

42-
// Set idle throttle only if ESC is found
43-
const uint16_t IdleThrottle_us = 10000; // 1000us (0.1us resolution)
44-
esc.setThrottleSettings2(IdleThrottle_us);
43+
// Defer sending throttle until after first adapter process to avoid null pointer in CANARD
4544
adapter.processTxRxOnce();
46-
vTaskDelay(pdMS_TO_TICKS(20)); // Wait for ESC to process the command
45+
vTaskDelay(pdMS_TO_TICKS(20)); // Give ESC time to be ready
46+
escReady = true;
4747
}
4848

4949
/**
@@ -54,6 +54,10 @@ void initESC() {
5454
* Important: The ESC requires messages at least every 300ms or it will reset
5555
*/
5656
void setESCThrottle(int throttlePWM) {
57+
// Ensure TWAI/ESC subsystem is initialized
58+
if (!escTwaiInitialized || !escReady) {
59+
return;
60+
}
5761
// Input validation
5862
if (throttlePWM < 1000 || throttlePWM > 2000) {
5963
return; // Ignore invalid throttle values

src/sp140/extra-data.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void updateBMSTelemetry(const STR_BMS_TELEMETRY_140& telemetry) {
224224

225225
class MetricAltCallbacks: public BLECharacteristicCallbacks {
226226
void onWrite(BLECharacteristic *pCharacteristic) {
227-
std::string value = pCharacteristic->getValue();
227+
String value = pCharacteristic->getValue();
228228

229229
if (value.length() == 1) { // Ensure we only get a single byte
230230
USBSerial.print("New: ");
@@ -243,7 +243,7 @@ class MetricAltCallbacks: public BLECharacteristicCallbacks {
243243

244244
class PerformanceModeCallbacks: public BLECharacteristicCallbacks {
245245
void onWrite(BLECharacteristic *pCharacteristic) {
246-
std::string value = pCharacteristic->getValue();
246+
String value = pCharacteristic->getValue();
247247

248248
if (value.length() == 1) {
249249
uint8_t mode = value[0];
@@ -262,7 +262,7 @@ class PerformanceModeCallbacks: public BLECharacteristicCallbacks {
262262

263263
class ScreenRotationCallbacks: public BLECharacteristicCallbacks {
264264
void onWrite(BLECharacteristic *pCharacteristic) {
265-
std::string value = pCharacteristic->getValue();
265+
String value = pCharacteristic->getValue();
266266

267267
if (value.length() == 1) {
268268
uint8_t rotation = value[0];
@@ -293,7 +293,7 @@ class ThrottleValueCallbacks: public BLECharacteristicCallbacks {
293293
return; // Only allow updates while in cruise mode
294294
}
295295

296-
std::string value = pCharacteristic->getValue();
296+
String value = pCharacteristic->getValue();
297297
if (value.length() == 2) { // Expecting 2 bytes for PWM value
298298
uint16_t newPWM = (value[0] << 8) | value[1];
299299

@@ -325,14 +325,14 @@ class MyServerCallbacks: public BLEServerCallbacks {
325325

326326
class TimeCallbacks: public BLECharacteristicCallbacks {
327327
void onWrite(BLECharacteristic *pCharacteristic) {
328-
std::string value = pCharacteristic->getValue();
328+
String value = pCharacteristic->getValue();
329329

330330
if (value.length() == sizeof(time_t)) { // Expecting just a unix timestamp
331331
struct timeval tv;
332332
time_t timestamp;
333333

334334
// Copy the incoming timestamp
335-
memcpy(&timestamp, value.data(), sizeof(timestamp));
335+
memcpy(&timestamp, value.c_str(), sizeof(timestamp));
336336

337337
// Apply timezone offset
338338
timestamp += deviceData.timezone_offset;
@@ -360,11 +360,11 @@ class TimeCallbacks: public BLECharacteristicCallbacks {
360360

361361
class TimezoneCallbacks: public BLECharacteristicCallbacks {
362362
void onWrite(BLECharacteristic *pCharacteristic) {
363-
std::string value = pCharacteristic->getValue();
363+
String value = pCharacteristic->getValue();
364364

365365
if (value.length() == 4) { // Expecting 4 bytes for timezone offset
366366
int32_t offset;
367-
memcpy(&offset, value.data(), sizeof(offset));
367+
memcpy(&offset, value.c_str(), sizeof(offset));
368368

369369
deviceData.timezone_offset = offset;
370370
writeDeviceData();

src/sp140/sp140.ino

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,15 @@ void setupAnalogRead() {
587587
void setupWatchdog() {
588588
#ifndef OPENPPG_DEBUG
589589
// Initialize Task Watchdog
590-
ESP_ERROR_CHECK(esp_task_wdt_init(3000, true)); // 3 second timeout, panic on timeout
590+
esp_task_wdt_config_t twdt_config = {
591+
.timeout_ms = 3000,
592+
.idle_core_mask = 0, // do not subscribe idle tasks
593+
.trigger_panic = true,
594+
};
595+
esp_err_t wdt_init_result = esp_task_wdt_init(&twdt_config);
596+
if (wdt_init_result != ESP_OK && wdt_init_result != ESP_ERR_INVALID_STATE) {
597+
ESP_ERROR_CHECK(wdt_init_result);
598+
}
591599
#endif // OPENPPG_DEBUG
592600
}
593601

@@ -604,8 +612,8 @@ void setup() {
604612

605613
// Pull CSB (pin 42) high to activate I2C mode
606614
// temporary fix TODO remove
607-
digitalWrite(42, HIGH);
608615
pinMode(42, OUTPUT);
616+
digitalWrite(42, HIGH);
609617

610618
// Initialize LVGL mutex before anything else
611619
lvglMutex = xSemaphoreCreateMutex();

src/sp140/vibration_pwm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ void vibeTask(void* parameter) {
6161
*/
6262
bool initVibeMotor() {
6363
extern HardwareConfig board_config;
64-
ledcSetup(VIBE_PWM_CHANNEL, VIBE_PWM_FREQ, VIBE_PWM_RESOLUTION);
65-
ledcAttachPin(board_config.vibe_pwm, VIBE_PWM_CHANNEL);
64+
// Arduino-ESP32 3.x LEDC API: use ledcAttach(pin, freq, resolution)
65+
ledcAttach(board_config.vibe_pwm, VIBE_PWM_FREQ, VIBE_PWM_RESOLUTION);
6666

6767
// Create vibration queue
6868
vibeQueue = xQueueCreate(5, sizeof(VibeRequest));

0 commit comments

Comments
 (0)