Skip to content

[Feature] Adds support for METADATA for the spin board - #157

Merged
luizvilla merged 7 commits into
owntech-foundation:mainfrom
luizvilla:metaData_PR
Jul 29, 2026
Merged

[Feature] Adds support for METADATA for the spin board#157
luizvilla merged 7 commits into
owntech-foundation:mainfrom
luizvilla:metaData_PR

Conversation

@luizvilla

Copy link
Copy Markdown
Member

Context

The spin board can hold information on its flash via the nvs driver in the OwnTech API. This can be leveraged to store important data onto the spin board indefinetly.

PR description

This pull request uses the nvs driver to write a certain number of parameters to the ship board in its NVS. These parameters can be used to store metadata. The current metadata stored is:

Metadata layout

All fields are stored under the BOARD_METADATA NVS category (0x0400).
Serial numbers and the shield password are stored as fixed-size raw ASCII
data without a null terminator. Version components and extra data are stored
as raw bytes.

Metadata field NVS key Stored format Stored size Set API Get API
Spin serial number 0x0400 Raw ASCII 13 bytes setSpinSerialNumber() getSpinSerialNumber()
Shield serial number 0x0401 Raw ASCII 13 bytes setShieldSerialNumber() getShieldSerialNumber()
Spin hardware version 0x0402 Three uint8_t values: major, minor, revision 3 bytes setSpinVersion() getSpinVersion()
Shield hardware version 0x0403 Three uint8_t values: major, minor, revision 3 bytes setShieldVersion() getShieldVersion()
Shield password 0x0404 Raw ASCII, stored in plain text 3 bytes setShieldPassword() getShieldPassword()
Extra data slots 0–4 0x04050x0409 Opaque byte data Up to 16 bytes per slot setExtraData() getExtraData()

Testing

This PR can be tested directly by checking out the branch metaData from Luiz Villa's fork:

https://github.com/luizvilla/Core/tree/metaData

You will be able to see that metaData and metaData_PR follow each other. the metaData branch only has the main.cpp implementation and the test_metadata_nvs.py test code.

Luiz Villa and others added 4 commits July 29, 2026 16:27
Checklist of the planned commit sequence so an interrupted implementation
can resume from where it stopped.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Adds a new NVS category for board/shield metadata (spin.metaData) and a
nvs_storage_get_free_space() helper to measure remaining budget in the
shared 4KB storage partition, since ADC calibration and safety thresholds
already compete for that space.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Adds spin/shield serial numbers, spin/shield versions (major/minor/rev),
a shield password, and 5 generic extra slots on top of the shared
nvs_storage_store_data/retrieve_data API, following the same one-key-per-
field pattern already used for ADC calibration and safety thresholds.
Not yet wired into the public spin.metaData surface.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Wires the new MetaDataAPI class into SpinAPI following the same static-
member composition pattern used for spin.data, spin.gpio, etc.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@luizvilla
luizvilla requested a review from Ayoub-Farah July 29, 2026 14:37
@luizvilla luizvilla self-assigned this Jul 29, 2026

@Ayoub-Farah Ayoub-Farah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have tested the code in https://github.com/luizvilla/Core/tree/metaData/src, here is the result after using the python file :

