32
32
#include <sys/time.h>
33
33
#include <sys/select.h>
34
34
#include <sys/ioctl.h>
35
- #include <arch/chip/pin.h>
36
35
#include <nuttx/serial/tioctl.h>
37
36
#include <nuttx/fs/ioctl.h>
38
37
42
41
43
42
#include "shared-bindings/busio/UART.h"
44
43
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
+
45
55
void common_hal_busio_uart_construct (busio_uart_obj_t * self ,
46
56
const mcu_pin_obj_t * tx , const mcu_pin_obj_t * rx , uint32_t baudrate ,
47
57
uint8_t bits , uart_parity_t parity , uint8_t stop , mp_float_t timeout ,
48
58
uint16_t receiver_buffer_size ) {
49
59
struct termios tio ;
50
60
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
-
61
61
if (bits != 8 ) {
62
62
mp_raise_ValueError (translate ("Could not initialize UART" ));
63
63
}
@@ -70,10 +70,32 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
70
70
mp_raise_ValueError (translate ("Could not initialize UART" ));
71
71
}
72
72
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 ) {
74
84
mp_raise_ValueError (translate ("Invalid pins" ));
75
85
}
76
86
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
+
77
99
claim_pin (tx );
78
100
claim_pin (rx );
79
101
@@ -88,15 +110,15 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
88
110
return ;
89
111
}
90
112
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 ;
93
115
94
116
reset_pin_number (self -> tx_pin -> number );
95
117
reset_pin_number (self -> rx_pin -> number );
96
118
}
97
119
98
120
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 ;
100
122
}
101
123
102
124
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
110
132
}
111
133
112
134
FD_ZERO (& rfds );
113
- FD_SET (self -> uart_fd , & rfds );
135
+ FD_SET (busio_uart_dev [ self -> number ]. fd , & rfds );
114
136
115
137
tv .tv_sec = 0 ;
116
138
tv .tv_usec = self -> timeout * 1000 ;
117
139
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 );
119
141
120
142
if (retval ) {
121
- bytes_read = read (self -> uart_fd , data , len );
143
+ bytes_read = read (busio_uart_dev [ self -> number ]. fd , data , len );
122
144
} else {
123
145
* errcode = EAGAIN ;
124
146
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
128
150
}
129
151
130
152
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 );
133
154
if (bytes_written < 0 ) {
134
155
* errcode = MP_EAGAIN ;
135
156
return MP_STREAM_ERROR ;
@@ -145,16 +166,16 @@ uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) {
145
166
void common_hal_busio_uart_set_baudrate (busio_uart_obj_t * self , uint32_t baudrate ) {
146
167
struct termios tio ;
147
168
148
- ioctl (self -> uart_fd , TCGETS , (long unsigned int )& tio );
169
+ ioctl (busio_uart_dev [ self -> number ]. fd , TCGETS , (long unsigned int )& tio );
149
170
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 );
152
173
}
153
174
154
175
uint32_t common_hal_busio_uart_rx_characters_available (busio_uart_obj_t * self ) {
155
176
int count = 0 ;
156
177
157
- ioctl (self -> uart_fd , FIONREAD , (long unsigned int )& count );
178
+ ioctl (busio_uart_dev [ self -> number ]. fd , FIONREAD , (long unsigned int )& count );
158
179
159
180
return count ;
160
181
}
@@ -163,6 +184,15 @@ void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self) {
163
184
}
164
185
165
186
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 );
167
188
return true;
168
189
}
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
+ }
0 commit comments