Skip to content

Commit 7d39db2

Browse files
committed
extmod/modmachine: Factor ports' machine module dict to common code.
This is a code factoring to have the dict for the machine module in one location, and all the ports use that same dict. The machine.soft_reset() function implementation is also factored because it's the same for all ports that did already implement it. Eventually more functions/bindings can be factored. All ports remain functionally the same, except: - cc3200 port: gains soft_reset, mem8, mem16, mem32, Signal; loses POWER_ON (which was a legacy constant, replaced long ago by PWRON_RESET) - nrf port: gains Signal - qemu-arm port: gains soft_reset - unix port: gains soft_reset - zephyr port: gains soft_reset, mem8, mem16, mem32 Signed-off-by: Damien George <[email protected]>
1 parent 14432b5 commit 7d39db2

Some content is hidden

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

44 files changed

+550
-922
lines changed

extmod/extmod.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(MICROPY_SOURCE_EXTMOD
2222
${MICROPY_EXTMOD_DIR}/modbluetooth.c
2323
${MICROPY_EXTMOD_DIR}/modframebuf.c
2424
${MICROPY_EXTMOD_DIR}/modlwip.c
25+
${MICROPY_EXTMOD_DIR}/modmachine.c
2526
${MICROPY_EXTMOD_DIR}/modnetwork.c
2627
${MICROPY_EXTMOD_DIR}/modonewire.c
2728
${MICROPY_EXTMOD_DIR}/modasyncio.c

extmod/extmod.mk

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ SRC_EXTMOD_C += \
2727
extmod/modheapq.c \
2828
extmod/modjson.c \
2929
extmod/modlwip.c \
30+
extmod/modmachine.c \
3031
extmod/modnetwork.c \
3132
extmod/modonewire.c \
3233
extmod/modos.c \

extmod/modmachine.c

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Damien P. George
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 "py/runtime.h"
28+
29+
#if MICROPY_PY_MACHINE
30+
31+
#include "extmod/modmachine.h"
32+
#include "shared/runtime/pyexec.h"
33+
34+
#if MICROPY_PY_MACHINE_DHT_READINTO
35+
#include "drivers/dht/dht.h"
36+
#endif
37+
38+
// The port can provide additional machine-module implementation in this file.
39+
#ifdef MICROPY_PY_MACHINE_INCLUDEFILE
40+
#include MICROPY_PY_MACHINE_INCLUDEFILE
41+
#endif
42+
43+
STATIC mp_obj_t machine_soft_reset(void) {
44+
pyexec_system_exit = PYEXEC_FORCED_EXIT;
45+
mp_raise_type(&mp_type_SystemExit);
46+
}
47+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
48+
49+
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
50+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_machine) },
51+
52+
// Memory access objects.
53+
{ MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },
54+
{ MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
55+
{ MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },
56+
57+
// Reset related functions.
58+
{ MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
59+
60+
// Functions for bit protocols.
61+
#if MICROPY_PY_MACHINE_BITSTREAM
62+
{ MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
63+
#endif
64+
#if MICROPY_PY_MACHINE_DHT_READINTO
65+
{ MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) },
66+
#endif
67+
#if MICROPY_PY_MACHINE_PULSE
68+
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
69+
#endif
70+
71+
// Class for Signal.
72+
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
73+
74+
// Classes for software bus protocols.
75+
#if MICROPY_PY_MACHINE_SOFTI2C
76+
{ MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
77+
#endif
78+
#if MICROPY_PY_MACHINE_SOFTSPI
79+
{ MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
80+
#endif
81+
82+
// Classes for hardware peripherals.
83+
#if MICROPY_PY_MACHINE_ADC
84+
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
85+
#endif
86+
#if MICROPY_PY_MACHINE_ADC_BLOCK
87+
{ MP_ROM_QSTR(MP_QSTR_ADCBlock), MP_ROM_PTR(&machine_adc_block_type) },
88+
#endif
89+
#if MICROPY_PY_MACHINE_DAC
90+
{ MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
91+
#endif
92+
#if MICROPY_PY_MACHINE_I2C
93+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
94+
#endif
95+
#if MICROPY_PY_MACHINE_I2S
96+
{ MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) },
97+
#endif
98+
#if MICROPY_PY_MACHINE_PWM
99+
{ MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
100+
#endif
101+
#if MICROPY_PY_MACHINE_SPI
102+
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
103+
#endif
104+
#if MICROPY_PY_MACHINE_UART
105+
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
106+
#endif
107+
#if MICROPY_PY_MACHINE_WDT
108+
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) },
109+
#endif
110+
111+
// A port can add extra entries to the module by defining the following macro.
112+
#ifdef MICROPY_PY_MACHINE_EXTRA_GLOBALS
113+
MICROPY_PY_MACHINE_EXTRA_GLOBALS
114+
#endif
115+
};
116+
STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
117+
118+
const mp_obj_module_t mp_module_machine = {
119+
.base = { &mp_type_module },
120+
.globals = (mp_obj_dict_t *)&machine_module_globals,
121+
};
122+
123+
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
124+
125+
#endif // MICROPY_PY_MACHINE

ports/cc3200/application.mk

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ APP_MISC_SRC_C = $(addprefix misc/,\
7676
)
7777

7878
APP_MODS_SRC_C = $(addprefix mods/,\
79-
modmachine.c \
8079
modnetwork.c \
8180
modos.c \
8281
modsocket.c \

ports/cc3200/mods/modmachine.c

+48-67
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2013, 2014 Damien P. George
6+
* Copyright (c) 2013-2023 Damien P. George
77
* Copyright (c) 2015 Daniel Campora
88
*
99
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -25,39 +25,74 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#include <stdint.h>
28+
// This file is never compiled standalone, it's included directly from
29+
// extmod/modmachine.c via MICROPY_PY_MACHINE_INCLUDEFILE.
30+
2931
#include <stdio.h>
3032

31-
#include "py/runtime.h"
32-
#include "py/mphal.h"
33-
#include "extmod/modmachine.h"
3433
#include "inc/hw_types.h"
3534
#include "inc/hw_gpio.h"
3635
#include "inc/hw_ints.h"
3736
#include "inc/hw_memmap.h"
3837
#include "inc/hw_uart.h"
3938
#include "rom_map.h"
40-
#include "prcm.h"
4139
#include "pybuart.h"
4240
#include "pybpin.h"
4341
#include "pybrtc.h"
4442
#include "simplelink.h"
45-
#include "modnetwork.h"
4643
#include "modwlan.h"
47-
#include "modos.h"
48-
#include "FreeRTOS.h"
49-
#include "portable.h"
50-
#include "task.h"
5144
#include "random.h"
5245
#include "pybadc.h"
5346
#include "pybi2c.h"
5447
#include "pybsd.h"
5548
#include "pybsleep.h"
5649
#include "pybspi.h"
5750
#include "pybtimer.h"
58-
#include "utils.h"
59-
#include "gccollect.h"
6051

52+
#ifdef DEBUG
53+
#define MICROPY_PY_MACHINE_INFO_ENTRY { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
54+
#else
55+
#define MICROPY_PY_MACHINE_INFO_ENTRY
56+
#endif
57+
58+
#define MICROPY_PY_MACHINE_EXTRA_GLOBALS \
59+
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, \
60+
MICROPY_PY_MACHINE_INFO_ENTRY \
61+
{ MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, \
62+
{ MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, \
63+
{ MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&machine_main_obj) }, \
64+
{ MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&machine_rng_get_obj) }, \
65+
{ MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, \
66+
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_obj) }, \
67+
{ MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) }, \
68+
{ MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) }, \
69+
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, \
70+
{ MP_ROM_QSTR(MP_QSTR_wake_reason), MP_ROM_PTR(&machine_wake_reason_obj) }, \
71+
\
72+
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, \
73+
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, \
74+
\
75+
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) }, \
76+
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) }, \
77+
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) }, \
78+
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) }, \
79+
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) }, \
80+
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, \
81+
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) }, \
82+
{ MP_ROM_QSTR(MP_QSTR_SD), MP_ROM_PTR(&pyb_sd_type) }, \
83+
\
84+
/* class constants */ \
85+
{ MP_ROM_QSTR(MP_QSTR_IDLE), MP_ROM_INT(PYB_PWR_MODE_ACTIVE) }, \
86+
{ MP_ROM_QSTR(MP_QSTR_SLEEP), MP_ROM_INT(PYB_PWR_MODE_LPDS) }, \
87+
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(PYB_PWR_MODE_HIBERNATE) }, \
88+
{ MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(PYB_SLP_PWRON_RESET) }, \
89+
{ MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(PYB_SLP_HARD_RESET) }, \
90+
{ MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(PYB_SLP_WDT_RESET) }, \
91+
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_ROM_INT(PYB_SLP_HIB_RESET) }, \
92+
{ MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(PYB_SLP_SOFT_RESET) }, \
93+
{ MP_ROM_QSTR(MP_QSTR_WLAN_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_WLAN) }, \
94+
{ MP_ROM_QSTR(MP_QSTR_PIN_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_GPIO) }, \
95+
{ MP_ROM_QSTR(MP_QSTR_RTC_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_RTC) }, \
6196

