Added to allow remapping of addresses for ESP8266/ESP32 without loads of code duplication

This commit is contained in:
Gordon Williams 2018-02-13 10:04:35 +00:00
parent d566d097c7
commit 3e08b0138e
13 changed files with 59 additions and 24 deletions

View File

@ -35,6 +35,7 @@
Add 'E.asm' placeholder to detect E.asm calls that haven't been replaced by the IDE
Add process.env.EXPTR to link to table of functions - will work better for compiled code over BLE
Added SAVE_ON_FLASH_EXTREME for HYSTM32_28, where we're now cutting out some Math.X functionality to keep builds going
Added `jshFlashGetMemMapAddress` to allow remapping of addresses for ESP8266/ESP32 without loads of code duplication
1v95 : nRF5x: Swap to UART fifo to avoid overrun errors at high baud rates
Ensure Exceptions/errors are reported on a blank line

View File

@ -346,6 +346,12 @@ void jshFlashRead(void *buf, uint32_t addr, uint32_t len);
* guaranteed to be 4-byte aligned, and length is a multiple of 4. */
void jshFlashWrite(void *buf, uint32_t addr, uint32_t len);
/** On most platforms, the address of something really is that address.
* In ESP32/ESP8266 the flash memory is mapped up at a much higher address,
* so we need to tweak any pointers that we use.
* */
size_t jshFlashGetMemMapAddress(size_t ptr);
/** Utility timer handling functions
* ------------------------------------------

View File

@ -861,7 +861,9 @@ JsVar *jswrap_espruino_memoryArea(int addr, int len) {
jsExceptionHere(JSET_ERROR, "Memory area too long! Max is 65535 bytes\n");
return 0;
}
return jsvNewNativeString((char*)(size_t)addr, (size_t)len);
// hack for ESP8266/ESP32 where the address can be different
size_t mappedAddr = jshFlashGetMemMapAddress((size_t)addr);
return jsvNewNativeString((char*)mappedAddr, (size_t)len);
}
/*JSON{

View File

@ -439,22 +439,7 @@ const char *jsfGetBootCodeFromFlash(bool isReset) {
// Don't execute code if we've reset and code shouldn't always be run
if (isReset && !(bootCodeInfo & BOOT_CODE_RUN_ALWAYS)) return 0;
#ifdef ESP32
// romdata_jscode is memory mapped address from the js_code partition in rom - targets/esp32/main.c
extern char* romdata_jscode;
if (romdata_jscode==0) {
jsError("Couldn't find js_code partition - update with partition_espruino.bin\n");
return 0;
}
code = &romdata_jscode[FLASH_DATA_LOCATION-FLASH_SAVED_CODE_START];
#else
code = (char *)(FLASH_DATA_LOCATION);
#endif
#ifdef ESP8266
// the flash address is just the offset into the flash chip, but to evaluate the code
// below we need to jump to the memory-mapped window onto flash, so adjust here
code += 0x40200000;
#endif
code = (char *)(jshFlashGetMemMapAddress(FLASH_DATA_LOCATION));
return code;
}

View File

@ -87,18 +87,20 @@ Read 32 bits of memory at the given location - DANGEROUS!
Write 32 bits of memory at the given location - VERY DANGEROUS!
*/
uint32_t _jswrap_io_peek(JsVarInt addr, int wordSize) {
if (wordSize==1) return READ_FLASH_UINT8((char*)(size_t)addr);
uint32_t _jswrap_io_peek(size_t addr, int wordSize) {
if (wordSize==1) return READ_FLASH_UINT8((char*)addr);
if (wordSize==2) {
return READ_FLASH_UINT8((char*)(size_t)addr) | (uint32_t)(READ_FLASH_UINT8((char*)(size_t)(addr+1)) << 8);
return READ_FLASH_UINT8((char*)addr) | (uint32_t)(READ_FLASH_UINT8((char*)(addr+1)) << 8);
}
if (wordSize==4) return (uint32_t)*(unsigned int*)(size_t)addr;
if (wordSize==4) return (uint32_t)*(unsigned int*)addr;
return 0;
}
JsVar *jswrap_io_peek(JsVarInt addr, JsVarInt count, int wordSize) {
// hack for ESP8266/ESP32 where the address can be different
size_t mappedAddr = jshFlashGetMemMapAddress((size_t)addr);
if (count<=1) {
return jsvNewFromLongInteger((long long)_jswrap_io_peek(addr, wordSize));
return jsvNewFromLongInteger((long long)_jswrap_io_peek(mappedAddr, wordSize));
} else {
JsVarDataArrayBufferViewType aType;
if (wordSize==1) aType=ARRAYBUFFERVIEW_UINT8;
@ -109,8 +111,8 @@ JsVar *jswrap_io_peek(JsVarInt addr, JsVarInt count, int wordSize) {
JsvArrayBufferIterator it;
jsvArrayBufferIteratorNew(&it, arr, 0);
while (jsvArrayBufferIteratorHasElement(&it)) {
jsvArrayBufferIteratorSetIntegerValue(&it, (JsVarInt)_jswrap_io_peek(addr, wordSize));
addr += wordSize;
jsvArrayBufferIteratorSetIntegerValue(&it, (JsVarInt)_jswrap_io_peek(mappedAddr, wordSize));
mappedAddr += (size_t)wordSize;
jsvArrayBufferIteratorNext(&it);
}
jsvArrayBufferIteratorFree(&it);

View File

@ -919,6 +919,9 @@ void jshFlashWrite(void * buf, uint32_t addr, uint32_t len)
NVMHAL_DeInit();
}
// Just pass data through, since we can access flash at the same address we wrote it
size_t jshFlashGetMemMapAddress(size_t ptr) { return ptr; }
/// Enter simple sleep mode (can be woken up by interrupts). Returns true on success
bool jshSleep(JsSysTime timeUntilWake) {
#ifdef USE_RTC

View File

@ -717,6 +717,18 @@ void jshFlashErasePage(
spi_flash_erase_sector(addr >> FLASH_PAGE_SHIFT);
}
size_t jshFlashGetMemMapAddress(size_t ptr) {
// if ptr is high already, assume we know what we're accessing
if (ptr > 0x10000000) return ptr;
// romdata_jscode is memory mapped address from the js_code partition in rom - targets/esp32/main.c
extern char* romdata_jscode;
if (romdata_jscode==0) {
jsError("Couldn't find js_code partition - update with partition_espruino.bin\n");
return 0;
}
return &romdata_jscode[ptr];
}
unsigned int jshSetSystemClock(JsVar *options) {
UNUSED(options);
jsError(">> jshSetSystemClock Not implemented");

View File

@ -1383,6 +1383,14 @@ void jshFlashErasePage(
res == SPI_FLASH_RESULT_ERR ? "error" : "timeout");
}
size_t jshFlashGetMemMapAddress(size_t ptr) {
// the flash address is just the offset into the flash chip, but to evaluate the code
// below we need to jump to the memory-mapped window onto flash, so adjust here
if (ptr < FLASH_MAX) {
return ptr + FLASH_MMAP;
return ptr;
}
unsigned int jshSetSystemClock(JsVar *options) {
int newFreq = jsvGetInteger(options);
if (newFreq != 80 && newFreq != 160) {

View File

@ -886,6 +886,9 @@ void jshFlashWrite(void *buf, uint32_t addr, uint32_t len) {
fclose(f);
}
// Just pass data through, since we can access flash at the same address we wrote it
size_t jshFlashGetMemMapAddress(size_t ptr) { return ptr; }
unsigned int jshSetSystemClock(JsVar *options) {
return 0;
}

View File

@ -1262,6 +1262,9 @@ void jshFlashWrite(void * buf, uint32_t addr, uint32_t len) {
//nrf_nvmc_write_bytes(addr, buf, len);
}
// Just pass data through, since we can access flash at the same address we wrote it
size_t jshFlashGetMemMapAddress(size_t ptr) { return ptr; }
/// Enter simple sleep mode (can be woken up by interrupts). Returns true on success
bool jshSleep(JsSysTime timeUntilWake) {
/* Wake ourselves up if we're supposed to, otherwise if we're not waiting for

View File

@ -298,6 +298,9 @@ void jshFlashRead(void * buf, uint32_t addr, uint32_t len) {
memcpy(buf, (void*)addr, len);
}
// Just pass data through, since we can access flash at the same address we wrote it
size_t jshFlashGetMemMapAddress(size_t ptr) { return ptr; }
JsSysTime jshGetSystemTime() {
return (JsSysTime) ((unsigned long long)uppercounter * 0xFFFFFFFF) + GetTickCount();
}

View File

@ -2936,6 +2936,9 @@ void jshFlashWrite(void *buf, uint32_t addr, uint32_t len) {
#endif
}
// Just pass data through, since we can access flash at the same address we wrote it
size_t jshFlashGetMemMapAddress(size_t ptr) { return ptr; }
int jshSetSystemClockPClk(JsVar *options, const char *clkName) {
JsVar *v = jsvObjectGetChild(options, clkName, 0);
JsVarInt i = jsvGetIntegerAndUnLock(v);

View File

@ -1974,6 +1974,7 @@ void jshFlashErasePage(uint32_t addr){
void jshFlashRead(void *buf, uint32_t addr, uint32_t len){
memcpy(buf, (void*)addr, len);
}
/** Write data to flash memory from the buffer, the buffer address and flash address are
* guaranteed to be 4-byte aligned, and length is a multiple of 4. */
void jshFlashWrite(void *buf, uint32_t addr, uint32_t len){
@ -1994,6 +1995,9 @@ void jshFlashWrite(void *buf, uint32_t addr, uint32_t len){
}
// Just pass data through, since we can access flash at the same address we wrote it
size_t jshFlashGetMemMapAddress(size_t ptr) { return ptr; }
/** Utility timer handling functions
* ------------------------------------------