1. Исправления в библиотеке EEPROM:
- исправлена работа EEPROM.put() при записи объектов по невыравненным адресам;
- исправлено чтение EEPROM.get() для объектов, занимающих несколько страниц;
- добавлены проверки выхода адреса и объекта за пределы EEPROM, убран переход записи/чтения в начало памяти при достижении конца EEPROM;
- EEPROM.put() теперь стирает и записывает страницу, только если данные изменились.
2. Исправления в библиотеке HardwareSerial:
- в обработчике прерываний по приему данных вычисление индекса для следующего принятого байта перенесено в цикл обработки принятых данных;
- размер буфера для приема теперь задается, только если не был задан ранее.
3. Использование драйвера при загрузке скетчей или загрузчика через программатор теперь задается отдельно для каждой платы - драйвер используется только при работе с платой START-MIK32.
Co-authored-by: KLASSENTS <klassen@elron.tech>
Co-committed-by: KLASSENTS <klassen@elron.tech>
This commit is contained in:
KLASSENTS 2026-05-13 07:59:59 +03:00 committed by klassents
parent 48f0786566
commit 41867b00a2
7 changed files with 96 additions and 59 deletions

View File

@ -14,6 +14,7 @@ aceUno8Mb.upload.speed=230400
# tool for bootloader update
aceUno8Mb.bootloader.tool=mik32_upload
aceUno8Mb.bootloader.tool.default=mik32_upload
aceUno8Mb.bootloader.driver_option=--no-driver
aceUno8Mb.bootloader.file=elbear/bootloader.hex
aceUno8Mb.bootloader.interface=ftdi/mikron-link.cfg
aceUno8Mb.bootloader.params.verbose=
@ -41,6 +42,7 @@ aceUno16Mb.upload.speed=230400
# tool for bootloader update
aceUno16Mb.bootloader.tool=mik32_upload
aceUno16Mb.bootloader.tool.default=mik32_upload
aceUno16Mb.bootloader.driver_option=--no-driver
aceUno16Mb.bootloader.file=elbear/bootloader.hex
aceUno16Mb.bootloader.interface=ftdi/mikron-link.cfg
aceUno16Mb.bootloader.params.verbose=
@ -68,6 +70,7 @@ aceUno32Mb.upload.speed=230400
# tool for bootloader update
aceUno32Mb.bootloader.tool=mik32_upload
aceUno32Mb.bootloader.tool.default=mik32_upload
aceUno32Mb.bootloader.driver_option=--no-driver
aceUno32Mb.bootloader.file=elbear/bootloader.hex
aceUno32Mb.bootloader.interface=ftdi/mikron-link.cfg
aceUno32Mb.bootloader.params.verbose=
@ -94,6 +97,7 @@ aceNano.upload.speed=230400
# tool for bootloader update
aceNano.bootloader.tool=mik32_upload
aceNano.bootloader.tool.default=mik32_upload
aceNano.bootloader.driver_option=--no-driver
aceNano.bootloader.file=elbear/bootloader.hex
aceNano.bootloader.interface=ftdi/mikron-link.cfg
aceNano.bootloader.params.verbose=
@ -120,6 +124,7 @@ elsomik.upload.speed=230400
# tool for bootloader update
elsomik.bootloader.tool=mik32_upload
elsomik.bootloader.tool.default=mik32_upload
elsomik.bootloader.driver_option=--no-driver
elsomik.bootloader.file=elsomik/bootloader.hex
elsomik.bootloader.interface=ftdi/mikron-link.cfg
elsomik.bootloader.params.verbose=
@ -147,6 +152,7 @@ start-mik32-v1.upload.speed=120000
# tool for bootloader update
start-mik32-v1.bootloader.tool=mik32_upload
start-mik32-v1.bootloader.tool.default=mik32_upload
start-mik32-v1.bootloader.driver_option=
start-mik32-v1.bootloader.file=start-mik32/bootloader.hex
start-mik32-v1.bootloader.interface=start-link.cfg
start-mik32-v1.bootloader.params.verbose=

View File

