Skip to content

Commit 9a6cc19

Browse files
committed
pbio/os: Generalize IRQ flags type.
Generalizes handling to work better for the virtual hub, which doesn't use uint32_t for the interrupt state.
1 parent d16e385 commit 9a6cc19

9 files changed

Lines changed: 40 additions & 28 deletions

File tree

bricks/_common_stm32/mphalport.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ void pb_stack_get_info(char **sstack, char **estack) {
2424
*estack = (char *)&_estack;
2525
}
2626

27-
uint32_t pbio_os_hook_disable_irq(void) {
27+
pbio_os_irq_flags_t pbio_os_hook_disable_irq(void) {
2828
mp_uint_t flags = __get_PRIMASK();
2929
__disable_irq();
3030
return flags;
3131
}
3232

33-
void pbio_os_hook_enable_irq(uint32_t flags) {
33+
void pbio_os_hook_enable_irq(pbio_os_irq_flags_t flags) {
3434
__set_PRIMASK(flags);
3535
}
3636

37-
void pbio_os_hook_wait_for_interrupt(void) {
37+
void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
3838
__WFI();
3939
}
4040

bricks/ev3/mphalport.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ void pb_stack_get_info(char **sstack, char **estack) {
2727
*estack = (char *)&_estack;
2828
}
2929

30-
uint32_t pbio_os_hook_disable_irq(void) {
30+
pbio_os_irq_flags_t pbio_os_hook_disable_irq(void) {
3131
return IntDisable();
3232
}
3333

34-
void pbio_os_hook_enable_irq(uint32_t flags) {
34+
void pbio_os_hook_enable_irq(pbio_os_irq_flags_t flags) {
3535
IntEnable(flags);
3636
}
3737

38-
void pbio_os_hook_wait_for_interrupt(void) {
38+
void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
3939
__asm volatile (
4040
"mov r0, #0\n"
4141
"mcr p15, 0, r0, c7, c0, 4\n" /* wait for interrupt */

bricks/nxt/mphalport.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
#include "py/mpconfig.h"
2121
#include "py/stream.h"
2222

23-
uint32_t pbio_os_hook_disable_irq(void) {
23+
pbio_os_irq_flags_t pbio_os_hook_disable_irq(void) {
2424
return nx_interrupts_disable();
2525
}
2626

27-
void pbio_os_hook_enable_irq(uint32_t flags) {
27+
void pbio_os_hook_enable_irq(pbio_os_irq_flags_t flags) {
2828
nx_interrupts_enable(flags);
2929
}
3030

31-
void pbio_os_hook_wait_for_interrupt(void) {
31+
void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
3232
// disable the processor clock which puts it in Idle Mode.
3333
AT91C_BASE_PMC->PMC_SCDR = AT91C_PMC_PCK;
3434
}

bricks/virtualhub/mp_port.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,29 @@ void pb_event_poll_hook(void) {
9494
pbio_os_run_while_idle();
9595
}
9696

97-
// This is used instead of the uint32_t flags used in embedded builds.
98-
static sigset_t global_origmask;
99-
100-
uint32_t pbio_os_hook_disable_irq(void) {
97+
pbio_os_irq_flags_t pbio_os_hook_disable_irq(void) {
10198
sigset_t sigmask;
10299
sigfillset(&sigmask);
103-
pthread_sigmask(SIG_SETMASK, &sigmask, &global_origmask);
104-
return 0;
100+
101+
sigset_t origmask;
102+
pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
103+
return origmask;
105104
}
106105

107-
void pbio_os_hook_enable_irq(uint32_t flags) {
108-
pthread_sigmask(SIG_SETMASK, &global_origmask, NULL);
106+
void pbio_os_hook_enable_irq(pbio_os_irq_flags_t flags) {
107+
sigset_t origmask = (sigset_t)flags;
108+
pthread_sigmask(SIG_SETMASK, &origmask, NULL);
109109
}
110110

111-
void pbio_os_hook_wait_for_interrupt(void) {
111+
void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
112112
struct timespec timeout = {
113113
.tv_sec = 0,
114114
.tv_nsec = 100000,
115115
};
116116
// "sleep" with "interrupts" enabled
117+
sigset_t origmask = flags;
117118
MP_THREAD_GIL_EXIT();
118-
pselect(0, NULL, NULL, NULL, &timeout, &global_origmask);
119+
pselect(0, NULL, NULL, NULL, &timeout, &origmask);
119120
MP_THREAD_GIL_ENTER();
120121
}
121122

lib/pbio/include/pbio/config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@
3030

3131
#define PBIO_CONFIG_NUM_DRIVEBASES (PBIO_CONFIG_SERVO_NUM_DEV / 2)
3232

33+
#ifndef PBIO_CONFIG_OS_IRQ_FLAGS_TYPE
34+
#include <stdint.h>
35+
#define PBIO_CONFIG_OS_IRQ_FLAGS_TYPE uint32_t
36+
#endif
37+
38+
typedef PBIO_CONFIG_OS_IRQ_FLAGS_TYPE pbio_os_irq_flags_t;
39+
3340
#endif // _PBIO_CONFIG_H_

lib/pbio/include/pbio/os.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
#include <stdbool.h>
6060
#include <stdint.h>
61+
#include <pbio/config.h>
6162
#include <pbio/error.h>
6263

6364
/**
@@ -252,7 +253,7 @@ struct _pbio_os_process_t {
252253

253254
/**
254255
* Yields the protothread until the specified timer expires.
255-
*
256+
*
256257
* @param [in] state Protothread state.
257258
* @param [in] timer The timer to check.
258259
* @param [in] duration The duration to wait for in milliseconds.
@@ -285,7 +286,7 @@ void pbio_os_process_init(pbio_os_process_t *process, pbio_os_process_func_t fun
285286
*
286287
* @return The previous interrupt state.
287288
*/
288-
uint32_t pbio_os_hook_disable_irq(void);
289+
pbio_os_irq_flags_t pbio_os_hook_disable_irq(void);
289290

290291
/**
291292
* Enables interrupts.
@@ -294,13 +295,13 @@ uint32_t pbio_os_hook_disable_irq(void);
294295
*
295296
* @param [in] flags The previous interrupt state.
296297
*/
297-
void pbio_os_hook_enable_irq(uint32_t flags);
298+
void pbio_os_hook_enable_irq(pbio_os_irq_flags_t flags);
298299

299300
/**
300301
* Waits for an interrupt.
301302
*
302303
* Must be implemented by the platform.
303304
*/
304-
void pbio_os_hook_wait_for_interrupt(void);
305+
void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags);
305306

306307
#endif // _PBIO_OS_H_

lib/pbio/platform/virtual_hub/pbioconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@
2727
#define PBIO_CONFIG_TACHO (1)
2828

2929
#define PBIO_CONFIG_ENABLE_SYS (1)
30+
31+
#include <signal.h>
32+
#define PBIO_CONFIG_OS_IRQ_FLAGS_TYPE sigset_t

lib/pbio/src/os.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void pbio_os_run_while_idle(void) {
160160
// poll_request_is_pending flag. If not, we can call wait_for_interrupt(),
161161
// which still wakes up the CPU on interrupt even though interrupts are
162162
// otherwise disabled.
163-
uint32_t irq_flags = pbio_os_hook_disable_irq();
163+
pbio_os_irq_flags_t irq_flags = pbio_os_hook_disable_irq();
164164

165165
// DELETEME: Legacy hook for pbio event loop that plays the same role as
166166
// the pending flag. Here it ensures we don't enter sleep if there are
@@ -171,7 +171,7 @@ void pbio_os_run_while_idle(void) {
171171
}
172172

173173
if (!poll_request_is_pending) {
174-
pbio_os_hook_wait_for_interrupt();
174+
pbio_os_hook_wait_for_interrupt(irq_flags);
175175
}
176176
pbio_os_hook_enable_irq(irq_flags);
177177

lib/pbio/test/test-pbio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ struct testcase_setup_t pbio_test_setup = {
143143
.cleanup_fn = cleanup,
144144
};
145145

146-
uint32_t pbio_os_hook_disable_irq(void) {
146+
pbio_os_irq_flags_t pbio_os_hook_disable_irq(void) {
147147
return 0;
148148
}
149149

150-
void pbio_os_hook_enable_irq(uint32_t flags) {
150+
void pbio_os_hook_enable_irq(pbio_os_irq_flags_t flags) {
151151
}
152152

153-
void pbio_os_hook_wait_for_interrupt(void) {
153+
void pbio_os_hook_wait_for_interrupt(pbio_os_irq_flags_t flags) {
154154
}
155155

156156
extern struct testcase_t pbdrv_bluetooth_tests[];

0 commit comments

Comments
 (0)