Skip to content

Commit

Permalink
[H7] Split config streamer code into CPU-specific implementations; Ti…
Browse files Browse the repository at this point in the history
…mer defines; IO fixes

[H7] Fix broken F3, F4, F7 builds
  • Loading branch information
digitalentity committed Jun 18, 2020
1 parent 39e76a5 commit 3de6c37
Show file tree
Hide file tree
Showing 26 changed files with 983 additions and 317 deletions.
1 change: 1 addition & 0 deletions make/mcu/STM32F3.mk
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ VCP_SRC = \


MCU_COMMON_SRC = \
config/config_streamer_stm32f3.c \
startup_stm32f30x_md_gcc.S \
target/system_stm32f30x.c \
drivers/accgyro/accgyro.c \
Expand Down
1 change: 1 addition & 0 deletions make/mcu/STM32F4.mk
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ endif
DEVICE_FLAGS += -DHSE_VALUE=$(HSE_VALUE)

MCU_COMMON_SRC = \
config/config_streamer_stm32f4.c \
target/system_stm32f4xx.c \
drivers/accgyro/accgyro.c \
drivers/accgyro/accgyro_mpu.c \
Expand Down
2 changes: 1 addition & 1 deletion make/mcu/STM32F7.mk
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ TARGET_FLAGS = -D$(TARGET)
VCP_SRC = \
vcp_hal/usbd_desc.c \
vcp_hal/usbd_conf_stm32f7xx.c \
vcp_hal/usbd_conf.c \
vcp_hal/usbd_cdc_interface.c \
drivers/serial_usb_vcp.c \
drivers/usb_io.c

MCU_COMMON_SRC = \
config/config_streamer_stm32f7.c \
target/system_stm32f7xx.c \
drivers/accgyro/accgyro.c \
drivers/accgyro/accgyro_mpu.c \
Expand Down
1 change: 1 addition & 0 deletions make/mcu/STM32H7.mk
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ VCP_SRC = \
drivers/usb_io.c

MCU_COMMON_SRC = \
config/config_streamer_stm32h7.c \
target/system_stm32h7xx.c \
drivers/memprot_hal.c \
drivers/memprot_stm32h7xx.c \
Expand Down
2 changes: 2 additions & 0 deletions make/openocd.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ else ifeq ($(TARGET_MCU_GROUP),STM32F4)
OPENOCD_TARGET := stm32f4x
else ifeq ($(TARGET_MCU_GROUP),STM32F7)
OPENOCD_TARGET := stm32f7x
else ifeq ($(TARGET_MCU_GROUP),STM32H7)
OPENOCD_TARGET := stm32h7x
endif
endif

Expand Down
1 change: 0 additions & 1 deletion src/main/config/config_eeprom.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "build/build_config.h"

#include "common/crc.h"
#include "common/utils.h"

#include "config/config_eeprom.h"
#include "config/config_streamer.h"
Expand Down
277 changes: 8 additions & 269 deletions src/main/config/config_streamer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,14 @@
*/

#include <string.h>

#include "platform.h"

#include "drivers/system.h"

#include "config/config_streamer.h"

#if !defined(CONFIG_IN_FLASH)
#if defined(CONFIG_IN_RAM) && defined(PERSISTENT)
PERSISTENT uint8_t eepromData[EEPROM_SIZE];
#else
uint8_t eepromData[EEPROM_SIZE];
#endif
#endif

#if defined(STM32H750xx) && !(defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD))
#error "STM32750xx only has one flash page which contains the bootloader, no spare flash pages available, use external storage for persistent config or ram for target testing"
#endif

#if !defined(FLASH_PAGE_SIZE)
// F1
# if defined(STM32F303xC)
# define FLASH_PAGE_SIZE (0x800)
// F4
# elif defined(STM32F40_41xxx)
# define FLASH_PAGE_SIZE ((uint32_t)0x4000)
# elif defined (STM32F411xE)
# define FLASH_PAGE_SIZE ((uint32_t)0x4000)
# elif defined(STM32F446xx)
# define FLASH_PAGE_SIZE ((uint32_t)0x4000)
# elif defined(STM32F427_437xx)
# define FLASH_PAGE_SIZE ((uint32_t)0x4000)
// F7
#elif defined(STM32F722xx)
# define FLASH_PAGE_SIZE ((uint32_t)0x4000) // 16K sectors
# elif defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
# define FLASH_PAGE_SIZE ((uint32_t)0x8000)
# elif defined(UNIT_TEST)
# define FLASH_PAGE_SIZE (0x400)
// H7
# elif defined(STM32H743xx) || defined(STM32H750xx)
# define FLASH_PAGE_SIZE ((uint32_t)0x20000) // 128K sectors
# else
# error "Flash page size not defined for target."
# endif
#endif
// Helper functions
extern void config_streamer_impl_unlock(void);
extern void config_streamer_impl_lock(void);
extern int config_streamer_impl_write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer);

