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

MultiMCU nvm size setting #643

Merged
merged 3 commits into from
Feb 24, 2024
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
15 changes: 14 additions & 1 deletion uCNC/src/hal/mcus/esp32/esp32_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ extern "C"
#include <SPIFFS.h>
#define FLASH_FS SPIFFS
#endif
static File upload_file;
static File upload_file;

void fs_file_updater()
{
Expand Down Expand Up @@ -971,11 +971,24 @@ extern "C"

uint8_t mcu_eeprom_getc(uint16_t address)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
return 0;
}
return EEPROM.read(address);
}

void mcu_eeprom_putc(uint16_t address, uint8_t value)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
}
EEPROM.write(address, value);
}

Expand Down
27 changes: 18 additions & 9 deletions uCNC/src/hal/mcus/esp32/mcu_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ void esp32_wifi_bt_init(void);
void esp32_wifi_bt_flush(uint8_t *buffer);
void esp32_wifi_bt_process(void);

#ifndef FLASH_EEPROM_SIZE
#define FLASH_EEPROM_SIZE 1024
#endif

#if !defined(RAM_ONLY_SETTINGS) && !defined(USE_ARDUINO_EEPROM_LIBRARY)
#include <nvs.h>
#include <esp_partition.h>
Expand All @@ -61,7 +57,7 @@ typedef struct
nvs_handle_t nvs_handle;
size_t size;
bool dirty;
uint8_t data[FLASH_EEPROM_SIZE];
uint8_t data[NVM_STORAGE_SIZE];
} flash_eeprom_t;

static flash_eeprom_t mcu_eeprom;
Expand Down Expand Up @@ -573,16 +569,16 @@ void mcu_init(void)

// starts EEPROM before UART to enable WiFi and BT settings
#if !defined(RAM_ONLY_SETTINGS) && !defined(USE_ARDUINO_EEPROM_LIBRARY)
// esp32_eeprom_init(FLASH_EEPROM_SIZE); // 1K Emulated EEPROM
// esp32_eeprom_init(NVM_STORAGE_SIZE); // 1K Emulated EEPROM

