










| Board Pin | Function | Board Pin | Function |
|---|---|---|---|
| 0 | GND | 1 | 5V |
| 2 | GND | 3 | 5V |
| 4 | GND | 5 | 5V |
| 6 | GND | 7 | 5V |
| 8 | GND | 9 | 5V |
| 10 | GND | 11 | 5V |
| 12 | NC | 13 | NC |
| 14 | NC | 15 | NC |
| 16 | NC | 17 | NC |
| 18 | NC | 19 | NC |
| 20 | NC | 21 | NC |
| 22 | NC | 23 | NC |
| 24 | NC | 25 | NC |
| 26 | NC | 27 | NC |
| 28 | NC | 29 | NC |
| 30 | NC | 31 | NC |
| 32 | NC | 33 | NC |
| 34 | NC | 35 | NC |
| 36 | NC | 37 | NC |
| 38 | GND | 39 | GND |
| 40 | A8 | 41 | P11 |
| 42 | NC | 43 | P12 |
| 44 | NC | 45 | N12 |
| 46 | NC | 47 | P13 |
| 48 | B8 | 49 | N13 |
| 50 | A7 | 51 | P14 |
| 52 | NC | 53 | M12 |
| 54 | GND | 55 | GND |
| 56 | B7 | 57 | M13 |
| 58 | A6 | 59 | L14 |
| 60 | B6 | 61 | L13 |
| 62 | A5 | 63 | K14 |
| 64 | B5 | 65 | K13 |
| 66 | A4 | 67 | J14 |
| 68 | B4 | 69 | J13 |
| 70 | A3 | 71 | H14 |
| 72 | B3 | 73 | H13 |
| 74 | A2 | 75 | G14 |
| 76 | B1 | 77 | G13 |
| 78 | B2 | 79 | F14 |
| 80 | C1 | 81 | F13 |
| 82 | C2 | 83 | E14 |
| 84 | D1 | 85 | E13 |
| 86 | D3 | 87 | E12 |
| 88 | E1 | 89 | C13 |
| 90 | E2 | 91 | D13 |
| 92 | F1 | 93 | C12 |
| 94 | F2 | 95 | D12 |
| 96 | G1 | 97 | C11 |
| 98 | G2 | 99 | D11 |
| 100 | H2 | 101 | C10 |
| 102 | J1 | 103 | D10 |
| 104 | GND | 105 | GND |
| 106 | GND | 107 | GND |
| 108 | J2 | 109 | C9 |
| 110 | K1 | 111 | D9 |
| 112 | K2 | 113 | C8 |
| 114 | L1 | 115 | D8 |
| 116 | L2 | 117 | C7 |
| 118 | M1 | 119 | D7 |
| 120 | M2 | 121 | C6 |
| 122 | N1 | 123 | D6 |
| 124 | N3 | 125 | C5 |
| 126 | P1 | 127 | D5 |
| 128 | P2 | 129 | C4 |
| 130 | R1 | 131 | D4 |
| 132 | R2 | 133 | C3 |
| 134 | T2 | 135 | E4 |
| 136 | R3 | 137 | E3 |
| 138 | T3 | 139 | F4 |
| 140 | R4 | 141 | F3 |
| 142 | T4 | 143 | G4 |
| 144 | R5 | 145 | G3 |
| 146 | R6 | 147 | H3 |
| 148 | T6 | 149 | J4 |
| 150 | P7 | 151 | J3 |
| 152 | R7 | 153 | K4 |
| 154 | R8 | 155 | K3 |
| 156 | GND | 157 | GND |
| 158 | NC | 159 | NC |
| 160 | NC | 161 | NC |
| 162 | NC | 163 | NC |
| 164 | NC | 165 | NC |
| 166 | NC | 167 | NC |
| 168 | NC | 169 | NC |
| 170 | NC | 171 | NC |
| 172 | NC | 173 | NC |
| 174 | NC | 175 | NC |
單看市場大盤,超過 HK$4,000 一個 Lead 屬於極端偏高(異常水平);但最終是否合理,取決於貴公司的客單價(毛利)以及該 Lead 的定義與質量。
1. 香港 Facebook 廣告市場參考基準(2026年數據)
| 廣告類型 / 行業 | 香港市場常見 CPL / CPA 範圍 | 備註 |
|---|---|---|
| 一般服務業(查詢 / WhatsApp) | HK55–HK280 | 超過 HK$900 通常代表設定或素材出問題 |
| 電商 / 零售(直接購買 CPA) | HK100–HK220 | 視乎產品單價及受眾精準度 |
| 高競爭行業(金融、地產、醫美、教育) | HK300–HK1,500+ | 競爭激烈導致 CPM 較高,但極少突破 HK$4,000 |
| 高單價 B2B / 企業級方案 | HK1,000–HK5,000+ | 客群極窄,若一單合約價值數十萬則屬正常 |
2. 評估 HK$4,000/Lead 的核心算式(回本線)
判斷貴不貴,直接用單位經濟效益(Unit Economics)反推:
可承受最高 CPL=每單平均毛利×銷售成交率(Close Rate)可承受最高 CPL=每單平均毛利×銷售成交率(Close Rate)
- 合理情況(高客單價 B2B / 企業服務): 假設公司賣的是企業 IT 系統、工程方案或高階顧問服務,每單毛利 HK$100,000,銷售團隊跟進 Lead 後有 20% 成交率(即 5 個 Lead 簽 1 單)。5 個 Lead 成本為 HK20,000,換取HK20,000,換取HK100,000 毛利(ROI 5倍),這在企業級市場完全可接受。
- 嚴重虧損情況(一般 B2C / 中小企服務): 假設賣的是數千至一兩萬元的課程、裝修或零售服務,成交率 10%(10 個 Lead 才成 1 單),變相獲客成本(CAC)高達 HK$40,000,屬於嚴重失血。
3. 常見導致 CPL 飆升至 HK$4,000 的原因
如果貴公司並非高單價 B2B 業務,通常由以下問題引起:
- 「Lead」的定義過於嚴苛或追蹤斷層: 若設定了必須「完成預約+支付訂金」甚至「線下成交」才回傳給 Meta 算作 1 個 Lead,數字自然高;另外,若網站表單的 Meta Pixel / CAPI(Conversions API)漏追蹤,實際有 10 個查詢但系統只記錄到 1 個,帳面 CPL 便會放大 10 倍。
- 受眾過窄或過度重疊: 香港人口基數小,若在後台設定過多層興趣交集(Interest Stacking),受眾池低於幾萬人,會令競價成本(CPM)急升。
- 每日預算不足以完成「學習階段(Learning Phase)」: Meta 演算法需要每週約 50 次轉換事件來優化投放。若單個轉換成本已達數千元,而每日預算只有幾百元,系統永遠無法累積足夠數據,只會盲目燒錢。Meta廣告+SEM+SEO | Sold Out Digital
- 表單與落地頁(Landing Page)摩擦力過大: 要求客戶填寫過多公司資料、電話及預算欄位,或者廣告素材與落地頁承諾不符,導致點擊率正常但轉換率極低。
from collections import defaultdict
import subprocess
import sys
def run_git(args, repo_path):
cmd = ["git", "-C", repo_path] + args
result = subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True
)
return result.stdout.strip()
def analyze_branches(repo_path):
print("Discovering branches and analyzing code volume per branch...")
raw_output = run_git(["branch", "-a", "--no-color"], repo_path)
branches = set()
for line in raw_output.splitlines():
line = line.strip().replace("* ", "")
if not line or "->" in line:
continue
if line.startswith("remotes/"):
line = line[8:]
branches.add(line)
branch_stats = {}
for branch in sorted(branches):
try:
files_out = run_git(["ls-tree", "-r", "--name-only", branch], repo_path)
files = [f for f in files_out.splitlines() if f.strip()]
except subprocess.CalledProcessError:
continue
total_lines = 0
file_count = 0
for file_path in files:
show_cmd = ["git", "-C", repo_path, "show", f"{branch}:{file_path}"]
try:
raw_content = subprocess.run(
show_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=False,
check=True,
).stdout
# Skip binary files containing null bytes
if b"\x00" in raw_content:
continue
lines = raw_content.count(b"\n")
if raw_content and not raw_content.endswith(b"\n"):
lines += 1
total_lines += lines
file_count += 1
except subprocess.CalledProcessError:
continue
branch_stats[branch] = {"lines": total_lines, "files": file_count}
return branch_stats
def analyze_developers(repo_path):
print("Analyzing developer contribution history across all branches...")
cmd = [
"git",
"-C",
repo_path,
"log",
"--all",
"--no-merges",
"--numstat",
"--format=AUTHOR:%an",
]
try:
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)
except subprocess.CalledProcessError as e:
print(f"Error executing git log: {e.stderr.strip()}")
return {}
developer_stats = defaultdict(lambda: {"added": 0, "deleted": 0, "net": 0})
current_author = "Unknown"
for line in result.stdout.splitlines():
line = line.strip()
if not line:
continue
if line.startswith("AUTHOR:"):
current_author = line[7:]
else:
parts = line.split("\t")
if len(parts) >= 2:
added, deleted = parts[0], parts[1]
if added.isdigit() and deleted.isdigit():
developer_stats[current_author]["added"] += int(added)
developer_stats[current_author]["deleted"] += int(deleted)
for author, stats in developer_stats.items():
stats["net"] = stats["added"] - stats["deleted"]
return developer_stats
if __name__ == "__main__":
target_path = sys.argv[1] if len(sys.argv) > 1 else "."
# --- Part 1: Branch Analysis ---
branch_stats = analyze_branches(target_path)
if branch_stats:
print("\n" + "=" * 80)
print("PART 1: CODE VOLUME BY BRANCH (Current HEAD of each branch)")
print("=" * 80)
print(f"{'Branch Name':<45} | {'Files':<8} | {'Total Lines':<12}")
print("-" * 80)
grand_total_lines = sum(data["lines"] for data in branch_stats.values())
for branch, data in sorted(
branch_stats.items(), key=lambda x: x[1]["lines"], reverse=True
):
print(f"{branch:<45} | {data['files']:<8,} | {data['lines']:<12,}")
print("=" * 80)
print(f"Combined total lines across all branch HEADs: {grand_total_lines:,}")
print("=" * 80)
# --- Part 2: Developer History Analysis ---
dev_stats = analyze_developers(target_path)
if dev_stats:
print("\n" + "=" * 95)
print(
"PART 2: DEVELOPER CONTRIBUTIONS ACROSS ALL HISTORY (All Branches,"
" No Merges)"
)
print("=" * 95)
print(
f"{'Developer':<30} | {'Lines Added':<14} | {'Lines Deleted':<14} |"
f" {'Net Lines':<12}"
)
print("-" * 95)
sorted_devs = sorted(
dev_stats.items(), key=lambda x: x[1]["added"], reverse=True
)
for author, data in sorted_devs:
print(
f"{author:<30} | {data['added']:<14,} | {data['deleted']:<14,}"
f" | {data['net']:<12,}"
)
print("=" * 95)
https://shop.m5stack.com/products/m5stack-tab5-iot-development-kit-esp32-p4
https://docs.m5stack.com/en/core/Tab5
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
cmake -B build -DGGML_CUDA=ON
cmake --build build --config Release -j
./build/bin/llama-server \
-hf bartowski/Qwen_Qwen3-30B-A3B-GGUF:Q4_K_M \
--alias qwen3-30b-a3b \
--host 0.0.0.0 --port 8080 \
--jinja \
-ngl 99 \
-c 65536 \
--parallel 1 \
--flash-attn on \
-b 2048 -ub 512 \
--temp 0.6 --top-p 0.95 --top-k 20 --min-p 0 \
--presence-penalty 1.5
DFlash2
./build/bin/llama-server \
-hf bartowski/Qwen3.8-27B-GGUF:Q4_K_M\
-hfd incoai/Qwen3.8-27B-DFlash2-GGUF:Q4_K_M\
--alias qwen3.8-27b-dflash2\
--host 0.0.0.0\
--port 8080\
--jinja\
-ngl 99\
-c 262144\
--parallel 1\
--flash-attn on\
--spec-type draft-dflash\
--spec-draft-n-max 7\
-b 2048 -ub 512\
--temp 1.0\
--top-p 0.95\
--top-k 20\
--min-p 0\
--chat-template-kwargs '{"reasoning_effort":"medium"}'
Qwen 3.5 9B
./build/bin/llama-server \
-hf DavidAU/Qwen3.5-9B-The-Defiant-Fable-Uncensored-Heretic-NEO-IMATRIX-MAX-MTP-GGUF:Q4_K_M \
--alias qwen3.5-9b \
--host 0.0.0.0 \
--port 8080 \
--jinja \
-ngl 99 \
-c 262144 \
--parallel 1 \
--flash-attn on \
-b 2048 -ub 512 \
--temp 1.0 \
--top-p 0.95 \
--top-k 20 \
--min-p 0 \
--chat-template-kwargs '{"reasoning_effort":"medium"}'


