Skip to content

Hardware serial sometimes doesn't send every data. #6642

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

Closed
1 task done
zekageri opened this issue Apr 27, 2022 · 70 comments
Closed
1 task done

Hardware serial sometimes doesn't send every data. #6642

zekageri opened this issue Apr 27, 2022 · 70 comments
Assignees
Labels
Area: Peripherals API Relates to peripheral's APIs. IDE: PlaformIO Issue relates to PlatformIO IDE Peripheral: UART Status: Solved Type: Question Only question

Comments

@zekageri
Copy link

Board

esp32-wrover-e

Device Description

Custom board, using ESP32-Wrover-E ( 16mb flash 8mb external ram )

Hardware Configuration

Ethernet -> ETH_LAN_8720 chip

RTC Module -> DS3231 chip ( i2c )

Transistor for restarting the device -> 2n2222 or similar. ( GPIO 12 )

Two hardware serial UART:

  • Debug Serial 0 : RX: GPIO 3 | TX: GPIO 1
  • Custom Modbus Serial 1 : RX: GPIO 34 | TX: GPIO 15

Version

latest master (checkout manually)

IDE Name

Platform IO

Operating System

Windows 10

Flash frequency

80Mhz

PSRAM enabled

yes

Upload speed

115200

Description

Serial1 sometimes does not send out all the bytes and i got a frame timeout in my communication because the modules that my esp communicates doesnt understand the broken message. Happens mostly on web page load or file upload via HTTP.

Communication runs on an available core in a separate task with 115200 baud rate, tries to write as soon as possible.

Sketch

#define MBUS_BAUD 115200
#define MBUS_RX 34  //   6.  PIN
#define MBUS_TX 15  //   23. PIN

void mBusSetup(){
    pinSetup();
    Serial1.begin(MBUS_BAUD,SERIAL_8N1,MBUS_RX,MBUS_TX);
}

void mSystem::pinSetup(){
    pinMode(MBUS_TX_EN, OUTPUT);
    digitalWrite(MBUS_TX_EN, LOW);
}

void mSystem::startWrite(){
    mBusLock();
    //Serial1.flush();
    digitalWrite(MBUS_TX_EN, HIGH);
}

void mSystem::endWrite(){
    Serial1.flush();          // does not help
    //delayMicroseconds(200); // Does not help. Dont want magic delays
    digitalWrite(MBUS_TX_EN, LOW); // TX_ENABLE pin pulled low too soon i assume.
    mBusUnLock();
}

void mSystem::rawWrite(uint8_t * data, int length){
    int crc16 = getCRC_16(data, length);
    startWrite();
    Serial1.write( data, length );
    Serial1.write( lowByte(crc16) );
    Serial1.write( highByte(crc16) );
    endWrite();
}

#define MBUS_DISCOVER_LISTEN_TIMEOUT 150 // Modules will respond back in maximum of 15ms.

void writeReadDevice(int index){
    // This function handles which device address to write and assmebles the writable data and calls
    // rawWrite. If rawWrite returns, this function waits for the response data.
    uint8_t exampleData[] = {0xFF, 0x03, 0x00, 0x06, 0x00, 0x03};
    uint8_t responseData[20]; // Maximum response data is 10-13.
    rawWrite(exampleData, sizeof(exampleData));

    // wait for the response data. The modules will respond back if the data is understandable.
    long startMs = millis();
    while( !Serial1.available() ){
        vTaskDelay(1);
        if( millis() - startMs >= MBUS_DISCOVER_LISTEN_TIMEOUT ){
            Serial.println("FRAME TIMEOUT!");
            return;
        }
    }

    int respDataCount = 0;
    while( Serial1.available() > 0 ){
        responseData[respDataCount] = Serial1.read();
        respDataCount++;
    }
}

void commLoop(){
    writeReadDevice(deviceIndex);
    deviceIndex++; // Used for communicating with different modules
    if( deviceIndex > deviceCounter ){deviceIndex = 0;}
}

void mBusLoopTask(void* parameter){
    mBusSetup();
    for(;;){
        commLoop();
        vTaskDelay(3); // The problem occours even faster if this delay is smaller.
    }
}

Debug Message

No debug message just broken data on Serial.

Other Steps to Reproduce

