bit banging code example

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
lvx_xvl
Posts: 5
Joined: Sun Jul 26, 2020 11:45 am

bit banging code example

Post by lvx_xvl » Sat Feb 05, 2022 6:58 pm

Hi All,

I'm trying to setup a four way communication, as a gateway, between two devices.

I'm using an ESP8266 (Wemos D1 mini) with MicroPython 1.18. This device receives and forwards communication too and from the other devices. To be precise, the other two devices are OpenTherm devices, and I'm trying to intervene the communication and modify certain parameters.

The communication is via a 2-wire (In and Out) communication. Currently I'm able to receive messages via an interrupt on the incoming pin. The trouble starts when I forward the message (without changing). The timing isn't according to specification and the receiving device dismisses to send package. At least that is what I suspect.
I don't own a digital oscilloscope, soo, it is hard to tell. I've timed the total package transmission in nano seconds which is way off and irregular. The specified package transmission is 1000us per bit, the package size is 34bit (32bit + start bit + stop bit) which should yield 34000us, but is measured somewhere between 40.000us and 50.000us.

Currently I'm using two delays of 500us to switch between the bit states. This software bit banging isn't the most practical, but I don't know how to resolve this. The interrupt handling of the incoming package works perfectly right. I've timed it and was somewhere close to 34.000us +/- a few nanoseconds.
This means the board is capable of handling such fast transitions.

My question: How to get better bit banging performance? I've looked at the documentation and found a couple of hints:
- machine.bitstream
- interrupt handlers
- machine.SoftI2C
- machine.I2C
- machine.SoftSPI
- machine.SPI

The I2C and SPI aren't usable I think, due to the protocol they are intended for.
Interrupt handlers sound as fast capable routines, but how to trigger them and get the timing in.
Bitstream sounds like the most promising, but I can't find a good example.

Can someone point out to info, or share example code, or share their opinion on the task at hand.

Thanks in advance,
best regards,
LVX

lvx_xvl
Posts: 5
Joined: Sun Jul 26, 2020 11:45 am

Re: bit banging code example

Post by lvx_xvl » Mon Feb 07, 2022 7:25 pm

Just wanted to add my current exploration details to solve this problem.

I found that the hardware SPI pins MISO and MOSI are 12 and 13, which seems to be a method of utilising the raw (potential) speed. Is it possible to provided bytes to a buffer directly and let the hardware deal with sending the bits?

Br,
LVX

fivdi
Posts: 16
Joined: Thu Feb 03, 2022 10:28 pm
Contact:

Re: bit banging code example

Post by fivdi » Mon Feb 07, 2022 8:17 pm

lvx_xvl wrote:
Mon Feb 07, 2022 7:25 pm
I found that the hardware SPI pins MISO and MOSI are 12 and 13, which seems to be a method of utilising the raw (potential) speed. Is it possible to provided bytes to a buffer directly and let the hardware deal with sending the bits?
Yes, it's possible to let hardware deal with sending the bits. Take a look at the HardwareSPI bus section of the Quick reference for the ESP8266. That being said, I don't know if it's suitable for communicating with OpenTherm devices.

Post Reply