| Electronics > Projects, Designs, and Technical Stuff |
| ESP32 Serial Won't Start After Serial.end() |
| (1/2) > >> |
| petersanch:
I wrote program to begin serial 1, send a byte then disable serial1 and loop. If Serial1.end() is called then the TX pin will not function properly. The first time loop() runs it send the byte then stays low for about 600 microseconds and stays high after that. The TX channel stops toggling after that. The reason I need to call Serial1.end() is because my TX pin is multiplexed and I need to use it as a general IO later. I tried using Serial1.flush() before and after end(), but it doesn't help. --- Code: ---#include "HardwareSerial.h" uint8_t a= 10; void setup() { } void loop() { Serial1.begin(250000); Serial1.write(a); Serial1.end(); //MAKE PIN 10 GENERAL IO AND DO SOMETHING WITH digitalWrite(10,LOW) or digitalWrite(10,HIGH) HERE delay(100); } --- End code --- Using the ESP32 Pico with 1.0.2-rc1 version in Arduino. Cheers |
| petersanch:
I tried changing the code to use HardwareSerial specifically but the results are the same. --- Code: ---#include <HardwareSerial.h> uint8_t a= 10; HardwareSerial MySerial(1); void setup() { } void loop() { MySerial.begin(250000, SERIAL_8N1, 9, 10); MySerial.write(a); MySerial.end(); //MAKE PIN 10 GENERAL IO AND DO SOMETHING WITH digitalWrite(10,LOW) or digitalWrite(10,HIGH) HERE delay(100); } --- End code --- |
| cv007:
I believe the arduino/sdk code is simply checking the fifo's- for the flush it waits until the fifo count is 0, but that still means a byte could still be on its way out (fifo is empty, but transmit shift register is not). Looking at the esp32 datasheet, they have a UART_TX_DONE_INT, which could be put to use I imagine (don't really need the irq, just its flag). There is also UART_ST_UTX_OUT which gives the state machine status of the transmitter. I would try the simple approach first- do the Serial1.flush() then delay for one byte of time + a little fudge. The flush will block until the fifo is empty (so if you later do more than one byte, your method will still work), then you delay while the last byte is going out. You could also move that code closer to where the pins will be used for the general io if you do not like the idea of waiting around (if for example a lot of bytes going out at once), assuming there is something useful to do while waiting. |
| petersanch:
Thanks. Your suggestion helped with part of it. Adding a flush and delay makes this program work as expected now. A byte is written on the TX pin every 100 ms. --- Code: ---#include <HardwareSerial.h> uint8_t a= 10; HardwareSerial MySerial(1); void setup() { pinMode(10, OUTPUT); } void loop() { MySerial.begin(250000, SERIAL_8N1, 9, 10); MySerial.write(a); MySerial.flush(); delay(1); MySerial.end(); delay(100); } --- End code --- The problem now is that I want to have TX pin be a digital output after MySerial.end(). If I try to do that then the output stays HIGH until the loop() starts again and the serial commands are run. There is no difference in the output of the TX pin in the bottom code vs the top code. --- Code: ---#include <HardwareSerial.h> uint8_t a= 10; HardwareSerial MySerial(1); void setup() { pinMode(10, OUTPUT); } void loop() { MySerial.begin(250000, SERIAL_8N1, 9, 10); MySerial.write(a); MySerial.flush(); delay(1); MySerial.end(); //MAKE PIN 10 GENERAL IO AND DO SOMETHING WITH digitalWrite(10,LOW) or digitalWrite(10,HIGH) HERE pinMode(10, OUTPUT); digitalWrite(10, LOW); delay(1); digitalWrite(10, HIGH); delay(1); digitalWrite(10, LOW); delay(1); digitalWrite(10, HIGH); delay(1); delay(100); } --- End code --- end() calls uartDetachRx(uart) and uartDetachTx(uart). Is it possible the detach is not working? |
| Nusa:
Check your assumptions. Does the general i/o code work as expected if the serial i/o is disabled? |
| Navigation |
| Message Index |
| Next page |