8000 Esp8266 uart fixes by svily0 · Pull Request #2990 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

Esp8266 uart fixes #2990

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

Closed
wants to merge 2 commits into from
Closed

Conversation

svily0
Copy link
@svily0 svily0 commented Apr 1, 2017

Doing something like:

data = bytes(bytearray(range(0x100)))
with serial.Serial(TTY, BAUD) as ser:
    ser.write(data)

when a ESP8266 is connected to the serial bus has the unfortunate effect of resetting the board.
Make it possible to put any data on the serial bus without REPL interpreting it.

@dpgeorge
Copy link
Member
dpgeorge commented Apr 4, 2017

This is essentially a duplicate of #2891. Please look at that PR and associated discussion.

@dpgeorge dpgeorge closed this Apr 4, 2017
@svily0
Copy link
Author
svily0 commented Apr 4, 2017

Well, I actually started from #2891.
The board still reboots when data from my acquisition device contains 0x03 in it. Hence the patch.

@sponsoredlinks
Copy link

Hi guys,

Has this been closed incorrectly? It appears to be useful, would be good to double-check

@dpgeorge dpgeorge reopened this Apr 7, 2017
@@ -108,6 +108,7 @@ void init_done(void) {
#endif
}

// Seems unused
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change.

Also, "seems" sounds dangerous. This patch also "seems" to work, right?

@dpgeorge
Copy link
Member
dpgeorge commented Apr 7, 2017

Ok, yes, it's not a duplicate of #2891, although it's related.

What you are really trying to do here is disable ctrl-C being picked up by the UART interrupt and turned into a KeyboardInterrupt exception. And that can be done in a simpler way that what's presented here, essentially all you need to do is call mp_hal_set_interrupt_char(-1) to disable it.

This is a general problem for all ports that needs to be solved.

The same issue is reported recently for the microbit: bbcmicrobit/micropython#423

stmhal solves it by providing USB_VCP.set_interrupt(): passing -1 disables ctrl-C, and passing any number between 1-255 enables interruption using that char (eg passing 3 corresponds to crtl-C).

So it remains to define a port-agnostic way of changing the interrupt character from a script. Since the function is related to the core of uPy I propose putting it in the micropython module as:

micropython.kbd_intr(value)

It would just call mp_hal_set_interrupt_char with the given value.

