Skip to content

Commit 8d8f4c7

Browse files
committedMar 14, 2025
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.
1 parent 33f1412 commit 8d8f4c7

File tree

7 files changed

+127
-131
lines changed

7 files changed

+127
-131
lines changed
 

‎hw/usb_interface/ch552_fw/Makefile

+75-24
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,88 @@
1-
TARGET = usb_device_cdc
1+
#######################################################
22

3-
CH554_SDCC=~/ch554_sdcc/
4-
CHPROG=chprog
3+
# Toolchain
4+
CC = sdcc
5+
OBJCOPY = objcopy
6+
PACK_HEX = packihx
7+
CHPROG = chprog
58

6-
# Adjust the XRAM location and size to leave space for the USB DMA buffers
7-
# Buffer layout in XRAM:
8-
# 0x0000 Ep0Buffer[8]
9-
# 0x0040 Ep1Buffer[8]
10-
# 0x0080 EP2Buffer[2*64]
11-
#
12-
# This takes a total of 256bytes, so there are 768 bytes left.
13-
#XRAM_SIZE = 0x0300
14-
#XRAM_LOC = 0x0100
9+
#######################################################
1510

16-
XRAM_SIZE = 0x0400
17-
XRAM_LOC = 0x0000
11+
TARGET = usb_device
1812

19-
FREQ_SYS = 16000000
13+
ROOT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
14+
15+
XRAM_SIZE = 0x0400 # 1 KB on-chip xRAM
16+
XRAM_LOC = 0x0000 # xRAM area starts at address 0 in the External Data Address Space
17+
CODE_SIZE = 0x3800 # 14 KB program storage area
18+
FREQ_SYS = 16000000 # 16 MHz system clock
19+
20+
EXTRA_FLAGS = -DBUILD_CODE
21+
22+
CFLAGS = \
23+
-V \
24+
-mmcs51 \
25+
--model-small \
26+
--xram-size $(XRAM_SIZE) \
27+
--xram-loc $(XRAM_LOC) \
28+
--code-size $(CODE_SIZE) \
29+
-I$(ROOT_DIR)include \
30+
-DFREQ_SYS=$(FREQ_SYS) \
31+
$(EXTRA_FLAGS)
32+
33+
LFLAGS = \
34+
$(CFLAGS)
35+
36+
C_FILES = \
37+
main.c \
38+
include/debug.c \
39+
include/print.c
40+
41+
# Create a .rel file for each .c file
42+
RELS = $(C_FILES:.c=.rel)
43+
44+
%.rel : %.c
45+
$(CC) -c $(CFLAGS) $<
2046

2147
usb_strings.h: encode_usb_strings.py
2248
./encode_usb_strings.py
2349

24-
C_FILES = \
25-
main.c \
26-
include/debug.c \
27-
include/print.c
50+
# Note: SDCC will dump all of the temporary files into this one,
51+
# so strip the paths from RELS
52+
# For now, get around this by stripping the paths off of the RELS list.
2853

29-
pre-flash:
54+
$(TARGET).ihx: $(RELS)
55+
$(CC) $(notdir $(RELS)) $(LFLAGS) -o $(TARGET).ihx
3056

57+
$(TARGET).hex: $(TARGET).ihx
58+
$(PACK_HEX) $(TARGET).ihx > $(TARGET).hex
3159

32-
flash_patched: usb_device_cdc.bin
33-
./inject_serial_number.py -i usb_device_cdc.bin -o patched.bin
34-
${CHPROG} patched.bin
60+
$(TARGET).bin: $(TARGET).ihx
61+
$(OBJCOPY) -I ihex -O binary $(TARGET).ihx $(TARGET).bin
62+
63+
flash: $(TARGET).bin
64+
$(CHPROG) $(TARGET).bin
65+
66+
flash_patched: $(TARGET).bin
67+
./inject_serial_number.py -i $(TARGET).bin -o patched.bin
68+
$(CHPROG) patched.bin
3569
rm patched.bin
3670

37-
include Makefile.include
71+
.DEFAULT_GOAL := all
72+
all: $(TARGET).bin $(TARGET).hex
73+
74+
clean:
75+
rm -f \
76+
$(notdir $(RELS:.rel=.asm)) \
77+
$(notdir $(RELS:.rel=.lst)) \
78+
$(notdir $(RELS:.rel=.mem)) \
79+
$(notdir $(RELS:.rel=.rel)) \
80+
$(notdir $(RELS:.rel=.rst)) \
81+
$(notdir $(RELS:.rel=.sym)) \
82+
$(notdir $(RELS:.rel=.adb)) \
83+
$(TARGET).lk \
84+
$(TARGET).map \
85+
$(TARGET).mem \
86+
$(TARGET).ihx \
87+
$(TARGET).hex \
88+
$(TARGET).bin

