- added variables_mode in config.
build_platform_config.py
- added define for variables_mode
jsvar.h
- added jsVarsSize;  //size of jsVars for option malloc
- added support of malloc jsVars in jsvInit
jsvar.c
- added definition jsVars for variables_mode_malloc
jshardwareESP32.h
- new file with functions to set/get enable status of BLE and Wifi
jshardwareESP32.c
- new file with functions to set/get enable status of BLE and Wifi
ESP32.make
- added jshardware.c to SOURCES
main.c
- added handling for Wifi enabled
- added calculation for jsVars
- initialise jsVars based on calculation
jshardware.c
- used new API call to get Serial Number, even if Wifi is not enabled
- initialises BLE, if BLE is enabled (in NVS storage)
bluetooth.c
- check if BLE is enabled before initialising
jswrap_esp32.h
- defines functions to enable BLE and/or Wifi
jswrap_esp32.c
- wrapper for functions to enable BLE/Wifi, remove code from Flash, to avoid overflow in jsVars
- added status for BLE/Wifi to ESP32.getStatus
esp32_gap_func.c
- get mac adress even if Wifi is not enabled

 Changes to be committed:
	modified:   boards/ESP32.py
	modified:   make/family/ESP32.make
	modified:   scripts/build_platform_config.py
	modified:   src/jsvar.c
	modified:   src/jsvar.h
	modified:   targets/esp32/BLE/esp32_gap_func.c
	modified:   targets/esp32/bluetooth.c
	modified:   targets/esp32/jshardware.c
	new file:   targets/esp32/jshardwareESP32.c
	new file:   targets/esp32/jshardwareESP32.h
	modified:   targets/esp32/jswrap_esp32.c
	modified:   targets/esp32/jswrap_esp32.h
	modified:   targets/esp32/main.c
This commit is contained in:
jumjum123 2018-06-01 12:23:00 +02:00
parent a77c9c6082
commit 0054b39c00
13 changed files with 189 additions and 26 deletions

View File

