Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload code ERROR #5

Open
ANH2018 opened this issue Aug 18, 2024 · 3 comments
Open

Upload code ERROR #5

ANH2018 opened this issue Aug 18, 2024 · 3 comments

Comments

@ANH2018
Copy link

ANH2018 commented Aug 18, 2024

Dear add.
I use PY32F003
Im upload code ok, firmware flash succesfully but MCU not work.
But i use keilc and use stlink upload ok and it work( blink led).
Please help me !
thanks

My code python:

import tkinter as tk
import subprocess
import time
import serial
from serial.tools.list_ports import comports

===================================================================================

Constants for puyaisp tool

PY_VID = '0403'
PY_PID = '6001'
PY_BAUD = 115200

===================================================================================

Programmer Class from your code

class Programmer(serial.Serial):
def init(self):
super().init(baudrate=PY_BAUD, parity=serial.PARITY_EVEN, timeout=1)
self.identify()

def identify(self):
    for p in comports():
        if (('PY_VID' not in globals()) or (PY_VID in p.hwid)) and (('PY_PID' not in globals()) or (PY_PID in p.hwid)):
            self.port = p.device
            try:
                self.open()
            except:
                continue
            self.boot()
            self.reset_input_buffer()
            self.write([0x7f])  # PY_SYNCH
            if not self.checkreply():
                self.close()
                continue
            return
    raise Exception('No MCU in boot mode found')

def sendcommand(self, command):
    self.write([command, command ^ 0xff])
    if not self.checkreply():
        raise Exception('Device has not acknowledged the command 0x%02x' % command)

def sendaddress(self, addr):
    stream = addr.to_bytes(4, byteorder='big')
    parity = 0x00
    for x in range(4):
        parity ^= stream[x]
    self.write(stream)
    self.write([parity])
    if not self.checkreply():
        raise Exception('Failed to send address')

def checkreply(self):
    reply = self.read(1)
    return (len(reply) == 1 and reply[0] == 0x79)  # PY_REPLY_ACK

def boot(self):
    self.rts = False
    self.dtr = False
    self.rts = True
    time.sleep(0.01)
    self.rts = False
    time.sleep(0.01)

def reset(self):
    self.dtr = True
    self.rts = True
    time.sleep(0.01)
    self.rts = False
    self.close()

def run(self):
    self.sendcommand(0x21)  # PY_CMD_GO
    self.sendaddress(0x08000000)  # PY_CODE_ADDR
    self.dtr = True
    self.close()

def readinfostream(self, command):
    self.sendcommand(command)
    size = self.read(1)[0]
    stream = self.read(size + 1)
    if not self.checkreply():
        raise Exception('Failed to read info')
    return stream

def readinfo(self):
    self.ver    = self.readinfostream(0x00)[0]  # PY_CMD_GET
    self.verstr = '%x.%x' % (self.ver >> 4, self.ver & 7)
    self.pid    = int.from_bytes(self.readinfostream(0x02), byteorder='big')  # PY_CMD_PID

def readuid(self):
    return self.readflash(0x1fff0e00, 128)  # PY_UID_ADDR

def readoption(self):
    try:
        self.option = list(self.readflash(0x1fff0e80, 16))  # PY_OPTION_ADDR
    except:
        raise Exception('Chip is locked')
    self.optionstr = 'OPTR: 0x%04x, SDKR: 0x%04x, WRPR: 0x%04x' % \
                     (( (self.option[ 0] << 8) + self.option[ 1], \
                        (self.option[ 4] << 8) + self.option[ 5], \
                        (self.option[12] << 8) + self.option[13] ))

def writeoption(self):
    self.writeflash(0x1fff0e80, self.option)  # PY_OPTION_ADDR

def resetoption(self):
    self.option = list(b'\xaa\xbe\x55\x41\xff\x00\x00\xff\xff\xff\xff\xff\xff\xff\x00\x00')

def lock(self):
    self.option[0]  = 0x55
    self.option[2]  = 0xaa

def nrst2gpio(self):
    self.option[1] |= 0x40
    self.option[3] &= 0xbf

def nrst2reset(self):
    self.option[1] &= 0xbf
    self.option[3] |= 0x40

def unlock(self):
    self.sendcommand(0x92)  # PY_CMD_R_UNLOCK
    if not self.checkreply():
        raise Exception('Failed to unlock chip')

def erase(self):
    self.sendcommand(0x44)  # PY_CMD_ERASE
    self.write(b'\xff\xff\x00')
    if not self.checkreply():
        raise Exception('Failed to erase chip')

def readflash(self, addr, size):
    data = bytes()
    while size > 0:
        blocksize = size
        if blocksize > 128: blocksize = 128
        self.sendcommand(0x11)  # PY_CMD_READ
        self.sendaddress(addr)
        self.sendcommand(blocksize - 1)
        data += self.read(blocksize)
        addr += blocksize
        size -= blocksize
    return data

def writeflash(self, addr, data):
    size = len(data)
    while size > 0:
        blocksize = size
        if blocksize > 128: blocksize = 128
        block = data[:blocksize]
        parity = blocksize - 1
        for x in range(blocksize):
            parity ^= block[x]
        self.sendcommand(0x31)  # PY_CMD_WRITE
        self.sendaddress(addr)
        self.write([blocksize - 1])
        self.write(block)
        self.write([parity])
        if not self.checkreply():
            raise Exception('Failed to write to address 0x%08x' % addr)
        data  = data[blocksize:]
        addr += blocksize
        size -= blocksize

def verifyflash(self, addr, data):
    flash = self.readflash(addr, len(data))
    if set(flash) != set(data):
        raise Exception('Verification failed')

===================================================================================

Flash Firmware Function

def flash_firmware():
try:
# Thực thi lệnh nạp firmware
subprocess.run(['puyaisp', '-f', 'Project.hex'], check=True)
status_label.config(text="Firmware flashed successfully!", fg="green")
except subprocess.CalledProcessError:
# Thông báo lỗi nếu việc nạp firmware thất bại
status_label.config(text="Failed to flash the program.", fg="red")
except Exception as e:
# Xử lý lỗi ngoài mong đợi
status_label.config(text=f"An unexpected error occurred: {str(e)}", fg="red")

===================================================================================

Create GUI

root = tk.Tk()
root.title("Flasher GUI")
root.geometry("300x120") # Kích thước cửa sổ

Tạo nút nạp firmware

flash_button = tk.Button(root, text="Flash Firmware", command=flash_firmware)
flash_button.pack(pady=20)

Tạo nhãn trạng thái

status_label = tk.Label(root, text="", font=("Arial", 12))
status_label.pack(pady=10)

Chạy GUI

root.mainloop()

@ANH2018
Copy link
Author

ANH2018 commented Aug 18, 2024

hello ad,
This is log:
SUCCESS: Connection established via COM9.
Getting chip info ...
SUCCESS: Found PY32F0xx with bootloader v1.0.
Reading OPTION bytes ...
SUCCESS: OPTR: 0xaabe, SDKR: 0xff00, WRPR: 0xffff.
Performing chip erase ...
SUCCESS: Chip is erased.
Flashing Project.hex to MCU ...
Verifying ...
SUCCESS: 4220 bytes written and verified.
DONE.

But not work !

@wagiminator
Copy link
Owner

Hi, puyaisp currently supports only binary files (.bin), NOT hex-files! That's why you uploaded only garbage. Please create a binary with your compiler and try again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@ANH2018 @wagiminator and others