Try to write frequently on serial1.

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@zekageri zekageri added the Status: Awaiting triage Issue is waiting for triage label Apr 27, 2022
@VojtechBartoska VojtechBartoska added the Area: Peripherals API Relates to peripheral's APIs. label Apr 27, 2022
@SuGlider SuGlider self-assigned this Apr 27, 2022
@SuGlider SuGlider removed the Status: Awaiting triage Issue is waiting for triage label Apr 27, 2022
@SuGlider
Copy link
Collaborator

@zekageri

I see you are using PlatformIO. Is your build based on Arduino Core 1.0.6?
Latest PIO Arduino Core is still 1.0.6.

Would you mind testing it using Arduino IDE with latest Arduino Core (2.0.3-RC1)?

Thanks.

@zekageri
Copy link
Author

zekageri commented Apr 27, 2022

This is my pio ini file

[env:arduino-esp32]
platform    = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
board       = esp32dev
framework   = arduino

platform_packages =
   framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32#master

board_build.f_cpu   = 240000000L

upload_port     = COM8
upload_speed    = 921600
monitor_speed   = 115200
monitor_filters = esp32_exception_decoder

board_build.flash_size = 16MB
board_build.flash_mode = dio
board_build.partitions = ./hsh_Partition.csv
board_build.f_flash    = 80000000L

build_flags =   -DBOARD_HAS_PSRAM
                -mfix-esp32-psram-cache-issue
                -DCORE_DEBUG_LEVEL=0
                ;-D CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
                ;-D CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
              
extra_scripts = mklittlefs/replace_fs.py

@zekageri
Copy link
Author

I downloaded the new Arduino IDE 2.0.0 and i can only download the 1.0.6 espressif framework.

@VojtechBartoska VojtechBartoska added the IDE: PlaformIO Issue relates to PlatformIO IDE label Apr 28, 2022
@VojtechBartoska
Copy link
Contributor

Please check this guide: https://docs.espressif.com/projects/arduino-esp32/en/latest/installing.html

For using v2.0.3-rc1, you need to change your link in Preferences to Development release link.

@VojtechBartoska VojtechBartoska added the Resolution: Awaiting response Waiting for response of author label Apr 28, 2022
@zekageri
Copy link
Author

I know that but with PIO i'm already using the latest with this ini file:

platform    = https://github.com/platformio/platform-espressif32.git#feature/arduino-upstream
board       = esp32dev
framework   = arduino
platform_packages =
   framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32#master

@SuGlider
Copy link
Collaborator

@zekageri
Copy link
Author

Thank you, i will try this!

@zekageri
Copy link
Author

zekageri commented Apr 28, 2022

Wow @SuGlider i got a bunch of warning messages ( like really much ) and then a linker fail

c:/users/pc/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: final link failed: No such file or directory
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\arduino-esp32\firmware.elf] Error 1

I will try to delete platform and packages folder from .platformio and recompile

@zekageri
Copy link
Author

Okay, deleted cache, packages and platform folder from .platformio inside users. The sketch compiled, i did upload it to my esp32 but the problem presist. Serial.flush() does not wait for all bytes

@SuGlider
Copy link
Collaborator

I'm pretty sure that Serial.flush() only returns when all bytes are sent.
https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-uart.c#L319-L332

There was an issue related to MODBUS and it was fixed there. Fixed by #6133

Are you sure you are building with the latest Arduino Core version?

@zekageri
Copy link
Author

Yeah, its 4.1.0

image

@zekageri
Copy link
Author

This is really interesting. Is there something wrong with my code? I configured Serial2 now instead of the Serial1 and commented out the microsec delay and i rarely got frame timeout now.

If there is file system access or other mid heavy process on other tasks, i got one or two frame timeouts.

@zekageri
Copy link
Author

void mBusSetup(){
    pinSetup();
    Serial2.begin(MBUS_BAUD,SERIAL_8N1,MBUS_RX,MBUS_TX);
}

void mSystem::pinSetup(){
    pinMode(MBUS_TX_EN, OUTPUT);
    digitalWrite(MBUS_TX_EN, LOW);
}

void mSystem::startWrite(){
    digitalWrite(MBUS_TX_EN, HIGH);
}

void mSystem::endWrite(){
    Serial2.flush();
    //delayMicroseconds(400);
    digitalWrite(MBUS_TX_EN, LOW);
}

void mSystem::rawWrite(uint8_t * data, int length){
    int crc16 = getCRC_16(data, length);
    startWrite();
    Serial2.write( data, length );
    Serial2.write( lowByte(crc16) );
    Serial2.write( highByte(crc16) );
    endWrite();
}

#define MBUS_LISTEN_TIMEOUT 150 // Modules will respond back in maximum of 15ms.

void writeReadDevice(int index){
    // This function handles which device address to write and assmebles the writable data and calls
    // rawWrite. If rawWrite returns, this function waits for the response data.
    uint8_t exampleData[] = {0xFF, 0x03, 0x00, 0x06, 0x00, 0x03};
    uint8_t responseData[20]; // Maximum response data is 10-13.
    rawWrite(exampleData, sizeof(exampleData));

    // wait for the response data. 
    // The modules will respond back if the data is understandable.

    long startMs = millis();
    while( !Serial1.available() ){
        vTaskDelay(1);
        if( millis() - startMs >= MBUS_LISTEN_TIMEOUT ){
            Serial.println("FRAME TIMEOUT!");
            return;
        }
    }

    int respDataCount = 0;
    while( Serial1.available() > 0 ){
        responseData[respDataCount] = Serial1.read();
        respDataCount++;
    }
}

@vshymanskyy
Copy link

vshymanskyy commented Apr 29, 2022

Are you absolutely sure that the output data is broken?
I currently have issues reading the data with v2.0.0+, including v2.0.3-rc1.
My issue is that incoming data up to 120 bytes (UART_FIFO_LEN - margin?) gets buffered (Serial1.available() returns 0), but then everything arrives in a burst.

@SuGlider
Copy link
Collaborator

There is a new API for UART called onReceive(callbackFunc)

It works as a sort of ISR... When data arrives and there is short interval of time (as long as about 11 symbols in the current baudrate), it will call right away the callback function.

It may help...

@vshymanskyy
Copy link

vshymanskyy commented Apr 30, 2022

@SuGlider thank you. onReceive works for the default Serial, but doesn't work on the Serial1, which is initialized this way:

//Serial1.setRxBufferSize(512); // tried this, doesn't affect anything. data is always read in chunks of 120 bytes
Serial1.begin(9600, SERIAL_8N1, 2, 15);

On 1.0.6 i don't see such issues. Could it be an ESP-IDF issue?

UPD: ok, what I experience is similar to this: espressif/esp-idf#8369
But in my case RS485 is not involved. Ok, will get to this with the logic analyzer.

@SuGlider
Copy link
Collaborator

Actually it works for all Serial, Serial1 and Serial2.

Just use Serial1.onReceive()

@vshymanskyy
Copy link

Sure. But it looks like i have problems on the signal lavel. Sorry, it is probably unrelated to this issue.

@SuGlider
Copy link
Collaborator

void mBusSetup(){
    pinSetup();
    Serial2.begin(MBUS_BAUD,SERIAL_8N1,MBUS_RX,MBUS_TX);
}

void mSystem::pinSetup(){
    pinMode(MBUS_TX_EN, OUTPUT);
    digitalWrite(MBUS_TX_EN, LOW);
}

void mSystem::startWrite(){
    digitalWrite(MBUS_TX_EN, HIGH);
}

void mSystem::endWrite(){
    Serial2.flush();
    //delayMicroseconds(400);
    digitalWrite(MBUS_TX_EN, LOW);
}

void mSystem::rawWrite(uint8_t * data, int length){
    int crc16 = getCRC_16(data, length);
    startWrite();
    Serial2.write( data, length );
    Serial2.write( lowByte(crc16) );
    Serial2.write( highByte(crc16) );
    endWrite();
}

#define MBUS_LISTEN_TIMEOUT 150 // Modules will respond back in maximum of 15ms.

void writeReadDevice(int index){
    // This function handles which device address to write and assmebles the writable data and calls
    // rawWrite. If rawWrite returns, this function waits for the response data.
    uint8_t exampleData[] = {0xFF, 0x03, 0x00, 0x06, 0x00, 0x03};
    uint8_t responseData[20]; // Maximum response data is 10-13.
    rawWrite(exampleData, sizeof(exampleData));

    // wait for the response data. 
    // The modules will respond back if the data is understandable.

    long startMs = millis();
    while( !Serial1.available() ){
        vTaskDelay(1);
        if( millis() - startMs >= MBUS_LISTEN_TIMEOUT ){
            Serial.println("FRAME TIMEOUT!");
            return;
        }
    }

    int respDataCount = 0;
    while( Serial1.available() > 0 ){
        responseData[respDataCount] = Serial1.read();
        respDataCount++;
    }
}

