mrrwa / LocoNet

An embedded Loconet interface library for Arduino family microcontrollers
Other
68 stars 32 forks source link

ESP8266 no packet receive #26

Closed Digital-MoBa closed 2 years ago

Digital-MoBa commented 2 years ago

I try to use the ESP8266 Wemos D1 Mini with the LocoNet library. All data that is send with the library is correctly received over PIN D6 (GPIO12). All other packets, send by other LocoNet clients aren’t received! But I cloud figure out that the library with LocoNet.available() give TRUE if there is send a packet by another device. But LocoNet.receive() doesn’t report any valid LnPacket from the buffer back. Why the packet is in the buffer but can’t read out with the receive function? (Notice: With a Arduino MEGA/UNO all packets are received with this library and the same source code)

kiwi64ajs commented 2 years ago

Hi Philipp,

On 9/11/2021, at 3:20 AM, Philipp Gahtow @.***> wrote: I try to use the ESP8266 Wemos D1 Mini with the LocoNet library. All data that is send with the library is correctly received over PIN D6 (GPIO12). All other packets, send by other LocoNet clients aren’t received! But I cloud figure out that the library with LocoNet.available() give TRUE if there is send a packet by another device. But LocoNet.receive() doesn’t report any valid LnPacket from the buffer back. Why the packet is in the buffer but can’t read out with the receive function? (Notice: With a Arduino MEGA/UNO all packets are received with this library and the same source code)

1) Hmmm… I’ve not personally used the library with an ESP8266 chip, but looking at the source code the Rx pin is defined to be “D6” here:

https://github.com/mrrwa/LocoNet/blob/master/utility/ln_config.h#L224 <https://github.com/mrrwa/LocoNet/blob/master/utility/ln_config.h#L224>

and the Default TX Pin is defined to be “D7” here:

https://github.com/mrrwa/LocoNet/blob/master/LocoNet.cpp#L113 <https://github.com/mrrwa/LocoNet/blob/master/LocoNet.cpp#L113>

So if you have those pins wired to your LocoNet interface circuitry then you should be ok.

2) Another issue could be with the LocoNet Interface circuit are you using as you may have to invert the LocoNet electronic signal polarity compared to the logic (active High/Low) in the library.

Check the logic of the circuit first and maybe experiment with commenting/uncommenting these two lines that Hans Tanner added:

https://github.com/mrrwa/LocoNet/blob/master/utility/ln_config.h#L87 <https://github.com/mrrwa/LocoNet/blob/master/utility/ln_config.h#L87>

https://github.com/mrrwa/LocoNet/blob/master/utility/ln_config.h#L92 <https://github.com/mrrwa/LocoNet/blob/master/utility/ln_config.h#L92>

3) Failing that let me know the version of the library you are using as there maybe some bugs as there has been quite a bit of change recently with STM31 and ESP8266 / ESP32.

HTH

Regards

Alex Shepherd

m: +64-21-777764 e: @.***

Digital-MoBa commented 2 years ago

Hi Alex,

thank you for your replay. The PIN configuration works and there is data received that is send by the ESP8266 itself. If I disconnect the RX PIN (D6) the sending doesn't work, because the library can't check the data signal collision detection.

I'm using the same LocoNet Interface that I use with the Arduino UNO and MEGA. Arduino-LocoNet_Interface.png So the logic should be correct, because also the sending (TX data and verify with RX) on the ESP8266 works fine.

What I find strange is that if another device send data to the ESP8266, it detects them with LocoNet.available() == true. But the data itself can't read with LnPacket = LocoNet.receive();

I wrote a small test application: ESP_LocoNet_Test.zip

Here is the result when it's working on a Arduino MEGA: LocoNet Monitor Send Request: DATA RX: BB 01 00 45 DATA DATA RX: 81 7E DATA DATA DATA DATA DATA DATA DATA DATA DATA DATA DATA RX: E7 0E 01 13 57 00 20 07 00 00 00 00 00 74 Send Request: DATA RX: BB 01 00 45 DATA DATA DATA DATA DATA DATA DATA DATA DATA DATA DATA DATA DATA DATA RX: E7 0E 01 13 57 00 20 07 00 00 00 00 00 74 There is normaly one "DATA" notified by LocoNet.available() for each byte that is received!

If I'm using the same sketch on the ESP8266 Wemos then I get only this: Send Request: DATA RX: BB 01 00 45 DATA Send Request: DATA RX: BB 01 00 45 DATA Send Request: DATA RX: BB 01 00 45 DATA DATA Send Request: DATA RX: BB 01 00 45 DATA Send Request: DATA RX: BB 01 00 45 DATA There is no answer from the master to the slot request received from the ESP8266. But for each data packet for example from the master on the Loconet line there is a LocoNet.available() == true) notification "DATA".

In addition I found that if LocoNet.available() is available also the LocoNet.length() is set correct. But not at all, I think thats because the lnRxBuffer overflows.

regards Philipp

Digital-MoBa commented 2 years ago

Hi Alex,

yesterday I got an new idea: I try to set up two ESP8266 with a LocoNet Interface and see if they can communicate with each other. Yes, they both can send messaged and also receive them without problems! Is it that simple?

I would answer this question by myself, no not at all. The major problem is a timing issue with the timer1 on the ESP8266. The timer1 is set with #define LN_BIT_PERIOD ((F_CPU / 16) / 16666) in the same way like for the other microcontroller. https://github.com/mrrwa/LocoNet/blob/master/utility/ln_config.h#L95 There is just the divider of 16 from the timer more inside. One issue is that the calculated timer1 speed doesn't fit to the LocoNet specification. But the big fault is, that you can config the CPU frequenz on the ESP8266 from 80Mhz to 160Mhz and this has no effect to the timer speed! So if this value is inside the speed calculation the timing will be wrong!

With my testing I found the best solution if the timer1 is used without a divider in _ln_swuart.cpp (line 439) So the timing can adjust very fine. timer1_enable(TIM_DIV1, TIM_EDGE, TIM_SINGLE); https://github.com/mrrwa/LocoNet/blob/master/utility/ln_sw_uart.cpp#L439

Also we need to fix with this line: #define LN_BIT_PERIOD 4720 [https://github.com/mrrwa/LocoNet/blob/master/utility/ln_config.h#L95] Without the divider this value can be much more accurate. The value is tested with both cpu frequencies and DAISY II, IntelliBox and Arduino MEGA as LocoNet peripherals. Also if I use more then one ESP8266 peripherals it works correct in RX and TX mode.

You will find the changes with a new pull request. https://github.com/mrrwa/LocoNet/pull/28 Thanks for your excellent work.

regards Philipp