& C:\Users\afarahhass\AppData\Local\Programs\Python\Python312\python.exe `  .\src\test_metadata_nvs.py --port COM29                                
Using board 58435001004B001B on port COM29
[1/6] Clearing all metadata fields...
  CLEAR=OK
[2/6] Reading back (expect all fields empty/ERR)...
[3/6] Checking free NVS space (baseline)...
  FREE_SPACE=2008
[4/6] Writing canned test values...
[5/6] Reading back immediately (same-boot sanity check)...
  FIELD            WRITTEN          READ             RESULT
  SPIN_SERIAL      'SPIN000000001'  'SPIN000000001'  OK
  SHIELD_SERIAL    'SHLD000000001'  'SHLD000000001'  OK
  SPIN_VERSION     '9.9.9'          '9.9.9'          OK
  SHIELD_VERSION   '8.8.8'          '8.8.8'          OK
  SHIELD_PASSWORD  'abc'            'abc'            OK
  EXTRA_0          'EXTRA0'         'EXTRA0'         OK
  EXTRA_1          'EXTRA1'         'EXTRA1'         OK
  EXTRA_2          'EXTRA2'         'EXTRA2'         OK
  EXTRA_3          'EXTRA3'         'EXTRA3'         OK
  EXTRA_4          'EXTRA4'         'EXTRA4'         OK
[6/6] Resetting board and re-reading (persistence check)...
Traceback (most recent call last):
  File "C:\Users\afarahhass\Documents\Repo\CoreV2\Core_Luiz\src\test_metadata_nvs.py", line 468, in <module>
    sys.exit(main())
             ^^^^^^
  File "C:\Users\afarahhass\Documents\Repo\CoreV2\Core_Luiz\src\test_metadata_nvs.py", line 439, in main
    ser, port = reset_and_reconnect(port, board_id)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\afarahhass\Documents\Repo\CoreV2\Core_Luiz\src\test_metadata_nvs.py", line 343, in reset_and_reconnect
    touch_reset(previous_port)
  File "C:\Users\afarahhass\Documents\Repo\CoreV2\Core_Luiz\src\test_metadata_nvs.py", line 243, in touch_reset
    reset_ser = serial.Serial(port, 1200, timeout=1)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\afarahhass\AppData\Local\Programs\Python\Python312\Lib\site-packages\serial\serialwin32.py", line 33, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "C:\Users\afarahhass\AppData\Local\Programs\Python\Python312\Lib\site-packages\serial\serialutil.py", line 244, in __init__
    self.open()
  File "C:\Users\afarahhass\AppData\Local\Programs\Python\Python312\Lib\site-packages\serial\serialwin32.py", line 80, in open
    self._reconfigure_port()
  File "C:\Users\afarahhass\AppData\Local\Programs\Python\Python312\Lib\site-packages\serial\serialwin32.py", line 222, in _reconfigure_port
    raise SerialException(
serial.serialutil.SerialException: Cannot configure port, something went wrong. Original message: OSError(22, 'Un périphérique qui n’existe pas a été spécifié.', None, 433)

It seems the test with metadata has been executed successfully, but the last test with the reset of the board has failed.

Comment thread zephyr/modules/owntech_spin_api/zephyr/src/MetaDataAPI.cpp Outdated
Comment thread zephyr/modules/owntech_spin_api/zephyr/src/MetaDataAPI.cpp Outdated
Comment thread zephyr/modules/owntech_spin_api/zephyr/src/MetaDataAPI.cpp Outdated
Comment thread zephyr/modules/owntech_spin_api/zephyr/src/MetaDataAPI.h Outdated
Comment thread metaData.md Outdated
setSpinSerialNumber, setShieldSerialNumber, and setShieldPassword
trusted their fixed-length constants regardless of how much data the
caller actually provided, so a shorter buffer (e.g. "ABC") caused
nvs_storage_store_data to read past its end. Callers must now pass
the buffer size explicitly, which is checked before touching NVS.

Addresses review feedback from @Ayoub-Farah on PR owntech-foundation#157.
@luizvilla

Copy link
Copy Markdown
Member Author

Updated the code. Got this result

luiz-villa@hp2-owntech:~/code/Core4$ /usr/bin/python3 /home/luiz-villa/code/Core4/src/test_metadata_nvs.py
Using board 423250070031003C on port /dev/ttyACM3
[1/8] Clearing all metadata fields...
  CLEAR=OK
[2/8] Reading back (expect all fields empty/ERR)...
[3/8] Attempting undersized writes (expect all rejected)...
  FIELD                    EXPECTED  ACTUAL  RESULT
  SPIN_SERIAL_BADSIZE      -2        -2      OK
  SHIELD_SERIAL_BADSIZE    -2        -2      OK
  SHIELD_PASSWORD_BADSIZE  -2        -2      OK
[4/8] Reading back (expect fields still empty/ERR)...
[5/8] Checking free NVS space (baseline)...
  FREE_SPACE=2008
[6/8] Writing canned test values...
[7/8] Reading back immediately (same-boot sanity check)...
  FIELD            WRITTEN          READ             RESULT
  SPIN_SERIAL      'SPIN000000001'  'SPIN000000001'  OK
  SHIELD_SERIAL    'SHLD000000001'  'SHLD000000001'  OK
  SPIN_VERSION     '9.9.9'          '9.9.9'          OK
  SHIELD_VERSION   '8.8.8'          '8.8.8'          OK
  SHIELD_PASSWORD  'abc'            'abc'            OK
  EXTRA_0          'EXTRA0'         'EXTRA0'         OK
  EXTRA_1          'EXTRA1'         'EXTRA1'         OK
  EXTRA_2          'EXTRA2'         'EXTRA2'         OK
  EXTRA_3          'EXTRA3'         'EXTRA3'         OK
  EXTRA_4          'EXTRA4'         'EXTRA4'         OK
[8/8] Resetting board and re-reading (persistence check)...
Done
  FIELD            WRITTEN          READ             RESULT
  SPIN_SERIAL      'SPIN000000001'  'SPIN000000001'  OK
  SHIELD_SERIAL    'SHLD000000001'  'SHLD000000001'  OK
  SPIN_VERSION     '9.9.9'          '9.9.9'          OK
  SHIELD_VERSION   '8.8.8'          '8.8.8'          OK
  SHIELD_PASSWORD  'abc'            'abc'            OK
  EXTRA_0          'EXTRA0'         'EXTRA0'         OK
  EXTRA_1          'EXTRA1'         'EXTRA1'         OK
  EXTRA_2          'EXTRA2'         'EXTRA2'         OK
  EXTRA_3          'EXTRA3'         'EXTRA3'         OK
  EXTRA_4          'EXTRA4'         'EXTRA4'         OK
PASS: values survived a board reset
[final] Checking free NVS space (post)...
  FREE_SPACE=1832
ALL CHECKS PASSED

@luizvilla

Copy link
Copy Markdown
Member Author

Changes made. New test code available on the same branch as before.

@Ayoub-Farah

Copy link
Copy Markdown
Member

test_metadata_nvs.py

It seems there is a problem on windows specifically with the python test file, so CODEX suggested the corrected file.

Using board 58435001004B001B on port COM29
[1/8] Clearing all metadata fields...
  CLEAR=OK
[2/8] Reading back (expect all fields empty/ERR)...
[3/8] Attempting undersized writes (expect all rejected)...
  FIELD                    EXPECTED  ACTUAL  RESULT
  SPIN_SERIAL_BADSIZE      -2        -2      OK
  SHIELD_SERIAL_BADSIZE    -2        -2      OK
  SHIELD_PASSWORD_BADSIZE  -2        -2      OK
[4/8] Reading back (expect fields still empty/ERR)...
[5/8] Checking free NVS space (baseline)...
  FREE_SPACE=2008
[6/8] Writing canned test values...
[7/8] Reading back immediately (same-boot sanity check)...
  FIELD            WRITTEN          READ             RESULT
  SPIN_SERIAL      'SPIN000000001'  'SPIN000000001'  OK
  SHIELD_SERIAL    'SHLD000000001'  'SHLD000000001'  OK
  SPIN_VERSION     '9.9.9'          '9.9.9'          OK
  SHIELD_VERSION   '8.8.8'          '8.8.8'          OK
  SHIELD_PASSWORD  'abc'            'abc'            OK
  EXTRA_0          'EXTRA0'         'EXTRA0'         OK
  EXTRA_1          'EXTRA1'         'EXTRA1'         OK
  EXTRA_2          'EXTRA2'         'EXTRA2'         OK
  EXTRA_3          'EXTRA3'         'EXTRA3'         OK
  EXTRA_4          'EXTRA4'         'EXTRA4'         OK
[8/8] Resetting board and re-reading (persistence check)...
Done
  FIELD            WRITTEN          READ             RESULT
  SPIN_SERIAL      'SPIN000000001'  'SPIN000000001'  OK
  SHIELD_SERIAL    'SHLD000000001'  'SHLD000000001'  OK
  SPIN_VERSION     '9.9.9'          '9.9.9'          OK
  SHIELD_VERSION   '8.8.8'          '8.8.8'          OK
  SHIELD_PASSWORD  'abc'            'abc'            OK
  EXTRA_0          'EXTRA0'         'EXTRA0'         OK
  EXTRA_1          'EXTRA1'         'EXTRA1'         OK
  EXTRA_2          'EXTRA2'         'EXTRA2'         OK
  EXTRA_3          'EXTRA3'         'EXTRA3'         OK
  EXTRA_4          'EXTRA4'         'EXTRA4'         OK
PASS: values survived a board reset
[final] Checking free NVS space (post)...
  FREE_SPACE=1832
ALL CHECKS PASSED

The test was a success, so it's good for me.

@Ayoub-Farah Ayoub-Farah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's all ok for me

@luizvilla
luizvilla merged commit 7628c05 into owntech-foundation:main Jul 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants