Skip to content

Commit 85d39cf

Browse files
Restore GPIO functions on SerialUART::end (#836)
Fix #834
1 parent 2d777ac commit 85d39cf

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

cores/rp2040/SerialUART.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,16 @@ void SerialUART::begin(unsigned long baud, uint16_t config) {
171171
break;
172172
}
173173
uart_set_format(_uart, bits, stop, parity);
174+
_fcnTx = gpio_get_function(_tx);
175+
_fcnRx = gpio_get_function(_rx);
174176
gpio_set_function(_tx, GPIO_FUNC_UART);
175177
gpio_set_function(_rx, GPIO_FUNC_UART);
176178
if (_rts != UART_PIN_NOT_DEFINED) {
179+
_fcnRts = gpio_get_function(_rts);
177180
gpio_set_function(_rts, GPIO_FUNC_UART);
178181
}
179182
if (_cts != UART_PIN_NOT_DEFINED) {
183+
_fcnCts = gpio_get_function(_cts);
180184
gpio_set_function(_cts, GPIO_FUNC_UART);
181185
}
182186
uart_set_hw_flow(_uart, _rts != UART_PIN_NOT_DEFINED, _cts != UART_PIN_NOT_DEFINED);
@@ -211,6 +215,7 @@ void SerialUART::end() {
211215
irq_set_enabled(UART1_IRQ, false);
212216
}
213217
}
218+
214219
// Paranoia - ensure nobody else is using anything here at the same time
215220
mutex_enter_blocking(&_mutex);
216221
mutex_enter_blocking(&_fifoMutex);
@@ -219,6 +224,16 @@ void SerialUART::end() {
219224
// Reset the mutexes once all is off/cleaned up
220225
mutex_exit(&_fifoMutex);
221226
mutex_exit(&_mutex);
227+
228+
// Restore pin functions
229+
gpio_set_function(_tx, _fcnTx);
230+
gpio_set_function(_rx, _fcnRx);
231+
if (_rts != UART_PIN_NOT_DEFINED) {
232+
gpio_set_function(_rts, _fcnRts);
233+
}
234+
if (_cts != UART_PIN_NOT_DEFINED) {
235+
gpio_set_function(_cts, _fcnCts);
236+
}
222237
}
223238

224239
void SerialUART::_pumpFIFO() {

cores/rp2040/SerialUART.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class SerialUART : public HardwareSerial {
7171
uart_inst_t *_uart;
7272
pin_size_t _tx, _rx;
7373
pin_size_t _rts, _cts;
74+
enum gpio_function _fcnTx, _fcnRx, _fcnRts, _fcnCts;
7475
int _baud;
7576
mutex_t _mutex;
7677
bool _polling = false;

0 commit comments

Comments
 (0)