// starts nvs
mcu_eeprom.size = 0;
memset(mcu_eeprom.data, 0, FLASH_EEPROM_SIZE);
memset(mcu_eeprom.data, 0, NVM_STORAGE_SIZE);
if (nvs_open("eeprom", NVS_READWRITE, &mcu_eeprom.nvs_handle) == ESP_OK)
{
// determines the maximum sector size of NVS that can be read/write
nvs_get_blob(mcu_eeprom.nvs_handle, "eeprom", NULL, &mcu_eeprom.size);
if (FLASH_EEPROM_SIZE > mcu_eeprom.size)
if (NVM_STORAGE_SIZE > mcu_eeprom.size)
{
log_e("eeprom does not have enough space");
mcu_eeprom.size = 0;
Expand All @@ -596,7 +592,7 @@ void mcu_init(void)
}
#else
extern void esp32_eeprom_init(int size);
esp32_eeprom_init(FLASH_EEPROM_SIZE);
esp32_eeprom_init(NVM_STORAGE_SIZE);
#endif

#ifdef MCU_HAS_UART
Expand Down Expand Up @@ -1019,6 +1015,13 @@ void mcu_dotasks(void)
#if !defined(RAM_ONLY_SETTINGS) && !defined(USE_ARDUINO_EEPROM_LIBRARY)
uint8_t mcu_eeprom_getc(uint16_t address)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
return 0;
}
#ifndef RAM_ONLY_SETTINGS
// return esp32_eeprom_read(address);
size_t size = mcu_eeprom.size;
Expand All @@ -1035,6 +1038,12 @@ uint8_t mcu_eeprom_getc(uint16_t address)
* */
void mcu_eeprom_putc(uint16_t address, uint8_t value)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
}
#ifndef RAM_ONLY_SETTINGS
// esp32_eeprom_write(address, value);
size_t size = mcu_eeprom.size;
Expand Down
16 changes: 14 additions & 2 deletions uCNC/src/hal/mcus/esp8266/esp8266_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
#define FS_WRITE_GZ_SIZE 305
const char fs_write_page[305] PROGMEM = {0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0a, 0x55, 0x51, 0x3d, 0x4f, 0xc3, 0x30, 0x10, 0xdd, 0x91, 0xf8, 0x0f, 0x87, 0x67, 0x52, 0x43, 0x27, 0x84, 0xec, 0x2c, 0x85, 0x4a, 0x4c, 0x74, 0x68, 0x85, 0x18, 0x2f, 0xf6, 0xb5, 0xb1, 0xe4, 0xd8, 0x56, 0x72, 0x69, 0x55, 0x7e, 0x3d, 0x97, 0xa4, 0x03, 0x0c, 0xfe, 0x7a, 0xf7, 0xee, 0xdd, 0xd3, 0xb3, 0x79, 0x78, 0xfb, 0xdc, 0xec, 0xbf, 0x77, 0xef, 0xd0, 0x72, 0x17, 0x6b, 0x33, 0xed, 0x10, 0x31, 0x9d, 0xac, 0xa2, 0xa4, 0xe4, 0x4d, 0xe8, 0x6b, 0xd3, 0x11, 0x23, 0xb8, 0x16, 0xfb, 0x81, 0xd8, 0xaa, 0xc3, 0x7e, 0x5b, 0xbd, 0xa8, 0x1b, 0x9a, 0xb0, 0x23, 0xab, 0xce, 0x81, 0x2e, 0x25, 0xf7, 0xac, 0xc0, 0xe5, 0xc4, 0x94, 0x84, 0x75, 0x09, 0x9e, 0x5b, 0xeb, 0xe9, 0x1c, 0x1c, 0x55, 0xf3, 0xe3, 0x11, 0x42, 0x0a, 0x1c, 0x30, 0x56, 0x83, 0xc3, 0x48, 0xf6, 0x79, 0xf5, 0x24, 0x2a, 0x1c, 0x38, 0x52, 0xfd, 0x45, 0x0d, 0xec, 0x28, 0x79, 0x4c, 0x0c, 0x63, 0xf1, 0xc8, 0x64, 0xf4, 0x52, 0x31, 0x7a, 0xf1, 0xd0, 0x64, 0x7f, 0x15, 0x3f, 0xeb, 0x7f, 0xd4, 0xc3, 0x4c, 0x85, 0x6d, 0xee, 0x3b, 0xe1, 0xad, 0x6b, 0x73, 0x94, 0x1b, 0xa0, 0xe3, 0x90, 0x93, 0x55, 0xfa, 0x38, 0x28, 0x10, 0x97, 0x6d, 0xf6, 0x56, 0x95, 0x3c, 0x88, 0x3d, 0x4a, 0x8e, 0xaf, 0x45, 0x1c, 0x77, 0x63, 0xe4, 0x50, 0xb0, 0x67, 0x3d, 0xb5, 0x54, 0x22, 0x83, 0x62, 0x26, 0x62, 0x43, 0x11, 0x04, 0xb1, 0xea, 0x18, 0x22, 0x7d, 0xa4, 0x32, 0xb2, 0xaa, 0x37, 0x6d, 0xce, 0x03, 0x01, 0xc2, 0xea, 0xf4, 0x03, 0x13, 0xfe, 0x6a, 0xf4, 0xcc, 0xac, 0x4d, 0x98, 0x18, 0xb0, 0x48, 0x4e, 0x15, 0x05, 0xc1, 0xff, 0xed, 0xbd, 0xe5, 0xf3, 0x07, 0x40, 0xe7, 0xa8, 0x48, 0x3e, 0xa2, 0x25, 0x03, 0x9b, 0x5e, 0xd6, 0xc8, 0x9c, 0xd3, 0x4d, 0x64, 0x18, 0x9b, 0x2e, 0xc8, 0xcc, 0x43, 0x89, 0x19, 0xbd, 0xd1, 0x4b, 0x51, 0x52, 0x98, 0x6c, 0xca, 0xb1, 0xc4, 0xa0, 0xe7, 0xdf, 0xba, 0xbf, 0xfb, 0x05, 0x44, 0x67, 0x16, 0x56, 0xbf, 0x01, 0x00, 0x00};


ESP8266WebServer web_server(WEBSERVER_PORT);
ESP8266HTTPUpdateServer httpUpdater;
const char *update_path = OTA_URI;
Expand Down Expand Up @@ -378,7 +377,7 @@ extern "C"
#include <SPIFFS.h>
#define FLASH_FS SPIFFS
#endif
static File upload_file;
static File upload_file;
void fs_file_updater()
{
if (web_server.uri() != FS_WRITE_URI || web_server.method() != HTTP_POST || !web_server.hasArg("update"))
Expand Down Expand Up @@ -830,11 +829,24 @@ extern "C"

uint8_t mcu_eeprom_getc(uint16_t address)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
return 0;
}
return EEPROM.read(address);
}

void mcu_eeprom_putc(uint16_t address, uint8_t value)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
}
EEPROM.write(address, value);
}

Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/hal/mcus/esp8266/mcu_esp8266.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ void mcu_init(void)
timer1_write((APB_CLK_FREQ / ITP_SAMPLE_RATE));