void config_streamer_init(config_streamer_t *c)
{
Expand All @@ -74,234 +36,19 @@ void config_streamer_start(config_streamer_t *c, uintptr_t base, int size)
c->address = base;
c->size = size;
if (!c->unlocked) {
#if defined(CONFIG_IN_RAM)
// NOP
#elif defined(CONFIG_IN_FLASH)
#if defined(STM32F7)
HAL_FLASH_Unlock();
#else
FLASH_Unlock();
#endif
#endif
config_streamer_impl_unlock();
c->unlocked = true;
}

#if defined(CONFIG_IN_RAM)
// NOP
#elif defined(CONFIG_IN_FLASH)
#if defined(STM32F303)
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
#elif defined(STM32F4)
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
#elif defined(STM32F7)
// NOP
#elif defined(UNIT_TEST)
// NOP
#else
# error "Unsupported CPU"
#endif
#endif
c->err = 0;
}

#if defined(CONFIG_IN_RAM)
// No flash sector method required.

#if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
/*
Sector 0 0x08000000 - 0x08007FFF 32 Kbytes
Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes
Sector 2 0x08010000 - 0x08017FFF 32 Kbytes
Sector 3 0x08018000 - 0x0801FFFF 32 Kbytes
Sector 4 0x08020000 - 0x0803FFFF 128 Kbytes
Sector 5 0x08040000 - 0x0807FFFF 256 Kbytes
Sector 6 0x08080000 - 0x080BFFFF 256 Kbytes
Sector 7 0x080C0000 - 0x080FFFFF 256 Kbytes
*/

static uint32_t getFLASHSectorForEEPROM(void)
{
if ((uint32_t)&__config_start <= 0x08007FFF)
return FLASH_SECTOR_0;
if ((uint32_t)&__config_start <= 0x0800FFFF)
return FLASH_SECTOR_1;
if ((uint32_t)&__config_start <= 0x08017FFF)
return FLASH_SECTOR_2;
if ((uint32_t)&__config_start <= 0x0801FFFF)
return FLASH_SECTOR_3;
if ((uint32_t)&__config_start <= 0x0803FFFF)
return FLASH_SECTOR_4;
if ((uint32_t)&__config_start <= 0x0807FFFF)
return FLASH_SECTOR_5;
if ((uint32_t)&__config_start <= 0x080BFFFF)
return FLASH_SECTOR_6;
if ((uint32_t)&__config_start <= 0x080FFFFF)
return FLASH_SECTOR_7;

// Not good
while (1) {
failureMode(FAILURE_FLASH_WRITE_FAILED);
}
}

#elif defined(STM32F722xx)
/*
Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
*/

static uint32_t getFLASHSectorForEEPROM(void)
{
if ((uint32_t)&__config_start <= 0x08003FFF)
return FLASH_SECTOR_0;
if ((uint32_t)&__config_start <= 0x08007FFF)
return FLASH_SECTOR_1;
if ((uint32_t)&__config_start <= 0x0800BFFF)
return FLASH_SECTOR_2;
if ((uint32_t)&__config_start <= 0x0800FFFF)
return FLASH_SECTOR_3;
if ((uint32_t)&__config_start <= 0x0801FFFF)
return FLASH_SECTOR_4;
if ((uint32_t)&__config_start <= 0x0803FFFF)
return FLASH_SECTOR_5;
if ((uint32_t)&__config_start <= 0x0805FFFF)
return FLASH_SECTOR_6;
if ((uint32_t)&__config_start <= 0x0807FFFF)
return FLASH_SECTOR_7;

// Not good
while (1) {
failureMode(FAILURE_FLASH_WRITE_FAILED);
}
}

#elif defined(STM32F4)
/*
Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
Sector 8 0x08080000 - 0x0809FFFF 128 Kbytes
Sector 9 0x080A0000 - 0x080BFFFF 128 Kbytes
Sector 10 0x080C0000 - 0x080DFFFF 128 Kbytes
Sector 11 0x080E0000 - 0x080FFFFF 128 Kbytes
*/

