blob: 36872c9dff688c6752dd2345de35854305e1cc01 [file] [log] [blame]
Paul Sokolovsky5d7c4082015-12-31 00:51:08 +02001/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013-2015 Damien P. George
Paul Sokolovsky71930862016-01-05 21:23:23 +02007 * Copyright (c) 2016 Paul Sokolovsky
Paul Sokolovsky5d7c4082015-12-31 00:51:08 +02008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28#include <stdint.h>
29
30#include "py/obj.h"
Paul Sokolovsky71930862016-01-05 21:23:23 +020031#include "py/runtime.h"
Paul Sokolovsky5d7c4082015-12-31 00:51:08 +020032#include "extmod/machine_mem.h"
Damien Georgecff2b7a2016-05-31 14:06:50 +010033#include "extmod/machine_pulse.h"
Damien Georgea5254932016-04-12 13:55:20 +010034#include "extmod/machine_i2c.h"
Paul Sokolovsky26f06162016-02-03 15:23:16 +020035#include "modpyb.h"
Damien George32d7cf62016-04-21 11:43:37 +010036#include "modpybrtc.h"
Paul Sokolovsky71930862016-01-05 21:23:23 +020037
Damien George9a368ae2016-05-20 13:30:33 +010038#include "xtirq.h"
Paul Sokolovsky71930862016-01-05 21:23:23 +020039#include "os_type.h"
40#include "osapi.h"
Paul Sokolovsky98b727c2016-02-06 01:36:17 +020041#include "etshal.h"
Damien George32d7cf62016-04-21 11:43:37 +010042#include "ets_alt_task.h"
Damien Georgef7be8032016-02-17 15:19:03 +000043#include "user_interface.h"
Paul Sokolovsky5d7c4082015-12-31 00:51:08 +020044
45#if MICROPY_PY_MACHINE
46
Damien George32d7cf62016-04-21 11:43:37 +010047//#define MACHINE_WAKE_IDLE (0x01)
48//#define MACHINE_WAKE_SLEEP (0x02)
49#define MACHINE_WAKE_DEEPSLEEP (0x04)
50
Damien Georgef7be8032016-02-17 15:19:03 +000051STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) {
52 if (n_args == 0) {
53 // get
54 return mp_obj_new_int(system_get_cpu_freq() * 1000000);
55 } else {
56 // set
57 mp_int_t freq = mp_obj_get_int(args[0]) / 1000000;
58 if (freq != 80 && freq != 160) {
59 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
60 "frequency can only be either 80Mhz or 160MHz"));
61 }
62 system_update_cpu_freq(freq);
63 return mp_const_none;
64 }
65}
66STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq);
67
Paul Sokolovsky81fd5682016-04-05 00:20:25 +030068STATIC mp_obj_t machine_reset(void) {
69 system_restart();
70 return mp_const_none;
71}
72STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
73
Paul Sokolovsky050e6452016-04-15 22:07:28 +030074STATIC mp_obj_t machine_reset_cause(void) {
75 return MP_OBJ_NEW_SMALL_INT(system_get_rst_info()->reason);
76}
77STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);
78
Paul Sokolovskyde125022016-04-05 00:57:49 +030079STATIC mp_obj_t machine_unique_id(void) {
80 uint32_t id = system_get_chip_id();
81 return mp_obj_new_bytes((byte*)&id, sizeof(id));
82}
83STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);
84
Damien George32d7cf62016-04-21 11:43:37 +010085STATIC mp_obj_t machine_deepsleep(void) {
86 // default to sleep forever
87 uint32_t sleep_us = 0;
88
89 // see if RTC.ALARM0 should wake the device
90 if (pyb_rtc_alarm0_wake & MACHINE_WAKE_DEEPSLEEP) {
91 uint64_t t = pyb_rtc_get_us_since_2000();
92 if (pyb_rtc_alarm0_expiry <= t) {
93 sleep_us = 1; // alarm already expired so wake immediately
94 } else {
95 uint64_t delta = pyb_rtc_alarm0_expiry - t;
96 if (delta <= 0xffffffff) {
97 // sleep for the desired time
98 sleep_us = delta;
99 } else {
100 // overflow, just set to maximum sleep time
101 sleep_us = 0xffffffff;
102 }
103 }
104 }
105
106 // put the device in a deep-sleep state
107 system_deep_sleep_set_option(0); // default power down mode; TODO check this
108 system_deep_sleep(sleep_us);
109
110 for (;;) {
111 // we must not return
112 ets_loop_iter();
113 }
114
115 return mp_const_none;
116}
117STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_deepsleep_obj, machine_deepsleep);
118
Paul Sokolovsky71930862016-01-05 21:23:23 +0200119typedef struct _esp_timer_obj_t {
120 mp_obj_base_t base;
121 os_timer_t timer;
122 mp_obj_t callback;
123} esp_timer_obj_t;
124
125const mp_obj_type_t esp_timer_type;
126
127STATIC void esp_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
128 esp_timer_obj_t *self = self_in;
129 mp_printf(print, "Timer(%p)", &self->timer);
130}
131
132STATIC mp_obj_t esp_timer_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
133 mp_arg_check_num(n_args, n_kw, 1, 1, false);
134 esp_timer_obj_t *tim = m_new_obj(esp_timer_obj_t);
135 tim->base.type = &esp_timer_type;
136 return tim;
137}
138
139STATIC void esp_timer_cb(void *arg) {
140 esp_timer_obj_t *self = arg;
Paul Sokolovsky6d103b62016-04-25 19:28:12 +0300141 mp_call_function_1_protected(self->callback, self);
Paul Sokolovsky71930862016-01-05 21:23:23 +0200142}
143
144STATIC mp_obj_t esp_timer_init_helper(esp_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
145 static const mp_arg_t allowed_args[] = {
146// { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
147 { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
148 { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
149 { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
150 };
151
152 // parse args
153 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
154 mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
155
156 self->callback = args[2].u_obj;
Paul Sokolovskyf39bcb32016-01-07 18:58:52 +0200157 // Be sure to disarm timer before making any changes
158 os_timer_disarm(&self->timer);
Paul Sokolovsky71930862016-01-05 21:23:23 +0200159 os_timer_setfn(&self->timer, esp_timer_cb, self);
160 os_timer_arm(&self->timer, args[0].u_int, args[1].u_int);
161
162 return mp_const_none;
163}
164
165STATIC mp_obj_t esp_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
166 return esp_timer_init_helper(args[0], n_args - 1, args + 1, kw_args);
167}
168STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp_timer_init_obj, 1, esp_timer_init);
169
170STATIC mp_obj_t esp_timer_deinit(mp_obj_t self_in) {
171 esp_timer_obj_t *self = self_in;
172 os_timer_disarm(&self->timer);
173 return mp_const_none;
174}
175STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_timer_deinit_obj, esp_timer_deinit);
176
177STATIC const mp_map_elem_t esp_timer_locals_dict_table[] = {
178 { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&esp_timer_deinit_obj },
179 { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&esp_timer_init_obj },
180// { MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&esp_timer_callback_obj },
Paul Sokolovskyc70637b2016-03-04 22:26:59 +0200181 { MP_OBJ_NEW_QSTR(MP_QSTR_ONE_SHOT), MP_OBJ_NEW_SMALL_INT(false) },
182 { MP_OBJ_NEW_QSTR(MP_QSTR_PERIODIC), MP_OBJ_NEW_SMALL_INT(true) },
Paul Sokolovsky71930862016-01-05 21:23:23 +0200183};
184STATIC MP_DEFINE_CONST_DICT(esp_timer_locals_dict, esp_timer_locals_dict_table);
185
186const mp_obj_type_t esp_timer_type = {
187 { &mp_type_type },
188 .name = MP_QSTR_Timer,
189 .print = esp_timer_print,
190 .make_new = esp_timer_make_new,
191 .locals_dict = (mp_obj_t)&esp_timer_locals_dict,
192};
193
Damien George927388e2016-06-01 17:26:49 +0100194// this bit is unused in the Xtensa PS register
195#define ETS_LOOP_ITER_BIT (12)
196
Damien George9a368ae2016-05-20 13:30:33 +0100197STATIC mp_obj_t machine_disable_irq(void) {
Damien George927388e2016-06-01 17:26:49 +0100198 uint32_t state = disable_irq();
199 state = (state & ~(1 << ETS_LOOP_ITER_BIT)) | (ets_loop_iter_disable << ETS_LOOP_ITER_BIT);
200 ets_loop_iter_disable = 1;
201 return mp_obj_new_int(state);
Damien George9a368ae2016-05-20 13:30:33 +0100202}
203MP_DEFINE_CONST_FUN_OBJ_0(machine_disable_irq_obj, machine_disable_irq);
204
Damien George927388e2016-06-01 17:26:49 +0100205STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) {
206 uint32_t state = mp_obj_get_int(state_in);
207 ets_loop_iter_disable = (state >> ETS_LOOP_ITER_BIT) & 1;
208 enable_irq(state & ~(1 << ETS_LOOP_ITER_BIT));
Damien George9a368ae2016-05-20 13:30:33 +0100209 return mp_const_none;
210}
211MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq);
212
Paul Sokolovsky5d7c4082015-12-31 00:51:08 +0200213STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
214 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
215 { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },
216 { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
217 { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },
Paul Sokolovsky71930862016-01-05 21:23:23 +0200218
Damien Georgef7be8032016-02-17 15:19:03 +0000219 { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
Paul Sokolovsky81fd5682016-04-05 00:20:25 +0300220 { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
Paul Sokolovsky050e6452016-04-15 22:07:28 +0300221 { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
Paul Sokolovskyde125022016-04-05 00:57:49 +0300222 { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
Damien George32d7cf62016-04-21 11:43:37 +0100223 { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
Damien Georgef7be8032016-02-17 15:19:03 +0000224
Damien George9a368ae2016-05-20 13:30:33 +0100225 { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
226 { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
227
Damien Georgecff2b7a2016-05-31 14:06:50 +0100228 { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
229
Damien George32d7cf62016-04-21 11:43:37 +0100230 { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
Paul Sokolovsky71930862016-01-05 21:23:23 +0200231 { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&esp_timer_type) },
Paul Sokolovsky26f06162016-02-03 15:23:16 +0200232 { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pyb_pin_type) },
Damien George632d8ef2016-03-02 13:37:27 +0000233 { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&pyb_pwm_type) },
Damien Georgeb62bead2016-03-08 22:59:58 +0000234 { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
Damien George1a0a3232016-04-06 19:45:52 +0300235 { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
Damien Georgea5254932016-04-12 13:55:20 +0100236 { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
Damien George82b95f62016-03-01 22:53:23 +0000237 { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) },
Damien George32d7cf62016-04-21 11:43:37 +0100238
239 // wake abilities
240 { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(MACHINE_WAKE_DEEPSLEEP) },
241
242 // reset causes
243 { MP_ROM_QSTR(MP_QSTR_PWR_ON_RESET), MP_ROM_INT(REASON_EXT_SYS_RST) },
244 { MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(REASON_EXT_SYS_RST) },
245 { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_ROM_INT(REASON_DEEP_SLEEP_AWAKE) },
Paul Sokolovsky5d7c4082015-12-31 00:51:08 +0200246};
247
248STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
249
250const mp_obj_module_t mp_module_machine = {
251 .base = { &mp_type_module },
252 .name = MP_QSTR_umachine,
253 .globals = (mp_obj_dict_t*)&machine_module_globals,
254};
255
256#endif // MICROPY_PY_MACHINE