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

@ -1,37 +1,88 @@
TARGET = usb_device_cdc
#######################################################
CH554_SDCC=~/ch554_sdcc/
CHPROG=chprog
# Toolchain
CC = sdcc
OBJCOPY = objcopy
PACK_HEX = packihx
CHPROG = chprog
# Adjust the XRAM location and size to leave space for the USB DMA buffers
# Buffer layout in XRAM:
# 0x0000 Ep0Buffer[8]
# 0x0040 Ep1Buffer[8]
# 0x0080 EP2Buffer[2*64]
#
# This takes a total of 256bytes, so there are 768 bytes left.
#XRAM_SIZE = 0x0300
#XRAM_LOC = 0x0100
#######################################################
XRAM_SIZE = 0x0400
XRAM_LOC = 0x0000
TARGET = usb_device
FREQ_SYS = 16000000
ROOT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
XRAM_SIZE = 0x0400 # 1 KB on-chip xRAM
XRAM_LOC = 0x0000 # xRAM area starts at address 0 in the External Data Address Space
CODE_SIZE = 0x3800 # 14 KB program storage area
FREQ_SYS = 16000000 # 16 MHz system clock
EXTRA_FLAGS = -DBUILD_CODE
CFLAGS = \
-V \
-mmcs51 \
--model-small \
--xram-size $(XRAM_SIZE) \
--xram-loc $(XRAM_LOC) \
--code-size $(CODE_SIZE) \
-I$(ROOT_DIR)include \
-DFREQ_SYS=$(FREQ_SYS) \
$(EXTRA_FLAGS)
LFLAGS = \
$(CFLAGS)
C_FILES = \
main.c \
include/debug.c \
include/print.c
# Create a .rel file for each .c file
RELS = $(C_FILES:.c=.rel)
%.rel : %.c
$(CC) -c $(CFLAGS) $<
usb_strings.h: encode_usb_strings.py
./encode_usb_strings.py
C_FILES = \
main.c \
include/debug.c \
include/print.c
# Note: SDCC will dump all of the temporary files into this one,
# so strip the paths from RELS
# For now, get around this by stripping the paths off of the RELS list.
pre-flash:
$(TARGET).ihx: $(RELS)
$(CC) $(notdir $(RELS)) $(LFLAGS) -o $(TARGET).ihx
$(TARGET).hex: $(TARGET).ihx
$(PACK_HEX) $(TARGET).ihx > $(TARGET).hex
flash_patched: usb_device_cdc.bin
./inject_serial_number.py -i usb_device_cdc.bin -o patched.bin
${CHPROG} patched.bin
$(TARGET).bin: $(TARGET).ihx
$(OBJCOPY) -I ihex -O binary $(TARGET).ihx $(TARGET).bin
flash: $(TARGET).bin
$(CHPROG) $(TARGET).bin
flash_patched: $(TARGET).bin
./inject_serial_number.py -i $(TARGET).bin -o patched.bin
$(CHPROG) patched.bin
rm patched.bin
include Makefile.include
.DEFAULT_GOAL := all
all: $(TARGET).bin $(TARGET).hex
clean:
rm -f \
$(notdir $(RELS:.rel=.asm)) \
$(notdir $(RELS:.rel=.lst)) \
$(notdir $(RELS:.rel=.mem)) \
$(notdir $(RELS:.rel=.rel)) \
$(notdir $(RELS:.rel=.rst)) \
$(notdir $(RELS:.rel=.sym)) \
$(notdir $(RELS:.rel=.adb)) \
$(TARGET).lk \
$(TARGET).map \
$(TARGET).mem \
$(TARGET).ihx \
$(TARGET).hex \
$(TARGET).bin

View File

@ -1,70 +0,0 @@
#######################################################
# toolchain
CC = sdcc
OBJCOPY = objcopy
PACK_HEX = packihx
WCHISP ?= wchisptool -g -f
#######################################################
EXTRA_FLAGS = -D BUILD_CODE
FREQ_SYS ?= 24000000
XRAM_SIZE ?= 0x0400
XRAM_LOC ?= 0x0000
CODE_SIZE ?= 0x3800
ROOT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
CFLAGS := -V -mmcs51 --model-small \
--xram-size $(XRAM_SIZE) --xram-loc $(XRAM_LOC) \
--code-size $(CODE_SIZE) \
-I$(ROOT_DIR)include -DFREQ_SYS=$(FREQ_SYS) \
$(EXTRA_FLAGS)
LFLAGS := $(CFLAGS)
RELS := $(C_FILES:.c=.rel)
print-% : ; @echo $* = $($*)
%.rel : %.c
$(CC) -c $(CFLAGS) $<
# Note: SDCC will dump all of the temporary files into this one, so strip the paths from RELS
# For now, get around this by stripping the paths off of the RELS list.
$(TARGET).ihx: $(RELS)
$(CC) $(notdir $(RELS)) $(LFLAGS) -o $(TARGET).ihx
$(TARGET).hex: $(TARGET).ihx
$(PACK_HEX) $(TARGET).ihx > $(TARGET).hex
$(TARGET).bin: $(TARGET).ihx
$(OBJCOPY) -I ihex -O binary $(TARGET).ihx $(TARGET).bin
flash: $(TARGET).bin pre-flash
$(WCHISP) $(TARGET).bin
.DEFAULT_GOAL := all
all: $(TARGET).bin $(TARGET).hex
clean:
rm -f \
$(notdir $(RELS:.rel=.asm)) \
$(notdir $(RELS:.rel=.lst)) \
$(notdir $(RELS:.rel=.mem)) \
$(notdir $(RELS:.rel=.rel)) \
$(notdir $(RELS:.rel=.rst)) \
$(notdir $(RELS:.rel=.sym)) \
$(notdir $(RELS:.rel=.adb)) \
$(TARGET).lk \
$(TARGET).map \
$(TARGET).mem \
$(TARGET).ihx \
$(TARGET).hex \
$(TARGET).bin

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')

