forked from letssteam/DAPLink
-
Notifications
You must be signed in to change notification settings - Fork 0
steami: Add config zone in F103 internal flash gap #4
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * @file steami_config.c | ||
| * @brief STeaMi board config zone in internal flash (F103 gap area) | ||
| */ | ||
|
|
||
| #include "steami_config.h" | ||
| #include "stm32f1xx.h" | ||
| #include <string.h> | ||
|
|
||
| /** Shadow buffer used during read-modify-write cycles. */ | ||
| static uint8_t shadow[STEAMI_CONFIG_SIZE] __attribute__((aligned(4))); | ||
|
|
||
| bool steami_config_read(uint16_t offset, uint8_t *buf, uint16_t len) | ||
| { | ||
| if ((uint32_t)offset + len > STEAMI_CONFIG_SIZE) { | ||
| return false; | ||
| } | ||
|
|
||
| /* Config zone is memory-mapped — direct read. */ | ||
| memcpy(buf, (const uint8_t *)(STEAMI_CONFIG_ADDR + offset), len); | ||
| return true; | ||
| } | ||
|
|
||
| bool steami_config_write(uint16_t offset, const uint8_t *data, uint16_t len) | ||
| { | ||
| if ((uint32_t)offset + len > STEAMI_CONFIG_SIZE) { | ||
| return false; | ||
| } | ||
|
|
||
| /* 1. Read current page into shadow buffer. */ | ||
| memcpy(shadow, (const uint8_t *)STEAMI_CONFIG_ADDR, STEAMI_CONFIG_SIZE); | ||
|
|
||
| /* 2. Merge new data into shadow. */ | ||
| memcpy(shadow + offset, data, len); | ||
|
|
||
| /* 3. Erase the page. */ | ||
| FLASH_EraseInitTypeDef erase; | ||
| uint32_t error; | ||
|
|
||
| HAL_FLASH_Unlock(); | ||
|
|
||
| memset(&erase, 0, sizeof(erase)); | ||
| erase.TypeErase = FLASH_TYPEERASE_PAGES; | ||
| erase.PageAddress = STEAMI_CONFIG_ADDR; | ||
| erase.NbPages = 1; | ||
|
|
||
| if (HAL_FLASHEx_Erase(&erase, &error) != HAL_OK) { | ||
| HAL_FLASH_Lock(); | ||
| return false; | ||
| } | ||
|
|
||
| /* 4. Program shadow buffer back (word-by-word). */ | ||
| for (uint32_t i = 0; i < STEAMI_CONFIG_SIZE; i += 4) { | ||
| uint32_t word; | ||
| memcpy(&word, shadow + i, 4); | ||
|
|
||
| /* Skip words that are already erased (0xFFFFFFFF). */ | ||
| if (word == 0xFFFFFFFF) { | ||
| continue; | ||
| } | ||
|
|
||
| if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, | ||
| STEAMI_CONFIG_ADDR + i, word) != HAL_OK) { | ||
| HAL_FLASH_Lock(); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| HAL_FLASH_Lock(); | ||
| return true; | ||
| } | ||
|
|
||
| bool steami_config_erase(void) | ||
| { | ||
| FLASH_EraseInitTypeDef erase; | ||
| uint32_t error; | ||
|
|
||
| HAL_FLASH_Unlock(); | ||
|
|
||
| memset(&erase, 0, sizeof(erase)); | ||
| erase.TypeErase = FLASH_TYPEERASE_PAGES; | ||
| erase.PageAddress = STEAMI_CONFIG_ADDR; | ||
| erase.NbPages = 1; | ||
|
|
||
| if (HAL_FLASHEx_Erase(&erase, &error) != HAL_OK) { | ||
| HAL_FLASH_Lock(); | ||
| return false; | ||
| } | ||
|
|
||
| HAL_FLASH_Lock(); | ||
| return true; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * @file steami_config.h | ||
| * @brief STeaMi board config zone in internal flash (F103 gap area) | ||
| * | ||
| * The config zone occupies the 1 KB gap between the bootloader and the | ||
| * interface firmware (0x0800BC00 - 0x0800BFFF). This area survives | ||
| * interface firmware updates, making it suitable for factory-programmed | ||
| * data such as board revision, name, and sensor calibration. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <stdint.h> | ||
| #include <stdbool.h> | ||
| #include "daplink_addr.h" | ||
| #include "compiler.h" | ||
|
|
||
| /** Start address of the config zone (BL/IF gap). */ | ||
| #define STEAMI_CONFIG_ADDR (DAPLINK_ROM_BL_START + DAPLINK_ROM_BL_SIZE) | ||
|
|
||
| /** Size of the config zone in bytes (one flash page). */ | ||
| #define STEAMI_CONFIG_SIZE DAPLINK_SECTOR_SIZE | ||
|
|
||
| /* Compile-time check: config zone must fit between bootloader and interface. */ | ||
| COMPILER_ASSERT(STEAMI_CONFIG_ADDR + STEAMI_CONFIG_SIZE <= DAPLINK_ROM_IF_START); | ||
|
|
||
| /** | ||
| * @brief Read data from the config zone. | ||
| * | ||
| * The config zone is memory-mapped, so this is a simple memcpy. | ||
| * | ||
| * @param offset Byte offset within the config zone (0 .. STEAMI_CONFIG_SIZE-1) | ||
| * @param buf Destination buffer | ||
| * @param len Number of bytes to read | ||
| * @return true on success, false if offset+len exceeds the zone | ||
| */ | ||
| bool steami_config_read(uint16_t offset, uint8_t *buf, uint16_t len); | ||
|
|
||
| /** | ||
| * @brief Write data to the config zone. | ||
| * | ||
| * Erases the entire 1 KB page then programs the supplied data. | ||
| * A shadow buffer is used to preserve existing content outside | ||
| * the written range. | ||
| * | ||
| * @param offset Byte offset within the config zone | ||
| * @param data Source data | ||
| * @param len Number of bytes to write | ||
| * @return true on success, false on parameter error or flash failure | ||
| */ | ||
| bool steami_config_write(uint16_t offset, const uint8_t *data, uint16_t len); | ||
|
|
||
| /** | ||
| * @brief Erase the entire config zone (fill with 0xFF). | ||
| * | ||
| * @return true on success, false on flash erase failure | ||
| */ | ||
| bool steami_config_erase(void); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.