Esp8266 returns unexpected byte

I use ESP-12F as master to update STM32 mcu through UART bootloader (built-in smt32).
To do it ESP sends specific byte-command. For example to activate bootloader we use 0x7F byte and is answer with 0x79 if Ok. Everything is ok and i can do it and read memory but is one moment.
Problem appears whet i try to activate bootloader second (and every even) run of 'start_bootloader' function. There appears 0xFF returned from stm32 but if we see to scope it is not true. This byte breaks the bootloader running until reload mcu.
No glitches i see (but i don't use oscil).



I try to put all files including datagram from 'Logic 2' app.
esp-rom.zip (712,5 КБ)
Serial-unexpected-FF.zip (40,8 КБ)

show routine which receive a byte and write "Received....."

int uart_receive_from_stm32(uint8_t* message, uint32_t length) {
    const uint32_t timeout = 100;
    uint32_t start_time;
    uint32_t i;
    uint32_t read_bytes;

    //clear buffer
    for (i = 0; i < length; i++) {
        *(message + i) = 0;
    }

    //Wait until incoming data
    i = 0;
    read_bytes = 0;
    start_time = millis();

    while (( (millis() - start_time) < timeout) && (read_bytes < length)) {
        if (STM32_UART.available()) {
            *(message + read_bytes++) = STM32_UART.read();
            start_time = millis();
        }
    }

    #if(DEBG==1)
    if (STM32_UART.available()) {
        debug.print("Was available bytes.\n");
    }

    //Print received
    debug.printf("Received %d bytes:\n", read_bytes);
    for (i = 0; i < read_bytes; i++) {
        debug.printf("%02X\n", *(message + i));
    }
    
    #endif

    return read_bytes;
}

can message be overwritten by second core of ESP?

ESP8266 on which based my esp12f has only one core. And no OS i use.

do you call uart_receive_from_stm32() from many places?

No. Only one tthread

so you have not need can place this code inline main loop without passing pointer

Thank for answering. Now i edit receive function so it has no input pointer and all data goes trough global variable. Has no affect, the same behavior.

how this part looks like now?

изображение
this picture show no error. 0x7F received, 0x79 answered.

int uart_receive_from_stm32_global(uint32_t length) {
    const uint32_t timeout = 100;
    uint32_t start_time;
    uint32_t i;
    uint32_t read_bytes;

    //clear buffer
    for (i = 0; i < length; i++) {
        message[i] = 0;
}

    //Wait until incoming data
    i = 0;
    read_bytes = 0;
    start_time = millis();

    while (((millis() - start_time) < timeout) && (read_bytes < length)) {
        if (STM32_UART.available()) {
            message[read_bytes++] = STM32_UART.read();
            start_time = millis();
        }
    }

#if(DEBG==1)
    if (STM32_UART.available()) {
        debug.print("Was available bytes.\n");
    }

    //Print received
    debug.printf("Received %d bytes:\n", read_bytes);
    for (i = 0; i < read_bytes; i++) {
        debug.printf("%02X\n", message[i]);
    }

#endif

    return read_bytes;
}

And variable defined in main file:
uint8_t message[256]

Unbeliebale how it works = ) I need a while

Unbelievable! how!? it works!
or what? what you want to say?

1 Like

Sorry dear. Unbelievable how can work my edited code (i see mistakes). I should go. See tomorrow

Yes. Scope show everything ok, but Serial of ESP returns 0xFF even faster than real answer.
7F -> FF (error)
7F -> 79 (read old answer. This moment STM bootloader already broken by twice sent 7F
00+FF -> 1F (STM rejects read command)

I put new version of the func. It does not change result.

Today i connect using Software Serial and everything works fine. Actually this was my plan to use software. But if anybody knows answer please type.

now show code from esp where Serial.read is

Please scroll up(post #3).

oh yes, of course it was. at some point I decided that this is a code from STM32. but now i have no more idea why ESP reads empty buffer and naturally get int -1 back. from 0xFFFF will only least 0xFF stored.
no idea except

if (STM32_UART.available()) {

to

if (STM32_UART.available()>0) {