racerxdl / air105-uploader

AIR105 MCU program uploader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Usage and hardware setup

smavridis opened this issue · comments

Hello @racerxdl , a few days ago i stumbled on your project.
Just like you i would like to flash an Air105 from linux.

I am trying to upload an empty main() program just to verify flashing works.

I tried using your tool with the following command:

python air105-uploader/upload.py /dev/ttyUSB0 firmware.bin

I am connecting the 105 with its usb-c connection but i am getting the following error:

>>> No signature key. Ignoring firmware signature
>>> Port: /dev/ttyUSB0
>>> Firmware: 52 bytes
>>> Starting bootloader
Received length > 256: 58339

So its clear i am doing something wrong, and i think it is hardware-related.
The board i am using is this: https://www.aliexpress.com/i/1005004481513531.html (The main ic has the code Air105 - A0032212)
It is flashed with a firmware that blinks the RGB lights from the factory.
When 'Starting bootloader', the blinking stops momentarily so i guess it sends a reset pulse, but then the error is displayed and the blinking program resumes. The dip switch that switches between UART/USB mode is set to the ON state, as in that position the /dev/ttyUSB0 is enabled.

Maybe i might need an external usb to serial chip?
But i not aware of any connection diagram and have not been able to find anything online.

Do you have any clue what might be wrong, or any idea what to try next?

Thanks in advance.

Hi @smavridis , that's odd. It should work fine with the embedded USB-Serial converter.

It always give the same message? (I mean 58339 as size)

Can you try putting a line here? https://github.com/racerxdl/air105-uploader/blob/main/scpukit/proto.py#L142

        while True:
            self.port.read_all()
            self.port.write(b"\xF8"*64)
            cmd, data = self.receive_packet()

Just to see if there isnt any garbage coming from the device?

Hi @racerxdl

I added a print(self.port.read_all()) at line 142.
I also added print("Recieved data:{}".format(data)) in receive_packet line 207 to dump the received data.

Here is a trace:

>>> No signature key. Ignoring firmware signature
>>> Port: /dev/ttyUSB0
>>> Firmware: 52 bytes
>>> Starting bootloader
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
Received length > 256: 60395
Recieved data:b'\x02\xe4\xeb\xeb\xeb\xeb'

Also the data change, but i think i understand what happening.

I pulled out a usb logic analyser and probed a bit.

The firmware that is currently running is executing this lua script:
https://github.com/openLuat/LuatOS/blob/9b90812aa142cd40b4aaf87af929870105b28c10/demo/gpio/gpio/main.lua

So it periodically , twice every second, sends messages over the uart.

I believe this is what gets received and is misinterpreted, resulting it the error.

So the root issue is, why even though the cpu reboots it does not enter the bootloader, but continues on the existing firmware.
Could be timing issue or baud settings?

I tried to capture the signals when running upload.py, but for some reason the logic analyser stops ...
It might be browning-out as the reset in the reset circuit cuts power to the board?
Ill try again later in the day and keep you posted if i find anything else.
Let me know if you have any other ideas i can try,

Thanks for the help.

Can be timing / latency. The bootrom doesnt keep the state for a long time (so the script should send the right packets to program otherwise it would just timeout and boot to the normal program).

This is really odd, I tested on two different machines with brand new devboards, and its working fine. Just curious, do you have more than one of that board? Have you tried in other one?

Same problem here with latest ari105 soc firmware LuatOS-SoC@Air103 V0015.

The soc file can be downloaded from: https://gitee.com/openLuat/LuatOS/releases/download/v0007.air103.v0015/core_V0015.zip

@smavridis / @cjacker can you try changing timeout here to 0.1 intead of 1? https://github.com/racerxdl/air105-uploader/blob/main/scpukit/proto.py#L180

We changed that because an issue with MacOSX. But now that I retried here on linux, it seens that it do. If that fixes it, I will make some changes so it works on both OS.

@smavridis / @cjacker can you try changing timeout here to 0.1 intead of 1? https://github.com/racerxdl/air105-uploader/blob/main/scpukit/proto.py#L180

We changed that because an issue with MacOSX. But now that I retried here on linux, it seens that it do. If that fixes it, I will make some changes so it works on both OS.

