/// \method any() /// Return `True` if any characters waiting, else `False`. STATIC mp_obj_t pyb_uart_any(mp_obj_t self_in) { pyb_uart_obj_t *self = self_in; if (uart_rx_any(self)) { return mp_const_true; } else { return mp_const_false; } }
int mp_hal_stdin_rx_chr(void) { for (;;) { byte c; if (usb_vcp_recv_byte(&c) != 0) { return c; } else if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) { return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart)); } __WFI(); } }
// Waits at most timeout milliseconds for at least 1 char to become ready for // reading (from buf or for direct reading). // Returns true if something available, false if not. STATIC bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout) { for (;;) { if (uart_rx_any(self)) { return true; // have at least 1 char ready for reading } if (timeout > 0) { HAL_Delay (1); timeout--; } else { return false; } } }
STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { pyb_uart_obj_t *self = self_in; mp_uint_t ret; if (request == MP_IOCTL_POLL) { mp_uint_t flags = arg; ret = 0; if ((flags & MP_IOCTL_POLL_RD) && uart_rx_any(self)) { ret |= MP_IOCTL_POLL_RD; } if ((flags & MP_IOCTL_POLL_WR) && MAP_UARTSpaceAvail(self->reg)) { ret |= MP_IOCTL_POLL_WR; } } else { *errcode = EINVAL; ret = MP_STREAM_ERROR; } return ret; }
int stdin_rx_chr(void) { for (;;) { #if 0 #ifdef USE_HOST_MODE pyb_usb_host_process(); int c = pyb_usb_host_get_keyboard(); if (c != 0) { return c; } #endif #endif byte c; if (usb_vcp_recv_byte(&c) != 0) { return c; } else if (pyb_stdio_uart != PYB_UART_NONE && uart_rx_any(pyb_stdio_uart)) { return uart_rx_char(pyb_stdio_uart); } __WFI(); } }
int mp_hal_stdin_rx_chr(void) { for (;;) { #if 0 #ifdef USE_HOST_MODE pyb_usb_host_process(); int c = pyb_usb_host_get_keyboard(); if (c != 0) { return c; } #endif #endif byte c; if (usb_vcp_recv_byte(&c) != 0) { return c; } else if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) { return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart)); } __WFI(); } }
mp_uint_t uart_ioctl(mp_obj_t self_in, mp_uint_t request, int *errcode, ...) { pyb_uart_obj_t *self = self_in; va_list vargs; va_start(vargs, errcode); mp_uint_t ret; if (request == MP_IOCTL_POLL) { mp_uint_t flags = va_arg(vargs, mp_uint_t); ret = 0; if ((flags & MP_IOCTL_POLL_RD) && uart_rx_any(self)) { ret |= MP_IOCTL_POLL_RD; } if ((flags & MP_IOCTL_POLL_WR) && __HAL_UART_GET_FLAG(&self->uart, UART_FLAG_TXE)) { ret |= MP_IOCTL_POLL_WR; } } else { *errcode = EINVAL; ret = -1; } va_end(vargs); return ret; }