-
Notifications
You must be signed in to change notification settings - Fork 17
Add nice!nano v2 board support #100
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| import argparse | ||
| import struct | ||
| import sys | ||
|
|
||
|
|
||
| UF2_MAGIC_START0 = 0x0A324655 | ||
| UF2_MAGIC_START1 = 0x9E5D5157 | ||
| UF2_MAGIC_END = 0x0AB16F30 | ||
| UF2_FLAG_FAMILY_ID_PRESENT = 0x00002000 | ||
| UF2_PAYLOAD_SIZE = 256 | ||
|
|
||
|
|
||
| def parse_int(value): | ||
| return int(value, 0) | ||
|
|
||
|
|
||
| def parse_hex_line(line, line_number): | ||
| line = line.strip() | ||
| if not line: | ||
| return None | ||
| if not line.startswith(":"): | ||
| raise ValueError(f"line {line_number}: missing ':'") | ||
|
|
||
| try: | ||
| raw = bytes.fromhex(line[1:]) | ||
| except ValueError as exc: | ||
| raise ValueError(f"line {line_number}: invalid hex") from exc | ||
|
|
||
| if len(raw) < 5: | ||
| raise ValueError(f"line {line_number}: record is too short") | ||
|
|
||
| count = raw[0] | ||
| if len(raw) != count + 5: | ||
| raise ValueError(f"line {line_number}: record length mismatch") | ||
|
|
||
| if (sum(raw) & 0xFF) != 0: | ||
| raise ValueError(f"line {line_number}: checksum mismatch") | ||
|
|
||
| address = (raw[1] << 8) | raw[2] | ||
| record_type = raw[3] | ||
| payload = raw[4:4 + count] | ||
| return record_type, address, payload | ||
|
Comment on lines
+31
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Arrr, reject malformed HEX records before generating firmware. The parser accepts invalid type-specific lengths—for example, extended-address records with payloads other than 2 bytes—and accepts malformed EOF records. It also stops at the first EOF and silently ignores any subsequent records, which can produce a partial or incorrectly addressed UF2 image. Validate each record type’s required byte count and address, require exactly one valid EOF record, and reject non-empty content after EOF. Also applies to: 57-71 🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| def read_ihex(filename): | ||
| data = {} | ||
| upper_address = 0 | ||
|
|
||
| with open(filename, "r", encoding="ascii") as hex_file: | ||
| for line_number, line in enumerate(hex_file, 1): | ||
| record = parse_hex_line(line, line_number) | ||
| if record is None: | ||
| continue | ||
|
|
||
| record_type, address, payload = record | ||
| if record_type == 0x00: | ||
| absolute = upper_address + address | ||
| for offset, value in enumerate(payload): | ||
| data[absolute + offset] = value | ||
| elif record_type == 0x01: | ||
| break | ||
| elif record_type == 0x02: | ||
| upper_address = int.from_bytes(payload, "big") << 4 | ||
| elif record_type == 0x04: | ||
| upper_address = int.from_bytes(payload, "big") << 16 | ||
| elif record_type in (0x03, 0x05): | ||
| continue | ||
| else: | ||
| raise ValueError(f"line {line_number}: unsupported record type {record_type:#x}") | ||
|
|
||
| if not data: | ||
| raise ValueError("input HEX contains no data records") | ||
|
|
||
| return data | ||
|
|
||
|
|
||
| def build_blocks(data): | ||
| blocks = {} | ||
| for address, value in data.items(): | ||
| block_address = address & ~(UF2_PAYLOAD_SIZE - 1) | ||
| if block_address not in blocks: | ||
| blocks[block_address] = bytearray([0xFF] * UF2_PAYLOAD_SIZE) | ||
| blocks[block_address][address - block_address] = value | ||
|
|
||
| return sorted(blocks.items()) | ||
|
|
||
|
|
||
| def write_uf2(blocks, family_id, output): | ||
| total = len(blocks) | ||
| with open(output, "wb") as uf2_file: | ||
| for index, (address, payload) in enumerate(blocks): | ||
| header = struct.pack( | ||
| "<IIIIIIII", | ||
| UF2_MAGIC_START0, | ||
| UF2_MAGIC_START1, | ||
| UF2_FLAG_FAMILY_ID_PRESENT, | ||
| address, | ||
| UF2_PAYLOAD_SIZE, | ||
| index, | ||
| total, | ||
| family_id, | ||
| ) | ||
| block = header + bytes(payload) | ||
| block += bytes(512 - len(block) - 4) | ||
| block += struct.pack("<I", UF2_MAGIC_END) | ||
| uf2_file.write(block) | ||
|
|
||
|
|
||
| def main(argv): | ||
| parser = argparse.ArgumentParser(description="Convert an Intel HEX file to UF2.") | ||
| parser.add_argument("-f", "--family", type=parse_int, required=True, help="UF2 family ID") | ||
| parser.add_argument("-o", "--output", required=True, help="output UF2 file") | ||
| parser.add_argument("input", help="input Intel HEX file") | ||
| args = parser.parse_args(argv) | ||
|
|
||
| data = read_ihex(args.input) | ||
| blocks = build_blocks(data) | ||
| write_uf2(blocks, args.family, args.output) | ||
| print(f"Wrote {args.output} ({len(blocks)} UF2 blocks)") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main(sys.argv[1:]) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #include "variant.h" | ||
| #include "wiring_constants.h" | ||
| #include "wiring_digital.h" | ||
| #include "nrf.h" | ||
|
|
||
| const uint32_t g_ADigitalPinMap[] = | ||
| { | ||
| // nice!nano v2 edge castellations | ||
| 8, // D0 is P0.08 | ||
| 6, // D1 is P0.06 | ||
| 17, // D2 is P0.17 | ||
| 20, // D3 is P0.20 | ||
| 22, // D4 is P0.22 | ||
| 24, // D5 is P0.24 | ||
| 32, // D6 is P1.00 | ||
| 11, // D7 is P0.11 | ||
| 36, // D8 is P1.04 | ||
| 38, // D9 is P1.06 | ||
| 9, // D10 is P0.09 | ||
|
|
||
| // Exposed bottom pads | ||
| 33, // D11 is P1.01 | ||
| 34, // D12 is P1.02 | ||
| 39, // D13 is P1.07 | ||
|
|
||
| // nice!nano v2 edge castellations | ||
| 43, // D14 is P1.11 | ||
| 45, // D15 is P1.13 | ||
| 10, // D16 is P0.10 | ||
| 42, // D17 is P1.10 | ||
| 47, // D18 is P1.15 | ||
| 2, // D19 is P0.02 (AIN0) | ||
| 29, // D20 is P0.29 (AIN5) | ||
| 31, // D21 is P0.31 (AIN7) | ||
|
|
||
| 15, // D22 is P0.15 (status LED) | ||
| 4, // D23 is P0.04 (battery voltage) | ||
| 13, // D24 is P0.13 (VCC cutoff) | ||
| }; | ||
|
|
||
| void initVariant() | ||
| { | ||
| pinMode(PIN_LED, OUTPUT); | ||
| ledOff(PIN_LED); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Arrr, add the missing UF2 workflow steps.
This section says to copy an “exported”
.uf2file but does not explain how to export it or how to trigger the board’s UF2 bootloader. Add the exact IDE action and reset sequence so users can complete the documented workflow.🤖 Prompt for AI Agents