Yep that was it, at least for me!

It now flashes:

``

Received ChipSN Packet
Boot Version: V1.3.0
ChipID: 50790400
ROM Version: Unknown
Series: S030 (MH1903S)
Serial: b'41313035513838540000000000001F82'
Initializing stage2
Erasing flash memory
Erasing flash from 00000000 to 00002000
Sending firmware
Writing @0x1001000
Upload finished
Resetting device
``

Thanks for everything @racerxdl & @cjacker

Just merged the changes after confirmed that works on MacOSX as well. @cjacker / @smavridis just in case, if you can test the code currently in main branch. After both tested, I will close the issue.

Tested the main branch, seems to flash just fine, so fine by me.

Tested, and confirm works for me.

But it can not program the app.bin of luatos, the error looks like:

$ python upload.py /dev/ttyUSB0 ~/AIR105/app.bin "0x01010000"
>>> No signature key. Ignoring firmware signature
>>> Port: /dev/ttyUSB0
>>> Firmware: 2825192 bytes
>>> Starting bootloader
>>> Received ChipSN Packet
>>>  Boot Version: V1.3.0
>>>  ChipID: 50790400
>>>  ROM Version: Unknown
>>>  Series: S030 (MH1903S)
>>>  Serial: b'4131303551383854000000000001460A'
>>> Initializing stage2
>>> Erasing flash memory
>>> Erasing flash from 00000000 to 002B3000
>>> Sending firmware
>>> Writing @0x1010000
Traceback (most recent call last):
  File "/home/cjacker/air105-uploader/upload.py", line 30, in <module>
    uploader.upload(firm)
  File "/home/cjacker/air105-uploader/scpukit/proto.py", line 81, in upload
    self.write_chunk(offset, chunk)
  File "/home/cjacker/air105-uploader/scpukit/proto.py", line 119, in write_chunk
    raise ValueError(f"Error sending chunk: {data}")
ValueError: Error sending chunk: b''

By the way, the 'upload.py':

if len(sys.argv) >= 4:
    offset = int(sys.argv[4], 16)

if len(sys.argv) >= 5:
    rsaKeyFile = sys.argv[5]

it should be

if len(sys.argv) >= 4:
    offset = int(sys.argv[3], 16)

if len(sys.argv) >= 5:
    rsaKeyFile = sys.argv[4]

the sys.argv index is wrong.

Too minor fix, not worth a pull request.

Just FYI, I converted a firmware library for mh1903s with GCC and Makefile support here:

https://github.com/cjacker/mh1903_firmware_library_gcc_makefile

Great @cjacker ! I also did a platform.io module for it: https://github.com/racerxdl/platformio-air105 and https://github.com/racerxdl/framework-megahunt

Which a simple example:

platformio.ini

[env:air105]
platform = https://github.com/racerxdl/platformio-air105
board = air105
framework = baremetal

main.cpp

#include <stdio.h>

extern "C" {
#include "air105.h"
#include "delay.h"
#include "sysc.h"
#include "uart.h"
}

int main(void) {
    SystemClock_Config_HSE();
    Delay_Init();
    USART_Init();
    SYSCTRL->CG_CTRL1 = SYSCTRL_APBPeriph_ALL;
    SYSCTRL->CG_CTRL2 = SYSCTRL_AHBPeriph_ALL;

    GPIO_InitTypeDef gpio;
    gpio.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15;
    gpio.GPIO_Mode = GPIO_Mode_Out_PP;
    gpio.GPIO_Remap = GPIO_Remap_1;
    GPIO_Init(GPIOD, &gpio);

    Delay_ms(2000);
    printf("USART Init\r\n");
    printf("CHIP ID: %04x\r\n", SYSCTRL->CHIP_ID);

    while (1) {
        GPIO_ResetBits(GPIOD, GPIO_Pin_14 | GPIO_Pin_15);
        printf("RESET \r\n");
        Delay_ms(1000);
        GPIO_SetBits(GPIOD, GPIO_Pin_14 | GPIO_Pin_15);
        printf("SET\r\n");
        Delay_ms(1000);
    }
}

It already integrates this uploader, so you can just use pio run command and it will build & upload. Still WIP, but already works :D

Thanks for your great work !