ch552: Misc. cleanup

- Merge Makefile and Makefile.include into Makefile
  - Format structure
  - Remove unused variables, targets etc.

- Add missing check if it is ok to send data to the FPGA.

- Remove 'baud rate calculator.ods'

- Update encode_usb_strings.py to generate strings for
  CdcCtrlInterfaceDesc, CdcDataInterfaceDesc,
  FidoHidInterfaceDesc, TkeyCtrlInterfaceDesc.
  Also store generated strings in UTF-16 instead of hex.

- Update usb_strings.h to match new encode_usb_strings.py
  output.

- Remove unused struct SetupReqBuf.
This commit is contained in:
Jonas Thörnblad 2025-03-13 14:32:59 +01:00
parent 33f14122ad
commit 8d8f4c7faf
No known key found for this signature in database
GPG key ID: 2D318AD00A326F95
7 changed files with 126 additions and 130 deletions

View file

@ -20,37 +20,51 @@ def descriptor_to_string(descriptor):
def string_to_descriptor(string):
""" Convert a python string into a bytes object containing a USB string descriptor"""
descriptor = bytearray()
descriptor.append(0x00) # placeholder for length
descriptor.append(0x03)
descriptor.append(0x00) # Placeholder for length
descriptor.append(0x03) # Descriptor type (String)
descriptor.extend(string.encode('utf-16')[2:]) # crop the BOM
descriptor[0] = len(descriptor)
descriptor[0] = len(descriptor) # Set length of this descriptor (in bytes)
return bytes(descriptor)
def format_descriptor(name, value):
descriptor = string_to_descriptor(value)
formatted = [
'unsigned char FLASH {}[] = {{ // "{}"'.format(name, value), # Add string as a comment
' {}, // Length of this descriptor (in bytes)'.format(descriptor[0]),
' 0x03, // Descriptor type (String)'
]
formatted.extend(
[
' ' + ', '.join(
["'{}', 0".format(chr(b)) if b != 0x00 else "0x00" for b in descriptor[2 + i:2 + i + 8:2]]
) + ','
for i in range(0, len(descriptor[2:]), 8) # 8 bytes = 4 characters
]
)
formatted.append('};\n')
return '\n'.join(formatted)
if __name__ == "__main__":
manufacturer = 'Tillitis'
product = 'MTA1-USB-V1'
serial = "68de5d27-e223-4874-bc76-a54d6e84068f"
strings = {
"ProdDesc": "MTA1-USB-V1",
"ManufDesc": "Tillitis",
"SerialDesc": "68de5d27-e223-4874-bc76-a54d6e84068f",
"CdcCtrlInterfaceDesc": "CDC-Ctrl",
"CdcDataInterfaceDesc": "CDC-Data",
"FidoHidInterfaceDesc": "FIDO-HID",
"TkeyCtrlInterfaceDesc": "TKEY-Ctrl"
}
with open('include/usb_strings.h', 'w') as f:
f.write('#ifndef __USB_STRINGS_H__\n')
f.write('#define __USB_STRINGS_H__\n')
f.write('\n')
f.write('#include "mem.h"\n')
f.write('\n')
for name, value in strings.items():
f.write(format_descriptor(name, value) + '\n')
with open('usb_strings.h', 'w') as f:
f.write('#ifndef USB_STRINGS\n')
f.write('#define USB_STRINGS\n')
f.write('unsigned char __code ProdDesc[]={{ // "{}"\n'.format(product))
f.write(' ')
f.write(', '.join(['0x{:02x}'.format(i) for i in string_to_descriptor(product)]))
f.write('\n};\n\n')
f.write('unsigned char __code ManufDesc[]={{ // "{}"\n'.format(manufacturer))
f.write(' ')
f.write(', '.join(['0x{:02x}'.format(i) for i in string_to_descriptor(manufacturer)]))
f.write('\n};\n\n')
f.write('unsigned char __code SerialDesc[]={{ // "{}"\n'.format(serial))
f.write(' ')
f.write(', '.join(['0x{:02x}'.format(i) for i in string_to_descriptor(serial)]))
f.write('\n};\n\n')
f.write('#endif\n')