It seems OK, if you are sending data through Serial2 and receiving through Serial1.

@SuGlider
Copy link
Collaborator

SuGlider commented May 1, 2022

Okay, deleted cache, packages and platform folder from .platformio inside users. The sketch compiled, i did upload it to my esp32 but the problem presist. Serial.flush() does not wait for all bytes

I just tested Serial.flush(). It only returns when FIFO is empty and all data has been sent out.
It is very easy to see it when baudrate is 300bps and we send a string with about 200 characters, associating turning on/off builtin LED before/after Serial.print().

@zekageri
Copy link
Author

zekageri commented May 2, 2022

Can it be high baud rate problem? ( 115200 ) Or can it be that other tasks interfere, and the pin is not going to low in time?

@zekageri
Copy link
Author

zekageri commented May 2, 2022

void mSystem::startWrite(){
    digitalWrite(MBUS_TX_EN, HIGH);
}

void mSystem::endWrite(){
    Serial1.flush();
    digitalWrite(MBUS_TX_EN, LOW);
}

void mSystem::rawWrite(uint8_t * data, int length){
    int crc16 = getCRC_16(data, length);
    startWrite();
    Serial1.write( data, length );
    Serial1.write( lowByte(crc16) );
    // Other tasks interrupts this task in here maybe??
    Serial1.write( highByte(crc16) );
    // OR here?
    endWrite();
}

@SuGlider
Copy link
Collaborator

SuGlider commented May 2, 2022

@zekageri - Just to make sure it is not bad configuration of the development environment, could you please update your PlatformIO version to the latest as described in https://piolabs.com/blog/news/platformio-oss-april-2022-updates.html

Thanks!

@zekageri
Copy link
Author

zekageri commented May 2, 2022

Thank you for your response.
I did update my environmnet.
Currently using espressif 4.2.0 and PlatformIO 5.2.5.

This results in a strange boot loop when using default_16MB.csv with esp-wrover-kit board.
The wrover kit's external ram and it's 16mb flash is absolutly necessary for me.
I can't go forward if this isnt solved.

I opened a new issue for this : Boot loop issue with 16mb csv

@SuGlider SuGlider moved this from Under investigation to In Progress in Arduino ESP32 Core Project Roadmap Jun 28, 2022
@zekageri
Copy link
Author

Thank you i will try it now.

@zekageri
Copy link
Author

I have got some compile errors with this:

src/utilities/modBus.cpp: In member function 'void mSystem::testSerialSetup()':
src/utilities/modBus.cpp:1506:27: error: 'UART_INTR_RXFIFO_FULL' was not declared in this scope
       .intr_enable_mask = UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT,
                           ^~~~~~~~~~~~~~~~~~~~~
src/utilities/modBus.cpp:1506:27: note: suggested alternative: 'UART_BUFFER_FULL'
       .intr_enable_mask = UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT,
                           ^~~~~~~~~~~~~~~~~~~~~
                           UART_BUFFER_FULL
src/utilities/modBus.cpp:1506:51: error: 'UART_INTR_RXFIFO_TOUT' was not declared in this scope
       .intr_enable_mask = UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT,
                                                   ^~~~~~~~~~~~~~~~~~~~~
src/utilities/modBus.cpp:1506:51: note: suggested alternative: 'UART_FIFO_OVF'
       .intr_enable_mask = UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT,
                                                   ^~~~~~~~~~~~~~~~~~~~~
                                                   UART_FIFO_OVF*** [.pio\build\esp-wrover-kit\src\utilities\modBus.cpp.o] Error 1
#include "driver/uart.h"

void mSystem::testSerialSetup(){
    uart_intr_config_t uart_intr = {
      .intr_enable_mask = UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT,
      .rxfifo_full_thresh = 112,
      .rx_timeout_thresh = 1,
      .txfifo_empty_intr_thresh = 10,
    };
    uart_intr_config((uart_port_t) 1, &uart_intr);
}

