75
75
#define UART_TRIGGER_RX_FULL (0x04)
76
76
#define UART_TRIGGER_TX_DONE (0x08)
77
77
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
+
78
81
/******************************************************************************
79
82
DECLARE PRIVATE FUNCTIONS
80
83
******************************************************************************/
@@ -93,6 +96,7 @@ struct _mach_uart_obj_t {
93
96
uint8_t uart_id ;
94
97
uint8_t rx_timeout ;
95
98
uint8_t n_pins ;
99
+ bool init ;
96
100
};
97
101
98
102
/******************************************************************************
@@ -269,6 +273,7 @@ STATIC bool uart_rx_wait (mach_uart_obj_t *self) {
269
273
270
274
STATIC void mach_uart_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
271
275
mach_uart_obj_t * self = self_in ;
276
+ MACH_UART_CHECK_INIT (self )
272
277
if (self -> config .baud_rate > 0 ) {
273
278
mp_printf (print , "UART(%u, baudrate=%u, bits=" , self -> uart_id , self -> config .baud_rate );
274
279
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
434
439
// configure the rx timeout threshold
435
440
self -> uart_reg -> conf1 .rx_tout_thrhd = self -> rx_timeout & UART_RX_TOUT_THRHD_V ;
436
441
442
+ // Init Done
443
+ self -> init = true;
444
+
437
445
return mp_const_none ;
438
446
439
447
error :
@@ -498,25 +506,30 @@ STATIC mp_obj_t mach_uart_deinit(mp_obj_t self_in) {
498
506
uart_driver_delete (self -> uart_id );
499
507
}
500
508
509
+ self -> init = false;
510
+
501
511
return mp_const_none ;
502
512
}
503
513
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mach_uart_deinit_obj , mach_uart_deinit );
504
514
505
515
STATIC mp_obj_t mach_uart_any (mp_obj_t self_in ) {
506
516
mach_uart_obj_t * self = self_in ;
517
+ MACH_UART_CHECK_INIT (self )
507
518
return mp_obj_new_int (uart_rx_any (self ));
508
519
}
509
520
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mach_uart_any_obj , mach_uart_any );
510
521
511
522
STATIC mp_obj_t mach_uart_wait_tx_done (mp_obj_t self_in , mp_obj_t timeout_ms ) {
512
523
mach_uart_obj_t * self = self_in ;
524
+ MACH_UART_CHECK_INIT (self )
513
525
TickType_t timeout_ticks = mp_obj_get_int_truncated (timeout_ms ) / portTICK_PERIOD_MS ;
514
526
return uart_wait_tx_done (self -> uart_id , timeout_ticks ) == ESP_OK ? mp_const_true : mp_const_false ;
515
527
}
516
528
STATIC MP_DEFINE_CONST_FUN_OBJ_2 (mach_uart_wait_tx_done_obj , mach_uart_wait_tx_done );
517
529
518
530
STATIC mp_obj_t mach_uart_sendbreak (mp_obj_t self_in , mp_obj_t bits ) {
519
531
mach_uart_obj_t * self = self_in ;
532
+ MACH_UART_CHECK_INIT (self )
520
533
pin_obj_t * pin = (pin_obj_t * )((mp_obj_t * )self -> pins )[0 ];
521
534
522
535
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);
576
589
577
590
STATIC mp_uint_t mach_uart_read (mp_obj_t self_in , void * buf_in , mp_uint_t size , int * errcode ) {
578
591
mach_uart_obj_t * self = self_in ;
592
+ MACH_UART_CHECK_INIT (self )
579
593
byte * buf = buf_in ;
580
594
581
595
// 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,
603
617
604
618
STATIC mp_uint_t mach_uart_write (mp_obj_t self_in , const void * buf_in , mp_uint_t size , int * errcode ) {
605
619
mach_uart_obj_t * self = self_in ;
620
+ MACH_UART_CHECK_INIT (self )
606
621
const char * buf = buf_in ;
607
622
608
623
// 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
614
629
615
630
STATIC mp_uint_t mach_uart_ioctl (mp_obj_t self_in , mp_uint_t request , mp_uint_t arg , int * errcode ) {
616
631
mach_uart_obj_t * self = self_in ;
632
+ MACH_UART_CHECK_INIT (self )
617
633
mp_uint_t ret ;
618
634
619
635
if (request == MP_IOCTL_POLL ) {
0 commit comments