#ifndef RAM_ONLY_SETTINGS
esp8266_eeprom_init(2048); // 2K Emulated EEPROM
esp8266_eeprom_init(NVM_STORAGE_SIZE); // 2K Emulated EEPROM
#endif
#ifdef MCU_HAS_SPI
esp8266_spi_init(SPI_FREQ, SPI_MODE);
Expand Down
8 changes: 4 additions & 4 deletions uCNC/src/hal/mcus/lpc176x/lpc176x_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ extern "C"
#if (I2C_ADDRESS != 0)
static uint8_t mcu_i2c_buffer_len;
static uint8_t mcu_i2c_buffer[I2C_SLAVE_BUFFER_SIZE];
void rp2040_i2c_onreceive(int len)
void lpc176x_i2c_onreceive(int len)
{
uint8_t l = I2C_REG.readBytes(mcu_i2c_buffer, len);
mcu_i2c_slave_cb(mcu_i2c_buffer, &l);
mcu_i2c_buffer_len = l;
}

void rp2040_i2c_onrequest(void)
void lpc176x_i2c_onrequest(void)
{
I2C_REG.write(mcu_i2c_buffer, mcu_i2c_buffer_len);
}
Expand All @@ -110,8 +110,8 @@ extern "C"
#if I2C_ADDRESS == 0
I2C_REG.begin();
#else
I2C_REG.onReceive(rp2040_i2c_onreceive);
I2C_REG.onRequest(rp2040_i2c_onrequest);
I2C_REG.onReceive(lpc176x_i2c_onreceive);
I2C_REG.onRequest(lpc176x_i2c_onrequest);
I2C_REG.begin(I2C_ADDRESS);
#endif
}
Expand Down
6 changes: 6 additions & 0 deletions uCNC/src/hal/mcus/lpc176x/mcu_lpc176x.c
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,9 @@ void mcu_dotasks()
* */
uint8_t mcu_eeprom_getc(uint16_t address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
return 0;
}

Expand All @@ -979,6 +982,9 @@ uint8_t mcu_eeprom_getc(uint16_t address)
* */
void mcu_eeprom_putc(uint16_t address, uint8_t value)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
}

/**
Expand Down
4 changes: 4 additions & 0 deletions uCNC/src/hal/mcus/mcudefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ extern "C"
#error Undefined mcu
#endif

#ifndef NVM_STORAGE_SIZE
#define NVM_STORAGE_SIZE 0x400
#endif

#include "mcu.h" //exposes the MCU HAL interface

#ifdef __cplusplus
Expand Down
15 changes: 14 additions & 1 deletion uCNC/src/hal/mcus/rp2040/mcu_rp2040.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void mcu_init(void)
#endif

#ifndef RAM_ONLY_SETTINGS
rp2040_eeprom_init(2048); // 2K Emulated EEPROM
rp2040_eeprom_init(NVM_STORAGE_SIZE); // 2K Emulated EEPROM
#endif
#ifdef MCU_HAS_SPI
mcu_spi_config(SPI_FREQ, SPI_MODE);
Expand Down Expand Up @@ -543,6 +543,13 @@ void mcu_dotasks(void)
* */
uint8_t mcu_eeprom_getc(uint16_t address)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
return 0;
}
#ifndef RAM_ONLY_SETTINGS
return rp2040_eeprom_read(address);
#else
Expand All @@ -555,6 +562,12 @@ uint8_t mcu_eeprom_getc(uint16_t address)
* */
void mcu_eeprom_putc(uint16_t address, uint8_t value)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
}
#ifndef RAM_ONLY_SETTINGS
rp2040_eeprom_write(address, value);
#endif
Expand Down
16 changes: 14 additions & 2 deletions uCNC/src/hal/mcus/samd21/mcu_samd21.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

// Non volatile memory
// SAMD devices page size never exceeds 1024 bytes
#define NVM_EEPROM_SIZE 0x400 // 1Kb of emulated EEPROM is enough
#define NVM_EEPROM_SIZE NVM_STORAGE_SIZE // 1Kb of emulated EEPROM is enough
#define NVM_PAGE_SIZE NVMCTRL_PAGE_SIZE
#define NVM_ROW_PAGES NVMCTRL_ROW_PAGES
#define NVM_ROW_SIZE NVMCTRL_ROW_SIZE
Expand Down Expand Up @@ -1191,6 +1191,13 @@ static void mcu_write_flash_page(const uint32_t destination_address, const uint8
* */
uint8_t mcu_eeprom_getc(uint16_t address)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
return 0;
}
address &= (NVM_EEPROM_SIZE - 1); // keep within 1Kb address range