void mSystem::setup(){
    Serial1.begin(MBUS_BAUD,SERIAL_8N1,MBUS_RX,MBUS_TX);
    testSerialSetup();
}

By the way here is all my task initialization.
I try to isolate the modbus task from the rest.

void initTasks(){
    fileSys.init(1,2,FILE_SYS_STACK_SIZE);
    hsh_modbus.init(-1,4,MODBUS_SYS_STACK_SIZE);
    networkSys.init(1,2,NETWORK_SYS_STACK_SIZE);
    hshDisplay.init(1,2,DISPLAY_SYS_STACK_SIZE);
    hsh_Server.init(1,2,SERVER_SYS_STACK_SIZE);
    hsh_Performance.init(1,2,PERFORMANCE_SYS_STACK_SIZE);
    hsh_GeoSystem.init(1,2,GEO_SYS_STACK_SIZE);
    hsh_timeSystem.init(1,2,TIME_SYS_STACK_SIZE);
    zones.init(1,3,ZONES_SYS_STACK_SIZE);
}

@SuGlider
Copy link
Collaborator

@zekageri

This will work:

// Necessary include for testing the fix
#include "driver/uart.h"


void setup() {
  // for example, start Serial2 - UART2
  Serial2.begin(115200);

  // right after starting UART2, add this code:
  uart_intr_config_t uart_intr = {
      .intr_enable_mask = (0x1<<0) | (0x8<<0),  // UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT,
      .rx_timeout_thresh = 1,
      .txfifo_empty_intr_thresh = 10,
      .rxfifo_full_thresh = 112,
  };
  uart_intr_config((uart_port_t) 2, &uart_intr);  // Two is the UART number for Arduino Serial
}

Thanks!

@SuGlider
Copy link
Collaborator

You code shall be like this:

#include "driver/uart.h"

void mSystem::testSerialSetup(){
    uart_intr_config_t uart_intr = {
        .intr_enable_mask = (0x1<<0) | (0x8<<0),  // UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT,
        .rx_timeout_thresh = 1,
        .txfifo_empty_intr_thresh = 10,
        .rxfifo_full_thresh = 112,
    };
    uart_intr_config((uart_port_t) 1, &uart_intr);
}

void mSystem::setup(){
    Serial1.begin(MBUS_BAUD,SERIAL_8N1,MBUS_RX,MBUS_TX);
    testSerialSetup();
}

@zekageri
Copy link
Author

Thank you for the suggestions.
I will try that tomorrow.

@zekageri
Copy link
Author

zekageri commented Jun 29, 2022

As soon as i run this, i get FRAME_TIMEOUT and no communication after that at all.
I will debug it now.

@zekageri
Copy link
Author

Here is how my code relevant part looks like.

#include "driver/uart.h"

boolean mSystem::newWriteToBuffer(int index, uint16_t registerAddr){
    int byteCount       = modules[index]->writeNumBytes*2;
    int writeBufferSize = byteCount + 7;
    int respDataCount   = 0;
    boolean isSuccess   = false;

    uint8_t writeBuffer[writeBufferSize];
    uint8_t respBuffer[150];
    writeBuffer[0] = uint8_t(modules[index]->address);
    writeBuffer[1] = 0x11;
    writeBuffer[2] = highByte(registerAddr);
    writeBuffer[3] = lowByte(registerAddr);
    writeBuffer[4] = highByte(modules[index]->writeNumBytes);
    writeBuffer[5] = lowByte(modules[index]->writeNumBytes);
    writeBuffer[6] = uint8_t(byteCount);

    // put the buffer data into writeBuffer
    for(int i = 0; i < modules[index]->writeNumBytes; i++){
        writeBuffer[i*2+7] = highByte(modules[index]->writeBuffer[i]);
        writeBuffer[i*2+8] = lowByte(modules[index]->writeBuffer[i]);
    }
    rawWrite(writeBuffer,writeBufferSize);
    long startMS = millis();
    while ( !Serial1.available() ){
        //vTaskDelay(1);
        // READ_WRITE_TIMEOUT == 55ms
        if( !Serial1.available() && ( (millis() - startMS) > READ_WRITE_TIMEOUT) ){
            Serial.printf("FRAME TIMEOUT at millis: %d module id: %d\n",millis(),modules[index]->id);
            return false;
        }
    }
    while ( Serial1.available() ){
        respBuffer[respDataCount] = Serial1.read();
        respDataCount++;
    }
    //Serial1.flush();
    if( respBuffer[0] == uint8_t(modules[index]->address) ){
        uint8_t gotCRC_High = respBuffer[respDataCount-2];
        uint8_t gotCRC_Low  = respBuffer[respDataCount-1];
        uint16_t gotCRC     = (gotCRC_Low << 8) | gotCRC_High;
        uint16_t calcCRC    = getCRC_16(respBuffer,respDataCount-2);
        if( gotCRC == calcCRC ){
            if( respBuffer[1] == 0x90 || respBuffer[1] == 0x83 ){
                Serial.printf("\nNew write failed with code: %02X\n",respBuffer[2]);
                return false;
            }
            for(int i = 0; i < modules[index]->readNumBytes; i++){
                modules[index]->readBuffer[i] = (respBuffer[i*2+3] << 8) | respBuffer[i*2+4];
            }
            return true;
        }else{
            Serial.printf("new write CRC Mismatch. Got: %04X Calculated: %04X\n",gotCRC,calcCRC);
            return false;
        }
    }
    return false;
}

