adafruit / Adafruit_nRF52_Arduino

Adafruit code for the Nordic nRF52 BLE SoC on Arduino
Other
615 stars 496 forks source link

Softwareserial Lib Is Reading Incorrect Values #805

Open macirtr opened 9 months ago

macirtr commented 9 months ago

Operating System

Windows 10

IDE version

Ardino 2.2.0

Board

Adafruit Feather nrf52832

BSP version

1

Sketch

Adafruit software serial lib

What happened ?

Softwareserial is reading incorrect value. We use 115200 as the baud rate. Usually the first and last element of the returned byte array are incorrect. For example, the other device sends 0xBB as the first byte. Serialsoftware sees it as 0x7B. While the last byte should be 0x7F, the value 0xFE is read. I investigated the reason for this and found that a timing error occurred in void SoftwareSerial::recv(). delayMicroseconds(_rx_delay_centering); _rx_delay_centering this value is 2 milliseconds. If I do this for 4 milliseconds, the first byte is read correctly as 0xBB. However, this change disrupts the reading of subsequent bytes. I tried the ADAFRUIT_TINYUSB library after seeing the error in the library. And it worked flawlessly there. However, rx and tx pins cannot be changed in this library. We should be able to select rx tx pins in the project. However, I also saw the uart.h code. Pins can be selected in this code. But there are no examples and resources in the folder or on the internet for uart. Can you help us with this? Do you have a solution suggestion?

How to reproduce ?

Use 115200 baud rate for swoftware serial lib. Send a byte array starting with BB from a device using 115200. You will see that the first byte is read 7B. delayMicroseconds(_rx_delay_centering); line delayMicroseconds(2); Change it to . You will see that it reads the BB value.

Debug Log

No response

Screenshots

No response

macirtr commented 9 months ago

After three days, I found the error.

https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/libraries/SoftwareSerial/SoftwareSerial.cpp _rx_delay_centering = (bit_delay/2) - 2;

Remove the -2 on line 63 and it works.

hathach commented 9 months ago

would you mind making an PR for the fix ?