Skip to content

Commit eacb7db

Browse files
committed
Don't block serial output in interrupt
The interrupt may have a higher priority than the serial output's (USB) interrupt and may never make room. This makes prints from interrupts (like the BLE event calls) best effort for what can be queued up. The rest of the output will be dropped.
1 parent ebfe36c commit eacb7db

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

supervisor/shared/cpu.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdint.h>
28+
29+
#include "supervisor/shared/cpu.h"
30+
31+
bool cpu_interrupt_active(void) {
32+
#if defined(__ARM_ARCH) && (__ARM_ARCH >= 6)
33+
// Check VECTACTIVE in ICSR. We don't need to disable interrupts because if
34+
// one occurs after we read, we won't continue until it is resolved.
35+
return (*((volatile uint32_t *)0xE000ED04) & 0x1ff) != 0;
36+
#else
37+
// We don't know.
38+
return false;
39+
#endif
40+
}

supervisor/shared/cpu.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SUPERVISOR_SHARED_CPU_H
28+
#define MICROPY_INCLUDED_SUPERVISOR_SHARED_CPU_H
29+
30+
#include <stdbool.h>
31+
#include <stddef.h>
32+
33+
// True when we're in an interrupt handler.
34+
bool cpu_interrupt_active(void);
35+
36+
#endif // MICROPY_INCLUDED_SUPERVISOR_SHARED_CPU_H

supervisor/shared/serial.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "py/mpconfig.h"
3030

31+
#include "supervisor/shared/cpu.h"
3132
#include "supervisor/shared/display.h"
3233
#include "shared-bindings/terminalio/Terminal.h"
3334
#include "supervisor/serial.h"
@@ -136,6 +137,10 @@ void serial_write_substring(const char *text, uint32_t length) {
136137
uint32_t count = 0;
137138
while (count < length && tud_cdc_connected()) {
138139
count += tud_cdc_write(text + count, length - count);
140+
// If we're in an interrupt, then don't wait for more room. Queue up what we can.
141+
if (cpu_interrupt_active()) {
142+
break;
143+
}
139144
usb_background();
140145
}
141146

supervisor/supervisor.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ SRC_SUPERVISOR = \
44
supervisor/shared/autoreload.c \
55
supervisor/shared/background_callback.c \
66
supervisor/shared/board.c \
7+
supervisor/shared/cpu.c \
78
supervisor/shared/filesystem.c \
89
supervisor/shared/flash.c \
910
supervisor/shared/micropython.c \

0 commit comments

Comments
 (0)