void mSystem::writeReadDevice(int index){
    if( modules[index] != 0 ){
        if( modules[index]->isActive ){

            if( modules[index]->onError ){
                if( millis() - modules[index]->wentToError >= 60000 ){//modules[index]->onErrorTryCounter >= 1000 ){
                    modules[index]->onError = false;
                }else{
                    return;
                }
            }

            uint16_t regWriteAddr = getRegWriteAddr(modules[index]->id);
            setThermosDefaultDisplay(index,regWriteAddr);
            if( !newWriteToBuffer(index,regWriteAddr) ){
                modules[index]->failCounter++;
                moduleCommError(index);
            }else{
                modules[index]->failCounter = 0;
                modules[index]->onError     = false;
                checkModuleFaultCode(index);
            }
        }
    }
}

void mSystem::testSerialSetup(){
    uart_intr_config_t uart_intr = {
        .intr_enable_mask = (0x1<<0) | (0x8<<0),  // UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT,
        .rx_timeout_thresh = 1,
        .txfifo_empty_intr_thresh = 10,
        .rxfifo_full_thresh = 112,
    };
    uart_intr_config((uart_port_t) 1, &uart_intr);
}

void mSystem::setup(){
    Serial1.begin(MBUS_BAUD,SERIAL_8N1,MBUS_RX,MBUS_TX);
    testSerialSetup();  // WITHOUT THIS SERIAL SETUP, IT IS WORKING.
}

void mSystem::loop(){
  writeReadDevice(deviceIndex);
  deviceIndex++;
  if( deviceIndex > deviceCounter ){ deviceIndex = 0; }
}

void mBusLoopTask(void* parameter){
    for(;;){
        hsh_modbus.loop();
        vTaskDelay(2);
    }
}

@VojtechBartoska VojtechBartoska removed this from the 2.0.4 milestone Jun 29, 2022
@SuGlider
Copy link
Collaborator

Please try this change:

void mSystem::testSerialSetup(){
    uart_intr_config_t uart_intr = {
        .intr_enable_mask = (0x1<<0) | (0x8<<0),  // UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT,
        .rx_timeout_thresh = 1,
        .txfifo_empty_intr_thresh = 10,
        .rxfifo_full_thresh = 1,
    };
    uart_intr_config((uart_port_t) 1, &uart_intr);
}

It will force the driver to read each byte, one by one very fast.

@zekageri
Copy link
Author

zekageri commented Jul 1, 2022

This causes CRC mismatches in every response

