Skip to content

Commit 4b15bfc

Browse files
author
Richard Unger
committed
fix bugs in STM32G4 settings storage
1 parent eb2fc66 commit 4b15bfc

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/settings/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Supported hardware:
1919
Some SAMD chips have a built-in NVRAM flash, which can be written from user code.
2020
- [CAT24 I2C Flash EEPROMs](i2c/) :warning: UNFINISHED \
2121
Simple programmable memories with I2C interface. Memory space is quite limited, but enough for storing settings.
22-
- [STM32 Flash](stm32/) :warning: UNFINISHED \
23-
Store settings directly to STM32 on-board flash
22+
- [STM32G4 Flash](stm32/) \
23+
Store settings directly to STM32G4 MCU's on-board flash.
2424

2525
See Roadmap, below, for systems we may support in the future.
2626

src/settings/stm32/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ of space, maximum. If you need more space than this, you can provide the start a
1212

1313
Needless to say, you can only store settings to flash if that memory is not also needed by your program. So you have to have some reserve space in the flash of the MCU you're using. If memory is too tight, use a STM32 model with more flash.
1414

15+
## Compatibility
16+
17+
Currently only **STM32G4** MCUs are supported.
18+
19+
G4s are nice MCUs for using the flash because the page size is 2kB and you can erase the flash page by page. For this reason the default settings use the last page of flash as the storage location, leaving all the rest of the flash for your code and data. Pages are 2kB in size, which should be more than enough for most setups.
20+
21+
Note: systems with dual bank flash have not been tested, and will probably not work.
22+
1523
## Board setup
1624

1725
When storing settings to the Flash of your STM32 MCU, you have to consider what happens when you reprogram the MCU. Unless you have taken steps in the board setup files to reserve the pages of flash used by the settings, they will generally be erased and/or overwritten when programming the MCU.

src/settings/stm32/STM32FlashSettingsStorage.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "./STM32FlashSettingsStorage.h"
44

5-
#if defined(_STM32_DEF_)
5+
#if defined(STM32G4xx)
66

77
#include "communication/SimpleFOCDebug.h"
88

@@ -38,23 +38,25 @@ void STM32FlashSettingsStorage::beforeSave(){
3838
_page = PAGE_OF(_writeptr);
3939
if (HAL_FLASH_Unlock()!=HAL_OK)
4040
SimpleFOCDebug::println("SS: Flash unlock err");
41-
delay(50);
41+
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
4242
erasePage(_page);
43-
delay(50);
4443
};
4544

4645

4746
void STM32FlashSettingsStorage::erasePage(uint32_t page) {
4847
FLASH_EraseInitTypeDef eraseInit;
4948
eraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
5049
eraseInit.Page = page;
51-
eraseInit.Banks = _bank;
50+
eraseInit.Banks = 0;//_bank;
5251
eraseInit.NbPages = 1;
5352
uint32_t err;
5453
SimpleFOCDebug::print("SS: erase page ");
5554
SimpleFOCDebug::println((int)page);
55+
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
5656
if (HAL_FLASHEx_Erase(&eraseInit, &err) != HAL_OK) {
57-
SimpleFOCDebug::println("SS: flash erase err");
57+
uint32_t ferr = HAL_FLASH_GetError();
58+
SimpleFOCDebug::print("SS: flash erase err nr ");
59+
SimpleFOCDebug::println((int)ferr);
5860
HAL_FLASH_Lock();
5961
return;
6062
}
@@ -67,8 +69,12 @@ void STM32FlashSettingsStorage::flushBuffer() {
6769
erasePage(page);
6870
_page = page;
6971
}
70-
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, (uint32_t)_writeptr, _writeBuffer.l)!=HAL_OK)
71-
SimpleFOCDebug::println("SS: Flash write err");
72+
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
73+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, (uint32_t)_writeptr, _writeBuffer.l)!=HAL_OK) {
74+
uint32_t ferr = HAL_FLASH_GetError();
75+
SimpleFOCDebug::println("SS: flash write err nr ");
76+
SimpleFOCDebug::println((int)ferr);
77+
}
7278
_writeptr += 8;
7379
_buffed = 0;
7480
_writeBuffer.l = 0;

src/settings/stm32/STM32FlashSettingsStorage.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "../SettingsStorage.h"
55

6-
#if defined(_STM32_DEF_)
6+
#if defined(STM32G4xx)
77

88
#define PAGE_OF(x) (((uint32_t)x - FLASH_BASE) / FLASH_PAGE_SIZE)
99

@@ -15,8 +15,8 @@ class STM32FlashSettingsStorage : public SettingsStorage {
1515

1616
void init() override;
1717

18-
uint32_t _bank = FLASH_BANK_1;
19-
18+
uint32_t _bank = FLASH_BANK_1; // TODO also support bank 2
19+
2020
protected:
2121
uint8_t readByte(uint8_t* valueToSet) override;
2222
uint8_t readFloat(float* valueToSet) override;
@@ -41,7 +41,7 @@ class STM32FlashSettingsStorage : public SettingsStorage {
4141
uint8_t* _writeptr;
4242
uint8_t _buffed = 0;
4343
uint32_t _page = 0;
44-
union {
44+
__attribute__((aligned(8))) union {
4545
uint8_t b[8];
4646
uint64_t l;
4747
} _writeBuffer; // 64-bit buffer

0 commit comments

Comments
 (0)