Skip to content

Commit a2f55fe

Browse files
committed
stmhal: Add polling ability to UART object.
1 parent 6c9c7bc commit a2f55fe

3 files changed

Lines changed: 43 additions & 10 deletions

File tree

stmhal/modselect.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,10 @@
3535
#include "qstr.h"
3636
#include "obj.h"
3737
#include "objlist.h"
38+
#include "pybioctl.h"
3839

3940
/// \moduleref select
4041

41-
#define MP_IOCTL_POLL (0x100 | 1)
42-
43-
#define MP_IOCTL_POLL_RD (0x0001)
44-
#define MP_IOCTL_POLL_WR (0x0002)
45-
#define MP_IOCTL_POLL_HUP (0x0004)
46-
#define MP_IOCTL_POLL_ERR (0x0008)
47-
4842
typedef struct _poll_obj_t {
4943
mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, int *errcode, ...);
5044
mp_uint_t flags;
@@ -85,13 +79,13 @@ STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map, mp_uint_t *rwx_num) {
8579
}
8680

8781
poll_obj_t *poll_obj = (poll_obj_t*)poll_map->table[i].value;
88-
int errno;
89-
mp_int_t ret = poll_obj->ioctl(poll_map->table[i].key, MP_IOCTL_POLL, &errno, poll_obj->flags);
82+
int errcode;
83+
mp_int_t ret = poll_obj->ioctl(poll_map->table[i].key, MP_IOCTL_POLL, &errcode, poll_obj->flags);
9084
poll_obj->flags_ret = ret;
9185

9286
if (ret == -1) {
9387
// error doing ioctl
94-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
88+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errcode)));
9589
}
9690

9791
if (ret != 0) {

stmhal/pybioctl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#define MP_IOCTL_POLL (0x100 | 1)
2+
3+
#define MP_IOCTL_POLL_RD (0x0001)
4+
#define MP_IOCTL_POLL_WR (0x0002)
5+
#define MP_IOCTL_POLL_HUP (0x0004)
6+
#define MP_IOCTL_POLL_ERR (0x0008)

stmhal/uart.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stdio.h>
2828
#include <string.h>
29+
#include <stdarg.h>
2930

3031
#include "stm32f4xx_hal.h"
3132

@@ -37,6 +38,7 @@
3738
#include "runtime.h"
3839
#include "bufhelper.h"
3940
#include "uart.h"
41+
#include "pybioctl.h"
4042

4143
/// \moduleref pyb
4244
/// \class UART - duplex serial communication bus
@@ -475,10 +477,41 @@ STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = {
475477

476478
STATIC MP_DEFINE_CONST_DICT(pyb_uart_locals_dict, pyb_uart_locals_dict_table);
477479

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+
478510
const mp_obj_type_t pyb_uart_type = {
479511
{ &mp_type_type },
480512
.name = MP_QSTR_UART,
481513
.print = pyb_uart_print,
482514
.make_new = pyb_uart_make_new,
515+
.stream_p = &uart_stream_p,
483516
.locals_dict = (mp_obj_t)&pyb_uart_locals_dict,
484517
};

0 commit comments

Comments
 (0)