7575#define UART_TRIGGER_RX_FULL (0x04)
7676#define UART_TRIGGER_TX_DONE (0x08)
7777
78+ #define MACH_UART_CHECK_INIT (self ) \
79+ if(!(self->init)) {nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "UART not Initialized!"));}
80+
7881/******************************************************************************
7982 DECLARE PRIVATE FUNCTIONS
8083 ******************************************************************************/
@@ -93,6 +96,7 @@ struct _mach_uart_obj_t {
9396 uint8_t uart_id ;
9497 uint8_t rx_timeout ;
9598 uint8_t n_pins ;
99+ bool init ;
96100};
97101
98102/******************************************************************************
@@ -269,6 +273,7 @@ STATIC bool uart_rx_wait (mach_uart_obj_t *self) {
269273
270274STATIC void mach_uart_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
271275 mach_uart_obj_t * self = self_in ;
276+ MACH_UART_CHECK_INIT (self )
272277 if (self -> config .baud_rate > 0 ) {
273278 mp_printf (print , "UART(%u, baudrate=%u, bits=" , self -> uart_id , self -> config .baud_rate );
274279 switch (self -> config .data_bits ) {
@@ -434,6 +439,9 @@ STATIC mp_obj_t mach_uart_init_helper(mach_uart_obj_t *self, const mp_arg_val_t
434439 // configure the rx timeout threshold
435440 self -> uart_reg -> conf1 .rx_tout_thrhd = self -> rx_timeout & UART_RX_TOUT_THRHD_V ;
436441
442+ // Init Done
443+ self -> init = true;
444+
437445 return mp_const_none ;
438446
439447error :
@@ -498,25 +506,30 @@ STATIC mp_obj_t mach_uart_deinit(mp_obj_t self_in) {
498506 uart_driver_delete (self -> uart_id );
499507 }
500508
509+ self -> init = false;
510+
501511 return mp_const_none ;
502512}
503513STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mach_uart_deinit_obj , mach_uart_deinit );
504514
505515STATIC mp_obj_t mach_uart_any (mp_obj_t self_in ) {
506516 mach_uart_obj_t * self = self_in ;
517+ MACH_UART_CHECK_INIT (self )
507518 return mp_obj_new_int (uart_rx_any (self ));
508519}
509520STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mach_uart_any_obj , mach_uart_any );
510521
511522STATIC mp_obj_t mach_uart_wait_tx_done (mp_obj_t self_in , mp_obj_t timeout_ms ) {
512523 mach_uart_obj_t * self = self_in ;
524+ MACH_UART_CHECK_INIT (self )
513525 TickType_t timeout_ticks = mp_obj_get_int_truncated (timeout_ms ) / portTICK_PERIOD_MS ;
514526 return uart_wait_tx_done (self -> uart_id , timeout_ticks ) == ESP_OK ? mp_const_true : mp_const_false ;
515527}
516528STATIC MP_DEFINE_CONST_FUN_OBJ_2 (mach_uart_wait_tx_done_obj , mach_uart_wait_tx_done );
517529
518530STATIC mp_obj_t mach_uart_sendbreak (mp_obj_t self_in , mp_obj_t bits ) {
519531 mach_uart_obj_t * self = self_in ;
532+ MACH_UART_CHECK_INIT (self )
520533 pin_obj_t * pin = (pin_obj_t * )((mp_obj_t * )self -> pins )[0 ];
521534
522535 uint32_t isrmask = MICROPY_BEGIN_ATOMIC_SECTION ();
@@ -576,6 +589,7 @@ STATIC MP_DEFINE_CONST_DICT(mach_uart_locals_dict, mach_uart_locals_dict_table);
576589
577590STATIC mp_uint_t mach_uart_read (mp_obj_t self_in , void * buf_in , mp_uint_t size , int * errcode ) {
578591 mach_uart_obj_t * self = self_in ;
592+ MACH_UART_CHECK_INIT (self )
579593 byte * buf = buf_in ;
580594
581595 // make sure we want at least 1 char
@@ -603,6 +617,7 @@ STATIC mp_uint_t mach_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size,
603617
604618STATIC mp_uint_t mach_uart_write (mp_obj_t self_in , const void * buf_in , mp_uint_t size , int * errcode ) {
605619 mach_uart_obj_t * self = self_in ;
620+ MACH_UART_CHECK_INIT (self )
606621 const char * buf = buf_in ;
607622
608623 // write the data
@@ -614,6 +629,7 @@ STATIC mp_uint_t mach_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t
614629
615630STATIC mp_uint_t mach_uart_ioctl (mp_obj_t self_in , mp_uint_t request , mp_uint_t arg , int * errcode ) {
616631 mach_uart_obj_t * self = self_in ;
632+ MACH_UART_CHECK_INIT (self )
617633 mp_uint_t ret ;
618634
619635 if (request == MP_IOCTL_POLL ) {
0 commit comments