Skip to content

Commit 3cc623b

Browse files
authored
Merge pull request #1880 from MaBecker/esp32_improvements
ESP32 improvements
2 parents f7a6ecb + a38b805 commit 3cc623b

File tree

11 files changed

+77
-77
lines changed

11 files changed

+77
-77
lines changed

libs/filesystem/fat_sd/flash_diskio.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ DSTATUS disk_initialize (
4242
{
4343
if (drv != 0)
4444
return STA_NODISK;
45-
//jsDebug("Flash Disk Init %d",drv);
4645
return RES_OK;
4746
}
4847

@@ -76,7 +75,7 @@ DRESULT disk_read (
7675
uint16_t size = (uint16_t)(count * FS_SECTOR_SIZE);
7776
uint32_t addr = sector * FS_SECTOR_SIZE + fs_flash_base;
7877

79-
jsDebug("Flash disk_read sector: %d, buff: mem: %d buff: %d len: %d", sector, addr, buff, size);
78+
jsDebug(DBG_INFO,"Flash disk_read sector: %d, buff: mem: %d buff: %d len: %d", sector, addr, buff, size);
8079
jshFlashRead( buff, addr, size);
8180
return RES_OK;
8281
}
@@ -98,7 +97,7 @@ DRESULT disk_write (
9897
uint16_t size = (uint16_t)(count * FS_SECTOR_SIZE);
9998
uint32_t addr = sector * FS_SECTOR_SIZE + fs_flash_base;
10099

101-
jsDebug("Flash disk_write sector: %d, buff: mem: %d buff: %d len: %d", sector, addr, buff, size);
100+
jsDebug(DBG_INFO,"Flash disk_write sector: %d, buff: mem: %d buff: %d len: %d", sector, addr, buff, size);
102101
jshFlashErasePage(addr);
103102
jshFlashWrite( (void *)buff, addr,size);
104103

@@ -123,7 +122,7 @@ DRESULT disk_ioctl (
123122
switch (ctrl) {
124123
case CTRL_SYNC : /// Make sure that no pending write process
125124
res = RES_OK;
126-
jsDebug("Flash disk_ioctl CTRL_SYNC");
125+
jsDebug(DBG_INFO,"Flash disk_ioctl CTRL_SYNC");
127126
break;
128127

129128
case GET_SECTOR_COUNT : // Get number of sectors on the disk (DWORD)

libs/filesystem/fat_sd/flash_diskio.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,3 @@
3131
#define FS_FLASH_BASE 0x300000;
3232

3333
uint8_t flashFatFsInit( uint32_t addr, uint16_t sectors );
34-
35-
/*
36-
#ifndef RELEASE
37-
#define jsDebug(fmt,...) jsWarn(fmt,##__VA_ARGS__)
38-
#else
39-
#define jsDebug(fmt,...)
40-
#endif
41-
#endif
42-
*/
43-
#define jsDebug(fmt,...)

libs/filesystem/jswrap_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ int jswrap_E_flashFatFS(JsVar* options) {
649649
if (init) {
650650
if ( format ) {
651651
uint8_t res = f_mount(&jsfsFAT, "", 0);
652-
jsDebug("Formatting Flash");
652+
jsDebug(DBG_INFO,"Formatting Flash");
653653
res = f_mkfs("", 1, 0); // Super Floppy format, using all space (not partition table)
654654
if (res != FR_OK) {
655655
jsExceptionHere(JSET_INTERNALERROR, "Flash Formatting error:",res);

libs/network/esp32/jswrap_esp32_network.c

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@
4242

4343
#define UNUSED(x) (void)(x)
4444

45-
#ifndef RELEASE
46-
#define jsDebug(format, ...) jsWarn(format, ## __VA_ARGS__)
47-
#else
48-
#define jsDebug(format, ...) do { } while(0)
49-
#endif
50-
5145
static void sendWifiCompletionCB(
5246
JsVar **g_jsCallback, //!< Pointer to the global callback variable
5347
char *reason //!< NULL if successful, error string otherwise
@@ -233,7 +227,7 @@ static char *wifiReasonToString(uint8_t reason) {
233227
case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
234228
return "REASON_UNSUPP_RSN_IE_VERSION";
235229
}
236-
jsDebug( "wifiReasonToString: Unknown reason %d", reason);
230+
jsDebug(DBG_INFO, "wifiReasonToString: Unknown reason %d", reason);
237231
return "Unknown reason";
238232
} // End of wifiReasonToString
239233

@@ -413,7 +407,7 @@ static JsVar *getWifiModule() {
413407
static int s_retry_num = 0;
414408

415409
static char *wifiGetEvent(uint32_t event) {
416-
jsDebug( "wifiGetEvent: Got event: %d", event);
410+
jsDebug(DBG_INFO,"wifiGetEvent: Got event: %d", event);
417411
switch(event) {
418412
case SYSTEM_EVENT_AP_PROBEREQRECVED:
419413
return "#onprobe_recv";
@@ -442,7 +436,7 @@ switch(event) {
442436
case SYSTEM_EVENT_WIFI_READY:
443437
break;
444438
}
445-
jsDebug( "Unhandled wifi event type: %d", event);
439+
jsDebug(DBG_INFO, "Unhandled wifi event type: %d", event);
446440
return NULL;
447441
} // End of wifiGetEvent
448442

@@ -488,12 +482,12 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
488482
* * bssid
489483
* * reason
490484
*/
491-
jsDebug("Wifi: Event(%d):SYSTEM_EVENT_%s\n",event->event_id,wifiEventToString(event->event_id));
485+
jsDebug(DBG_INFO,"Wifi: Event(%d):SYSTEM_EVENT_%s\n",event->event_id,wifiEventToString(event->event_id));
492486

493487
if (event->event_id == SYSTEM_EVENT_STA_DISCONNECTED) {
494488
if (--s_retry_num > 0 ) {
495489
esp_wifi_connect();
496-
jsDebug("retry to AP connect");
490+
jsDebug(DBG_INFO,"retry to AP connect");
497491
return;
498492
}
499493
g_isStaConnected = false; // Flag as disconnected
@@ -578,7 +572,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
578572
jsvObjectSetChildAndUnLock(jsDetails, "netmask", jsvNewFromString(temp));
579573
sprintf(temp, IPSTR, IP2STR(&event->event_info.got_ip.ip_info.gw));
580574
jsvObjectSetChildAndUnLock(jsDetails, "gw", jsvNewFromString(temp));
581-
jsDebug("Wifi: About to emit connect!");
575+
jsDebug(DBG_INFO, "Wifi: About to emit connect!");
582576
sendWifiEvent(event->event_id, jsDetails);
583577
// start mDNS
584578
const char * hostname;
@@ -642,7 +636,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
642636
sendWifiCompletionCB(&g_jsAPStartedCallback, NULL);
643637
return ESP_OK;
644638
}
645-
jsDebug("Wifi: event_handler -> NOT HANDLED EVENT: %d", event->event_id );
639+
jsDebug(DBG_INFO, "Wifi: event_handler -> NOT HANDLED EVENT: %d", event->event_id );
646640
return ESP_OK;
647641
} // End of event_handler
648642

@@ -658,7 +652,7 @@ void esp32_wifi_init() {
658652
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
659653
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
660654

661-
jsDebug("esp32_wifi_init complete");
655+
jsDebug(DBG_INFO, "esp32_wifi_init complete");
662656

663657
} // End of esp32_wifi_init
664658

@@ -674,7 +668,7 @@ static void sendWifiCompletionCB(
674668
JsVar **g_jsCallback, //!< Pointer to the global callback variable
675669
char *reason //!< NULL if successful, error string otherwise
676670
) {
677-
jsDebug("sendWifiCompletionCB");
671+
jsDebug(DBG_INFO, "sendWifiCompletionCB");
678672
// Check that we have a callback function.
679673
if (!jsvIsFunction(*g_jsCallback)){
680674
return; // we have not got a function pointer: nothing to do
@@ -699,7 +693,7 @@ static void sendWifiCompletionCB(
699693
* Perform a soft initialization of ESP32 networking.
700694
*/
701695
void jswrap_esp32_wifi_soft_init() {
702-
jsDebug("jswrap_esp32_wifi_soft_init()");
696+
jsDebug(DBG_INFO, "jswrap_esp32_wifi_soft_init()");
703697
JsNetwork net;
704698
networkCreate(&net, JSNETWORKTYPE_ESP32); // Set the network type to be ESP32
705699
networkState = NETWORKSTATE_ONLINE; // Set the global state of the networking to be online
@@ -724,7 +718,7 @@ void jswrap_wifi_disconnect(JsVar *jsCallback) {
724718
g_jsDisconnectCallback = jsvLockAgainSafe(jsCallback);
725719

726720
// Call the ESP-IDF to disconnect us from the access point.
727-
jsDebug("Disconnecting.....");
721+
jsDebug(DBG_INFO, "Disconnecting.....");
728722
// turn off auto-connect
729723
esp_wifi_set_auto_connect(false);
730724
s_retry_num = 0; // flag so we don't attempt to reconnect
@@ -758,7 +752,7 @@ void jswrap_wifi_stopAP(JsVar *jsCallback) {
758752
}
759753
err = esp_wifi_set_mode(mode);
760754
if (err != ESP_OK) {
761-
jsDebug("jswrap_wifi_stopAP: esp_wifi_set_mode rc=%d(%s)", err,wifiErrorToString(err));
755+
jsDebug(DBG_INFO, "jswrap_wifi_stopAP: esp_wifi_set_mode rc=%d(%s)", err,wifiErrorToString(err));
762756
}
763757

764758
if (jsvIsFunction(jsCallback)) {
@@ -772,7 +766,7 @@ void jswrap_wifi_connect(
772766
JsVar *jsCallback
773767
) {
774768

775-
jsDebug("jswrap_wifi_connect: entry");
769+
jsDebug(DBG_INFO, "jswrap_wifi_connect: entry");
776770

777771
// Check that the ssid value isn't obviously in error.
778772
if (!jsvIsString(jsSsid)) {
@@ -821,7 +815,7 @@ jsDebug("jswrap_wifi_connect: entry");
821815
}
822816
jsvUnLock(jsPassword);
823817
} // End of we had options
824-
jsDebug("jswrap_wifi_connect: SSID, password, Callback done");
818+
jsDebug(DBG_INFO, "jswrap_wifi_connect: SSID, password, Callback done");
825819

826820
// At this point, we have the ssid in "ssid" and the password in "password".
827821
// Perform an esp_wifi_set_mode
@@ -851,7 +845,7 @@ jsDebug("jswrap_wifi_connect: entry");
851845
jsError( "jswrap_wifi_connect: esp_wifi_set_mode: %d(%s), mode=%d", err,wifiErrorToString(err), mode);
852846
return;
853847
}
854-
jsDebug("jswrap_wifi_connect: esi_wifi_set_mode done");
848+
jsDebug(DBG_INFO, "jswrap_wifi_connect: esi_wifi_set_mode done");
855849

856850
// Perform a an esp_wifi_set_config
857851
wifi_config_t staConfig;
@@ -861,17 +855,17 @@ jsDebug("jswrap_wifi_connect: entry");
861855
memcpy(staConfig.sta.password, password, sizeof(staConfig.sta.password));
862856
staConfig.sta.bssid_set = false;
863857
esp_wifi_set_auto_connect(true);
864-
jsDebug("jswrap_wifi_connect: esp_wifi_set_autoconnect done");
858+
jsDebug(DBG_INFO, "jswrap_wifi_connect: esp_wifi_set_autoconnect done");
865859

866860
err = esp_wifi_set_config(ESP_IF_WIFI_STA, &staConfig);
867861
if (err != ESP_OK) {
868862
jsError( "jswrap_wifi_connect: esp_wifi_set_config: %d(%s)", err,wifiErrorToString(err));
869863
return;
870864
}
871-
jsDebug("jswrap_wifi_connect: esp_wifi_set_config done");
865+
jsDebug(DBG_INFO, "jswrap_wifi_connect: esp_wifi_set_config done");
872866

873867
// Perform an esp_wifi_start
874-
jsDebug("jswrap_wifi_connect: esp_wifi_start %s",ssid);
868+
jsDebug(DBG_INFO, "jswrap_wifi_connect: esp_wifi_start %s",ssid);
875869
err = esp_wifi_start();
876870
if (err != ESP_OK) {
877871
jsError( "jswrap_wifi_connect: esp_wifi_start: %d(%s)", err,wifiErrorToString(err));
@@ -1307,15 +1301,15 @@ JsVar *jswrap_wifi_getAPDetails(JsVar *jsCallback) {
13071301
} // End of jswrap_wifi_getAPDetails
13081302

13091303
void jswrap_wifi_save(JsVar *what) {
1310-
jsDebug("Wifi.save");
1304+
jsDebug(DBG_INFO, "Wifi.save");
13111305
JsVar *o = jsvNewObject();
13121306
if (!o) return;
13131307

13141308
if (jsvIsString(what) && jsvIsStringEqual(what, "clear")) {
13151309
JsVar *name = jsvNewFromString(WIFI_CONFIG_STORAGE_NAME);
13161310
jswrap_storage_erase(name);
13171311
jsvUnLock(name);
1318-
jsDebug("Wifi.save(clear)");
1312+
jsDebug(DBG_INFO, "Wifi.save(clear)");
13191313
return;
13201314
}
13211315

@@ -1353,16 +1347,16 @@ void jswrap_wifi_save(JsVar *what) {
13531347
jswrap_storage_write(name,o,0,0);
13541348
jsvUnLock2(name,o);
13551349

1356-
jsDebug("Wifi.save: write completed");
1350+
jsDebug(DBG_INFO, "Wifi.save: write completed");
13571351
}
13581352

13591353
void jswrap_wifi_restore(void) {
1360-
jsDebug("jswrap_wifi_restore");
1354+
jsDebug(DBG_INFO, "jswrap_wifi_restore");
13611355

13621356
JsVar *name = jsvNewFromString(WIFI_CONFIG_STORAGE_NAME);
13631357
JsVar *o = jswrap_storage_readJSON(name, true);
13641358
if (!o) { // no data
1365-
jsDebug("jswrap_wifi_restore: No data - Starting default AP");
1359+
jsDebug(DBG_INFO, "jswrap_wifi_restore: No data - Starting default AP");
13661360
esp_wifi_start();
13671361
jsvUnLock2(name,o);
13681362
return;
@@ -1458,7 +1452,7 @@ void jswrap_wifi_restore(void) {
14581452
return;
14591453
}
14601454
} else {
1461-
jsDebug( "Wifi: Both STA AND APSTA are off");
1455+
jsDebug(DBG_INFO, "Wifi: Both STA AND APSTA are off");
14621456
}
14631457
} // End of jswrap_wifi_restore
14641458

@@ -1533,7 +1527,7 @@ void jswrap_wifi_setHostname(
15331527
) {
15341528
char hostname[256];
15351529
jsvGetString(jsHostname, hostname, sizeof(hostname));
1536-
jsDebug("Wifi.setHostname: %s\n", hostname);
1530+
jsDebug(DBG_INFO, "Wifi.setHostname: %s\n", hostname);
15371531
tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA,hostname);
15381532

15391533
// now update mDNS

make/family/ESP32.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,4 @@ LDFLAGS+= -L$(ESP_APP_TEMPLATE_PATH)/build/components/bt/bluedroid/api \
148148
-L$(ESP_APP_TEMPLATE_PATH)/build/components/bt/bluedroid/bta
149149
endif
150150

151+
FLASH_BAUD ?= 921600 # The flash baud rate

make/targets/ESP32.make

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ flash: $(PROJ_NAME).bin
3131
python $(ESP_IDF_PATH)/components/esptool_py/esptool/esptool.py \
3232
--chip esp32 \
3333
--port ${COMPORT} \
34-
--baud 921600 \
34+
--baud $(FLASH_BAUD) \
3535
write_flash \
3636
-z \
3737
--flash_mode "dio" \
@@ -43,6 +43,6 @@ flash: $(PROJ_NAME).bin
4343
erase_flash:
4444
python $(ESP_IDF_PATH)/components/esptool_py/esptool/esptool.py \
4545
--chip esp32 \
46-
--port ${COMPORT}\
47-
--baud 921600 \
46+
--port ${COMPORT} \
47+
--baud $(FLASH_BAUD) \
4848
erase_flash

src/jsutils.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,22 @@ typedef enum {
400400

401401
void jsAssertFail(const char *file, int line, const char *expr);
402402

403+
#define DBG_INFO 0
404+
#define DBG_VERBOSE 1
405+
406+
/*
407+
#if defined(DEBUG) || __FILE__ == DEBUG_FILE
408+
#define jsDebug(dbg_type, format, ...) jsiConsolePrintf("[" __FILE__ "]:" format, ## __VA_ARGS__)
409+
#else
410+
#define jsDebug(dbg_type, format, ...) do { } while(0)
411+
#endif
412+
*/
413+
#if (defined DEBUG ) || ( defined __FILE__ == DEBUG_FILE)
414+
#define jsDebug(dbg_type, format, ...) jsiConsolePrintf("[" __FILE__ "]:" format, ## __VA_ARGS__)
415+
#else
416+
#define jsDebug(dbg_type, format, ...) do { } while(0)
417+
#endif
418+
403419
#ifndef USE_FLASH_MEMORY
404420
// Normal functions thet place format string in ram
405421
void jsExceptionHere(JsExceptionType type, const char *fmt, ...);

targets/esp32/bluetooth.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ void jsble_init(){
4141
esp_err_t ret;
4242
if(ESP32_Get_NVS_Status(ESP_NETWORK_BLE)){
4343
ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
44-
if(ret) {jsWarn("mem release failed:%x\n",ret); return;}
44+
if(ret) {
45+
jsExceptionHere(JSET_ERROR,"mem release failed:%x\n",ret);
46+
return;
47+
}
4548

4649
if(initController()) return;
4750
if(initBluedroid()) return;
@@ -51,7 +54,7 @@ void jsble_init(){
5154
}
5255
else{
5356
ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
54-
jsWarn("Bluetooth is disabled per ESP32.enableBLE(false)\n");
57+
jsWarn("Bluetooth is disabled per ESP32.enableBLE(false)\n");
5558
}
5659
}
5760
/** Completely deinitialise the BLE stack */
@@ -96,7 +99,9 @@ uint32_t jsble_advertising_start(){
9699
void jsble_advertising_stop(){
97100
esp_err_t status;
98101
status = bluetooth_gap_startAdvertizing(false);
99-
if(status) jsWarn("error in stop advertising:0X%x\n",status);
102+
if(status){
103+
jsExceptionHere(JSET_ERROR,"error in stop advertising:0X%x",status);
104+
}
100105
}
101106
/** Is BLE connected to any device at all? */
102107
bool jsble_has_connection(){
@@ -130,7 +135,9 @@ bool jsble_check_error(uint32_t err_code){
130135
}
131136
/// Scanning for advertisign packets
132137
uint32_t jsble_set_scanning(bool enabled, bool activeScan){
133-
if (activeScan) jsWarn("active scan not implemented\n");
138+
if (activeScan) {
139+
jsWarn("active scan not implemented\n");
140+
}
134141
bluetooth_gap_setScan(enabled);
135142
return 0;
136143
}

targets/esp32/jshardware.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,6 @@ void jshInit() {
154154
if (JSHPINSTATE_I2C != 13 || JSHPINSTATE_GPIO_IN_PULLDOWN != 6 || JSHPINSTATE_MASK != 15) {
155155
jsError("JshPinState #defines have changed, please update pinStateToString()");
156156
}
157-
/*
158-
jsWarn( "JSHPINSTATE_I2C %d\n",JSHPINSTATE_I2C );
159-
jsWarn( "JSHPINSTATE_GPIO_IN_PULLDOWN %d\n",JSHPINSTATE_GPIO_IN_PULLDOWN );
160-
jsWarn( "JSHPINSTATE_MASK %d\n",JSHPINSTATE_MASK );
161-
*/
162157
gpio_isr_register(gpio_intr_handler,NULL,0,NULL); //changed to automatic assign of interrupt
163158
// Initialize something for each of the possible pins.
164159
jshPinDefaultPullup();

0 commit comments

Comments
 (0)