@@ -81,7 +81,7 @@ typedef struct {
UartBautRate baut_rate;
UartBitsNum4Char data_bits;
UartExistParity exist_parity;
UartParityMode parity; // chip size in byte
UartParityMode parity; // even/odd
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not very related change, should be submitted separately.

@@ -92,12 +92,12 @@ typedef struct {
} UartDevice;

void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
int uart0_rx(void);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change.

@@ -165,22 +171,29 @@ static void uart0_rx_intr_handler(void *para) {
// fifo full
goto read_chars;
} else if (UART_RXFIFO_TOUT_INT_ST == (READ_PERI_REG(UART_INT_ST(uart_no)) & UART_RXFIFO_TOUT_INT_ST)) {
// rx timeout
read_chars:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rx timeout what?

@pfalcon
Copy link
Contributor
pfalcon commented Apr 7, 2017

The pull request title appears to be incorrect - the patch doesn't seem to provide "fixes", but rather something else. To help with timely reviews, please use descriptive titles. Thanks.

@dpgeorge
Copy link
Member

This feature is now available by calling micropython.kbd_intr(-1) to disable ctrl-C capture.

@sponsoredlinks
Copy link

Hi Damien, thanks for following up on this. It seemed that the original intent of some of these PRs was to allow (all) interpretation of input on the UART by the repl to be disabled.

Do you know if this is currently the case? It seems from the description that if a serial device is attached to UART1 then buffers will still fill and the REPL will attempt to interpret the data received, save for control-c

@dpgeorge
Copy link
Member

It seemed that the original intent of some of these PRs was to allow (all) interpretation of input on the UART by the repl to be disabled.

That is what #2891 is trying to do. And we will still work towards that feature.

It seems from the description that if a serial device is attached to UART1 then buffers will still fill and the REPL will attempt to interpret the data received, save for control-c

The UART buffer will fill. If your scripts is running then the REPL is not active and will not touch the UART buffer. So your script is free to take the data from it. If you run micropython.kbd_intr(-1) at the start of your script then ctrl-C is not captured, this char is just added to the UART buffer in the same way as all other chars.

@sponsoredlinks
Copy link

Ah, got it. Ok thanks

@mohpor
Copy link
mohpor commented Apr 20, 2017

@dpgeorge Hi,
In need for a usable UART (like many other Micropython developers), I have found this PR. It truly solves my problems, especially when I combine it with select.poll(). Now I am able to easily send AT commands to my modem and get the results and handle them beautifully.
Now, I know this is not your ideal solution at the moment and I have tested #2891 which seems rather off at the moment (see the comments, mine included). Is there a way this PR could be merged for all those poor guys like me who are desperate to see UART work, so that we can have other updates from Micropython too, until a proper implementation of UART and REPL coexistence comes to life?

@dpgeorge
Copy link
Member

@mohpor did you try using the new micropython.kbd_intr() function? The code @svily0 posted above can now work correctly using this function, like:

import micropython as mp, machine
uart = machine.UART(0, 115200, timeout=500)

def uart_listen():
    mp.kbd_intr(-1)
    try:
        data = bytearray(0x100)
        n = uart.readinto(data)
        print(n, data)
    finally:
        mp.kbd_intr(3)

You can send (or type) any characters into the UART (even ctrl-C) and it will collect them all.

@mohpor
Copy link
mohpor commented Apr 20, 2017

I have not tested the code you have sent, but then again, I have no problem with Ctrl-C per se.
If I'm not mistaken, kbd_intr had been added yesterday, do I need to patch @svily0's PR with the new update? (I'm a little bit confused obviously)

@mohpor
Copy link
mohpor commented Apr 20, 2017

Here it is my code sample (based on @svily0's PR):

import time
import select
import esp
from machine import UART
import gc

uart = UART(0)
uart.init(9600)

def repl_on():
    esp.replinput(0)
    time.sleep_ms(10)

def repl_off():
    esp.replinput(None)
    time.sleep_ms(10)

def send(cmd):
    cmd += "\r"
    repl_off()
    h = select.poll()
    h.register(uart, select.POLLIN | select.POLLHUP)
    uart.write(cmd)
    data = ""
    try:
        while True:
            gc.collect()
            event = h.poll()
            e = event[0][-1]
            if select.POLLIN == e:
                d = uart.read().decode()
                data += d
                print("NEW DATA: %r" % (d))
            elif select.POLLHUP == e:
                print("End received")
                h.unregister(uart)
                gc.collect()
                return data
            else:
                print("Unknown event [%d] happened" % e)
                h.unregister(uart)
                gc.collect()
                return data

    finally:
        repl_on()

Sorry for the messy code, it is just a test.

@dpgeorge
Copy link
Member

I have no problem with Ctrl-C per se.

In that case I don't understand what the problem is...

do I need to patch @svily0's PR with the new update

No, this PR should not be needed, just use micropython.kbd_intr(-1) to "disable" the REPL, and micropython.kbd_intr(3) to "enable" it.

To help fix your problem please tell how you are running your code (eg via UART REPL, script saved to flash, or WebREPL), what the output is and what you would expect the output should be, ie what the problem is.

@mohpor
Copy link
mohpor commented Apr 20, 2017

No, this PR should not be needed, just use micropython.kbd_intr(-1) to "disable" the REPL, and micropython.kbd_intr(3) to "enable" it.

I said I'm a bit confused! That's wonderful news! will get on it right now!
Thanks!

@roger-
Copy link
roger- commented May 9, 2017

Can a UART object be closed so the kbd_intr() trick can be used on the webrepl?

deinit() is unfortunately not implemented.

@dpgeorge
Copy link
Member

Can a UART object be closed

Not yet. This all depends on #2891 and working out how to consistently (across ports) disable some source to dupterm.

tannewt pushed a commit to tannewt/circuitpython that referenced this pull request Jun 1, 2020
…n-master

Translations update from Weblate
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

Successfully merging this pull request may close these issues.

6 participants
0