Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wagiminator committed May 19, 2023
1 parent 06b7f02 commit 9d200e5
Show file tree
Hide file tree
Showing 34 changed files with 5,484 additions and 2 deletions.
3 changes: 3 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send
a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
66 changes: 64 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,64 @@
# CH552-DAPLink
CMSIS-DAP compliant debugging probe with SWD, JTAG, and VCP support
# CH552 DAPLink
The DAPLink, based on the CH552 microcontroller, is a debugging probe that follows the CMSIS-DAP standard. It supports both SWD and JTAG protocols, making it suitable for programming Microchip SAM and other ARM-based microcontrollers. In addition, it includes a Virtual COM Port (VCP) which adds an extra debugging capability. The SWD firmware is derived from [Ralph Doncaster's](https://github.com/nerdralph/ch554_sdcc/tree/master/examples/CMSIS_DAP) DAPLink implementation for CH55x microcontrollers, along with Deqing Sun's [CH55xduino](https://github.com/DeqingSun/ch55xduino) port.

![DAPLink_pic1.jpg](https://raw.githubusercontent.com/wagiminator/CH552-DAPLink/main/documentation/DAPLink_pic1.jpg)

# CMSIS-DAP
CMSIS-DAP provides a standardized way to access the Coresight Debug Access Port (DAP) of an ARM Cortex microcontroller via USB. CMSIS-DAP is generally implemented as an on-board interface chip, providing direct USB connection from a development board to a debugger running on a host computer on one side, and over JTAG (Joint Test Action Group) or SWD (Serial Wire Debug) to the target device to access the Coresight DAP on the other. As a USB HID compliant device, it typically does not require any drivers for the operating system. For more information refer to the [CMSIS-DAP Handbook](https://os.mbed.com/handbook/CMSIS-DAP).

![CMSIS-DAP.png](https://raw.githubusercontent.com/wagiminator/CH552-DAPLink/main/documentation/DAPLink_CMSIS-DAP.png)

# Compiling and Installing Firmware
## Preparing the CH55x Bootloader
### Installing Drivers for the CH55x Bootloader
On Linux you do not need to install a driver. However, by default Linux will not expose enough permission to upload your code with the USB bootloader. In order to fix this, open a terminal and run the following commands:

```
echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="4348", ATTR{idProduct}=="55e0", MODE="666"' | sudo tee /etc/udev/rules.d/99-ch55x.rules
sudo service udev restart
```

For Windows, you need the [CH372 driver](http://www.wch-ic.com/downloads/CH372DRV_EXE.html). Alternatively, you can also use the [Zadig Tool](https://zadig.akeo.ie/) to install the correct driver. Here, click "Options" and "List All Devices" to select the USB module, and then install the libusb-win32 driver. To do this, the board must be connected and the CH55x must be in bootloader mode.

### Entering CH55x Bootloader Mode
A brand new chip starts automatically in bootloader mode as soon as it is connected to the PC via USB. Once firmware has been uploaded, the bootloader must be started manually for new uploads. To do this, the board must first be disconnected from the USB port and all voltage sources. Now press the BOOT button and keep it pressed while reconnecting the board to the USB port of your PC. The chip now starts again in bootloader mode, the BOOT button can be released and new firmware can be uploaded within the next couple of seconds.

## Compiling and Uploading using the makefile
### Installing SDCC Toolchain for CH55x
Install the [SDCC Compiler](https://sdcc.sourceforge.net/). In order for the programming tool to work, Python3 must be installed on your system. To do this, follow these [instructions](https://www.pythontutorial.net/getting-started/install-python/). In addition [pyusb](https://github.com/pyusb/pyusb) must be installed. On Linux (Debian-based), all of this can be done with the following commands:

```
sudo apt install build-essential sdcc python3 python3-pip
sudo pip install pyusb
```

### Compiling and Uploading Firmware
- Open a terminal.
- Navigate to the folder with the makefile.
- Connect the board and make sure the CH552 is in bootloader mode.
- Run ```make flash``` to compile and upload the firmware.
- If you don't want to compile the firmware yourself, you can also upload the precompiled binary. To do this, just run ```python3 ./tools/chprog.py daplink.bin```.

# Operating Instructions
Connect the DAPLink to the target board via the pin header. You can supply power via the 3V3 pin or the 5V pin (max 400 mA). Plug the DAPLink into a USB port on your PC. Since it is recognized as a Human Interface Device (HID), no driver installation is required. However, Windows users may need to install a CDC driver for the Virtual COM Port (VCP) using the [Zadig Tool](https://zadig.akeo.ie/). The DAPLink should work with any debugging software that supports CMSIS-DAP (e.g. OpenOCD or PyOCD). Of course, it also works with the [SAMD DevBoards](https://github.com/wagiminator/SAMD-Development-Boards) in the Arduino IDE (Tools -> Programmer -> Generic CMSIS-DAP). The virtual COM port (8N1 only) can be used with any serial monitor.

# References, Links and Notes
1. [EasyEDA Design Files](https://oshwlab.com/wagiminator/ch552-swd-programmer)
2. [ARMmbed DAPLink](https://github.com/ARMmbed/DAPLink)
3. [CH55xduino](https://github.com/DeqingSun/ch55xduino)
4. [Ralph Doncaster's Implementation](https://github.com/nerdralph/ch554_sdcc/tree/master/examples/CMSIS_DAP)
5. [CMSIS-DAP Handbook](https://os.mbed.com/handbook/CMSIS-DAP)
6. [SDCC Compiler](https://sdcc.sourceforge.net/)
7. [CH55x SDK for SDCC](https://github.com/Blinkinlabs/ch554_sdcc)
8. [OpenOCD](http://openocd.org/)
9. [SAMD DevBoards](https://github.com/wagiminator/SAMD-Development-Boards)
10. [picoDAP](https://github.com/wagiminator/CH552-picoDAP)

![DAPLink_pic2.jpg](https://raw.githubusercontent.com/wagiminator/CH552-DAPLink/main/documentation/DAPLink_pic2.jpg)
![DAPLink_pic3.jpg](https://raw.githubusercontent.com/wagiminator/CH552-DAPLink/main/documentation/DAPLink_pic3.jpg)

# License
![license.png](https://i.creativecommons.org/l/by-sa/3.0/88x31.png)

This work is licensed under Creative Commons Attribution-ShareAlike 3.0 Unported License.
(http://creativecommons.org/licenses/by-sa/3.0/)
Binary file added documentation/DAPLink_CMSIS-DAP.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/DAPLink_pic1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/DAPLink_pic2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/DAPLink_pic3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added documentation/DAPLink_pic4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hardware/DAPLink_BOM.tsv
Binary file not shown.
Binary file added hardware/DAPLink_gerber.zip
Binary file not shown.
Binary file added hardware/DAPLink_schematic.pdf
Binary file not shown.
Binary file added software/daplink.bin
Binary file not shown.
102 changes: 102 additions & 0 deletions software/daplink.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// ===================================================================================
// Project: DAPLink - CMSIS-DAP compliant debugging probe with VCP based on CH552
// Version: v1.0
// Year: 2023
// Author: Stefan Wagner
// Github: https://github.com/wagiminator
// EasyEDA: https://easyeda.com/wagiminator
// License: http://creativecommons.org/licenses/by-sa/3.0/
// ===================================================================================
//
// Description:
// ------------
// The CH552-based DAPLink is a CMSIS-DAP compliant debugging probe with SWD and JTAG
// protocol support. It can be used to program Microchip SAM and other ARM-based
// microcontrollers. The additional Virtual COM Port (VCP) provides an additional
// debugging feature. The SWD-part of the firmware is based on Ralph Doncaster's
// DAPLink-implementation for CH55x microcontrollers and Deqing Sun's CH55xduino port.
//
// References:
// -----------
// - Blinkinlabs: https://github.com/Blinkinlabs/ch554_sdcc
// - Deqing Sun: https://github.com/DeqingSun/ch55xduino
// - Ralph Doncaster: https://github.com/nerdralph/ch554_sdcc
// - WCH Nanjing Qinheng Microelectronics: http://wch.cn
// - ARMmbed DAPLink: https://github.com/ARMmbed/DAPLink
// - picoDAP: https://github.com/wagiminator/CH552-picoDAP
//
// Compilation Instructions:
// -------------------------
// - Chip: CH552
// - Clock: 16 MHz internal
// - Adjust the firmware parameters in src/config.h if necessary.
// - Make sure SDCC toolchain and Python3 with PyUSB is installed.
// - Press BOOT button on the board and keep it pressed while connecting it via USB
// with your PC.
// - Run 'make flash' immediatly afterwards.
//
// Operating Instructions:
// -----------------------
// Connect the DAPLink to the target board via the pin header. You can supply power
// via the 3V3 pin or the 5V pin (max 400 mA). Plug the DAPLink into a USB port on
// your PC. Since it is recognized as a Human Interface Device (HID), no driver
// installation is required. However, Windows users may need to install a CDC driver
// for the Virtual COM Port (VCP). The DAPLink should work with any debugging software
// that supports CMSIS-DAP (e.g. OpenOCD or PyOCD). The virtual COM port (8N1 only)
// can be used with any serial monitor.


// ===================================================================================
// Libraries, Definitions and Macros
// ===================================================================================

// Libraries
#include "src/system.h" // system functions
#include "src/delay.h" // delay functions
#include "src/dap.h" // CMSIS-DAP functions
#include "src/usb_cdc.h" // USB CDC functions
#include "src/uart.h" // UART functions

// Prototypes for used interrupts
void USB_interrupt(void);
void USB_ISR(void) __interrupt(INT_NO_USB) {
USB_interrupt();
}

void UART_interrupt(void);
void UART_ISR(void) __interrupt(INT_NO_UART0) {
UART_interrupt();
}

// Number of received bytes in endpoint
extern volatile __xdata uint8_t HID_byteCount;

// ===================================================================================
// Main Function
// ===================================================================================
void main(void) {
// Setup
CLK_config(); // configure system clock
DLY_ms(10); // wait for clock to settle
UART_init(); // init UART
DAP_init(); // init CMSIS-DAP
CDC_init(); // init virtual COM

// Loop
while(1) {
// Handle DAP
if(HID_byteCount && !UEP1_T_LEN) { // DAP packet received and out buffer empty?
DAP_Thread(); // handle DAP packet
HID_byteCount = 0; // clear byte counter
UEP1_T_LEN = 64; // Windows hangs if smaller
UEP1_CTRL = UEP1_CTRL & ~(MASK_UEP_R_RES | MASK_UEP_T_RES); // send/receive package
}

// Handle virtual COM
if(CDC_available() && UART_ready()) UART_write(CDC_read());
if(UART_available()) {
while(UART_available()) CDC_write(UART_read());
CDC_flush();
}
}
}
88 changes: 88 additions & 0 deletions software/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# ===================================================================================
# Project: DAPLink CMSIS-DAP compliant debugging probe based on CH552
# Author: Stefan Wagner
# Year: 2023
# URL: https://github.com/wagiminator
# ===================================================================================
# Type "make help" in the command line.
# ===================================================================================

# Input and Output File Names
SKETCH = daplink.c
TARGET = daplink
INCLUDE = src

# Microcontroller Settings
FREQ_SYS = 16000000
XRAM_LOC = 0x0200
XRAM_SIZE = 0x0200
CODE_SIZE = 0x3800

# Toolchain
CC = sdcc
OBJCOPY = objcopy
PACK_HEX = packihx
WCHISP ?= python3 tools/chprog.py

# Compiler Flags
CFLAGS = -mmcs51 --model-small --no-xinit-opt
CFLAGS += --xram-size $(XRAM_SIZE) --xram-loc $(XRAM_LOC) --code-size $(CODE_SIZE)
CFLAGS += -I$(INCLUDE) -DF_CPU=$(FREQ_SYS)
CFILES = $(SKETCH) $(wildcard $(INCLUDE)/*.c)
RFILES = $(CFILES:.c=.rel)
CLEAN = rm -f *.ihx *.lk *.map *.mem *.lst *.rel *.rst *.sym *.asm *.adb

# Symbolic Targets
help:
@echo "Use the following commands:"
@echo "make all compile, build and keep all files"
@echo "make hex compile and build $(TARGET).hex"
@echo "make bin compile and build $(TARGET).bin"
@echo "make flash compile, build and upload $(TARGET).bin to device"
@echo "make clean remove all build files"

%.rel : %.c
@echo "Compiling $< ..."
@$(CC) -c $(CFLAGS) $<

$(TARGET).ihx: $(RFILES)
@echo "Building $(TARGET).ihx ..."
@$(CC) $(notdir $(RFILES)) $(CFLAGS) -o $(TARGET).ihx

$(TARGET).hex: $(TARGET).ihx
@echo "Building $(TARGET).hex ..."
@$(PACK_HEX) $(TARGET).ihx > $(TARGET).hex

$(TARGET).bin: $(TARGET).ihx
@echo "Building $(TARGET).bin ..."
@$(OBJCOPY) -I ihex -O binary $(TARGET).ihx $(TARGET).bin

flash: $(TARGET).bin size removetemp
@echo "Uploading to CH55x ..."
@$(WCHISP) $(TARGET).bin

all: $(TARGET).bin $(TARGET).hex size

hex: $(TARGET).hex size removetemp

bin: $(TARGET).bin size removetemp

bin-hex: $(TARGET).bin $(TARGET).hex size removetemp

install: flash

size:
@echo "------------------"
@echo "FLASH: $(shell awk '$$1 == "ROM/EPROM/FLASH" {print $$4}' $(TARGET).mem) bytes"
@echo "IRAM: $(shell awk '$$1 == "Stack" {print 248-$$10}' $(TARGET).mem) bytes"
@echo "XRAM: $(shell awk '$$1 == "EXTERNAL" {print $(XRAM_LOC)+$$5}' $(TARGET).mem) bytes"
@echo "------------------"

removetemp:
@echo "Removing temporary files ..."
@$(CLEAN)

clean:
@echo "Cleaning all up ..."
@$(CLEAN)
@rm -f $(TARGET).hex $(TARGET).bin
Loading

0 comments on commit 9d200e5

Please sign in to comment.