|
26 | 26 |
|
27 | 27 | #include <stdio.h> |
28 | 28 | #include <string.h> |
| 29 | +#include <stdarg.h> |
29 | 30 |
|
30 | 31 | #include "stm32f4xx_hal.h" |
31 | 32 |
|
|
37 | 38 | #include "runtime.h" |
38 | 39 | #include "bufhelper.h" |
39 | 40 | #include "uart.h" |
| 41 | +#include "pybioctl.h" |
40 | 42 |
|
41 | 43 | /// \moduleref pyb |
42 | 44 | /// \class UART - duplex serial communication bus |
@@ -475,10 +477,41 @@ STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = { |
475 | 477 |
|
476 | 478 | STATIC MP_DEFINE_CONST_DICT(pyb_uart_locals_dict, pyb_uart_locals_dict_table); |
477 | 479 |
|
| 480 | +mp_uint_t uart_ioctl(mp_obj_t self_in, mp_uint_t request, int *errcode, ...) { |
| 481 | + pyb_uart_obj_t *self = self_in; |
| 482 | + va_list vargs; |
| 483 | + va_start(vargs, errcode); |
| 484 | + mp_uint_t ret; |
| 485 | + if (request == MP_IOCTL_POLL) { |
| 486 | + mp_uint_t flags = va_arg(vargs, mp_uint_t); |
| 487 | + ret = 0; |
| 488 | + if (flags & MP_IOCTL_POLL_RD && uart_rx_any(self)) { |
| 489 | + ret |= MP_IOCTL_POLL_RD; |
| 490 | + } |
| 491 | + if (flags & MP_IOCTL_POLL_WR) { |
| 492 | + // TODO can we always write? |
| 493 | + ret |= MP_IOCTL_POLL_WR; |
| 494 | + } |
| 495 | + } else { |
| 496 | + *errcode = 1; // EPERM, operation not permitted |
| 497 | + ret = -1; |
| 498 | + } |
| 499 | + va_end(vargs); |
| 500 | + return ret; |
| 501 | +} |
| 502 | + |
| 503 | +STATIC const mp_stream_p_t uart_stream_p = { |
| 504 | + //.read = uart_read, // TODO |
| 505 | + //.write = uart_write, // TODO |
| 506 | + .ioctl = uart_ioctl, |
| 507 | + .is_text = false, |
| 508 | +}; |
| 509 | + |
478 | 510 | const mp_obj_type_t pyb_uart_type = { |
479 | 511 | { &mp_type_type }, |
480 | 512 | .name = MP_QSTR_UART, |
481 | 513 | .print = pyb_uart_print, |
482 | 514 | .make_new = pyb_uart_make_new, |
| 515 | + .stream_p = &uart_stream_p, |
483 | 516 | .locals_dict = (mp_obj_t)&pyb_uart_locals_dict, |
484 | 517 | }; |
0 commit comments