git clone https://github.com/lvgl-micropython/lvgl_micropython.git
cd lvgl_micropython
docker run -it -v .:/micropython --name micropython ubuntu
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get install -y gcc g++ make automake python3 git gcc-arm-none-eabi libusb-1.0-0 python3-venv python3-click python3-yaml cmake vim
ln -s /usr/bin/python3 /usr/bin/python
cd /micropython
python3 make.py esp32 \
--flash-size=16 \
BOARD=ESP32_GENERIC_S3 \
DISPLAY=st7789
The ESP32-S3R2's PSRAM is quad SPI, which plain `ESP32_GENERIC_S3` supports, so leave out `BOARD_VARIANT`. A `BOARD_VARIANT=SPIRAM_OCT` build still boots, but without PSRAM, which is too little RAM for larger programs such as the SemiBlock sprite runtime.
If you got compile trouble, read https://share.gemini.google/mN8nWFLXggme
sudo apt install gocryptfs
mkdir ~/encrypted-vault ~/secret-folder
gocryptfs -init ~/encrypted-vault
gocryptfs ~/encrypted-vault ~/secret-folder
gocryptfs /home/peter/encrypted-vault /home/peter/workspace
fusermount -u /home/peter/workspace

git clone https://github.com/lvgl-micropython/lvgl_micropython.git
cd lvgl_micropython
# git checkout 15a414bc03486017235234882ce7415532c6325e
docker run -it -v .:/micropython --name micropython ubuntu
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get install -y gcc g++ make automake python3 git gcc-arm-none-eabi libusb-1.0-0 python3-venv python3-click python3-yaml cmake vim
ln -s /usr/bin/python3 /usr/bin/python
cd /micropython
python3 make.py esp32 clean \
--flash-size=8 \
BOARD=ESP32_GENERIC_S3 \
DISPLAY=st7789 \
BOARD_VARIANT=SPIRAM_OCT

git clone https://github.com/lvgl-micropython/lvgl_micropython.git
cd lvgl_micropython
# git checkout 15a414bc03486017235234882ce7415532c6325e
docker run -it -v .:/micropython --name micropython ubuntu
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get install -y gcc g++ make automake python3 git gcc-arm-none-eabi libusb-1.0-0 python3-venv python3-click python3-yaml cmake vim
ln -s /usr/bin/python3 /usr/bin/python
cd /micropython
python3 make.py esp32 clean \
--flash-size=16 \
BOARD=ESP32_GENERIC_S3 \
BOARD_VARIANT=SPIRAM_OCT \
DISPLAY=rgb_display \
INDEV=gt911

import os
f=open("servo.py", "w")
f.write("""
from machine import Pin, PWM
class Servo:
def __init__(self, pin: int or Pin or PWM, minVal=2500, maxVal=7500):
if isinstance(pin, int):
pin = Pin(pin, Pin.OUT)
if isinstance(pin, Pin):
self.__pwm = PWM(pin)
if isinstance(pin, PWM):
self.__pwm = pin
self.__pwm.freq(50)
self.minVal = minVal
self.maxVal = maxVal
def deinit(self):
self.__pwm.deinit()
def goto(self, value: int):
if value < 0:
value = 0
if value > 1024:
value = 1024
delta = self.maxVal-self.minVal
target = int(self.minVal + ((value / 1024) * delta))
self.__pwm.duty_u16(target)
def middle(self):
self.goto(512)
def free(self):
self.__pwm.duty_u16(0)""");
f.close()
def servo_Angle(var1, angle):
if angle < 0:
angle = 0
if angle > 180:
angle = 180
var1.goto(round(angle * 1024 / 180))
from machine import Pin, SoftI2C, ADC, PWM, UART
from time import sleep,sleep_ms, sleep_us
#import tm1637
import network
import math
from servo import Servo
servo=Servo(32)
for i in range(10):
servo_Angle(servo, 0)
sleep(1)
servo_Angle(servo, 180)
sleep(1)

git clone https://github.com/lvgl-micropython/lvgl_micropython.git
cd lvgl_micropython
# git checkout 15a414bc03486017235234882ce7415532c6325e
docker run -it -v .:/micropython --name micropython ubuntu
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get install -y gcc g++ make automake python3 git gcc-arm-none-eabi libusb-1.0-0 python3-venv python3-click python3-yaml cmake vim
ln -s /usr/bin/python3 /usr/bin/python
cd /micropython
python3 make.py esp32 clean \
--flash-size=16 \
BOARD=ESP32_GENERIC_S3 \
BOARD_VARIANT=SPIRAM_OCT \
DISPLAY=axs15231b \
INDEV=axs15231
exit docker
esptool.py --chip esp32s3 -b 460800 \
--before default_reset \
--after hard_reset write_flash \
--flash_mode dio \
--flash_size 16MB \
--flash_freq 80m \
--erase-all 0x0 \
build/lvgl_micropy_ESP32_GENERIC_S3-SPIRAM_OCT-16.bin
from pathlib import Path
# Replace '.' with your directory path
directory = Path('.')
# List all files and folders
for item in directory.iterdir():
# if dir
if item.is_dir():
# check if .git exists
git_dir = item / '.git'
if git_dir.exists() and git_dir.is_dir():
print(f"{item.name} is a git repository")
# run git pull in there
import subprocess
subprocess.run(['git', '-C', str(item), 'pull'])
mvn install all repo
from pathlib import Path
# Replace '.' with your directory path
directory = Path('.')
# List all files and folders
for item in directory.iterdir():
# if dir
if item.is_dir():
# check if .git exists
git_dir = item / 'pom.xml'
if git_dir.exists() and git_dir.is_file():
print(f"{item.name} has a pom.xml file")
# run mvn install in there
import subprocess
subprocess.run(['mvn', '-f', str(item / 'pom.xml'), '-DskipTests', 'clean', 'install'])
Install-Module -Name PnP.PowerShell -Scope CurrentUser
Connect-PnPOnline -Url "https://quantr.sharepoint.com/gitlab" -Interactive
Remove-PnPList -Identity "Commit" -Recycle -Force -LargeList
Get-PnPLargeListOperationStatus -Identity "60f2e783-aba5-46e8-9621-070ede81482b" -OperationId "2a93bded-42f7-4879-977a-20d631216bc4"

!!! This is not the perfect guide, during the compilation it got errors, ask gemini. https://share.gemini.google/bFyDzMtkYZVk
Step 1: Install Tools
Step 1: Install Vitis (it has Vivado)
Step 2: Install petalinux https://www.amd.com/en/products/software/adaptive-socs-and-fpgas/embedded-software/petalinux-sdk.html


chmod+x petalinux-v20XX.X-final-installer.run
./petalinux-v20XX.X-final-installer.run

Open Vivado and create project

Project name : petalinux_boot_from_flash_vivado

Choose: XC7Z020CLG400-2








petalinux-package --boot --fsbl images/linux/zynq_fsbl.elf --u-boot images/linux/u-boot.elf --force

Step 2: Build the image using petalinux
source /opt/petalinux/settings.sh
petalinux-create --type project --template zynq --name petalinux_boot_from_flash
cd petalinux_boot_from_flash
find .. -name "*.xsa"
cp ../petalinux_boot_from_flash_vivado/design_1_wrapper.xsa .
petalinux-config --get-hw-description=.

(Just save and exit)
petalinux-package --boot --fsbl images/linux/zynq_fsbl.elf --u-boot images/linux/u-boot.elf --force
now you got BOOT.BIN