@ -20,6 +20,7 @@ info = {
'default_console' : "EV_SERIAL1",
'default_console_baudrate' : "115200",
'variables' : 2500,
'variables_mode' : "malloc",
'binary_name' : 'espruino_%v_esp32.bin',
'build' : {
'optimizeflags' : '-Og',

View File

@ -130,7 +130,8 @@ SOURCES+= targets/esp32/bluetooth.c \
targets/esp32/BLE/esp32_bluetooth_utils.c \
targets/esp32/BLE/esp32_gap_func.c \
targets/esp32/BLE/esp32_gatts_func.c \
targets/esp32/BLE/esp32_gattc_func.c
targets/esp32/BLE/esp32_gattc_func.c \
targets/esp32/jshardwareESP32.c
INCLUDE+= -I$(ESP_IDF_PATH)/components/bt/bluedroid/include \
-I$(ESP_IDF_PATH)/components/bt/bluedroid/api/include \
-I$(ESP_IDF_PATH)/components/bt/bluedroid/bta/include \

View File

@ -223,6 +223,10 @@ elif board.chip["family"]=="SAMD":
else:
die('Unknown chip family '+board.chip["family"])
if board.info["variables_mode"]:
codeOut('#define VARIABLES_MODE_'+board.info["variables_mode"].upper() + ' // mode for allocating memory, standard is statically assigned');
codeOut('');
codeOut("#define LINKER_END_VAR "+linker_end_var);
codeOut("#define LINKER_ETEXT_VAR "+linker_etext_var);

View File

@ -42,9 +42,14 @@ unsigned int jsVarsSize = 0;
#define JSVAR_BLOCK_SIZE 4096
#define JSVAR_BLOCK_SHIFT 12
#else
#ifdef VARIABLES_MODE_MALLOC
unsigned int jsVarsSize = 0;
JsVar *jsVars = NULL;
#else
JsVar jsVars[JSVAR_CACHE_SIZE];
unsigned int jsVarsSize = JSVAR_CACHE_SIZE;
#endif
#endif
typedef enum {
MEM_NOT_BUSY,
@ -242,6 +247,10 @@ void jsvInit() {
jsVarsSize = JSVAR_BLOCK_SIZE;
jsVarBlocks = malloc(sizeof(JsVar*)); // just 1
jsVarBlocks[0] = malloc(sizeof(JsVar) * JSVAR_BLOCK_SIZE);
#else
#ifdef VARIABLES_MODE_MALLOC
if(!jsVars) jsVars = (JsVar *)malloc(sizeof(JsVar) * jsVarsSize);
#endif
#endif
jsVarFirstEmpty = jsvInitJsVars(1/*first*/, jsVarsSize);

View File

@ -183,6 +183,7 @@ typedef struct {
#endif
} PACKED_FLAGS JsVarDataRef;
extern unsigned int jsVarsSize; //size of jsVars for option malloc
/// Union that contains all the different types of data
typedef union {

View File

@ -268,7 +268,7 @@ void bluetooth_initDeviceName(){
char deviceName[14];
strcpy(deviceName,"ESP32.js 0123");
uint8_t macnr[6];
esp_wifi_get_mac(WIFI_IF_STA, macnr);
esp_efuse_mac_get_default(macnr);
deviceName[9] = itoch((macnr[4]>>4)&15);
deviceName[10] = itoch(macnr[4]&15);
deviceName[11] = itoch((macnr[5]>>4)&15);

View File

@ -27,6 +27,7 @@
#include "BLE/esp32_gatts_func.h"
#include "BLE/esp32_gattc_func.h"
#include "BLE/esp32_bluetooth_utils.h"
#include "jshardwareESP32.h"
#define UNUSED(x) (void)(x)
@ -37,14 +38,20 @@ volatile uint16_t m_central_conn_handle; /**< Handle of central mode connection
/** Initialise the BLE stack */
void jsble_init(){
esp_err_t ret;
ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
if(ret) {jsWarn("mem release failed:%x\n",ret); return;}
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(initController()) return;
if(initBluedroid()) return;
if(registerCallbacks()) return;
setMtu();
if(initController()) return;
if(initBluedroid()) return;
if(registerCallbacks()) return;
setMtu();
}
else{
ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
jsWarn("Bluetooth is disabled per ESP32.enableBLE(false)\n");
}
}
/** Completely deinitialise the BLE stack */
void jsble_kill(){
@ -80,7 +87,6 @@ void jsble_restart_softdevice(){
}
void jsble_advertising_start(){
//jsWarn("advertising start\n");
esp_err_t status;
if (bleStatus & BLE_IS_ADVERTISING) return;
status = bluetooth_gap_startAdvertizing(true);

View File

@ -37,6 +37,7 @@
#include "BLE/esp32_gattc_func.h"
#include "BLE/esp32_gatts_func.h"
#endif
#include "jshardwareESP32.h"
#include "jsutils.h"
#include "jstimer.h"
@ -82,6 +83,17 @@ static uint8_t g_pinState[JSH_PIN_COUNT];
// Whether a pin is being used for soft PWM or not
BITFIELD_DECL(jshPinSoftPWM, JSH_PIN_COUNT);
static uint64_t DEVICE_INITIALISED_FLAGS = 0L;
void jshSetDeviceInitialised(IOEventFlags device, bool isInit) {
uint64_t mask = 1ULL << (int)device;
if (isInit) {
DEVICE_INITIALISED_FLAGS |= mask;
} else {
DEVICE_INITIALISED_FLAGS &= ~mask;
}
}
/**
* interrupt handler for gpio interrupts
*/
@ -119,8 +131,10 @@ void jshPinSetStateRange( Pin start, Pin end, JshPinState state ) {
void jshPinDefaultPullup() {
// 6-11 are used by Flash chip
// 32-33 are routed to rtc for xtal
// 16-17 are used for PSRAM (future use)
jshPinSetStateRange(0,0,JSHPINSTATE_GPIO_IN_PULLUP);
jshPinSetStateRange(12,19,JSHPINSTATE_GPIO_IN_PULLUP);
jshPinSetStateRange(12,15,JSHPINSTATE_GPIO_IN_PULLUP);
jshPinSetStateRange(18,19,JSHPINSTATE_GPIO_IN_PULLUP);
jshPinSetStateRange(21,22,JSHPINSTATE_GPIO_IN_PULLUP);
jshPinSetStateRange(25,27,JSHPINSTATE_GPIO_IN_PULLUP);
jshPinSetStateRange(34,39,JSHPINSTATE_GPIO_IN_PULLUP);
@ -131,9 +145,9 @@ void jshPinDefaultPullup() {
* Initialize the JavaScript hardware interface.
*/
void jshInit() {
esp32_wifi_init();
if(ESP32_Get_NVS_Status(ESP_NETWORK_WIFI)) esp32_wifi_init();
#ifdef BLUETOOTH
gattc_init();
if(ESP32_Get_NVS_Status(ESP_NETWORK_BLE)) gattc_init();
#endif
jshInitDevices();
BITFIELD_CLEAR(jshPinSoftPWM);
@ -162,7 +176,7 @@ void jshReset() {
SPIReset();
I2CReset();
#ifdef BLUETOOTH
gatts_reset(false);
if(ESP32_Get_NVS_Status(ESP_NETWORK_BLE)) gatts_reset(false);
#endif
}
@ -170,7 +184,7 @@ void jshReset() {
* Re-init the ESP32 after a soft-reset
*/
void jshSoftInit() {
jswrap_esp32_wifi_soft_init();
if(ESP32_Get_NVS_Status(ESP_NETWORK_WIFI)) jswrap_esp32_wifi_soft_init();
}
/**
@ -185,7 +199,7 @@ void jshIdle() {
// ESP32 chips don't have a serial number but they do have a MAC address
int jshGetSerialNumber(unsigned char *data, int maxChars) {
assert(maxChars >= 6); // it's 32
esp_wifi_get_mac(WIFI_IF_STA, data);
esp_efuse_mac_get_default(data);
return 6;
}
@ -617,7 +631,6 @@ void jshUtilTimerReschedule(JsSysTime period) {
//===== Miscellaneous =====
static uint64_t DEVICE_INITIALISED_FLAGS = 0L;
bool jshIsDeviceInitialised(IOEventFlags device) {
uint64_t mask = 1ULL << (int)device;
return (DEVICE_INITIALISED_FLAGS & mask) != 0L;
@ -627,14 +640,6 @@ bool jshIsDeviceInitialised(IOEventFlags device) {
// return 0;
} // End of jshIsDeviceInitialised
void jshSetDeviceInitialised(IOEventFlags device, bool isInit) {
uint64_t mask = 1ULL << (int)device;
if (isInit) {
DEVICE_INITIALISED_FLAGS |= mask;
} else {
DEVICE_INITIALISED_FLAGS &= ~mask;
}
}
// the esp32 temperature sensor - undocumented library function call. Unsure of values returned.
JsVarFloat jshReadTemperature() {

View File

@ -0,0 +1,49 @@
/*
* This file is designed to support Analog functions in Espruino,
* a JavaScript interpreter for Microcontrollers designed by Gordon Williams
*
* Copyright (C) 2016 by Juergen Marsch
*
* This Source Code Form is subject to the terms of the Mozilla Publici
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* Contains ESP32 board specific functions for networking (wifi, ble).
* ----------------------------------------------------------------------------
*/
#include "jshardwareESP32.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "nvs.h"
static char *ESP32_hardwareName(esp_hardware_esp32_t hardware){
switch(hardware){
case ESP_NETWORK_BLE: return "bleStatus";
case ESP_NETWORK_WIFI: return "wifiStatus";
}
return "unknownHardware";
}
bool ESP32_Get_NVS_Status(esp_hardware_esp32_t hardware){
esp_err_t err;nvs_handle hardwareHandle; uint32_t status;
nvs_open("nvs",NVS_READWRITE,&hardwareHandle);
err = nvs_get_u32(hardwareHandle,ESP32_hardwareName(hardware),&status);
if(err) {
status = ESP32HARDWAREDEFAULT;
nvs_set_u32(hardwareHandle,ESP32_hardwareName(hardware),ESP32HARDWAREDEFAULT);
}
nvs_close(hardwareHandle);
return (bool) status;
}
void ESP32_Set_NVS_Status(esp_hardware_esp32_t hardware, bool enable){
nvs_handle hardwareHandle; uint32_t status;
if(enable) status = 1; else status = 0;
nvs_open("nvs",NVS_READWRITE,&hardwareHandle);
nvs_set_u32(hardwareHandle,ESP32_hardwareName(hardware),status);
nvs_close(hardwareHandle);
}

View File

@ -0,0 +1,34 @@
/*
* This file is designed to support Analog functions in Espruino for ESP32,
* a JavaScript interpreter for Microcontrollers designed by Gordon Williams
*
* Copyright (C) 2016 by Juergen Marsch
*
* This Source Code Form is subject to the terms of the Mozilla Publici
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* Contains ESP32 board specific functions for networking (wifi, ble).
* ----------------------------------------------------------------------------
*/
#ifndef TARGETS_ES32_JSHARDWARE_ESP32_H_
#define TARGETS_ES32_JSHARDWARE_ESP32_H_
#include <stdio.h>
#include "jsvar.h"
typedef enum{
ESP_NETWORK_BLE = 1,
ESP_NETWORK_WIFI = 2
} esp_hardware_esp32_t;
#define ESP32HARDWAREDEFAULT 1
bool ESP32_Get_NVS_Status(esp_hardware_esp32_t hardware);
void ESP32_Set_NVS_Status(esp_hardware_esp32_t hardware, bool enable);
#endif /* TARGETS_ES32_JSHARDWARE_ESP32_H_ */

View File

@ -18,6 +18,7 @@
#include "jsutils.h"
#include "jsinteractive.h"
#include "jsparse.h"
#include "jsflash.h"
#include "esp_system.h"
#include "esp_sleep.h"
@ -25,6 +26,7 @@
#ifdef BLUETOOTH
#include "BLE/esp32_bluetooth_utils.h"
#endif
#include "jshardwareESP32.h"
#include "jsutils.h"
#include "jsinteractive.h"
@ -84,6 +86,8 @@ Returns an object that contains details about the state of the ESP32 with the fo
* `sdkVersion` - Version of the SDK.
* `freeHeap` - Amount of free heap in bytes.
* `BLE` - Status of BLE, enabled if true.
* `Wifi` - Status of Wifi, enabled if true.
*/
JsVar *jswrap_ESP32_getState() {
@ -92,6 +96,8 @@ JsVar *jswrap_ESP32_getState() {
JsVar *esp32State = jsvNewObject();
jsvObjectSetChildAndUnLock(esp32State, "sdkVersion", jsvNewFromString(esp_get_idf_version()));
jsvObjectSetChildAndUnLock(esp32State, "freeHeap", jsvNewFromInteger(esp_get_free_heap_size()));
jsvObjectSetChildAndUnLock(esp32State, "BLE", jsvNewFromBool(ESP32_Get_NVS_Status(ESP_NETWORK_BLE)));
jsvObjectSetChildAndUnLock(esp32State, "Wifi", jsvNewFromBool(ESP32_Get_NVS_Status(ESP_NETWORK_WIFI)));
return esp32State;
} // End of jswrap_ESP32_getState
@ -110,4 +116,40 @@ JsVar *jswrap_ESP32_getState() {
void jswrap_ESP32_setBLE_Debug(int level){
ESP32_setBLE_Debug(level);
}
/*JSON{
"type" : "staticmethod",
"class" : "ESP32",
"name" : "enableBLE",
"generate" : "jswrap_ESP32_enableBLE",
"params" : [
["enable", "bool", "switches Bluetooth on or off" ]
],
"ifdef" : "BLUETOOTH"
}
Switches Bluetooth off/on, removes saved code from Flash, resets the board,
and on restart creates jsVars depending on available heap (actual additional 1800)
*/
void jswrap_ESP32_enableBLE(bool enable){ //may be later, we will support BLEenable(ALL/SERVER/CLIENT)
ESP32_Set_NVS_Status(ESP_NETWORK_BLE,enable);
jsfRemoveCodeFromFlash();
esp_restart();
}
#endif
/*JSON{
"type" : "staticmethod",
"class" : "ESP32",
"name" : "enableWifi",
"generate" : "jswrap_ESP32_enableWifi",
"params" : [
["enable", "bool", "switches Wifi on or off" ]
]
}
Switches Wifi off/on, removes saved code from Flash, resets the board,
and on restart creates jsVars depending on available heap (actual additional 3900)
*/
void jswrap_ESP32_enableWifi(bool enable){ //may be later, we will support BLEenable(ALL/SERVER/CLIENT)
ESP32_Set_NVS_Status(ESP_NETWORK_WIFI,enable);
jsfRemoveCodeFromFlash();
esp_restart();
}

View File

@ -27,5 +27,7 @@ void jswrap_ESP32_setAtten(Pin pin,int atten);
#ifdef BLUETOOTH
void jswrap_ESP32_setBLE_Debug(int level);
void jswrap_ESP32_enableBLE(bool enable);
#endif
void jswrap_ESP32_enableWifi(bool enable);
#endif /* TARGETS_ESP32_JSWRAP_ESP32_H_ */

View File

@ -16,6 +16,7 @@
#include "jshardwarePWM.h"
#include "jshardwarePulse.h"
#include "jshardwareSpi.h"
#include "jshardwareESP32.h"
#include "jswrap_wifi.h" // jswrap_wifi_restore
#ifdef BLUETOOTH
@ -27,6 +28,9 @@
#include "spi_flash/include/esp_partition.h"
#include "esp_log.h"
#include "jsvar.h"
extern void initialise_wifi(void);
static void uartTask(void *data) {
@ -38,12 +42,17 @@ static void uartTask(void *data) {
}
static void espruinoTask(void *data) {
int heapVars;
PWMInit();
RMTInit();
SPIChannelsInit();
initADC(1);
jshInit(); // Initialize the hardware
jswrap_wifi_restore();
if(ESP32_Get_NVS_Status(ESP_NETWORK_WIFI)) jswrap_wifi_restore();
heapVars = (esp_get_free_heap_size() - 40000) / 16; //calculate space for jsVars
heapVars = heapVars - heapVars % 100; //round to 100
if(heapVars > 20000) heapVars = 20000; //WROVER boards have much more RAM, so we set a limit
jsVarsSize = heapVars;
jsvInit(); // Initialize the variables
// not sure why this delay is needed?
vTaskDelay(200 / portTICK_PERIOD_MS);