Date: | 2024-02-25 08:03:32 |
Description: | 不想用 Keil 开发,尝试用命令行工具通过简单直接的方式开发 8051 单片机程序。 |
Keywords: | 8051, MCU, STC, Command Line Tools, sdas8051, 命令行工具, Makefile |
Tag: | mcu, build, sdas8051, mcs-51, 8051, stc, makefile |
Link: | https://www.diewuxi.com/blog/article/53.html |
Table of contents
^ 1 开发环境
macOS Sonoma 14.1
sdcc 4.2.0 #13081 (Mac OS X ppc)
sdas8051 V02.00 + NoICE + SDCC mods (Intel 8051)
sdld V03.00 + NoICE + sdld
packihx unknown
makebin unknown
stcgal 1.10
STC90C516RD+ 11.0592 MHz
^ 2 目录结构和代码
- 目录结构
2024-02-26_led_flowing_cli/
led_flowing.asm
- 代码(
led_flowing.asm
)
Port 0(P0) 上的 8 个 LED 灯逐个亮起一下。按字节方式修改 P0 寄存器,使用循环指令 RL 改变字节数值,使用软件方式延时。
START:
MOV R0, #8
MOV A, #0b11111110
LOOP:
MOV P0, A
ACALL DELAY
RL A
DJNZ R0, LOOP
AJMP START
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Delay time ~ (R5 * 0.01) s, at 12 MHz clock.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DELAY:
MOV R5, #20
DLY1:
MOV R6, #100
DLY2:
MOV R7, #50
DJNZ R7, .
DJNZ R6, DLY2
DJNZ R5, DLY1
RET
^ 3 构建程序的步骤
^ 3.1 用汇编器汇编代码,生成对象文件(.rel)
$ sdas8051 -o led_flowing.rel led_flowing.asm
sdas8051 是 sdcc 包含的 Intel 8051 单片机汇编器,使用方法:
sdas Assembler V02.00 + NoICE + SDCC mods (Intel 8051)
Copyright (C) 2012 Alan R. Baldwin
This program comes with ABSOLUTELY NO WARRANTY.
Usage: [-Options] file
Usage: [-Options] outfile file1 [file2 file3 ...]
-d Decimal listing
-q Octal listing
-x Hex listing (default)
-g Undefined symbols made global
-n Don't resolve global assigned value symbols
-a All user symbols made global
-b Display .define substitutions in listing
-bb and display without .define substitutions
-c Disable instruction cycle count in listing
-j Enable NoICE Debug Symbols
-y Enable SDCC Debug Symbols
-l Create list file/outfile[.lst]
-o Create object file/outfile[.rel]
-s Create symbol file/outfile[.sym]
-p Disable automatic listing pagination
-u Disable .list/.nlist processing
-w Wide listing format for symbol table
-z Disable case sensitivity for symbols
-f Flag relocatable references by ` in listing file
-ff Flag relocatable references by mode in listing file
-I Add the named directory to the include file
search path. This option may be used more than once.
Directories are searched in the order given.
removing
使用 -o
选项创建对象文件.
^ 3.2 使用链接器生成 .ihx 文件
$ sdld -n -i led_flowing.rel
sdld 是 sdcc 包含的链接器,使用方法:
sdld Linker V03.00 + NoICE + sdld
Usage: [-Options] [-Option with arg] file
Usage: [-Options] [-Option with arg] outfile file1 [file2 ...]
Startup:
-p Echo commands to stdout (default)
-n No echo of commands to stdout
Alternates to Command Line Input:
-c ASlink >> prompt input
-f file[.lk] Command File input
Libraries:
-k Library path specification, one per -k
-l Library file specification, one per -l
Relocation:
-b area base address = expression
-g global symbol = expression
Map format:
-m Map output generated as (out)file[.map]
-w Wide listing format for map file
-x Hexadecimal (default)
-d Decimal
-q Octal
Output:
-i Intel Hex as (out)file[.ihx]
-s Motorola S Record as (out)file[.s19]
-j NoICE Debug output as (out)file[.noi]
-y SDCDB Debug output as (out)file[.cdb]
List:
-u Update listing file(s) with link data as file(s)[.rst]
Case Sensitivity:
-z Disable Case Sensitivity for Symbols
Miscellaneous:
-I [iram-size] Check for internal RAM overflow
-X [xram-size] Check for external RAM overflow
-C [code-size] Check for code overflow
-M Generate memory usage summary file[.mem]
-S [stack-size] Allocate space for stack
End:
-e or null line terminates input
使用 -i
选项创建 Intel Hex 格式的.
^ 3.3 生成 hex 文件供单片机下载
(1) 优化 ihx 文件,生成 hex 文件供单片机下载
$ packihx led_flowing.ihx > led_flowing.hex
sdas8051 是 sdcc 包含的工具,工具本身没有使用方法,其源代码中有以下说明:
/*-----------------------------------------------------------------------
* packihx.c:
*
* utility to pack an Intel HEX format file by removing redundant
* extended offset records and accumulating data records up to
* OUTPUT_CHUNK (currently 16) bytes.
*
* Released to the public domain 10/16/2000 Kevin Vigor.
*/
大概是优化 ihx 文件,如把长行变短。对比文件内容如下:
led_flowing.ihx
:
:1A002000780874FEF580112D23D8F901207D147E647F32DFFEDEFADDF6223E
:00000001FF
led_flowing.hex
:
:10002000780874FEF580112D23D8F901207D147E07
:0A003000647F32DFFEDEFADDF62207
:00000001FF
(2) 转换 ihx 到二进制格式数据
$ makebin -p -s 65536 led_flowing.ihx led_flowing.bin
makebin 是 sdcc 包含的工具,转换 ihx 文件到二进制或者 GameBoy 二进制格式,使用方法如下:
makebin: convert a Intel IHX file to binary or GameBoy format binary.
Usage: makebin [options] [<in_file> [<out_file>]]
Options:
-p pack mode: the binary file size will be truncated to the last occupied byte
-s romsize size of the binary file (default: rom banks * 16384)
-Z generate GameBoy format binary file
-S generate Sega Master System format binary file
-o bytes skip amount of bytes in binary file
SMS format options (applicable only with -S option):
-xo n rom size (0xa-0x2) (default: 0xc)
-xj n set region code (3-7) (default: 4)
-xv n version number (0-15) (default: 0)
GameBoy format options (applicable only with -Z option):
-yo n number of rom banks (default: 2) (autosize: A)
-ya n number of ram banks (default: 0)
-yt n MBC type (default: no MBC)
-yl n old licensee code (default: 0x33)
-yk cc new licensee string (default: 00)
-yn name cartridge name (default: none)
-yc GameBoy Color compatible
-yC GameBoy Color only
-ys Super GameBoy
-yS Convert .noi file named like input file to .sym
-yj set non-Japanese region flag
-yN do not copy big N validation logo into ROM header
-yp addr=value Set address in ROM to given value (address 0x100-0x1FE)
Arguments:
<in_file> optional IHX input file, '-' means stdin. (default: stdin)
<out_file> optional output file, '-' means stdout. (default: stdout)
使用 -p
选项只保留代码用到的空间,否则的话会补成 FF
,使用 -s
指定代码空间的大小,使用默认值有时候会出错(error: size of the buffer is too small.
)。
^ 4 下载到单片机
# stcgal -b 115200 -p /dev/tty.usbserial-2410 led_flowing.hex
或
# stcgal -b 115200 -p /dev/tty.usbserial-2410 led_flowing.bin
stcgal 是一个 Python 实现的 STC 单片机 ISP 下载工具,我通过 pip 安装的,使用方法如下:
usage: stcgal [-h] [-e] [-a] [-A {dtr,rts}] [-r RESETCMD] [-P {stc89,stc89a,stc12a,stc12b,stc12,stc15a,stc15,stc8,stc8d,stc8g,usb15,auto}] [-p PORT]
[-b BAUD] [-l HANDSHAKE] [-o OPTION] [-t TRIM] [-D] [-V]
[code_image] [eeprom_image]
stcgal 1.10 - an STC MCU ISP flash tool
(C) 2014-2018 Grigori Goronzy and others
https://github.com/grigorig/stcgal
positional arguments:
code_image code segment file to flash (BIN/HEX)
eeprom_image eeprom segment file to flash (BIN/HEX)
optional arguments:
-h, --help show this help message and exit
-e, --erase only erase flash memory
-a, --autoreset cycle power automatically by asserting DTR
-A {dtr,rts}, --resetpin {dtr,rts}
pin to hold down when using --autoreset (default: DTR)
-r RESETCMD, --resetcmd RESETCMD
shell command for board power-cycling (instead of DTR assertion)
-P {stc89,stc89a,stc12a,stc12b,stc12,stc15a,stc15,stc8,stc8d,stc8g,usb15,auto}, --protocol {stc89,stc89a,stc12a,stc12b,stc12,stc15a,stc15,stc8,stc8d,stc8g,usb15,auto}
protocol version (default: auto)
-p PORT, --port PORT serial port device
-b BAUD, --baud BAUD transfer baud rate (default: 115200)
-l HANDSHAKE, --handshake HANDSHAKE
handshake baud rate (default: 2400)
-o OPTION, --option OPTION
set option (can be used multiple times, see documentation)
-t TRIM, --trim TRIM RC oscillator frequency in kHz (STC15+ series only)
-D, --debug enable debug output
-V, --version print version info and exit
使用 -b
选项设置传输 Baud rate,使用 -p
选项设置串口设备,我的开发板的 USB 转串口芯片是 PL2303,发现在 MacOS 下不用装驱动,串口在操作系统里面显示为 /dev/tty.usbserial-2410,在不通计算机上后面的数字可能不同。其它选项使用默认值。
命令执行后,会有一个提示,这时需要关闭一下开发板电源(已经关了的请忽略),然后再打开,这时才开始传输数据,即所谓的冷启动。
^ 5 使用 Makefile 文件
MAIN = led_flowing
PORT=/dev/tty.usbserial-2410
.PHONY: all clean rel ihx hex bin upload uploadbin
all: hex bin
$(MAIN).rel: $(MAIN).asm
sdas8051 -o $(MAIN).rel $(MAIN).asm
$(MAIN).ihx: $(MAIN).rel
sdld -i $(MAIN).ihx $(MAIN).rel
$(MAIN).hex: $(MAIN).ihx
packihx $(MAIN).ihx > $(MAIN).hex
$(MAIN).bin: $(MAIN).ihx
makebin -p -s 65536 $(MAIN).ihx $(MAIN).bin
upload: $(MAIN).hex
stcgal -b 115200 -p $(PORT) $(MAIN).hex
clean:
- rm $(MAIN).rel
- rm $(MAIN).ihx
- rm $(MAIN).hex
- rm $(MAIN).bin
make
构建 hex 和 bin 文件make upload
先构建程序,然后下载 hex 到单片机make clean
清理生成的文件
^ 6 Changelog
* 2025-04-19
* Fix: sdas8051 sdld 是 sdcc 包含的链接器
* 2025-03-30
* Fix: Duplicate header number.
* 2024-02-26
* Done
Last modified: 2025-04-19
There is no comments now.