Skip to content

Commit af1fab1

Browse files
authored
Merge pull request #2226 from kamtom480/circuitpython-device-open
Do not open the same PWM device if it is already open
2 parents 8f1421e + e4574fa commit af1fab1

File tree

7 files changed

+74
-34
lines changed

7 files changed

+74
-34
lines changed

ports/cxd56/common-hal/analogio/AnalogIn.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ void common_hal_analogio_analogin_construct(analogio_analogin_obj_t *self, const
6868
mp_raise_ValueError(translate("Pin does not have ADC capabilities"));
6969
}
7070

71-
analogin_dev[self->number].fd = open(analogin_dev[pin->number].devpath, O_RDONLY);
7271
if (analogin_dev[self->number].fd < 0) {
73-
mp_raise_ValueError(translate("Pin does not have ADC capabilities"));
72+
analogin_dev[self->number].fd = open(analogin_dev[self->number].devpath, O_RDONLY);
73+
if (analogin_dev[self->number].fd < 0) {
74+
mp_raise_ValueError(translate("Pin does not have ADC capabilities"));
75+
}
7476
}
7577

7678
// SCU FIFO overwrite
@@ -99,7 +101,7 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
99101
}
100102

101103
bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) {
102-
return self->pin == mp_const_none;
104+
return analogin_dev[self->number].fd < 0;
103105
}
104106

105107
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {

ports/cxd56/common-hal/analogio/AnalogIn.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
typedef struct {
3535
mp_obj_base_t base;
3636
const mcu_pin_obj_t *pin;
37-
uint8_t number;
37+
int8_t number;
3838
} analogio_analogin_obj_t;
3939

4040
void analogin_reset(void);

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

Lines changed: 55 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,15 @@ 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
}
190+
191+
void busio_uart_reset(void) {
192+
for (int i = 0; i < MP_ARRAY_SIZE(busio_uart_dev); i++) {
193+
if (busio_uart_dev[i].fd >= 0) {
194+
close(busio_uart_dev[i].fd);
195+
busio_uart_dev[i].fd = -1;
196+
}
197+
}
198+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
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;
4040
uint32_t timeout;
4141
} busio_uart_obj_t;
4242

43+
void busio_uart_reset(void);
44+
4345
#endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_BUSIO_UART_H

ports/cxd56/common-hal/pulseio/PWMOut.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t *self,
6262
return PWMOUT_INVALID_PIN;
6363
}
6464

65-
pwmout_dev[self->number].fd = open(pwmout_dev[self->number].devpath, O_RDONLY);
6665
if (pwmout_dev[self->number].fd < 0) {
67-
return PWMOUT_INVALID_PIN;
66+
pwmout_dev[self->number].fd = open(pwmout_dev[self->number].devpath, O_RDONLY);
67+
if (pwmout_dev[self->number].fd < 0) {
68+
return PWMOUT_INVALID_PIN;
69+
}
6870
}
6971

7072
self->info.frequency = frequency;
@@ -97,7 +99,7 @@ void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t *self) {
9799
}
98100

99101
bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t *self) {
100-
return self->pin == mp_const_none;
102+
return pwmout_dev[self->number].fd < 0;
101103
}
102104

103105
void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t *self, uint16_t duty) {

ports/cxd56/common-hal/pulseio/PWMOut.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef struct {
3838
const mcu_pin_obj_t *pin;
3939
struct pwm_info_s info;
4040
bool variable_frequency;
41-
uint8_t number;
41+
int8_t number;
4242
} pulseio_pwmout_obj_t;
4343

4444
void pwmout_reset(void);

ports/cxd56/supervisor/port.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "common-hal/analogio/AnalogIn.h"
3838
#include "common-hal/pulseio/PulseOut.h"
3939
#include "common-hal/pulseio/PWMOut.h"
40+
#include "common-hal/busio/UART.h"
4041

4142
safe_mode_t port_init(void) {
4243
boardctl(BOARDIOC_INIT, 0);
@@ -62,6 +63,9 @@ void reset_port(void) {
6263
pulseout_reset();
6364
pwmout_reset();
6465
#endif
66+
#if CIRCUITPY_BUSIO
67+
busio_uart_reset();
68+
#endif
6569

6670
reset_all_pins();
6771
}

0 commit comments

Comments
 (0)