Skip to content

Commit 3ad13e1

Browse files
committed
Do not open the same UART device again
1 parent b39ca3f commit 3ad13e1

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

ports/cxd56/common-hal/busio/UART.c

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <sys/time.h>
3333
#include <sys/select.h>
3434
#include <sys/ioctl.h>
35-
#include <arch/chip/pin.h>
3635
#include <nuttx/serial/tioctl.h>
3736
#include <nuttx/fs/ioctl.h>
3837

@@ -42,22 +41,23 @@
4241

4342
#include "shared-bindings/busio/UART.h"
4443

44+
typedef struct {
45+
const char* devpath;
46+
const mcu_pin_obj_t *tx;
47+
const mcu_pin_obj_t *rx;
48+
int fd;
49+
} busio_uart_dev_t;
50+
51+
STATIC busio_uart_dev_t busio_uart_dev[] = {
52+
{"/dev/ttyS2", &pin_UART2_TXD, &pin_UART2_RXD, -1},
53+
};
54+
4555
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
4656
const mcu_pin_obj_t *tx, const mcu_pin_obj_t *rx, uint32_t baudrate,
4757
uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout,
4858
uint16_t receiver_buffer_size) {
4959
struct termios tio;
5060

51-
self->uart_fd = open("/dev/ttyS2", O_RDWR);
52-
if (self->uart_fd < 0) {
53-
mp_raise_ValueError(translate("Could not initialize UART"));
54-
}
55-
56-
ioctl(self->uart_fd, TCGETS, (long unsigned int)&tio);
57-
tio.c_speed = baudrate;
58-
ioctl(self->uart_fd, TCSETS, (long unsigned int)&tio);
59-
ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL);
60-
6161
if (bits != 8) {
6262
mp_raise_ValueError(translate("Could not initialize UART"));
6363
}
@@ -70,10 +70,32 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
7070
mp_raise_ValueError(translate("Could not initialize UART"));
7171
}
7272

73-
if (tx->number != PIN_UART2_TXD || rx->number != PIN_UART2_RXD) {
73+
self->number = -1;
74+
75+
for (int i = 0; i < MP_ARRAY_SIZE(busio_uart_dev); i++) {
76+
if (tx->number == busio_uart_dev[i].tx->number &&
77+
rx->number == busio_uart_dev[i].rx->number) {
78+
self->number = i;
79+
break;
80+
}
81+
}
82+
83+
if (self->number < 0) {
7484
mp_raise_ValueError(translate("Invalid pins"));
7585
}
7686

87+
if (busio_uart_dev[self->number].fd < 0) {
88+
busio_uart_dev[self->number].fd = open(busio_uart_dev[self->number].devpath, O_RDWR);
89+
if (busio_uart_dev[self->number].fd < 0) {
90+
mp_raise_ValueError(translate("Could not initialize UART"));
91+
}
92+
}
93+
94+
ioctl(busio_uart_dev[self->number].fd, TCGETS, (long unsigned int)&tio);
95+
tio.c_speed = baudrate;
96+
ioctl(busio_uart_dev[self->number].fd, TCSETS, (long unsigned int)&tio);
97+
ioctl(busio_uart_dev[self->number].fd, TCFLSH, (long unsigned int)NULL);
98+
7799
claim_pin(tx);
78100
claim_pin(rx);
79101

@@ -88,15 +110,15 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
88110
return;
89111
}
90112

91-
close(self->uart_fd);
92-
self->uart_fd = -1;
113+
close(busio_uart_dev[self->number].fd);
114+
busio_uart_dev[self->number].fd = -1;
93115

94116
reset_pin_number(self->tx_pin->number);
95117
reset_pin_number(self->rx_pin->number);
96118
}
97119

98120
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
99-
return self->uart_fd < 0;
121+
return busio_uart_dev[self->number].fd < 0;
100122
}
101123

102124
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {
@@ -110,15 +132,15 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
110132
}
111133

112134
FD_ZERO(&rfds);
113-
FD_SET(self->uart_fd, &rfds);
135+
FD_SET(busio_uart_dev[self->number].fd, &rfds);
114136

115137
tv.tv_sec = 0;
116138
tv.tv_usec = self->timeout * 1000;
117139

118-
retval = select(self->uart_fd + 1, &rfds, NULL, NULL, &tv);
140+
retval = select(busio_uart_dev[self->number].fd + 1, &rfds, NULL, NULL, &tv);
119141

120142
if (retval) {
121-
bytes_read = read(self->uart_fd, data, len);
143+
bytes_read = read(busio_uart_dev[self->number].fd, data, len);
122144
} else {
123145
*errcode = EAGAIN;
124146
return MP_STREAM_ERROR;
@@ -128,8 +150,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
128150
}
129151

130152
size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
131-
int bytes_written = write(self->uart_fd, data, len);
132-
153+
int bytes_written = write(busio_uart_dev[self->number].fd, data, len);
133154
if (bytes_written < 0) {
134155
*errcode = MP_EAGAIN;
135156
return MP_STREAM_ERROR;
@@ -145,16 +166,16 @@ uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) {
145166
void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t baudrate) {
146167
struct termios tio;
147168

148-
ioctl(self->uart_fd, TCGETS, (long unsigned int)&tio);
169+
ioctl(busio_uart_dev[self->number].fd, TCGETS, (long unsigned int)&tio);
149170
tio.c_speed = baudrate;
150-
ioctl(self->uart_fd, TCSETS, (long unsigned int)&tio);
151-
ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL);
171+
ioctl(busio_uart_dev[self->number].fd, TCSETS, (long unsigned int)&tio);
172+
ioctl(busio_uart_dev[self->number].fd, TCFLSH, (long unsigned int)NULL);
152173
}
153174

154175
uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self) {
155176
int count = 0;
156177

157-
ioctl(self->uart_fd, FIONREAD, (long unsigned int)&count);
178+
ioctl(busio_uart_dev[self->number].fd, FIONREAD, (long unsigned int)&count);
158179

159180
return count;
160181
}
@@ -163,6 +184,6 @@ void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) {
163184
}
164185

165186
bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self) {
166-
ioctl(self->uart_fd, TCFLSH, (long unsigned int)NULL);
187+
ioctl(busio_uart_dev[self->number].fd, TCFLSH, (long unsigned int)NULL);
167188
return true;
168189
}

ports/cxd56/common-hal/busio/UART.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
typedef struct {
3535
mp_obj_base_t base;
36-
int uart_fd;
36+
int8_t number;
3737
const mcu_pin_obj_t *tx_pin;
3838
const mcu_pin_obj_t *rx_pin;
3939
uint32_t baudrate;

0 commit comments

Comments
 (0)