static uint32_t getFLASHSectorForEEPROM(void)
{
if ((uint32_t)&__config_start <= 0x08003FFF)
return FLASH_Sector_0;
if ((uint32_t)&__config_start <= 0x08007FFF)
return FLASH_Sector_1;
if ((uint32_t)&__config_start <= 0x0800BFFF)
return FLASH_Sector_2;
if ((uint32_t)&__config_start <= 0x0800FFFF)
return FLASH_Sector_3;
if ((uint32_t)&__config_start <= 0x0801FFFF)
return FLASH_Sector_4;
if ((uint32_t)&__config_start <= 0x0803FFFF)
return FLASH_Sector_5;
if ((uint32_t)&__config_start <= 0x0805FFFF)
return FLASH_Sector_6;
if ((uint32_t)&__config_start <= 0x0807FFFF)
return FLASH_Sector_7;
if ((uint32_t)&__config_start <= 0x0809FFFF)
return FLASH_Sector_8;
if ((uint32_t)&__config_start <= 0x080DFFFF)
return FLASH_Sector_9;
if ((uint32_t)&__config_start <= 0x080BFFFF)
return FLASH_Sector_10;
if ((uint32_t)&__config_start <= 0x080FFFFF)
return FLASH_Sector_11;

// Not good
while (1) {
failureMode(FAILURE_FLASH_WRITE_FAILED);
}
}
#endif
#endif // CONFIG_IN_FLASH

static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer)
{
if (c->err != 0) {
return c->err;
}
#if defined(CONFIG_IN_RAM)
if (c->address == (uintptr_t)&eepromData[0]) {
memset(eepromData, 0, sizeof(eepromData));
}

uint64_t *dest_addr = (uint64_t *)c->address;
uint64_t *src_addr = (uint64_t*)buffer;
uint8_t row_index = 4;
/* copy the 256 bits flash word */
do
{
*dest_addr++ = *src_addr++;
} while (--row_index != 0);

#elif defined(CONFIG_IN_FLASH)

#if defined(STM32F7)
if (c->address % FLASH_PAGE_SIZE == 0) {
FLASH_EraseInitTypeDef EraseInitStruct = {
.TypeErase = FLASH_TYPEERASE_SECTORS,
.VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V
.NbSectors = 1
};
EraseInitStruct.Sector = getFLASHSectorForEEPROM();
uint32_t SECTORError;
const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
if (status != HAL_OK){
return -1;
}
}

// For F7
// HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, c->address, (uint64_t)*buffer);
if (status != HAL_OK) {
return -2;
}
#else
if (c->address % FLASH_PAGE_SIZE == 0) {
#if defined(STM32F4)
const FLASH_Status status = FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3); //0x08080000 to 0x080A0000
#else
const FLASH_Status status = FLASH_ErasePage(c->address);
#endif
if (status != FLASH_COMPLETE) {
return -1;
}
}
const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer);
if (status != FLASH_COMPLETE) {
return -2;
}
#endif
#endif
c->address += CONFIG_STREAMER_BUFFER_SIZE;
return 0;
}

int config_streamer_write(config_streamer_t *c, const uint8_t *p, uint32_t size)
{
for (const uint8_t *pat = p; pat != (uint8_t*)p + size; pat++) {
c->buffer.b[c->at++] = *pat;

if (c->at == sizeof(c->buffer)) {
c->err = write_word(c, &c->buffer.w);
c->err = config_streamer_impl_write_word(c, &c->buffer.w);
c->at = 0;
}
}
Expand All @@ -317,7 +64,7 @@ int config_streamer_flush(config_streamer_t *c)
{
if (c->at != 0) {
memset(c->buffer.b + c->at, 0, sizeof(c->buffer) - c->at);
c->err = write_word(c, &c->buffer.w);
c->err = config_streamer_impl_write_word(c, &c->buffer.w);
c->at = 0;
}
return c-> err;
Expand All @@ -326,15 +73,7 @@ int config_streamer_flush(config_streamer_t *c)
int config_streamer_finish(config_streamer_t *c)
{
if (c->unlocked) {
#if defined(CONFIG_IN_RAM)
// NOP
#elif defined(CONFIG_IN_FLASH)
#if defined(STM32F7)
HAL_FLASH_Lock();
#else
FLASH_Lock();
#endif
#endif
config_streamer_impl_lock();
c->unlocked = false;
}
return c->err;
Expand Down
1 change: 1 addition & 0 deletions src/main/config/config_streamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef uint64_t config_streamer_buffer_align_type_t;
#define CONFIG_STREAMER_BUFFER_SIZE 4
typedef uint32_t config_streamer_buffer_align_type_t;
#endif

typedef struct config_streamer_s {
uintptr_t address;
int size;
Expand Down

0 comments on commit 3de6c37

Please sign in to comment.