mirror of
https://github.com/tillitis/tillitis-key1.git
synced 2025-03-26 00:28:15 -04:00
Merge d080dedca0219deef426f54a509e6e27474267a1 into 8d8f4c7fafe84ee198f6794ed4b9aa0a0aecdefe
This commit is contained in:
commit
058be0c51f
@ -62,11 +62,11 @@ Mode Protocol. It is used in both directions.
|
||||
|
||||
The different endpoints:
|
||||
|
||||
| *Name* | *Value* | *Comment* |
|
||||
|--------|---------|---------------------------------------------------------------------|
|
||||
| CTRL | 0x20 | A USB HID special debug pipe. Useful for debug prints. |
|
||||
| CDC | 0x40 | USB CDC-ACM, a serial port on the client. |
|
||||
| HID | 0x80 | A USB HID security token device, useful for FIDO-type applications. |
|
||||
| *Name* | *Value* | *Comment* |
|
||||
|--------|---------|----------------------------------------------------------------------|
|
||||
| DEBUG | 0x20 | A USB HID special debug pipe. Useful for debug prints. |
|
||||
| CDC | 0x40 | USB CDC-ACM, a serial port on the client. |
|
||||
| FIDO | 0x80 | A USB FIDO security token device, useful for FIDO-type applications. |
|
||||
|
||||
On top of the USB Mode Protocol is [the TKey Framing
|
||||
Protocol](https://dev.tillitis.se/protocol/) which is described in the
|
||||
|
@ -10,7 +10,7 @@ CHPROG = chprog
|
||||
|
||||
TARGET = usb_device
|
||||
|
||||
ROOT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
|
||||
OUT_DIR = _out
|
||||
|
||||
XRAM_SIZE = 0x0400 # 1 KB on-chip xRAM
|
||||
XRAM_LOC = 0x0000 # xRAM area starts at address 0 in the External Data Address Space
|
||||
@ -26,7 +26,7 @@ CFLAGS = \
|
||||
--xram-size $(XRAM_SIZE) \
|
||||
--xram-loc $(XRAM_LOC) \
|
||||
--code-size $(CODE_SIZE) \
|
||||
-I$(ROOT_DIR)include \
|
||||
-Iinc \
|
||||
-DFREQ_SYS=$(FREQ_SYS) \
|
||||
$(EXTRA_FLAGS)
|
||||
|
||||
@ -34,25 +34,31 @@ LFLAGS = \
|
||||
$(CFLAGS)
|
||||
|
||||
C_FILES = \
|
||||
main.c \
|
||||
include/debug.c \
|
||||
include/print.c
|
||||
src/debug.c \
|
||||
src/flash.c \
|
||||
src/gpio.c \
|
||||
src/lib.c \
|
||||
src/main.c \
|
||||
src/print.c
|
||||
|
||||
# Create a .rel file for each .c file
|
||||
RELS = $(C_FILES:.c=.rel)
|
||||
# Create a .rel file for each .c file in $(OUT_DIR)/
|
||||
RELS = $(patsubst %.c,$(OUT_DIR)/%.rel,$(C_FILES))
|
||||
|
||||
%.rel : %.c
|
||||
$(CC) -c $(CFLAGS) $<
|
||||
OUT_SUBDIRS = $(sort $(dir $(RELS)))
|
||||
|
||||
# Ensure out directory exists
|
||||
$(OUT_DIR):
|
||||
mkdir -p $(OUT_SUBDIRS)
|
||||
|
||||
$(OUT_DIR)/%.rel: %.c | $(OUT_DIR)
|
||||
$(CC) -c $(CFLAGS) $< -o $@
|
||||
|
||||
usb_strings.h: encode_usb_strings.py
|
||||
./encode_usb_strings.py
|
||||
|
||||
# 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.
|
||||
|
||||
# Compile the final ihx file
|
||||
$(TARGET).ihx: $(RELS)
|
||||
$(CC) $(notdir $(RELS)) $(LFLAGS) -o $(TARGET).ihx
|
||||
$(CC) $(RELS) $(LFLAGS) -o $(TARGET).ihx
|
||||
|
||||
$(TARGET).hex: $(TARGET).ihx
|
||||
$(PACK_HEX) $(TARGET).ihx > $(TARGET).hex
|
||||
@ -72,14 +78,7 @@ flash_patched: $(TARGET).bin
|
||||
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)) \
|
||||
rm -rf $(OUT_DIR) \
|
||||
$(TARGET).lk \
|
||||
$(TARGET).map \
|
||||
$(TARGET).mem \
|
||||
|
@ -53,11 +53,11 @@ if __name__ == "__main__":
|
||||
"SerialDesc": "68de5d27-e223-4874-bc76-a54d6e84068f",
|
||||
"CdcCtrlInterfaceDesc": "CDC-Ctrl",
|
||||
"CdcDataInterfaceDesc": "CDC-Data",
|
||||
"FidoHidInterfaceDesc": "FIDO-HID",
|
||||
"TkeyCtrlInterfaceDesc": "TKEY-Ctrl"
|
||||
"FidoInterfaceDesc": "FIDO",
|
||||
"DebugInterfaceDesc": "DEBUG"
|
||||
}
|
||||
|
||||
with open('include/usb_strings.h', 'w') as f:
|
||||
with open('inc/usb_strings.h', 'w') as f:
|
||||
f.write('#ifndef __USB_STRINGS_H__\n')
|
||||
f.write('#define __USB_STRINGS_H__\n')
|
||||
f.write('\n')
|
||||
|
@ -195,8 +195,8 @@ Header file for CH554 microcontrollers.
|
||||
#define USB_IDX_SERIAL_STR 0x03
|
||||
#define USB_IDX_INTERFACE_CDC_CTRL_STR 0x04
|
||||
#define USB_IDX_INTERFACE_CDC_DATA_STR 0x05
|
||||
#define USB_IDX_INTERFACE_FIDO_HID_STR 0x06
|
||||
#define USB_IDX_INTERFACE_TKEY_CTRL_STR 0x07
|
||||
#define USB_IDX_INTERFACE_FIDO_STR 0x06
|
||||
#define USB_IDX_INTERFACE_DEBUG_STR 0x07
|
||||
#endif
|
||||
|
||||
#ifndef USB_DEVICE_ADDR
|
@ -8,6 +8,7 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "ch554.h"
|
||||
#include "gpio.h"
|
||||
|
||||
// UART1 baud rates
|
||||
// Setting Actual % error
|
||||
@ -40,18 +41,6 @@ void CfgFsys(void); // CH554 clock selection and configuration
|
||||
void mDelayuS(uint16_t n); // Delay in units of uS
|
||||
void mDelaymS(uint16_t n); // Delay in mS
|
||||
|
||||
// Set pin p1.4 and p1.5 to GPIO output mode.
|
||||
void gpio_init(void);
|
||||
void gpio_set(uint8_t pin);
|
||||
void gpio_unset(uint8_t pin);
|
||||
uint8_t gpio_get(uint8_t pin);
|
||||
|
||||
void gpio_init_p1_4_in(void);
|
||||
void gpio_init_p1_5_out(void);
|
||||
uint8_t gpio_p1_4_get(void);
|
||||
void gpio_p1_5_set(void);
|
||||
void gpio_p1_5_unset(void);
|
||||
|
||||
/*******************************************************************************
|
||||
* Function Name : CH554UART0Alter()
|
||||
* Description : Set the alternate pin mappings for UART0 (RX on P1.2, TX on P1.3)
|
9
hw/usb_interface/ch552_fw/inc/flash.h
Normal file
9
hw/usb_interface/ch552_fw/inc/flash.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef __FLASH_H__
|
||||
#define __FLASH_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint8_t write_code_flash(uint16_t address, uint16_t data);
|
||||
uint8_t write_data_flash(uint8_t address, uint8_t data);
|
||||
|
||||
#endif
|
20
hw/usb_interface/ch552_fw/inc/gpio.h
Normal file
20
hw/usb_interface/ch552_fw/inc/gpio.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef __GPIO_H__
|
||||
#define __GPIO_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "main.h"
|
||||
|
||||
// Set pin p1.4 and p1.5 to GPIO output mode.
|
||||
void gpio_init(void);
|
||||
void gpio_set(uint8_t pin);
|
||||
void gpio_unset(uint8_t pin);
|
||||
uint8_t gpio_get(uint8_t pin);
|
||||
|
||||
void gpio_init_p1_4_in(void);
|
||||
void gpio_init_p1_5_out(void);
|
||||
uint8_t gpio_p1_4_get(void);
|
||||
void gpio_p1_5_set(void);
|
||||
void gpio_p1_5_unset(void);
|
||||
|
||||
#endif
|
14
hw/usb_interface/ch552_fw/inc/io.h
Normal file
14
hw/usb_interface/ch552_fw/inc/io.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef __IO_H__
|
||||
#define __IO_H__
|
||||
|
||||
enum ioend {
|
||||
IO_NONE = 0x00, // No endpoint
|
||||
IO_UART = 0x01, // Only destination, raw UART access
|
||||
IO_QEMU = 0x02, // Only destination, QEMU debug port
|
||||
IO_CH552 = 0x10, // Internal CH552 control port
|
||||
IO_DEBUG = 0x20, // HID debug port
|
||||
IO_CDC = 0x40, // CDC "serial port"
|
||||
IO_FIDO = 0x80, // FIDO security token port
|
||||
};
|
||||
|
||||
#endif
|
23
hw/usb_interface/ch552_fw/inc/lib.h
Normal file
23
hw/usb_interface/ch552_fw/inc/lib.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef __LIB_H__
|
||||
#define __LIB_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#ifdef USE_NUM_U8
|
||||
int8_t uint8_to_str(uint8_t *buf, uint8_t bufsize, uint8_t n);
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U32
|
||||
int8_t uint32_to_str(uint8_t *buf, uint8_t bufsize, uint32_t n);
|
||||
#endif
|
||||
|
||||
//uint8_t hex_to_uint8(const char *hex, uint8_t len);
|
||||
//uint16_t hex_to_uint16(const char *hex, uint8_t len);
|
||||
uint32_t hex_to_uint32(const char *hex, uint8_t len);
|
||||
|
||||
uint8_t ascii_hex_char_to_byte(uint8_t c);
|
||||
//int ascii_hex_string_to_bytes(uint8_t *hex_str, uint8_t *out_bytes, size_t out_len);
|
||||
|
||||
#endif
|
18
hw/usb_interface/ch552_fw/inc/main.h
Normal file
18
hw/usb_interface/ch552_fw/inc/main.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef __MAIN_H__
|
||||
#define __MAIN_H__
|
||||
|
||||
//#define USE_DEBUG_PRINT /* Enable to print debug messages */
|
||||
//#define DEBUG_PRINT_SW /* Enable to print debug messages via FPGA */
|
||||
//#define DEBUG_PRINT_HW /* Enable to print debug messages via UART0 */
|
||||
|
||||
#define USE_NUM_U8
|
||||
//#define USE_NUM_U32
|
||||
|
||||
//#define DEBUG_SETUP /* Enable to debug USB setup flow (printStrSetup,printNumU8HexSetup) */
|
||||
|
||||
#define USE_NUM_U8HEX
|
||||
#define USE_NUM_U16HEX
|
||||
//#define USE_NUM_U32HEX
|
||||
//#define USE_NEGATIVE_NUMS
|
||||
|
||||
#endif
|
39
hw/usb_interface/ch552_fw/inc/print.h
Normal file
39
hw/usb_interface/ch552_fw/inc/print.h
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef __PRINT_H__
|
||||
#define __PRINT_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "main.h"
|
||||
|
||||
void printStr(uint8_t *str);
|
||||
void printChar(uint8_t c);
|
||||
|
||||
#ifdef DEBUG_SETUP
|
||||
#define printStrSetup printStr
|
||||
#define printNumU8HexSetup printNumU8Hex
|
||||
#else
|
||||
#define printStrSetup
|
||||
#define printNumU8HexSetup
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U8
|
||||
void printNumU8(uint8_t num);
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U32
|
||||
void printNumU32(uint32_t num);
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U8HEX
|
||||
void printNumU8Hex(uint8_t num);
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U16HEX
|
||||
void printNumU16Hex(uint16_t num);
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U32HEX
|
||||
void printNumU32Hex(uint32_t num);
|
||||
#endif
|
||||
|
||||
#endif
|
@ -46,19 +46,17 @@ unsigned char FLASH CdcDataInterfaceDesc[] = { // "CDC-Data"
|
||||
'D', 0, 'a', 0, 't', 0, 'a', 0,
|
||||
};
|
||||
|
||||
unsigned char FLASH FidoHidInterfaceDesc[] = { // "FIDO-HID"
|
||||
18, // Length of this descriptor (in bytes)
|
||||
unsigned char FLASH FidoInterfaceDesc[] = { // "FIDO"
|
||||
10, // 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[] = { // "TKEY-Ctrl"
|
||||
20, // Length of this descriptor (in bytes)
|
||||
unsigned char FLASH DebugInterfaceDesc[] = { // "DEBUG"
|
||||
12, // Length of this descriptor (in bytes)
|
||||
0x03, // Descriptor type (String)
|
||||
'T', 0, 'K', 0, 'E', 0, 'Y', 0,
|
||||
'-', 0, 'C', 0, 't', 0, 'r', 0,
|
||||
'l', 0,
|
||||
'D', 0, 'E', 0, 'B', 0, 'U', 0,
|
||||
'G', 0,
|
||||
};
|
||||
|
||||
#endif
|
@ -1,42 +0,0 @@
|
||||
#ifndef __PRINT_H__
|
||||
#define __PRINT_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
//#define DEBUG_PRINT
|
||||
//#define DEBUG_SETUP
|
||||
//#define UART_OUT_DEBUG
|
||||
//#define USE_NUM_U8
|
||||
#define USE_NUM_U32
|
||||
//#define USE_NEGATIVE_NUMS
|
||||
|
||||
void printStr(uint8_t *str);
|
||||
void printChar(uint8_t c);
|
||||
|
||||
#ifdef DEBUG_SETUP
|
||||
#define printStrSetup(x) printStr(x)
|
||||
#define printNumHexSetup(x) printNumHex(x)
|
||||
#else
|
||||
#define printStrSetup(x)
|
||||
#define printNumHexSetup(x)
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U8
|
||||
int8_t uint8_to_str(uint8_t *buf, uint8_t bufsize, uint8_t n);
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U32
|
||||
int8_t uint32_to_str(uint8_t *buf, uint8_t bufsize, uint32_t n);
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U8
|
||||
void printNumU8(uint8_t num);
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U32
|
||||
void printNumU32(uint32_t num);
|
||||
#endif
|
||||
|
||||
void printNumHex(uint8_t num);
|
||||
|
||||
#endif
|
@ -160,108 +160,3 @@ int getchar(void)
|
||||
return SBUF;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Set pin p1.4 and p1.5 to GPIO output mode.
|
||||
void gpio_init()
|
||||
{
|
||||
// p1.4
|
||||
P1_MOD_OC &= ~0x10;
|
||||
P1_DIR_PU |= 0x10;
|
||||
|
||||
// p1.5
|
||||
P1_MOD_OC &= ~0x20;
|
||||
P1_DIR_PU |= 0x20;
|
||||
}
|
||||
|
||||
void gpio_set(uint8_t pin)
|
||||
{
|
||||
switch (pin) {
|
||||
case 0x10: // p1.4
|
||||
P1 |= 0x10;
|
||||
break;
|
||||
case 0x20: // p1.5
|
||||
P1 |= 0x20;
|
||||
break;
|
||||
default: // do nothing, unsupported pin.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_unset(uint8_t pin)
|
||||
{
|
||||
switch (pin) {
|
||||
case 0x10:
|
||||
P1 &= ~0x10;
|
||||
break;
|
||||
case 0x20:
|
||||
P1 &= ~0x20;
|
||||
break;
|
||||
default: // do nothing, unsupported pin.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t gpio_get(uint8_t pin)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
|
||||
switch (pin) {
|
||||
case 0x10: // p1.4
|
||||
ret = P1 & 0x10;
|
||||
break;
|
||||
case 0x20: // p1.5
|
||||
ret = P1 & 0x20;
|
||||
break;
|
||||
default: // do nothing, unsupported pin.
|
||||
ret = 0xff;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Set pin p1.4 to GPIO input mode. (FPGA_CTS)
|
||||
void gpio_init_p1_4_in()
|
||||
{
|
||||
// p1.4
|
||||
P1_MOD_OC &= ~0x10; // Output Mode: 0 = Push-pull output, 1 = Open-drain output
|
||||
P1_DIR_PU &= ~0x10; // Port Direction Control and Pull-up Enable Register:
|
||||
// Push-pull output mode:
|
||||
// 0 = Input.
|
||||
// 1 = Output
|
||||
// Open-drain output mode:
|
||||
// 0 = Pull-up resistor disabled
|
||||
// 1 = Pull-up resistor enabled
|
||||
}
|
||||
|
||||
// Read status of pin 1.4
|
||||
uint8_t gpio_p1_4_get(void)
|
||||
{
|
||||
return (P1 & 0x10); // p1.4
|
||||
}
|
||||
|
||||
// Set pin p1.5 to GPIO output mode. (CH552_CTS)
|
||||
void gpio_init_p1_5_out()
|
||||
{
|
||||
// p1.5
|
||||
P1_MOD_OC &= ~0x20; // Output Mode: 0 = Push-pull output, 1 = Open-drain output
|
||||
P1_DIR_PU |= 0x20; // Port Direction Control and Pull-up Enable Register:
|
||||
// Push-pull output mode:
|
||||
// 0 = Input.
|
||||
// 1 = Output
|
||||
// Open-drain output mode:
|
||||
// 0 = Pull-up resistor disabled
|
||||
// 1 = Pull-up resistor enabled
|
||||
}
|
||||
|
||||
// Set pin 1.5 high
|
||||
void gpio_p1_5_set(void)
|
||||
{
|
||||
P1 |= 0x20;
|
||||
}
|
||||
|
||||
// Set pin 1.5 low
|
||||
void gpio_p1_5_unset(void)
|
||||
{
|
||||
P1 &= ~0x20;
|
||||
}
|
79
hw/usb_interface/ch552_fw/src/flash.c
Normal file
79
hw/usb_interface/ch552_fw/src/flash.c
Normal file
@ -0,0 +1,79 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "ch554.h"
|
||||
|
||||
uint8_t write_code_flash(uint16_t address, uint16_t data)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
|
||||
SAFE_MOD = 0x55;
|
||||
SAFE_MOD = 0xAA; // Enter Safe mode
|
||||
GLOBAL_CFG |= bCODE_WE; // Enable flash-ROM write
|
||||
SAFE_MOD = 0; // Exit Safe mode
|
||||
|
||||
// Set address
|
||||
ROM_ADDR = address;
|
||||
ROM_DATA = data;
|
||||
|
||||
// Check that write operation address is valid
|
||||
if ((ROM_STATUS & bROM_ADDR_OK) == 0) {
|
||||
ret |= 0x01;
|
||||
}
|
||||
|
||||
if (ROM_CTRL & bROM_ADDR_OK) {
|
||||
E_DIS = 1; // Disable global interrupts to prevent Flash operation from being interrupted
|
||||
ROM_CTRL = ROM_CMD_WRITE; // Write
|
||||
E_DIS = 0; // Enable global interrupts
|
||||
}
|
||||
|
||||
// Check operation command error
|
||||
if (ROM_STATUS & bROM_CMD_ERR) {
|
||||
ret |= 0x02;
|
||||
}
|
||||
|
||||
SAFE_MOD = 0x55;
|
||||
SAFE_MOD = 0xAA; // Enter Safe mode
|
||||
GLOBAL_CFG &= ~bCODE_WE; // Disable flash-ROM write
|
||||
SAFE_MOD = 0; // Exit Safe mode
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8_t write_data_flash(uint8_t address, uint8_t data)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
|
||||
EA = 0; // Disable global interrupts to prevent Flash operation from being interrupted
|
||||
|
||||
SAFE_MOD = 0x55;
|
||||
SAFE_MOD = 0xAA; // Enter Safe mode
|
||||
GLOBAL_CFG |= bDATA_WE; // Enable Data-Flash write
|
||||
SAFE_MOD = 0; // Exit Safe mode
|
||||
|
||||
ROM_ADDR_H = DATA_FLASH_ADDR >> 8;
|
||||
ROM_ADDR_L = address << 1;
|
||||
ROM_DATA_L = data;
|
||||
|
||||
// Check that write operation address is valid
|
||||
if ((ROM_STATUS & bROM_ADDR_OK) == 0) {
|
||||
ret |= 0x01;
|
||||
}
|
||||
|
||||
if (ROM_CTRL & bROM_ADDR_OK) {
|
||||
ROM_CTRL = ROM_CMD_WRITE; // Write
|
||||
}
|
||||
|
||||
// Check operation command error
|
||||
if (ROM_STATUS & bROM_CMD_ERR) {
|
||||
ret |= 0x02;
|
||||
}
|
||||
|
||||
SAFE_MOD = 0x55;
|
||||
SAFE_MOD = 0xAA; // Enter Safe mode
|
||||
GLOBAL_CFG &= ~bDATA_WE; // Disable Data-Flash write
|
||||
SAFE_MOD = 0; // Exit Safe mode
|
||||
|
||||
EA = 1; // Enable global interrupts
|
||||
|
||||
return ret;
|
||||
}
|
108
hw/usb_interface/ch552_fw/src/gpio.c
Normal file
108
hw/usb_interface/ch552_fw/src/gpio.c
Normal file
@ -0,0 +1,108 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "ch554.h"
|
||||
|
||||
// Set pin p1.4 and p1.5 to GPIO output mode.
|
||||
void gpio_init()
|
||||
{
|
||||
// p1.4
|
||||
P1_MOD_OC &= ~0x10;
|
||||
P1_DIR_PU |= 0x10;
|
||||
|
||||
// p1.5
|
||||
P1_MOD_OC &= ~0x20;
|
||||
P1_DIR_PU |= 0x20;
|
||||
}
|
||||
|
||||
void gpio_set(uint8_t pin)
|
||||
{
|
||||
switch (pin) {
|
||||
case 0x10: // p1.4
|
||||
P1 |= 0x10;
|
||||
break;
|
||||
case 0x20: // p1.5
|
||||
P1 |= 0x20;
|
||||
break;
|
||||
default: // do nothing, unsupported pin.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void gpio_unset(uint8_t pin)
|
||||
{
|
||||
switch (pin) {
|
||||
case 0x10:
|
||||
P1 &= ~0x10;
|
||||
break;
|
||||
case 0x20:
|
||||
P1 &= ~0x20;
|
||||
break;
|
||||
default: // do nothing, unsupported pin.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t gpio_get(uint8_t pin)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
|
||||
switch (pin) {
|
||||
case 0x10: // p1.4
|
||||
ret = P1 & 0x10;
|
||||
break;
|
||||
case 0x20: // p1.5
|
||||
ret = P1 & 0x20;
|
||||
break;
|
||||
default: // do nothing, unsupported pin.
|
||||
ret = 0xff;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Set pin p1.4 to GPIO input mode. (FPGA_CTS)
|
||||
void gpio_init_p1_4_in()
|
||||
{
|
||||
// p1.4
|
||||
P1_MOD_OC &= ~0x10; // Output Mode: 0 = Push-pull output, 1 = Open-drain output
|
||||
P1_DIR_PU &= ~0x10; // Port Direction Control and Pull-up Enable Register:
|
||||
// Push-pull output mode:
|
||||
// 0 = Input.
|
||||
// 1 = Output
|
||||
// Open-drain output mode:
|
||||
// 0 = Pull-up resistor disabled
|
||||
// 1 = Pull-up resistor enabled
|
||||
}
|
||||
|
||||
// Read status of pin 1.4
|
||||
uint8_t gpio_p1_4_get(void)
|
||||
{
|
||||
return (P1 & 0x10); // p1.4
|
||||
}
|
||||
|
||||
// Set pin p1.5 to GPIO output mode. (CH552_CTS)
|
||||
void gpio_init_p1_5_out()
|
||||
{
|
||||
// p1.5
|
||||
P1_MOD_OC &= ~0x20; // Output Mode: 0 = Push-pull output, 1 = Open-drain output
|
||||
P1_DIR_PU |= 0x20; // Port Direction Control and Pull-up Enable Register:
|
||||
// Push-pull output mode:
|
||||
// 0 = Input.
|
||||
// 1 = Output
|
||||
// Open-drain output mode:
|
||||
// 0 = Pull-up resistor disabled
|
||||
// 1 = Pull-up resistor enabled
|
||||
}
|
||||
|
||||
// Set pin 1.5 high
|
||||
void gpio_p1_5_set(void)
|
||||
{
|
||||
P1 |= 0x20;
|
||||
}
|
||||
|
||||
// Set pin 1.5 low
|
||||
void gpio_p1_5_unset(void)
|
||||
{
|
||||
P1 &= ~0x20;
|
||||
}
|
@ -1,28 +1,6 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "print.h"
|
||||
|
||||
void printStr(uint8_t *str)
|
||||
{
|
||||
#ifdef DEBUG_PRINT
|
||||
while (*str != 0) {
|
||||
CH554UART0SendByte(*str);
|
||||
++str;
|
||||
}
|
||||
#else
|
||||
(void)str;
|
||||
#endif
|
||||
}
|
||||
|
||||
void printChar(uint8_t c)
|
||||
{
|
||||
#ifdef DEBUG_PRINT
|
||||
CH554UART0SendByte(c);
|
||||
#else
|
||||
(void)c;
|
||||
#endif
|
||||
}
|
||||
#include "main.h"
|
||||
|
||||
#ifdef USE_NUM_U8
|
||||
int8_t uint8_to_str(uint8_t *buf, uint8_t bufsize, uint8_t n)
|
||||
@ -132,57 +110,91 @@ int8_t uint32_to_str(uint8_t *buf, uint8_t bufsize, uint32_t n)
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U8
|
||||
void printNumU8(uint8_t num)
|
||||
#if 0
|
||||
uint8_t hex_to_uint8(const char *hex, uint8_t len)
|
||||
{
|
||||
#ifdef DEBUG_PRINT
|
||||
uint8_t num_str[4] = { 0 };
|
||||
int8_t ret;
|
||||
ret = uint8_to_str(num_str, 4, num);
|
||||
if (!ret) {
|
||||
printStr(num_str);
|
||||
uint8_t val = 0;
|
||||
while (len) {
|
||||
char c = *hex++;
|
||||
val <<= 4;
|
||||
if (c >= '0' && c <= '9') {
|
||||
val |= c - '0';
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
val |= c - 'A' + 10;
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
val |= c - 'a' + 10;
|
||||
} else {
|
||||
return 0; // Invalid character
|
||||
}
|
||||
len--;
|
||||
}
|
||||
#endif
|
||||
return val;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U32
|
||||
void printNumU32(uint32_t num)
|
||||
#if 0
|
||||
uint16_t hex_to_uint16(const char *hex, uint8_t len)
|
||||
{
|
||||
#ifdef DEBUG_PRINT
|
||||
uint8_t num_str[11] = { 0 };
|
||||
int8_t ret;
|
||||
ret = uint32_to_str(num_str, 10, num);
|
||||
if (!ret) {
|
||||
printStr(num_str);
|
||||
uint16_t val = 0;
|
||||
while (len) {
|
||||
char c = *hex++;
|
||||
val <<= 4;
|
||||
if (c >= '0' && c <= '9') {
|
||||
val |= c - '0';
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
val |= c - 'A' + 10;
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
val |= c - 'a' + 10;
|
||||
} else {
|
||||
return 0; // Invalid character
|
||||
}
|
||||
len--;
|
||||
}
|
||||
#else
|
||||
(void)num;
|
||||
#endif
|
||||
return val;
|
||||
}
|
||||
#endif
|
||||
|
||||
void printNumHex(uint8_t num)
|
||||
uint32_t hex_to_uint32(const char *hex, uint8_t len)
|
||||
{
|
||||
#ifdef DEBUG_PRINT
|
||||
// High nibble
|
||||
uint8_t val = num >> 4;
|
||||
if (val <= 9) {
|
||||
val = val + '0';
|
||||
} else {
|
||||
val = (val-10) + 'A';
|
||||
uint32_t val = 0;
|
||||
while (len) {
|
||||
char c = *hex++;
|
||||
val <<= 4;
|
||||
if (c >= '0' && c <= '9') {
|
||||
val |= c - '0';
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
val |= c - 'A' + 10;
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
val |= c - 'a' + 10;
|
||||
} else {
|
||||
return 0; // Invalid character
|
||||
}
|
||||
len--;
|
||||
}
|
||||
printChar(val);
|
||||
|
||||
// Low nibble
|
||||
val = num & 0x0F;
|
||||
if (val <= 9) {
|
||||
val = val + '0';
|
||||
} else {
|
||||
val = (val-10) + 'A';
|
||||
}
|
||||
printChar(val);
|
||||
#else
|
||||
(void)num;
|
||||
#endif
|
||||
return val;
|
||||
}
|
||||
|
||||
uint8_t ascii_hex_char_to_byte(uint8_t c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
return c - '0';
|
||||
if (c >= 'A' && c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
if (c >= 'a' && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
return 0; // Invalid character, should not happen if input is valid
|
||||
}
|
||||
|
||||
#if 0
|
||||
int ascii_hex_string_to_bytes(uint8_t *hex_str, uint8_t *out_bytes, size_t out_len)
|
||||
{
|
||||
if (!hex_str || !out_bytes || out_len < 32)
|
||||
return -1; // Error handling
|
||||
|
||||
for (size_t i = 0; i < 32; i++) {
|
||||
out_bytes[i] = (ascii_hex_char_to_byte(hex_str[i * 2]) << 4) | ascii_hex_char_to_byte(hex_str[i * 2 + 1]);
|
||||
}
|
||||
|
||||
return 0; // Success
|
||||
}
|
||||
#endif
|
File diff suppressed because it is too large
Load Diff
144
hw/usb_interface/ch552_fw/src/print.c
Normal file
144
hw/usb_interface/ch552_fw/src/print.c
Normal file
@ -0,0 +1,144 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "io.h"
|
||||
#include "lib.h"
|
||||
#include "main.h"
|
||||
#include "mem.h"
|
||||
#include "print.h"
|
||||
|
||||
void printStr(uint8_t *str)
|
||||
{
|
||||
#ifdef USE_DEBUG_PRINT
|
||||
#if defined(DEBUG_PRINT_HW)
|
||||
while (*str != 0) {
|
||||
CH554UART0SendByte(*str);
|
||||
++str;
|
||||
}
|
||||
#elif defined(DEBUG_PRINT_SW)
|
||||
uint32_t str_len = strlen(str);
|
||||
CH554UART1SendByte(IO_CH552);
|
||||
CH554UART1SendByte(str_len);
|
||||
CH554UART1SendBuffer(str, str_len);
|
||||
#endif
|
||||
#else
|
||||
(void)str;
|
||||
#endif
|
||||
}
|
||||
|
||||
void printChar(uint8_t c)
|
||||
{
|
||||
#ifdef USE_DEBUG_PRINT
|
||||
#if defined(DEBUG_PRINT_HW)
|
||||
CH554UART0SendByte(c);
|
||||
#elif defined(DEBUG_PRINT_SW)
|
||||
CH554UART1SendByte(IO_CH552);
|
||||
CH554UART1SendByte(1);
|
||||
CH554UART1SendByte(c);
|
||||
#endif
|
||||
#else
|
||||
(void)c;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_NUM_U8
|
||||
void printNumU8(uint8_t num)
|
||||
{
|
||||
#ifdef USE_DEBUG_PRINT
|
||||
int8_t ret;
|
||||
uint8_t num_str[4] = { 0 };
|
||||
ret = uint8_to_str(num_str, 4, num);
|
||||
if (!ret) {
|
||||
printStr(num_str);
|
||||
}
|
||||
#else
|
||||
(void)num;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U32
|
||||
void printNumU32(uint32_t num)
|
||||
{
|
||||
#ifdef USE_DEBUG_PRINT
|
||||
int8_t ret;
|
||||
uint8_t num_str[11] = { 0 };
|
||||
ret = uint32_to_str(num_str, 10, num);
|
||||
if (!ret) {
|
||||
printStr(num_str);
|
||||
}
|
||||
#else
|
||||
(void)num;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void printNumHex(uint8_t num)
|
||||
{
|
||||
#ifdef USE_DEBUG_PRINT
|
||||
// High nibble
|
||||
uint8_t val = num >> 4;
|
||||
if (val <= 9) {
|
||||
val = val + '0';
|
||||
} else {
|
||||
val = (val-10) + 'A';
|
||||
}
|
||||
printChar(val);
|
||||
|
||||
// Low nibble
|
||||
val = num & 0x0F;
|
||||
if (val <= 9) {
|
||||
val = val + '0';
|
||||
} else {
|
||||
val = (val-10) + 'A';
|
||||
}
|
||||
printChar(val);
|
||||
#else
|
||||
(void)num;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_NUM_U8HEX
|
||||
void printNumU8Hex(uint8_t num)
|
||||
{
|
||||
#ifdef USE_DEBUG_PRINT
|
||||
printStr("0x");
|
||||
printNumHex(num);
|
||||
#else
|
||||
(void)num;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U16HEX
|
||||
void printNumU16Hex(uint16_t num)
|
||||
{
|
||||
#ifdef USE_DEBUG_PRINT
|
||||
uint8_t buf[2];
|
||||
memcpy(buf, &num, 2);
|
||||
printStr("0x");
|
||||
for (int8_t i = 1; i > -1; i--) {
|
||||
printNumHex(buf[i]);
|
||||
}
|
||||
#else
|
||||
(void)num;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUM_U32HEX
|
||||
void printNumU32Hex(uint32_t num)
|
||||
{
|
||||
#ifdef USE_DEBUG_PRINT
|
||||
uint8_t buf[4];
|
||||
memcpy(buf, &num, 4);
|
||||
printStr("0x");
|
||||
for (int8_t i = 3; i > -1; i--) {
|
||||
printNumHex(buf[i]);
|
||||
}
|
||||
#else
|
||||
(void)num;
|
||||
#endif
|
||||
}
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user