diff --git a/Labs/Solutions/Lab4-Sol.tar.gz b/Labs/Solutions/Lab4-Sol.tar.gz new file mode 100644 index 0000000..9c0af63 Binary files /dev/null and b/Labs/Solutions/Lab4-Sol.tar.gz differ diff --git a/Labs/lab2/lab2.md b/Labs/lab2/lab2.md index a8add60..84057ec 100644 --- a/Labs/lab2/lab2.md +++ b/Labs/lab2/lab2.md @@ -1,8 +1,11 @@ # Lab2 C 语言基本语法 + ## Lab2_1 C 语言基本语法 + - 完成lab2_1中的四个.c文件 - 请根据每个ex_中的TODO完成内容。 - 你可以使用 make 自动编译可执行文件 + ```bash make # 编译所有可执行文件 make ex1_ptr # 仅编译 ex1_ptr.c @@ -11,12 +14,16 @@ ``` ## Lab2_2 字符串和循环 + - 本实验使用了参数char *argv[],请在编译后尝试向程序输入命令行参数,如: + ```bash make ./str_and_cyc California Oregon Washington Texas ``` + 输出内容,如下所示: + ```bash for: arg 1: California @@ -41,6 +48,7 @@ state 2: Washington state 3: Texas ``` + - 请对srt_and_cyc.c文件中的for和while循环尝试以下操作并查看结果。 - for循环 - 将i初始化为0看看会发生什么。是否也需要改动argc,不改动的话它能正常工作吗?为什么下标从0开始可以正常工作? @@ -52,7 +60,6 @@ - 让这个复制循环不会执行失败,即使argv之中有很多元素也不会全部放进states。 - 研究你是否真正复制了这些字符串。答案可能会让你感到意外和困惑。 - ## 递归(可选,难度较大) - 经典递归例子 @@ -96,6 +103,7 @@ - 实现一个递归函数`bool solveSudoku(int board[N][N], int row, int col)`判断数独是否可解,有解则填充正确答案。 - 题中数独采用二维数组,需解答填充的数字用0代替如: + ```txt 0 6 0 0 0 0 0 7 1 7 0 5 4 0 3 0 0 0 @@ -107,6 +115,7 @@ 0 7 9 0 0 0 5 0 2 0 0 0 0 0 4 7 0 0 ``` + - 使用`printBoard(int board[N][N])`函数打印数独 - 使用`isSafe(int board[N][N], int row, int col, int num)`函数检查数字是否可以放在board[row][col]位置。 - 运行judge.sh检测程序正确性。 diff --git a/Labs/lab3/AnswerSheet.md b/Labs/lab3/AnswerSheet.md deleted file mode 100644 index 850a3fd..0000000 --- a/Labs/lab3/AnswerSheet.md +++ /dev/null @@ -1,29 +0,0 @@ -## 任务答题卡 -### 任务一 文件配对 -**Q1:task0.c对应哪个.ihx文件呢** -- [ ] maomao.ihx -- [ ] miaomiao.ihx -- [ ] wangwang.ihx - -**Q2:task1.c对应哪个.ihx文件呢** -- [ ] maomao.ihx -- [ ] miaomiao.ihx -- [ ] wangwang.ihx - -**Q3:task2.c对应哪个.ihx文件呢** -- [ ] maomao.ihx -- [ ] miaomiao.ihx -- [ ] wangwang.ihx - -### 任务二 完善呼吸灯 -**Q4:请把代码输入到代码框中**(C语言代码和编译生成的十六进制数据都粘帖上来吧) - -**这里是C语言代码** -```c - -``` - -**这里是编译产生的十六进制数据** -```hex - -``` \ No newline at end of file diff --git a/Labs/lab3/Demo/.clang-format b/Labs/lab3/Demo/.clang-format deleted file mode 100644 index 5cf6964..0000000 --- a/Labs/lab3/Demo/.clang-format +++ /dev/null @@ -1,38 +0,0 @@ ---- -BasedOnStyle: Microsoft -Language: Cpp - -################################### -# indent conf -################################### - -UseTab: Never -IndentWidth: 4 -TabWidth: 4 -ColumnLimit: 0 -AccessModifierOffset: -4 -NamespaceIndentation: All -FixNamespaceComments: false -BreakBeforeBraces: Linux - -################################### -# other styles -################################### - -# -# for more conf, you can ref: https://clang.llvm.org/docs/ClangFormatStyleOptions.html -# - -AllowShortIfStatementsOnASingleLine: true - -AllowShortLoopsOnASingleLine: true - -AllowShortBlocksOnASingleLine: true - -IndentCaseLabels: true - -SortIncludes: false - -AlignConsecutiveMacros: AcrossEmptyLines - -AlignConsecutiveAssignments: Consecutive diff --git a/Labs/lab3/Demo/.eide/debug.files.options.yml b/Labs/lab3/Demo/.eide/debug.files.options.yml deleted file mode 100644 index 196cc55..0000000 --- a/Labs/lab3/Demo/.eide/debug.files.options.yml +++ /dev/null @@ -1,31 +0,0 @@ -########################################################################################## -# Append Compiler Options For Source Files -# -# syntax: -# : -# -# examples: -# 'main.cpp': --cpp11 -Og ... -# 'src/*.c': -gnu -O2 ... -# 'src/lib/**/*.cpp': --cpp11 -Os ... -# '!Application/*.c': -O0 -# '**/*.c': -O2 -gnu ... -# -# For more syntax, please refer to: https://www.npmjs.com/package/micromatch -# -########################################################################################## - -version: '1.0' - -# -# for source files with filesystem paths -# -files: -# './test/**/*.c': --c99 - -# -# for source files with virtual paths -# -virtualPathFiles: -# 'virtual_folder/**/*.c': --c99 - diff --git a/Labs/lab3/Demo/.eide/debug.options.sdcc.json b/Labs/lab3/Demo/.eide/debug.options.sdcc.json deleted file mode 100644 index 4e1f207..0000000 --- a/Labs/lab3/Demo/.eide/debug.options.sdcc.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "version": 3, - "beforeBuildTasks": [], - "afterBuildTasks": [], - "global": { - "device": "mcs51", - "optimize-type": "speed", - "misc-controls": "--iram-size 256 --xram-size 0 --code-size 8192" - }, - "c/cpp-compiler": { - "language-c": "c99" - }, - "asm-compiler": {}, - "linker": { - "$mainFileName": "main", - "output-format": "hex" - } -} \ No newline at end of file diff --git a/Labs/lab3/Demo/.eide/eide.json b/Labs/lab3/Demo/.eide/eide.json deleted file mode 100644 index 36ea454..0000000 --- a/Labs/lab3/Demo/.eide/eide.json +++ /dev/null @@ -1,43 +0,0 @@ -{ - "name": "Demo", - "type": "C51", - "dependenceList": [], - "srcDirs": [ - "src", - "inc" - ], - "virtualFolder": { - "name": "", - "files": [], - "folders": [] - }, - "outDir": "build", - "deviceName": null, - "packDir": null, - "miscInfo": { - "uid": "657417bca61dce1087a93a37f599d290" - }, - "targets": { - "Debug": { - "excludeList": [], - "toolchain": "SDCC", - "compileConfig": { - "options": "null" - }, - "uploader": "Custom", - "uploadConfig": { - "bin": "", - "commandLine": "sudo chmod 666 /dev/ttyUSB0 && python ./tools/stcflash.py -p /dev/ttyUSB0 \"${hexFile}\"", - "eraseChipCommand": "" - }, - "uploadConfigMap": {}, - "custom_dep": { - "name": "default", - "incList": [], - "libList": [], - "defineList": [] - } - } - }, - "version": "3.4" -} \ No newline at end of file diff --git a/Labs/lab3/Demo/Demo.code-workspace b/Labs/lab3/Demo/Demo.code-workspace deleted file mode 100644 index e14d20b..0000000 --- a/Labs/lab3/Demo/Demo.code-workspace +++ /dev/null @@ -1,49 +0,0 @@ -{ - "folders": [ - { - "path": "." - } - ], - "settings": { - "EIDE.SourceTree.AutoSearchIncludePath": true, - "EIDE.SourceTree.AutoSearchObjFile": true, - "files.associations": { - "*.C": "c", - ".eideignore": "ignore", - "stdio.h": "c", - "*.a51": "a51", - "*.h": "c", - "*.c": "c", - "*.hxx": "cpp", - "*.hpp": "cpp", - "*.c++": "cpp", - "*.cpp": "cpp", - "*.cxx": "cpp", - "*.cc": "cpp" - }, - "files.autoGuessEncoding": true, - "EIDE.ARM.Option.AutoGenerateRTE_Components": false, - "C_Cpp.default.configurationProvider": "cl.eide", - "C_Cpp.errorSquiggles": "disabled", - "[yaml]": { - "editor.insertSpaces": true, - "editor.tabSize": 4, - "editor.autoIndent": "advanced" - } - }, - "extensions": { - "recommendations": [ - "cl.eide", - "keroc.hex-fmt", - "xiaoyongdong.srecord", - "hars.cppsnippets", - "zixuanwang.linkerscript", - "redhat.vscode-yaml", - "IBM.output-colorizer", - "cschlosser.doxdocgen", - "ms-vscode.vscode-serial-monitor", - "alefragnani.project-manager", - "cl.stm8-debug" - ] - } -} \ No newline at end of file diff --git a/Labs/lab3/Demo/README.md b/Labs/lab3/Demo/README.md deleted file mode 100644 index c8eb093..0000000 --- a/Labs/lab3/Demo/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# 使用说明 -这是使用EIDE的一个示例 diff --git a/Labs/lab3/Demo/README.md.bak b/Labs/lab3/Demo/README.md.bak deleted file mode 100644 index 3863379..0000000 --- a/Labs/lab3/Demo/README.md.bak +++ /dev/null @@ -1,401 +0,0 @@ -# Lab3-体验嵌入式 - -part1基础配置的相关配置我已经写好了插件,不过还是建议一步一步配置下去,这样才能快速了解环境,下次换台电脑也能很快的配置好环境 - -> 安装文件放在./install文件夹中 - -当然,假如你是在配到头大,那还是退回到上一个`快照`,然后在 -bash中输入 - -```bash -./install/install.sh -``` - -一键安装,编译,和烧录 -在烧录之前记得检查一下串口设备有没有接入 - -```bash -ls /dev/ttyUSB* -``` - -假如出现 - -```bash -mayge@mayge:~/Desktop/WorkSpace/lab2/install $ls /dev/ttyUSB* -ls: 无法访问 '/dev/ttyUSB*': 没有那个文件或目录 -``` - -那检查一下在哪一部出现了问题,可以仔细阅读**Part1 基础配置** - -假如出现 - -```bash -mayge@mayge:~/Desktop/WorkSpace/lab2/install $ls /dev/ttyUSB* -/dev/ttyUSB0 -``` - -干的漂亮 -假如是一键安装的话,成功之后还是尝试一下自己配置哦. - -## Part1 基础配置 - -**所需材料**:一块stc89c52rc芯片,一个USB-TO-TTL(CH340),4根公对母杜邦线,2根跳线,2个10uF的电容,2个33pF的电容,1个10kΩ的电阻,1个4.7kΩ的电阻,以及一个1kΩ的电阻,最后再来一个发光二极管. - -> 或者直接用虚拟的板子吧(Part4配置虚拟51) - -![Core](./Image/Core.png) -![Bread](./Image/Bread.jpg) - -试试看按照电路图进行连线吧,LED正极接在VCC,负极接在P2_0,别忘了串联上电阻哦(这里两个电阻并联意思是100Ω和1kΩ都可以) - -### 配置sdcc - -在ubuntu上搭建51单片机的开发环境,会被windows稍微麻烦一些,但是只要坚持下去,把环境配好,你在linux能学到一些不一样的知识. - -配置sdcc会遇到很多小小的问题,让我们来一一解决 - -1. 进行解压 - - ```bash - mkdir ~/tmp - tar -xvjf sdcc-src-4.4.0.tar.bz2 -C ~/tmp - cd ~/tmp/sdcc-4.4.0 - ``` - -2. 运行./configure - - ```bash - ./configure - ``` - - 期间也许你会遇到各种各样的问题,不过没关系,借助强大的搜索软件和gpt,聪明的你一定会想出合理的解决方法,配置环境本身也是锻炼自身能力的一部分. - - 这里就简单列出一些常见问题吧,有问题可以谷歌,实在不行还可以带上你的日志,问一下你的牛马学长哦. - - ```bash - #一般来说,问题都会出现在输出内容的最后 - #就比如说 - configure:error: Cannot find required program bison - #这段报错指的是你可能没有安装bison这个库,简单装一下就行 - sudo apt update - sudo apt install bison - #################################### - #或者是这段报错 - configure: error: boost library not found (boost/graph/adjacency_list.hpp). - #你只需要输入 - sudo apt install libboost-all-dev - #################################### - #或者说是这个 - configure:error: Cannot find required program flex - #你只需要输入 - configure:error: Cannot find required program bison - #Sorry,黏贴错了,应该是这个 - sudo apt install flex - #################################### - #我碰到的唯一比较难办的就是这段报错 - failedfor device/lib/pic14 - #解决方法也很简单把输入的指令做一下更改就行 - ./configure --disable-pic14-port--disable-pic16-port - #反正只要报错,问gpt,问google都没问题 - ``` - -3. 当处理完configure的报错之后,就可以进行下一步的操作了 - - ```bash - sudo make install - ``` - - 这里吐嘈一下,这个安装是真的好慢 - -4. 最后,在验证一下安装是否成功 - - ```bash - sdcc --version - ``` - - 假如你输出的是这样的界面 - - ```bash - sdcc --version - SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/r800/ds390/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02 TD- 4.4.0 #14620 (Linux) - published under GNU General Public License (GPL) - ``` - - good job(吐嘈fcitx打不出gou xi这两个字,服了) - -5. 尝试编译 -先尝试随便建一个工程,就比如说main.c - - ```c - #include <8052.h> - - void delay100ms() //@11.0592MHz - { - unsigned char i, j; - - i = 180; - j = 73; - do - { - while (--j); - } while (--i); - } - - void main() - { - while(1) - { - P2_0 = !P2_0; - delay100ms(); - } - } - ``` - - 编写完成后,`Ctrl+Alt+T`召唤神龙,再cd到main.c的目录,输入 - - ```bash - sdcc -mmcs51 main.c - ``` - - 这个时候跳出了一大堆文件 - - ```bash - mayge@mayge:~/Desktop/tmp $ls - main.asm main.ihx main.lst main.mem main.rst - main.c main.lk main.map main.rel main.sym - ``` - - 接下了,我们需要使用的就是main.ihx文件 - -### 配置stcgal - -在上一节,我们获得了需要烧录的文件`main.ihx`,那我们怎么将其转移到我们的单片机中呢,我们就需要配置烧录工具,最简单的就是stcgal,不过它只能烧录stc系列的一些单片机,对于stm32这类单片机,就需要另外的解决方法了. - -1. 打开终端输入 - - ```bash - pip3 install stcgal - ``` - -2. 验证安装 - - ```bash - stcgal -h - ``` - -3. 然而在我们检查串口设备的时候,却出现了一些意外 - - ```bash - mayge@mayge:~/Desktop ls /dev/ttyUSB* - - ls: 无法访问 '/dev/ttyUSB*': 没有那个文件或目录 - ``` - - 这就奇怪了,明明我们已经接上了我们的串口驱动程序,可是为什么仍然检测不到呢? - - ```bash - mayge@mayge:~/Desktop/tmp $sudo dmesg | grep -i ch34 - - [ 3109.998639] usbcore: registered new interface driver ch341 - [ 3109.998653] usbserial: USB Serial support registered for ch341-uart - [ 3109.998663] ch341 1-2.1:1.0: ch341-uart converter detected - [ 3110.010320] usb 1-2.1: ch341-uart converter now attached to ttyUSB0 - [ 3110.591802] usb 1-2.1: usbfs: interface 0 claimed by ch341 while 'brltty' sets config #1 - [ 3110.595796] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0 - [ 3110.595811] ch341 1-2.1:1.0: device disconnected - [ 3658.565668] ch341 1-2.1:1.0: ch341-uart converter detected - [ 3658.575821] usb 1-2.1: ch341-uart converter now attached to ttyUSB0 - [ 3659.520808] usb 1-2.1: usbfs: interface 0 claimed by ch341 while 'brltty' sets config #1 - [ 3659.523783] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0 - [ 3659.523826] ch341 1-2.1:1.0: device disconnected - ``` - - 重新连接并查询ttyUSB0的历史连接状态,我们发现在接入的一瞬间USB0设备是成功连接上的,只是后来`brltty`这个服务打断了我们的连接. - - 查询到`brltty`这个服务是用来控制**盲文显示器**的,我又不瞎,直接送它进小黑屋 - - ```bash - #停止 brltty 服务 - sudo systemctl stop brltty - #禁用 brltty 服务 - sudo systemctl disable brltty - #这里重新插入CH340设备 - sudo dmesg | grep -i ch34 - ``` - - ```bash - mayge@mayge:~/Desktop/tmp $ls /dev/ttyUSB* - /dev/ttyUSB0 - ``` - - 这就非常成功了 - -4. 接下来我们进行烧写 - - ```bash - mayge@mayge:~/Desktop/tmp $stcgal -p /dev/ttyUSB0 -b 115200 main.ihx - ``` - - 不过很遗憾,又报错了 - - ```bash - Serial port error: [Errno 13] could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0' - ``` - - 但这次的报错处理起来还是很简单的,只需要给ttyUSB0赋一个较低的权限,我们的串口程序就能对其进行访问了 - - ```bash - sudo chmod 666 /dev/ttyUSB0 - ``` - - 再次输入烧写指令,插拔Vcc引脚,就能开始烧录了 - - ```bash - mayge@mayge:~/Desktop/tmp $stcgal -p /dev/ttyUSB0 -b 115200 main.ihx - - Waiting for MCU, please cycle power: done - Protocol detected: stc89 - Target model: - Name: STC89C52RC/LE52RC - Magic: F002 - Code flash: 8.0 KB - EEPROM flash: 6.0 KB - Target frequency: 11.030 MHz - Target BSL version: 6.6C - Target options: - cpu_6t_enabled=False - bsl_pindetect_enabled=False - eeprom_erase_enabled=False - clock_gain=high - ale_enabled=True - xram_enabled=True - watchdog_por_enabled=False - Loading flash: 118 bytes (Intel HEX) - Switching to 115200 baud: checking setting testing done - Erasing 2 blocks: done - Writing flash: 640 Bytes [00:00, 7497.76 Bytes/s] - Setting options: done - Disconnected! - ``` - - 这样我们就能看到单片机的现象啦 - -## Part2 进阶配置 - -### 配置EIDE - -想要在vscode中拥有一套完善的开发环境就少不了一些好用的工具,下面我来讲解一下怎么配置EIDE这款好用的工具 - -1. 第一步当然是下载`Enbedded IDE`这款插件了,动动你的小手指,在扩展里面下载一下吧 - -2. 然后我们开始配置我们的EIDE,此时你一定会发现,在vscode的左边,出现了一个小芯片的按钮,我们点一下它,在新跳出来的界面中点击`New Project`开启第一个项目,选择**内置项目模板**,再选择**89C52 SDCC QuickStart** 输入一个项目名称这里我就随便输入一个`test`吧. - -3. 选择切换到目录,此时进入跳转,再然后,我们什么都不需要更改,只需要点击`build`,项目就成功构建好了 - -4. 点击烧录,发现一个奇怪的报错 - - ```bash - 终端进程“/bin/bash '-c', 'python ./tools/stcflash.py -p /dev/ttyS0 "/home/mayge/Desktop/WorkSpace/lab2/test0/test/build/Debug/test.hex"'”启动失败(退出代码: 1)。 - ``` - - 原来是调用了ttyS0这个设备啊,很明显,我们的CH340接在了ttyUSB0这个端口上 - - 最简单的处理办法就是直接更改eide.json文件,找到这一行 - - ```json - "commandLine": "python ./tools/stcflash.py -p ${port} \"${hexFile}\"", - ``` - - 将其修改为 - - ```json - "commandLine": "python ./tools/stcflash.py -p /dev/ttyUSB0 \"${hexFile}\"", - ``` - - 或者更进一步,直接把之前更改权限的命令也加上,这样一劳永逸 - - ```bash - "commandLine": "sudo chmod 666 /dev/ttyUSB0 && python ./tools/stcflash.py -p /dev/ttyUSB0 \"${hexFile}\"" - ``` - - 这是点击烧录,就成功啦 - -- 小插曲:使用EIDE时还需要安装`.NET6`这个包,众所周知,微软的产品对linux一直不大友好,按照常规的安装方法,一定会出现一大堆问题,所以,这里着重强调一下安装过程: - ![NET6](./Image/NET6.png) - - 打开终端,输入 - - ```bash - sudo apt install dotnet-sdk-7.0 - ``` - - 验证安装: - - ```bash - mayge@mayge:~/Desktop/WorkSpace/lab2 $dotnet --version - 7.0.119 - ``` - - 这样就好了,其实也并不难,但是假设自己慢慢去找,一定会遇到很多没办法解决的问题,尤其是在`CSDN`这种平台上,很大可能遇到不正确的解决方法,还是建议使用[stackoverflow](https://stackoverflow.com/questions/73753672/a-fatal-error-occurred-the-folder-usr-share-dotnet-host-fxr-does-not-exist).各位新手同志们,别忘了点一点快照,学会优雅的读档哦 - -> EIDE的程序示例放在了./Demo文件中 - -### 串口调试工具 - -在硬件开发的工程中,并不能像在家用电脑上使用`printf`打印出当前状态来得轻松,我们这时候就需要借助串口这一工具,将数据传输到电脑上,这点非常重要,可以极大的方便我们进行调试. -这里我们需要一个usb转ttl的设备,并安装其对应的驱动程序,就比如说,我现在使用的是`CH340`首先我要去查看ubuntu中有没有其对应的驱动程序 - -```bash -mayge@mayge:~/Desktop/WorkSpace/lab2 $ls /lib/modules/$(uname -r)/kernel/drivers/usb/serial - -aircable.ko io_ti.ko navman.ko ti_usb_3410_5052.ko -ark3116.ko ipaq.ko omninet.ko upd78f0730.ko -belkin_sa.ko ipw.ko opticon.ko usb_debug.ko -ch341.ko ir-usb.ko option.ko usbserial.ko -cp210x.ko iuu_phoenix.ko oti6858.ko usb-serial-simple.ko -cyberjack.ko keyspan.ko pl2303.ko usb_wwan.ko -cypress_m8.ko keyspan_pda.ko qcaux.ko visor.ko -digi_acceleport.ko kl5kusb105.ko qcserial.ko whiteheat.ko -empeg.ko kobil_sct.ko quatech2.ko wishbone-serial.ko -f81232.ko mct_u232.ko safe_serial.ko xr_serial.ko -f81534.ko metro-usb.ko sierra.ko xsens_mt.ko -ftdi_sio.ko mos7720.ko spcp8x5.ko -garmin_gps.ko mos7840.ko ssu100.ko -io_edgeport.ko mxuport.ko symbolserial.ko -``` - -这里我们看到了ch341.ko这个文件就不需要额外的操作了 - -- 安装cutecom - -```bash -sudo apt install cutecom -cutecom -``` - -启动串口助手,去网上随便搞一点`stc89c52rc`的串口驱动程序体验一下吧 - -## Part3 小任务 - -接下来我们将会有一些小小的任务,很简单的哦 - -### 任务1 - -这里会有三份程序源码以及三个十六进制文件,你需要做的就是通过烧录之后的现象,给他们进行配对,很简单吧. - -### 任务2 - -看出来了吧,task1.c是一段呼吸灯的代码.不过他并不完善,他只能呼气不能吸气 -请在终端中使用指令copy一份`task1.c`,将其命名为`task3.c`,完善呼吸灯之后对其进行编译,烧录到单片机中 - -tips: -> 编译代码 sdcc -mmcs51 taskx.c -> 烧录代码 stcgal -p /dev/ttyUSB0 -b 115200 taskx.ihx - -> 任务文件放在./task文件夹中 -> 答题卡是./AnswerSheet.md - -## Part4 虚拟51单片机安装 - -很遗憾,我们没有那么多资源购置人手一块的芯片,大家也暂时不会深入研究硬件相关的内容 -假如大家不想要购置实体的板材的话,这里还有一款工具用来虚拟51单片机 -具体的程序以及安装说明放在了./emu目录下 diff --git a/Labs/lab3/Demo/inc/reg52.h b/Labs/lab3/Demo/inc/reg52.h deleted file mode 100644 index dc1e374..0000000 --- a/Labs/lab3/Demo/inc/reg52.h +++ /dev/null @@ -1,182 +0,0 @@ -/*-------------------------------------------------------------------------- -REG52.H - -Header file for generic 80C52 and 80C32 microcontroller. -Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc. -All rights reserved. ---------------------------------------------------------------------------*/ - -#ifndef __REG52_H__ -#define __REG52_H__ - -#ifndef __SDCC -#error "This header file must be used for SDCC compiler !" -#endif - -/* redefine some keywords */ - -// primitive type -typedef __sfr sfr; -typedef __sfr16 sfr16; -typedef __sfr32 sfr32; -typedef __sbit sbit; -typedef __bit bit; - -// keywords -#define interrupt __interrupt -#define using __using -#define _at_ __at -#define _priority_ -#define _task_ - -// storage type -#define reentrant __reentrant -#define compact -#define small __near -#define large __far -#define data __data -#define bdata -#define idata __idata -#define pdata __pdata -#define xdata __xdata -#define code __code - -/* BYTE Registers */ -__sfr __at(0x80) P0; -__sbit __at(0x80+0) P00; -__sbit __at(0x80+1) P01; -__sbit __at(0x80+2) P02; -__sbit __at(0x80+3) P03; -__sbit __at(0x80+4) P04; -__sbit __at(0x80+5) P05; -__sbit __at(0x80+6) P06; -__sbit __at(0x80+7) P07; - -__sfr __at(0x90) P1; -__sbit __at(0x90+0) P10; -__sbit __at(0x90+1) P11; -__sbit __at(0x90+2) P12; -__sbit __at(0x90+3) P13; -__sbit __at(0x90+4) P14; -__sbit __at(0x90+5) P15; -__sbit __at(0x90+6) P16; -__sbit __at(0x90+7) P17; - -__sfr __at(0xA0) P2; -__sbit __at(0xA0+0) P20; -__sbit __at(0xA0+1) P21; -__sbit __at(0xA0+2) P22; -__sbit __at(0xA0+3) P23; -__sbit __at(0xA0+4) P24; -__sbit __at(0xA0+5) P25; -__sbit __at(0xA0+6) P26; -__sbit __at(0xA0+7) P27; - -__sfr __at(0xB0) P3; -__sbit __at(0xB0+0) P30; -__sbit __at(0xB0+1) P31; -__sbit __at(0xB0+2) P32; -__sbit __at(0xB0+3) P33; -__sbit __at(0xB0+4) P34; -__sbit __at(0xB0+5) P35; -__sbit __at(0xB0+6) P36; -__sbit __at(0xB0+7) P37; - -__sfr __at(0xD0) PSW; -__sfr __at(0xE0) ACC; -__sfr __at(0xF0) B; -__sfr __at(0x81) SP; -__sfr __at(0x82) DPL; -__sfr __at(0x83) DPH; -__sfr __at(0x87) PCON; -__sfr __at(0x88) TCON; -__sfr __at(0x89) TMOD; -__sfr __at(0x8A) TL0; -__sfr __at(0x8B) TL1; -__sfr __at(0x8C) TH0; -__sfr __at(0x8D) TH1; -__sfr __at(0xA8) IE; -__sfr __at(0xB8) IP; -__sfr __at(0x98) SCON; -__sfr __at(0x99) SBUF; - -/* 8052 Extensions */ -__sfr __at(0xC8) T2CON; -__sfr __at(0xCA) RCAP2L; -__sfr __at(0xCB) RCAP2H; -__sfr __at(0xCC) TL2; -__sfr __at(0xCD) TH2; - - -/* BIT Registers */ -/* PSW */ -__sbit __at(0xd7) CY; -__sbit __at(0xd6) AC; -__sbit __at(0xd5) F0; -__sbit __at(0xd4) RS1; -__sbit __at(0xd3) RS0; -__sbit __at(0xd2) OV; -__sbit __at(0xd0) P; //8052 only - -/* TCON */ -__sbit __at(0x8f) TF1; -__sbit __at(0x8e) TR1; -__sbit __at(0x8d) TF0; -__sbit __at(0x8c) TR0; -__sbit __at(0x8b) IE1; -__sbit __at(0x8a) IT1; -__sbit __at(0x89) IE0; -__sbit __at(0x88) IT0; - -/* IE */ -__sbit __at(0xaf) EA; -__sbit __at(0xad) ET2; //8052 only -__sbit __at(0xac) ES; -__sbit __at(0xab) ET1; -__sbit __at(0xaa) EX1; -__sbit __at(0xa9) ET0; -__sbit __at(0xa8) EX0; - -/* IP */ -__sbit __at(0xbd) PT2; -__sbit __at(0xbc) PS; -__sbit __at(0xbb) PT1; -__sbit __at(0xba) PX1; -__sbit __at(0xb9) PT0; -__sbit __at(0xb8) PX0; - -/* P3 */ -__sbit __at(0xb7) RD; -__sbit __at(0xb6) WR; -__sbit __at(0xb5) T1; -__sbit __at(0xb4) T0; -__sbit __at(0xb3) INT1; -__sbit __at(0xb2) INT0; -__sbit __at(0xb1) TXD; -__sbit __at(0xb0) RXD; - -/* SCON */ -__sbit __at(0x9f) SM0; -__sbit __at(0x9e) SM1; -__sbit __at(0x9d) SM2; -__sbit __at(0x9c) REN; -__sbit __at(0x9b) TB8; -__sbit __at(0x9a) RB8; -__sbit __at(0x99) TI; -__sbit __at(0x98) RI; - -/* P1 */ -__sbit __at(0x91) T2EX; // 8052 only -__sbit __at(0x90) T2; // 8052 only - -/* T2CON */ -__sbit __at(0xcf) TF2; -__sbit __at(0xce) EXF2; -__sbit __at(0xcd) RCLK; -__sbit __at(0xcc) TCLK; -__sbit __at(0xcb) EXEN2; -__sbit __at(0xca) TR2; -__sbit __at(0xc9) C_T2; -__sbit __at(0xc8) CP_RL2; - -#endif diff --git a/Labs/lab3/Demo/src/main.c b/Labs/lab3/Demo/src/main.c deleted file mode 100644 index c43c96d..0000000 --- a/Labs/lab3/Demo/src/main.c +++ /dev/null @@ -1,39 +0,0 @@ -/************************************* - * - * 89c52 sdcc blink demo - * - * CPU: 89C52 - * FREQ: 12MHz - * PIN: P20 - * - * *********************************** -*/ - -#include -#include - -void delay_100ms(); //@12MHz - -void main() -{ - while (1) - { - P20 = !P20; - delay_100ms(); - } -} - -void delay_100ms() -{ - unsigned char i, j; - - i = 195; - j = 138; - - do - { - while (--j) - ; - } while (--i); -} - diff --git a/Labs/lab3/Demo/tools/stcflash.py b/Labs/lab3/Demo/tools/stcflash.py deleted file mode 100644 index 171647c..0000000 --- a/Labs/lab3/Demo/tools/stcflash.py +++ /dev/null @@ -1,1412 +0,0 @@ -#!/usr/bin/env python -#coding=utf-8 -# stcflash Copyright (C) 2013 laborer (laborer@126.com) - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - - -import time -import logging -import sys -import serial -import os.path -import binascii -import struct -import argparse - - -PROTOCOL_89 = "89" -PROTOCOL_12C5A = "12c5a" -PROTOCOL_12C52 = "12c52" -PROTOCOL_12Cx052 = "12cx052" -PROTOCOL_8 = "8" -PROTOCOL_15 = '15' - -PROTOSET_89 = [PROTOCOL_89] -PROTOSET_12 = [PROTOCOL_12C5A, PROTOCOL_12C52, PROTOCOL_12Cx052] -PROTOSET_12B = [PROTOCOL_12C52, PROTOCOL_12Cx052] -PROTOSET_8 = [PROTOCOL_8] -PROTOSET_15 = [PROTOCOL_15] -PROTOSET_PARITY = [PROTOCOL_12C5A, PROTOCOL_12C52] - -class Programmer: - def __init__(self, conn, protocol=None): - self.conn = conn - self.protocol = protocol - - self.conn.timeout = 0.05 - if self.protocol in PROTOSET_PARITY: - self.conn.parity = serial.PARITY_EVEN - else: - self.conn.parity = serial.PARITY_NONE - - self.chkmode = 0 - - def __conn_read(self, size): - buf = bytearray() - while len(buf) < size: - s = bytearray(self.conn.read(size - len(buf))) - buf += s - - logging.debug("recv: " + " ".join(["%02X" % i for i in s])) - - if len(s) == 0: - raise IOError() - - return list(buf) - - def __conn_write(self, s): - logging.debug("send: " + " ".join(["%02X" % i for i in s])) - - self.conn.write(bytearray(s)) - - def __conn_baudrate(self, baud, flush=True): - logging.debug("baud: %d" % baud) - - if flush: - if self.protocol not in PROTOSET_8 and self.protocol not in PROTOSET_15: - self.conn.flush() - time.sleep(0.2) - - self.conn.baudrate = baud - - def __model_database(self, model): - modelmap = {0xE0: ("12", 1, {(0x00, 0x1F): ("C54", ""), - (0x60, 0x7F): ("C54", "AD"), - (0x80, 0x9F): ("LE54", ""), - (0xE0, 0xFF): ("LE54", "AD"), - }), - 0xE1: ("12", 1, {(0x00, 0x1F): ("C52", ""), - (0x20, 0x3F): ("C52", "PWM"), - (0x60, 0x7F): ("C52", "AD"), - (0x80, 0x9F): ("LE52", ""), - (0xA0, 0xBF): ("LE52", "PWM"), - (0xE0, 0xFF): ("LE52", "AD"), - }), - 0xE2: ("11", 1, {(0x00, 0x1F): ("F", ""), - (0x20, 0x3F): ("F", "E"), - (0x70, 0x7F): ("F", ""), - (0x80, 0x9F): ("L", ""), - (0xA0, 0xBF): ("L", "E"), - (0xF0, 0xFF): ("L", ""), - }), - 0xE6: ("12", 1, {(0x00, 0x1F): ("C56", ""), - (0x60, 0x7F): ("C56", "AD"), - (0x80, 0x9F): ("LE56", ""), - (0xE0, 0xFF): ("LE56", "AD"), - }), - 0xD1: ("12", 2, {(0x20, 0x3F): ("C5A", "CCP"), - (0x40, 0x5F): ("C5A", "AD"), - (0x60, 0x7F): ("C5A", "S2"), - (0xA0, 0xBF): ("LE5A", "CCP"), - (0xC0, 0xDF): ("LE5A", "AD"), - (0xE0, 0xFF): ("LE5A", "S2"), - }), - 0xD2: ("10", 1, {(0x00, 0x0F): ("F", ""), - (0x60, 0x6F): ("F", "XE"), - (0x70, 0x7F): ("F", "X"), - (0xA0, 0xAF): ("L", ""), - (0xE0, 0xEF): ("L", "XE"), - (0xF0, 0xFF): ("L", "X"), - }), - 0xD3: ("11", 2, {(0x00, 0x1F): ("F", ""), - (0x40, 0x5F): ("F", "X"), - (0x60, 0x7F): ("F", "XE"), - (0xA0, 0xBF): ("L", ""), - (0xC0, 0xDF): ("L", "X"), - (0xE0, 0xFF): ("L", "XE"), - }), - 0xF0: ("89", 4, {(0x00, 0x10): ("C5", "RC"), - (0x20, 0x30): ("C5", "RC"), #STC90C5xRC - }), - 0xF1: ("89", 4, {(0x00, 0x10): ("C5", "RD+"), - (0x20, 0x30): ("C5", "RD+"), #STC90C5xRD+ - }), - 0xF2: ("12", 1, {(0x00, 0x0F): ("C", "052"), - (0x10, 0x1F): ("C", "052AD"), - (0x20, 0x2F): ("LE", "052"), - (0x30, 0x3F): ("LE", "052AD"), - }), - 0xF2A0: ("15W", 1, {(0xA0, 0xA5): ("1", ""), #STC15W1系列 - }), - 0xF400: ("15F", 8, {(0x00, 0x07): ("2K", "S2"), #STC15F2K系列 - }), - 0xF407: ("15F", 60, {(0x07, 0x08): ("2K", "S2"), #STC15F2K系列 - }), - 0xF408: ("15F", 61, {(0x08, 0x09): ("2K", "S2"), #STC15F2K系列 - }), - 0xF400: ("15F", 4, {(0x09, 0x0C): ("4", "AD"), #STC15FAD系列 - }), - 0xF410: ("15F", 8, {(0x10, 0x17): ("1K", "AS"), #STC15F1KAS系列 - }), - 0xF417: ("15F", 60, {(0x17, 0x18): ("1K", "AS"), #STC15F1KAS系列 - }), - 0xF418: ("15F", 61, {(0x18, 0x19): ("1K", "AS"), #STC15F1KAS系列 - }), - 0xF420: ("15F", 8, {(0x20, 0x27): ("1K", "S"), #STC15F1KS系列 - }), - 0xF427: ("15F", 60, {(0x27, 0x28): ("1K", "S"), #STC15F1KS系列 - }), - 0xF440: ("15F", 8, {(0x40, 0x47): ("1K", "S2"), #STC15F1KS2系列 - }), - 0xF447: ("15F", 60, {(0x47, 0x48): ("1K", "S2"), #STC15F1KS2系列 - }), - 0xF448: ("15F", 61, {(0x48, 0x49): ("1K", "S2"), #STC15F1KS2系列 - }), - 0xF44C: ("15F", 13, {(0x4C, 0x4D): ("4", "AD"), - }), - 0xF450: ("15F", 8, {(0x50, 0x57): ("1K", "AS"), #STC15F1KAS系列 - }), - 0xF457: ("15F", 60, {(0x57, 0x58): ("1K", "AS"), #STC15F1KAS系列 - }), - 0xF458: ("15F", 61, {(0x58, 0x59): ("1K", "AS"), #STC15F1KAS系列 - }), - 0xF460: ("15F", 8, {(0x60, 0x67): ("1K", "S"), #STC15F1KS系列 - }), - 0xF467: ("15F", 60, {(0x67, 0x68): ("1K", "S"), #STC15F1KS系列 - }), - 0xF468: ("15F", 61, {(0x68, 0x69): ("1K", "S"), #STC15F1KS系列 - }), - 0xF480: ("15L", 8, {(0x80, 0x87): ("2K", "S2"), #STC15L2KS2系列 - }), - 0xF487: ("15L", 60, {(0x87, 0x88): ("2K", "S2"), #STC15L2KS2系列 - }), - 0xF488: ("15L", 61, {(0x88, 0x89): ("2K", "S2"), #STC15L2KS2系列 - }), - 0xF489: ("15L", 5, {(0x89, 0x8C): ("4", "AD"), #STC15L4AD系列 - }), - 0xF490: ("15L", 8, {(0x90, 0x97): ("2K", "AS"), #STC15L2KAS系列 - }), - 0xF497: ("15L", 60, {(0x97, 0x98): ("2K", "AS"), #STC15L2KAS系列 - }), - 0xF498: ("15L", 61, {(0x98, 0x99): ("2K", "AS"), #STC15L2KAS系列 - }), - 0xF4A0: ("15L", 8, {(0xA0, 0xA7): ("2K", "S"), #STC15L2KS系列 - }), - 0xF4A7: ("15L", 60, {(0xA7, 0xA8): ("2K", "S"), #STC15L2KS系列 - }), - 0xF4A8: ("15L", 61, {(0xA8, 0xA9): ("2K", "S"), #STC15L2KS系列 - }), - 0xF4C0: ("15L", 8, {(0xC0, 0xC7): ("1K", "S2"), #STC15L1KS2系列 - }), - 0xF4C7: ("15L", 60, {(0xC7, 0xC8): ("1K", "S2"), #STC15L1KS2系列 - }), - 0xF4C8: ("15L", 61, {(0xC8, 0xC9): ("1K", "S2"), #STC15L1KS2系列 - }), - 0xF4CC: ("15L", 13, {(0xCC, 0xCD): ("4", "AD"), - }), - 0xF4D0: ("15L", 8, {(0xD0, 0xD7): ("1K", "AS"), #STC15L1KS2系列 - }), - 0xF4D7: ("15L", 60, {(0xD7, 0xD8): ("1K", "AS"), #STC15L1KS2系列 - }), - 0xF4D8: ("15L", 61, {(0xD8, 0xD9): ("1K", "AS"), #STC15L1KS2系列 - }), - 0xF4E0: ("15L", 8, {(0xE0, 0xE7): ("1K", "S"), #STC15L1KS系列 - }), - 0xF4E7: ("15L", 60, {(0xE7, 0xE8): ("1K", "S"), #STC15L1KS系列 - }), - 0xF4E8: ("15L", 61, {(0xE8, 0xE9): ("1K", "S"), #STC15L1KS系列 - }), - 0xF500: ("15W", 1, {(0x00, 0x04): ("1", "SW"), #STC15W1SW系列 - }), - 0xF507: ("15W", 1, {(0x07, 0x0B): ("1", "S"), #STC15W1S系列 - }), - 0xF510: ("15W", 1, {(0x10, 0x14): ("2", "S"), #STC15W2S系列 - }), - 0xF514: ("15W", 8, {(0x14, 0x17): ("1K", "S"), #STC15W1KS系列 - }), - 0xF518: ("15W", 4, {(0x18, 0x1A): ("4", "S"), #STC15W4S系列 - }), - 0xF51A: ("15W", 4, {(0x1A, 0x1C): ("4", "S"), #STC15W4S系列 - }), - 0xF51C: ("15W", 4, {(0x1C, 0x1F): ("4", "AS"), #STC15W4AS系列 - }), - 0xF51F: ("15W", 10, {(0x19, 0x20): ("4", "AS"), #STC15W4AS系列 - }), - 0xF520: ("15W", 12, {(0x20, 0x21): ("4", "AS"), #STC15W4AS系列 - }), - 0xF522: ("15W", 16, {(0x22, 0x23): ("4K", "S4"), #STC15W4KS4系列 - }), - 0xF523: ("15W",24, {(0x23, 0x24): ("4K", "S4"), #STC15W4KS4系列 - }), - 0xF524: ("15W", 32, {(0x24, 0x25): ("4K", "S4"), #STC15W4KS4系列 - }), - 0xF525: ("15W", 40, {(0x25, 0x26): ("4K", "S4"), #STC15W4KS4系列 - }), - 0xF526: ("15W", 48, {(0x26, 0x27): ("4K", "S4"), #STC15W4KS4系列 - }), - 0xF527: ("15W", 56, {(0x27, 0x28): ("4K", "S4"), #STC15W4KS4系列 - }), - 0xF529: ("15W", 1, {(0x29, 0x2B): ("4", "A4"), #STC15W4AS系列 - }), - 0xF52C: ("15W", 8, {(0x2C, 0x2E): ("1K", "PWM"), #STC15W1KPWM系列 - }), - 0xF52E: ("15W", 20, {(0x2E, 0x2F): ("1K", "S"), #STC15W1KS系列 - }), - 0xF52F: ("15W", 32, {(0x2F, 0x30): ("2K", "S2"), #STC15W2KS2系列 - }), - 0xF530: ("15W", 48, {(0x30, 0x31): ("2K", "S2"), #STC15W2KS2系列 - }), - 0xF531: ("15W", 32, {(0x31, 0x32): ("2K", "S2"), #STC15W2KS2系列 - }), - 0xF533: ("15W", 20, {(0x33, 0x34): ("1K", "S2"), #STC15W1KS2系列 - }), - 0xF534: ("15W", 32, {(0x34, 0x35): ("1K", "S2"), #STC15W1KS2系列 - }), - 0xF535: ("15W", 48, {(0x35, 0x36): ("1K", "S2"), #STC15W1KS2系列 - }), - 0xF544: ("15W", 5, {(0x44, 0x45): ("", "SW"), #STC15SW系列 - }), - 0xF554: ("15W", 5, {(0x54, 0x55): ("2", "S"), #STC15W2S系列 - }), - 0xF557: ("15W", 29, {(0x57, 0x58): ("1K", "S"), #STC15W1KS系列 - }), - 0xF55C: ("15W", 13, {(0x5C, 0x5D): ("4", "S"), #STC15W4S系列 - }), - 0xF568: ("15W", 58, {(0x68, 0x69): ("4K", "S4"), #STC15W4KS4系列 - }), - 0xF569: ("15W", 61, {(0x69, 0x6A): ("4K", "S4"), #STC15W4KS4系列 - }), - 0xF56C: ("15W", 58, {(0x6C, 0x6D): ("4K", "S4-Student"), #STC15W4KS4系列 - }), - 0xF57E: ("15U", 8, {(0x7E, 0x85): ("4K", "S4"), #STC15U4KS4系列 - }), - 0xF600: ("15H", 8, {(0x00, 0x08): ("4K", "S4"), #STC154K系列 - }), - 0xF620: ("8A", 8, {(0x20, 0x28): ("8K", "S4A12"), #STC8A8K系列 - }), - 0xF628: ("8A", 60, {(0x28, 0x29): ("8K", "S4A12"), #STC8A8K系列 - }), - 0xF630: ("8F", 8, {(0x30, 0x38): ("2K", "S4"), #STC8F2K系列 - }), - 0xF638: ("8F", 60, {(0x38, 0x39): ("2K", "S4"), #STC8F2K系列 - }), - 0xF640: ("8F", 8, {(0x40, 0x48): ("2K", "S2"), #STC8F2K系列 - }), - 0xF648: ("8F", 60, {(0x48, 0x49): ("2K", "S2"), #STC8F2K系列 - }), - 0xF650: ("8A", 8, {(0x50, 0x58): ("4K", "S2A12"), #STC8A4K系列 - }), - 0xF658: ("8A", 60, {(0x58, 0x59): ("4K", "S2A12"), #STC8A4K系列 - }), - 0xF660: ("8F", 2, {(0x60, 0x66): ("1K", "S2"), #STC8F1K系列 - }), - 0xF666: ("8F", 17, {(0x66, 0x67): ("1K", "S2"), #STC8F1K系列 - }), - 0xF670: ("8F", 2, {(0x70, 0x76): ("1K", ""), #STC8F1K系列 - }), - 0xF676: ("8F", 17, {(0x76, 0x77): ("1K", ""), #STC8F1K系列 - }), - 0xF700: ("8C", 2, {(0x00, 0x06): ("1K", ""), #STC8C系列 - }), - 0xF730: ("8H", 2, {(0x30, 0x36): ("1K", ""), #STC8H1K系列 - }), - 0xF736: ("8H", 17, {(0x36, 0x37): ("1K", ""), #STC8H1K系列 - }), - 0xF740: ("8H", 8, {(0x40, 0x42): ("3K", "S4"), #STC8H3K系列 - }), - 0xF742: ("8H", 60, {(0x42, 0x43): ("3K", "S4"), #STC8H3K系列 - }), - 0xF743: ("8H", 64, {(0x43, 0x44): ("3K", "S4"), #STC8H3K系列 - }), - 0xF748: ("8H", 16, {(0x48, 0x4A): ("3K", "S2"), #STC8H3K系列 - }), - 0xF74A: ("8H", 60, {(0x4A, 0x4B): ("3K", "S2"), #STC8H3K系列 - }), - 0xF74B: ("8H", 64, {(0x4B, 0x4C): ("3K", "S2"), #STC8H3K系列 - }), - 0xF750: ("8G", 2, {(0x50, 0x56): ("1K", "-20/16pin"), #STC8G1K系列 - }), - 0xF756: ("8G", 17, {(0x56, 0x57): ("1K", "-20/16pin"), #STC8G1K系列 - }), - 0xF760: ("8G", 16, {(0x60, 0x62): ("2K", "S4"), #STC8G2K系列 - }), - 0xF762: ("8G", 60, {(0x62, 0x63): ("2K", "S4"), #STC8G2K系列 - }), - 0xF763: ("8G", 64, {(0x63, 0x64): ("2K", "S4"), #STC8G2K系列 - }), - 0xF768: ("8G", 16, {(0x68, 0x6A): ("2K", "S2"), #STC8G2K系列 - }), - 0xF76A: ("8G", 60, {(0x6A, 0x6B): ("2K", "S2"), #STC8G2K系列 - }), - 0xF76B: ("8G", 64, {(0x6B, 0x6C): ("2K", "S2"), #STC8G2K系列 - }), - 0xF770: ("8G", 2, {(0x70, 0x76): ("1K", "T"), #STC8G2K系列 - }), - 0xF776: ("8G", 17, {(0x76, 0x77): ("1K", "T"), #STC8G2K系列 - }), - 0xF780: ("8H", 16, {(0x80, 0x82): ("8K", "U"), #STC8H8K系列 - }), - 0xF782: ("8H", 60, {(0x82, 0x83): ("8K", "U"), #STC8H8K系列 - }), - 0xF783: ("8H", 64, {(0x83, 0x84): ("8K", "U"), #STC8H8K系列 - }), - 0xF790: ("8G", 2, {(0x90, 0x96): ("1K", "A-8PIN"), #STC8G1K系列 - }), - 0xF796: ("8G", 17, {(0x96, 0x97): ("1K", "A-8PIN"), #STC8G1K系列 - }), - 0xF7A0: ("8G", 2, {(0xA0, 0xA6): ("1K", "-8PIN"), #STC8G1K系列 - }), - 0xF7A6: ("8G", 17, {(0xA6, 0xA7): ("1K", "-8PIN"), #STC8G1K系列 - }), - } - - iapmcu = ((0xD1, 0x3F), (0xD1, 0x5F), (0xD1, 0x7F), (0xF4, 0x4D), (0xF4, 0x99), (0xF4, 0xD9), (0xF5, 0x58), - (0xD2, 0x7E), (0xD2, 0xFE), (0xF4, 0x09), (0xF4, 0x59), (0xF4, 0xA9), (0xF4, 0xE9), (0xF5, 0x5D), - (0xD3, 0x5F), (0xD3, 0xDF), (0xF4, 0x19), (0xF4, 0x69), (0xF4, 0xC9), (0xF5, 0x45), (0xF5, 0x62), - (0xE2, 0x76), (0xE2, 0xF6), (0xF4, 0x49), (0xF4, 0x89), (0xF4, 0xCD), (0xF5, 0x55), (0xF5, 0x69), - (0xF5, 0x6A), (0xF5, 0x6D), - ) - - try: - model = tuple(model) - if self.model[0] in [0xF4, 0xF5, 0xF6, 0xF7]: - prefix, romratio, fixmap = modelmap[stc_type_map(model[0],model[1])] - elif self.model[0] == 0xF2 and self.model[1] in range(0xA0, 0xA6): - prefix, romratio, fixmap = modelmap[stc_type_map(model[0],model[1])] - self.protocol = PROTOCOL_15 - else: - prefix, romratio, fixmap = modelmap[model[0]] - - if model[0] in (0xF0, 0xF1) and 0x20 <= model[1] <= 0x30: - prefix = "90" - - for key, value in fixmap.items(): - if key[0] <= model[1] <= key[1]: - break - else: - raise KeyError() - - infix, postfix = value - - romsize = romratio * (model[1] - key[0]) - - try: - romsize = {(0xF0, 0x03): 13}[model] - except KeyError: - pass - - if model[0] in (0xF0, 0xF1): - romfix = str(model[1] - key[0]) - elif model[0] in (0xF2,): - romfix = str(romsize) - else: - romfix = "%02d" % romsize - - name = "IAP" if model in iapmcu else "STC" - name += prefix + infix + romfix + postfix - return (name, romsize) - - except KeyError: - return ("Unknown %02X %02X" % model, None) - - def recv(self, timeout = 1, start = [0x46, 0xB9, 0x68]): - timeout += time.time() - - while time.time() < timeout: - try: - if self.__conn_read(len(start)) == start: - break - except IOError: - continue - else: - logging.debug("recv(..): Timeout") - raise IOError() - - chksum = start[-1] - - s = self.__conn_read(2) - n = s[0] * 256 + s[1] - if n > 64: - logging.debug("recv(..): Incorrect packet size") - raise IOError() - chksum += sum(s) - - s = self.__conn_read(n - 3) - if s[n - 4] != 0x16: - logging.debug("recv(..): Missing terminal symbol") - raise IOError() - - chksum += sum(s[:-(1+self.chkmode)]) - if self.chkmode > 0 and chksum & 0xFF != s[-2]: - logging.debug("recv(..): Incorrect checksum[0]") - raise IOError() - elif self.chkmode > 1 and (chksum >> 8) & 0xFF != s[-3]: - logging.debug("recv(..): Incorrect checksum[1]") - raise IOError() - - return (s[0], s[1:-(1+self.chkmode)]) - def first_recv(self, timeout = 1, start = [0x46, 0xB9, 0x68]): - timeout += time.time() - - while time.time() < timeout: - try: - if self.__conn_read(len(start)) == start: - time.sleep(0.02) #加上20ms延时,增大接收成功率 - break - except IOError: - continue - else: - logging.debug("recv(..): Timeout") - raise IOError() - - chksum = start[-1] - - s = self.__conn_read(2) - n = s[0] * 256 + s[1] - if n > 64: - logging.debug("recv(..): Incorrect packet size") - raise IOError() - chksum += sum(s) - - s = self.__conn_read(n - 3) - if s[n - 4] != 0x16: - logging.debug("recv(..): Missing terminal symbol") - raise IOError() - - chksum += sum(s[:-(1+self.chkmode)]) - if self.chkmode > 0 and chksum & 0xFF != s[-2]: - logging.debug("recv(..): Incorrect checksum[0]") - raise IOError() - elif self.chkmode > 1 and (chksum >> 8) & 0xFF != s[-3]: - logging.debug("recv(..): Incorrect checksum[1]") - raise IOError() - - return (s[0], s[1:-(1+self.chkmode)]) - - def send(self, cmd, dat): - buf = [0x46, 0xB9, 0x6A] - - n = 1 + 2 + 1 + len(dat) + self.chkmode + 1 - buf += [n >> 8, n & 0xFF, cmd] - - buf += dat - - chksum = sum(buf[2:]) - if self.chkmode > 1: - buf += [(chksum >> 8) & 0xFF] - buf += [chksum & 0xFF, 0x16] - - self.__conn_write(buf) - - def detect(self): - - for i in range(500): - try: - if self.protocol in [PROTOCOL_89,PROTOCOL_12C52,PROTOCOL_12Cx052,PROTOCOL_12C5A]: - self.__conn_write([0x7F,0x7F]) - cmd, dat = self.first_recv(0.03, [0x68]) - else: - self.__conn_write([0x7F]) - cmd, dat = self.first_recv(0.03, [0x68]) - break - except IOError: - pass - else: - raise IOError() - - - self.info = dat[16:] - - self.version = "%d.%d%c" % (self.info[0] >> 4, - self.info[0] & 0x0F, - self.info[1]) - - self.model = self.info[3:5] - - self.name, self.romsize = self.__model_database(self.model) - - logging.info("Model ID: %02X %02X" % tuple(self.model)) - logging.info("Model name: %s" % self.name) - logging.info("ROM size: %s" % self.romsize) - - - if self.protocol is None: - try: - self.protocol = {0xF0: PROTOCOL_89, #STC89/90C5xRC - 0xF1: PROTOCOL_89, #STC89/90C5xRD+ - 0xF2: PROTOCOL_12Cx052, #STC12Cx052 - 0xD1: PROTOCOL_12C5A, #STC12C5Ax - 0xD2: PROTOCOL_12C5A, #STC10Fx - 0xE1: PROTOCOL_12C52, #STC12C52x - 0xE2: PROTOCOL_12C5A, #STC11Fx - 0xE6: PROTOCOL_12C52, #STC12C56x - 0xF4: PROTOCOL_15, #STC15系列 - 0xF5: PROTOCOL_15, #STC15系列 - 0xF6: PROTOCOL_8, #STC8系列 - 0xF7: PROTOCOL_8, #STC8系列 - }[self.model[0]] - except KeyError: - pass - - if self.protocol in PROTOSET_8: - self.fosc = (dat[0]*0x1000000 +dat[1]*0x10000+dat[2]*0x100) /1000000 - self.internal_vol = (dat[34]*256+dat[35]) - self.wakeup_fosc = (dat[22]*256+dat[23]) /1000 - self.test_year = str(hex(dat[36])).replace("0x",'') - self.test_month = str(hex(dat[37])).replace("0x",'') - self.test_day = str(hex(dat[38])).replace("0x",'') - self.version = "%d.%d.%d%c" % (self.info[0] >> 4, - self.info[0] & 0x0F, - self.info[5], - self.info[1]) - if dat[10] == 191: - self.det_low_vol = 2.2 - else: - self.det_low_vol = (191 - dat[10])*0.3 + 2.1 - - elif self.protocol in PROTOSET_15: - self.fosc = (dat[7]*0x1000000 +dat[8]*0x10000+dat[9]*0x100) /1000000 - self.wakeup_fosc = (dat[0]*256+dat[1]) /1000 - self.internal_vol = (dat[34]*256+dat[35]) - self.test_year = str(hex(dat[41])).replace("0x",'') - self.test_month = str(hex(dat[42])).replace("0x",'') - self.test_day = str(hex(dat[43])).replace("0x",'') - self.version = "%d.%d.%d%c" % (self.info[0] >> 4, - self.info[0] & 0x0F, - self.info[5], - self.info[1]) - - else: - self.fosc = (float(sum(dat[0:16:2]) * 256 + sum(dat[1:16:2])) / 8 - * self.conn.baudrate / 580974) - - if self.protocol in PROTOSET_PARITY or self.protocol in PROTOSET_8 or self.protocol in PROTOSET_15: - self.chkmode = 2 - self.conn.parity = serial.PARITY_EVEN - else: - self.chkmode = 1 - self.conn.parity = serial.PARITY_NONE - - if self.protocol is not None: - del self.info[-self.chkmode:] - - logging.info("Protocol ID: %s" % self.protocol) - logging.info("Checksum mode: %d" % self.chkmode) - logging.info("UART Parity: %s" - % {serial.PARITY_NONE: "NONE", - serial.PARITY_EVEN: "EVEN", - }[self.conn.parity]) - - for i in range(0, len(self.info), 16): - logging.info("Info string [%d]: %s" - % (i // 16, - " ".join(["%02X" % j for j in self.info[i:i+16]]))) - def print_info(self): - print("系统时钟频率: %.3fMHz" % self.fosc) - if self.protocol in PROTOSET_8: - print("掉电唤醒定时器频率: %.3fKHz" % self.wakeup_fosc) - print("内部参考电压: %d mV" %self.internal_vol) - print("低压检测电压: %.1f V" %self.det_low_vol) - print("内部安排测试时间: 20%s年%s月%s日" %(self.test_year,self.test_month,self.test_day)) - - if self.protocol in PROTOSET_15: - print("掉电唤醒定时器频率: %.3fKHz" % self.wakeup_fosc) - print("内部参考电压: %d mV" %self.internal_vol) - print("内部安排测试时间: 20%s年%s月%s日" %(self.test_year,self.test_month,self.test_day)) - - print("单片机型号: %s" % self.name) - print("固件版本号: %s" % self.version) - if self.romsize is not None: - print("程序空间: %dKB" % self.romsize) - - if self.protocol == PROTOCOL_89: - switches = [( 2, 0x80, "Reset stops "), - ( 2, 0x40, "Internal XRAM"), - ( 2, 0x20, "Normal ALE pin"), - ( 2, 0x10, "Full gain oscillator"), - ( 2, 0x08, "Not erase data EEPROM"), - ( 2, 0x04, "Download regardless of P1"), - ( 2, 0x01, "12T mode")] - - elif self.protocol == PROTOCOL_12C5A: - switches = [( 6, 0x40, "Disable reset2 low level detect"), - ( 6, 0x01, "Reset pin not use as I/O port"), - ( 7, 0x80, "Disable long power-on-reset latency"), - ( 7, 0x40, "Oscillator high gain"), - ( 7, 0x02, "External system clock source"), - ( 8, 0x20, "WDT disable after power-on-reset"), - ( 8, 0x04, "WDT count in idle mode"), - (10, 0x02, "Not erase data EEPROM"), - (10, 0x01, "Download regardless of P1")] - print(" WDT prescal: %d" % 2**((self.info[8] & 0x07) + 1)) - - elif self.protocol in PROTOSET_12B: - switches = [(8, 0x02, "Not erase data EEPROM")] - - else: - switches = [] - - for pos, bit, desc in switches: - print(" [%c] %s" % ("X" if self.info[pos] & bit else " ", desc)) - - def handshake(self): - baud0 = self.conn.baudrate - - if self.protocol in PROTOSET_8: - baud = 115200 #若没指定波特率,默认为115200 - if highbaud_pre != 115200: - baud = highbaud_pre - #支持460800以内的任意波特率 - #典型波特率:460800、230400、115200、57600、38400、28800、19200、14400、9600、4800 - if baud in range(460801): - #定时器1重载值计算微调,可能由于目标芯片的差异性需要微调 - if baud in [300000,350000]: - Timer1_value = int(65536.2 - float(24.0 * 1000000 / 4 / baud)) - else: - Timer1_value = int(65536.5 - float(24.0 * 1000000 / 4 / baud)) - - if self.fosc < 24.5 and self.fosc > 23.5: #24M - foc_value = 0x7B - elif self.fosc < 27.5 and self.fosc > 26.5: #27M - foc_value = 0xB0 - elif self.fosc < 22.7 and self.fosc > 21.7: #22.1184M - foc_value = 0x5A - elif self.fosc < 20.5 and self.fosc > 19.5: #20M - foc_value = 0x35 - elif self.fosc < 12.3 and self.fosc > 11.7: #12M - foc_value = 0x7B - elif self.fosc < 11.4 and self.fosc > 10.8: #11.0592M - foc_value = 0x5A - elif self.fosc < 18.8 and self.fosc > 18.0: #18.432M - foc_value = 0x1A - elif self.fosc < 6.3 and self.fosc > 5.7:#6M - foc_value = 0x12 - elif self.fosc < 5.9 and self.fosc > 5.0: #5.5296M - foc_value = 0x5A - else: - foc_value = 0x6B - - baudstr = [0x00, 0x00, Timer1_value >> 8, Timer1_value & 0xff, 0x01, foc_value, 0x81] - - self.send(0x01, baudstr ) - try: - cmd, dat = self.recv() - except Exception: - logging.info("Cannot use baudrate %d" % baud) - time.sleep(0.2) - self.conn.flushInput() - finally: - self.__conn_baudrate(baud0, False) - - logging.info("Change baudrate to %d" % baud) - self.__conn_baudrate(baud) - self.baudrate = baud - elif self.protocol in PROTOSET_15: - baud = 115200 #若没指定波特率,默认为115200 - if highbaud_pre != 115200: - baud = highbaud_pre - #支持460800以内的任意波特率 - #典型波特率:460800、230400、115200、57600、38400、28800、19200、14400、9600、4800 - if baud in range(460801): - #定时器1重载值计算微调,可能由于目标芯片的差异性需要微调 - if baud in [300000,350000]: - Timer1_value = int(65536.2 - float(22.1184 * 1000000 / 4 / baud)) - else: - Timer1_value = int(65536.5 - float(22.1184 * 1000000 / 4 / baud)) - - if self.fosc < 24.5 and self.fosc > 23.5: #24M - foc_value_1 = 0x40 - foc_value_2 = 0x9F - elif self.fosc < 27.5 and self.fosc > 26.5: #27M - foc_value_1 = 0x40 - foc_value_2 = 0xDC - elif self.fosc < 22.7 and self.fosc > 21.7: #22.1184M - foc_value_1 = 0x40 - foc_value_2 = 0x79 - elif self.fosc < 20.5 and self.fosc > 19.5: #20M - foc_value_1 = 0x40 - foc_value_2 = 0x4F - elif self.fosc < 12.3 and self.fosc > 11.7: #12M - foc_value_1 = 0x80 - foc_value_2 = 0xA2 - elif self.fosc < 11.4 and self.fosc > 10.8: #11.0592M - foc_value_1 = 0x80 - foc_value_2 = 0x7D - elif self.fosc < 18.8 and self.fosc > 18.0: #18.432M - foc_value_1 = 0x40 - foc_value_2 = 0x31 - elif self.fosc < 6.3 and self.fosc > 5.7:#6M - foc_value_1 = 0xC0 - foc_value_2 = 0x9f - elif self.fosc < 5.9 and self.fosc > 5.0: #5.5296M - foc_value_1 = 0xC0 - foc_value_2 = 0x7B - - baudstr = [0x6d, 0x40, Timer1_value >> 8, Timer1_value & 0xff, foc_value_1,foc_value_2, 0x81] - #baudstr = [0x6b, 0x40, 0xff,0xf4, 0x40,0x92, 0x81] - - self.send(0x01, baudstr ) - try: - cmd, dat = self.recv() - except Exception: - logging.info("Cannot use baudrate %d" % baud) - time.sleep(0.2) - self.conn.flushInput() - finally: - self.__conn_baudrate(baud0, False) - - logging.info("Change baudrate to %d" % baud) - self.__conn_baudrate(baud) - self.baudrate = baud - else: - for baud in [115200, 57600, 38400, 28800, 19200, - 14400, 9600, 4800, 2400, 1200]: - - t = self.fosc * 1000000 / baud / 32 - if self.protocol not in PROTOSET_89: - t *= 2 - - if abs(round(t) - t) / t > 0.03: - continue - - if self.protocol in PROTOSET_89: - tcfg = 0x10000 - int(t + 0.5) - else: - if t > 0xFF: - continue - tcfg = 0xC000 + 0x100 - int(t + 0.5) - - baudstr = [tcfg >> 8, - tcfg & 0xFF, - 0xFF - (tcfg >> 8), - min((256 - (tcfg & 0xFF)) * 2, 0xFE), - int(baud0 / 60)] - - logging.info("Test baudrate %d (accuracy %0.4f) using config %s" - % (baud, - abs(round(t) - t) / t, - " ".join(["%02X" % i for i in baudstr]))) - - if self.protocol in PROTOSET_89: - freqlist = (40, 20, 10, 5) - else: - freqlist = (30, 24, 20, 12, 6, 3, 2, 1) - - for twait in range(0, len(freqlist)): - if self.fosc > freqlist[twait]: - break - - logging.info("Waiting time config %02X" % (0x80 + twait)) - - self.send(0x8F, baudstr + [0x80 + twait]) - - try: - self.__conn_baudrate(baud) - cmd, dat = self.recv() - break - except Exception: - logging.info("Cannot use baudrate %d" % baud) - - time.sleep(0.2) - self.conn.flushInput() - finally: - self.__conn_baudrate(baud0, False) - - else: - raise IOError() - logging.info("Change baudrate to %d" % baud) - - self.send(0x8E, baudstr) - self.__conn_baudrate(baud) - self.baudrate = baud - - cmd, dat = self.recv() - - - def erase(self): - if self.protocol in PROTOSET_89: - self.send(0x84, [0x01, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33]) - cmd, dat = self.recv(10) - assert cmd == 0x80 - - elif self.protocol in PROTOSET_8 or self.protocol in PROTOSET_15: - self.send(0x05, [0x00, 0x00, 0x5A, 0xA5]) - cmd, dat = self.recv(10) - self.send(0x03, [0x00, 0x00, 0x5A, 0xA5]) - cmd, dat = self.recv(10) - for i in range(7): - dat[i] = hex(dat[i]) - dat[i] = str(dat[i]) - dat[i] = dat[i].replace("0x",'') - if len(dat[i]) == 1: - dat_value = list(dat[i]) - dat_value.insert(0, '0') - dat[i] = ''.join(dat_value) - serial_number = "" - for i in dat: - serial_number = serial_number +str(i) - self.serial_number = str(serial_number) - print("\r") - sys.stdout.write("芯片出厂序列号: ") - sys.stdout.write(self.serial_number.upper()) - sys.stdout.flush() - print("\r") - - else: - self.send(0x84, ([0x00, 0x00, self.romsize * 4, - 0x00, 0x00, self.romsize * 4] - + [0x00] * 12 - + [i for i in range(0x80, 0x0D, -1)])) - cmd, dat = self.recv(10) - if dat: - logging.info("Serial number: " - + " ".join(["%02X" % j for j in dat])) - - def flash(self, code): - code = list(code) + [0xff] * (511 - (len(code) - 1) % 512) - - for i in range(0, len(code), 128): - logging.info("Flash code region (%04X, %04X)" % (i, i + 127)) - - if self.protocol in PROTOSET_8 or self.protocol in PROTOSET_15: - flag_test = 1 - addr = [i >> 8, i & 0xFF, 0x5A, 0xA5] - if flag_test == 1: - self.send(0x22, addr + code[i:i+128]) - flag_test = 10 - else: - self.send(0x02, addr + code[i:i+128]) - else: - addr = [0, 0, i >> 8, i & 0xFF, 0, 128] - self.send(0x00, addr + code[i:i+128]) - cmd, dat = self.recv() - - #assert dat[0] == sum(code[i:i+128]) % 256 - - yield (i + 128.0) / len(code) - - def options(self, **kwargs): - erase_eeprom = kwargs.get("erase_eeprom", None) - - dat = [] - fosc = list(bytearray(struct.pack(">I", int(self.fosc * 1000000)))) - - if self.protocol == PROTOCOL_89: - if erase_eeprom is not None: - self.info[2] &= 0xF7 - self.info[2] |= 0x00 if erase_eeprom else 0x08 - dat = self.info[2:3] + [0xFF]*3 - - elif self.protocol == PROTOCOL_12C5A: - if erase_eeprom is not None: - self.info[10] &= 0xFD - self.info[10] |= 0x00 if erase_eeprom else 0x02 - dat = (self.info[6:9] + [0xFF]*5 + self.info[10:11] - + [0xFF]*6 + fosc) - - elif self.protocol in PROTOSET_12B: - if erase_eeprom is not None: - self.info[8] &= 0xFD - self.info[8] |= 0x00 if erase_eeprom else 0x02 - dat = (self.info[6:11] + fosc + self.info[12:16] + [0xFF]*4 - + self.info[8:9] + [0xFF]*7 + fosc + [0xFF]*3) - - elif erase_eeprom is not None: - logging.info("Modifying options is not supported for this target") - return False - - if dat: - self.send(0x8D, dat) - cmd, dat = self.recv() - - return True - - def terminate(self): - logging.info("Send termination command") - - if self.protocol in PROTOSET_8 or self.protocol in PROTOSET_15: - self.send(0xFF, []) - else: - self.send(0x82, []) - self.conn.flush() - time.sleep(0.2) - - def unknown_packet_1(self): - if self.protocol in PROTOSET_PARITY: - logging.info("Send unknown packet (50 00 00 36 01 ...)") - self.send(0x50, [0x00, 0x00, 0x36, 0x01] + self.model) - cmd, dat = self.recv() - assert cmd == 0x8F and not dat - - def unknown_packet_2(self): - if self.protocol not in PROTOSET_PARITY and self.protocol not in PROTOSET_8 and self.protocol not in PROTOSET_15: - for i in range(5): - logging.info("Send unknown packet (80 00 00 36 01 ...)") - self.send(0x80, [0x00, 0x00, 0x36, 0x01] + self.model) - cmd, dat = self.recv() - assert cmd == 0x80 and not dat - - def unknown_packet_3(self): - if self.protocol in PROTOSET_PARITY: - logging.info("Send unknown packet (69 00 00 36 01 ...)") - self.send(0x69, [0x00, 0x00, 0x36, 0x01] + self.model) - cmd, dat = self.recv() - assert cmd == 0x8D and not dat - - -def autoisp(conn, baud, magic): - if not magic: - return - - bak = conn.baudrate - conn.baudrate = baud - conn.write(bytearray(ord(i) for i in magic)) - conn.flush() - time.sleep(0.5) - conn.baudrate = bak - - -def program(prog, code, erase_eeprom=None): - sys.stdout.write("检测目标...") - sys.stdout.flush() - - prog.detect() - - print("完成") - - prog.print_info() - - if prog.protocol is None: - raise IOError("未知目标") - - if code is None: - return - - prog.unknown_packet_1() - - sys.stdout.write("切换至最高波特率: ") - sys.stdout.flush() - - prog.handshake() - - print("%d bps"% prog.baudrate) - - prog.unknown_packet_2() - - sys.stdout.write("开始擦除芯片...") - sys.stdout.flush() - - time_start = time.time() - - prog.erase() - - print("擦除完成") - - print("代码长度: %d bytes" % len(code)) - - - # print("Programming: ", end="", flush=True) - sys.stdout.write("正在下载用户代码...") - sys.stdout.flush() - - oldbar = 0 - for progress in prog.flash(code): - bar = int(progress * 25) - sys.stdout.write("#" * (bar - oldbar)) - sys.stdout.flush() - oldbar = bar - - print(" 完成") - - prog.unknown_packet_3() - - sys.stdout.write("设置选项...") - sys.stdout.flush() - - if prog.options(erase_eeprom=erase_eeprom): - print("设置完成") - else: - print("设置失败") - - prog.terminate() - time_end = time.time() - print("耗时: %.3fs"% (time_end-time_start)) - - -# Convert Intel HEX code to binary format -def hex2bin(code): - buf = bytearray() - base = 0 - line = 0 - - for rec in code.splitlines(): - # Calculate the line number of the current record - line += 1 - - try: - # bytes(...) is to support python<=2.6 - # bytearray(...) is to support python<=2.7 - n = bytearray(binascii.a2b_hex(bytes(rec[1:3])))[0] - dat = bytearray(binascii.a2b_hex(bytes(rec[1:n*2+11]))) - except: - raise Exception("Line %d: Invalid format" % line) - - if rec[0] != ord(":"): - raise Exception("Line %d: Missing start code \":\"" % line) - if sum(dat) & 0xFF != 0: - raise Exception("Line %d: Incorrect checksum" % line) - - if dat[3] == 0: # Data record - addr = base + (dat[1] << 8) + dat[2] - # Allocate memory space and fill it with 0xFF - buf[len(buf):] = [0xFF] * (addr + n - len(buf)) - # Copy data to the buffer - buf[addr:addr+n] = dat[4:-1] - - elif dat[3] == 1: # EOF record - if n != 0: - raise Exception("Line %d: Incorrect data length" % line) - - elif dat[3] == 2: # Extended segment address record - if n != 2: - raise Exception("Line %d: Incorrect data length" % line) - base = ((dat[4] << 8) + dat[5]) << 4 - - elif dat[3] == 4: # Extended linear address record - if n != 2: - raise Exception("Line %d: Incorrect data length" % line) - base = ((dat[4] << 8) + dat[5]) << 16 - - else: - raise Exception("Line %d: Unsupported record type" % line) - - return buf - -def stc_type_map(type, value): - if type == 0xF6: - if value in range(0x01,0x09): - return 0xF600 - elif value in range(0x21,0x29): - return 0xF620 - elif value == 0x29: - return 0xF628 - elif value in range(0x31,0x39): - return 0xF630 - elif value == 0x39: - return 0xF638 - elif value in range(0x41,0x49): - return 0xF640 - elif value == 0x49: - return 0xF648 - elif value in range(0x51,0x59): - return 0xF650 - elif value == 0x59: - return 0xF658 - elif value in range(0x61,0x67): - return 0xF660 - elif value == 0x67: - return 0xF666 - elif value in range(0x71,0x77): - return 0xF670 - elif value == 0x77: - return 0xF676 - if type == 0xF7: - if value in range(0x01,0x07): - return 0xF700 - elif value in range(0x31,0x37): - return 0xF730 - elif value == 0x37: - return 0xF736 - elif value in range(0x41,0x43): - return 0xF740 - elif value == 0x43: - return 0xF742 - elif value == 0x44: - return 0xF743 - elif value in range(0x49,0x4B): - return 0xF748 - elif value == 0x4B: - return 0xF74A - elif value == 0x4C: - return 0xF74B - elif value in range(0x51,0x57): - return 0xF750 - elif value == 0x57: - return 0xF756 - elif value in range(0x61,0x63): - return 0xF760 - elif value == 0x63: - return 0xF762 - elif value == 0x64: - return 0xF763 - elif value in range(0x69,0x6B): - return 0xF768 - elif value == 0x6B: - return 0xF76A - elif value == 0x6C: - return 0xF76B - elif value in range(0x71,0x77): - return 0xF770 - elif value == 0x77: - return 0xF776 - elif value in range(0x81,0x83): - return 0xF780 - elif value == 0x83: - return 0xF782 - elif value == 0x84: - return 0xF783 - elif value in range(0x91,0x97): - return 0xF790 - elif value == 0x97: - return 0xF796 - elif value in range(0xA1,0xA7): - return 0xF7A0 - elif value == 0xA7: - return 0xF7A6 - if type == 0xF4: - if value in range(0x01,0x08): - return 0xF400 - elif value == 0x08: - return 0xF407 - elif value == 0x09: - return 0xF408 - elif value in range(0x0A,0x0D): - return 0xF409 - elif value in range(0x11,0x18): - return 0xF410 - elif value == 0x18: - return 0xF417 - elif value == 0x19: - return 0xF418 - elif value in range(0x21,0x28): - return 0xF420 - elif value == 0x28: - return 0xF427 - elif value == 0x29: - return 0xF428 - elif value in range(0x41,0x48): - return 0xF440 - elif value == 0x48: - return 0xF447 - elif value == 0x49: - return 0xF448 - elif value == 0x4D: - return 0xF44C - elif value in range(0x51,0x57): - return 0xF450 - elif value == 0x58: - return 0xF457 - elif value == 0x59: - return 0xF458 - elif value in range(0x61,0x68): - return 0xF460 - elif value == 0x68: - return 0xF467 - elif value == 0x69: - return 0xF468 - elif value in range(0x81,0x88): - return 0xF480 - elif value == 0x88: - return 0xF487 - elif value == 0x89: - return 0xF488 - elif value in range(0x8A,0x8D): - return 0xF489 - elif value in range(0x91,0x98): - return 0xF490 - elif value == 0x98: - return 0xF497 - elif value == 0x99: - return 0xF498 - elif value in range(0xA1,0xA8): - return 0xF4A0 - elif value == 0xA8: - return 0xF4A7 - elif value == 0xA9: - return 0xF4A8 - elif value in range(0xC1,0xC8): - return 0xF4C0 - elif value == 0xC8: - return 0xF4C7 - elif value == 0xC9: - return 0xF4C8 - elif value == 0xCD: - return 0xF4CC - elif value in range(0xD1,0xD8): - return 0xF4D0 - elif value == 0xD8: - return 0xF4D7 - elif value == 0xD9: - return 0xF4D8 - elif value in range(0xE1,0xE8): - return 0xF4E0 - elif value == 0xE8: - return 0xF4E7 - elif value == 0xE9: - return 0xF4E8 - if type == 0xF5: - if value in range(0x01,0x05): - return 0xF500 - elif value in range(0x08,0x0C): - return 0xF507 - elif value in range(0x11,15): - return 0xF510 - elif value in range(0x15,0x18): - return 0xF514 - elif value in range(0x19,0x1B): - return 0xF518 - elif value in range(0x1B,0x1D): - return 0xF51A - elif value in range(0x1D,0x20): - return 0xF51C - elif value == 0x20: - return 0xF51F - elif value == 0x21: - return 0xF520 - elif value == 0x23: - return 0xF522 - elif value == 0x24: - return 0xF523 - elif value == 0x25: - return 0xF524 - elif value == 0x26: - return 0xF525 - elif value == 0x27: - return 0xF526 - elif value == 0x28: - return 0xF527 - elif value in range(0x2A,0x2C): - return 0xF529 - elif value in range(0x2D,0x2F): - return 0xF52C - elif value == 0x2F: - return 0xF52E - elif value == 0x30: - return 0xF52F - elif value == 0x31: - return 0xF530 - elif value == 0x32: - return 0xF531 - elif value == 0x34: - return 0xF533 - elif value == 0x35: - return 0xF534 - elif value == 0x36: - return 0xF535 - elif value == 0x45: - return 0xF544 - elif value == 0x55: - return 0xF554 - elif value == 0x58: - return 0xF557 - elif value == 0x5D: - return 0xF55C - elif value == 0x69: - return 0xF568 - elif value == 0x6A: - return 0xF569 - elif value == 0x6D: - return 0xF56C - elif value in range(0x7F,0x86): - return 0xF57E - if type == 0xF2: - if value in range(0xA0,0xA6): - return 0xF2A0 - - -def main(): - - if sys.platform == "win32": - port = "COM3" - elif sys.platform == "darwin": - port = "/dev/tty.usbserial" - else: - port = "/dev/ttyUSB0" - - parser = argparse.ArgumentParser( - description=("Stcflash, a command line programmer for " - + "STC 8051 microcontroller.\n" - + "https://github.com/laborer/stcflash")) - parser.add_argument("image", - help="code image (bin/hex)", - type=argparse.FileType("rb"), nargs='?') - parser.add_argument("-p", "--port", - help="serial port device (default: %s)" % port, - default=port) - parser.add_argument("-l", "--lowbaud", - help="initial baud rate (default: 2400)", - type=int, - default=2400) - parser.add_argument("-hb", "--highbaud", - help="initial baud rate (default: 115200)", - type=int, - default=115200) - parser.add_argument("-r", "--protocol", - help="protocol to use for programming", - choices=["89", "12c5a", "12c52", "12cx052", "8", "15", "auto"], - default="auto") - parser.add_argument("-a", "--aispbaud", - help="baud rate for AutoISP (default: 4800)", - type=int, - default=4800) - parser.add_argument("-m", "--aispmagic", - help="magic word for AutoISP") - parser.add_argument("-v", "--verbose", - help="be verbose", - default=0, - action="count") - parser.add_argument("-e", "--erase_eeprom", - help=("erase data eeprom during next download" - +"(experimental)"), - action="store_true") - parser.add_argument("-ne", "--not_erase_eeprom", - help=("do not erase data eeprom next download" - +"(experimental)"), - action="store_true") - - opts = parser.parse_args() - - opts.loglevel = (logging.CRITICAL, - logging.INFO, - logging.DEBUG)[min(2, opts.verbose)] - - opts.protocol = {'89': PROTOCOL_89, - '12c5a': PROTOCOL_12C5A, - '12c52': PROTOCOL_12C52, - '12cx052': PROTOCOL_12Cx052, - '8': PROTOCOL_8, - '15': PROTOCOL_15, - 'auto': None}[opts.protocol] - - if not opts.erase_eeprom and not opts.not_erase_eeprom: - opts.erase_eeprom = None - - logging.basicConfig(format=("%(levelname)s: " - + "[%(relativeCreated)d] " - + "%(message)s"), - level=opts.loglevel) - - if opts.image: - code = bytearray(opts.image.read()) - opts.image.close() - if os.path.splitext(opts.image.name)[1] in (".hex", ".ihx"): - code = hex2bin(code) - else: - code = None - - print("通信端口:%s 最低波特率:%d bps" % (opts.port, opts.lowbaud)) - - global highbaud_pre - highbaud_pre = opts.highbaud - with serial.Serial(port=opts.port, - baudrate=opts.lowbaud, - parity=serial.PARITY_NONE) as conn: - if opts.aispmagic: - autoisp(conn, opts.aispbaud, opts.aispmagic) - program(Programmer(conn, opts.protocol), code, opts.erase_eeprom) - - -if __name__ == "__main__": - main() diff --git a/Labs/lab3/Image/Bread.jpg b/Labs/lab3/Image/Bread.jpg deleted file mode 100755 index 6996e50..0000000 Binary files a/Labs/lab3/Image/Bread.jpg and /dev/null differ diff --git a/Labs/lab3/Image/Core.png b/Labs/lab3/Image/Core.png deleted file mode 100755 index 1332119..0000000 Binary files a/Labs/lab3/Image/Core.png and /dev/null differ diff --git a/Labs/lab3/Image/NET6.png b/Labs/lab3/Image/NET6.png deleted file mode 100755 index def1fa0..0000000 Binary files a/Labs/lab3/Image/NET6.png and /dev/null differ diff --git a/Labs/lab3/install/sdcc-src-4.4.0.tar.bz2 b/Labs/lab3/Lab3.tar.gz old mode 100755 new mode 100644 similarity index 71% rename from Labs/lab3/install/sdcc-src-4.4.0.tar.bz2 rename to Labs/lab3/Lab3.tar.gz index 9b664d5..70f0d56 Binary files a/Labs/lab3/install/sdcc-src-4.4.0.tar.bz2 and b/Labs/lab3/Lab3.tar.gz differ diff --git a/Labs/lab3/README.md b/Labs/lab3/README.md index 3991b82..5aceb3e 100644 --- a/Labs/lab3/README.md +++ b/Labs/lab3/README.md @@ -1,6 +1,7 @@ # Lab3-体验嵌入式 ## 快速安装 + part1基础配置的相关配置我已经写好了插件,不过还是建议一步一步配置下去,这样才能快速了解环境,下次换台电脑也能很快的配置好环境 > 安装文件放在./install文件夹中 @@ -45,6 +46,7 @@ $ls /dev/ttyUSB* ## 任务 ### 任务1 + 请移步到`./tasks`文件夹 将三个.ihx文件放置到emu8051文件夹中 即可执行文件emu的同目录下 @@ -144,6 +146,7 @@ tips: SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/r800/ds390/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02 TD- 4.4.0 #14620 (Linux) published under GNU General Public License (GPL) ``` + 那进行下一步吧 5. 尝试编译 @@ -302,7 +305,3 @@ tips: ``` 这样我们就能看到单片机的现象啦 - - - - diff --git a/Labs/lab3/emu8051/README.md b/Labs/lab3/emu8051/README.md deleted file mode 100644 index cb62801..0000000 --- a/Labs/lab3/emu8051/README.md +++ /dev/null @@ -1,25 +0,0 @@ -## 51虚拟工具安装介绍 -### 安装说明 -1. 解压emu8051-master.zip文件 -2. 调取终端,输入 - ```bash - sudo apt-get install libncurses5 libncurses5-dev - make - ``` - -### 使用说明 -1. 运行文件 - ```bash - ./emu - ``` - -2. 按键说明: - - v:切换显示窗口 - - l:烧录本目录下的十六进制文件 - - +/-:调节运行的频率 - - Q:退出 - - r:运行 - -3. 想要观察现象,单击v进行跳转直到看到P2这个数据框,需要观察的数据是P2的最低位,其中 "*" 表示高电平(5V),"_"表示低电平(0V) - -> [仓库链接](https://github.com/jarikomppa/emu8051) \ No newline at end of file diff --git a/Labs/lab3/emu8051/emu8051-master.zip b/Labs/lab3/emu8051/emu8051-master.zip deleted file mode 100644 index e3ddf2d..0000000 Binary files a/Labs/lab3/emu8051/emu8051-master.zip and /dev/null differ diff --git a/Labs/lab3/install/install.sh b/Labs/lab3/install/install.sh deleted file mode 100755 index 619abe0..0000000 --- a/Labs/lab3/install/install.sh +++ /dev/null @@ -1,134 +0,0 @@ -#!/bin/bash - -# 确保脚本在错误时退出,并启用调试模式 -set -e -set -x - -# 依赖项列表 -DEPENDENCIES=("sdcc" "python3-pip" "bison" "libboost-all-dev" "flex") - -# 检查依赖项是否已安装 -check_dependencies() { - missing=() - for cmd in "${DEPENDENCIES[@]}"; do - if ! dpkg -l | grep -q "$cmd"; then - missing+=("$cmd") - fi - done - - if [ ${#missing[@]} -ne 0 ]; then - echo "Missing dependencies: ${missing[*]}" - return 1 - fi - return 0 -} - -# 安装依赖项 -install_dependencies() { - echo "Installing missing dependencies..." - sudo apt update - sudo apt install -y bison libboost-all-dev flex sdcc python3-pip - - # 安装 stcgal - python3 -m pip install --user stcgal -} - -# 设置 pip 安装路径 -ensure_path() { - if ! echo "$PATH" | grep -q "$HOME/.local/bin"; then - export PATH="$HOME/.local/bin:$PATH" - echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc - source ~/.bashrc - fi -} - -# 主函数 -main() { - echo "Checking and installing dependencies if needed..." - # 检查并安装依赖项 - if ! check_dependencies; then - install_dependencies - else - echo "All dependencies are already installed." - fi - - echo "Ensuring ~/.local/bin is in PATH..." - # 确保 ~/.local/bin 在 PATH 中 - ensure_path - - echo "Verifying sdcc installation..." - # 验证 sdcc 安装 - sdcc --version - - # echo "Creating example program..." - # 编写示例程序 -# cat < main.c -# #include <8052.h> - -# void delay100ms() //@11.0592MHz -# { -# unsigned char i, j; - -# i = 180; -# j = 73; -# do -# { -# while (--j); -# } while (--i); -# } - -# void main() -# { -# while(1) -# { -# P2_0 = !P2_0; -# delay100ms(); -# } -# } -# EOF - - echo "Compiling example program..." - # 编译示例程序 - sdcc -mmcs51 main.c - - echo "Verifying stcgal installation..." - # 验证 stcgal 安装 - if ! command -v stcgal &> /dev/null; then - echo "stcgal could not be found" - exit 1 - fi - - echo "Stopping and disabling brltty service..." - # 停止并禁用 brltty 服务 - sudo systemctl stop brltty - sudo systemctl disable brltty - - echo "Checking serial devices..." - # 检查串口设备 - sudo dmesg | grep -i ch34 - - echo "Listing USB serial devices..." - ls /dev/ttyUSB* - - if [ ! -e /dev/ttyUSB0 ]; then - echo "/dev/ttyUSB0 not found. Exiting." - exit 1 - fi - - echo "Setting permissions for /dev/ttyUSB0..." - # 设置权限并烧录固件 - sudo chmod 666 /dev/ttyUSB0 - - echo "Flashing firmware..." - # 烧录固件 - stcgal -p /dev/ttyUSB0 -b 115200 main.ihx - - echo "Cleaning up compiled files..." - # 删除编译产生的文件 - rm -f main.asm main.cdb main.lst main.map main.mem main.rst main.sym main.rel main.lk - - echo "Done." -} - -# 执行主函数 -main diff --git a/Labs/lab3/install/main.c b/Labs/lab3/install/main.c deleted file mode 100755 index 9541454..0000000 --- a/Labs/lab3/install/main.c +++ /dev/null @@ -1,22 +0,0 @@ -#include <8052.h> - -void delay100ms() //@11.0592MHz -{ - unsigned char i, j; - - i = 180; - j = 73; - do - { - while (--j); - } while (--i); -} - -void main() -{ - while(1) - { - P2_0 = !P2_0; - delay100ms(); - } -} diff --git a/Labs/lab3/tasks/maomao.ihx b/Labs/lab3/tasks/maomao.ihx deleted file mode 100644 index 2535be9..0000000 --- a/Labs/lab3/tasks/maomao.ihx +++ /dev/null @@ -1,13 +0,0 @@ -:03000000020006F5 -:03005F0002000399 -:0300030002006296 -:20006200D2A012007A12007A12007A12007A12007AC2A012007A80E87F8A7EC3DFFEDEFCF9 -:01008200225B -:06003500E478FFF6D8FD9F -:200013007900E94400601B7A00900087780175A000E493F2A308B8000205A0D9F4DAF2750C -:02003300A0FF2C -:20003B007800E84400600A790175A000E4F309D8FC7800E84400600C7900900001E4F0A3C3 -:04005B00D8FCD9FAFA -:0D000600758107120083E58260030200038C -:040083007582002260 -:00000001FF diff --git a/Labs/lab3/tasks/miaomiao.ihx b/Labs/lab3/tasks/miaomiao.ihx deleted file mode 100644 index aebbdbc..0000000 --- a/Labs/lab3/tasks/miaomiao.ihx +++ /dev/null @@ -1,13 +0,0 @@ -:03000000020006F5 -:06005F0075080002000319 -:0300030002006593 -:20006500C2A0B2A07F00C3EF950850030F80F70508B2A07F00AD087E0074FFC39DFDE49EBD -:17008500FE8F037C00C3EB9DEC64808EF063F08095F050CE0F80DEDC -:06003500E478FFF6D8FD9F -:200013007900E94400601B7A009000A0780175A000E493F2A308B8000205A0D9F4DAF275F3 -:02003300A0FF2C -:20003B007800E84400600A790175A000E4F309D8FC7800E84400600C7900900001E4F0A3C3 -:04005B00D8FCD9FAFA -:0D00060075810812009CE582600302000372 -:04009C007582002247 -:00000001FF diff --git a/Labs/lab3/tasks/task0.c b/Labs/lab3/tasks/task0.c deleted file mode 100644 index 62ec912..0000000 --- a/Labs/lab3/tasks/task0.c +++ /dev/null @@ -1,35 +0,0 @@ -#include "8052.h" - -// 发光二极管正极接在Vcc(5V),负极接在P2_0引脚上 -// P2_0 = 1代表P2_0引脚输出5V电压,0代表接地 - -void delay_100ms(); - -void main() // @11.0592MHZ -{ - while(1) - { - P2_0 = 1; - delay_100ms(); - delay_100ms(); - delay_100ms(); - delay_100ms(); - delay_100ms(); - P2_0 = 0; - delay_100ms(); - } -} - -void delay_100ms() -{ - unsigned char i, j; - - i = 195; - j = 138; - - do - { - while (--j) - ; - } while (--i); -} \ No newline at end of file diff --git a/Labs/lab3/tasks/task1.c b/Labs/lab3/tasks/task1.c deleted file mode 100644 index 342e0ae..0000000 --- a/Labs/lab3/tasks/task1.c +++ /dev/null @@ -1,20 +0,0 @@ -#include "8052.h" - -#define LED P2_0 - -unsigned char cnt = 0; - -void main() -{ - LED = 0; - while(1) - { - unsigned char i = 0; - LED=!LED; - for(i = 0;i