8787/******************************************************************************
8888 DEFINE CONSTANTS
8989 ******************************************************************************/
90- #define PYBUART_TX_WAIT_MS 1
91- #define PYBUART_TX_MAX_TIMEOUT_MS 5
90+ #define PYBUART_TX_WAIT_MS 1
91+ #define PYBUART_TX_MAX_TIMEOUT_MS 5
9292
9393/******************************************************************************
9494 DECLARE PRIVATE FUNCTIONS
9595 ******************************************************************************/
9696STATIC void uart_init (pyb_uart_obj_t * self );
9797STATIC bool uart_rx_wait (pyb_uart_obj_t * self , uint32_t timeout );
98- STATIC pyb_uart_obj_t * pyb_uart_add (pyb_uart_id_t uart_id );
99- STATIC pyb_uart_obj_t * pyb_uart_find (pyb_uart_id_t uart_id );
10098STATIC void UARTGenericIntHandler (uint32_t uart_id );
10199STATIC void UART0IntHandler (void );
102100STATIC void UART1IntHandler (void );
103- STATIC mp_obj_t pyb_uart_deinit (mp_obj_t self_in );
104101
105102/******************************************************************************
106103 DEFINE PRIVATE TYPES
107104 ******************************************************************************/
108-
109105struct _pyb_uart_obj_t {
110106 mp_obj_base_t base ;
111107 pyb_uart_id_t uart_id ;
@@ -122,21 +118,15 @@ struct _pyb_uart_obj_t {
122118 bool enabled ;
123119};
124120
121+ /******************************************************************************
122+ DECLARE PRIVATE DATA
123+ ******************************************************************************/
124+ STATIC pyb_uart_obj_t pyb_uart_obj [PYB_NUM_UARTS ];
125+
125126/******************************************************************************
126127 DEFINE PUBLIC FUNCTIONS
127128 ******************************************************************************/
128129void uart_init0 (void ) {
129- mp_obj_list_init (& MP_STATE_PORT (pyb_uart_list ), 0 );
130- }
131-
132- // unregister all interrupt sources
133- void uart_deinit (void ) {
134- for (int i = PYB_UART_0 ; i < PYB_NUM_UARTS ; i ++ ) {
135- pyb_uart_obj_t * self ;
136- if ((self = pyb_uart_find (i ))) {
137- pyb_uart_deinit (self );
138- }
139- }
140130}
141131
142132bool uart_rx_any (pyb_uart_obj_t * self ) {
@@ -255,50 +245,27 @@ STATIC bool uart_rx_wait (pyb_uart_obj_t *self, uint32_t timeout) {
255245 }
256246}
257247
258- STATIC pyb_uart_obj_t * pyb_uart_add (pyb_uart_id_t uart_id ) {
259- // create a new uart object
260- pyb_uart_obj_t * self = m_new_obj (pyb_uart_obj_t );
261- self -> base .type = & pyb_uart_type ;
262- self -> uart_id = uart_id ;
263- self -> read_buf = NULL ;
264- self -> enabled = false;
265- // add it to the list
266- mp_obj_list_append (& MP_STATE_PORT (pyb_uart_list ), self );
267- return self ;
268- }
269-
270- STATIC pyb_uart_obj_t * pyb_uart_find (pyb_uart_id_t uart_id ) {
271- for (mp_uint_t i = 0 ; i < MP_STATE_PORT (pyb_uart_list ).len ; i ++ ) {
272- pyb_uart_obj_t * self = (pyb_uart_obj_t * )MP_STATE_PORT (pyb_uart_list ).items [i ];
273- if (self -> uart_id == uart_id ) {
274- return self ;
275- }
276- }
277- return NULL ;
278- }
279-
280248STATIC void UARTGenericIntHandler (uint32_t uart_id ) {
281249 pyb_uart_obj_t * self ;
282250 uint32_t status ;
283251
284- if ((self = pyb_uart_find (uart_id ))) {
285- status = MAP_UARTIntStatus (self -> reg , true);
286- // receive interrupt
287- if (status & (UART_INT_RX | UART_INT_RT )) {
288- MAP_UARTIntClear (self -> reg , UART_INT_RX | UART_INT_RT );
289- while (UARTCharsAvail (self -> reg )) {
290- int data = MAP_UARTCharGetNonBlocking (self -> reg );
291- if (MICROPY_STDIO_UART == self -> uart_id && data == user_interrupt_char ) {
292- // raise exception when interrupts are finished
293- mpexception_keyboard_nlr_jump ();
294- }
295- else if (self -> read_buf_len != 0 ) {
296- uint16_t next_head = (self -> read_buf_head + 1 ) % self -> read_buf_len ;
297- if (next_head != self -> read_buf_tail ) {
298- // only store data if room in buf
299- self -> read_buf [self -> read_buf_head ] = data ;
300- self -> read_buf_head = next_head ;
301- }
252+ self = & pyb_uart_obj [uart_id ];
253+ status = MAP_UARTIntStatus (self -> reg , true);
254+ // receive interrupt
255+ if (status & (UART_INT_RX | UART_INT_RT )) {
256+ MAP_UARTIntClear (self -> reg , UART_INT_RX | UART_INT_RT );
257+ while (UARTCharsAvail (self -> reg )) {
258+ int data = MAP_UARTCharGetNonBlocking (self -> reg );
259+ if (MICROPY_STDIO_UART == self -> uart_id && data == user_interrupt_char ) {
260+ // raise exception when interrupts are finished
261+ mpexception_keyboard_nlr_jump ();
262+ }
263+ else if (self -> read_buf_len != 0 ) {
264+ uint16_t next_head = (self -> read_buf_head + 1 ) % self -> read_buf_len ;
265+ if (next_head != self -> read_buf_tail ) {
266+ // only store data if room in buf
267+ self -> read_buf [self -> read_buf_head ] = data ;
268+ self -> read_buf_head = next_head ;
302269 }
303270 }
304271 }
@@ -469,13 +436,14 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
469436 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_resource_not_avaliable ));
470437 }
471438
472- // search for an object in the list
473- pyb_uart_obj_t * self ;
474- if (!(self = pyb_uart_find (uart_id ))) {
475- self = pyb_uart_add (uart_id );
476- }
477-
439+ // get the correct uart instance
440+ pyb_uart_obj_t * self = & pyb_uart_obj [uart_id ];
441+ self -> base .type = & pyb_uart_type ;
442+ self -> uart_id = uart_id ;
478443 if (n_args > 1 || n_kw > 0 ) {
444+ // invalidate the buffer and clear the enabled flag
445+ self -> read_buf = NULL ;
446+ self -> enabled = false;
479447 // start the peripheral
480448 mp_map_t kw_args ;
481449 mp_map_init_fixed_table (& kw_args , n_kw , args + n_args );
@@ -492,7 +460,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init);
492460
493461/// \method deinit()
494462/// Turn off the UART bus.
495- STATIC mp_obj_t pyb_uart_deinit (mp_obj_t self_in ) {
463+ mp_obj_t pyb_uart_deinit (mp_obj_t self_in ) {
496464 pyb_uart_obj_t * self = self_in ;
497465 uint uartPerh ;
498466
0 commit comments