Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 1 | /* |
| 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 Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 7 | * Copyright (c) 2016 Paul Sokolovsky |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 8 | * |
| 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 Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 31 | #include "py/runtime.h" |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 32 | #include "extmod/machine_mem.h" |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 33 | #include "utils.h" |
Paul Sokolovsky | 26f0616 | 2016-02-03 15:23:16 +0200 | [diff] [blame] | 34 | #include "modpyb.h" |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 35 | |
| 36 | #include "os_type.h" |
| 37 | #include "osapi.h" |
Paul Sokolovsky | 98b727c | 2016-02-06 01:36:17 +0200 | [diff] [blame] | 38 | #include "etshal.h" |
Damien George | f7be803 | 2016-02-17 15:19:03 +0000 | [diff] [blame] | 39 | #include "user_interface.h" |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 40 | |
| 41 | #if MICROPY_PY_MACHINE |
| 42 | |
Damien George | f7be803 | 2016-02-17 15:19:03 +0000 | [diff] [blame] | 43 | STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) { |
| 44 | if (n_args == 0) { |
| 45 | // get |
| 46 | return mp_obj_new_int(system_get_cpu_freq() * 1000000); |
| 47 | } else { |
| 48 | // set |
| 49 | mp_int_t freq = mp_obj_get_int(args[0]) / 1000000; |
| 50 | if (freq != 80 && freq != 160) { |
| 51 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, |
| 52 | "frequency can only be either 80Mhz or 160MHz")); |
| 53 | } |
| 54 | system_update_cpu_freq(freq); |
| 55 | return mp_const_none; |
| 56 | } |
| 57 | } |
| 58 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq); |
| 59 | |
Paul Sokolovsky | 81fd568 | 2016-04-05 00:20:25 +0300 | [diff] [blame^] | 60 | STATIC mp_obj_t machine_reset(void) { |
| 61 | system_restart(); |
| 62 | return mp_const_none; |
| 63 | } |
| 64 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); |
| 65 | |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 66 | typedef struct _esp_timer_obj_t { |
| 67 | mp_obj_base_t base; |
| 68 | os_timer_t timer; |
| 69 | mp_obj_t callback; |
| 70 | } esp_timer_obj_t; |
| 71 | |
| 72 | const mp_obj_type_t esp_timer_type; |
| 73 | |
| 74 | STATIC void esp_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 75 | esp_timer_obj_t *self = self_in; |
| 76 | mp_printf(print, "Timer(%p)", &self->timer); |
| 77 | } |
| 78 | |
| 79 | STATIC 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) { |
| 80 | mp_arg_check_num(n_args, n_kw, 1, 1, false); |
| 81 | esp_timer_obj_t *tim = m_new_obj(esp_timer_obj_t); |
| 82 | tim->base.type = &esp_timer_type; |
| 83 | return tim; |
| 84 | } |
| 85 | |
| 86 | STATIC void esp_timer_cb(void *arg) { |
| 87 | esp_timer_obj_t *self = arg; |
| 88 | call_function_1_protected(self->callback, self); |
| 89 | } |
| 90 | |
| 91 | STATIC 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) { |
| 92 | static const mp_arg_t allowed_args[] = { |
| 93 | // { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 94 | { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} }, |
| 95 | { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, |
| 96 | { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 97 | }; |
| 98 | |
| 99 | // parse args |
| 100 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 101 | mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 102 | |
| 103 | self->callback = args[2].u_obj; |
Paul Sokolovsky | f39bcb3 | 2016-01-07 18:58:52 +0200 | [diff] [blame] | 104 | // Be sure to disarm timer before making any changes |
| 105 | os_timer_disarm(&self->timer); |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 106 | os_timer_setfn(&self->timer, esp_timer_cb, self); |
| 107 | os_timer_arm(&self->timer, args[0].u_int, args[1].u_int); |
| 108 | |
| 109 | return mp_const_none; |
| 110 | } |
| 111 | |
| 112 | STATIC mp_obj_t esp_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
| 113 | return esp_timer_init_helper(args[0], n_args - 1, args + 1, kw_args); |
| 114 | } |
| 115 | STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp_timer_init_obj, 1, esp_timer_init); |
| 116 | |
| 117 | STATIC mp_obj_t esp_timer_deinit(mp_obj_t self_in) { |
| 118 | esp_timer_obj_t *self = self_in; |
| 119 | os_timer_disarm(&self->timer); |
| 120 | return mp_const_none; |
| 121 | } |
| 122 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_timer_deinit_obj, esp_timer_deinit); |
| 123 | |
| 124 | STATIC const mp_map_elem_t esp_timer_locals_dict_table[] = { |
| 125 | { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&esp_timer_deinit_obj }, |
| 126 | { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&esp_timer_init_obj }, |
| 127 | // { MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&esp_timer_callback_obj }, |
Paul Sokolovsky | c70637b | 2016-03-04 22:26:59 +0200 | [diff] [blame] | 128 | { MP_OBJ_NEW_QSTR(MP_QSTR_ONE_SHOT), MP_OBJ_NEW_SMALL_INT(false) }, |
| 129 | { MP_OBJ_NEW_QSTR(MP_QSTR_PERIODIC), MP_OBJ_NEW_SMALL_INT(true) }, |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 130 | }; |
| 131 | STATIC MP_DEFINE_CONST_DICT(esp_timer_locals_dict, esp_timer_locals_dict_table); |
| 132 | |
| 133 | const mp_obj_type_t esp_timer_type = { |
| 134 | { &mp_type_type }, |
| 135 | .name = MP_QSTR_Timer, |
| 136 | .print = esp_timer_print, |
| 137 | .make_new = esp_timer_make_new, |
| 138 | .locals_dict = (mp_obj_t)&esp_timer_locals_dict, |
| 139 | }; |
| 140 | |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 141 | STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { |
| 142 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, |
| 143 | { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, |
| 144 | { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, |
| 145 | { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 146 | |
Damien George | f7be803 | 2016-02-17 15:19:03 +0000 | [diff] [blame] | 147 | { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, |
Paul Sokolovsky | 81fd568 | 2016-04-05 00:20:25 +0300 | [diff] [blame^] | 148 | { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, |
Damien George | f7be803 | 2016-02-17 15:19:03 +0000 | [diff] [blame] | 149 | |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 150 | { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&esp_timer_type) }, |
Paul Sokolovsky | 26f0616 | 2016-02-03 15:23:16 +0200 | [diff] [blame] | 151 | { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pyb_pin_type) }, |
Damien George | 632d8ef | 2016-03-02 13:37:27 +0000 | [diff] [blame] | 152 | { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&pyb_pwm_type) }, |
Damien George | b62bead | 2016-03-08 22:59:58 +0000 | [diff] [blame] | 153 | { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) }, |
Damien George | dd32f02 | 2016-02-11 12:43:41 +0000 | [diff] [blame] | 154 | { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&pyb_i2c_type) }, |
Damien George | 82b95f6 | 2016-03-01 22:53:23 +0000 | [diff] [blame] | 155 | { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&pyb_spi_type) }, |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); |
| 159 | |
| 160 | const mp_obj_module_t mp_module_machine = { |
| 161 | .base = { &mp_type_module }, |
| 162 | .name = MP_QSTR_umachine, |
| 163 | .globals = (mp_obj_dict_t*)&machine_module_globals, |
| 164 | }; |
| 165 | |
| 166 | #endif // MICROPY_PY_MACHINE |