Software serial?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
pythoncoder
Posts: 3719
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Software serial?

Post by pythoncoder » Sat Mar 17, 2018 6:08 am

A full duplex software UART is quite hard to do, especially if all the features of a hardware UART are to be emulated. Further the Pyboard has five user-accessible hardware UARTs so use-cases on that platform will be few.

The main use-cases are on Espressif devices, especially the ESP8266 which has only 1.5 UARTs, one of which is used for debugging. Alas the interrupt latency of ESPx chips will limit the baudrate of any soft UART.

I think the useful user-contributed half-duplex solution discussed in this thread may be the best we'll get.
Peter Hinch

jimeer
Posts: 3
Joined: Mon Jul 30, 2018 1:44 pm
Location: Barnsley
Contact:

Re: Software serial?

Post by jimeer » Tue Nov 06, 2018 4:23 pm

Why not use an I2C UART?
https://www.byvac.com/shop/product/i2c-to-uart-gpio/
This chip also has DAC/ADC and GPIO. I use a couple for two BLE gateways.

kevinkk525
Posts: 527
Joined: Sat Feb 03, 2018 7:02 pm

Re: Software serial?

Post by kevinkk525 » Tue Nov 06, 2018 4:35 pm

Why bring up an ancient thread?
Kevin Köck
Micropython Smarthome Firmware: https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 3719
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Software serial?

Post by pythoncoder » Wed Nov 07, 2018 7:47 am

The datasheet for that chip is amateurish and inconsistent.
Peter Hinch

MisterVenez
Posts: 1
Joined: Tue May 28, 2019 1:09 pm

Re: Software serial?

Post by MisterVenez » Tue May 28, 2019 1:30 pm

Hi cversec,
I saw your evaluable job in SoftUART. Great !
I think that after 2 year there were some adjustments and experience. Below some questions:
a) I need to set the Parity (ODD) and StopBit (1). May I modify directly on the code (machine_softuart.c) ?
b) Along the code I've seen some sentences like _pyb_softuart_obj_t. Because I'm not using a PYB but another module based on ESP8266, do i need to modify something ?
c) For flashing on a ESP8266 chip, I prefer use the latest micropython code (a lot of water is passed under the bridges...), which files I need to import and modify in order to include the SoftUART in the flashing code ?
d) Do you have a some suggestions on PyCharm to manage correctly the new SoftUART (interpreter related)?

Thanks in advance

Massimo
cversek wrote:
Sat Jan 07, 2017 6:12 am
Hi All,
I'm new to micropython and the esp8266, but I've been a long time Python and Arduino user. On my first IoT data logger project I was finding micropython a joy to develop in and the software machine.I2C library quite easy to adapt for an AM2315 (http://www.adafruit.com/products/1293) humidity and temperature sensor. However, I hit a wall when I needed to integrate a UART communicating carbon dioxide sensor and found that the hardware UARTs were tied up and there was no built-in software serial module.

I actually found deshipu 's original suggestion to be quite helpful. After learning the basics of writing C extension modules (and a lot of trial-and-error hacking) I was able to successfully adapt a `softuart` C code library (http://github.com/plieningerweb/esp8266-software-uart) and wrap it in a micropython extension class `machine.SoftUART` that is based very closely on `machine.UART`.

I wanted to share this code to help others who may find this thread, so I put together a github micropython fork that makes the needed changes (https://github.com/p-v-o-s/micropython/ ... 8a89287a8f).
This firmware can be built according to Adafruit's helpful Feather HUZZAH guide (https://learn.adafruit.com/building-and ... d-firmware) if you substitute this repo instead of the one included on the vagrant VM.

In order initialize the SoftUART object, two Pin objects, tx & rx, are the only required arguments (similar to the I2C object's sda & scl pins). For example:
>>> import machine
>>> ser = machine.SoftUART(machine.Pin(12),machine.Pin(14), baudrate=9600, timeout=1000, timeout_char = 10)
>>> print(ser)
SoftUART(tx=12, rx=14, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, timeout_char=10)
>>> ser.
init flush read readline
readinto write
>>>

The baudrate and timeouts are configurable; however, the data bits, stop bits, and parity are fixed. The softuart.c library uses a statically allocated receive buffer with a default size of 64 bytes (https://github.com/p-v-o-s/micropython/ ... tuart.h#L4). The new `flush` method resets the receive buffer to empty state. One major shortcoming of the `machine.SoftUART` implementation is that only one instance of a SoftUART class can function in overlapping code since it uses a global Softuart struct instance `softuartDevice` - I was unable to get multiple instances working properly using dynamic allocation.

Please let me know if you find this useful or need help to get it working.

tusker
Posts: 2
Joined: Wed Jul 17, 2019 5:14 am

Re: Software serial?

Post by tusker » Wed Jul 17, 2019 5:32 am

Hi MisterVenez,

I have been looking at the SoftUART support for Micropython (for integrating with RS485 adapters), and therefore have pulled in the code changes and adapted the patches for the current micropython code base. You can see on https://github.com/dmascord/micropython.

I have also pulled in the performance improvements from https://github.com/juancgalvez/Arduino- ... tware-UART, so hopefully it is better performing now.

In any case, in terms of your questions:
a) I am not sure that you will get away from just modifying the parity and stop bit, because parity and stop bit isn't implemented in softuart.c (you can see alternative softuart implementations that do implement parity, such as https://github.com/kirtikaran3/TM4C/blo ... softuart.c)
b) This doesn't look specific to PYB and should be generic across all ESP8266.
c) As per above comment, you are welcome to try out my fork (I have only compile tested it so far)
d) What do you mean about manage correctly ?

Cheers,

Damien

liuyuelin
Posts: 1
Joined: Fri Jul 26, 2019 8:52 am

Re: Software serial?

Post by liuyuelin » Fri Jul 26, 2019 8:57 am

Do you know how to turn off an external interrupt setting?I want to write softuart, but I want external interrupt mode to detect the start bit, and turn off the interrupt after data transmission starts, but how to turn off the interrupt?

tusker
Posts: 2
Joined: Wed Jul 17, 2019 5:14 am

Re: Software serial?

Post by tusker » Fri Aug 16, 2019 3:34 am

liuyuelin wrote:
Fri Jul 26, 2019 8:57 am
Do you know how to turn off an external interrupt setting?I want to write softuart, but I want external interrupt mode to detect the start bit, and turn off the interrupt after data transmission starts, but how to turn off the interrupt?
In the softuart.c, you can see the following lines:

// disable all interrupts
ets_intr_lock();
.
.
.
// disable interrupt for GPIO0
gpio_pin_intr_state_set(GPIO_ID_PIN(s->pin_rx.gpio_id), GPIO_PIN_INTR_DISABLE);

You can see some discussion on the topic at https://github.com/esp8266/Arduino/issues/2218

Post Reply