Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 52da273

Browse files
author
Islam Wahdan
authored
Merge pull request #21 from iwahdan88/master
esp32/machuart: Fix Issue #256
2 parents 82cd397 + 2cdc603 commit 52da273

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

esp32/mods/machuart.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
#define UART_TRIGGER_RX_FULL (0x04)
7272
#define UART_TRIGGER_TX_DONE (0x08)
7373

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+
7477
/******************************************************************************
7578
DECLARE PRIVATE FUNCTIONS
7679
******************************************************************************/
@@ -89,6 +92,7 @@ struct _mach_uart_obj_t {
8992
uint8_t uart_id;
9093
uint8_t rx_timeout;
9194
uint8_t n_pins;
95+
bool init;
9296
};
9397

9498
/******************************************************************************
@@ -265,6 +269,7 @@ STATIC bool uart_rx_wait (mach_uart_obj_t *self) {
265269

266270
STATIC void mach_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
267271
mach_uart_obj_t *self = self_in;
272+
MACH_UART_CHECK_INIT(self)
268273
if (self->config.baud_rate > 0) {
269274
mp_printf(print, "UART(%u, baudrate=%u, bits=", self->uart_id, self->config.baud_rate);
270275
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
430435
// configure the rx timeout threshold
431436
self->uart_reg->conf1.rx_tout_thrhd = self->rx_timeout & UART_RX_TOUT_THRHD_V;
432437

438+
// Init Done
439+
self->init = true;
440+
433441
return mp_const_none;
434442

435443
error:
@@ -494,25 +502,30 @@ STATIC mp_obj_t mach_uart_deinit(mp_obj_t self_in) {
494502
uart_driver_delete(self->uart_id);
495503
}
496504

505+
self->init = false;
506+
497507
return mp_const_none;
498508
}
499509
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mach_uart_deinit_obj, mach_uart_deinit);
500510

501511
STATIC mp_obj_t mach_uart_any(mp_obj_t self_in) {
502512
mach_uart_obj_t *self = self_in;
513+
MACH_UART_CHECK_INIT(self)
503514
return mp_obj_new_int(uart_rx_any(self));
504515
}
505516
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mach_uart_any_obj, mach_uart_any);
506517

507518
STATIC mp_obj_t mach_uart_wait_tx_done(mp_obj_t self_in, mp_obj_t timeout_ms) {
508519
mach_uart_obj_t *self = self_in;
520+
MACH_UART_CHECK_INIT(self)
509521
TickType_t timeout_ticks = mp_obj_get_int_truncated(timeout_ms) / portTICK_PERIOD_MS;
510522
return uart_wait_tx_done(self->uart_id, timeout_ticks) == ESP_OK ? mp_const_true : mp_const_false;
511523
}
512524
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mach_uart_wait_tx_done_obj, mach_uart_wait_tx_done);
513525

514526
STATIC mp_obj_t mach_uart_sendbreak(mp_obj_t self_in, mp_obj_t bits) {
515527
mach_uart_obj_t *self = self_in;
528+
MACH_UART_CHECK_INIT(self)
516529
pin_obj_t * pin = (pin_obj_t *)((mp_obj_t *)self->pins)[0];
517530

518531
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);
571584

572585
STATIC mp_uint_t mach_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
573586
mach_uart_obj_t *self = self_in;
587+
MACH_UART_CHECK_INIT(self)
574588
byte *buf = buf_in;
575589

576590
// 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,
598612

599613
STATIC mp_uint_t mach_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
600614
mach_uart_obj_t *self = self_in;
615+
MACH_UART_CHECK_INIT(self)
601616
const char *buf = buf_in;
602617

603618
// 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
609624

610625
STATIC mp_uint_t mach_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
611626
mach_uart_obj_t *self = self_in;
627+
MACH_UART_CHECK_INIT(self)
612628
mp_uint_t ret;
613629

614630
if (request == MP_STREAM_POLL) {

0 commit comments

Comments
 (0)