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

ESP32 port tx/rx for sbus2 #21

Closed
magnum3131 opened this issue Jun 10, 2020 · 26 comments
Closed

ESP32 port tx/rx for sbus2 #21

magnum3131 opened this issue Jun 10, 2020 · 26 comments

Comments

@magnum3131
Copy link

Hi,

Thank you for your work, i tried to use your sbus2 telemetry on ESP32

Can you tell me on which port i have to connect the SBUS2 signal, tx0/rx0 ? other ?
Do i need a resistance between tx/rx ?

Thanks
Julien

@BrushlessPower
Copy link
Owner

Dear magnum,

sorry for the late reply, i was out of office for 2 weeks.

please look in "SBUS_usart_ESP32.cpp"

the is a define for the Pins. You can use almost every pin you want.

actual it's pin 25 and 26
#define SBUS2_UART_RX_PIN (GPIO_NUM_25)
#define SBUS2_UART_TX_PIN (GPIO_NUM_26)

you will need a 1k resistor between rx and tx. Connect the SBUS2 Cable to RX.

Keep in mind that this version is not very stable and could have a lot of bugs. That's the reason why i didn't write a "how to"

keep me up to Date

@magnum3131
Copy link
Author

Hi thanks a lot,

I found the information by parsing your code, i use a 2.4K resistor and it works.
Unfortunately i have stability issue. When i try to implement GPS with TinyGPS++ i have frequently the following error.

Guru Meditation Error: Core 1 panic'ed (Interrupt wdt timeout on CPU1)
Core 1 register dump:
PC : 0x40081089 PS : 0x00060434 A0 : 0x40085168 A1 : 0x3ffbe7b0
A2 : 0xe000c000 A3 : 0x3ffc03c4 A4 : 0x3ff50000 A5 : 0x3ffc0441
A6 : 0x00000000 A7 : 0x00000019 A8 : 0x80081078 A9 : 0x3ffbe790
A10 : 0x00008000 A11 : 0x00000001 A12 : 0x00000000 A13 : 0x00000000
A14 : 0x3ffbeee0 A15 : 0x00000000 SAR : 0x00000011 EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000
Core 1 was running in ISR context:
EPC1 : 0x40081068 EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x40081089

Backtrace: 0x40081089:0x3ffbe7b0 0x40085165:0x3ffbe7d0 0x40081065:0x3ffbc570 0x400d7853:0x3ffbc590 0x4008b26a:0x3ffbc5b0 0x40089ac9:0x3ffbc5d0

Core 0 register dump:
PC : 0x400f096e PS : 0x00060134 A0 : 0x800d7856 A1 : 0x3ffbbff0
A2 : 0x00000000 A3 : 0x00000001 A4 : 0x00000000 A5 : 0x00000001
A6 : 0x00060320 A7 : 0x00000000 A8 : 0x800d741e A9 : 0x3ffbbfc0
A10 : 0x00000000 A11 : 0x400858f0 A12 : 0x00060320 A13 : 0x3ffbb710
A14 : 0x3ff000e0 A15 : 0x00000001 SAR : 0x00000000 EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000

Backtrace: 0x400f096e:0x3ffbbff0 0x400d7853:0x3ffbc010 0x4008b26a:0x3ffbc030 0x40089ac9:0x3ffbc050

Rebooting...
ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:8896
load:0x40080400,len:5828
entry 0x400806ac

I try to find a solution

Thanks a lot for your work

@magnum3131
Copy link
Author

Hi,

I think i found the origin of my problem.

I change the TX/RX port for the following
#define SBUS2_UART_RX_PIN (GPIO_NUM_17)
#define SBUS2_UART_TX_PIN (GPIO_NUM_16)

But in your code you use pin 16 for " trigger PIN for logic analyser " this pin is use in

  • void disable_receiving()
  • static void IRAM_ATTR ISR_transmit_frame(void *arg)

Can i use an other free port for the trigger Pin ?

Thanks
Julien

@BrushlessPower
Copy link
Owner

You can use another port or delete the code with Pin16

It's just for debugging. With that i can connect an oscilloscope or logic analyzer to show me the correct timings.
My code has many lines for that reason, but most of them are commented out.

Maybe there is a Problem with TinyGPS or the Softserial
Please keep me up to Date

@magnum3131
Copy link
Author

Hi,

I do some more test, I have the problem with TinyGPS++ also in software or hardware serial 2.

According to the debugger the ESP32 crash here :
########
Error
########