@ -121,18 +121,16 @@ int HardwareSerial::availableForWrite(void)
void HardwareSerial::rx_complete_irq(void)
{
// which UART to use
UART_TypeDef* uart = UART_0;
if (uartNum == 1)
uart = UART_1;
// find next index in buffer with upper limit
uint8_t i = (uint8_t)(_rx_buffer_head + 1)%SERIAL_RX_BUFFER_SIZE;
UART_TypeDef* uart = (uartNum == 1) ? UART_1 : UART_0;
rx_buffer_index_t i;
unsigned char c;
// while there is something to receive, put the data into the buffer
// and edit the buffer index
while(!UART_IS_RX_FIFO_EMPTY(uart))
{
c = UART_READ_BYTE(uart);
// find next index in buffer with upper limit
i = (unsigned int)(_rx_buffer_head + 1)%SERIAL_RX_BUFFER_SIZE;
if (i != _rx_buffer_tail)
{
// write if there is space in the buffer

View File

@ -41,7 +41,15 @@
// often work, but occasionally a race condition can occur that makes
// Serial behave erratically. See https://github.com/arduino/Arduino/issues/2405
#if !defined(SERIAL_RX_BUFFER_SIZE)
#define SERIAL_RX_BUFFER_SIZE 64
#endif
#if (SERIAL_RX_BUFFER_SIZE>256)
typedef uint16_t rx_buffer_index_t;
#else
typedef uint8_t rx_buffer_index_t;
#endif
// Define config for Serial.begin(baud, config);
// parity: 7...4 bits = 0 (no), 2 (even), 3 (odd)
@ -68,8 +76,8 @@
class HardwareSerial : public Stream
{
protected:
volatile uint8_t _rx_buffer_head;
volatile uint8_t _rx_buffer_tail;
volatile rx_buffer_index_t _rx_buffer_head;
volatile rx_buffer_index_t _rx_buffer_tail;
unsigned char _rx_buffer[SERIAL_RX_BUFFER_SIZE];
private:

View File

@ -134,6 +134,5 @@ This is useful for STL objects, custom iteration and C++11 style ranged for loop
#### **`EEPROM.end()`**
This function returns an `EEPtr` pointing at the location after the last EEPROM cell.
Used with `begin()` to provide custom iteration.
**Note:** The `EEPtr` returned is invalid as it is out of range. In fact the hardware causes wrapping of the address (overflow) and `EEPROM.end()` actually references the first EEPROM cell.
**Note:** The `EEPtr` returned is invalid as it is out of range and must not be dereferenced.

View File

@ -20,13 +20,13 @@ uint8_t read_byte( int idx )
// check if idx is valid
if (idx < 0)
{
idx = -idx;
ErrorMsgHandler("EEPROM.read(): The eeprom cell address must be non-negative");
return 0xFF;
}
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGTH)
{
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
ErrorMsgHandler("EEPROM.read(): The address of the eeprom cell goes beyond the eeprom");
return 0xFF;
}
uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {};
@ -50,13 +50,13 @@ void write_byte( int idx, uint8_t val )
// check if idx is valid
if (idx < 0)
{
idx = -idx;
ErrorMsgHandler("EEPROM.write(): The eeprom cell address must be non-negative");
return;
}
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGTH)
{
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
ErrorMsgHandler("EEPROM.write(): The address of the eeprom cell goes beyond the eeprom");
return;
}
update_byte(idx, val);
}
@ -66,13 +66,13 @@ void update_byte( int idx, uint8_t val )
// check if idx is valid
if (idx < 0)
{
idx = -idx;
ErrorMsgHandler("EEPROM.update(): The eeprom cell address must be non-negative");
return;
}
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGTH)
{
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
ErrorMsgHandler("EEPROM.update(): The address of the eeprom cell goes beyond the eeprom");
return;
}
uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {};
@ -88,15 +88,16 @@ void update_byte( int idx, uint8_t val )
// byte number in a word
uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE;
// get desired byte
uint32_t byte = ((uint32_t)val) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8);
uint8_t oldVal = (uint8_t)(*((uint8_t*)write_data_buf + word_idx * EEPROM_WORD_SIZE + (EEPROM_WORD_SIZE - byte_idx - 1)));
uint32_t shift = (EEPROM_WORD_SIZE - byte_idx - 1) * 8;
uint32_t byte = ((uint32_t)val) << shift;
uint8_t oldVal = (uint8_t)((write_data_buf[word_idx] >> shift) & 0xFF);
// checking if written byte is different from the new one
if(oldVal != val)
{
// clear page
HAL_EEPROM_Erase(&heeprom, (uint16_t)addr, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT);
// get and replace the desired byte
write_data_buf[word_idx] = (write_data_buf[word_idx] & (~((uint32_t)(0xFF) << ((EEPROM_WORD_SIZE - byte_idx - 1) * 8)))) | byte;
write_data_buf[word_idx] = (write_data_buf[word_idx] & (~((uint32_t)(0xFF) << shift))) | byte;
HAL_EEPROM_Write(&heeprom, (uint16_t)addr, write_data_buf, EEPROM_PAGE_WORDS, HAL_EEPROM_WRITE_SINGLE, EEPROM_OP_TIMEOUT);
}

