8000 Merge branch 'micropython:master' into master · micropython/micropython@a0e043a · GitHub
[go: up one dir, main page]

Skip to content

Commit a0e043a

Browse files
Merge branch 'micropython:master' into master
2 parents 9c71d71 + 41ed01f commit a0e043a

File tree

88 files changed

+427
-258
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+427
-258
lines changed

.git-blame-ignore-revs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#all: Reformat remaining C code that doesn't have a space after a comma.
1+
# top: Update Python formatting to black "2023 stable style".
2+
8b2748269244304854b3462cb8902952b4dcb892
3+
4+
# all: Reformat remaining C code that doesn't have a space after a comma.
25
5b700b0af90591d6b1a2c087bb8de6b7f1bfdd2d
36

47
# ports: Reformat more C and Python source code.

examples/SDdatalogger/datalogger.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
# loop
1212
while True:
13-
1413
# wait for interrupt
1514
# this reduces power consumption while waiting for switch press
1615
pyb.wfi()

examples/asmsum.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
@micropython.asm_thumb
22
def asm_sum_words(r0, r1):
3-
43
# r0 = len
54
# r1 = ptr
65
# r2 = sum
@@ -25,7 +24,6 @@ def asm_sum_words(r0, r1):
2524

2625
@micropython.asm_thumb
2726
def asm_sum_bytes(r0, r1):
28-
2927
# r0 = len
3028
# r1 = ptr
3129
# r2 = sum

examples/bluetooth/ble_uart_repl.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
else:
2121
_timer = None
2222

23+
2324
# Batch writes into 50ms intervals.
2425
def schedule_in(handler, delay_ms):
2526
def _wrap(_arg):

examples/conwaylife.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
lcd = pyb.LCD("x")
55
lcd.light(1)
66

7+
78
# do 1 iteration of Conway's Game of Life
89
def conway_step():
910
for x in range(128): # loop over x coordinates

examples/rp2/pio_1hz.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Example using PIO to blink an LED and raise an IRQ at 1Hz.
2+
# Note: this does not work on Pico W because it uses Pin(25) for LED output.
23

34
import time
45
from machine import Pin

examples/rp2/pio_exec.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Example using PIO to turn on an LED via an explicit exec.
2+
# Note: this does not work on Pico W because it uses Pin(25) for LED output.
23
#
34
# Demonstrates:
45
# - using set_init and set_base
@@ -8,6 +9,7 @@
89
from machine import Pin
910
import rp2
1011

12+
1113
# Define an empty program that uses a single set pin.
1214
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
1315
def prog():

examples/rp2/pio_uart_tx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def uart_tx():
3333
sm.active(1)
3434
uarts.append(sm)
3535

36+
3637
# We can print characters from each UART by pushing them to the TX FIFO
3738
def pio_uart_print(sm, s):
3839
for c in s:

examples/rp2/pwm_fade.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Example using PWM to fade an LED.
2+
# Note: this does not work on Pico W because it uses Pin(25) for LED output.
23

34
import time
45
from machine import Pin, PWM

extmod/uasyncio/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"StreamWriter": "stream",
1919
}
2020

21+
2122
# Lazy loader, effectively does:
2223
# global attr
2324
# from .mod import attr

extmod/uasyncio/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TimeoutError(Exception):
3030
################################################################################
3131
# Sleep functions
3232

33+
3334
# "Yield" once, then raise StopIteration
3435
class SingletonGenerator:
3536
def __init__(self):
@@ -132,6 +133,7 @@ def wait_io_event(self, dt):
132133
################################################################################
133134
# Main run loop
134135

136+
135137
# Ensure the awaitable is a task
136138
def _promote_to_task(aw):
137139
return aw if isinstance(aw, Task) else create_task(aw)

extmod/uasyncio/event.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from . import core
55

6+
67
# Event class for primitive events that can be waited on, set, and cleared
78
class Event:
89
def __init__(self):

extmod/uasyncio/lock.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from . import core
55

6+
67
# Lock class for primitive mutex capability
78
class Lock:
89
def __init__(self):

ports/esp32/boards/UM_FEATHERS2/modules/feathers2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
# Helper functions
4141

42+
4243
# LED & Ambient Light Sensor control
4344
def set_led(state):
4445
l = Pin(LED, Pin.OUT)