Guru Meditation Error: Core 1 panic'ed (Interrupt wdt timeout on CPU1)
Core 1 register dump:
PC : 0x40085119 PS : 0x00050434 A0 : 0x4008105c A1 : 0x3ffbe7d0
A2 : 0x00000250 A3 : 0x3ffbc570 A4 : 0x000637ff A5 : 0x3ffbe7b0
A6 : 0x00000000 A7 : 0x3ffc03c4 A8 : 0x3ff50000 A9 : 0x3ffc0441
A10 : 0x00000000 A11 : 0x00000019 A12 : 0x800811be A13 : 0x3ffbe790
A14 : 0x00000004 A15 : 0x00000000 SAR : 0x0000001e EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000
Core 1 was running in ISR context:
EPC1 : 0x4008105c EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x40085119

Backtrace: 0x40085119:0x3ffbe7d0 0x40081059:0x3ffbc570 0x400d75bf:0x3ffbc590 0x4008b256:0x3ffbc5b0 0x40089ab5:0x3ffbc5d0

PC: 0x40085119
EXCVADDR: 0x00000000

Decoding stack results
0x40081059: ISR_transmit_frame(void) at C:\Users\Portable\Documents\Arduino\libraries\SBUS2\SBUS_usart_ESP32.cpp line 304* => delayMicroseconds(5);
0x400d75bf: esp_vApplicationIdleHook at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/freertos_hooks.c line 63
0x4008b256: prvIdleTask at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/tasks.c line 3382
0x40089ab5: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

it's always the same error, maybe the delay is in cause ?

Thanks

Julien

@BrushlessPower
Copy link
Owner

Hello Julien,

please delete the delayMicroseconds(5) from the code and check if it's working

Sbus telemetry should work without delay
Please keep me up to Date

@magnum3131
Copy link
Author

Hi,

I do some more test and i also have crash if i use the adafruit BME280 library.

It can work for 5 minutes and after crash every 20s and working again for 5min

According to the debugger it's crashing here
0x40080ff9: ISR_transmit_frame(void*) at C:\Users\Portable\Documents\Arduino\libraries\SBUS2\SBUS_usart_ESP32.cpp line 288
uart_write_bytes(UART_NUM_1, buffer, SLOT_DATA_LENGTH);

I'll continue my test

Thanks

Julien

@BrushlessPower
Copy link
Owner

Dear Julien,

ISR_transmit_frame(void*) is an function called by the Timer Interrupt.
I think there is a crash if the timer interrupt and another interrupt are running at the same time.

But this is just an idea.
ther could be 1000 other problems.

is the sbus telemetry working without other hardware/libraries?
are the other libraries working without sbus telemetry?

@magnum3131
Copy link
Author

Hi,

Every things is working fine separately, i'll try to debug by searching timer interrupt

Thanks a lot
Julien

@magnum3131
Copy link
Author

Hi,

More test today, still crash when SBUS2 function call to send telemetry data

  • uart_write_bytes(UART_NUM_1, buffer, SLOT_DATA_LENGTH);
  • uart_flush(UART_NUM_1); in enable receiving

I remark some difference between ESP32 lib et 328P

