Add flash storage support to emscripten build

This commit is contained in:
Gordon Williams 2020-06-11 15:45:00 +01:00
parent e209ebe692
commit 21168a2b29
3 changed files with 44 additions and 12 deletions

View File

@ -64,6 +64,7 @@
Bangle.js: Fix occasional execution errors when executing from Flash (fix #1854)
Add Graphics.transformVertices()
Re-add Graphics.quadraticBezier for Bangle.js, move to integer math to halve fn size
Emscripten: (fake) Flash memory support
2v05 : Add Array.includes
Fix (Number.toFixed) rounding, eg (1234.505).toFixed(2)

View File

@ -35,6 +35,7 @@ info = {
'EMSCRIPTEN=1',
'DEFINES += -DUSE_TENSORFLOW',
'DEFINES += -DBANGLEJS',
'DEFINES += -DSPIFLASH_BASE=0x8000000 -DSPIFLASH_LENGTH=4194304',
# 'DEFINES+=-DCUSTOM_GETBATTERY=jswrap_banglejs_getBattery',
'DEFINES+=-DDUMP_IGNORE_VARIABLES=\'"g\\0"\'',
'DEFINES+=-DUSE_FONT_6X8 -DGRAPHICS_PALETTED_IMAGES -DUSE_LCD_EMSCRTIPTEN',
@ -50,13 +51,19 @@ chip = {
'family' : "EMSCRIPTEN",
'package' : "",
'ram' : 0,
'flash' : 256, # size of file used to fake flash memory (kb)
'flash' : 4096, # size of file used to fake flash memory (kb)
'speed' : -1,
'usart' : 1,
'spi' : 1,
'i2c' : 1,
'adc' : 0,
'dac' : 0,
'saved_code' : {
'address' : 0x8000000,
'page_size' : 4096,
'pages' : 1024, # Entire 4MB of external flash
'flash_available' : 1024 # lots - this is fake
},
};
devices = {

View File

@ -11,16 +11,16 @@
* Platform Specific part of Hardware interface Layer
* ----------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/select.h>
#include <termios.h>
#include <fcntl.h>
#include <signal.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/select.h>
#include <termios.h>
#include <fcntl.h>
#include <signal.h>
#include <inttypes.h>
#include <emscripten.h>
#include "platform_config.h"
@ -261,6 +261,7 @@ JsVarFloat jshReadVRef() { return NAN; };
unsigned int jshGetRandomNumber() { return rand(); }
bool jshFlashGetPage(uint32_t addr, uint32_t *startAddr, uint32_t *pageSize) {
if (addr<FLASH_START) return false;
*startAddr = (uint32_t)(floor(addr / FAKE_FLASH_BLOCKSIZE) * FAKE_FLASH_BLOCKSIZE);
*pageSize = FAKE_FLASH_BLOCKSIZE;
return true;
@ -270,14 +271,37 @@ JsVar *jshFlashGetFree() {
return jsFreeFlash;
}
void jshFlashErasePage(uint32_t addr) {
#ifdef EMSCRIPTEN
uint32_t startAddr;
uint32_t pageSize;
if (jshFlashGetPage(addr, &startAddr, &pageSize)) {
for (uint32_t i=0;i<pageSize;i++)
EM_ASM_({ hwFlashWrite($0,0xFF); }, startAddr+i-FLASH_START);
}
#endif
}
void jshFlashRead(void *buf, uint32_t addr, uint32_t len) {
if (addr<FLASH_START) return;
#ifdef EMSCRIPTEN
for (uint32_t i=0;i<len;i++)
((uint8_t*)buf)[i] = EM_ASM_INT({ return hwFlashRead($0) }, addr+i-FLASH_START);
#endif
}
void jshFlashWrite(void *buf, uint32_t addr, uint32_t len) {
if (addr<FLASH_START) return;
#ifdef EMSCRIPTEN
for (uint32_t i=0;i<len;i++)
EM_ASM_({ hwFlashWrite($0,$1); }, addr+i-FLASH_START, ((uint8_t*)buf)[i]);
#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; }
size_t jshFlashGetMemMapAddress(size_t addr) {
// don't allow flash to be memory mapped
if (addr>=FLASH_START && addr<FLASH_START+FLASH_TOTAL)
return 0;
return addr;
}
unsigned int jshSetSystemClock(JsVar *options) {
return 0;