if (!samd21_eeprom_loaded)
Expand All @@ -1206,7 +1213,12 @@ uint8_t mcu_eeprom_getc(uint16_t address)
* */
void mcu_eeprom_putc(uint16_t address, uint8_t value)
{

if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
}
address &= (NVM_EEPROM_SIZE - 1);

if (!samd21_eeprom_loaded)
Expand Down
30 changes: 22 additions & 8 deletions uCNC/src/hal/mcus/stm32f1x/mcu_stm32f1x.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,23 @@
#error "The set FLASH_SIZE is beyond the chip capability"
#endif

// set the FLASH EEPROM SIZE
#define FLASH_EEPROM_SIZE 0x400

#if (FLASH_BANK1_END <= 0x0801FFFFUL)
#define FLASH_EEPROM_PAGES (((FLASH_EEPROM_SIZE - 1) >> 10) + 1)
#define FLASH_PAGE_SIZE (1 << 10)
#define FLASH_EEPROM_PAGES (((NVM_STORAGE_SIZE - 1) >> 10) + 1)
#define FLASH_EEPROM (FLASH_LIMIT - ((FLASH_EEPROM_PAGES << 10) - 1))
#define FLASH_PAGE_MASK (0xFFFF - (1 << 10) + 1)
#define FLASH_PAGE_OFFSET_MASK (0xFFFF & ~FLASH_PAGE_MASK)
#else
#define FLASH_EEPROM_PAGES (((FLASH_EEPROM_SIZE - 1) >> 11) + 1)
#define FLASH_PAGE_SIZE 0x800
#define FLASH_EEPROM_PAGES (((NVM_STORAGE_SIZE - 1) >> 11) + 1)
#define FLASH_EEPROM (FLASH_LIMIT - ((FLASH_EEPROM_PAGES << 11) - 1))
#define FLASH_PAGE_MASK (0xFFFF - (1 << 11) + 1)
#define FLASH_PAGE_OFFSET_MASK (0xFFFF & ~FLASH_PAGE_MASK)
#endif

#define READ_FLASH(ram_ptr, flash_ptr) (*ram_ptr = ~(*flash_ptr))
#define WRITE_FLASH(flash_ptr, ram_ptr) (*flash_ptr = ~(*ram_ptr))
static uint8_t stm32_flash_page[FLASH_EEPROM_SIZE];
static uint8_t stm32_flash_page[FLASH_PAGE_SIZE];
static uint16_t stm32_flash_current_page;
static bool stm32_flash_modified;

Expand Down Expand Up @@ -845,9 +844,10 @@ static uint16_t mcu_access_flash_page(uint16_t address)
uint16_t address_offset = address & FLASH_PAGE_OFFSET_MASK;
if (stm32_flash_current_page != address_page)
{
mcu_eeprom_flush();
stm32_flash_modified = false;
stm32_flash_current_page = address_page;
uint16_t counter = (uint16_t)(FLASH_EEPROM_SIZE >> 2);
uint16_t counter = (uint16_t)(FLASH_PAGE_SIZE >> 2);
uint32_t *ptr = ((uint32_t *)&stm32_flash_page[0]);
volatile uint32_t *eeprom = ((volatile uint32_t *)(FLASH_EEPROM + address_page));
while (counter--)
Expand All @@ -864,6 +864,13 @@ static uint16_t mcu_access_flash_page(uint16_t address)
// Non volatile memory
uint8_t mcu_eeprom_getc(uint16_t address)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
return 0;
}
uint16_t offset = mcu_access_flash_page(address);
return stm32_flash_page[offset];
}
Expand All @@ -889,6 +896,13 @@ static void mcu_eeprom_erase(uint16_t address)

void mcu_eeprom_putc(uint16_t address, uint8_t value)
{
if (NVM_STORAGE_SIZE <= address)
{
DEBUG_STR("EEPROM invalid address @ ");
DEBUG_INT(address);
DEBUG_PUTC('\n');
}

uint16_t offset = mcu_access_flash_page(address);

if (stm32_flash_page[offset] != value)
Expand All @@ -906,7 +920,7 @@ void mcu_eeprom_flush()
mcu_eeprom_erase(stm32_flash_current_page);
volatile uint16_t *eeprom = ((volatile uint16_t *)(FLASH_EEPROM + stm32_flash_current_page));
uint16_t *ptr = ((uint16_t *)&stm32_flash_page[0]);
uint16_t counter = (uint16_t)(FLASH_EEPROM_SIZE >> 1);
uint16_t counter = (uint16_t)(FLASH_PAGE_SIZE >> 1);
while (counter--)
{
while (FLASH->SR & FLASH_SR_BSY)
Expand Down
Loading
Loading