ESP 32
void SBUS2_uart_setup()
{
uint32_t counter = 64000;
uint8_t response = 0xAA;

328p arduino
void SBUS2_uart_setup ()
{
uint32_t counter = 640000;
uint8_t response = 0x00;

noInterrupts();

Are these difference normal ?

Thanks
Julien

@BrushlessPower
Copy link
Owner

yes this is normal

a big problem with this code is the USART Receive interrupt:
on328P the interrupt was called with every SBUS Byte
onESP32 the interrupt is called after a timeout after the last received byte -> that means that the interrupt i called once with every sbus frame

But in some cases the interrupt was called permanent
This could trigger a system crash and maybe its your problem.

But you can only check it with oscilloscpoe or logic analyser.

Possible fixes:

  • change timeout of USART Interrupt -> BrushlessPower/SBUS2-Telemetry-ESP32#1
  • change input buffer -> BrushlessPower/SBUS2-Telemetry-ESP32#1
  • set the interrupt functions to critical

Example:
void IRAM_ATTR onTime() {
portENTER_CRITICAL_ISR(&timerMux);
interrupts++;
portEXIT_CRITICAL_ISR(&timerMux);
}

@BrushlessPower
Copy link
Owner

or try to use a different Timer and/or different USART

@magnum3131
Copy link
Author

Hi,

Thanks for your answer, i played with these value but it still crashing
uart_intr.rxfifo_full_thresh = 1;
uart_intr.rx_timeout_thresh = 10;

For the interrupt functions critical, i read article and i found an exemple in Jeti Exbus telemetry

void IRAM_ATTR _onTimer() {
	portENTER_CRITICAL_ISR(&_timerMux);
	timerAlarmDisable(_timer);
	_bTimerRunning = false;
	Esp32Serial.uartDetachTx();
	// digitalWrite(15, false);
	portEXIT_CRITICAL_ISR(&_timerMux);
}
size_t JetiExBusESP32Serial::write(const uint8_t *buffer, size_t size)
{
	if( !_bTimerRunning )
	{
		**portENTER_CRITICAL(&_timerMux);**
		// https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/
		**_timer = timerBegin(0, 160, true);               // timer 0, prescaler 160 --> 80MHz/160 = 0,5 MHz = 2us, count up
		timerAttachInterrupt(_timer, &_onTimer, true);   // attach handler, trigger on edge 
		timerAlarmWrite(_timer, 2000L, true);            // 2000 * 2us = 4ms, single shot
		timerAlarmEnable(_timer); // start**
		_bTimerRunning = true;
		m_pSerial->uartAttachTx();
		// digitalWrite(15, true);
		**portEXIT_CRITICAL(&_timerMux);**
	}
	return m_pSerial->write(buffer, size);
}

For being sure, if i try to do the same thing in SBUS2 ESP32, do i have to do it in these function
static void IRAM_ATTR ISR_transmit_frame(void *arg) ???

Sorry for all my question, i'm not a software developper

Thanks a lot
Julien

@BrushlessPower
Copy link
Owner

portENTER_CRITICAL_ISR should be in every ISR i think. But it seems that the timer ISR is the Problem. You should start there.

I'am also no Software Dev. And ESP32 with RTOS and DUAL is a kind of black magic for me.

Greetings John

@magnum3131
Copy link
Author

Hi,

I don't manage to improve the stability, it's crashing all the time when i change parameter.

Do you use the SBUS2 Librabry with sensor like bme280 or GPS ?

Thanks
Julien

@BrushlessPower
Copy link
Owner

No
but someone else used it with a GPS Sensor without problems.

Could you send me your Project (INO file)?
Maybe i can see something

@BrushlessPower
Copy link
Owner

BrushlessPower commented Jul 8, 2020

i had a look on TinyGPS++
this is just a "conversion" from Chars to Coordinates....so its the same like delay(x)

could you try to read GPS data with your Hardware Serial (another than the SBUS Hardwareserial!) and just insert some delay()
without TinyGPS++

this test could be easily repeated without GPS sensor, just with a stream of UART Data

@magnum3131
Copy link
Author

No
but someone else used it with a GPS Sensor without problems.

Could you send me your Project (INO file)?
Maybe i can see something

Hi John,

Thanks for your help

Here the files
https://www.mab.asso.fr/Futaba_Telemetry.rar

Julien

@magnum3131
Copy link
Author

i had a look on TinyGPS++
this is just a "conversion" from Chars to Coordinates....so its the same like delay(x)

could you try to read GPS data with your Hardware Serial (another than the SBUS Hardwareserial!) and just insert some delay()
without TinyGPS++

this test could be easily repeated without GPS sensor, just with a stream of UART Data

I'll try these this night

thanks a lot
Julien

@magnum3131
Copy link
Author

Hi Jonh,

I made more test today, i unloaded all library (Tiny GPS, BME280,..) and i still having crash by reading GPS value with only serial.read() command on hardware serial.

Guru Meditation Error: Core  1 panic'ed (Interrupt wdt timeout on CPU1)
Core 1 register dump:
PC      : 0x40084914  PS      : 0x00050434  A0      : 0x40081010  A1      : 0x3ffbe7d0  
A2      : 0x00000000  A3      : 0x00000013  A4      : 0x3ffbe7e8  A5      : 0x3ffbe7b0  
A6      : 0x00000004  A7      : 0x00000000  A8      : 0x3ff50000  A9      : 0x3ffc023d  
A10     : 0x00000000  A11     : 0x00000019  A12     : 0x00000001  A13     : 0x3ffbe780  
A14     : 0x00000000  A15     : 0x00000101  SAR     : 0x0000001d  EXCCAUSE: 0x00000006  
EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  
Core 1 was running in ISR context:
EPC1    : 0x40081010  EPC2    : 0x00000000  EPC3    : 0x00000000  EPC4    : 0x40084914

Backtrace: 0x40084914:0x3ffbe7d0 0x4008100d:0x3ffbc570 0x400d4b33:0x3ffbc590 0x4008a7c2:0x3ffbc5b0 0x40089281:0x3ffbc5d0

Core 0 register dump:
PC      : 0x400ed8fa  PS      : 0x00060134  A0      : 0x800d4b36  A1      : 0x3ffbbff0  
A2      : 0x00000000  A3      : 0x00000001  A4      : 0x00000000  A5      : 0x00000001  
A6      : 0x00060120  A7      : 0x00000000  A8      : 0x800d46fe  A9      : 0x3ffbbfc0  
A10     : 0x00000000  A11     : 0x400850a8  A12     : 0x00060120  A13     : 0x3ffbb970  
A14     : 0x00000000  A15     : 0x3ffbbce0  SAR     : 0x00000000  EXCCAUSE: 0x00000006  
EXCVADDR: 0x00000000  LBEG    : 0x00000000  LEND    : 0x00000000  LCOUNT  : 0x00000000  

Backtrace: 0x400ed8fa:0x3ffbbff0 0x400d4b33:0x3ffbc010 0x4008a7c2:0x3ffbc030 0x40089281:0x3ffbc050

Thanks
Julien

@BrushlessPower
Copy link
Owner

That's "good". With a simple Serial Terminal i can send a stread of Data to simulate the Problem

could you send me your INO file?
maybe via email
brushlesspower@protonmail.com

@BrushlessPower
Copy link
Owner

BrushlessPower commented Jul 15, 2020

so a little test today:

GPS Data (750 Byte) with 0.1s delay send with HTerm

no crash over 30min

here my loop:

void loop()
{
uint16_t uart_dropped_frame = 0;
bool transmision_dropt_frame = false;
bool failsave = false;

/* DO YOUR STUFF HERE */
/* Make sure you do not do any blocking calls in the loop, e.g. delay()!!! */
while(Serial.available()){
  Serial.write(Serial.read());
}
//delay(500);

if (SBUS_Ready())
{                                      // SBUS Frames available -> Ready for getting Servo Data
    channel = SBUS2_get_servo_data(5); // Channel = Servo Value of Channel 5
    
}

//Serial.println(channel);

if (SBUS2_Ready())
{                                                                                                                  // SBUS2 Frame available -> Ready for transmit Telemetry
    //digitalWrite(13, HIGH);                                                                                        // set pin D13 (LED ON) -> SBUS2 Frames OK
    SBUS2_get_status(&uart_dropped_frame, &transmision_dropt_frame, &failsave);                                    // Check SBUS(2) Status
    if((uart_dropped_frame != 0) ||(transmision_dropt_frame != 0) || (failsave != 0) ){
      Serial.print("UART dropped Frame: ");
      Serial.println(uart_dropped_frame);
      Serial.print("Transmission dropped Frame: ");
      Serial.println(transmision_dropt_frame);
      Serial.print("Failsave: ");
      Serial.println(failsave);
    }
    send_alarm_as_temp125(ERROR_SLOT, ((failsave * 1000) + (transmision_dropt_frame * 100) + uart_dropped_frame)); // Warning with over Temp at Error Slot

    send_temp125(TEMPRATURE_SLOT, (int16_t)(uint16_t)50);                                        // Temperature [°C]
    send_RPM(RPM_SLOT, (uint16_t)600);                                                 // RPM = 600-> rounding Error +/- 3 RPM
    send_s1678_current(CURRENT_SLOT, (uint16_t)2345, (uint16_t)15000, (uint16_t)1234); // Current = 23.45A, Capacity = 15000mAh, Voltage = 12.34V

    send_f1675_gps(GPS_SLOT, (uint16_t)50, (int16_t)1000, (int16_t)200, latitude, longitude); // Speed = 50km/h, Altitude = 1000m, Vario = 200m/s
}
else
{
    // digitalWrite(13, LOW); // set pin D13 (LED OFF) -> No SBUS2 Frames
}

} // End of Loop()

@BrushlessPower
Copy link
Owner

try to update your SBUS lib with this file

SBUS_usart_ESP32.zip

@magnum3131
Copy link
Author

magnum3131 commented Jul 15, 2020 via email

@BrushlessPower
Copy link
Owner

Dear Julien,

you should not try to change these Parameters without oscilloscope or logic analyzer!
The old code had a bug that sometime the USART Interrupt was called all the time without break. I thought it could be the reason for your crashes.

I tested the GPS Stream but i had no crashes. Everything worked perfect. But the USART Interrupt Bug started.
So i fixed that.

But at this point i cant help you any more. I cant reproduce your fault. And i cant use your sketch because i don't have your library's.

So start to reproduce the error with my example. And please keep me up to date with your findings.
Greetings John

@magnum3131
Copy link
Author

magnum3131 commented Jul 16, 2020 via email

@BrushlessPower BrushlessPower transferred this issue from another repository Jul 21, 2020
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