-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathmp_port.c
More file actions
157 lines (121 loc) · 3.71 KB
/
mp_port.c
File metadata and controls
157 lines (121 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-License-Identifier: MIT
// Copyright (c) 2022-2023 The Pybricks Authors
// MicroPython port-specific implementation hooks
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <unistd.h>
#include <contiki.h>
#include "pbio_os_config.h"
#include <pbio/main.h>
#include <pbio/os.h>
#include <pbsys/core.h>
#include <pbsys/program_stop.h>
#include <pbsys/status.h>
#include "py/mphal.h"
#include "py/mpconfig.h"
#include "py/obj.h"
#include "py/objexcept.h"
#include "py/objstr.h"
#include "py/objtuple.h"
#include "py/runtime.h"
#include "py/stream.h"
#include "pybricks/util_pb/pb_error.h"
#include <pybricks/common.h>
// from micropython/ports/unix/main.c
#define FORCED_EXIT (0x100)
// callback for when stop button is pressed in IDE or on hub
void pbsys_main_stop_program(bool force_stop) {
static const mp_rom_obj_tuple_t args = {
.base = { .type = &mp_type_tuple },
.len = 2,
.items = {
// NB: currently, first arg has to be FORCED_EXIT for default
// unix unhandled exception handler.
// https://github.com/micropython/micropython/pull/8151
MP_ROM_INT(FORCED_EXIT),
MP_ROM_QSTR(MP_QSTR_stop_space_button_space_pressed),
},
};
static mp_obj_exception_t system_exit;
// Schedule SystemExit exception.
system_exit.base.type = &mp_type_SystemExit;
system_exit.traceback_alloc = 0;
system_exit.traceback_len = 0;
system_exit.traceback_data = NULL;
system_exit.args = (mp_obj_tuple_t *)&args;
mp_sched_exception(MP_OBJ_FROM_PTR(&system_exit));
}
bool pbsys_main_stdin_event(uint8_t c) {
return false;
}
// MICROPY_PORT_INIT_FUNC
void pb_virtualhub_port_init(void) {
pbio_init(true);
pbsys_init();
pbsys_status_set(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING);
while (pbio_os_run_processes_once()) {
}
pb_package_pybricks_init(true);
}
// MICROPY_PORT_DEINIT_FUNC
void pb_virtualhub_port_deinit(void) {
pb_package_pybricks_deinit();
}
// Implementation for MICROPY_EVENT_POLL_HOOK
void pb_event_poll_hook(void) {
while (pbio_os_run_processes_once()) {
}
mp_handle_pending(true);
pbio_os_run_processes_and_wait_for_event();
}
pbio_os_irq_flags_t pbio_os_hook_disable_irq(void) {
sigset_t sigmask;
sigfillset(&sigmask);
sigset_t origmask;
pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
return origmask;
}
void pbio_os_hook_enable_irq(pbio_os_irq_flags_t flags) {
sigset_t origmask = (sigset_t)flags;
pthread_sigmask(SIG_SETMASK, &origmask, NULL);
}
void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
struct timespec timeout = {
.tv_sec = 0,
.tv_nsec = 100000,
};
// "sleep" with "interrupts" enabled
sigset_t origmask = flags;
MP_THREAD_GIL_EXIT();
pselect(0, NULL, NULL, NULL, &timeout, &origmask);
MP_THREAD_GIL_ENTER();
}
void pb_virtualhub_delay_us(mp_uint_t us) {
mp_uint_t start = mp_hal_ticks_us();
while (mp_hal_ticks_us() - start < us) {
MICROPY_VM_HOOK_LOOP;
}
}
uintptr_t mp_hal_stdio_poll(uintptr_t flags) {
struct pollfd fds[] = {
{ .fd = STDIN_FILENO, .events = flags & MP_STREAM_POLL_RD ? POLLIN : 0, },
{ .fd = STDOUT_FILENO, .events = flags & MP_STREAM_POLL_WR ? POLLOUT : 0, },
};
int ret;
MP_HAL_RETRY_SYSCALL(ret, poll(fds, MP_ARRAY_SIZE(fds), 0), mp_raise_OSError(err));
uintptr_t rflags = 0;
if (ret > 0) {
if (fds[0].revents & POLLIN) {
rflags |= MP_STREAM_POLL_RD;
}
if (fds[1].revents & POLLOUT) {
rflags |= MP_STREAM_POLL_WR;
}
}
return rflags;
}