ports/esp32/boards/UM_FEATHERS2NEO/modules/feathers2neo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
DAC1 = const(17)
3939
DAC2 = const(18)
4040

41+
4142
# Helper functions
4243
def set_pixel_power(state):
4344
"""Enable or Disable power to the onboard NeoPixel to either show colour, or to reduce power for deep sleep."""

ports/esp32/boards/UM_FEATHERS3/modules/feathers3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
# Helper functions
3434

35+
3536
# LED & Ambient Light Sensor control
3637
def led_set(state):
3738
"""Set the state of the BLUE LED on IO13"""

ports/esp32/boards/UM_PROS3/modules/pros3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
I2C_SDA = const(8)
2929
I2C_SCL = const(9)
3030

31+
3132
# Helper functions
3233
def set_ldo2_power(state):
3334
"""Enable or Disable power to the second LDO"""

ports/esp32/boards/UM_TINYPICO/modules/tinypico.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
# Helper functions
4141

42+
4243
# Get a *rough* estimate of the current battery voltage
4344
# If the battery is not present, the charge IC will still report it's trying to charge at X voltage
4445
# so it will still show a voltage.

ports/esp32/boards/UM_TINYS3/modules/tinys3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
I2C_SDA = const(8)
2929
I2C_SCL = const(9)
3030

31+
3132
# Helper functions
3233
def set_pixel_power(state):
3334
"""Enable or Disable power to the onboard NeoPixel to either show colour, or to reduce power for deep sleep."""

ports/esp8266/makeimg.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
md5 = hashlib.md5()
1515

1616
with open(sys.argv[3], "wb") as fout:
17-
1817
with open(sys.argv[1], "rb") as f:
1918
data_flash = f.read()
2019
fout.write(data_flash)

ports/esp8266/modules/port_diag.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
def main():
8-
98
ROM = uctypes.bytearray_at(0x40200000, 16)
109
fid = esp.flash_id()
1110