View File

@ -186,6 +186,8 @@ inline uint8_t CH554UART1RcvByte( )
*******************************************************************************/
inline void CH554UART1SendByte(uint8_t SendDat)
{
while(gpio_p1_4_get() == 0)
;
SBUF1 = SendDat; // Query sending, the interrupt mode does not need the following two statements, but TI=0 is required before sending
while (U1TI == 0)
;

View File

@ -3,22 +3,22 @@
#include "mem.h"
unsigned char FLASH ProdDesc[]={ // "MTA1-USB-V1"
unsigned char FLASH ProdDesc[] = { // "MTA1-USB-V1"
24, // Length of this descriptor (in bytes)
0x03, // Descriptor type (String)
'M', 0, 'T', 0, 'A', 0, '1', 0,
'-', 0, 'U', 0, 'S', 0, 'B', 0,
'-', 0, 'V', 0, '1', 0
'-', 0, 'V', 0, '1', 0,
};
unsigned char FLASH ManufDesc[]={ // "Tillitis"
unsigned char FLASH ManufDesc[] = { // "Tillitis"
18, // Length of this descriptor (in bytes)
0x03, // Descriptor type (String)
'T', 0, 'i', 0, 'l', 0, 'l', 0,
'i', 0, 't', 0, 'i', 0, 's', 0
'i', 0, 't', 0, 'i', 0, 's', 0,
};
unsigned char FLASH SerialDesc[]={ // "68de5d27-e223-4874-bc76-a54d6e84068f"
unsigned char FLASH SerialDesc[] = { // "68de5d27-e223-4874-bc76-a54d6e84068f"
74, // Length of this descriptor (in bytes)
0x03, // Descriptor type (String)
'6', 0, '8', 0, 'd', 0, 'e', 0,
@ -32,28 +32,28 @@ unsigned char FLASH SerialDesc[]={ // "68de5d27-e223-4874-bc76-a54d6e84068f"
'0', 0, '6', 0, '8', 0, 'f', 0,
};
unsigned char FLASH CdcCtrlInterfaceDesc[] = {
unsigned char FLASH CdcCtrlInterfaceDesc[] = { // "CDC-Ctrl"
18, // Length of this descriptor (in bytes)
0x03, // Descriptor type (String)
'C', 0, 'D', 0, 'C', 0, '-', 0,
'C', 0, 't', 0, 'r', 0, 'l', 0,
};
unsigned char FLASH CdcDataInterfaceDesc[] = {
unsigned char FLASH CdcDataInterfaceDesc[] = { // "CDC-Data"
18, // Length of this descriptor (in bytes)
0x03, // Descriptor type (String)
'C', 0, 'D', 0, 'C', 0, '-', 0,
'D', 0, 'a', 0, 't', 0, 'a', 0,
};
unsigned char FLASH FidoHidInterfaceDesc[] = {
unsigned char FLASH FidoHidInterfaceDesc[] = { // "FIDO-HID"
18, // Length of this descriptor (in bytes)
0x03, // Descriptor type (String)
'F', 0, 'I', 0, 'D', 0, 'O', 0,
'-', 0, 'H', 0, 'I', 0, 'D', 0,
};
unsigned char FLASH TkeyCtrlInterfaceDesc[] = {
unsigned char FLASH TkeyCtrlInterfaceDesc[] = { // "TKEY-Ctrl"
20, // Length of this descriptor (in bytes)
0x03, // Descriptor type (String)
'T', 0, 'K', 0, 'E', 0, 'Y', 0,

View File

@ -27,7 +27,6 @@ uint16_t SetupLen = 0;
uint8_t SetupReq = 0;
uint8_t UsbConfig = 0;
const uint8_t *pDescr = NULL; // USB configuration flag
USB_SETUP_REQ SetupReqBuf = { 0 }; // Temporary Setup package
#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer)