Skip to content

steami: Add config zone commands (WRITE/READ/CLEAR_CONFIG).#3

Open
nedseb wants to merge 2 commits intorelease_letssteamfrom
feat/config-zone
Open

steami: Add config zone commands (WRITE/READ/CLEAR_CONFIG).#3
nedseb wants to merge 2 commits intorelease_letssteamfrom
feat/config-zone

Conversation

@nedseb
Copy link
Copy Markdown

@nedseb nedseb commented Mar 16, 2026

Summary

Reserve a 4 KB config zone in the W25Q64JV flash at 0x7FE000 for persistent configuration storage (calibration, board info, etc.). Add 3 new I2C commands.

Flash layout

0x000000 - 0x7FDFFF : Data zone (~8 MB, unchanged)
0x7FE000 - 0x7FEFFF : Config zone (4 KB, new)
0x7FF000 - 0x7FFFFF : Filename (unchanged)

STEAMI_FLASH_FILE_SIZE reduced from 8384512 to 8380416 to exclude config zone.

New I2C commands

Command Code Argument Response Description
WRITE_CONFIG 0x30 offset_hi, offset_lo, len, data[] -- Write to config zone
READ_CONFIG 0x31 offset_hi, offset_lo 256 bytes Read 256 bytes from config zone
CLEAR_CONFIG 0x32 -- -- Erase config zone (4K sector erase)

Key behavior

  • CLEAR_FLASH (0x10) does NOT erase the config zone (data zone stops at 0x7FDFFF)
  • CLEAR_CONFIG (0x32) only erases the 4 KB config sector
  • Config and data are fully independent

Files modified

  • steami_i2c.h -- 3 new enum values
  • steami_i2c.c -- command validation + argument byte counts
  • steami_flash.h -- STEAMI_FLASH_CONFIG_ADDR, 3 function prototypes, adjusted FILE_SIZE
  • steami_flash.c -- erase_config(), write_config(), read_config() implementations
  • steami32.c -- 3 new tasks + callback handlers + process_task cases

Test script

"""Test the 3 config zone commands on hardware."""

from machine import I2C
from time import sleep_ms

i2c = I2C(1)
addr = 0x3B

def read_status():
    return i2c.readfrom_mem(addr, 0x80, 1)[0]

def read_error():
    return i2c.readfrom_mem(addr, 0x81, 1)[0]

def wait_busy():
    for _ in range(200):
        if not (read_status() & 0x80):
            return
        sleep_ms(5)
    raise OSError("busy timeout")

print("WHO_AM_I:", hex(i2c.readfrom_mem(addr, 0x01, 1)[0]))

# --- Test 1: CLEAR_CONFIG (0x32) ---
print("\n--- CLEAR_CONFIG ---")
wait_busy()
buf = bytearray([0x32])
i2c.writeto(addr, buf)
sleep_ms(200)
wait_busy()
print("ERROR after clear:", hex(read_error()))

# --- Test 2: WRITE_CONFIG (0x30) ---
print("\n--- WRITE_CONFIG ---")
data = b'{"test": 42}'
# offset=0x0000, len=12
wait_busy()
payload = bytearray([0x30, 0x00, 0x00, len(data)]) + data
# Pad to 31+1=32 bytes (cmd + 31 arg bytes)
while len(payload) < 32:
    payload.append(0x00)
i2c.writeto(addr, payload)
sleep_ms(200)
wait_busy()
print("ERROR after write:", hex(read_error()))

# --- Test 3: READ_CONFIG (0x31) ---
print("\n--- READ_CONFIG ---")
wait_busy()
i2c.writeto_mem(addr, 0x31, bytes([0x00, 0x00]))
sleep_ms(200)
result = i2c.readfrom(addr, 256)
# Find end of data
end = 256
for i in range(256):
    if result[i] == 0xFF:
        end = i
        break
print("Read {} bytes: {}".format(end, bytes(result[:end])))
print("ERROR after read:", hex(read_error()))

# --- Test 4: CLEAR_FLASH should NOT erase config ---
print("\n--- CLEAR_FLASH preserves config ---")
wait_busy()
buf = bytearray([0x10])
i2c.writeto(addr, buf)
sleep_ms(1000)
wait_busy()
# Read config again
i2c.writeto_mem(addr, 0x31, bytes([0x00, 0x00]))
sleep_ms(200)
result = i2c.readfrom(addr, 256)
end = 256
for i in range(256):
    if result[i] == 0xFF:
        end = i
        break
print("Config after CLEAR_FLASH: {}".format(bytes(result[:end])))
preserved = bytes(result[:end]) == b'{"test": 42}'
print("Preserved:", preserved)

print("\nAll done!")

Test plan

  • Build firmware
  • Flash and verify WHO_AM_I still works
  • WRITE_CONFIG + READ_CONFIG round-trip
  • CLEAR_CONFIG erases config zone
  • CLEAR_FLASH does NOT erase config zone
  • Write data file, verify config is preserved