ports/mimxrt/boards/make-flexram-config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"""
5454
ocram_min_size = 0x00010000 # 64 KB
5555

56+
5657
# Value parser
5758
def mimxrt_default_parser(defines_file, features_file, ld_script):
5859
with open(ld_script, "r") as input_file:

ports/nrf/examples/powerup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ def loop(self):
187187
self.old_speed = 0
188188

189189
while True:
190-
191190
time.sleep_ms(100)
192191

193192
# read out new angle

ports/rp2/rp2_pio.c

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ typedef struct _rp2_state_machine_irq_obj_t {
6868
STATIC const rp2_state_machine_obj_t rp2_state_machine_obj[8];
6969
STATIC uint8_t rp2_state_machine_initial_pc[8];
7070

71+
// These masks keep track of PIO instruction memory used by this module.
72+
STATIC uint32_t rp2_pio_instruction_memory_usage_mask[2];
73+
74+
STATIC const rp2_state_machine_obj_t *rp2_state_machine_get_object(mp_int_t sm_id);
75+
STATIC void rp2_state_machine_reset_all(void);
7176
STATIC mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
7277

7378
STATIC void pio_irq0(PIO pio) {
@@ -101,16 +106,42 @@ STATIC void pio1_irq0(void) {
101106
pio_irq0(pio1);
102107
}
103108

104-
void rp2_pio_init(void) {
105-
// Reset all PIO instruction memory.
106-
#if MICROPY_PY_NETWORK_CYW43
107-
// TODO: cannot reset PIO memory when CYW43 driver is enabled and active
108-
// because it uses a PIO for the bus interface.
109-
#else
110-
pio_clear_instruction_memory(pio0);
111-
pio_clear_instruction_memory(pio1);
112-
#endif
109+
// Calls pio_add_program() and keeps track of used instruction memory.
110+
STATIC uint rp2_pio_add_managed_program(PIO pio, struct pio_program *pio_program) {
111+
uint offset = pio_add_program(pio, pio_program);
112+
uint32_t mask = ((1 << pio_program->length) - 1) << offset;
113+
rp2_pio_instruction_memory_usage_mask[pio_get_index(pio)] |= mask;
114+
return offset;
115+
}
116+
117+
// Calls pio_remove_program() and keeps track of used instruction memory.
118+
STATIC void rp2_pio_remove_managed_program(PIO pio, struct pio_program *pio_program, uint offset) {
119+
pio_remove_program(pio, pio_program, offset);
120+
uint32_t mask = ((1 << pio_program->length) - 1) << offset;
121+
rp2_pio_instruction_memory_usage_mask[pio_get_index(pio)] &= ~mask;
122+
}
123+
124+
// Calls pio_remove_program() for all programs registered with rp2_pio_add_managed_program(),
125+
// that weren't already removed via rp2_pio_remove_managed_program().
126+
STATIC void rp2_pio_remove_all_managed_programs(PIO pio) {
127+
uint32_t mask = rp2_pio_instruction_memory_usage_mask[pio_get_index(pio)];
128+
for (size_t i = 0; i < 32; ++i) {
129+
if (mask & (1 << i)) {
130+
size_t j;
131+
for (j = i + 1; j < 32; ++j) {
132+
if (!(mask & (1 << j))) {
133+
break;
134+
}
135+
}
136+
struct pio_program pio_program = { NULL, j - i, -1 };
137+
pio_remove_program(pio, &pio_program, i);
138+
i = j; // i indexes an unused bit, or is 32
139+
}
140+
}
141+
rp2_pio_instruction_memory_usage_mask[pio_get_index(pio)] = 0;
142+
}
113143

144+
void rp2_pio_init(void) {
114145
// Set up interrupts.
115146
memset(MP_STATE_PORT(rp2_pio_irq_obj), 0, sizeof(MP_STATE_PORT(rp2_pio_irq_obj)));
116147
memset(MP_STATE_PORT(rp2_state_machine_irq_obj), 0, sizeof(MP_STATE_PORT(rp2_state_machine_irq_obj)));
@@ -123,6 +154,14 @@ void rp2_pio_deinit(void) {
123154
irq_set_mask_enabled((1u << PIO0_IRQ_0) | (1u << PIO0_IRQ_1), false);
124155
irq_remove_handler(PIO0_IRQ_0, pio0_irq0);
125156
irq_remove_handler(PIO1_IRQ_0, pio1_irq0);
157+
158+
rp2_state_machine_reset_all();
159+
160+
// Reset all PIO instruction memory allocated by this module.
161+
// Note: other subsystems (eg the CYW43 driver) may use the PIO,
162+
// and their PIO programs should remain intact.
163+
rp2_pio_remove_all_managed_programs(pio0);
164+
rp2_pio_remove_all_managed_programs(pio1);
126165
}
127166

128167
/******************************************************************************/
@@ -238,7 +277,7 @@ STATIC mp_obj_t rp2_pio_add_program(mp_obj_t self_in, mp_obj_t prog_in) {
238277
if (!pio_can_add_program(self->pio, &pio_program)) {
239278
mp_raise_OSError(MP_ENOMEM);
240279
}
241-
uint offset = pio_add_program(self->pio, &pio_program);
280+
uint offset = rp2_pio_add_managed_program(self->pio, &pio_program);
242281

243282
// Store the program offset in the program object.
244283
prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)] = MP_OBJ_NEW_SMALL_INT(offset);
@@ -272,7 +311,7 @@ STATIC mp_obj_t rp2_pio_remove_program(size_t n_args, const mp_obj_t *args) {
272311

273312
// Remove the program from the instruction memory.
274313
struct pio_program pio_program = { NULL, length, -1 };
275-
pio_remove_program(self->pio, &pio_program, offset);
314+
rp2_pio_remove_managed_program(self->pio, &pio_program, offset);
276315

277316
return mp_const_none;
278317
}
@@ -285,11 +324,11 @@ STATIC mp_obj_t rp2_pio_state_machine(size_t n_args, const mp_obj_t *pos_args, m
285324
// Get and verify the state machine id.
286325
mp_int_t sm_id = mp_obj_get_int(pos_args[1]);
287326
if (!(0 <= sm_id && sm_id < 4)) {
288-
mp_raise_ValueError("invalide state machine");
327+
mp_raise_ValueError("invalid StateMachine");
289328
}
290329

291330
// Return the correct StateMachine object.
292-
const rp2_state_machine_obj_t *sm = &rp2_state_machine_obj[(self->pio == pio0 ? 0 : 4) + sm_id];
331+
const rp2_state_machine_obj_t *sm = rp2_state_machine_get_object((self->pio == pio0 ? 0 : 4) + sm_id);
293332

294333
if (n_args > 2 || kw_args->used > 0) {
295334
// Configuration arguments given so init this StateMachine.
@@ -414,6 +453,9 @@ STATIC const mp_irq_methods_t rp2_pio_irq_methods = {
414453
/******************************************************************************/
415454
// StateMachine object
416455

456+
// This mask keeps track of state machines claimed by this module.
457+
STATIC uint32_t rp2_state_machine_claimed_mask;
458+
417459
STATIC const mp_irq_methods_t rp2_state_machine_irq_methods;
418460

419461
STATIC const rp2_state_machine_obj_t rp2_state_machine_obj[] = {
@@ -427,6 +469,35 @@ STATIC const rp2_state_machine_obj_t rp2_state_machine_obj[] = {
427469
{ { &rp2_state_machine_type }, pio1, PIO1_IRQ_0, 3, 7 },
428470
};
429471

472+
STATIC const rp2_state_machine_obj_t *rp2_state_machine_get_object(mp_int_t sm_id) {
473+
if (!(0 <= sm_id && sm_id < MP_ARRAY_SIZE(rp2_state_machine_obj))) {
474+
mp_raise_ValueError("invalid StateMachine");
475+
}
476+
477+
const rp2_state_machine_obj_t *sm_obj = &rp2_state_machine_obj[sm_id];
478+
479+
if (!(rp2_state_machine_claimed_mask & (1 << sm_id))) {
480+
if (pio_sm_is_claimed(sm_obj->pio, sm_obj->sm)) {
481+
mp_raise_ValueError("StateMachine claimed by external resource");
482+
}
483+
pio_sm_claim(sm_obj->pio, sm_obj->sm);
484+
rp2_state_machine_claimed_mask |= 1 << sm_id;
485+
}
486+
487+
return sm_obj;
488+
}
489+
490+
STATIC void rp2_state_machine_reset_all(void) {
491+
for (size_t i = 0; i < MP_ARRAY_SIZE(rp2_state_machine_obj); ++i) {
492+
if (rp2_state_machine_claimed_mask & (1 << i)) {
493+
const rp2_state_machine_obj_t *sm_obj = &rp2_state_machine_obj[i];
494+
pio_sm_unclaim(sm_obj->pio, sm_obj->sm);
495+
pio_sm_set_enabled(sm_obj->pio, sm_obj->sm, false);
496+
}
497+
}
498+
rp2_state_machine_claimed_mask = 0;
499+
}
500+
430501
STATIC void rp2_state_machine_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
431502
rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in);
432503
mp_printf(print, "StateMachine(%u)", self->id);
@@ -573,10 +644,7 @@ STATIC mp_obj_t rp2_state_machine_make_new(const mp_obj_type_t *type, size_t n_a
573644

574645
// Get the StateMachine object.
575646
mp_int_t sm_id = mp_obj_get_int(args[0]);
576-
if (!(0 <= sm_id && sm_id < MP_ARRAY_SIZE(rp2_state_machine_obj))) {
577-
mp_raise_ValueError("invalid StateMachine");
578-
}
579-
const rp2_state_machine_obj_t *self = &rp2_state_machine_obj[sm_id];
647+
const rp2_state_machine_obj_t *self = rp2_state_machine_get_object(sm_id);
580648

581649
if (n_args > 1 || n_kw > 0) {
582650
// Configuration arguments given so init this StateMachine.

ports/stm32/boards/NUCLEO_WB55/rfcore_makefirmware.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def get_details(release_notes, filename):
6565

6666

6767
def main(src_path, dest_path):
68-
6968
# Load the release note to parse for important details
7069
with open(os.path.join(src_path, _RELEASE_NOTES), "rb") as f:
7170
release_notes = f.read()

ports/stm32/boards/make-pins.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,6 @@ def print_af_hdr(self, af_const_filename):
506506

507507
def print_af_defs(self, af_defs_filename, cmp_strings):
508508
with open(af_defs_filename, "wt") as af_defs_file:
509-
510509
STATIC_AF_TOKENS = {}
511510
for named_pin in self.cpu_pins:
512511
for af in named_pin.pin().alt_fn:

ports/stm32/make-stmconst.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def convert_bytes_to_str(b):
2929

3030
# end compatibility code
3131

32+
3233
# given a list of (name,regex) pairs, find the first one that matches the given line
3334
def re_match_first(regexs, line):
3435
for name, regex in regexs:

0 commit comments

Comments
 (0)
0