Skip to content

Commit

Permalink
EEPROM implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
ClemensElflein committed Oct 28, 2024
1 parent 43d8112 commit bf9a30d
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 133 deletions.
2 changes: 1 addition & 1 deletion bootloader/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Compiler options here.
ifeq ($(USE_OPT),)
USE_OPT = -O0 -ggdb -fomit-frame-pointer -falign-functions=16
USE_OPT = -O1 -ggdb -fomit-frame-pointer -falign-functions=16
endif

# C specific options here (added to USE_OPT).
Expand Down
2 changes: 1 addition & 1 deletion bootloader/boards/XCORE/STM32H723xG_ITCM64k.ld
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
MEMORY
{
flash0 (rx) : org = 0x08000000, len = 256k /* Flash bank1 */
flash0 (rx) : org = 0x08000000, len = 128k /* Flash bank1 */
flash1 (rx) : org = 0x00000000, len = 0
flash2 (rx) : org = 0x00000000, len = 0
flash3 (rx) : org = 0x00000000, len = 0
Expand Down
2 changes: 0 additions & 2 deletions bootloader/boards/XCORE/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ static void stm32_gpio_init(void) {
void __early_init(void) {

stm32_gpio_init();
SYSCFG->UR18 = SYSCFG_UR18_CPU_FREQ_BOOST;
stm32_clock_init();
SYSCFG->UR18 = SYSCFG_UR18_CPU_FREQ_BOOST;
}

#if HAL_USE_SDC || defined(__DOXYGEN__)
Expand Down
13 changes: 3 additions & 10 deletions bootloader/boards/XCORE/board_ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,11 @@
#define BOARD_STATUS_LED_INVERTED
#define BOARD_HEARTBEAT_LED_INVERTED

#define BOARD_HAS_EEPROM 0

// Default mac address (same as the one provided in lwip)
#define BOARD_ETHADDR_0 0xC2
#define BOARD_ETHADDR_1 0xAF
#define BOARD_ETHADDR_2 0x51
#define BOARD_ETHADDR_3 0x03
#define BOARD_ETHADDR_4 0xCF
#define BOARD_ETHADDR_5 0x46
#define BOARD_HAS_EEPROM 1
#define EEPROM_DEVICE_ADDRESS 0b1010011

#define BOARD_VERSION_MAJOR 1
#define BOARD_VERSION_MINOR 0
#define BOARD_VERSION_MINOR 1
#define BOARD_VERSION_PATCH 0

// Define the fallback IP settings for this board (if DHCP fails)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
#define STM32_ADCSEL STM32_ADCSEL_PLL3_R_CK
#define STM32_LPTIM345SEL STM32_LPTIM345SEL_PCLK4
#define STM32_LPTIM2SEL STM32_LPTIM2SEL_PCLK4
#define STM32_I2C4SEL STM32_I2C4SEL_PCLK4
#define STM32_I2C4SEL STM32_I2C4SEL_CSI_KER_CK
#define STM32_LPUART1SEL STM32_LPUART1SEL_PCLK4

/*
Expand Down
8 changes: 8 additions & 0 deletions bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ bool jump_to_user_program(void) {
return false;
}

if (info.image_present != 1) {
return false;
}

// Check, if image size is valid
if (info.image_size > PROGRAM_FLASH_SIZE_BYTES) {
return false;
Expand Down Expand Up @@ -80,6 +84,10 @@ int main(void) {
halInit();
chSysInit();

#if BOARD_HAS_EEPROM
ID_EEPROM_Init();
#endif

/*
* InitGlobals() sets up global variables shared by threads. (e.g. mutex)
* InitHeartbeat() starts the heartbeat timer to blink an LED as long as the
Expand Down
33 changes: 29 additions & 4 deletions bootloader/src/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "hal.h"
// clang-format on

#include <id_eeprom.h>
#include <sha256.h>
#include <stdlib.h>
#include <stm32h7xx_hal.h>
Expand Down Expand Up @@ -201,6 +202,15 @@ static THD_FUNCTION(bootloader_thread, arg) {
continue;
}

#if BOARD_HAS_EEPROM
struct bootloader_info info = {0};
if (!ID_EEPROM_SaveBootloaderInfo(&info)) {
SEND(connfd, ">Error clearing EEPROM");
ok = false;
continue;
}
#endif

EraseInitStruct.NbSectors =
(image_size + FLASH_PAGE_SIZE_BYTES - 1) / FLASH_PAGE_SIZE_BYTES;
chsnprintf(out_buffer, sizeof(out_buffer),
Expand Down Expand Up @@ -271,9 +281,9 @@ static THD_FUNCTION(bootloader_thread, arg) {
// This verifies the received bytes
sha256_final(&sha256, validate_hash);
if (memcmp(validate_hash, hash, sizeof(hash)) == 0) {
SEND(connfd, "Receive Verify OK\n");
SEND(connfd, ">Receive Verify OK\n");
} else {
SEND(connfd, "Receive Verify Failed\n");
SEND(connfd, ">Receive Verify Failed\n");
ok = false;
}
}
Expand All @@ -286,12 +296,27 @@ static THD_FUNCTION(bootloader_thread, arg) {
sha256_update(&sha256, (uint8_t *)TARGET_FLASH_ADDRESS, image_size);
sha256_final(&sha256, validate_hash);
if (memcmp(validate_hash, hash, sizeof(hash)) == 0) {
SEND(connfd, "Flash Verify OK\n");
SEND(connfd, ">Flash Verify OK\n");
} else {
SEND(connfd, "Flash Verify Failed\n");
SEND(connfd, ">Flash Verify Failed\n");
ok = false;
}
}

#if BOARD_HAS_EEPROM
if (ok) {
// Store bootloader info
struct bootloader_info info = {0};
info.image_present = 1;
info.image_size = image_size;
memcpy(info.image_sha256, hash, sizeof(hash));
if (!ID_EEPROM_SaveBootloaderInfo(&info)) {
SEND(connfd, ">Error storing EEPROM");
ok = false;
continue;
}
}
#endif

if (ok) {
// Allow reboot into user program
Expand Down
163 changes: 50 additions & 113 deletions bootloader/src/id_eeprom.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
#include "id_eeprom.h"

#include <string.h>

#include "board_ex.h"
#include "ch.h"
#include "hal.h"
static I2CConfig i2cConfig;
static I2CConfig i2cConfig = {0};

void ID_EEPROM_Init() {
#if BOARD_HAS_EEPROM
// TODO: init eeprom
while (1)
;
i2cAcquireBus(&I2CD4);

// Calculated depending on clock source, check reference manual
i2cConfig.timingr = 0xE14;

if (i2cStart(&I2CD4, &i2cConfig) != HAL_RET_SUCCESS) {
while (1)
;
}
i2cReleaseBus(&I2CD4);
#endif
}

bool ID_EEPROM_GetMacAddress(uint8_t *buf, size_t buflen) {
#if BOARD_HAS_EEPROM
// TODO: Implement
return false;
i2cAcquireBus(&I2CD4);

uint8_t reg = 0xFA;
bool success = i2cMasterTransmit(&I2CD4, EEPROM_DEVICE_ADDRESS, &reg, 1, buf,
buflen) == MSG_OK;
i2cReleaseBus(&I2CD4);
return success;
#else
#if !defined(BOARD_ETHADDR_0) || !defined(BOARD_ETHADDR_1) || \
!defined(BOARD_ETHADDR_2) || !defined(BOARD_ETHADDR_3) || \
Expand All @@ -39,8 +54,14 @@ bool ID_EEPROM_GetMacAddress(uint8_t *buf, size_t buflen) {

bool ID_EEPROM_GetBootloaderInfo(struct bootloader_info *buffer) {
#if BOARD_HAS_EEPROM
// TODO: implement
return false;
i2cAcquireBus(&I2CD4);

uint8_t reg = 0x00;
bool success = i2cMasterTransmit(&I2CD4, EEPROM_DEVICE_ADDRESS, &reg, 1,
(uint8_t *)buffer,
sizeof(struct bootloader_info)) == MSG_OK;
i2cReleaseBus(&I2CD4);
return success;
#else
// no eeprom - we don't store anything (the board will just try to boot
// without checking).
Expand All @@ -51,116 +72,32 @@ bool ID_EEPROM_GetBootloaderInfo(struct bootloader_info *buffer) {

bool ID_EEPROM_SaveBootloaderInfo(struct bootloader_info *buffer) {
#if BOARD_HAS_EEPROM
// TODO: implement
return false;
i2cAcquireBus(&I2CD4);

bool success = true;
// Write single bytes
for (uint8_t i = 0; success && i < sizeof(struct bootloader_info); i++) {
uint8_t data[2] = {i, ((uint8_t *)buffer)[i]};
success &= i2cMasterTransmit(&I2CD4, EEPROM_DEVICE_ADDRESS, data, 2, NULL,
0) == MSG_OK;
// Wait for write to finish
uint8_t dummy = 0;
while (i2cMasterTransmit(&I2CD4, EEPROM_DEVICE_ADDRESS, &dummy, 1, NULL,
0) == MSG_RESET) {
if (i2cGetErrors(&I2CD4) != I2C_ACK_FAILURE) {
success = false;
break;
}
chThdSleep(1);
}
}

i2cReleaseBus(&I2CD4);
return success;
#else
// no eeprom - we don't store anything (the board will just try to boot
// without checking).
return false;
#endif
return true;
}

/*//
// Created by clemens on 7/7/24.
//
#include <FastCRC.h>
#include <stm32h7xx_hal.h>
#include <tx_api.h>
#include "board.h"
#include "id_eeprom.h"
I2C_HandleTypeDef hi2c4;
TX_MUTEX eeprom_mutex;
FastCRC32 CRC32;
void Board::ID::Init() {
tx_mutex_create(&eeprom_mutex, "EEPROM", 0);
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
/* USER CODE BEGIN I2C4_MspInit 0 #1#
/* USER CODE END I2C4_MspInit 0 #1#
/** Initializes the peripherals clock
#1#
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C4;
PeriphClkInitStruct.I2c4ClockSelection = RCC_I2C4CLKSOURCE_D3PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
Error_Handler();
}
__HAL_RCC_GPIOD_CLK_ENABLE();
/**I2C4 GPIO Configuration
PD12 ------> I2C4_SCL
PD13 ------> I2C4_SDA
#1#
GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C4;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/* I2C4 clock enable #1#
__HAL_RCC_I2C4_CLK_ENABLE();
/* USER CODE BEGIN I2C4_MspInit 1 #1#
hi2c4.Instance = I2C4;
hi2c4.Init.Timing = 0x60404E72;
hi2c4.Init.OwnAddress1 = 0;
hi2c4.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c4.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c4.Init.OwnAddress2 = 0;
hi2c4.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c4.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c4.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c4) != HAL_OK) {
Error_Handler();
}
/** Configure Analogue filter
#1#
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c4, I2C_ANALOGFILTER_ENABLE) != HAL_OK) {
Error_Handler();
}
/** Configure Digital filter
#1#
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c4, 0) != HAL_OK) {
Error_Handler();
}
}
bool Board::ID::GetMacAddress(void* buf, size_t buflen) {
if (buflen < 6) return false;
tx_mutex_get(&eeprom_mutex, TX_WAIT_FOREVER);
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(
&hi2c4, eeprom_device_address, 0xFA, I2C_MEMADD_SIZE_8BIT,
static_cast<uint8_t*>(buf), 6, 1000);
tx_mutex_put(&eeprom_mutex);
return status == HAL_OK;
}
bool Board::ID::SaveBootloaderInfo(struct bootloader_info* buffer) {
tx_mutex_get(&eeprom_mutex, TX_WAIT_FOREVER);
buffer->crc = CRC32.crc32((uint8_t*)buffer,
sizeof(struct bootloader_info) - sizeof(uint32_t));
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(
&hi2c4, eeprom_device_address, 0x00, I2C_MEMADD_SIZE_8BIT,
(uint8_t*)(buffer), sizeof(struct bootloader_info), 1000);
tx_mutex_put(&eeprom_mutex);
return status == HAL_OK;
}
bool Board::ID::GetBootloaderInfo(struct bootloader_info* buffer) {
tx_mutex_get(&eeprom_mutex, TX_WAIT_FOREVER);
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(
&hi2c4, eeprom_device_address, 0x00, I2C_MEMADD_SIZE_8BIT,
(uint8_t*)(buffer), sizeof(struct bootloader_info), 1000);
uint32_t expected_crc = CRC32.crc32(
(uint8_t*)buffer, sizeof(struct bootloader_info) - sizeof(uint32_t));
tx_mutex_put(&eeprom_mutex);
return status == HAL_OK && expected_crc == buffer->crc;
}*/
1 change: 0 additions & 1 deletion bootloader/src/id_eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ struct bootloader_info {
uint8_t image_present;
uint32_t image_size;
uint32_t image_sha256[8];
uint32_t crc;
} __attribute__((packed));
#pragma pack(pop)

Expand Down

0 comments on commit bf9a30d

Please sign in to comment.