Skip to content

Commit 69d3a1b

Browse files
committed
Only change UART pin mode when needed
1 parent 86e574a commit 69d3a1b

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

applications/app_uartcomm.c

+18-14
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static stm32_gpio_t * TxGpioPort[UART_NUMBER];
6464
static stm32_gpio_t * RxGpioPort[UART_NUMBER];
6565
static uint8_t TxGpioPin[UART_NUMBER], RxGpioPin[UART_NUMBER], gpioAF[UART_NUMBER];
6666
static PACKET_STATE_t packet_state[UART_NUMBER];
67+
static bool pins_enabled[UART_NUMBER];
6768

6869
// Private functions
6970
static void process_packet(unsigned char *data, unsigned int len, unsigned int port_number);
@@ -108,8 +109,8 @@ void app_uartcomm_initialize(void) {
108109
serialPortDriverTx[0] = &HW_UART_DEV;
109110
serialPortDriverRx[0] = &HW_UART_DEV;
110111
uart_cfg[0].speed = BAUDRATE;
111-
RxGpioPort[0] = HW_UART_RX_PORT; RxGpioPin[0] = HW_UART_RX_PIN;
112-
TxGpioPort[0] = HW_UART_TX_PORT; TxGpioPin[0] = HW_UART_TX_PIN;
112+
RxGpioPort[0] = HW_UART_RX_PORT; RxGpioPin[0] = HW_UART_RX_PIN;
113+
TxGpioPort[0] = HW_UART_TX_PORT; TxGpioPin[0] = HW_UART_TX_PIN;
113114
gpioAF[0] = HW_UART_GPIO_AF;
114115

115116
#ifdef HW_UART_P_DEV
@@ -130,7 +131,7 @@ void app_uartcomm_initialize(void) {
130131
serialPortDriverRx[2] = &HW_UART_3_DEV;
131132
uart_cfg[2].speed = HW_UART_3_BAUD;
132133
RxGpioPort[2] = HW_UART_3_RX_PORT; RxGpioPin[2] = HW_UART_3_RX_PIN;
133-
TxGpioPort[2] = HW_UART_3_TX_PORT;TxGpioPin[2] = HW_UART_3_TX_PIN;
134+
TxGpioPort[2] = HW_UART_3_TX_PORT; TxGpioPin[2] = HW_UART_3_TX_PIN;
134135
gpioAF[2] = HW_UART_3_GPIO_AF;
135136
#endif
136137
}
@@ -156,6 +157,7 @@ void app_uartcomm_start(UART_PORT port_number) {
156157
PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP);
157158
palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) |
158159
PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP);
160+
pins_enabled[port_number] = true;
159161
}
160162

161163
void app_uartcomm_stop(UART_PORT port_number) {
@@ -199,18 +201,20 @@ void app_uartcomm_configure(uint32_t baudrate, bool enabled, UART_PORT port_numb
199201

200202
if (thread_is_running && uart_is_running[port_number]) {
201203
sdStart(serialPortDriverRx[port_number], &uart_cfg[port_number]);
202-
}
203204

204-
if (enabled) {
205-
palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) |
206-
PAL_STM32_OSPEED_HIGHEST |
207-
PAL_STM32_PUDR_PULLUP);
208-
palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) |
209-
PAL_STM32_OSPEED_HIGHEST |
210-
PAL_STM32_PUDR_PULLUP);
211-
} else {
212-
palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_INPUT);
213-
palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_INPUT);
205+
if (enabled && !pins_enabled[port_number]) {
206+
palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) |
207+
PAL_STM32_OSPEED_HIGHEST |
208+
PAL_STM32_PUDR_PULLUP);
209+
palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) |
210+
PAL_STM32_OSPEED_HIGHEST |
211+
PAL_STM32_PUDR_PULLUP);
212+
pins_enabled[port_number] = true;
213+
} else if (!enabled && pins_enabled[port_number]) {
214+
palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_INPUT);
215+
palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_INPUT);
216+
pins_enabled[port_number] = false;
217+
}
214218
}
215219
}
216220

0 commit comments

Comments
 (0)