Skip to content

Commit 684bffc

Browse files
committed
Some updates
1 parent ddca3e7 commit 684bffc

File tree

12 files changed

+816
-541
lines changed

12 files changed

+816
-541
lines changed

picoISP_Programmer/software/picoasp/tools/chprog.py

100755100644
Lines changed: 201 additions & 167 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Programming the MCU using the factory built-in USB Bootloader
2+
## Installing Drivers for the Bootloader
3+
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:
4+
5+
```
6+
echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="4348", ATTR{idProduct}=="55e0", MODE="666"' | sudo tee /etc/udev/rules.d/99-ch55x.rules
7+
sudo udevadm control --reload-rules
8+
```
9+
10+
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" -> "List All Devices" and select the USB module. Then install the libusb-win32 driver. To do this, the board must be connected and the microcontroller must be in bootloader mode.
11+
12+
## Entering Bootloader Mode
13+
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 in bootloader mode, the BOOT button can be released and new firmware can be uploaded via USB. Alternatively, you can leave the board connected to the USB port, press and hold the BOOT button, press and release the RESET button and then release the BOOT button to enter the bootloader mode.
14+
15+
## WCHISPTool
16+
WCH offers the free but closed-source software [WCHISPTool](https://www.wch.cn/downloads/WCHISPTool_Setup_exe.html) to upload firmware with Windows via the USB bootloader.
17+
18+
## chprog.py
19+
You can also use chprog.py, a simple Python tool provided in this folder, to flash the microcontroller via the built-in USB bootloader. In order for this 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:
20+
21+
```
22+
sudo apt install python3 python3-pip
23+
python3 -m pip install pyusb
24+
```
25+
26+
```
27+
Usage example:
28+
python3 chprog.py firmware.bin
29+
```
30+
31+
## Alternative Software Tools
32+
- [isp55e0](https://github.com/frank-zago/isp55e0)
33+
- [wchisp](https://github.com/ch32-rs/wchisp)

picoISP_Programmer/software/picoisp/makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
# Type "make help" in the command line.
88
# ===================================================================================
99

10-
# Input and Output File Names
11-
SKETCH = picoisp.c
10+
# Files and Folders
11+
MAINFILE = picoisp.c
1212
TARGET = picoisp
1313
INCLUDE = src
14+
TOOLS = tools
1415

1516
# Microcontroller Settings
1617
FREQ_SYS = 16000000
@@ -22,13 +23,12 @@ CODE_SIZE = 0x3800
2223
CC = sdcc
2324
OBJCOPY = objcopy
2425
PACK_HEX = packihx
25-
WCHISP ?= python3 tools/chprog.py
26+
ISPTOOL ?= python3 $(TOOLS)/chprog.py
2627

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

@@ -59,7 +59,7 @@ $(TARGET).bin: $(TARGET).ihx
5959

6060
flash: $(TARGET).bin size removetemp
6161
@echo "Uploading to CH55x ..."
62-
@$(WCHISP) $(TARGET).bin
62+
@$(ISPTOOL) $(TARGET).bin
6363

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

picoISP_Programmer/software/picoisp/src/gpio.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ===================================================================================
2-
// Basic GPIO, PWM and ADC Functions for CH551, CH552 and CH554 * v1.4 *
2+
// Basic GPIO, PWM and ADC Functions for CH551, CH552 and CH554 * v1.5 *
33
// ===================================================================================
44
//
55
// Pins must be defined as P10, P11, P12, etc. - e.g.:
@@ -107,6 +107,9 @@ SBIT(PP37, 0xB0, 7);
107107
((PIN>=P30)&&(PIN<=P37) ? (P3_DIR_PU &= ~(1<<(PIN&7)), P3_MOD_OC &= ~(1<<(PIN&7))) : \
108108
(0)))
109109

110+
#define PIN_input_HI PIN_input
111+
#define PIN_input_FL PIN_input
112+
110113
// ===================================================================================
111114
// Set pin as INPUT with internal PULLUP resistor (also open-drain output,
112115
// when output changes from LOW to HIGH, it will drive HIGH for 2 clock cycles)
@@ -124,6 +127,8 @@ SBIT(PP37, 0xB0, 7);
124127
((PIN>=P30)&&(PIN<=P37) ? (P3_MOD_OC &= ~(1<<(PIN&7)), P3_DIR_PU |= (1<<(PIN&7))) : \
125128
(0)))
126129

130+
#define PIN_output_PP PIN_output
131+
127132
// ===================================================================================
128133
// Set pin as OPEN-DRAIN OUTPUT (also high-impedance input, no pullup)
129134
// ===================================================================================

picoISP_Programmer/software/picoisp/src/system.h

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
// ===================================================================================
2-
// Basic System Functions for CH551, CH552 and CH554 * v1.4 *
2+
// Basic System Functions for CH551, CH552 and CH554 * v1.6 *
33
// ===================================================================================
44
//
5-
// Functions available:
6-
// --------------------
5+
// System clock (CLK)functions available:
6+
// --------------------------------------
77
// CLK_config() set system clock frequency according to F_CPU
88
// CLK_external() set external crystal as clock source
99
// CLK_internal() set internal oscillator as clock source
1010
//
11+
// Watchdog Timer (WDT) functions available:
12+
// -----------------------------------------
1113
// WDT_start() start watchdog timer with full period
1214
// WDT_stop() stop watchdog timer
1315
// WDT_reset() reload watchdog timer with full period
1416
// WDT_set(time) reload watchdog timer with specified time in ms
1517
// WDT_feed(value) reload watchdog timer with specified value
1618
//
17-
// BOOT_now() enter bootloader
18-
// SLEEP_now() put device into sleep
19+
// Reset (RST) and bootloader functions available:
20+
// -----------------------------------------------
1921
// RST_now() perform software reset
20-
//
2122
// RST_keep(value) keep this value after RESET
2223
// RST_getKeep() read the keeped value
2324
// RST_wasWDT() check if last RESET was caused by watchdog timer
2425
// RST_wasPIN() check if last RESET was caused by RST PIN
2526
// RST_wasPWR() check if last RESET was caused by power-on
2627
// RST_wasSOFT() check if last RESET was caused by software
2728
//
29+
// BOOT_now() enter bootloader
30+
//
31+
// Sleep functions available:
32+
// --------------------------
33+
// SLEEP_now() put device into sleep
34+
//
2835
// WAKE_enable(source) enable wake-up from sleep source (sources see below)
2936
// WAKE_disable(source) disable wake-up from sleep source
3037
// WAKE_all_disable() disable all wake-up sources
@@ -58,14 +65,20 @@
5865
// WAKE_RST wake-up by pin RST high level
5966
// WAKE_INT wake-up by pin P3.2 edge or pin P3.3 low level
6067
//
68+
// Interrupt (INT) functions available:
69+
// ------------------------------------
70+
// INT_enable() global interrupt enable
71+
// INT_disable() global interrupt disable
72+
// INT_ATOMIC_BLOCK { } execute block without being interrupted
73+
//
6174
// 2023 by Stefan Wagner: https://github.com/wagiminator
6275

6376
#pragma once
6477
#include <stdint.h>
6578
#include "ch554.h"
6679

6780
// ===================================================================================
68-
// System Clock
81+
// System Clock (CLK) Functions
6982
// ===================================================================================
7083
inline void CLK_config(void) {
7184
SAFE_MOD = 0x55;
@@ -117,7 +130,7 @@ inline void CLK_inernal(void) {
117130
}
118131

119132
// ===================================================================================
120-
// Watchdog Timer
133+
// Watchdog Timer (WDT) Functions
121134
// ===================================================================================
122135
#define WDT_reset() WDOG_COUNT = 0
123136
#define WDT_feed(value) WDOG_COUNT = value
@@ -139,7 +152,7 @@ inline void WDT_stop(void) {
139152
}
140153

141154
// ===================================================================================
142-
// Reset
155+
// Reset (RST) Functions
143156
// ===================================================================================
144157
#define RST_keep(value) RESET_KEEP = value
145158
#define RST_getKeep() (RESET_KEEP)
@@ -155,16 +168,28 @@ inline void RST_now(void) {
155168
}
156169

157170
// ===================================================================================
158-
// Bootloader
171+
// Bootloader (BOOT) Functions
159172
// ===================================================================================
160173
inline void BOOT_now(void) {
161174
__asm
162-
lcall #BOOT_LOAD_ADDR
175+
ljmp #BOOT_LOAD_ADDR
163176
__endasm;
164177
}
165178

179+
inline void BOOT_prepare(void) {
180+
ES = 0;
181+
PS = 0;
182+
TMOD = 0;
183+
P1_DIR_PU = 0;
184+
P1_MOD_OC = 0;
185+
P1 = 0xFF;
186+
USB_INT_EN = 0;
187+
USB_CTRL = 0x06;
188+
EA = 0;
189+
}
190+
166191
// ===================================================================================
167-
// Sleep
192+
// Sleep Functions
168193
// ===================================================================================
169194
#define SLEEP_now() PCON |= PD
170195

@@ -198,3 +223,10 @@ inline void BOOT_now(void) {
198223
#define WAKE_P15_disable() WAKE_CTRL &= ~bWAK_P1_5_LO
199224
#define WAKE_RST_disable() WAKE_CTRL &= ~bWAK_RST_HI
200225
#define WAKE_INT_disable() WAKE_CTRL &= ~bWAK_P3_2E_3L
226+
227+
// ===================================================================================
228+
// Interrupt (INT) Functions
229+
// ===================================================================================
230+
#define INT_enable() EA = 1
231+
#define INT_disable() EA = 0
232+
#define INT_ATOMIC_BLOCK for(EA=0;!EA;EA=1)

0 commit comments

Comments
 (0)