Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 improvements #1880

Merged
merged 2 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions libs/filesystem/fat_sd/flash_diskio.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ DSTATUS disk_initialize (
{
if (drv != 0)
return STA_NODISK;
//jsDebug("Flash Disk Init %d",drv);
return RES_OK;
}

Expand Down Expand Up @@ -76,7 +75,7 @@ DRESULT disk_read (
uint16_t size = (uint16_t)(count * FS_SECTOR_SIZE);
uint32_t addr = sector * FS_SECTOR_SIZE + fs_flash_base;

jsDebug("Flash disk_read sector: %d, buff: mem: %d buff: %d len: %d", sector, addr, buff, size);
jsDebug(DBG_INFO,"Flash disk_read sector: %d, buff: mem: %d buff: %d len: %d", sector, addr, buff, size);
jshFlashRead( buff, addr, size);
return RES_OK;
}
Expand All @@ -98,7 +97,7 @@ DRESULT disk_write (
uint16_t size = (uint16_t)(count * FS_SECTOR_SIZE);
uint32_t addr = sector * FS_SECTOR_SIZE + fs_flash_base;

jsDebug("Flash disk_write sector: %d, buff: mem: %d buff: %d len: %d", sector, addr, buff, size);
jsDebug(DBG_INFO,"Flash disk_write sector: %d, buff: mem: %d buff: %d len: %d", sector, addr, buff, size);
jshFlashErasePage(addr);
jshFlashWrite( (void *)buff, addr,size);

Expand All @@ -123,7 +122,7 @@ DRESULT disk_ioctl (
switch (ctrl) {
case CTRL_SYNC : /// Make sure that no pending write process
res = RES_OK;
jsDebug("Flash disk_ioctl CTRL_SYNC");
jsDebug(DBG_INFO,"Flash disk_ioctl CTRL_SYNC");
break;

case GET_SECTOR_COUNT : // Get number of sectors on the disk (DWORD)
Expand Down
10 changes: 0 additions & 10 deletions libs/filesystem/fat_sd/flash_diskio.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,3 @@
#define FS_FLASH_BASE 0x300000;

uint8_t flashFatFsInit( uint32_t addr, uint16_t sectors );

/*
#ifndef RELEASE
#define jsDebug(fmt,...) jsWarn(fmt,##__VA_ARGS__)
#else
#define jsDebug(fmt,...)
#endif
#endif
*/
#define jsDebug(fmt,...)
2 changes: 1 addition & 1 deletion libs/filesystem/jswrap_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ int jswrap_E_flashFatFS(JsVar* options) {
if (init) {
if ( format ) {
uint8_t res = f_mount(&jsfsFAT, "", 0);
jsDebug("Formatting Flash");
jsDebug(DBG_INFO,"Formatting Flash");
res = f_mkfs("", 1, 0); // Super Floppy format, using all space (not partition table)
if (res != FR_OK) {
jsExceptionHere(JSET_INTERNALERROR, "Flash Formatting error:",res);
Expand Down
56 changes: 25 additions & 31 deletions libs/network/esp32/jswrap_esp32_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@

#define UNUSED(x) (void)(x)

#ifndef RELEASE
#define jsDebug(format, ...) jsWarn(format, ## __VA_ARGS__)
#else
#define jsDebug(format, ...) do { } while(0)
#endif

static void sendWifiCompletionCB(
JsVar **g_jsCallback, //!< Pointer to the global callback variable
char *reason //!< NULL if successful, error string otherwise
Expand Down Expand Up @@ -233,7 +227,7 @@ static char *wifiReasonToString(uint8_t reason) {
case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
return "REASON_UNSUPP_RSN_IE_VERSION";
}
jsDebug( "wifiReasonToString: Unknown reason %d", reason);
jsDebug(DBG_INFO, "wifiReasonToString: Unknown reason %d", reason);
return "Unknown reason";
} // End of wifiReasonToString

Expand Down Expand Up @@ -413,7 +407,7 @@ static JsVar *getWifiModule() {
static int s_retry_num = 0;

static char *wifiGetEvent(uint32_t event) {
jsDebug( "wifiGetEvent: Got event: %d", event);
jsDebug(DBG_INFO,"wifiGetEvent: Got event: %d", event);
switch(event) {
case SYSTEM_EVENT_AP_PROBEREQRECVED:
return "#onprobe_recv";
Expand Down Expand Up @@ -442,7 +436,7 @@ switch(event) {
case SYSTEM_EVENT_WIFI_READY:
break;
}
jsDebug( "Unhandled wifi event type: %d", event);
jsDebug(DBG_INFO, "Unhandled wifi event type: %d", event);
return NULL;
} // End of wifiGetEvent

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

if (event->event_id == SYSTEM_EVENT_STA_DISCONNECTED) {
if (--s_retry_num > 0 ) {
esp_wifi_connect();
jsDebug("retry to AP connect");
jsDebug(DBG_INFO,"retry to AP connect");
return;
}
g_isStaConnected = false; // Flag as disconnected
Expand Down Expand Up @@ -578,7 +572,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
jsvObjectSetChildAndUnLock(jsDetails, "netmask", jsvNewFromString(temp));
sprintf(temp, IPSTR, IP2STR(&event->event_info.got_ip.ip_info.gw));
jsvObjectSetChildAndUnLock(jsDetails, "gw", jsvNewFromString(temp));
jsDebug("Wifi: About to emit connect!");
jsDebug(DBG_INFO, "Wifi: About to emit connect!");
sendWifiEvent(event->event_id, jsDetails);
// start mDNS
const char * hostname;
Expand Down Expand Up @@ -642,7 +636,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
sendWifiCompletionCB(&g_jsAPStartedCallback, NULL);
return ESP_OK;
}
jsDebug("Wifi: event_handler -> NOT HANDLED EVENT: %d", event->event_id );
jsDebug(DBG_INFO, "Wifi: event_handler -> NOT HANDLED EVENT: %d", event->event_id );
return ESP_OK;
} // End of event_handler

Expand All @@ -658,7 +652,7 @@ void esp32_wifi_init() {
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));

jsDebug("esp32_wifi_init complete");
jsDebug(DBG_INFO, "esp32_wifi_init complete");

} // End of esp32_wifi_init

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

// Call the ESP-IDF to disconnect us from the access point.
jsDebug("Disconnecting.....");
jsDebug(DBG_INFO, "Disconnecting.....");
// turn off auto-connect
esp_wifi_set_auto_connect(false);
s_retry_num = 0; // flag so we don't attempt to reconnect
Expand Down Expand Up @@ -758,7 +752,7 @@ void jswrap_wifi_stopAP(JsVar *jsCallback) {
}
err = esp_wifi_set_mode(mode);
if (err != ESP_OK) {
jsDebug("jswrap_wifi_stopAP: esp_wifi_set_mode rc=%d(%s)", err,wifiErrorToString(err));
jsDebug(DBG_INFO, "jswrap_wifi_stopAP: esp_wifi_set_mode rc=%d(%s)", err,wifiErrorToString(err));
}

if (jsvIsFunction(jsCallback)) {
Expand All @@ -772,7 +766,7 @@ void jswrap_wifi_connect(
JsVar *jsCallback
) {

jsDebug("jswrap_wifi_connect: entry");
jsDebug(DBG_INFO, "jswrap_wifi_connect: entry");

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

// At this point, we have the ssid in "ssid" and the password in "password".
// Perform an esp_wifi_set_mode
Expand Down Expand Up @@ -851,7 +845,7 @@ jsDebug("jswrap_wifi_connect: entry");
jsError( "jswrap_wifi_connect: esp_wifi_set_mode: %d(%s), mode=%d", err,wifiErrorToString(err), mode);
return;
}
jsDebug("jswrap_wifi_connect: esi_wifi_set_mode done");
jsDebug(DBG_INFO, "jswrap_wifi_connect: esi_wifi_set_mode done");

// Perform a an esp_wifi_set_config
wifi_config_t staConfig;
Expand All @@ -861,17 +855,17 @@ jsDebug("jswrap_wifi_connect: entry");
memcpy(staConfig.sta.password, password, sizeof(staConfig.sta.password));
staConfig.sta.bssid_set = false;
esp_wifi_set_auto_connect(true);
jsDebug("jswrap_wifi_connect: esp_wifi_set_autoconnect done");
jsDebug(DBG_INFO, "jswrap_wifi_connect: esp_wifi_set_autoconnect done");

err = esp_wifi_set_config(ESP_IF_WIFI_STA, &staConfig);
if (err != ESP_OK) {
jsError( "jswrap_wifi_connect: esp_wifi_set_config: %d(%s)", err,wifiErrorToString(err));
return;
}
jsDebug("jswrap_wifi_connect: esp_wifi_set_config done");
jsDebug(DBG_INFO, "jswrap_wifi_connect: esp_wifi_set_config done");

// Perform an esp_wifi_start
jsDebug("jswrap_wifi_connect: esp_wifi_start %s",ssid);
jsDebug(DBG_INFO, "jswrap_wifi_connect: esp_wifi_start %s",ssid);
err = esp_wifi_start();
if (err != ESP_OK) {
jsError( "jswrap_wifi_connect: esp_wifi_start: %d(%s)", err,wifiErrorToString(err));
Expand Down Expand Up @@ -1307,15 +1301,15 @@ JsVar *jswrap_wifi_getAPDetails(JsVar *jsCallback) {
} // End of jswrap_wifi_getAPDetails

void jswrap_wifi_save(JsVar *what) {
jsDebug("Wifi.save");
jsDebug(DBG_INFO, "Wifi.save");
JsVar *o = jsvNewObject();
if (!o) return;

if (jsvIsString(what) && jsvIsStringEqual(what, "clear")) {
JsVar *name = jsvNewFromString(WIFI_CONFIG_STORAGE_NAME);
jswrap_storage_erase(name);
jsvUnLock(name);
jsDebug("Wifi.save(clear)");
jsDebug(DBG_INFO, "Wifi.save(clear)");
return;
}

Expand Down Expand Up @@ -1353,16 +1347,16 @@ void jswrap_wifi_save(JsVar *what) {
jswrap_storage_write(name,o,0,0);
jsvUnLock2(name,o);

jsDebug("Wifi.save: write completed");
jsDebug(DBG_INFO, "Wifi.save: write completed");
}

void jswrap_wifi_restore(void) {
jsDebug("jswrap_wifi_restore");
jsDebug(DBG_INFO, "jswrap_wifi_restore");

JsVar *name = jsvNewFromString(WIFI_CONFIG_STORAGE_NAME);
JsVar *o = jswrap_storage_readJSON(name, true);
if (!o) { // no data
jsDebug("jswrap_wifi_restore: No data - Starting default AP");
jsDebug(DBG_INFO, "jswrap_wifi_restore: No data - Starting default AP");
esp_wifi_start();
jsvUnLock2(name,o);
return;
Expand Down Expand Up @@ -1458,7 +1452,7 @@ void jswrap_wifi_restore(void) {
return;
}
} else {
jsDebug( "Wifi: Both STA AND APSTA are off");
jsDebug(DBG_INFO, "Wifi: Both STA AND APSTA are off");
}
} // End of jswrap_wifi_restore

Expand Down Expand Up @@ -1533,7 +1527,7 @@ void jswrap_wifi_setHostname(
) {
char hostname[256];
jsvGetString(jsHostname, hostname, sizeof(hostname));
jsDebug("Wifi.setHostname: %s\n", hostname);
jsDebug(DBG_INFO, "Wifi.setHostname: %s\n", hostname);
tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA,hostname);

// now update mDNS
Expand Down
1 change: 1 addition & 0 deletions make/family/ESP32.make
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,4 @@ LDFLAGS+= -L$(ESP_APP_TEMPLATE_PATH)/build/components/bt/bluedroid/api \
-L$(ESP_APP_TEMPLATE_PATH)/build/components/bt/bluedroid/bta
endif

FLASH_BAUD ?= 921600 # The flash baud rate
6 changes: 3 additions & 3 deletions make/targets/ESP32.make
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ flash: $(PROJ_NAME).bin
python $(ESP_IDF_PATH)/components/esptool_py/esptool/esptool.py \
--chip esp32 \
--port ${COMPORT} \
--baud 921600 \
--baud $(FLASH_BAUD) \
write_flash \
-z \
--flash_mode "dio" \
Expand All @@ -43,6 +43,6 @@ flash: $(PROJ_NAME).bin
erase_flash:
python $(ESP_IDF_PATH)/components/esptool_py/esptool/esptool.py \
--chip esp32 \
--port ${COMPORT}\
--baud 921600 \
--port ${COMPORT} \
--baud $(FLASH_BAUD) \
erase_flash
16 changes: 16 additions & 0 deletions src/jsutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,22 @@ typedef enum {

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

#define DBG_INFO 0
#define DBG_VERBOSE 1

/*
#if defined(DEBUG) || __FILE__ == DEBUG_FILE
#define jsDebug(dbg_type, format, ...) jsiConsolePrintf("[" __FILE__ "]:" format, ## __VA_ARGS__)
#else
#define jsDebug(dbg_type, format, ...) do { } while(0)
#endif
*/
#if (defined DEBUG ) || ( defined __FILE__ == DEBUG_FILE)
#define jsDebug(dbg_type, format, ...) jsiConsolePrintf("[" __FILE__ "]:" format, ## __VA_ARGS__)
#else
#define jsDebug(dbg_type, format, ...) do { } while(0)
#endif

#ifndef USE_FLASH_MEMORY
// Normal functions thet place format string in ram
void jsExceptionHere(JsExceptionType type, const char *fmt, ...);
Expand Down
15 changes: 11 additions & 4 deletions targets/esp32/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ void jsble_init(){
esp_err_t ret;
if(ESP32_Get_NVS_Status(ESP_NETWORK_BLE)){
ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
if(ret) {jsWarn("mem release failed:%x\n",ret); return;}
if(ret) {
jsExceptionHere(JSET_ERROR,"mem release failed:%x\n",ret);
return;
}

if(initController()) return;
if(initBluedroid()) return;
Expand All @@ -51,7 +54,7 @@ void jsble_init(){
}
else{
ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
jsWarn("Bluetooth is disabled per ESP32.enableBLE(false)\n");
jsWarn("Bluetooth is disabled per ESP32.enableBLE(false)\n");
}
}
/** Completely deinitialise the BLE stack */
Expand Down Expand Up @@ -96,7 +99,9 @@ uint32_t jsble_advertising_start(){
void jsble_advertising_stop(){
esp_err_t status;
status = bluetooth_gap_startAdvertizing(false);
if(status) jsWarn("error in stop advertising:0X%x\n",status);
if(status){
jsExceptionHere(JSET_ERROR,"error in stop advertising:0X%x",status);
}
}
/** Is BLE connected to any device at all? */
bool jsble_has_connection(){
Expand Down Expand Up @@ -130,7 +135,9 @@ bool jsble_check_error(uint32_t err_code){
}
/// Scanning for advertisign packets
uint32_t jsble_set_scanning(bool enabled, bool activeScan){
if (activeScan) jsWarn("active scan not implemented\n");
if (activeScan) {
jsWarn("active scan not implemented\n");
}
bluetooth_gap_setScan(enabled);
return 0;
}
Expand Down
5 changes: 0 additions & 5 deletions targets/esp32/jshardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ void jshInit() {
if (JSHPINSTATE_I2C != 13 || JSHPINSTATE_GPIO_IN_PULLDOWN != 6 || JSHPINSTATE_MASK != 15) {
jsError("JshPinState #defines have changed, please update pinStateToString()");
}
/*
jsWarn( "JSHPINSTATE_I2C %d\n",JSHPINSTATE_I2C );
jsWarn( "JSHPINSTATE_GPIO_IN_PULLDOWN %d\n",JSHPINSTATE_GPIO_IN_PULLDOWN );
jsWarn( "JSHPINSTATE_MASK %d\n",JSHPINSTATE_MASK );
*/
gpio_isr_register(gpio_intr_handler,NULL,0,NULL); //changed to automatic assign of interrupt
// Initialize something for each of the possible pins.
jshPinDefaultPullup();
Expand Down
Loading