6297
#ifdef DEBUG
6398
extern OsiTaskHandle mpTaskHandle;
@@ -161,58 +196,4 @@ STATIC mp_obj_t machine_wake_reason (void) {
161196
}
162197
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_wake_reason_obj, machine_wake_reason);
163198

164-
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
165-
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_machine) },
166-
167-
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
168-
#ifdef DEBUG
169-
{ MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
170-
#endif
171-
{ MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
172-
{ MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
173-
{ MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&machine_main_obj) },
174-
{ MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&machine_rng_get_obj) },
175-
{ MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) },
176-
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_lightsleep_obj) },
177-
{ MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) },
178-
{ MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
179-
{ MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
180-
{ MP_ROM_QSTR(MP_QSTR_wake_reason), MP_ROM_PTR(&machine_wake_reason_obj) },
181-
182-
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
183-
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
184-
185-
{ MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
186-
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) },
187-
{ MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
188-
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) },
189-
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) },
190-
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
191-
{ MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) },
192-
{ MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) },
193-
{ MP_ROM_QSTR(MP_QSTR_SD), MP_ROM_PTR(&pyb_sd_type) },
194-
195-
// class constants
196-
{ MP_ROM_QSTR(MP_QSTR_IDLE), MP_ROM_INT(PYB_PWR_MODE_ACTIVE) },
197-
{ MP_ROM_QSTR(MP_QSTR_SLEEP), MP_ROM_INT(PYB_PWR_MODE_LPDS) },
198-
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(PYB_PWR_MODE_HIBERNATE) },
199-
{ MP_ROM_QSTR(MP_QSTR_POWER_ON), MP_ROM_INT(PYB_SLP_PWRON_RESET) }, // legacy constant
200-
{ MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(PYB_SLP_PWRON_RESET) },
201-
{ MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(PYB_SLP_HARD_RESET) },
202-
{ MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(PYB_SLP_WDT_RESET) },
203-
{ MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_ROM_INT(PYB_SLP_HIB_RESET) },
204-
{ MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(PYB_SLP_SOFT_RESET) },
205-
{ MP_ROM_QSTR(MP_QSTR_WLAN_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_WLAN) },
206-
{ MP_ROM_QSTR(MP_QSTR_PIN_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_GPIO) },
207-
{ MP_ROM_QSTR(MP_QSTR_RTC_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_RTC) },
208-
};
209-
210-
STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
211-
212-
const mp_obj_module_t mp_module_machine = {
213-
.base = { &mp_type_module },
214-
.globals = (mp_obj_dict_t*)&machine_module_globals,
215-
};
216-
217-
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
218199
MP_REGISTER_ROOT_POINTER(mp_obj_t machine_config_main);

ports/cc3200/mpconfigport.h

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
#define MICROPY_PY_TIME_TIME_TIME_NS (1)
122122
#define MICROPY_PY_TIME_INCLUDEFILE "ports/cc3200/mods/modtime.c"
123123
#define MICROPY_PY_MACHINE (1)
124+
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/cc3200/mods/modmachine.c"
124125
#define MICROPY_PY_MACHINE_WDT (1)
125126
#define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/cc3200/mods/machine_wdt.c"
126127

ports/esp32/esp32_common.cmake

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ list(APPEND MICROPY_SOURCE_PORT
6969
machine_touchpad.c
7070
machine_dac.c
7171
machine_i2c.c
72-
modmachine.c
7372
network_common.c
7473
network_lan.c
7574
network_ppp.c

0 commit comments

Comments
 (0)