Skip to content

Commit

Permalink
Fix smg_uart_uninit() from blocking subsequent re-initialisation (#…
Browse files Browse the repository at this point in the history
…2560) (#2561)

This PR fixes a bug in the Esp8266 and Host uart driver implementations of `smg_uart_uninit` which causes subsequent calls to `smg_uart_init_ex` to fail as it thinks the port is in use.

This bug is exposed if a port is opened, closed and then re-opened:

```
Serial.begin(COM_SPEED_SERIAL);
...
Serial.end(); // optional - begin() calls this internally anyway
Serial.begin(9600); // fails
```

Also add a `bool` return value from `HardwareSerial::begin()` method so caller can check for errors.

Co-authored-by: Mike <[email protected]>
  • Loading branch information
slaff and mikee47 authored Oct 4, 2022
1 parent 0552170 commit 4cf5a69
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions Sming/Arch/Esp8266/Components/driver/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ void smg_uart_uninit(smg_uart_t* uart)
break;
}

uartInstances[uart->uart_nr] = nullptr;
delete uart->rx_buffer;
delete uart->tx_buffer;
delete uart;
Expand Down
1 change: 1 addition & 0 deletions Sming/Arch/Host/Components/driver/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ void smg_uart_uninit(smg_uart_t* uart)
smg_uart_set_debug(UART_NO);
}

uartInstances[uart->uart_nr] = nullptr;
delete uart->rx_buffer;
delete uart->tx_buffer;
delete uart;
Expand Down
13 changes: 6 additions & 7 deletions Sming/Core/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ HardwareSerial::~HardwareSerial()
#endif
}

void HardwareSerial::begin(uint32_t baud, SerialConfig config, SerialMode mode, uint8_t txPin, uint8_t rxPin)
bool HardwareSerial::begin(uint32_t baud, SerialConfig config, SerialMode mode, uint8_t txPin, uint8_t rxPin)
{
end();

if(uartNr < 0)
return;
if(uartNr < 0) {
return false;
}

smg_uart_config_t cfg = {
.uart_nr = (uint8_t)uartNr,
Expand All @@ -48,6 +49,8 @@ void HardwareSerial::begin(uint32_t baud, SerialConfig config, SerialMode mode,
};
uart = smg_uart_init_ex(cfg);
updateUartCallback();

return uart != nullptr;
}

void HardwareSerial::end()
Expand All @@ -56,10 +59,6 @@ void HardwareSerial::end()
return;
}

if(smg_uart_get_debug() == uartNr) {
smg_uart_set_debug(UART_NO);
}

smg_uart_uninit(uart);
uart = nullptr;
}
Expand Down
18 changes: 11 additions & 7 deletions Sming/Core/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,11 @@ class HardwareSerial : public ReadWriteStream

/** @brief Initialise the serial port
* @param baud BAUD rate of the serial port (Default: 9600)
* @retval bool true on success
*/
void begin(uint32_t baud = 9600)
bool begin(uint32_t baud = 9600)
{
begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
return begin(baud, SERIAL_8N1, SERIAL_FULL, 1);
}

/**
Expand All @@ -142,10 +143,11 @@ class HardwareSerial : public ReadWriteStream
* even (E), and no (N) parity, and 1 or 2 stop bits.
* To set the desired mode, call Serial.begin(baudrate, SERIAL_8N1),
* Serial.begin(baudrate, SERIAL_6E2), etc.
* @retval bool true on success
*/
void begin(uint32_t baud, SerialConfig config)
bool begin(uint32_t baud, SerialConfig config)
{
begin(baud, config, SERIAL_FULL, 1);
return begin(baud, config, SERIAL_FULL, 1);
}

/**
Expand All @@ -156,10 +158,11 @@ class HardwareSerial : public ReadWriteStream
* To set the desired mode, call Serial.begin(baudrate, SERIAL_8N1),
* Serial.begin(baudrate, SERIAL_6E2), etc.
* @param mode specifies if the UART supports receiving (RX), transmitting (TX) or both (FULL) operations
* @retval bool true on success
*/
void begin(uint32_t baud, SerialConfig config, SerialMode mode)
bool begin(uint32_t baud, SerialConfig config, SerialMode mode)
{
begin(baud, config, mode, 1);
return begin(baud, config, mode, 1);
}

/**
Expand All @@ -169,8 +172,9 @@ class HardwareSerial : public ReadWriteStream
* @param mode
* @param txPin Can specify alternate pin for TX
* @param rxPin
* @retval bool true on success
*/
void begin(uint32_t baud, SerialConfig config, SerialMode mode, uint8_t txPin, uint8_t rxPin = UART_PIN_DEFAULT);
bool begin(uint32_t baud, SerialConfig config, SerialMode mode, uint8_t txPin, uint8_t rxPin = UART_PIN_DEFAULT);

/**
* @brief De-inits the current UART if it is already used
Expand Down

0 comments on commit 4cf5a69

Please sign in to comment.