71
71
#define UART_TRIGGER_RX_FULL (0x04)
72
72
#define UART_TRIGGER_TX_DONE (0x08)
73
73
74
+ #define MACH_UART_CHECK_INIT (self ) \
75
+ if(!(self->init)) {nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "UART not Initialized!"));}
76
+
74
77
/******************************************************************************
75
78
DECLARE PRIVATE FUNCTIONS
76
79
******************************************************************************/
@@ -89,6 +92,7 @@ struct _mach_uart_obj_t {
89
92
uint8_t uart_id ;
90
93
uint8_t rx_timeout ;
91
94
uint8_t n_pins ;
95
+ bool init ;
92
96
};
93
97
94
98
/******************************************************************************
@@ -265,6 +269,7 @@ STATIC bool uart_rx_wait (mach_uart_obj_t *self) {
265
269
266
270
STATIC void mach_uart_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
267
271
mach_uart_obj_t * self = self_in ;
272
+ MACH_UART_CHECK_INIT (self )
268
273
if (self -> config .baud_rate > 0 ) {
269
274
mp_printf (print , "UART(%u, baudrate=%u, bits=" , self -> uart_id , self -> config .baud_rate );
270
275
switch (self -> config .data_bits ) {
@@ -430,6 +435,9 @@ STATIC mp_obj_t mach_uart_init_helper(mach_uart_obj_t *self, const mp_arg_val_t
430
435
// configure the rx timeout threshold
431
436
self -> uart_reg -> conf1 .rx_tout_thrhd = self -> rx_timeout & UART_RX_TOUT_THRHD_V ;
432
437
438
+ // Init Done
439
+ self -> init = true;
440
+
433
441
return mp_const_none ;
434
442
435
443
error :
@@ -494,25 +502,30 @@ STATIC mp_obj_t mach_uart_deinit(mp_obj_t self_in) {
494
502
uart_driver_delete (self -> uart_id );
495
503
}
496
504
505
+ self -> init = false;
506
+
497
507
return mp_const_none ;
498
508
}
499
509
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mach_uart_deinit_obj , mach_uart_deinit );
500
510
501
511
STATIC mp_obj_t mach_uart_any (mp_obj_t self_in ) {
502
512
mach_uart_obj_t * self = self_in ;
513
+ MACH_UART_CHECK_INIT (self )
503
514
return mp_obj_new_int (uart_rx_any (self ));
504
515
}
505
516
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mach_uart_any_obj , mach_uart_any );
506
517
507
518
STATIC mp_obj_t mach_uart_wait_tx_done (mp_obj_t self_in , mp_obj_t timeout_ms ) {
508
519
mach_uart_obj_t * self = self_in ;
520
+ MACH_UART_CHECK_INIT (self )
509
521
TickType_t timeout_ticks = mp_obj_get_int_truncated (timeout_ms ) / portTICK_PERIOD_MS ;
510
522
return uart_wait_tx_done (self -> uart_id , timeout_ticks ) == ESP_OK ? mp_const_true : mp_const_false ;
511
523
}
512
524
STATIC MP_DEFINE_CONST_FUN_OBJ_2 (mach_uart_wait_tx_done_obj , mach_uart_wait_tx_done );
513
525
514
526
STATIC mp_obj_t mach_uart_sendbreak (mp_obj_t self_in , mp_obj_t bits ) {
515
527
mach_uart_obj_t * self = self_in ;
528
+ MACH_UART_CHECK_INIT (self )
516
529
pin_obj_t * pin = (pin_obj_t * )((mp_obj_t * )self -> pins )[0 ];
517
530
518
531
uint32_t isrmask = MICROPY_BEGIN_ATOMIC_SECTION ();
@@ -571,6 +584,7 @@ STATIC MP_DEFINE_CONST_DICT(mach_uart_locals_dict, mach_uart_locals_dict_table);
571
584
572
585
STATIC mp_uint_t mach_uart_read (mp_obj_t self_in , void * buf_in , mp_uint_t size , int * errcode ) {
573
586
mach_uart_obj_t * self = self_in ;
587
+ MACH_UART_CHECK_INIT (self )
574
588
byte * buf = buf_in ;
575
589
576
590
// make sure we want at least 1 char
@@ -598,6 +612,7 @@ STATIC mp_uint_t mach_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size,
598
612
599
613
STATIC mp_uint_t mach_uart_write (mp_obj_t self_in , const void * buf_in , mp_uint_t size , int * errcode ) {
600
614
mach_uart_obj_t * self = self_in ;
615
+ MACH_UART_CHECK_INIT (self )
601
616
const char * buf = buf_in ;
602
617
603
618
// write the data
@@ -609,6 +624,7 @@ STATIC mp_uint_t mach_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t
609
624
610
625
STATIC mp_uint_t mach_uart_ioctl (mp_obj_t self_in , mp_uint_t request , mp_uint_t arg , int * errcode ) {
611
626
mach_uart_obj_t * self = self_in ;
627
+ MACH_UART_CHECK_INIT (self )
612
628
mp_uint_t ret ;
613
629
614
630
if (request == MP_STREAM_POLL ) {
0 commit comments