~/xilinx/2026.1/Vitis/bin/program_flash -f images/linux/BOOT.BIN \
-offset 0x0 \
-fsbl images/linux/zynq_fsbl.elf \
-flash_type qspi_single \
-verify \
-url tcp:localhost:3121
Add macros and the rest of a professional RISC-V assembler
The RISC-V front end already encodes instructions and a handful of data directives. It does not assemble the files you actually write. Those are GNU as: .macro / .endm, \name, numbered local labels, .option, .pushsection. r-test/fp.S is the concrete target.
This document tells you how to add that, in order. Do not expand macros inside the ANTLR grammar. The encoder already advances address in parser actions; a .macro body that is a lines rule will emit bytes at definition time, or fight the address, or both.
The one-line version
Parse once to collect directives, expand macros / conditionals / includes into plain source, then parse again to encode. New language features that invent text belong in a preprocessor. New language features that invent bytes (.align, .globl, .option) belong in the grammar and RISCVEncoder.
source .S → preprocessor → ANTLR parse + encode → bin / ELF
Dialect for everything new is GNU as. Keep the NASM %define / %include that already work so testbench/define.s does not break. Do not add a second full NASM %macro unless a later day needs it.
What already exists
Do not reinvent these. They are incomplete, not absent.
| Piece | Where | What it actually does |
|---|---|---|
preProcess | AssemblerLib.java | Parse, then string-replace %include and %define |
%define | lexer DEFINE, DefineListener | Flat name → value. No parameters. Redefine calls System.exit |
%include | lexer INCLUDE, IncludeListener | Inlines a file. Search path is hardcoded testbench/ |
%ifdef / %elif / %else / %endif | grammar ifdef | Parsed. Never evaluated |
%times / times | lexer TIMES, preProcess | Empty if (content.contains("%times")) |
.byte .half .word .dword .string | grammar dot* | Encoded. .string takes IDENTIFIER, not a quoted string |
. IDENTIFIER | grammar section | Stores a name for the listing. No real section |
| labels | grammar label | IDENTIFIER COLON only. No 1: / 1b / 1f |
| comments | lexer LINE_COMMENT | ; only. GNU as uses # |
| listing | -l, RISCVEncoder.listing | Records the line the parser saw, not an expansion |
| ELF | Assembler.java -f elf | Already writes via executablelibrary |
DefineListener.map and DefineListener.lines are static. A second file in the same JVM inherits the first file's defines. Make them instance state when you touch that class.
The grammar rule named macro is not a macro. It is a bucket for define, ifdef, include, and data:
macro : define | ifdef | include | dotbyte | dothalf | dotword | dotdword | dotstring ;
Leave that name alone until the preprocessor owns define/ifdef/include. Then the rule can shrink to data directives.
Why the preprocessor, not the grammar
RISCVAssemblerParser members hold address and every instruction does address+=encoder.encodeType...(...). A .macro ADD rd, rs whose body is parsed as lines will encode add while you are still defining ADD. The invocation then has nothing to expand, or encodes a second time.
preProcess already does the right shape: walk the text, produce new text, then the real assemble() encodes. Grow that into a package hk.quantr.assembler.riscv.preprocess rather than piling more static maps into AssemblerLib.
Assembler.main must call the preprocessor before the encode parse. Today preProcess is used from tests (TestMacro, TestMissingMacro) and is easy to skip from the CLI path. Grep preProcess( and make every assemble entry go through it.
Files you will touch
src/main/java/hk/quantr/assembler/antlr/RISCVAssemblerLexer.g4
src/main/java/hk/quantr/assembler/antlr/RISCVAssemblerParser.g4
src/main/java/hk/quantr/assembler/AssemblerLib.java
src/main/java/hk/quantr/assembler/Assembler.java
src/main/java/hk/quantr/assembler/riscv/listener/DefineListener.java
src/main/java/hk/quantr/assembler/riscv/listener/IncludeListener.java
src/main/java/hk/quantr/assembler/riscv/RISCVEncoder.java
src/main/java/hk/quantr/assembler/riscv/preprocess/ (new)
src/test/java/hk/quantr/assembler/riscv/TestGasMacro.java (new)
After any .g4 change: mvn -DskipTests compile so ANTLR regenerates into target/generated-sources/antlr4.
Phase 1: real macros
This is the feature the request named. Do it first and stop to test.
Tokens
In RISCVAssemblerLexer.g4, next to DEFINE / INCLUDE:
DOTMACRO : '.macro';
DOTENDM : '.endm';
DOTEXITM : '.exitm';
Do not add a parser rule that treats the body as lines. The preprocessor reads these as lines of text.
Data
A definition is a name, a list of formal parameters, and the raw body (lines between .macro and .endm, not encoded). An invocation is a name used as an opcode with comma-separated arguments.
GNU as substitution:
| In the body | Becomes |
|---|---|
\formal | the matching argument |
\\@ | a counter that increments every invocation (unique local labels) |
\() | concatenator, so \a\()b is arg a then the letter b |
GAS also allows .macro NAME arg1=default. Defaults can wait until the required-argument form works.
Algorithm
- Walk the source line by line.
#and;comments strip for this walk, but keep the original line text for error reporting. - On
.macro NAME [formals...], slurp until.endm. Nested.macroinside a body is stored, not executed. Missing.endmis an error that names the opening line. - On a line whose first identifier is a defined macro, split arguments on commas that are not inside
(...)or quotes, bind formals, substitute, splice the expansion in place of the invocation, and re-scan from there so macros can call macros. - Recursion depth: cap at something like 100. The error must name the invocation site and the definition site.
- Unknown arity: error, do not encode a truncated body.
Invocations look like instructions (SECTION name, T fld, LDD fs0, d_one). The encode parse must not see those names. After expansion, SECTION is gone and the body (la, call, ...) remains.
What not to put in the grammar
Do not add NAME args as a generic instruction alternative. That would steal real opcodes (add, ld) if someone names a macro after one. Expansion happens first; the parser only ever sees real instructions.
First golden test
r-test/fp.S macros, smallest useful subset:
.macro E
la a3, 7b
call test_end
.endm
.macro LDD freg, sym
la a0, \sym
c.fld \freg, 0(a0)
.endm
A file that does LDD fs0, scratch then E must expand to la / c.fld / la / call. Compare bytes to riscv64-elf-as (phase 1 can ignore .option and .pushsection by not using those lines yet).
TestGasMacro outline:
String src = Files.readString(Path.of("src/test/resources/macro_ldd.s"));
String expanded = Preprocessor.expand(src, "rv64");
byte[] ours = assembleRv64(expanded);
byte[] gas = gasAssemble(src); // riscv64-elf-as -march=rv64imafdc
assertArrayEquals(gas, ours);
gasAssemble writes a temp .s, runs riscv64-elf-as -o t.o, then riscv64-elf-objcopy -O binary t.o t.bin (or read .text from the ELF). Same idea as FullTest versus gas.
Phase 1 is done when that test passes and testbench/define.s still works.
Phase 2: conditionals and repeats
%ifdef is already in the grammar and does nothing. Evaluating it in the parser would still encode the false branch, because both lines alternatives are walked. Evaluate in the preprocessor, then delete the ifdef parser rule or leave it as a no-op that never fires on expanded text.
Add GAS forms. These are what fp.S does not use yet but every real tree has:
.if expr
.ifdef name
.ifndef name
.else
.endif
.rept N
.endr
.if uses the same expression evaluator as immediates (CalculatorLibrary.cal). A name in .ifdef is defined if it is in the %define / .equ table or is a .macro.
.rept N splices the body N times, then re-scans. That is also the correct implementation of times / %times (the empty branch in preProcess).
.irp reg, t0, t1, t2 can wait until .rept works. It is the same loop with a formal rebound each iteration.
False branches must not define macros and must not invoke them. Skip lines until the matching .else / .endif, tracking nest depth. A dangling .endif is an error.
Phase 3: symbols and local labels
Without this, expanded fp.S still will not assemble.
.equ / .set
Same table as %define. .set may redefine; %define today forbids it and exits. Pick one policy and document it: .set overwrites, %define of an existing name is an error (keep today's behaviour).
Lexer: DOTEQU : '.equ'; DOTSET : '.set';
The encode parse needs these names in immediates. Either the preprocessor substitutes them (like %define already does with replaceAll("\\b"+name+"\\b")) or CalculatorLibrary consults the table. Substitution is simpler and matches the current %define path.
Numbered local labels
GNU as: 1: through 19: (you only need 0–9 to start), referenced as 1b (nearest backward) and 1f (nearest forward). fp.S uses 7: / 7b and 9: / 9b.
This cannot be only a preprocessor rewrite of the current file, because 1f depends on a label that appears later. Do it in the encoder:
- Lexer: allow
labelto be[0-9]+ COLON, and immediates / jump targets to be[0-9]+ [bf]. - First pass, or a collected list: each
N:at address A is pushed on a per-digit list. - When encoding
jal/beq/lathat uses7b, take the last7:whose address is<=current address;7ftakes the next one after.
If you stay strictly one-pass, 7f is unknown when you see it. The encoder already has labels on the parser (ArrayList<Label>). Either two-pass (walk once for labels, once to encode) or record a fixup and patch the immediate when the forward label is defined. Two-pass is less clever and matches how gas works. The current address+= one-pass is why forward regular labels are already shaky; this is the moment to make a label pass explicit if you have to fight it.
# comments
LINE_COMMENT : [;#] ~[\r\n]* ;
GAS also treats /* */ as comments. Not required for fp.S. # is.
Phase 4: sections and options
Needed for a real object, and for fp.S after macros expand.
Sections
Replace the catch-all
section : DOT sectionName=IDENTIFIER IDENTIFIER? ;
with explicit directives so .macro / .equ / .option are not eaten as section names (today . plus an identifier is a section).
.text
.data
.rodata
.bss
.align N
.globl name
.asciz "string"
.ascii "string"
.pushsection name
.popsection
.byte already exists. Add .asciz (NUL-terminated) distinct from .string if .string stays identifier-only; or teach .string to take DOUBLE_QUOTATION ... DOUBLE_QUOTATION. fp.S uses .asciz.
RISCVEncoder must keep a current section and a stack for .pushsection / .popsection. Bytes go into that section's buffer, not one flat out. ELF output (-f elf) already exists; point it at those buffers instead of a single blob. bin output can concatenate .text then .data the way a raw image expects, or refuse and require -f elf. Pick one and test it.
.align N on RISC-V gas is power-of-two (.align 3 means 8 bytes). Pad with zeros or nop/c.nop in .text. Wrong interpretation here will fail every gas comparison.
.option
.option rvc
.option norvc
.option push
.option pop
LDD in fp.S wraps c.fld in .option push / rvc / pop. Without this, either you always accept compressed (current behaviour) or you reject c.fld when someone writes .option norvc. Store a stack of flags on the encoder. encodeType* for compressed opcodes checks rvcEnabled.
Phase 5: professional tooling
Do this after the language works. None of it changes what bytes mean.
- Include path.
preProcessIncludeFileopens"testbench/" + to. Resolve against the including file's directory, then each-IfromAssembler.main. Add the CLI option next to-a/-o. - Listing of expansions.
-lalready writeslisting. After preprocess, each encoded line should carry the invocation that produced it, so a failure in an expandedT fldnamesTand the.macro Tline. - Errors. Invocation site plus definition site, for macros, includes, and missing
.endm. Stop callingSystem.exitfromDefineListener/preProcess; throw or record onMessageHandlerand letmainset the exit code. Tests cannot survive an exit. - Instance state.
DefineListener.mapand friends must not be static.
How to test, every phase
Always compare to GNU as. That is the project's existing contract (FullTest / gas vs quantr).
# gas
riscv64-elf-as -march=rv64imafdc -o gas.o t.s
riscv64-elf-objcopy -O binary --only-section=.text gas.o gas.bin
# ours, after you wire preProcess into main
java -jar target/assembler-*-jar-with-dependencies.jar -a rv64 -f bin -o ours.bin t.s
cmp gas.bin ours.bin
Keep a table of fixtures under src/test/resources/riscv/:
| File | Phase | What it proves |
|---|---|---|
define_still_works.s | 1 | %define + addi unchanged |
macro_ldd.s | 1 | .macro args, \formal |
macro_nested.s | 1 | macro calls macro |
macro_count.s | 1 | \\@ unique labels |
ifdef_false.s | 2 | false branch emits nothing |
rept_3.s | 2 | body appears three times |
local_label.s | 3 | 1: ... j 1b |
option_rvc.s | 4 | c.fld only with .option rvc |
pushsection.s | 4 | string lands in .rodata, code in .text |
testbench/define.s and testbench/macro.s stay green.
Acceptance
Phase 1–4 together: this jar assembles r-test/fp.S (or a copy with the # comments and .equ values it already has) and the .text bytes match riscv64-elf-as -march=rv64imafdc. That file uses every phase: macros with arguments, # comments, 7: / 7b, .option push/rvc/pop, .pushsection, .asciz.
Until that works, do not start ia32 macros, a C preprocessor, or a relocating linker. Those are different jobs.
Out of scope
- Rewriting the encoder into a full relocating linker (relocs,
-shared) - ia32 / NASM
%macro/%endmacro - Running
cppfor#include/#ifdef/#define(GAS#is a comment, not cpp, unless you pass-x assembler-with-cpp) - Changing instruction encodings or the disassembler
Add this to your .bashrc
export GDK_SCALE=2 GDK_DPI_SCALE=0.5
export _JAVA_OPTIONS="-Dsun.java2d.uiScale=2.5"
!!! This is not the perfect guide, during the compilation it got errors, ask gemini. https://share.gemini.google/bFyDzMtkYZVk
I am using this board

Step 1: Install Vitis (it has Vivado)
Step 2: Install petalinux https://www.amd.com/en/products/software/adaptive-socs-and-fpgas/embedded-software/petalinux-sdk.html


chmod +x petalinux-v20XX.X-final-installer.run
./petalinux-v20XX.X-final-installer.run

Step 3: Add the board files to Vivado
git clone https://github.com/karolzmijewski/z7-nano-7020.git
mkdir /home/peter/xilinx/2026.1/data/boards/board_files
cp -r z7-nano-7020/board_files/z7-nano-7020 /home/peter/xilinx/2026.1/data/boards/board_files/
Step 4: Build a minimal hardware design in Vivado
vivado→ Create New Project → RTL Project, no sources- Choose Boards tab → select Z7-Nano-7020 (now visible thanks to step 2)
- Create Block Design → add ZYNQ7 Processing System → click Run Block Automation (it will apply the board preset automatically: DDR3, UART, SD, ETH, USB)
- Validate the design (F6), no errors
- Create HDL wrapper → right click wrapper → Generate Bitstream
- File → Export → Export Hardware → check Include bitstream → produces a
.xsafile (e.g.z7nano_wrapper.xsa)


Step 5: Create the PetaLinux project
source home/peter/xilinx/2026.1/Model_Composer/settings64.sh
source /opt/petalinux/settings.sh
petalinux-create -t project --template zynq -n z7nano-linux
cd z7nano-linux
petalinux-config --get-hw-description=/path/to/xsa/dir
In the config menu that opens:
- Subsystem AUTO Hardware Settings → confirm/adjust serial console (should be
ps7_uart_1or_0matching your design), Ethernet, SD - Image Packaging Configuration → Root filesystem type → set to
EXT4 (SD/eMMC/USB)(so rootfs lives directly on the SD card, not inside an initramfs) - Boot Image Settings → FSBL and u-boot should default correctly for Zynq-7000
petalinux-config -c kernel
petalinux-config -c rootfs
Step 6: Build
petalinux-build
This produces (under images/linux/): zynq_fsbl.elf, u-boot.elf, image.ub (kernel+devicetree+ramdisk fitImage), system.dtb, rootfs.tar.gz.
Then package the boot binary:
petalinux-package --boot --fsbl images/linux/zynq_fsbl.elf \
--fpga images/linux/system.bit \
--u-boot --force
petalinux-package --boot --fsbl images/linux/zynq_fsbl.elf --u-boot images/linux/u-boot.elf --force
This creates images/linux/BOOT.BIN.
Step 7: Prepare the microSD card
Partition it with two partitions (use fdisk/gparted):
| Partition | Size | Type | Contents |
|---|---|---|---|
| 1 | ~500MB | FAT32, boot flag | BOOT.BIN, image.ub, boot.scr (if generated) |
| 2 | remainder | ext4 | extracted rootfs |
sudo mkfs.vfat -F 32 -n BOOT /dev/sdX1
sudo mkfs.ext4 -L rootfs /dev/sdX2
sudo mount /dev/sdX1 /mnt/boot
sudo cp images/linux/BOOT.BIN images/linux/image.ub /mnt/boot/
sudo umount /mnt/boot
sudo mount /dev/sdX2 /mnt/root
sudo tar xzf images/linux/rootfs.tar.gz -C /mnt/root
sudo umount /mnt/root
Step 8: Set the boot mode jumper
On the Z7-Nano, boot mode is set by jumper J1 ("MODE" pins) — set it to the SD boot position (JTAG/QSPI/SD options are silkscreened near J1; check the reference manual's Boot Config diagram or the schematic if the silkscreen is unclear — standard Zynq-7000 SD boot mode pins are MIO[6:2] = 1 0 1 0 1).
Step 9: Boot it
- Insert the microSD card
- Connect the USB-UART port to your PC (
/dev/ttyUSB0, appears as CH340 device) - Open a serial terminal:
screen /dev/ttyUSB0 115200(orminicom -D /dev/ttyUSB0 -b 115200) - Power the board via USB
- You should see FSBL → U-Boot → kernel boot messages, ending in a login prompt (default PetaLinux root/root or root/petalinux depending on version config)
Troubleshooting notes
- DDR/FSBL hangs at boot: usually a MIG/PS7 DDR config mismatch — re-run Block Automation in Vivado rather than hand-editing PS7 DDR settings, since the board preset already has correct MT41K256M16 timings.
- No Ethernet: the RTL8211F PHY sometimes needs a reset GPIO toggle in the device tree (
phy-reset-gpio) — checkkarolzmijewski/z7-nano-7020examples for the exact PHY reset pin if this happens. - U-Boot doesn't find
image.ub: confirm the FAT32 partition has the boot flag set and file names match whatpetalinux-packageproduced. - Faster iteration: once this works, you can skip re-running Vivado each time and just re-run
petalinux-build+petalinux-packagefor software-only changes (keep the same.xsaunless you change PL hardware).
Alternative: PYNQ instead of plain PetaLinux
Since the board explicitly advertises a microSD slot "for PYNQ," if your goal is Python/Jupyter-based FPGA development rather than a bare Linux console, you can follow the same Vivado XSA export above but instead build via the PYNQ SD card image build flow, which layers Jupyter + the PYNQ Python overlay framework on top of a PetaLinux-built image. It's more work (bigger BSP customization, xilinx-pynq recipe) but gives you a full Jupyter notebook environment on the board out of the box.
Let me know which route you want (plain embedded Linux console vs. PYNQ/Jupyter), and whether you'd like help writing the actual Vivado TCL block-design script or PetaLinux device-tree overlay for specific peripherals (Ethernet, HDMI, GPIO) — I can generate those files for you.
1. Install Git LFS
- macOS (via Homebrew):
brew install git-lfs - Ubuntu / Debian:
sudo apt install git-lfs - Windows (via Chocolatey or standalone installer):PowerShell
choco install git-lfs
2. Initialize Git LFS
Run this command once on your computer to hook it into Git:
Bash
git lfs install
Step 4: Track and Push Large Files in a Repository
To use LFS inside a repository, point Git to the specific file types you want to manage.
- Clone your repository and navigate into it:
git clone [email protected]:group/my-project.git cd my-project - Tell Git LFS which file extensions or patterns to track (e.g.,
.psd,.iso,.zip,.mp4):git lfs track "*.iso"This command automatically creates or updates a.gitattributesfile. - Important: Ensure the
.gitattributesfile is committed to your repository, otherwise collaborators will run into errors cloning LFS files:git add .gitattributesgit commit -m "Configure Git LFS tracking" - Add, commit, and push your large files as you normally would with Git:
cp ~/Downloads/large-file.iso ./git add large-file.isogit commit -m "Add large file via Git LFS"git push origin main
You will see output indicating that Git LFS is uploading the binary blobs separately from the standard Git commit metadata.
"http.proxy": "http://3.1.240.71:8888",
"http.proxySupport": "override",
"http.noProxy": [
"localhost",
"127.0.0.1",
"192.168.1.88",
"192.168.1.88:1234",
"61.244.87.154",
"github.com",
"gitlab.quantr.hk",
"gitlab.hkprog.org"
],
"chat.sessionSync.enabled": true,
"cursor.general.disableHttp2": true
Tested in mac m5
Step 1: Qemu. First test the default qemu, then modify in step 3
git clone [email protected]:qemu/qemu.git
export PATH="$(brew --prefix bison)/bin:$PATH"
export PKG_CONFIG_PATH="$(brew --prefix glib)/lib/pkgconfig:$PKG_CONFIG_PATH"
# ./configure --extra-cflags="-I/opt/homebrew/opt/libiconv/include -I/usr/local/include" --extra-ldflags="-L/opt/homebrew/opt/libiconv/lib -L/usr/local/lib" --target-list=riscv64-softmmu --enable-plugins
./configure --extra-cflags="-I$(brew --prefix libiconv)/include -I/usr/local/include" --extra-ldflags="-L$(brew --prefix libiconv)/lib -L/usr/local/lib" --target-list=riscv64-softmmu --enable-plugins
make -j
sudo make install
Step 2: xv6-riscv
brew install riscv64-elf-gcc riscv64-elf-binutils riscv64-elf-gdb
git clone [email protected]:mit-pdos/xv6-riscv.git
make -j
make qemu
If you meet this, comment the variable out


Step 3: modify the qemu
follow this https://www.quantr.foundation/project/?project=QEMU%20Log%20Panel
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/github
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/github
cat ~/.ssh/github.pub
https://github.com/settings/keys
Add key permanently
vi ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github
AddKeysToAgent yes















Step 1:
Check your Tools menu and make sure your settings look exactly like this:
- Board: Generic STM32F4 series
- Board Part Number: BlackPill F411CE (Do not pick the generic F411 options)
- U(S)ART support: Enabled (generic 'Serial')
- USB support: CDC (generic 'Serial' supersede U(S)ART)

//PA10 (RX) and PA9 (TX)
void setup() {
// Initialize USB CDC
Serial.begin(9600);
// Initialize Hardware UART
Serial1.begin(9600);
}
void loop() {
// Read from USB and send to UART
if (Serial.available()) {
Serial1.write(Serial.read());
}
// Read from UART and send to USB
if (Serial1.available()) {
Serial.write(Serial1.read());
}
}



#define F_CPU 16000000UL // 1. Define CPU Frequency (16MHz is common for 328PB)
#include <avr/io.h>
#include <util/delay.h> // 2. Include the delay library
int main(void) {
// 3. Set PD7 as an output
// DDRD is the Data Direction Register for Port D
DDRD |= (1 << DDD7);
while (1) {
// 4. Toggle PD7 using the XOR operator
PORTD ^= (1 << PORTD7);
// 5. Wait for 500 milliseconds
_delay_ms(500);
}
}
To burn the hex to avr in mac using command line, use this Makefile
export PATH="/Applications/microchip/mplabx/v6.30/mplab_platform/mplab_ipe/:$PATH"
MODEL=atmega328p
MODEL_AVRDUDE=m328pb
# MODEL_ISP=usbasp-clone
MODEL_ISP=avrisp2 # mkII
# For Microchip IPECMD, the device name usually needs to match the exact chip
MODEL_IPE=ATmega328PB
# IPECMD Tool Configuration
# -TPPK5 tells IPE to use the PICkit 5
TOOL_IPE=-TPPK5
all: SSD1306.o TWI.o main.hex
SSD1306.o: ssd1306/SSD1306.c
avr-gcc -mmcu=$(MODEL) -Wall -Os -c $? -o $@
TWI.o: ssd1306/TWI.c
avr-gcc -mmcu=$(MODEL) -Wall -Os -c $? -o $@
main.hex: main.c
avr-gcc -mmcu=$(MODEL) -Wall -Os -o main.elf SSD1306.o TWI.o main.c
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size --format=avr --mcu=$(MODEL) main.elf
upload:
avrdude -c $(MODEL_ISP) -p $(MODEL_AVRDUDE) -U flash:w:main.hex
upload_pickit5:
ipecmd.sh $(TOOL_IPE) -P$(MODEL_IPE) -F"$(CURDIR)/main.hex" -M
readfuse:
avrdude -c $(MODEL_ISP) -p $(MODEL_AVRDUDE) -U hfuse:r:-:h -U lfuse:r:-:h
writefuse:
avrdude -c $(MODEL_ISP) -p $(MODEL_AVRDUDE) -U lfuse:w:0x62:m -U hfuse:w:0xD9:m -U efuse:w:0xFF:m -U lock:w:0xFF:m
clean:
-rm *.o
-rm main.hex
-rm main.elf
I bought 10 ATF22V10C from here and T48 programmer here, got 1 broken. First, here is the simple and gate program, save it to and.pld
GAL22V10
AND_Gate
Clock A B NC NC NC NC NC NC NC NC GND
NC NC NC NC NC NC NC NC NC NC Y VCC
Y = A * B ; AND gate: Y is high only when both A and B are high
DESCRIPTION
Simple 2-input AND gate example using a GAL22V10.
Inputs: A (pin 2), B (pin 3)
Output: Y (pin 23) - combinatorial output
Build galasm and minipro. Then compile and burn it by:
galasm and.pld
minipro -p ATF22V10C -w and.jed

To read back the jed from chip, you can
minipro -p ATF22V10C -r output.jed
You can just compare the output.jed to your original and.jed, because and.jed is compiled by galasm and shortformed. I have a python to expand the jed, so the addresses in both jed files will be aligned.
See the address on left hand side then you see it is shortformed

import re
import sys
def expand_jedec_32bits(input_text):
# 1. Determine default value from *F flag
default_val = '0' if '*F0' in input_text else '1'
qf_match = re.search(r'\*QF(\d+)', input_text)
if not qf_match:
raise ValueError("Could not find fuse count (*QF) field in JEDEC file.")
total_fuses = int(qf_match.group(1))
# 2. Initialize the entire fuse array with the default value
fuse_array = [default_val] * total_fuses
# 3. Parse and fill explicit allocation fields (*LXXXX)
l_fields = re.findall(r'\*L(\d+)\s+([01\s]+)', input_text)
for start_index_str, bit_string in l_fields:
start_index = int(start_index_str)
bits = bit_string.replace(" ", "").replace("\n", "").replace("\r", "")
for i, bit in enumerate(bits):
if start_index + i < total_fuses:
fuse_array[start_index + i] = bit
# 4. Calculate Fuse Checksum (Sum of all 8-bit fuse bytes)
fuse_checksum = 0
for i in range(0, total_fuses, 8):
byte_bits = "".join(fuse_array[i:i+8])
if len(byte_bits) < 8:
byte_bits = byte_bits.ljust(8, '0')
# Standard JEDEC checksum mirrors the bit order of each byte
byte_val = int(byte_bits[::-1], 2)
fuse_checksum = (fuse_checksum + byte_val) & 0xFFFF
# 5. Construct the Longform Output Body (Row width = 32 bits)
output_lines = []
# Grab everything before the QF tag to keep original headers/comments
header_end_idx = input_text.find('*QF')
output_lines.append(input_text[:header_end_idx].strip())
output_lines.append(f"*QF{total_fuses}*")
row_size = 32 # 32 bits per row configuration
for addr in range(0, total_fuses, row_size):
chunk = fuse_array[addr:addr+row_size]
chunk_str = "".join(chunk)
# Append row format: *L<address> <bits>*
# Keeping a space here as it is standard formatting for readability
output_lines.append(f"*L{addr:05d} {chunk_str}")
# Add the generated fuse checksum
output_lines.append(f"*C{fuse_checksum:04X}*")
# End of text transmission block
output_body = "\n".join(output_lines) + "\n\x03"
# 6. Calculate File Checksum (ASCII sum from STX to ETX)
file_checksum = sum(ord(c) for c in output_body) + 0x02 # Include STX (0x02)
file_checksum &= 0xFFFF
final_jedec = f"\x02\n{output_body}{file_checksum:04x}"
return final_jedec
# --- Execution ---
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: expand_jedec.py <input.jed> [output.jed]")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2] if len(sys.argv) > 2 else input_file.rsplit(".", 1)[0] + "_expanded.jed"
try:
with open(input_file, "r", encoding="ascii") as f:
shortform_data = f.read()
longform_jedec = expand_jedec_32bits(shortform_data)
with open(output_file, "w", encoding="ascii") as f:
f.write(longform_jedec)
print(f"Success! Generated '{output_file}' with 32-bit width arrays.")
except FileNotFoundError:
print(f"Error: Input file '{input_file}' not found.")
sys.exit(1)
except Exception as e:
print(f"Error expanding JEDEC map: {e}")
sys.exit(1)
run "python expand_jedec.py and.jed", then you got and_expand.jed, then you can diff and_expand.jed and and.jed to provide they are the same, so the program you burn to ATF22V10C are same as what you want (and.jed)




Disable cursor capturing my project's information to prevent leaking. Open settings.json and set
"telemetry.enableTelemetry": false,
"telemetry.telemetryLevel": "off"

If you build pulseview in mac, you got "fatal error: 'glib.h' file not found". Do these
cd /opt/homebrew/lib/pkgconfig # or /usr/local/lib/pkgconfig if on Intel Mac
ln -s glibmm-2.68.pc glibmm-2.4.pc
cd /Users/peter/workspace/pulseview
rm -rf CMakeCache.txt CMakeFiles/
make clean # or just rm -rf the build artefacts if needed
export PKG_CONFIG_PATH="/usr/local/opt/glib/lib/pkgconfig:/usr/local/opt/qt@6/lib/pkgconfig:$PKG_CONFIG_PATH"
cmake .
make -j



How to Run
mpremote exec "import flashRead_w25; flashRead_w25.dump_flash(0, 1280)"
mpremote exec "import flashWrite_w25; flashWrite_w25.write('w 2 0x23 3 0x45')"
Write
import machine
import time
import ssd1306
from machine import SPI, Pin
# W25Q128 SPI Flash Configuration
# W25Q128 has 16MB (16777216 bytes) = 128 Mbit
# Page size: 256 bytes
# Sector size: 4KB
# Block size: 64KB
# W25Q128 Commands
CMD_WRITE_ENABLE = 0x06
CMD_WRITE_DISABLE = 0x04
CMD_READ_STATUS = 0x05
CMD_READ_STATUS2 = 0x35
CMD_READ_STATUS3 = 0x15
CMD_WRITE_STATUS = 0x01
CMD_READ_DATA = 0x03
CMD_PAGE_PROGRAM = 0x02
CMD_SECTOR_ERASE = 0x20
CMD_BLOCK_ERASE_32K = 0x52
CMD_BLOCK_ERASE_64K = 0xD8
CMD_CHIP_ERASE = 0xC7
CMD_READ_ID = 0x9F
CMD_POWER_DOWN = 0xB9
CMD_RELEASE_POWER_DOWN = 0xAB
CMD_RESET_ENABLE = 0x66
CMD_RESET_MEMORY = 0x99
# SPI Configuration
spi = None
cs = None
def init_spi():
global spi, cs
# Initialize SPI bus (SPI1) for WeAct BlackPill
# SCK=PA5, MISO=PA6, MOSI=PA7
try:
spi = machine.SPI(
1,
baudrate=1000000,
polarity=0,
phase=0,
bits=8,
firstbit=machine.SPI.MSB,
sck=machine.Pin('A5'),
mosi=machine.Pin('A7'),
miso=machine.Pin('A6'),
)
except (ValueError, TypeError):
try:
spi = machine.SPI(1, baudrate=1000000, polarity=0, phase=0)
except Exception:
spi = machine.SoftSPI(
baudrate=500000,
polarity=0,
phase=0,
sck=machine.Pin('A5'),
mosi=machine.Pin('A7'),
miso=machine.Pin('A6'),
)
# CS pin (adjust based on your wiring)
cs = machine.Pin('A4', machine.Pin.OUT, value=1)
time.sleep_ms(1)
flash_wake()
flash_reset()
disable_protection()
# Check device ID
device_id = read_device_id()
if device_id == 0x000000:
time.sleep_ms(5)
device_id = read_device_id()
print(f"W25Q128 Device ID: {device_id:06X}")
if device_id == 0xEF4018:
print("W25Q128 detected successfully")
else:
print(f"Warning: Unexpected device ID: {device_id:06X} (expected 0xEF4018)")
def read_device_id():
"""Read W25Q128 manufacturer and device ID"""
cs.value(0)
spi.write(bytes([CMD_READ_ID]))
id_data = spi.read(3)
cs.value(1)
return (id_data[0] << 16) | (id_data[1] << 8) | id_data[2]
def flash_wake():
"""Release from power-down (safe to call even if not asleep)"""
cs.value(0)
spi.write(bytes([CMD_RELEASE_POWER_DOWN]))
cs.value(1)
time.sleep_ms(1)
def flash_reset():
"""Reset the flash (W25Q series supports 0x66/0x99)"""
cs.value(0)
spi.write(bytes([CMD_RESET_ENABLE]))
cs.value(1)
time.sleep_us(50)
cs.value(0)
spi.write(bytes([CMD_RESET_MEMORY]))
cs.value(1)
time.sleep_ms(1)
def read_status():
"""Read status register"""
cs.value(0)
spi.write(bytes([CMD_READ_STATUS]))
status = spi.read(1)[0]
cs.value(1)
return status
def read_status2():
"""Read status register-2"""
cs.value(0)
spi.write(bytes([CMD_READ_STATUS2]))
status = spi.read(1)[0]
cs.value(1)
return status
def read_status3():
"""Read status register-3"""
cs.value(0)
spi.write(bytes([CMD_READ_STATUS3]))
status = spi.read(1)[0]
cs.value(1)
return status
def write_status(sr1, sr2):
"""Write status register-1 and -2"""
wait_busy()
write_enable()
cs.value(0)
spi.write(bytes([CMD_WRITE_STATUS, sr1 & 0xFF, sr2 & 0xFF]))
cs.value(1)
wait_busy()
def disable_protection():
"""Clear block protection bits and SRP"""
sr1 = read_status()
sr2 = read_status2()
sr3 = read_status3()
if (sr1 & 0x1C) or (sr1 & 0x80):
print(f"Status before: SR1={sr1:02X} SR2={sr2:02X} SR3={sr3:02X}")
write_status(sr1 & ~0x9C, sr2 & ~0x40)
sr1 = read_status()
sr2 = read_status2()
sr3 = read_status3()
print(f"Status after: SR1={sr1:02X} SR2={sr2:02X} SR3={sr3:02X}")
def wait_busy():
"""Wait until write operation completes"""
while read_status() & 0x01:
time.sleep_us(10)
def write_enable():
"""Enable write operations"""
cs.value(0)
spi.write(bytes([CMD_WRITE_ENABLE]))
cs.value(1)
def write_disable():
"""Disable write operations"""
cs.value(0)
spi.write(bytes([CMD_WRITE_DISABLE]))
cs.value(1)
def read_byte(addr):
"""Read a single byte from address"""
cs.value(0)
spi.write(bytes([CMD_READ_DATA, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF]))
data = spi.read(1)[0]
cs.value(1)
return data
def read_bytes(addr, length):
"""Read multiple bytes from address"""
cs.value(0)
spi.write(bytes([CMD_READ_DATA, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF]))
data = spi.read(length)
cs.value(1)
return data
def write_page(addr, data):
"""Write up to 256 bytes (one page). Address must be page-aligned."""
if len(data) > 256:
raise ValueError("Page write data must be <= 256 bytes")
wait_busy()
write_enable()
# Ensure write-enable latch is set (bit 1)
if (read_status() & 0x02) == 0:
write_enable()
if (read_status() & 0x02) == 0:
raise RuntimeError("Write enable latch not set. Check /WP pin.")
cs.value(0)
spi.write(bytes([CMD_PAGE_PROGRAM, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF]))
spi.write(data)
cs.value(1)
wait_busy()
def write_byte(addr, value):
"""Write a single byte to address"""
write_page(addr, bytes([value & 0xFF]))
def sector_erase(addr):
"""Erase a 4KB sector (sector address must be sector-aligned)"""
wait_busy()
write_enable()
if (read_status() & 0x02) == 0:
write_enable()
if (read_status() & 0x02) == 0:
raise RuntimeError("Write enable latch not set. Check /WP pin.")
cs.value(0)
spi.write(bytes([CMD_SECTOR_ERASE, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF]))
cs.value(1)
wait_busy()
def block_erase_64k(addr):
"""Erase a 64KB block (address must be block-aligned)"""
wait_busy()
write_enable()
cs.value(0)
spi.write(bytes([CMD_BLOCK_ERASE_64K, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF]))
cs.value(1)
wait_busy()
def chip_erase():
"""Erase entire chip (takes several seconds)"""
wait_busy()
write_enable()
cs.value(0)
spi.write(bytes([CMD_CHIP_ERASE]))
cs.value(1)
print("Chip erase started (this may take 10-30 seconds)...")
wait_busy()
print("Chip erase complete")
def write_00_to_ff():
init_spi()
# i2c_display = machine.I2C(1)
# display = ssd1306.SSD1306_I2C(128, 64, i2c_display)
# display.fill(0)
# display.text("W25Q128 Writer", 5, 5, 1)
# display.show()
# Erase first 64KB (16 sectors) before writing
# for sector in range(0, 16):
# sector_addr = sector * 0x1000
# print(f"E {sector_addr:06X}")
# sector_erase(sector_addr)
# Write first 64KB for testing, page-by-page
for page_addr in range(0, 65536, 256):
if page_addr % 1000 == 0:
print(f"W {page_addr:06X}")
# display.fill(0)
# display.text("W25Q128 Writer", 5, 5, 1)
# display.text(f"W {page_addr:06X}", 5, 30, 1)
# display.show()
page = bytes([(page_addr + i) & 0xFF for i in range(256)])
write_page(page_addr, page)
# display.fill(0)
# display.text("W25Q128 Writer", 5, 5, 1)
# display.text(f"Write Complete", 5, 30, 1)
# display.show()
def write(str):
init_spi()
# Parse the input string into address-value pairs and write each value
str = str[2:] # Remove "w " prefix
tokens = str.strip().split()
if len(tokens) % 2 != 0:
raise ValueError("Input string must contain pairs of <addr> <value>")
# i2c_display = machine.I2C(1)
# display = ssd1306.SSD1306_I2C(128, 64, i2c_display)
# display.fill(0)
# display.text("W25Q128 Writer", 5, 5, 1)
# display.show()
for i in range(0, len(tokens), 2):
addr = int(tokens[i], 0) # Support hex (0x...), decimal, etc.
value = int(tokens[i+1], 0)
write_byte(addr, value)
if addr > 0 and addr % 1000 == 0:
print(f"Wrote {value:02X} to {addr:06X}")
# display.fill(0)
# display.text("W25Q128 Writer", 5, 5, 1)
# display.text(f"W {value:02X} to {addr:06X}", 5, 30, 1)
# display.show()
# display.fill(0)
# display.text("W25Q128 Writer", 5, 5, 1)
# display.text(f"W {value:02X} to {addr:06X}", 5, 30, 1)
# display.show()
def erase():
init_spi()
print("Erasing entire W25Q128 chip...")
chip_erase()
print("Erase complete")
def erase_sector(sector_addr):
"""Erase a specific 4KB sector"""
init_spi()
# Align to sector boundary (4KB = 0x1000)
sector_addr = sector_addr & 0xFFFFF000
print(f"Erasing sector at {sector_addr:06X}...")
sector_erase(sector_addr)
print(f"Sector at {sector_addr:06X} erased")
if __name__ == "__main__":
write_00_to_ff()
READ:
import machine
import time
import ssd1306
from machine import SPI, Pin
# W25Q128 SPI Flash Configuration
# W25Q128 has 16MB (16777216 bytes) = 128 Mbit
# W25Q128 Commands
CMD_READ_DATA = 0x03
CMD_READ_STATUS = 0x05
CMD_READ_ID = 0x9F
CMD_FAST_READ = 0x0B
CMD_POWER_DOWN = 0xB9
CMD_RELEASE_POWER_DOWN = 0xAB
CMD_RESET_ENABLE = 0x66
CMD_RESET_MEMORY = 0x99
# SPI Configuration
spi = None
cs = None
def init_spi():
global spi, cs
# Initialize SPI bus (SPI1) for WeAct BlackPill
# SCK=PA5, MISO=PA6, MOSI=PA7
try:
spi = machine.SPI(
1,
baudrate=1000000,
polarity=0,
phase=0,
bits=8,
firstbit=machine.SPI.MSB,
sck=machine.Pin('A5'),
mosi=machine.Pin('A7'),
miso=machine.Pin('A6'),
)
except (ValueError, TypeError):
try:
spi = machine.SPI(1, baudrate=1000000, polarity=0, phase=0)
except Exception:
spi = machine.SoftSPI(
baudrate=500000,
polarity=0,
phase=0,
sck=machine.Pin('A5'),
mosi=machine.Pin('A7'),
miso=machine.Pin('A6'),
)
# CS pin (adjust based on your wiring)
cs = machine.Pin('A4', machine.Pin.OUT, value=1)
time.sleep_ms(1)
flash_wake()
flash_reset()
# Check device ID
device_id = read_device_id()
if device_id == 0x000000:
time.sleep_ms(5)
device_id = read_device_id()
print(f"W25Q128 Device ID: {device_id:06X}")
if device_id == 0xEF4018:
print("W25Q128 detected successfully")
else:
print(f"Warning: Unexpected device ID: {device_id:06X} (expected 0xEF4018)")
def read_device_id():
"""Read W25Q128 manufacturer and device ID"""
cs.value(0)
spi.write(bytes([CMD_READ_ID]))
id_data = spi.read(3)
cs.value(1)
return (id_data[0] << 16) | (id_data[1] << 8) | id_data[2]
def flash_wake():
"""Release from power-down (safe to call even if not asleep)"""
cs.value(0)
spi.write(bytes([CMD_RELEASE_POWER_DOWN]))
cs.value(1)
time.sleep_ms(1)
def flash_reset():
"""Reset the flash (W25Q series supports 0x66/0x99)"""
cs.value(0)
spi.write(bytes([CMD_RESET_ENABLE]))
cs.value(1)
time.sleep_us(50)
cs.value(0)
spi.write(bytes([CMD_RESET_MEMORY]))
cs.value(1)
time.sleep_ms(1)
def read_status():
"""Read status register"""
cs.value(0)
spi.write(bytes([CMD_READ_STATUS]))
status = spi.read(1)[0]
cs.value(1)
return status
def read_byte(addr):
"""Read a single byte from address"""
cs.value(0)
spi.write(bytes([CMD_READ_DATA, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF]))
data = spi.read(1)[0]
cs.value(1)
return data
def read_bytes(addr, length):
"""Read multiple bytes from address"""
cs.value(0)
spi.write(bytes([CMD_READ_DATA, (addr >> 16) & 0xFF, (addr >> 8) & 0xFF, addr & 0xFF]))
data = spi.read(length)
cs.value(1)
return data
def dump_flash(start, length):
"""Dump W25Q128 flash contents"""
init_spi()
# Initialize display (optional)
# i2c_display = machine.I2C(1)
# display = ssd1306.SSD1306_I2C(128, 64, i2c_display)
# display.fill(0)
# display.text("W25Q128 Reader", 5, 5, 1)
# display.show()
for base in range(start, start + length, 16):
row = [f"{base:06X}:"]
# if base % 1000 == 0:
# display.fill(0)
# display.text("W25Q128 Reader", 5, 5, 1)
# display.text(f"R {base:06X}", 5, 30, 1)
# display.show()
chunk_len = min(16, (start + length) - base)
data = read_bytes(base, chunk_len)
for b in data:
row.append(f"{b:02X}")
print(' '.join(row))
if __name__ == "__main__":
# W25Q128 has 16MB (16777216 bytes)
# Read first 64KB for testing
dump_flash(0, 65536)

"""
MPU9250 MicroPython Example
9-axis IMU (Accelerometer, Gyroscope, Magnetometer)
"""
from machine import I2C, Pin
from time import sleep_ms, ticks_ms, ticks_diff
import math
class MPU9250:
"""MPU9250 9-axis IMU driver"""
# MPU9250 I2C address
MPU9250_ADDRESS = 0x68
AK8963_ADDRESS = 0x0C
# Register addresses
PWR_MGMT_1 = 0x6B
ACCEL_XOUT_H = 0x3B
GYRO_XOUT_H = 0x43
TEMP_OUT_H = 0x41
WHO_AM_I = 0x75
# Magnetometer registers
MAG_CNTL = 0x0A
MAG_XOUT_L = 0x03
MAG_ST1 = 0x02
MAG_CNTL2 = 0x0B
MAG_ASAX = 0x10
# Configuration registers
CONFIG = 0x1A
GYRO_CONFIG = 0x1B
ACCEL_CONFIG = 0x1C
ACCEL_CONFIG2 = 0x1D
INT_PIN_CFG = 0x37
USER_CTRL = 0x6A
def __init__(self, i2c, address=MPU9250_ADDRESS):
self.i2c = i2c
self.address = address
# Wake up the MPU9250
self.i2c.writeto_mem(self.address, self.PWR_MGMT_1, b'\x00')
sleep_ms(100)
# Check WHO_AM_I register
who_am_i = self.i2c.readfrom_mem(self.address, self.WHO_AM_I, 1)[0]
if who_am_i != 0x71:
raise RuntimeError(f"MPU9250 not found. WHO_AM_I: 0x{who_am_i:02X}")
# Configure gyroscope (±250°/s)
self.i2c.writeto_mem(self.address, self.GYRO_CONFIG, b'\x00')
# Configure accelerometer (±2g)
self.i2c.writeto_mem(self.address, self.ACCEL_CONFIG, b'\x00')
# Set accelerometer data rate (1kHz) and bandwidth (184Hz)
self.i2c.writeto_mem(self.address, self.ACCEL_CONFIG2, b'\x01')
# Set gyroscope data rate (1kHz) and bandwidth (184Hz)
self.i2c.writeto_mem(self.address, self.CONFIG, b'\x01')
# Initialize magnetometer
self._init_magnetometer()
print("MPU9250 initialized successfully")
def read_accel(self):
"""Read accelerometer data (m/s²)"""
data = self.i2c.readfrom_mem(self.address, self.ACCEL_XOUT_H, 6)
# Convert to signed 16-bit integers
ax = self._combine_bytes(data[0], data[1])
ay = self._combine_bytes(data[2], data[3])
az = self._combine_bytes(data[4], data[5])
# Scale to g (±2g range, 16384 LSB/g)
scale = 16384.0
ax = (ax / scale) * 9.81 # Convert to m/s²
ay = (ay / scale) * 9.81
az = (az / scale) * 9.81
return (ax, ay, az)
def read_gyro(self):
"""Read gyroscope data (°/s)"""
data = self.i2c.readfrom_mem(self.address, self.GYRO_XOUT_H, 6)
# Convert to signed 16-bit integers
gx = self._combine_bytes(data[0], data[1])
gy = self._combine_bytes(data[2], data[3])
gz = self._combine_bytes(data[4], data[5])
# Scale to °/s (±250°/s range, 131 LSB/°/s)
scale = 131.0
gx = gx / scale
gy = gy / scale
gz = gz / scale
return (gx, gy, gz)
def read_temp(self):
"""Read temperature (°C)"""
data = self.i2c.readfrom_mem(self.address, self.TEMP_OUT_H, 2)
temp_raw = self._combine_bytes(data[0], data[1])
# Convert to °C
temp = (temp_raw / 333.87) + 21.0
return temp
def _init_magnetometer(self):
"""Initialize AK8963 magnetometer"""
# Enable I2C master mode and set I2C bypass
self.i2c.writeto_mem(self.address, self.INT_PIN_CFG, b'\x02')
sleep_ms(10)
# Power down magnetometer
self.i2c.writeto_mem(self.AK8963_ADDRESS, self.MAG_CNTL, b'\x00')
sleep_ms(10)
# Enter fuse ROM access mode
self.i2c.writeto_mem(self.AK8963_ADDRESS, self.MAG_CNTL, b'\x0F')
sleep_ms(10)
# Read sensitivity adjustment values
asa_data = self.i2c.readfrom_mem(self.AK8963_ADDRESS, self.MAG_ASAX, 3)
self.mag_sensitivity = [(((d - 128) * 0.5) / 128 + 1) for d in asa_data]
# Power down magnetometer
self.i2c.writeto_mem(self.AK8963_ADDRESS, self.MAG_CNTL, b'\x00')
sleep_ms(10)
# Set to continuous measurement mode (16-bit, 100Hz)
self.i2c.writeto_mem(self.AK8963_ADDRESS, self.MAG_CNTL, b'\x16')
sleep_ms(10)
print("Magnetometer initialized")
def read_mag(self):
"""Read magnetometer data (µT - microtesla)"""
try:
# Check if data is ready
status = self.i2c.readfrom_mem(self.AK8963_ADDRESS, self.MAG_ST1, 1)[0]
if not (status & 0x01):
return (0, 0, 0)
# Read magnetometer data (7 bytes: ST1, XL, XH, YL, YH, ZL, ZH)
data = self.i2c.readfrom_mem(self.AK8963_ADDRESS, self.MAG_XOUT_L, 7)
# Check overflow
if data[6] & 0x08:
return (0, 0, 0)
# Convert to signed 16-bit integers (little-endian)
mx = self._combine_bytes(data[2], data[1])
my = self._combine_bytes(data[4], data[3])
mz = self._combine_bytes(data[6], data[5])
# Apply sensitivity adjustment
mx = mx * self.mag_sensitivity[0] * 0.6 # Convert to µT (4912/32760 * 4)
my = my * self.mag_sensitivity[1] * 0.6
mz = mz * self.mag_sensitivity[2] * 0.6
return (mx, my, mz)
except:
return (0, 0, 0)
def _combine_bytes(self, msb, lsb):
"""Combine two bytes into signed 16-bit integer"""
value = (msb << 8) | lsb
if value >= 0x8000:
value = -((65535 - value) + 1)
return value
def main():
"""Example usage of MPU9250"""
# Initialize I2C
# For ESP32-C6: Adjust pins according to your wiring
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
# Scan I2C bus
print("Scanning I2C bus...")
devices = i2c.scan()
print(f"Found devices: {[hex(d) for d in devices]}")
# Initialize MPU9250
try:
mpu = MPU9250(i2c)
except Exception as e:
print(f"Error initializing MPU9250: {e}")
return
print("\nReading MPU9250 data...")
print("Press Ctrl+C to stop\n")
# Initialize angle tracking
angle_x = 0.0
angle_y = 0.0
angle_z = 0.0
last_time = ticks_ms()
try:
while True:
# Calculate time difference
current_time = ticks_ms()
dt = ticks_diff(current_time, last_time) / 1000.0 # Convert to seconds
last_time = current_time
# Read accelerometer
ax, ay, az = mpu.read_accel()
# Read gyroscope
gx, gy, gz = mpu.read_gyro()
# Integrate gyroscope to get angles
angle_x += gx * dt
angle_y += gy * dt
angle_z += gz * dt
# Read magnetometer
mx, my, mz = mpu.read_mag()
# Calculate heading (0-360°)
heading = math.atan2(my, mx) * 180 / math.pi
if heading < 0:
heading += 360
# Read temperature
temp = mpu.read_temp()
# clear console
print("\033[2J\033[H", end="") # ANSI escape codes to clear screen
# Display data
print("=" * 50)
print(f"Accelerometer (m/s²):")
print(f" X: {ax:7.3f} Y: {ay:7.3f} Z: {az:7.3f}")
print(f"Gyroscope Angles (°):")
print(f" Roll: {angle_x:7.2f}° (X-axis)")
print(f" Pitch: {angle_y:7.2f}° (Y-axis)")
print(f" Yaw: {angle_z:7.2f}° (Z-axis)")
print(f"Gyroscope Speed (°/s):")
print(f" X: {gx:7.2f} Y: {gy:7.2f} Z: {gz:7.2f}")
print(f"Magnetometer (µT):")
print(f" X: {mx:7.2f} Y: {my:7.2f} Z: {mz:7.2f}")
print(f"Heading: {heading:6.2f}° (0°=North, 90°=East)")
print(f"Temperature: {temp:.2f}°C")
sleep_ms(100)
except KeyboardInterrupt:
print("\n\nStopped by user")
if __name__ == "__main__":
main()


# tb6612fng_simple.py
# MicroPython example for TB6612FNG + two DC motors (e.g. Tamiya 4WD)
# Works on Pico, ESP32, etc.
from machine import Pin, PWM
from time import sleep
# ────────────────────────────────────────────────
# Pin definitions (change to match YOUR wiring!)
# ────────────────────────────────────────────────
STBY = Pin(22, Pin.OUT) # Standby pin – must be HIGH to enable driver
# Motor A (usually left motors on Tamiya 4WD)
AIN1 = Pin(18, Pin.OUT)
AIN2 = Pin(19, Pin.OUT)
PWMA = PWM(Pin(20), freq=1000, duty_u16=0) # PWM freq 1kHz is fine
# ────────────────────────────────────────────────
# Helper functions
# ────────────────────────────────────────────────
def motor_a(speed):
"""
speed: -1000 to +1000
positive = forward
negative = reverse
0 = stop (coast)
"""
if speed > 0:
AIN1.value(1)
AIN2.value(0)
PWMA.duty_u16(speed * 65) # 0→65535 range
elif speed < 0:
AIN1.value(0)
AIN2.value(1)
PWMA.duty_u16((-speed) * 65)
else:
AIN1.value(0) # coast stop
AIN2.value(0)
PWMA.duty_u16(0)
def drive(left_speed):
""" left_speed: -1000 to 1000 """
motor_a(left_speed)
# ────────────────────────────────────────────────
# Main demo
# ────────────────────────────────────────────────
STBY.value(1) # Enable the driver (important!)
print("TB6612FNG enabled")
try:
print("Forward slow...")
drive(700)
sleep(5)
print("Forward full...")
drive(1000)
sleep(5)
print("Stop (coast)...")
drive(0)
sleep(3)
print("Backward slow...")
drive(-700)
sleep(5)
print("Backward full...")
drive(-1000)
sleep(5)
print("Brake demo (short brake)...")
AIN1.value(1);
AIN2.value(1) # short brake on A
PWMA.duty_u16(0)
sleep(3)
print("Stop everything")
drive(0)
finally:
STBY.value(0) # Optional: go to low-power standby
print("Done.")
Setting up the ICESugar FPGA toolchain is trouble in mac, so i built the docker image. This toolchain is for icesugar 40 only, my board is muselab.
Dockerfile is in https://gitlab.quantr.hk/example/chisel/chisel-book/-/blob/master/Dockerfile?ref_type=heads . See this project makefile then you know how to use it https://gitlab.quantr.hk/example/chisel/chisel-book/-/blob/master/example13_icesugar40_rgb_led/Makefile?ref_type=heads
因為想知道Pulseview背後運作原理來,又想為cpu開發加入一啲特別嘅功能,所以決定用STM32自己搞一隻logic analyzer,雖然STM32用來搞不會很高速,但足了之解其原理,之後再用FPGA搞會非常容易

Pulseview是logic analyzer的GUI界面,sigrok-cli是命令行模式,兩者是呼叫一個名為libsigrok的庫去和你的logic analyzer溝通。至於libsigrok用什麼去和你的logic analyzer溝運,這個是你個人意願,因為stm32可以自己變成uart device所以就當它是uart去溝通最容易。你的logic analyzer可以有兩個mode,stream vs buffer。stream就是收幾多signal就立即傳回給libsigrok,buffer就是先儲起直至你認為儲夠再傳回至libsigrok。
我用stm32的F411和H750完成,可以看代碼,主要是main.c。因為libsigrok無可能認得你個device,所以要跟這個tutorial去改。最終效果如下

Filters and Power Converters in Electronics – A Brief Overview
In analog electronics, frequency-selective filters are essential building blocks for signal processing. The low-pass filter (LPF) allows low frequencies to pass while attenuating higher ones, making it ideal for noise reduction. Conversely, the high-pass filter (HPF) blocks low frequencies (including DC offset) and passes higher ones. Band-pass filters (BPF) permit only a specific range of frequencies to pass, useful for frequency selection in communication systems, while band-stop/notch filters (BSF) suppress a narrow unwanted frequency band (e.g. 50/60 Hz hum interference). These first- and second-order RC/RL/RLC circuits exhibit characteristic -20 dB/decade or -40 dB/decade roll-off beyond their corner/cutoff frequencies.
On the power electronics side, static converters enable efficient transformation between different voltage types and levels. AC-DC converters (rectifiers) transform alternating current into direct current, forming the front-end of most power supplies. DC-DC converters (such as buck, boost, buck-boost, etc.) step up or step down DC voltage levels and are ubiquitous in battery-powered devices, electric vehicles, and renewable energy systems. DC-AC converters (inverters) convert DC (from batteries, solar panels, or DC links) into AC, powering AC motors, grid-tied solar systems, and uninterruptible power supplies. Finally, AC-AC converters (direct or indirect via DC link) allow voltage magnitude and/or frequency transformation, commonly used in motor drives and power transmission.
Together, these two domains — precise signal filtering and high-efficiency power conversion — form the foundation of modern electronics, spanning audio processing, RF communication, renewable energy systems, electric transportation, and industrial automation.



#include <Servo.h>
#include <AccelStepper.h>
// Define stepper driver interface and pins (Driver mode: 1 = Step/DIR)
#define STEP_PIN 2
#define DIR_PIN 5
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
// Define servo object and pin
Servo myServo;
#define SERVO_PIN 9
void setup() {
Serial.begin(115200);
// Initialize the servo
myServo.attach(SERVO_PIN);
myServo.write(90); // Start at 90 degrees
// Configure stepper motor parameters
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(500.0);
stepper.moveTo(1600); // Move 1600 steps
}
void loop() {
// 1. Non-blocking AccelStepper update (must be called frequently)
stepper.run();
// 2. Check if the stepper reached its target and reverse direction
if (stepper.distanceToGo() == 0) {
// Reverse target position
stepper.moveTo(-stepper.currentPosition());
// Sweep the servo to a new angle (e.g., alternate between 0 and 180)
int currentAngle = myServo.read();
Serial.println(currentAngle);
if (currentAngle == 0) {
myServo.write(90);
} else if (currentAngle == 90) {
myServo.write(180);
} else if (currentAngle == 180) {
myServo.write(270);
} else{
myServo.write(0);
}
}
}

docker run -it -v .:/chisel-book --name ice40 ubuntu:24.04
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get install build-essential clang bison flex libreadline-dev \
gawk tcl-dev libffi-dev git mercurial graphviz \
xdot pkg-config python3 libftdi-dev vim \
curl openjdk-25-jdk python3-dev libboost-all-dev cmake libeigen3-dev -y
cd /chisel-book
mkdir ice40
cd ice40
git clone https://github.com/YosysHQ/icestorm.git icestorm
cd icestorm
make -j
make install
cd ..
git clone https://github.com/cseed/arachne-pnr.git arachne-pnr
cd arachne-pnr
make -j
make install
cd ..
git clone https://github.com/YosysHQ/nextpnr nextpnr
cd nextpnr
cmake -DARCH=ice40 -DCMAKE_INSTALL_PREFIX=/usr/local . -B build
cd build
make # -j won't work
make install
cd ../..
git clone https://github.com/YosysHQ/yosys.git yosys
cd yosys
git submodule update --init
make -j4
make install
cd ..
Backup
docker exec -it gitlab gitlab-backup create STRATEGY=copy
This creates a single .tar file that contains everything: repositories, database, uploads, builds, artifacts, LFS, registry, pages, etc. The backup file is saved inside the container at $GITLAB_HOME/data/backups
Restore
# 1. Stop GitLab
docker compose down
# 2. Put the .tar file into $GITLAB_HOME/data/backups/
# 3. Restore
docker exec -it gitlab gitlab-backup restore BACKUP=1732301234_2025_11_22_18.6.0
# 4. Start again
docker compose up -d
https://github.com/quantrpeter/ec11-waveshare-esp32c6

from machine import Pin
import time
class EC11:
def __init__(self, pin_a, pin_b, pin_c):
"""
Initialize EC11 rotary encoder for TaoBao version with external pull-ups
pin_a, pin_b: rotation pins (Terminal A and B)
pin_c: push button pin (Terminal C)
IMPORTANT: This version has external 10K pull-ups to 5V
So we use Pin.IN (no internal pull-up)
"""
# No internal pull-ups since external 10K pull-ups exist
self.pin_a = Pin(pin_a, Pin.IN)
self.pin_b = Pin(pin_b, Pin.IN)
# Use pull-down for button to make logic clearer: pressed=1, unpressed=0
self.pin_c = Pin(pin_c, Pin.IN, Pin.PULL_DOWN)
# Quadrature state tracking
self.last_state = (self.pin_a.value() << 1) | self.pin_b.value()
# Lookup table for quadrature decoding
# Based on state transitions: 00->01->11->10->00 (CW) or reverse (CCW)
self.state_table = [0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0]
# Counter for rotation
self.counter = 0
# Accumulator for detent detection (4 steps = 1 detent)
self.step_accumulator = 0
# Button state tracking
self.last_button = self.pin_c.value()
self.button_debounce_time = 0
def read_rotation(self):
"""
Read rotation direction using quadrature decoding
Returns: 1 for clockwise, -1 for counter-clockwise, 0 for no change
"""
# Read current state
current_state = (self.pin_a.value() << 1) | self.pin_b.value()
# Calculate index for lookup table
index = (self.last_state << 2) | current_state
# Get direction from lookup table
direction = self.state_table[index]
# Accumulate steps - only return value after 4 steps (1 detent)
if direction != 0:
self.step_accumulator += direction
self.counter += direction
# Check if we've completed a detent (4 steps in one direction)
if abs(self.step_accumulator) >= 4:
result = 1 if self.step_accumulator > 0 else -1
self.step_accumulator = 0
self.last_state = current_state
return result
# Update last state
self.last_state = current_state
return 0
def read_button(self):
"""
Read button press with software debouncing
Returns: True if button was just pressed, False otherwise
"""
current_time = time.ticks_ms()
current_button = self.pin_c.value()
# print('current_button', current_button)
# Button is pressed when pin goes HIGH (from LOW to HIGH)
# With internal pull-down: unpressed=0, pressed=1
# Add debouncing: ignore changes within 50ms
if self.last_button == 0 and current_button == 1:
if time.ticks_diff(current_time, self.button_debounce_time) > 50:
self.button_debounce_time = current_time
self.last_button = current_button
return True
self.last_button = current_button
return False
def get_counter(self):
"""Get current counter value"""
return self.counter
def reset_counter(self):
"""Reset counter to zero"""
self.counter = 0
# Initialize EC11 with pins 3, 4, 5
encoder = EC11(pin_a=5, pin_b=3, pin_c=4)
print("EC11 Rotary Encoder Test (TaoBao Version)")
print("Pin A: 3, Pin B: 4, Pin C: 5")
print("External 10K pull-ups to 5V with 0.01uF caps")
print("Rotate encoder or press button...")
print("Press Ctrl+C to exit")
print()
try:
while True:
# Check rotation
rotation = encoder.read_rotation()
if rotation == 1:
print(f"↻ Clockwise - Counter: {encoder.get_counter()}")
elif rotation == -1:
print(f"↺ Anti-clockwise - Counter: {encoder.get_counter()}")
# Check button press
if encoder.read_button():
print(f"🔘 Button pressed! Counter: {encoder.get_counter()} → 0")
encoder.reset_counter()
time.sleep_ms(1) # Small delay to prevent excessive polling
except KeyboardInterrupt:
print("\nProgram stopped")
