diff --git a/README.md b/README.md index a73ce74..9324782 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# NOTE- THIS REPO HAS MOVE TO https://github.com/OpenBekenIOT - - - # What is this? This repo is a fork of a Beken repo which can program BK7321 series devices over serial using the serial bootloader. +It is mostly used to flash our Tasmota replacement on BK7231T/BK7231N, for details see here: + +https://github.com/openshwprojects/OpenBK7231T_App + HID has been disabled so that you don't need to find the relevant libraries, to enable easy windows use. It has been modified to @@ -16,27 +16,76 @@ It has been modified to 3/ spiprogram has been added - this works on a Raspberry pi using it's native SPI - see SPIFlash.md and rpi3install.md -* pairs with https://github.com/btsimonh/tuya-iotos-embeded-sdk-wifi-ble-bk7231t +# [Youtube guide for flashing BK7231N with hid_download_py](https://www.youtube.com/watch?v=2e1SUQNMrgY&ab_channel=Elektrodacom) +You can also check our other videos related to flashing IoT devices. + +# Blog post about BK7231N flashing by Zorruno + +https://zorruno.com/2022/zemismart-ks-811-with-openbk7231n-openbeken/ + +# Detailed examples of flashing Beken chips + +Please see our detailed step by step guides for more information: +https://www.elektroda.com/rtvforum/topic3880540.html + +https://www.elektroda.com/rtvforum/topic3875654.html + +https://www.elektroda.com/rtvforum/topic3874289.html # Install for Debian/Ubuntu/Linux Mint ## Installation -``` +```shell $ apt install python3-hid python3-serial python3-tqdm $ python3 setup.py install --user ``` +Or use `requirements.txt` with `pip`: + +```shell +# If you use mkvenv, it will pick up the requirements.txt for you +$ mkvenv +Creating hid_download_py-master-uD7eej0e virtualenv +created virtual environment CPython3.10.8.final.0-64 in 86ms +<...> +Found a requirements.txt file. Install? [y/N]: Y +Collecting hid + Using cached hid-1.0.5-py3-none-any.whl +Collecting pyserial + Using cached pyserial-3.5-py2.py3-none-any.whl (90 kB) +Collecting tqdm + Using cached tqdm-4.64.1-py2.py3-none-any.whl (78 kB) +Installing collected packages: pyserial, hid, tqdm +Successfully installed hid-1.0.5 pyserial-3.5 tqdm-4.64.1 +# Or with regular `pip` +$ pip3 install -r requirements.txt +Collecting hid + Using cached hid-1.0.5-py3-none-any.whl +Collecting pyserial + Using cached pyserial-3.5-py2.py3-none-any.whl (90 kB) +Collecting tqdm + Using cached tqdm-4.64.1-py2.py3-none-any.whl (78 kB) +Installing collected packages: pyserial, hid, tqdm +Successfully installed hid-1.0.5 pyserial-3.5 tqdm-4.64.1 +``` + ## Windows -Create a python virtual environment +* Download hidapi from https://github.com/libusb/hidapi/releases, extrat it to somewhere in windows %PATH% +* Create a python virtual environment -run install.bat to install the needed packages +* run `install.bat` to install the needed packages ## SPI Usage +see SPIFlash.md for instructions on how to unbrick devices using a raspberry pi... + + +## HID Usage + (disabled in this repo) ``` @@ -78,10 +127,10 @@ optional arguments: -l LENGTH, --length LENGTH length to read, defaults to 0x1000 -b BAUDRATE, --baudrate BAUDRATE - burn uart baudrate, defaults to 1500000 + burn uart baudrate, defaults to 921600 -u, --unprotect unprotect flash first, used by BK7231N -r, --read read flash - -w, --write read flash + -w, --write write flash -p, --unpackage unPackage firmware ``` @@ -89,6 +138,7 @@ optional arguments: * For `BK7231N`, set download address to `0x0`, and **set** `-u` option. +* note that the default baud rate is 921600 - it connects first at 115200, then sends a command to change the baudrate. So if you get a connection, but then 'Set Baudrate Failed', it could be that your connections/uart are not capable of the default 921600 baud, so try a lower one. If you get 'write sector failed', this can also be a mis-communication, so lower the baud rate. Common 'faster' baud rates are 115200, 230400, 460800, 576000, 921600, 1500000. uartprogram's default used to be 1.5Mbit, but we reduced it to 921600 for better reliability - but this may still be too high for some USB devices & connections. ## examples: diff --git a/bkutils/boot_protocol.py b/bkutils/boot_protocol.py index 80ae480..fdd415d 100644 --- a/bkutils/boot_protocol.py +++ b/bkutils/boot_protocol.py @@ -403,6 +403,15 @@ def CheckRespond_WriteReg(buf, regAddr, val): return True if len(cBuf) <= len(buf) and cBuf == buf[:len(cBuf)] else False def CheckRespond_SetBaudRate(buf, baudrate, dly_ms): + # It seems like multiple people are affected by the baud rate reply + # containing two concatenated messages, with the one we need (baud rate reply) + # arriving second. Therefore ignore the unexpected-but-actually-expected + # message if it's there. + # https://github.com/OpenBekenIOT/hid_download_py/issues/3 + unexpected = bytearray([0x04, 0x0e, 0x05, 0x01, 0xe0, 0xfc, 0x01, 0x00]) + if buf[:len(unexpected)] == unexpected: + buf = buf[len(unexpected):] + print("caution: ignoring unexpected reply in SetBaudRate") cBuf =bytearray([0x04,0x0e,0x05,0x01,0xe0,0xfc,CMD_SetBaudRate,0,0,0,0,0]) cBuf[2]=3+1+4+1 cBuf[7]=(baudrate&0xff) diff --git a/bkutils/flash_list.py b/bkutils/flash_list.py index 7cd1383..3f50ac7 100644 --- a/bkutils/flash_list.py +++ b/bkutils/flash_list.py @@ -1,36 +1,62 @@ # encoding: utf8 FLASH_ID_XTX_25F08B=0x14405e # 芯天下flash,w+v:6.5s,e+w+v:11.5s -FLASH_ID_MXIC_25V8035F=0x1423c2 # 旺宏flash,w+v:8.2s,e+w+v:17.2s FLASH_ID_XTX_25F04B=0x13311c # 芯天下flash-4M +FLASH_ID_XTX_25F16B=0x15400b # XTX 16M +FLASH_ID_XTX_25F32B=0x0016400b # xtx 32M******暂时只用于脱机烧录器上7231的外挂flash +FLASH_ID_XTX_25Q64B=0x0017600b # xtx 64M******暂时只用于脱机烧录器上7231的外挂flash +FLASH_ID_XTX_25F64B=0x0017400b # xtx 64M******暂时只用于脱机烧录器上7231的外挂flash +FLASH_ID_XTX_25Q128B=0x0018600b # xtx 128M +FLASH_ID_XTX_25F128F=0x0018400b # xtx 128M + +FLASH_ID_GT25Q16B=0x1560C4 # 聚辰16M-Bits + +FLASH_ID_MXIC_25V8035F=0x1423c2 # 旺宏flash,w+v:8.2s,e+w+v:17.2s +FLASH_ID_MXIC_25V4035F=0x1323c2 # 旺宏flash,w+v:8.2s,e+w+v:17.2s +FLASH_ID_MXIC_25V1635F=0x1523c2 # 旺宏flash,w+v:8.2s,e+w+v:17.2s + FLASH_ID_GD_25D40=0x134051 # GD flash-4M,w+v:3.1s,e+w+v:5.1s FLASH_ID_GD_25D80=0x144051 # GD flash-8M,e+w+v=9.8s FLASH_ID_GD_1_25D80=0x1440C8 # GD flash-8M, +FLASH_ID_GD_25WD80E=0x1464C8 # GD flash-8M, +FLASH_ID_GD_25WQ64E=0x001765c8 +FLASH_ID_GD_25WQ32E=0x001665c8 +FLASH_ID_GD_25WQ16E=0x001565c8 +FLASH_ID_GD_25Q64=0x001740c8 +FLASH_ID_GD_25Q16B=0x001540c8 # GD 16M******暂时只用于脱机烧录器上7231的外挂flash +FLASH_ID_GD_25Q41B=0x1340c8 # GD flash-4M,w+v:3.1s,e+w+v:5.1s +FLASH_ID_GD_25Q41B_T=0x1364c8 # GD flash-4M,w+v:3.1s,e+w+v:5.1s +FLASH_ID_GD_25LQ128E=0x1860c8 # media +FLASH_ID_GD_25WQ128E=0x1865c8 # media +FLASH_ID_GD25LX256E=0x1968C8 + +FLASH_ID_Puya_25Q16HB_K=0x152085 +FLASH_ID_Puya_25Q40=0x136085 # puya 4M,e+w+v:6s,新版w+v=4s,e+w+v=4.3s 普冉 +FLASH_ID_Puya_25Q64H=0x176085 FLASH_ID_Puya_25Q80=0x146085 # puya 8M,w+v:10.4s,e+w+v:11.3s,新版e+w+v:8.3s -FLASH_ID_Puya_25Q40=0x136085 # puya 4M,e+w+v:6s,新版w+v=4s,e+w+v=4.3s +FLASH_ID_Puya_25Q80_38=0x154285 # puya 16M FLASH_ID_Puya_25Q32H=0x166085 # puya 32M******暂时只用于脱机烧录器上7231的外挂flash -FLASH_ID_GD_25Q16=0x001540c8 # GD 16M******暂时只用于脱机烧录器上7231的外挂flash -FLASH_ID_GD_25Q16B=0x001540c8 # GD 16M******暂时只用于脱机烧录器上7231的外挂flash -FLASH_ID_XTX_25F16B=0x15400b # XTX 16M -FLASH_ID_XTX_25F32B =0x0016400b # xtx 32M******暂时只用于脱机烧录器上7231的外挂flash +FLASH_ID_Puya_25Q16SU=0x156085 # puya 16M +FLASH_ID_Puya_25Q128HA=0x182085 # puya 128M -FLASH_ID_MXIC_25V4035F=0x1323c2 # 旺宏flash,w+v:8.2s,e+w+v:17.2s -FLASH_ID_MXIC_25V1635F=0x1523c2 # 旺宏flash,w+v:8.2s,e+w+v:17.2s -FLASH_ID_GD_25Q41B=0x1340c8 # GD flash-4M,w+v:3.1s,e+w+v:5.1s FLASH_ID_BY_PN25Q80A=0x1440e0 # GD flash-4M,w+v:3.1s,e+w+v:5.1s FLASH_ID_BY_PN25Q40A=0x1340e0 # GD flash-4M,w+v:3.1s,e+w+v:5.1s -FLASH_ID_XTX_25Q64B =0x0017600b # xtx 64M******暂时只用于脱机烧录器上7231的外挂flash -FLASH_ID_XTX_25F64B =0x0017400b # xtx 64M******暂时只用于脱机烧录器上7231的外挂flash -FLASH_ID_Puya_25Q64H=0x00176085 -FLASH_ID_GD_25Q64=0x001740c8 FLASH_ID_WB_25Q128JV=0x001840ef + +FLASH_ID_DS_ZB25LQ128C=0x0018505e + FLASH_ID_ESMT_25QH16B=0x0015701c -FLASH_ID_GD_25WQ64E=0x001765c8 -FLASH_ID_GD_25WQ32E=0x001665c8 -FLASH_ID_GD_25WQ16E=0x001565c8 -FLASH_ID_TH25Q_16HB = 0x001560eb -FLASH_ID_NA = 0x001640c8 +FLASH_ID_ESMT_25QE32A=0x0016411c +FLASH_ID_ESMT_25QW32A=0x0016611c + +FLASH_ID_TH25Q_16HB=0x001560eb # EN25QH16B +FLASH_ID_TH25Q_80HB=0x001460cd +FLASH_ID_TH25D_40HB=0x001360cd + +FLASH_ID_XM25QU128C=0x00184120 + +FLASH_ID_NA=0x001640c8 # GD flash FLASH_ID_UNKNOWN=-1 @@ -56,34 +82,56 @@ def BFD(v,bs,bl): def BIT(n): return 1<> 16 send_buf[2] = (addr & 0xFF00) >> 8 send_buf[3] = addr & 0xFF - send_buf[4:4+256] = - self.spi.xfer(send_buf) + send_buf[4:4+256] = self.spi.xfer(send_buf) Wait_Busy_Down(self.spi) buf = f.read(256) diff --git a/bkutils/uart_downloader.py b/bkutils/uart_downloader.py index 06b82bb..943a4ef 100644 --- a/bkutils/uart_downloader.py +++ b/bkutils/uart_downloader.py @@ -150,7 +150,11 @@ def read(self, filename, startAddr=0x11000, length=0x119000): f.close() self.log("Wrote {:x} bytes to ".format(i) + filename) else: + f = open(filename, "wb") + f.write(fileBuf) + f.close() self.log("CRC check failed") + self.log("Wrote {:x} bytes to ".format(i) + filename) return @@ -200,7 +204,7 @@ def programm(self, filename, startAddr=0x11000): # time.sleep(0.01) self.log("Gotten Bus...") - time.sleep(0.01) + time.sleep(0.1) self.bootItf.Drain() # Step3: set baudrate, delay 100ms diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d17e1d6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +hid +pyserial +tqdm