new write CRC Mismatch. Got: 1311 Calculated: 807E
new write CRC Mismatch. Got: 110B Calculated: FFFF
new write CRC Mismatch. Got: 943D Calculated: 97BC
new write CRC Mismatch. Got: C03D Calculated: 4C34
new write CRC Mismatch. Got: 94F1 Calculated: 79B2
new write CRC Mismatch. Got: C03D Calculated: 6FDA
new write CRC Mismatch. Got: 943D Calculated: 965C
new write CRC Mismatch. Got: C03D Calculated: E6A2
new write CRC Mismatch. Got: 943D Calculated: C9C7
new write CRC Mismatch. Got: 11C0 Calculated: FFFF
new write CRC Mismatch. Got: 943D Calculated: EECA
new write CRC Mismatch. Got: C03D Calculated: BF9E
Making config from sun times calculated 
new write CRC Mismatch. Got: 943D Calculated: 838A
new write CRC Mismatch. Got: 11C0 Calculated: FFFF
new write CRC Mismatch. Got: 0B3D Calculated: 53E9
new write CRC Mismatch. Got: 943D Calculated: 81E6
new write CRC Mismatch. Got: C03D Calculated: EEE3
new write CRC Mismatch. Got: 943D Calculated: 5DCA
new write CRC Mismatch. Got: 1101 Calculated: FFFF
new write CRC Mismatch. Got: 8CF1 Calculated: 522D
new write CRC Mismatch. Got: C0F1 Calculated: 3C9C
new write CRC Mismatch. Got: 0002 Calculated: 7CA5
new write CRC Mismatch. Got: F110 Calculated: 3021
FS - (copy config) Firmware version: 1.34

It is possible that i have to adjust my code in order for this to work.

@zekageri
Copy link
Author

With my workaround to put a bigger delay in the loop it was working.
Now i have upgraded to the latest update and these timeouts are back again with my workaround.
The ESP misses the packets...

PLATFORM: Espressif 32 (5.0.0) > Espressif ESP-WROVER-KIT
esp-wrover-kit (platform: espressif32; board: esp-wrover-kit; framework: arduino)

PACKAGES:
 - framework-arduinoespressif32 @ 3.20003.220626 (2.0.3)
 - tool-esptoolpy @ 1.30300.0 (3.3.0)
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3

@SuGlider
Copy link
Collaborator

Which version of Arduino Core were you using before updating to 2.0.3? Was that the 2.0.0?

@zekageri
Copy link
Author

I have got a platform update to 5.0.0.

I have so much Espressif 32 platform installed i don't know what i was used. Probably 4.1.0.

image
image

@zekageri
Copy link
Author

In my PIO ini i did not modified the framework or the platform.

[env:esp-wrover-kit]
platform = espressif32

board       = esp-wrover-kit
framework   = arduino

@zekageri
Copy link
Author

Can i jump between versions with this line in the PIO ini?

platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.3

If i modify the last version number

@SuGlider
Copy link
Collaborator

I'm not familiar with PIO... I think that maybe @Jason2866 can help on it.

@zekageri
Copy link
Author

I tested with

platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.3
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.1
platform_packages = framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.0

same in every case

@Jason2866
Copy link
Collaborator

Jason2866 commented Jul 22, 2022

Platformio actual latest release is 5.0 (which is Arduino core 2.0.3)
the way you did to tag the version is working and fetches the corresponding commit.
To use latest core 2.0.4 you can do
platform = https://github.com/platformio/platform-espressif32.git
since this branch (develop) already uses core 2.0.4

@zekageri
Copy link
Author

Thank you for the suggestion.

What is the difference beetween platform_packages, platform, and framework?

Framework should be the ESP IDF, but my default PIO configuration for the framework is arduino, but i have seen arduino_espressif too since Arduino framework is top on IDF.

@Jason2866
Copy link
Collaborator

Jason2866 commented Jul 25, 2022

platforms includes all needed toolchains, tools and the framework (here Arduino) since defined as wanted in platformio setup.
With platform_packages you can define (override) anything what is default defined in platforms
IDF and Arduino are two different frameworks. It does not count here that Arduino framwork is builded with IDF.

@zekageri
Copy link
Author

I see. Thank you for the explanation. I will investigate it, it might be a problem from my side.

@VojtechBartoska VojtechBartoska added the Resolution: Awaiting response Waiting for response of author label Jul 25, 2022
@VojtechBartoska
Copy link
Contributor

@zekageri Hi, can I consider this as solved?

@zekageri
Copy link
Author

Sorry. It seems the update solved the problem. Thank you.

Repository owner moved this from In Progress to Done in Arduino ESP32 Core Project Roadmap Aug 23, 2022
@VojtechBartoska VojtechBartoska added Status: Solved and removed Resolution: Awaiting response Waiting for response of author labels Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Peripherals API Relates to peripheral's APIs. IDE: PlaformIO Issue relates to PlatformIO IDE Peripheral: UART Status: Solved Type: Question Only question
Projects
Development

No branches or pull requests

5 participants