|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Noralf Trønnes |
| 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 "clocks.h" |
| 28 | +#include "bindings/samd/Clock.h" |
| 29 | + |
| 30 | +#include "py/obj.h" |
| 31 | +#include "py/objproperty.h" |
| 32 | +#include "py/runtime.h" |
| 33 | + |
| 34 | +//| .. currentmodule:: samd |
| 35 | +//| |
| 36 | +//| :class:`Clock` --- Clock reference |
| 37 | +//| ------------------------------------------ |
| 38 | +//| |
| 39 | +//| Identifies a clock on the microcontroller. |
| 40 | +//| |
| 41 | +//| .. class:: Clock |
| 42 | +//| |
| 43 | +//| Identifies a clock on the microcontroller. They are fixed by the |
| 44 | +//| hardware so they cannot be constructed on demand. Instead, use |
| 45 | +//| `samd.clock` to reference the desired clock. |
| 46 | +//| |
| 47 | + |
| 48 | +STATIC void samd_clock_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 49 | + samd_clock_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 50 | + |
| 51 | + mp_printf(print, "%q.%q.%s(", MP_QSTR_samd, MP_QSTR_clock, self->name); |
| 52 | + if (clock_get_enabled(self->type, self->index)) { |
| 53 | + mp_printf(print, "frequency=%u", clock_get_frequency(self->type, self->index)); |
| 54 | + uint32_t calibration = clock_get_calibration(self->type, self->index); |
| 55 | + if (calibration) { |
| 56 | + mp_printf(print, ", calibration=%u", calibration); |
| 57 | + } |
| 58 | + } |
| 59 | + mp_printf(print, ")"); |
| 60 | +} |
| 61 | + |
| 62 | +//| .. attribute:: enabled |
| 63 | +//| |
| 64 | +//| Is the clock enabled? (read-only) |
| 65 | +//| |
| 66 | +STATIC mp_obj_t samd_clock_get_enabled(mp_obj_t self_in) { |
| 67 | + samd_clock_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 68 | + return mp_obj_new_bool(clock_get_enabled(self->type, self->index)); |
| 69 | +} |
| 70 | + |
| 71 | +MP_DEFINE_CONST_FUN_OBJ_1(samd_clock_get_enabled_obj, samd_clock_get_enabled); |
| 72 | + |
| 73 | +const mp_obj_property_t samd_clock_enabled_obj = { |
| 74 | + .base.type = &mp_type_property, |
| 75 | + .proxy = {(mp_obj_t)&samd_clock_get_enabled_obj, |
| 76 | + (mp_obj_t)&mp_const_none_obj, |
| 77 | + (mp_obj_t)&mp_const_none_obj, |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +//| .. attribute:: parent |
| 82 | +//| |
| 83 | +//| Clock parent. (read-only) |
| 84 | +//| |
| 85 | +STATIC mp_obj_t samd_clock_get_parent(mp_obj_t self_in) { |
| 86 | + samd_clock_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 87 | + uint8_t p_type, p_index; |
| 88 | + if (!clock_get_parent(self->type, self->index, &p_type, &p_index)) |
| 89 | + return mp_const_none; |
| 90 | + |
| 91 | + const mp_map_t* samd_map = &samd_clock_globals.map; |
| 92 | + for (uint8_t i = 0; i < samd_map->alloc; i++) { |
| 93 | + samd_clock_obj_t *iter = samd_map->table[i].value; |
| 94 | + if (iter->type == p_type && iter->index == p_index) |
| 95 | + return iter; |
| 96 | + } |
| 97 | + return mp_const_none; |
| 98 | +} |
| 99 | + |
| 100 | +MP_DEFINE_CONST_FUN_OBJ_1(samd_clock_get_parent_obj, samd_clock_get_parent); |
| 101 | + |
| 102 | +const mp_obj_property_t samd_clock_parent_obj = { |
| 103 | + .base.type = &mp_type_property, |
| 104 | + .proxy = {(mp_obj_t)&samd_clock_get_parent_obj, |
| 105 | + (mp_obj_t)&mp_const_none_obj, |
| 106 | + (mp_obj_t)&mp_const_none_obj, |
| 107 | + }, |
| 108 | +}; |
| 109 | + |
| 110 | +//| .. attribute:: frequency |
| 111 | +//| |
| 112 | +//| Clock frequency. (read-only) |
| 113 | +//| |
| 114 | +STATIC mp_obj_t samd_clock_get_frequency(mp_obj_t self_in) { |
| 115 | + samd_clock_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 116 | + return mp_obj_new_int_from_uint(clock_get_frequency(self->type, self->index)); |
| 117 | +} |
| 118 | + |
| 119 | +MP_DEFINE_CONST_FUN_OBJ_1(samd_clock_get_frequency_obj, samd_clock_get_frequency); |
| 120 | + |
| 121 | +const mp_obj_property_t samd_clock_frequency_obj = { |
| 122 | + .base.type = &mp_type_property, |
| 123 | + .proxy = {(mp_obj_t)&samd_clock_get_frequency_obj, |
| 124 | + (mp_obj_t)&mp_const_none_obj, |
| 125 | + (mp_obj_t)&mp_const_none_obj, |
| 126 | + }, |
| 127 | +}; |
| 128 | + |
| 129 | +//| .. attribute:: calibration |
| 130 | +//| |
| 131 | +//| Clock calibration. Not all clocks can be calibrated. |
| 132 | +//| |
| 133 | +STATIC mp_obj_t samd_clock_get_calibration(mp_obj_t self_in) { |
| 134 | + samd_clock_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 135 | + return mp_obj_new_int_from_uint(clock_get_calibration(self->type, self->index)); |
| 136 | +} |
| 137 | + |
| 138 | +MP_DEFINE_CONST_FUN_OBJ_1(samd_clock_get_calibration_obj, samd_clock_get_calibration); |
| 139 | + |
| 140 | +STATIC mp_obj_t samd_clock_set_calibration(mp_obj_t self_in, mp_obj_t calibration) { |
| 141 | + samd_clock_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 142 | + int ret = clock_set_calibration(self->type, self->index, mp_obj_get_int(calibration)); |
| 143 | + if (ret == -2) |
| 144 | + mp_raise_AttributeError("calibration is read only"); |
| 145 | + if (ret == -1) |
| 146 | + mp_raise_ValueError("calibration is out of range"); |
| 147 | + return mp_const_none; |
| 148 | +} |
| 149 | + |
| 150 | +MP_DEFINE_CONST_FUN_OBJ_2(samd_clock_set_calibration_obj, samd_clock_set_calibration); |
| 151 | + |
| 152 | +const mp_obj_property_t samd_clock_calibration_obj = { |
| 153 | + .base.type = &mp_type_property, |
| 154 | + .proxy = {(mp_obj_t)&samd_clock_get_calibration_obj, |
| 155 | + (mp_obj_t)&samd_clock_set_calibration_obj, |
| 156 | + (mp_obj_t)&mp_const_none_obj, |
| 157 | + }, |
| 158 | +}; |
| 159 | + |
| 160 | +STATIC const mp_rom_map_elem_t samd_clock_locals_dict_table[] = { |
| 161 | + { MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&samd_clock_enabled_obj) }, |
| 162 | + { MP_ROM_QSTR(MP_QSTR_parent), MP_ROM_PTR(&samd_clock_parent_obj) }, |
| 163 | + { MP_ROM_QSTR(MP_QSTR_frequency), MP_ROM_PTR(&samd_clock_frequency_obj) }, |
| 164 | + { MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&samd_clock_calibration_obj) }, |
| 165 | +}; |
| 166 | + |
| 167 | +STATIC MP_DEFINE_CONST_DICT(samd_clock_locals_dict, samd_clock_locals_dict_table); |
| 168 | + |
| 169 | +const mp_obj_type_t samd_clock_type = { |
| 170 | + { &mp_type_type }, |
| 171 | + .name = MP_QSTR_Clock, |
| 172 | + .print = samd_clock_print, |
| 173 | + .locals_dict = (mp_obj_t)&samd_clock_locals_dict, |
| 174 | +}; |
0 commit comments