View File

@ -12,7 +12,7 @@
#define EEPROM_WORD_SIZE 4 // word takes 4 bytes
#define EEPROM_PAGE_SIZE ( EEPROM_PAGE_WORDS * EEPROM_WORD_SIZE ) // page takes 32*4 = 128 bytes
#define EEPROM_END 0x1FFF
#define EEPROM_LENGHT (EEPROM_PAGE_SIZE * EEPROM_PAGE_COUNT)
#define EEPROM_LENGTH (EEPROM_PAGE_SIZE * EEPROM_PAGE_COUNT)
void HAL_read(uint16_t addr, uint32_t * data);
void HAL_write(uint16_t addr, uint32_t * data);
@ -81,6 +81,14 @@ struct EEPtr{
int index;
};
static inline uint32_t eeprom_reverse_word(uint32_t word)
{
return ((word & 0x000000FFUL) << 24) |
((word & 0x0000FF00UL) << 8) |
((word & 0x00FF0000UL) >> 8) |
((word & 0xFF000000UL) >> 24);
}
struct EEPROMClass{
//Basic user access methods.
@ -91,7 +99,7 @@ struct EEPROMClass{
void begin();
EEPtr end() { return length(); } // Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
uint16_t length() { return (uint16_t)EEPROM_LENGHT; }
uint16_t length() { return (uint16_t)EEPROM_LENGTH; }
template< typename T >
const T &put(int idx, const T &data)
@ -100,16 +108,22 @@ struct EEPROMClass{
// check if idx is valid
if (idx < 0)
{
idx = -idx;
ErrorMsgHandler("EEPROM.put(): The eeprom cell address must be non-negative");
return data;
}
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGTH)
{
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
ErrorMsgHandler("EEPROM.put(): The address of the eeprom cell goes beyond the eeprom");
return data;
}
if (sizeof(data) > ((uint32_t)EEPROM_LENGTH - (uint32_t)idx))
{
ErrorMsgHandler("EEPROM.put(): The data goes beyond the eeprom");
return data;
}
uint32_t write_data_buf[EEPROM_PAGE_WORDS] = {};
uint32_t old_data_buf[EEPROM_PAGE_WORDS];
uint32_t dataSize = sizeof(data); // writing data size
uint32_t dataShift = 0; // shift of the data writing start address
uint32_t writeSize = dataSize;
@ -126,45 +140,56 @@ struct EEPROMClass{
uint32_t byte_idx = ((uint32_t)idx) % EEPROM_WORD_SIZE;
// read first page
HAL_read((uint16_t)addr, write_data_buf);
memcpy(old_data_buf, write_data_buf, sizeof(write_data_buf));
// if data does not fit on the first page, then write down only what fits
if (EEPROM_PAGE_SIZE - byte_addr < dataSize)
writeSize = EEPROM_PAGE_SIZE - byte_addr;
uint32_t lastWord = (writeSize + byte_idx - 1) / EEPROM_WORD_SIZE + word_idx;
dataSize -= writeSize;
// Convert affected words to byte order suitable for memcpy()
for(uint8_t i = word_idx; i <= lastWord; i++)
{
write_data_buf[i] = eeprom_reverse_word(write_data_buf[i]);
}
// write data page by page, first separately write the first page
memcpy((void *)((uint8_t *)write_data_buf + byte_addr), (void*)dataPointer, writeSize);
// prepare words for writing
for(uint8_t i = word_idx; i <= lastWord; i++)
{
uint32_t word = write_data_buf[i];
write_data_buf[i] = 0;
write_data_buf[i] = ((word & 0xFF)<<24) | ((word & (0xFF<<8))<<8) | ((word & (0xFF<<16))>>8) | ((word & (0xFF<<24))>>24);
write_data_buf[i] = eeprom_reverse_word(write_data_buf[i]);
}
if (memcmp(old_data_buf, write_data_buf, sizeof(write_data_buf)) != 0)
{
HAL_erase((uint16_t)addr);
HAL_write((uint16_t)addr, write_data_buf);
}
HAL_erase((uint16_t)addr);
HAL_write((uint16_t)addr, write_data_buf);
// if there is data left after writing the first page, then write it page by page until it runs out
while (dataSize > 0)
{
addr += EEPROM_PAGE_SIZE;
// if reaching the eeprom end address, return to the initial address
if (addr == EEPROM_START_ADDR + EEPROM_LENGHT)
addr = EEPROM_START_ADDR;
HAL_read((uint16_t)addr, write_data_buf);
memcpy(old_data_buf, write_data_buf, sizeof(write_data_buf));
dataShift += writeSize;
writeSize = dataSize;
if (EEPROM_PAGE_SIZE < dataSize)
writeSize = EEPROM_PAGE_SIZE;
lastWord = (writeSize - 1) / EEPROM_WORD_SIZE;
// Convert affected words to byte order suitable for memcpy()
for(uint8_t i = 0; i <= lastWord; i++)
{
write_data_buf[i] = eeprom_reverse_word(write_data_buf[i]);
}
memcpy((void *)(write_data_buf), (void*)((uint8_t *)dataPointer + dataShift), writeSize);
// prepare words for writing
for(uint8_t i = 0; i <= lastWord; i++)
{
uint32_t word = write_data_buf[i];
write_data_buf[i] = 0;
write_data_buf[i] = ((word & 0xFF)<<24) | ((word & (0xFF<<8))<<8) | ((word & (0xFF<<16))>>8) | ((word & (0xFF<<24))>>24);
write_data_buf[i] = eeprom_reverse_word(write_data_buf[i]);
}
if (memcmp(old_data_buf, write_data_buf, sizeof(write_data_buf)) != 0)
{
HAL_erase((uint16_t)addr);
HAL_write((uint16_t)addr, write_data_buf);
}
HAL_erase((uint16_t)addr);
HAL_write((uint16_t)addr, write_data_buf);
dataSize -= writeSize;
}
return data;
@ -177,13 +202,18 @@ struct EEPROMClass{
// check if idx is valid
if (idx < 0)
{
idx = -idx;
ErrorMsgHandler("EEPROM.get(): The eeprom cell address must be non-negative");
return data;
}
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGHT)
if ((uint32_t)idx >= (uint32_t)EEPROM_LENGTH)
{
idx = (int)((uint32_t)idx % EEPROM_LENGHT);
ErrorMsgHandler("EEPROM.get(): The address of the eeprom cell goes beyond the eeprom");
return data;
}
if (sizeof(data) > ((uint32_t)EEPROM_LENGTH - (uint32_t)idx))
{
ErrorMsgHandler("EEPROM.get(): The data goes beyond the eeprom");
return data;
}
uint32_t read_data_buf[EEPROM_PAGE_WORDS] = {};
@ -210,9 +240,7 @@ struct EEPROMClass{
// prepare words
for(uint8_t i = word_idx; i <= lastWord; i++)
{
uint32_t word = read_data_buf[i];
read_data_buf[i] = 0;
read_data_buf[i] = ((word & 0xFF)<<24) | ((word & (0xFF<<8))<<8) | ((word & (0xFF<<16))>>8) | ((word & (0xFF<<24))>>24);
read_data_buf[i] = eeprom_reverse_word(read_data_buf[i]);
}
// read data page by page, first separately read the first page
memcpy((void *)dataPointer, (void*)((uint8_t *)read_data_buf + byte_addr), readSize);
@ -221,21 +249,16 @@ struct EEPROMClass{
while (dataSize > 0)
{
addr += EEPROM_PAGE_SIZE;
// if reaching the eeprom end address, return to the initial address
if (addr == EEPROM_START_ADDR + EEPROM_LENGHT)
addr = EEPROM_START_ADDR;
HAL_read((uint16_t)addr, read_data_buf);
dataShift += readSize;
readSize = dataSize;
if (EEPROM_PAGE_SIZE < dataSize)
readSize = EEPROM_PAGE_SIZE;
lastWord = (dataSize - 1) / EEPROM_WORD_SIZE;
lastWord = (readSize - 1) / EEPROM_WORD_SIZE;
// prepare words
for(uint8_t i = 0; i <= lastWord; i++)
{
uint32_t word = read_data_buf[i];
read_data_buf[i] = 0;
read_data_buf[i] = ((word & 0xFF)<<24) | ((word & (0xFF<<8))<<8) | ((word & (0xFF<<16))>>8) | ((word & (0xFF<<24))>>24);
read_data_buf[i] = eeprom_reverse_word(read_data_buf[i]);
}
memcpy((void *)((uint8_t *)dataPointer + dataShift), (void*)(read_data_buf), readSize);
dataSize -= readSize;
@ -244,7 +267,9 @@ struct EEPROMClass{
}
};
#pragma GCC diagnostic ignored "-Wunused-variable" // for GCC and Clang
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
static EEPROMClass EEPROM;
#pragma GCC diagnostic pop
#endif

View File

@ -126,10 +126,10 @@ tools.mik32_upload.erase.params.quiet=
tools.mik32_upload.erase.params.verbose=
# For Tools > Burn Bootloader
tools.mik32_upload.bootloader.pattern="{path}/{cmd}" "{runtime.platform.path}/bootloaders/{bootloader.file}" --run-openocd --no-driver --openocd-exec "{runtime.tools.openocd.path}/bin/openocd{cmd.extension}" --openocd-interface "{path}/openocd-scripts/interface/{bootloader.interface}" --openocd-target "{path}/openocd-scripts/target/mik32.cfg"
tools.mik32_upload.bootloader.pattern="{path}/{cmd}" "{runtime.platform.path}/bootloaders/{bootloader.file}" --run-openocd {bootloader.driver_option} --openocd-exec "{runtime.tools.openocd.path}/bin/openocd{cmd.extension}" --openocd-interface "{path}/openocd-scripts/interface/{bootloader.interface}" --openocd-target "{path}/openocd-scripts/target/mik32.cfg"
tools.mik32_upload.bootloader.params.quiet=
# For Sketch > Upload Using Programmer
tools.mik32_upload.program.pattern="{path}/{cmd}" "{build.path}/{build.project_name}.hex" --run-openocd --no-driver --openocd-exec "{runtime.tools.openocd.path}/bin/openocd{cmd.extension}" --openocd-interface "{path}/openocd-scripts/interface/{bootloader.interface}" --openocd-target "{path}/openocd-scripts/target/mik32.cfg"
tools.mik32_upload.program.pattern="{path}/{cmd}" "{build.path}/{build.project_name}.hex" --run-openocd {bootloader.driver_option} --openocd-exec "{runtime.tools.openocd.path}/bin/openocd{cmd.extension}" --openocd-interface "{path}/openocd-scripts/interface/{bootloader.interface}" --openocd-target "{path}/openocd-scripts/target/mik32.cfg"
tools.mik32_upload.program.params.quiet=
tools.mik32_upload.program.params.verbose=