/* * This file is part of the Micro Python project, http://micropython.org/ * * The MIT License (MIT) * * Copyright (c) 2016 Glenn Ruben Bakke * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include "py/nlr.h" #include "py/runtime.h" #include "py/mphal.h" #include "rtc.h" #include "hal_rtc.h" #if MICROPY_PY_MACHINE_RTC typedef struct _machine_rtc_obj_t { mp_obj_base_t base; RTC_HandleTypeDef *rtc; } machine_rtc_obj_t; RTC_HandleTypeDef RTCHandle0 = {.instance = NULL, .id = 0}; RTC_HandleTypeDef RTCHandle1 = {.instance = NULL, .id = 1}; STATIC const machine_rtc_obj_t machine_rtc_obj[] = { {{&machine_rtc_type}, &RTCHandle0}, {{&machine_rtc_type}, &RTCHandle1}, }; void rtc_init0(void) { // reset the RTC handles memset(&RTCHandle0, 0, sizeof(RTC_HandleTypeDef)); RTCHandle0.instance = RTC0; memset(&RTCHandle1, 0, sizeof(RTC_HandleTypeDef)); RTCHandle1.instance = RTC1; } STATIC int rtc_find(mp_obj_t id) { // given an integer id int rtc_id = mp_obj_get_int(id); if (rtc_id >= 0 && rtc_id <= MP_ARRAY_SIZE(machine_rtc_obj) && machine_rtc_obj[rtc_id].rtc != NULL) { return rtc_id; } nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "RTC(%d) does not exist", rtc_id)); } STATIC void rtc_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) { machine_rtc_obj_t *self = o; mp_printf(print, "RTC(%u)", self->rtc->id); } /******************************************************************************/ /* MicroPython bindings for machine API */ // for make_new enum { ARG_NEW_id, }; STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)} }, }; // parse args mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); if (args[ARG_NEW_id].u_obj == MP_OBJ_NEW_SMALL_INT(-1)) { // index -1 does not exist return mp_const_none; // TODO: raise exception } // get static peripheral object int rtc_id = rtc_find(args[ARG_NEW_id].u_obj); const machine_rtc_obj_t *self = &machine_rtc_obj[rtc_id]; hal_rtc_init(self->rtc->instance, &self->rtc->init); return MP_OBJ_FROM_PTR(self); } const mp_obj_type_t machine_rtc_type = { { &mp_type_type }, .name = MP_QSTR_RTC, .print = rtc_print, .make_new = machine_rtc_make_new, #if 0 .locals_dict = (mp_obj_t)&machine_rtc_locals_dict #endif }; #endif // MICROPY_PY_MACHINE_RTC