Closes #2

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fa805b35a7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@nedseb nedseb requested a review from Copilot March 16, 2026 15:53
@nedseb nedseb self-assigned this Mar 16, 2026
@nedseb nedseb added the enhancement New feature or request label Mar 16, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a dedicated 4 KB “config zone” at 0x7FE000 in the W25Q64JV flash and exposes it via new I2C commands so persistent board configuration (calibration, board info, etc.) can be stored independently of the main data/file region.

Changes:

  • Introduces new I2C commands: WRITE_CONFIG (0x30), READ_CONFIG (0x31), CLEAR_CONFIG (0x32).
  • Reserves flash address range 0x7FE000–0x7FEFFF for config and reduces STEAMI_FLASH_FILE_SIZE accordingly.
  • Implements flash helpers to erase/write/read the config zone and wires them into the task/command processing loop.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
source/board/steami/steami_i2c.h Adds new command enum values for config operations.
source/board/steami/steami_i2c.c Extends command validation and argument-length mapping for new commands.
source/board/steami/steami_flash.h Defines config zone address/size and declares config read/write/erase APIs; adjusts file size.
source/board/steami/steami_flash.c Implements config zone erase/write/read helpers on top of W25Q64 primitives.
source/board/steami/steami32.c Adds new tasks and command handlers to execute config operations via I2C.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nedseb nedseb force-pushed the feat/config-zone branch from fa805b3 to 14ed7ca Compare March 16, 2026 16:13
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 14ed7cabd0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +7 to +10
#define STEAMI_FLASH_CONFIG_ADDR (uint32_t)0x007F0000
#define STEAMI_FLASH_NAME_ADDR (uint32_t)0x007FF000

#define STEAMI_FLASH_FILE_SIZE 8384512
#define STEAMI_FLASH_FILE_SIZE 8323072 /* 0x7F0000 - 0x000000 */
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Move config region to end-of-flash 4KB window

STEAMI_FLASH_CONFIG_SIZE declares a 4KB config partition, but STEAMI_FLASH_CONFIG_ADDR is set to 0x007F0000 while the filename sector is at 0x007FF000, which leaves an unintended 0xF000 (60KB) gap that is no longer part of STEAMI_FLASH_FILE_SIZE. This silently reduces usable data capacity far beyond the intended 4KB reservation and makes addresses 0x7F1000..0x7FEFFF unreachable by file operations.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: the config zone has been moved to 0x007FE000 (last 4K before the filename sector at 0x7FF000), eliminating the gap. File region now starts at 0x00000000 and extends to 0x7FE000, preserving full data capacity.

@nedseb nedseb force-pushed the feat/config-zone branch from 14ed7ca to 4d5773c Compare March 16, 2026 18:27
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4d5773c5eb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@nedseb nedseb force-pushed the feat/config-zone branch from 4d5773c to a954f62 Compare March 16, 2026 18:34
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b93c219197

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

for(uint16_t i = 0; i < STEAMI_FLASH_MAX_DATA_SIZE; ++i){
if( data[i] == 0xFF ){
return STEAMI_FLASH_FILE_ADDR + offset + i;
return offset + i;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 Badge Restore file-base offset before programming appended data

Changing steami_flash_get_size() to return offset + i makes the size relative to the file partition, but steami_flash_append_file() still uses that value as an absolute flash address in w25q64_page_program. With STEAMI_FLASH_FILE_ADDR now non-zero, WRITE_DATA writes into 0x000000 (config area) while reads/size scans start at 0x001000, so appended file data is not visible in the MSC file and config bytes can be corrupted.

Useful? React with 👍 / 👎.

#define STEAMI_FLASH_NAME_ADDR (uint32_t)0x007FF000
#define STEAMI_FLASH_CONFIG_ADDR (uint32_t)0x00000000
#define STEAMI_FLASH_CONFIG_SIZE 4096
#define STEAMI_FLASH_FILE_ADDR (uint32_t)0x00001000 /* after config zone */
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Align file partition start with block erase operations

Moving STEAMI_FLASH_FILE_ADDR to 0x00001000 makes the data region unaligned for the existing 32KB/64KB erase path in steami_flash_erase(). For files of at least 32KB, the first block erase is issued at 0x1000, which on W25Q64 erases the containing aligned block starting at 0x0000; that includes the config sector and breaks the new guarantee that CLEAR_FLASH does not erase config data.

Useful? React with 👍 / 👎.

- Add page-boundary and length validation in steami_flash_write_config
- Fix steami_flash_get_size returning absolute address instead of relative size
- Separate bad-parameters error from flash-write failure in TASK_WRITE_CONFIG
- Update doc to reflect actual 28-byte max payload via I2C protocol
- Document fixed 31-byte frame layout for WRITE_CONFIG command
@nedseb nedseb force-pushed the feat/config-zone branch from b93c219 to 5770096 Compare March 16, 2026 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

steami: Add config zone in F103 internal flash gap.

2 participants