‎hw/usb_interface/ch552_fw/Makefile.include

-70
This file was deleted.
Binary file not shown.

‎hw/usb_interface/ch552_fw/encode_usb_strings.py

+41-27
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,51 @@ def descriptor_to_string(descriptor):
2020
def string_to_descriptor(string):
2121
""" Convert a python string into a bytes object containing a USB string descriptor"""
2222
descriptor = bytearray()
23-
descriptor.append(0x00) # placeholder for length
24-
descriptor.append(0x03)
23+
descriptor.append(0x00) # Placeholder for length
24+
descriptor.append(0x03) # Descriptor type (String)
2525
descriptor.extend(string.encode('utf-16')[2:]) # crop the BOM
26-
descriptor[0] = len(descriptor)
26+
descriptor[0] = len(descriptor) # Set length of this descriptor (in bytes)
2727

2828
return bytes(descriptor)
2929

30+
def format_descriptor(name, value):
31+
descriptor = string_to_descriptor(value)
32+
formatted = [
33+
'unsigned char FLASH {}[] = {{ // "{}"'.format(name, value), # Add string as a comment
34+
' {}, // Length of this descriptor (in bytes)'.format(descriptor[0]),
35+
' 0x03, // Descriptor type (String)'
36+
]
37+
38+
formatted.extend(
39+
[
40+
' ' + ', '.join(
41+
["'{}', 0".format(chr(b)) if b != 0x00 else "0x00" for b in descriptor[2 + i:2 + i + 8:2]]
42+
) + ','
43+
for i in range(0, len(descriptor[2:]), 8) # 8 bytes = 4 characters
44+
]
45+
)
46+
formatted.append('};\n')
47+
return '\n'.join(formatted)
3048

3149
if __name__ == "__main__":
32-
manufacturer = 'Tillitis'
33-
product = 'MTA1-USB-V1'
34-
serial = "68de5d27-e223-4874-bc76-a54d6e84068f"
35-
36-
with open('usb_strings.h', 'w') as f:
37-
f.write('#ifndef USB_STRINGS\n')
38-
f.write('#define USB_STRINGS\n')
39-
40-
f.write('unsigned char __code ProdDesc[]={{ // "{}"\n'.format(product))
41-
f.write(' ')
42-
f.write(', '.join(['0x{:02x}'.format(i) for i in string_to_descriptor(product)]))
43-
f.write('\n};\n\n')
44-
45-
f.write('unsigned char __code ManufDesc[]={{ // "{}"\n'.format(manufacturer))
46-
f.write(' ')
47-
f.write(', '.join(['0x{:02x}'.format(i) for i in string_to_descriptor(manufacturer)]))
48-
f.write('\n};\n\n')
49-
50-
f.write('unsigned char __code SerialDesc[]={{ // "{}"\n'.format(serial))
51-
f.write(' ')
52-
f.write(', '.join(['0x{:02x}'.format(i) for i in string_to_descriptor(serial)]))
53-
f.write('\n};\n\n')
54-
55-
50+
strings = {
51+
"ProdDesc": "MTA1-USB-V1",
52+
"ManufDesc": "Tillitis",
53+
"SerialDesc": "68de5d27-e223-4874-bc76-a54d6e84068f",
54+
"CdcCtrlInterfaceDesc": "CDC-Ctrl",
55+
"CdcDataInterfaceDesc": "CDC-Data",
56+
"FidoHidInterfaceDesc": "FIDO-HID",
57+
"TkeyCtrlInterfaceDesc": "TKEY-Ctrl"
58+
}
59+
60+
with open('include/usb_strings.h', 'w') as f:
61+
f.write('#ifndef __USB_STRINGS_H__\n')
62+
f.write('#define __USB_STRINGS_H__\n')
63+
f.write('\n')
64+
f.write('#include "mem.h"\n')
65+
f.write('\n')
66+
67+
for name, value in strings.items():
68+
f.write(format_descriptor(name, value) + '\n')
69+
5670
f.write('#endif\n')

‎hw/usb_interface/ch552_fw/include/debug.h

