Branch data Line data Source code
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 must provide implementations of these low-level machine functions.
39 : :
40 : : static void mp_machine_idle(void);
41 : :
42 : : #if MICROPY_PY_MACHINE_BOOTLOADER
43 : : NORETURN void mp_machine_bootloader(size_t n_args, const mp_obj_t *args);
44 : : #endif
45 : :
46 : : #if MICROPY_PY_MACHINE_RESET
47 : : NORETURN static void mp_machine_reset(void);
48 : : static mp_int_t mp_machine_reset_cause(void);
49 : : #endif
50 : :
51 : : #if MICROPY_PY_MACHINE_BARE_METAL_FUNCS
52 : : static mp_obj_t mp_machine_unique_id(void);
53 : : static mp_obj_t mp_machine_get_freq(void);
54 : : static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args);
55 : : static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args);
56 : : NORETURN static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args);
57 : : #endif
58 : :
59 : : // The port can provide additional machine-module implementation in this file.
60 : : #ifdef MICROPY_PY_MACHINE_INCLUDEFILE
61 : : #include MICROPY_PY_MACHINE_INCLUDEFILE
62 : : #endif
63 : :
64 : 0 : static mp_obj_t machine_soft_reset(void) {
65 : 0 : pyexec_system_exit = PYEXEC_FORCED_EXIT;
66 : 0 : mp_raise_type(&mp_type_SystemExit);
67 : : }
68 : : static MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
69 : :
70 : : #if MICROPY_PY_MACHINE_BOOTLOADER
71 : : NORETURN mp_obj_t machine_bootloader(size_t n_args, const mp_obj_t *args) {
72 : : mp_machine_bootloader(n_args, args);
73 : : }
74 : : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bootloader_obj, 0, 1, machine_bootloader);
75 : : #endif
76 : :
77 : 0 : static mp_obj_t machine_idle(void) {
78 : 0 : mp_machine_idle();
79 : 0 : return mp_const_none;
80 : : }
81 : : static MP_DEFINE_CONST_FUN_OBJ_0(machine_idle_obj, machine_idle);
82 : :
83 : : #if MICROPY_PY_MACHINE_RESET
84 : :
85 : : NORETURN static mp_obj_t machine_reset(void) {
86 : : mp_machine_reset();
87 : : }
88 : : MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
89 : :
90 : : static mp_obj_t machine_reset_cause(void) {
91 : : return MP_OBJ_NEW_SMALL_INT(mp_machine_reset_cause());
92 : : }
93 : : MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);
94 : :
95 : : #endif
96 : :
97 : : #if MICROPY_PY_MACHINE_BARE_METAL_FUNCS
98 : :
99 : : static mp_obj_t machine_unique_id(void) {
100 : : return mp_machine_unique_id();
101 : : }
102 : : MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);
103 : :
104 : : static mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
105 : : if (n_args == 0) {
106 : : return mp_machine_get_freq();
107 : : } else {
108 : : mp_machine_set_freq(n_args, args);
109 : : return mp_const_none;
110 : : }
111 : : }
112 : : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq);
113 : :
114 : : static mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) {
115 : : mp_machine_lightsleep(n_args, args);
116 : : return mp_const_none;
117 : : }
118 : : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_lightsleep_obj, 0, 1, machine_lightsleep);
119 : :
120 : : NORETURN static mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *args) {
121 : : mp_machine_deepsleep(n_args, args);
122 : : }
123 : : MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_deepsleep_obj, 0, 1, machine_deepsleep);
124 : :
125 : : #endif
126 : :
127 : : #if MICROPY_PY_MACHINE_DISABLE_IRQ_ENABLE_IRQ
128 : :
129 : : static mp_obj_t machine_disable_irq(void) {
130 : : uint32_t state = MICROPY_BEGIN_ATOMIC_SECTION();
131 : : return mp_obj_new_int(state);
132 : : }
133 : : static MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq);
134 : :
135 : : static mp_obj_t machine_enable_irq(mp_obj_t state_in) {
136 : : uint32_t state = mp_obj_get_int(state_in);
137 : : MICROPY_END_ATOMIC_SECTION(state);
138 : : return mp_const_none;
139 : : }
140 : : static MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq);
141 : :
142 : : #endif
143 : :
144 : : static const mp_rom_map_elem_t machine_module_globals_table[] = {
145 : : { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_machine) },
146 : :
147 : : // Memory access objects.
148 : : #if MICROPY_PY_MACHINE_MEMX
149 : : { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },
150 : : { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
151 : : { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },
152 : : #endif
153 : :
154 : : // Miscellaneous functions.
155 : : #if MICROPY_PY_MACHINE_BARE_METAL_FUNCS
156 : : { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
157 : : #endif
158 : :
159 : : // Reset related functions.
160 : : { MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
161 : : #if MICROPY_PY_MACHINE_BOOTLOADER
162 : : { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) },
163 : : #endif
164 : : #if MICROPY_PY_MACHINE_RESET
165 : : { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
166 : : { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
167 : : #endif
168 : :
169 : : // Power related functions.
170 : : { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) },
171 : : #if MICROPY_PY_MACHINE_BARE_METAL_FUNCS
172 : : { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
173 : : { MP_ROM_QSTR(MP_QSTR_lightsleep), MP_ROM_PTR(&machine_lightsleep_obj) },
174 : : { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
175 : : #endif
176 : :
177 : : // Interrupt related functions.
178 : : #if MICROPY_PY_MACHINE_DISABLE_IRQ_ENABLE_IRQ
179 : : { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
180 : : { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
181 : : #endif
182 : :
183 : : // Functions for bit protocols.
184 : : #if MICROPY_PY_MACHINE_BITSTREAM
185 : : { MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
186 : : #endif
187 : : #if MICROPY_PY_MACHINE_DHT_READINTO
188 : : { MP_ROM_QSTR(MP_QSTR_dht_readinto), MP_ROM_PTR(&dht_readinto_obj) },
189 : : #endif
190 : : #if MICROPY_PY_MACHINE_PULSE
191 : : { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
192 : : #endif
193 : :
194 : : // Classes for PinBase and Signal.
195 : : #if MICROPY_PY_MACHINE_PIN_BASE
196 : : { MP_ROM_QSTR(MP_QSTR_PinBase), MP_ROM_PTR(&machine_pinbase_type) },
197 : : #endif
198 : : #if MICROPY_PY_MACHINE_SIGNAL
199 : : { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
200 : : #endif
201 : :
202 : : // Classes for software bus protocols.
203 : : #if MICROPY_PY_MACHINE_SOFTI2C
204 : : { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) },
205 : : #endif
206 : : #if MICROPY_PY_MACHINE_SOFTSPI
207 : : { MP_ROM_QSTR(MP_QSTR_SoftSPI), MP_ROM_PTR(&mp_machine_soft_spi_type) },
208 : : #endif
209 : :
210 : : // Classes for hardware peripherals.
211 : : #if MICROPY_PY_MACHINE_ADC
212 : : { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) },
213 : : #endif
214 : : #if MICROPY_PY_MACHINE_ADC_BLOCK
215 : : { MP_ROM_QSTR(MP_QSTR_ADCBlock), MP_ROM_PTR(&machine_adc_block_type) },
216 : : #endif
217 : : #if MICROPY_PY_MACHINE_DAC
218 : : { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) },
219 : : #endif
220 : : #if MICROPY_PY_MACHINE_I2C
221 : : { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
222 : : #endif
223 : : #if MICROPY_PY_MACHINE_I2S
224 : : { MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) },
225 : : #endif
226 : : #if MICROPY_PY_MACHINE_PWM
227 : : { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) },
228 : : #endif
229 : : #if MICROPY_PY_MACHINE_SPI
230 : : { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },
231 : : #endif
232 : : #if MICROPY_PY_MACHINE_UART
233 : : { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
234 : : #endif
235 : : #if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
236 : : { MP_ROM_QSTR(MP_QSTR_USBDevice), MP_ROM_PTR(&machine_usb_device_type) },
237 : : #endif
238 : : #if MICROPY_PY_MACHINE_WDT
239 : : { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&machine_wdt_type) },
240 : : #endif
241 : :
242 : : // A port can add extra entries to the module by defining the following macro.
243 : : #ifdef MICROPY_PY_MACHINE_EXTRA_GLOBALS
244 : : MICROPY_PY_MACHINE_EXTRA_GLOBALS
245 : : #endif
246 : : };
247 : : static MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
248 : :
249 : : const mp_obj_module_t mp_module_machine = {
250 : : .base = { &mp_type_module },
251 : : .globals = (mp_obj_dict_t *)&machine_module_globals,
252 : : };
253 : :
254 : : MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_machine, mp_module_machine);
255 : :
256 : : #endif // MICROPY_PY_MACHINE
|