+2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ inline uint8_t CH554UART1RcvByte( )
186186
*******************************************************************************/
187187
inline void CH554UART1SendByte(uint8_t SendDat)
188188
{
189+
while(gpio_p1_4_get() == 0)
190+
;
189191
SBUF1 = SendDat; // Query sending, the interrupt mode does not need the following two statements, but TI=0 is required before sending
190192
while (U1TI == 0)
191193
;

‎hw/usb_interface/ch552_fw/include/usb_strings.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33

44
#include "mem.h"
55

6-
unsigned char FLASH ProdDesc[]={ // "MTA1-USB-V1"
6+
unsigned char FLASH ProdDesc[] = { // "MTA1-USB-V1"
77
24, // Length of this descriptor (in bytes)
88
0x03, // Descriptor type (String)
99
'M', 0, 'T', 0, 'A', 0, '1', 0,
1010
'-', 0, 'U', 0, 'S', 0, 'B', 0,
11-
'-', 0, 'V', 0, '1', 0
11+
'-', 0, 'V', 0, '1', 0,
1212
};
1313

14-
unsigned char FLASH ManufDesc[]={ // "Tillitis"
14+
unsigned char FLASH ManufDesc[] = { // "Tillitis"
1515
18, // Length of this descriptor (in bytes)
1616
0x03, // Descriptor type (String)
1717
'T', 0, 'i', 0, 'l', 0, 'l', 0,
18-
'i', 0, 't', 0, 'i', 0, 's', 0
18+
'i', 0, 't', 0, 'i', 0, 's', 0,
1919
};
2020

21-
unsigned char FLASH SerialDesc[]={ // "68de5d27-e223-4874-bc76-a54d6e84068f"
21+
unsigned char FLASH SerialDesc[] = { // "68de5d27-e223-4874-bc76-a54d6e84068f"
2222
74, // Length of this descriptor (in bytes)
2323
0x03, // Descriptor type (String)
2424
'6', 0, '8', 0, 'd', 0, 'e', 0,
@@ -32,28 +32,28 @@ unsigned char FLASH SerialDesc[]={ // "68de5d27-e223-4874-bc76-a54d6e84068f"
3232
'0', 0, '6', 0, '8', 0, 'f', 0,
3333
};
3434

35-
unsigned char FLASH CdcCtrlInterfaceDesc[] = {
35+
unsigned char FLASH CdcCtrlInterfaceDesc[] = { // "CDC-Ctrl"
3636
18, // Length of this descriptor (in bytes)
3737
0x03, // Descriptor type (String)
3838
'C', 0, 'D', 0, 'C', 0, '-', 0,
3939
'C', 0, 't', 0, 'r', 0, 'l', 0,
4040
};
4141

42-
unsigned char FLASH CdcDataInterfaceDesc[] = {
42+
unsigned char FLASH CdcDataInterfaceDesc[] = { // "CDC-Data"
4343
18, // Length of this descriptor (in bytes)
4444
0x03, // Descriptor type (String)
4545
'C', 0, 'D', 0, 'C', 0, '-', 0,
4646
'D', 0, 'a', 0, 't', 0, 'a', 0,
4747
};
4848

49-
unsigned char FLASH FidoHidInterfaceDesc[] = {
49+
unsigned char FLASH FidoHidInterfaceDesc[] = { // "FIDO-HID"
5050
18, // Length of this descriptor (in bytes)
5151
0x03, // Descriptor type (String)
5252
'F', 0, 'I', 0, 'D', 0, 'O', 0,
5353
'-', 0, 'H', 0, 'I', 0, 'D', 0,
5454
};
5555

56-
unsigned char FLASH TkeyCtrlInterfaceDesc[] = {
56+
unsigned char FLASH TkeyCtrlInterfaceDesc[] = { // "TKEY-Ctrl"
5757
20, // Length of this descriptor (in bytes)
5858
0x03, // Descriptor type (String)
5959
'T', 0, 'K', 0, 'E', 0, 'Y', 0,

‎hw/usb_interface/ch552_fw/main.c

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ uint16_t SetupLen = 0;
2727
uint8_t SetupReq = 0;
2828
uint8_t UsbConfig = 0;
2929
const uint8_t *pDescr = NULL; // USB configuration flag
30-
USB_SETUP_REQ SetupReqBuf = { 0 }; // Temporary Setup package
3130

3231
#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer)
3332

0 commit comments

Comments
 (0)