From d2b1b747a1b8d3456b062c90b2f5b6f55d5dab2a Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Tue, 5 Mar 2024 15:31:06 -0700 Subject: [PATCH 01/11] Add support for RP2040 GPIN/GPOUT (clkio) --- ports/raspberrypi/Makefile | 11 + ports/raspberrypi/bindings/clkio/ClkAuxSrc.c | 200 +++++++++++++++++ ports/raspberrypi/bindings/clkio/ClkAuxSrc.h | 66 ++++++ ports/raspberrypi/bindings/clkio/ClkIndex.c | 188 ++++++++++++++++ ports/raspberrypi/bindings/clkio/ClkIndex.h | 65 ++++++ ports/raspberrypi/bindings/clkio/ClkInput.c | 205 ++++++++++++++++++ ports/raspberrypi/bindings/clkio/ClkInput.h | 31 +++ ports/raspberrypi/bindings/clkio/ClkOutput.c | 176 +++++++++++++++ ports/raspberrypi/bindings/clkio/ClkOutput.h | 31 +++ ports/raspberrypi/bindings/clkio/__init__.c | 55 +++++ ports/raspberrypi/bindings/clkio/__init__.h | 29 +++ ports/raspberrypi/common-hal/clkio/ClkInput.c | 94 ++++++++ ports/raspberrypi/common-hal/clkio/ClkInput.h | 48 ++++ .../raspberrypi/common-hal/clkio/ClkOutput.c | 93 ++++++++ .../raspberrypi/common-hal/clkio/ClkOutput.h | 47 ++++ ports/raspberrypi/common-hal/clkio/__init__.c | 1 + py/circuitpy_mpconfig.mk | 6 + 17 files changed, 1346 insertions(+) create mode 100644 ports/raspberrypi/bindings/clkio/ClkAuxSrc.c create mode 100644 ports/raspberrypi/bindings/clkio/ClkAuxSrc.h create mode 100644 ports/raspberrypi/bindings/clkio/ClkIndex.c create mode 100644 ports/raspberrypi/bindings/clkio/ClkIndex.h create mode 100644 ports/raspberrypi/bindings/clkio/ClkInput.c create mode 100644 ports/raspberrypi/bindings/clkio/ClkInput.h create mode 100644 ports/raspberrypi/bindings/clkio/ClkOutput.c create mode 100644 ports/raspberrypi/bindings/clkio/ClkOutput.h create mode 100644 ports/raspberrypi/bindings/clkio/__init__.c create mode 100644 ports/raspberrypi/bindings/clkio/__init__.h create mode 100644 ports/raspberrypi/common-hal/clkio/ClkInput.c create mode 100644 ports/raspberrypi/common-hal/clkio/ClkInput.h create mode 100644 ports/raspberrypi/common-hal/clkio/ClkOutput.c create mode 100644 ports/raspberrypi/common-hal/clkio/ClkOutput.h create mode 100644 ports/raspberrypi/common-hal/clkio/__init__.c diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 2eb81017b3ed..9930c3226bad 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -259,6 +259,17 @@ SRC_C += \ $(SRC_CYW43) \ $(SRC_LWIP) \ +ifeq ($(CIRCUITPY_RP2CLKIO),1) +SRC_C += \ + bindings/clkio/__init__.c \ + bindings/clkio/ClkOutput.c \ + bindings/clkio/ClkInput.c \ + bindings/clkio/ClkAuxSrc.c \ + bindings/clkio/ClkIndex.c \ + common-hal/clkio/ClkOutput.c \ + common-hal/clkio/ClkInput.c \ + +endif ifeq ($(CIRCUITPY_USB_HOST), 1) SRC_C += \ diff --git a/ports/raspberrypi/bindings/clkio/ClkAuxSrc.c b/ports/raspberrypi/bindings/clkio/ClkAuxSrc.c new file mode 100644 index 000000000000..34fa26a56821 --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/ClkAuxSrc.c @@ -0,0 +1,200 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "py/runtime.h" +#include "supervisor/shared/translate/translate.h" +#include "bindings/clkio/ClkAuxSrc.h" + +//| class ClkAuxSrc: +//| """Defines the input clock for GPOUT on RP2040""" +//| +//| def __init__(self) -> None: +//| """Enum-like class to define the clock src.""" +//| +const mp_obj_type_t clkio_clkauxsrc_type; + +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_sys_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_gpin0_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_gpin1_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_usb_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_rosc_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_xosc_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_sys_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_usb_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_adc_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_rtc_obj = { + { &clkio_clkauxsrc_type }, +}; +const clkio_clkauxsrc_obj_t clkio_clkauxsrc_ref_obj = { + { &clkio_clkauxsrc_type }, +}; + + +STATIC const mp_rom_map_elem_t clkio_clkauxsrc_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_PLL_SYS), MP_ROM_PTR(&clkio_clkauxsrc_pll_sys_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPIN0), MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPIN1), MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj) }, + { MP_ROM_QSTR(MP_QSTR_PLL_USB), MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj) }, + { MP_ROM_QSTR(MP_QSTR_PLL_ROSC), MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj) }, + { MP_ROM_QSTR(MP_QSTR_PLL_XOSC), MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj) }, + { MP_ROM_QSTR(MP_QSTR_SYS), MP_ROM_PTR(&clkio_clkauxsrc_sys_obj) }, + { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&clkio_clkauxsrc_usb_obj) }, + { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&clkio_clkauxsrc_adc_obj) }, + { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj) }, + { MP_ROM_QSTR(MP_QSTR_REF), MP_ROM_PTR(&clkio_clkauxsrc_ref_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(clkio_clkauxsrc_locals_dict, clkio_clkauxsrc_locals_dict_table); + +STATIC void clkio_clkauxsrc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr clk = MP_QSTR_INVALID; + if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_sys_obj)) { + clk = MP_QSTR_PLL_SYS; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj)) { + clk = MP_QSTR_GPIN0; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj)) { + clk = MP_QSTR_GPIN1; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj)) { + clk = MP_QSTR_PLL_USB; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj)) { + clk = MP_QSTR_PLL_ROSC; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj)) { + clk = MP_QSTR_PLL_XOSC; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_sys_obj)) { + clk = MP_QSTR_SYS; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_usb_obj)) { + clk = MP_QSTR_USB; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_adc_obj)) { + clk = MP_QSTR_ADC; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj)) { + clk = MP_QSTR_RTC; + } + else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_ref_obj)) { + clk = MP_QSTR_REF; + } + mp_printf(print, "%q.%q.%q", MP_QSTR_clkio, MP_QSTR_ClkAuxSrc, clk); +} + +MP_DEFINE_CONST_OBJ_TYPE( + clkio_clkauxsrc_type, + MP_QSTR_ClkAuxSrc, + MP_TYPE_FLAG_NONE, + print, clkio_clkauxsrc_print, + locals_dict, &clkio_clkauxsrc_locals_dict + ); + +mp_obj_t clkauxsrc_get_obj (clkio_clkauxsrc_t type) +{ + if (type == CLKAUXSRC_PLL_SYS) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_sys_obj); + else if (type == CLKAUXSRC_GPIN0) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_gpin0_obj); + else if (type == CLKAUXSRC_GPIN1) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_gpin1_obj); + else if (type == CLKAUXSRC_PLL_USB) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_usb_obj); + else if (type == CLKAUXSRC_PLL_ROSC) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_rosc_obj); + else if (type == CLKAUXSRC_PLL_XOSC) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_xosc_obj); + else if (type == CLKAUXSRC_SYS) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_sys_obj); + else if (type == CLKAUXSRC_USB) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_usb_obj); + else if (type == CLKAUXSRC_ADC) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_adc_obj); + else if (type == CLKAUXSRC_RTC) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_rtc_obj); + else if (type == CLKAUXSRC_REF) + return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_ref_obj); + else + return MP_ROM_NONE; +} +clkio_clkauxsrc_t validate_clkauxsrc(mp_rom_obj_t obj, qstr arg_name) { + if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_sys_obj)) { + return CLKAUXSRC_PLL_SYS; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj)) { + return CLKAUXSRC_GPIN0; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj)) { + return CLKAUXSRC_GPIN1; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj)) { + return CLKAUXSRC_PLL_USB; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj)) { + return CLKAUXSRC_PLL_ROSC; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj)) { + return CLKAUXSRC_PLL_XOSC; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_sys_obj)) { + return CLKAUXSRC_SYS; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_usb_obj)) { + return CLKAUXSRC_USB; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_adc_obj)) { + return CLKAUXSRC_ADC; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj)) { + return CLKAUXSRC_RTC; + } + else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_ref_obj)) { + return CLKAUXSRC_REF; + } + else if (obj == MP_ROM_NONE) { + return CLKAUXSRC_NONE; + } + mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_ClkAuxSrc, MP_QSTR_None, mp_obj_get_type(obj)->name); +} diff --git a/ports/raspberrypi/bindings/clkio/ClkAuxSrc.h b/ports/raspberrypi/bindings/clkio/ClkAuxSrc.h new file mode 100644 index 000000000000..4eeaacbbf9c8 --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/ClkAuxSrc.h @@ -0,0 +1,66 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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. + */ + +#pragma once + +#include "py/obj.h" + +// Output sources +typedef enum _clkio_clkauxsrc_t { + CLKAUXSRC_PLL_SYS = 0, + CLKAUXSRC_GPIN0 = 1, + CLKAUXSRC_GPIN1 = 2, + CLKAUXSRC_PLL_USB = 3, + CLKAUXSRC_PLL_ROSC = 4, + CLKAUXSRC_PLL_XOSC = 5, + CLKAUXSRC_SYS = 6, + CLKAUXSRC_USB = 7, + CLKAUXSRC_ADC = 8, + CLKAUXSRC_RTC = 9, + CLKAUXSRC_REF = 10, + CLKAUXSRC_NONE = 11, +} clkio_clkauxsrc_t; + +extern const mp_obj_type_t clkio_clkauxsrc_type; + +typedef struct { + mp_obj_base_t base; +} clkio_clkauxsrc_obj_t; + +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_sys_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_gpin0_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_gpin1_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_usb_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_rosc_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_xosc_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_sys_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_usb_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_adc_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_rtc_obj; +extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_ref_obj; + +clkio_clkauxsrc_t validate_clkauxsrc(mp_rom_obj_t obj, qstr arg_name); +mp_obj_t clkauxsrc_get_obj (clkio_clkauxsrc_t type); diff --git a/ports/raspberrypi/bindings/clkio/ClkIndex.c b/ports/raspberrypi/bindings/clkio/ClkIndex.c new file mode 100644 index 000000000000..43f8519a394f --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/ClkIndex.c @@ -0,0 +1,188 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "py/runtime.h" +#include "supervisor/shared/translate/translate.h" +#include "bindings/clkio/ClkIndex.h" + +//| class ClkIndex: +//| """Defines the internal clock index to drive GPIN from an external pin.""" +//| +//| def __init__(self) -> None: +//| """Enum-like class to define the internal clock index.""" +//| +const mp_obj_type_t clkio_clkindex_type; + +const clkio_clkindex_obj_t clkio_clkindex_gpout0_obj = { + { &clkio_clkindex_type }, +}; +const clkio_clkindex_obj_t clkio_clkindex_gpout1_obj = { + { &clkio_clkindex_type }, +}; +const clkio_clkindex_obj_t clkio_clkindex_gpout2_obj = { + { &clkio_clkindex_type }, +}; +const clkio_clkindex_obj_t clkio_clkindex_gpout3_obj = { + { &clkio_clkindex_type }, +}; +const clkio_clkindex_obj_t clkio_clkindex_ref_obj = { + { &clkio_clkindex_type }, +}; +const clkio_clkindex_obj_t clkio_clkindex_sys_obj = { + { &clkio_clkindex_type }, +}; +const clkio_clkindex_obj_t clkio_clkindex_peri_obj = { + { &clkio_clkindex_type }, +}; +const clkio_clkindex_obj_t clkio_clkindex_usb_obj = { + { &clkio_clkindex_type }, +}; +const clkio_clkindex_obj_t clkio_clkindex_adc_obj = { + { &clkio_clkindex_type }, +}; +const clkio_clkindex_obj_t clkio_clkindex_rtc_obj = { + { &clkio_clkindex_type }, +}; + + +STATIC const mp_rom_map_elem_t clkio_clkindex_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_GPOUT0), MP_ROM_PTR(&clkio_clkindex_gpout0_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPOUT1), MP_ROM_PTR(&clkio_clkindex_gpout1_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPOUT2), MP_ROM_PTR(&clkio_clkindex_gpout2_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPOUT3), MP_ROM_PTR(&clkio_clkindex_gpout3_obj) }, + { MP_ROM_QSTR(MP_QSTR_REF), MP_ROM_PTR(&clkio_clkindex_ref_obj) }, + { MP_ROM_QSTR(MP_QSTR_SYS), MP_ROM_PTR(&clkio_clkindex_sys_obj) }, + { MP_ROM_QSTR(MP_QSTR_PERI), MP_ROM_PTR(&clkio_clkindex_peri_obj) }, + { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&clkio_clkindex_usb_obj) }, + { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&clkio_clkindex_adc_obj) }, + { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&clkio_clkindex_rtc_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(clkio_clkindex_locals_dict, clkio_clkindex_locals_dict_table); + +STATIC void clkio_clkindex_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr clk = MP_QSTR_INVALID; + if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout0_obj)) { + clk = MP_QSTR_GPOUT0; + } + else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout1_obj)) { + clk = MP_QSTR_GPOUT1; + } + else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout2_obj)) { + clk = MP_QSTR_GPOUT2; + } + else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout3_obj)) { + clk = MP_QSTR_GPOUT3; + } + else if (self_in == MP_ROM_PTR(&clkio_clkindex_ref_obj)) { + clk = MP_QSTR_REF; + } + else if (self_in == MP_ROM_PTR(&clkio_clkindex_sys_obj)) { + clk = MP_QSTR_SYS; + } + else if (self_in == MP_ROM_PTR(&clkio_clkindex_peri_obj)) { + clk = MP_QSTR_PERI; + } + else if (self_in == MP_ROM_PTR(&clkio_clkindex_usb_obj)) { + clk = MP_QSTR_USB; + } + else if (self_in == MP_ROM_PTR(&clkio_clkindex_adc_obj)) { + clk = MP_QSTR_ADC; + } + else if (self_in == MP_ROM_PTR(&clkio_clkindex_rtc_obj)) { + clk = MP_QSTR_RTC; + } + mp_printf(print, "%q.%q.%q", MP_QSTR_clkio, MP_QSTR_ClkIndex, clk); +} + +MP_DEFINE_CONST_OBJ_TYPE( + clkio_clkindex_type, + MP_QSTR_ClkIndex, + MP_TYPE_FLAG_NONE, + print, clkio_clkindex_print, + locals_dict, &clkio_clkindex_locals_dict + ); + +mp_obj_t clkindex_get_obj(clkio_clkindex_t type) { + if (type == CLKINDEX_GPOUT0) + return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout0_obj); + else if (type == CLKINDEX_GPOUT1) + return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout1_obj); + else if (type == CLKINDEX_GPOUT2) + return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout2_obj); + else if (type == CLKINDEX_GPOUT3) + return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout3_obj); + else if (type == CLKINDEX_REF) + return MP_OBJ_FROM_PTR(&clkio_clkindex_ref_obj); + else if (type == CLKINDEX_SYS) + return MP_OBJ_FROM_PTR(&clkio_clkindex_sys_obj); + else if (type == CLKINDEX_PERI) + return MP_OBJ_FROM_PTR(&clkio_clkindex_peri_obj); + else if (type == CLKINDEX_USB) + return MP_OBJ_FROM_PTR(&clkio_clkindex_usb_obj); + else if (type == CLKINDEX_ADC) + return MP_OBJ_FROM_PTR(&clkio_clkindex_adc_obj); + else if (type == CLKINDEX_RTC) + return MP_OBJ_FROM_PTR(&clkio_clkindex_rtc_obj); + else + return MP_ROM_NONE; +} + +clkio_clkindex_t validate_clkindex(mp_rom_obj_t obj, qstr arg_name) { + if (obj == MP_ROM_PTR(&clkio_clkindex_gpout0_obj)) { + return CLKINDEX_GPOUT0; + } + else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout1_obj)) { + return CLKINDEX_GPOUT1; + } + else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout2_obj)) { + return CLKINDEX_GPOUT2; + } + else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout3_obj)) { + return CLKINDEX_GPOUT3; + } + else if (obj == MP_ROM_PTR(&clkio_clkindex_ref_obj)) { + return CLKINDEX_REF; + } + else if (obj == MP_ROM_PTR(&clkio_clkindex_sys_obj)) { + return CLKINDEX_SYS; + } + else if (obj == MP_ROM_PTR(&clkio_clkindex_peri_obj)) { + return CLKINDEX_PERI; + } + else if (obj == MP_ROM_PTR(&clkio_clkindex_usb_obj)) { + return CLKINDEX_USB; + } + else if (obj == MP_ROM_PTR(&clkio_clkindex_adc_obj)) { + return CLKINDEX_ADC; + } + else if (obj == MP_ROM_PTR(&clkio_clkindex_rtc_obj)) { + return CLKINDEX_RTC; + } + else if (obj == MP_ROM_NONE) { + return CLKINDEX_NONE; + } + mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_ClkIndex, MP_QSTR_None, mp_obj_get_type(obj)->name); +} diff --git a/ports/raspberrypi/bindings/clkio/ClkIndex.h b/ports/raspberrypi/bindings/clkio/ClkIndex.h new file mode 100644 index 000000000000..3edad726c8ab --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/ClkIndex.h @@ -0,0 +1,65 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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. + */ + +#pragma once + +#include "py/obj.h" +#include "hardware/clocks.h" + +// Input sources +typedef enum _clkio_clkindex_t { + CLKINDEX_GPOUT0 = clk_gpout0, + CLKINDEX_GPOUT1 = clk_gpout1, + CLKINDEX_GPOUT2 = clk_gpout2, + CLKINDEX_GPOUT3 = clk_gpout3, + CLKINDEX_REF = clk_ref, + CLKINDEX_SYS = clk_sys, + CLKINDEX_PERI = clk_peri, + CLKINDEX_USB = clk_usb, + CLKINDEX_ADC = clk_adc, + CLKINDEX_RTC = clk_rtc, + CLKINDEX_NONE = CLK_COUNT +} clkio_clkindex_t; + +extern const mp_obj_type_t clkio_clkindex_type; + +typedef struct { + mp_obj_base_t base; +} clkio_clkindex_obj_t; + +extern const clkio_clkindex_obj_t clkio_clkindex_gpout0_obj; +extern const clkio_clkindex_obj_t clkio_clkindex_gpout1_obj; +extern const clkio_clkindex_obj_t clkio_clkindex_gpout2_obj; +extern const clkio_clkindex_obj_t clkio_clkindex_gpout3_obj; +extern const clkio_clkindex_obj_t clkio_clkindex_ref_obj; +extern const clkio_clkindex_obj_t clkio_clkindex_sys_obj; +extern const clkio_clkindex_obj_t clkio_clkindex_peri_obj; +extern const clkio_clkindex_obj_t clkio_clkindex_usb_obj; +extern const clkio_clkindex_obj_t clkio_clkindex_adc_obj; +extern const clkio_clkindex_obj_t clkio_clkindex_rtc_obj; + +clkio_clkindex_t validate_clkindex(mp_rom_obj_t obj, qstr arg_name); +mp_obj_t clkindex_get_obj(clkio_clkindex_t type); diff --git a/ports/raspberrypi/bindings/clkio/ClkInput.c b/ports/raspberrypi/bindings/clkio/ClkInput.c new file mode 100644 index 000000000000..a266f5ee9582 --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/ClkInput.c @@ -0,0 +1,205 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "py/obj.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/objarray.h" + +#include "shared-bindings/util.h" +#include "bindings/clkio/ClkInput.h" +#include "common-hal/clkio/ClkInput.h" + +STATIC void check_for_deinit (clkio_clkinput_obj_t *self) { + if (common_hal_clkio_clkinput_deinited (self)) { + raise_deinited_error (); + } +} + +//| class ClkInput: +//| def __init__( +//| self, +//| pin: microcontroller.Pin, +//| *, +//| clkindex: clkio.ClkIndex, +//| src_freq: uint32, +//| target_freq: uint32 +//| ) -> None: +//| """Creates a clock input pin object. +//| pin: Pin to be used as clock input, allowed pins: 20,22 +//| clkindex: points to the destination clock to be connected to the input pin. +//| src_freq: External input frequency at the pin. +//| target_freq: Desired frequency for clkindex. +//| """ + +STATIC mp_obj_t clkio_clkinput_make_new (const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_pin, ARG_clkindex, ARG_src_freq, ARG_target_freq }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_clkindex, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, + { MP_QSTR_src_freq, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} }, + { MP_QSTR_target_freq, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} }, + }; + 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); + + clkio_clkinput_obj_t *self = m_new_obj(clkio_clkinput_obj_t); + self->base.type = &clkio_clkinput_type; + + // Validate pin number + self->pin = args[ARG_pin].u_rom_obj; + + // Validate pin based on clock + if (args[ARG_clkindex].u_rom_obj != mp_const_none) { + common_hal_clkio_clkinput_validate_clkindex_pin (self->pin); + self->clkindex = validate_clkindex (args[ARG_clkindex].u_rom_obj, MP_QSTR_clkindex); + self->src_freq = args[ARG_src_freq].u_int; + self->target_freq = args[ARG_target_freq].u_int; + // Validate frequencies if set + if (self->src_freq && self->target_freq) + common_hal_clkio_clkinput_validate_freqs (args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); + } + else { + self->clkindex = CLKINDEX_NONE; + } + self->enabled = false; + return MP_OBJ_FROM_PTR (self); +}; + +//| def deinit(self) -> None: +//| """Releases the pin and frees any resources.""" +STATIC mp_obj_t clkio_clkinput_deinit (mp_obj_t self_in) { + clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); + // Release pin + common_hal_clkio_clkinput_deinit (self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkinput_deinit_obj, clkio_clkinput_deinit); + +//| def enable(self) -> None: +//| """Configures the pin and enables the internal clock""" +STATIC mp_obj_t clkio_clkinput_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit (self); + common_hal_clkio_clkinput_enable (self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_enable_obj, 1, clkio_clkinput_enable); + +//| def disable(self) -> None: +//| """Disableds the pin and internal clock""" +STATIC mp_obj_t clkio_clkinput_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit (self); + common_hal_clkio_clkinput_disable (self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_disable_obj, 1, clkio_clkinput_disable); + +//| def set_freq( +//| self, +//| src_freq -> uint32 +//| target_freq -> uint32 +//| ) -> None: +//| """Configures the src and target frequency. Must be set before enable() is called.""" +//| +STATIC mp_obj_t clkio_clkinput_set_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_src_freq, ARG_target_freq }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_src_freq, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_target_freq, MP_ARG_REQUIRED | MP_ARG_INT }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit (self); + common_hal_clkio_clkinput_validate_freqs (args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); + self->src_freq = args[ARG_src_freq].u_int; + self->target_freq = args[ARG_target_freq].u_int; + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_set_freq_obj, 1, clkio_clkinput_set_freq); + +//| def set_freq( +//| self, +//| ) -> tuple (src_freq, target_freq): +//| """Returns the src and target frequency.""" +//| +STATIC mp_obj_t clkio_clkinput_get_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit (self); + mp_obj_t tup[] = { + mp_obj_new_int (self->src_freq), + mp_obj_new_int (self->target_freq)}; + // Return tuple with (src, target) + return mp_obj_new_tuple (2, tup); +} +MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_get_freq_obj, 1, clkio_clkinput_get_freq); + +//| clkindex: clkio.ClkIndex +//| """Get clock that will be driven from external pin.""" +static mp_obj_t clkio_clkinput_clkindex_get (mp_obj_t self_in) { + clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit (self); + return clkindex_get_obj (self->clkindex); +} +MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkinput_clkindex_get_obj, clkio_clkinput_clkindex_get); + +//| clkindex: clkio.ClkIndex +//| """Set clock that will be driven from external pin.""" +static mp_obj_t clkio_clkinput_clkindex_set (mp_obj_t self_in, mp_obj_t clkindex_obj) { + clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit (self); + self->clkindex = validate_clkindex (clkindex_obj, MP_QSTR_clkindex); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(clkio_clkinput_clkindex_set_obj, clkio_clkinput_clkindex_set); +MP_PROPERTY_GETSET(clkio_clkinput_clkindex_obj, + (mp_obj_t)&clkio_clkinput_clkindex_get_obj, + (mp_obj_t)&clkio_clkinput_clkindex_set_obj); + + +STATIC const mp_rom_map_elem_t clkio_clkinput_locals_dict_table[] = { + // Functions + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&clkio_clkinput_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&clkio_clkinput_enable_obj) }, + { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&clkio_clkinput_disable_obj) }, + { MP_ROM_QSTR(MP_QSTR_set_freq), MP_ROM_PTR(&clkio_clkinput_set_freq_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_freq), MP_ROM_PTR(&clkio_clkinput_get_freq_obj) }, + //Properties + { MP_ROM_QSTR(MP_QSTR_clkindex), MP_ROM_PTR(&clkio_clkinput_clkindex_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(clkio_clkinput_locals_dict, clkio_clkinput_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + clkio_clkinput_type, + MP_QSTR_ClkInput, + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, + make_new, clkio_clkinput_make_new, + locals_dict, &clkio_clkinput_locals_dict + ); diff --git a/ports/raspberrypi/bindings/clkio/ClkInput.h b/ports/raspberrypi/bindings/clkio/ClkInput.h new file mode 100644 index 000000000000..3e69f15a4b16 --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/ClkInput.h @@ -0,0 +1,31 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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. + */ + +#pragma once + +#include "shared-bindings/microcontroller/Pin.h" + +extern const mp_obj_type_t clkio_clkinput_type; diff --git a/ports/raspberrypi/bindings/clkio/ClkOutput.c b/ports/raspberrypi/bindings/clkio/ClkOutput.c new file mode 100644 index 000000000000..bf6abaa1f5c7 --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/ClkOutput.c @@ -0,0 +1,176 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "py/obj.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/objarray.h" + +#include "shared-bindings/util.h" +#include "bindings/clkio/ClkOutput.h" +#include "common-hal/clkio/ClkOutput.h" + +STATIC void check_for_deinit (clkio_clkoutput_obj_t *self) { + if (common_hal_clkio_clkoutput_deinited (self)) { + raise_deinited_error (); + } +} + +//| class ClkOutput: +//| def __init__( +//| self, +//| *, +//| clksrc: clkio.ClkAuxSrc, +//| divisor: float +//| ) -> None: +//| """Creates a clock output pip object. +//| pin: Pin to be used as clock input, allowed pins: 22,23,24,25 +//| clksrc: points to the source clock to be connected to the output pin. +//| divisor: Divisor for clock before it's driven onto pin. +//| """ +STATIC mp_obj_t clkio_clkoutput_make_new (const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_pin, ARG_clksrc, ARG_divisor }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_clksrc, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, + { MP_QSTR_divisor, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} }, + }; + 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); + + clkio_clkoutput_obj_t *self = m_new_obj(clkio_clkoutput_obj_t); + self->base.type = &clkio_clkoutput_type; + + // Validate pin number + common_hal_clkio_clkoutput_validate_clksrc_pin (args[ARG_pin].u_rom_obj); + self->pin = args[ARG_pin].u_rom_obj; + self->divisor = common_hal_clkio_clkoutput_validate_divisor (mp_obj_get_float(args[ARG_divisor].u_obj)); + + // Validate pin based on clock + if (args[ARG_clksrc].u_rom_obj != mp_const_none) { + self->clksrc = validate_clkauxsrc (args[ARG_clksrc].u_rom_obj, MP_QSTR_clksrc); + } + else { + self->clksrc = CLKAUXSRC_NONE; + } + self->enabled = false; + return MP_OBJ_FROM_PTR (self); +}; + +//| def deinit(self) -> None: +//| """Releases the pin and frees any resources.""" +STATIC mp_obj_t clkio_clkoutput_deinit (mp_obj_t self_in) { + clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); + // Release pin + common_hal_clkio_clkoutput_deinit (self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_deinit_obj, clkio_clkoutput_deinit); + +//| def enable(self) -> None: +//| """Configures the pin and enables the clock output""" +STATIC mp_obj_t clkio_clkoutput_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit (self); + common_hal_clkio_clkoutput_enable (self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkoutput_enable_obj, 1, clkio_clkoutput_enable); + +//| def disable(self) -> None: +//| """Disableds the pin and external clock""" +STATIC mp_obj_t clkio_clkoutput_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit (self); + common_hal_clkio_clkoutput_disable (self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkoutput_disable_obj, 1, clkio_clkoutput_disable); + +//| clksrc: clkio.ClkAuxSrc +//| """Gets the auxillary source clock to be driven to pin.""" +static mp_obj_t clkio_clkoutput_clksrc_get (mp_obj_t self_in) { + clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit (self); + return clkauxsrc_get_obj (self->clksrc); +} +MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_clksrc_get_obj, clkio_clkoutput_clksrc_get); + +//| clksrc: clkio.ClkAuxSrc +//| """Sets the auxillary source clock to be driven to pin.""" +static mp_obj_t clkio_clkoutput_clksrc_set (mp_obj_t self_in, mp_obj_t clksrc_obj) { + clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit (self); + self->clksrc = validate_clkauxsrc (clksrc_obj, MP_QSTR_clksrc); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(clkio_clkoutput_clksrc_set_obj, clkio_clkoutput_clksrc_set); +MP_PROPERTY_GETSET(clkio_clkoutput_clksrc_obj, + (mp_obj_t)&clkio_clkoutput_clksrc_get_obj, + (mp_obj_t)&clkio_clkoutput_clksrc_set_obj); + +//| divisor: float +//| """Gets the divisor used to divide the clock before it's output to pin.""" +static mp_obj_t clkio_clkoutput_divisor_get (mp_obj_t self_in) { + clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit (self); + return mp_obj_new_float (self->divisor); +} +MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_divisor_get_obj, clkio_clkoutput_divisor_get); + +//| divisor: float +//| """Sets the divisor used to divide the clock before it's output to pin.""" +static mp_obj_t clkio_clkoutput_divisor_set (mp_obj_t self_in, mp_obj_t divisor_obj) { + clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit (self); + self->divisor = common_hal_clkio_clkoutput_validate_divisor(mp_obj_get_float(divisor_obj)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(clkio_clkoutput_divisor_set_obj, clkio_clkoutput_divisor_set); +MP_PROPERTY_GETSET(clkio_clkoutput_divisor_obj, + (mp_obj_t)&clkio_clkoutput_divisor_get_obj, + (mp_obj_t)&clkio_clkoutput_divisor_set_obj); + + +STATIC const mp_rom_map_elem_t clkio_clkoutput_locals_dict_table[] = { + // Functions + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&clkio_clkoutput_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&clkio_clkoutput_enable_obj) }, + { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&clkio_clkoutput_disable_obj) }, + //Properties + { MP_ROM_QSTR(MP_QSTR_clksrc), MP_ROM_PTR(&clkio_clkoutput_clksrc_obj) }, + { MP_ROM_QSTR(MP_QSTR_divisor), MP_ROM_PTR(&clkio_clkoutput_divisor_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(clkio_clkoutput_locals_dict, clkio_clkoutput_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + clkio_clkoutput_type, + MP_QSTR_ClkOutput, + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, + make_new, clkio_clkoutput_make_new, + locals_dict, &clkio_clkoutput_locals_dict + ); diff --git a/ports/raspberrypi/bindings/clkio/ClkOutput.h b/ports/raspberrypi/bindings/clkio/ClkOutput.h new file mode 100644 index 000000000000..941cbb8f8627 --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/ClkOutput.h @@ -0,0 +1,31 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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. + */ + +#pragma once + +#include "shared-bindings/microcontroller/Pin.h" + +extern const mp_obj_type_t clkio_clkoutput_type; diff --git a/ports/raspberrypi/bindings/clkio/__init__.c b/ports/raspberrypi/bindings/clkio/__init__.c new file mode 100644 index 000000000000..cd7c8ea3c719 --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/__init__.c @@ -0,0 +1,55 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "py/runtime.h" + +#include "shared-bindings/board/__init__.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "bindings/clkio/__init__.h" +#include "bindings/clkio/ClkAuxSrc.h" +#include "bindings/clkio/ClkIndex.h" +#include "bindings/clkio/ClkOutput.h" +#include "bindings/clkio/ClkInput.h" + +STATIC const mp_rom_map_elem_t clkio_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_clkio) }, + { MP_ROM_QSTR(MP_QSTR_ClkOutput), MP_ROM_PTR(&clkio_clkoutput_type) }, + { MP_ROM_QSTR(MP_QSTR_ClkInput), MP_ROM_PTR(&clkio_clkinput_type) }, + + // Enum like classes + { MP_ROM_QSTR(MP_QSTR_ClkAuxSrc), MP_ROM_PTR(&clkio_clkauxsrc_type) }, + { MP_ROM_QSTR(MP_QSTR_ClkIndex), MP_ROM_PTR(&clkio_clkindex_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(clkio_module_globals, clkio_module_globals_table); + +const mp_obj_module_t clkio_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&clkio_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_clkio, clkio_module); diff --git a/ports/raspberrypi/bindings/clkio/__init__.h b/ports/raspberrypi/bindings/clkio/__init__.h new file mode 100644 index 000000000000..2c5c13dfda66 --- /dev/null +++ b/ports/raspberrypi/bindings/clkio/__init__.h @@ -0,0 +1,29 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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. + */ + +#pragma once + +#include "py/obj.h" diff --git a/ports/raspberrypi/common-hal/clkio/ClkInput.c b/ports/raspberrypi/common-hal/clkio/ClkInput.c new file mode 100644 index 000000000000..5402ec3eccb3 --- /dev/null +++ b/ports/raspberrypi/common-hal/clkio/ClkInput.c @@ -0,0 +1,94 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "shared-bindings/microcontroller/Pin.h" +#include "common-hal/clkio/ClkInput.h" +#include "bindings/clkio/ClkIndex.h" +#include "hardware/clocks.h" +#include "hardware/gpio.h" +#include "py/runtime.h" + +// GPIN must be [20,22] +void common_hal_clkio_clkinput_validate_clkindex_pin (const mcu_pin_obj_t *pin) +{ + if ((pin->number != 20) && (pin->number != 22)) + mp_raise_ValueError_varg (MP_ERROR_TEXT("Pin %d invalid, valid pins are: 20,22"), pin->number); +} + +void common_hal_clkio_clkinput_validate_freqs (uint32_t src, uint32_t target) +{ + if (src == 0) + mp_raise_ValueError (MP_ERROR_TEXT("src freq == 0")); + if (target == 0) + mp_raise_ValueError (MP_ERROR_TEXT("target freq == 0")); + if (target > src) + mp_raise_ValueError_varg (MP_ERROR_TEXT("Invalid freqs: %u > %u"), target, src); +} + +bool common_hal_clkio_clkinput_deinited (clkio_clkinput_obj_t *self) +{ + return self->pin == NULL; +} + +void common_hal_clkio_clkinput_deinit (clkio_clkinput_obj_t *self) +{ + if (common_hal_clkio_clkinput_deinited (self)) + return; + if (self->enabled) + common_hal_clkio_clkinput_disable (self); + self->pin = NULL; +} + +static void common_hal_clkio_clkinput_claim_pin (clkio_clkinput_obj_t *self) +{ + // Avoid runtime error if enable already called + if (self->enabled) + return; + // Check pin is available + if (!common_hal_mcu_pin_is_free(self->pin)) + mp_raise_RuntimeError(MP_ERROR_TEXT("Pin in use")); + // Claim pin + common_hal_mcu_pin_claim(self->pin); + // Store flag + self->enabled = true; +} + +void common_hal_clkio_clkinput_enable (clkio_clkinput_obj_t *self) +{ + if (self->clkindex == CLKINDEX_NONE) + mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_clkindex); + common_hal_clkio_clkinput_claim_pin (self); + // Check return value + if (!clock_configure_gpin (self->clkindex, self->pin->number, self->src_freq, self->target_freq)) + mp_raise_RuntimeError (MP_ERROR_TEXT("clock_configure_gpin failed!")); +} + +void common_hal_clkio_clkinput_disable (clkio_clkinput_obj_t *self) +{ + if (!self->enabled) + return; + common_hal_reset_pin (self->pin); + self->enabled = 0; +} diff --git a/ports/raspberrypi/common-hal/clkio/ClkInput.h b/ports/raspberrypi/common-hal/clkio/ClkInput.h new file mode 100644 index 000000000000..4b8bfce07268 --- /dev/null +++ b/ports/raspberrypi/common-hal/clkio/ClkInput.h @@ -0,0 +1,48 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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. + */ +#pragma once + +#include "common-hal/microcontroller/Pin.h" +#include "bindings/clkio/ClkIndex.h" +#include "hardware/clocks.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + clkio_clkindex_t clkindex; + uint32_t src_freq; + uint32_t target_freq; + bool enabled; +} clkio_clkinput_obj_t; + +void common_hal_clkio_clkinput_validate_clkindex_pin (const mcu_pin_obj_t *pin); +bool common_hal_clkio_clkinput_deinited (clkio_clkinput_obj_t *self); +void common_hal_clkio_clkinput_deinit (clkio_clkinput_obj_t *self); +void common_hal_clkio_clkinput_validate_freqs (uint32_t src, uint32_t target); + +// Configure clock in/out +void common_hal_clkio_clkinput_enable (clkio_clkinput_obj_t *self); +void common_hal_clkio_clkinput_disable (clkio_clkinput_obj_t *self); diff --git a/ports/raspberrypi/common-hal/clkio/ClkOutput.c b/ports/raspberrypi/common-hal/clkio/ClkOutput.c new file mode 100644 index 000000000000..3978594fef56 --- /dev/null +++ b/ports/raspberrypi/common-hal/clkio/ClkOutput.c @@ -0,0 +1,93 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "shared-bindings/microcontroller/Pin.h" +#include "common-hal/clkio/ClkOutput.h" +#include "bindings/clkio/ClkAuxSrc.h" +#include "bindings/clkio/ClkIndex.h" +#include "hardware/clocks.h" +#include "hardware/gpio.h" +#include "py/runtime.h" + +// GPOUT must be [21,23,24,25] +void common_hal_clkio_clkoutput_validate_clksrc_pin (const mcu_pin_obj_t *pin) +{ + if ((pin->number != 21) && (pin->number != 23) && + (pin->number != 24) && (pin->number != 25)) + mp_raise_ValueError_varg (MP_ERROR_TEXT("Pin %d invalid, valid pins are: 21,23,24,25"), pin->number); +} + +mp_float_t common_hal_clkio_clkoutput_validate_divisor (mp_float_t div) +{ + if (mp_obj_float_binary_op (MP_BINARY_OP_EQUAL, div, mp_obj_new_float (1.0f)) || + (div >= 2.0f && div <= 16777215.0f)) + return div; + mp_raise_ValueError (MP_ERROR_TEXT("Invalid divisor: [1, 2-16777215]")); + return 1.0f; +} + +bool common_hal_clkio_clkoutput_deinited (clkio_clkoutput_obj_t *self) +{ + return self->pin == NULL; +} + +void common_hal_clkio_clkoutput_deinit (clkio_clkoutput_obj_t *self) +{ + if (common_hal_clkio_clkoutput_deinited (self)) + return; + if (self->enabled) + common_hal_clkio_clkoutput_disable (self); + self->pin = NULL; +} + +static void common_hal_clkio_clkoutput_claim_pin (clkio_clkoutput_obj_t *self) +{ + // Avoid runtime error if enable already called + if (self->enabled) + return; + // Check pin is available + if (!common_hal_mcu_pin_is_free(self->pin)) + mp_raise_RuntimeError(MP_ERROR_TEXT("Pin in use")); + // Claim pin + common_hal_mcu_pin_claim(self->pin); + // Store flag + self->enabled = true; +} + +void common_hal_clkio_clkoutput_enable (clkio_clkoutput_obj_t *self) +{ + if (self->clksrc == CLKAUXSRC_NONE) + mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_clksrc); + common_hal_clkio_clkoutput_claim_pin (self); + clock_gpio_init (self->pin->number, self->clksrc, self->divisor); +} + +void common_hal_clkio_clkoutput_disable (clkio_clkoutput_obj_t *self) +{ + if (!self->enabled) + return; + common_hal_reset_pin (self->pin); + self->enabled = 0; +} diff --git a/ports/raspberrypi/common-hal/clkio/ClkOutput.h b/ports/raspberrypi/common-hal/clkio/ClkOutput.h new file mode 100644 index 000000000000..0ca9562cec9f --- /dev/null +++ b/ports/raspberrypi/common-hal/clkio/ClkOutput.h @@ -0,0 +1,47 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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. + */ +#pragma once + +#include "common-hal/microcontroller/Pin.h" +#include "bindings/clkio/ClkAuxSrc.h" +#include "hardware/clocks.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; + clkio_clkauxsrc_t clksrc; + mp_float_t divisor; + bool enabled; +} clkio_clkoutput_obj_t; + +void common_hal_clkio_clkoutput_validate_clksrc_pin (const mcu_pin_obj_t *pin); +bool common_hal_clkio_clkoutput_deinited (clkio_clkoutput_obj_t *self); +void common_hal_clkio_clkoutput_deinit (clkio_clkoutput_obj_t *self); +mp_float_t common_hal_clkio_clkoutput_validate_divisor (mp_float_t div); + +// Configure clock out +void common_hal_clkio_clkoutput_enable (clkio_clkoutput_obj_t *self); +void common_hal_clkio_clkoutput_disable (clkio_clkoutput_obj_t *self); diff --git a/ports/raspberrypi/common-hal/clkio/__init__.c b/ports/raspberrypi/common-hal/clkio/__init__.c new file mode 100644 index 000000000000..fdb830ba8ae3 --- /dev/null +++ b/ports/raspberrypi/common-hal/clkio/__init__.c @@ -0,0 +1 @@ +// No clkio module functions diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index fc9fdcbd31af..a94bb8dd2357 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -431,6 +431,12 @@ CFLAGS += -DCIRCUITPY_REQUIRE_I2C_PULLUPS=$(CIRCUITPY_REQUIRE_I2C_PULLUPS) CIRCUITPY_RP2PIO ?= 0 CFLAGS += -DCIRCUITPY_RP2PIO=$(CIRCUITPY_RP2PIO) +# CIRCUITPY_RP2CLKIO is handled in the raspberrypi tree. +# Only for rp2 chips. +# Assume not a rp2 build. +CIRCUITPY_RP2CLKIO ?= 0 +CFLAGS += -DCIRCUITPY_RP2CLKIO=$(CIRCUITPY_RP2CLKIO) + CIRCUITPY_RGBMATRIX ?= 0 CFLAGS += -DCIRCUITPY_RGBMATRIX=$(CIRCUITPY_RGBMATRIX) From 3b1a31a1e693e5f770ca9299f1ec5a1912a967c6 Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Tue, 5 Mar 2024 16:23:30 -0700 Subject: [PATCH 02/11] Run make translate, remove whitespace --- locale/circuitpython.pot | 47 +++++++- ports/raspberrypi/bindings/clkio/ClkInput.c | 106 +++++++++-------- ports/raspberrypi/bindings/clkio/ClkOutput.c | 107 +++++++++--------- ports/raspberrypi/common-hal/clkio/ClkInput.c | 70 ++++++------ .../raspberrypi/common-hal/clkio/ClkOutput.c | 59 +++++----- 5 files changed, 213 insertions(+), 176 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 4c58cd284852..b9c36f43120e 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -206,6 +206,8 @@ msgstr "" msgid "%q must be multiple of 8." msgstr "" +#: ports/raspberrypi/bindings/clkio/ClkAuxSrc.c +#: ports/raspberrypi/bindings/clkio/ClkIndex.c #: ports/raspberrypi/bindings/cyw43/__init__.c py/argcheck.c py/objexcept.c #: shared-bindings/bitmapfilter/__init__.c shared-bindings/canio/CAN.c #: shared-bindings/digitalio/Pull.c shared-bindings/supervisor/__init__.c @@ -226,6 +228,11 @@ msgstr "" msgid "%q must be power of 2" msgstr "" +#: ports/raspberrypi/common-hal/clkio/ClkInput.c +#: ports/raspberrypi/common-hal/clkio/ClkOutput.c +msgid "%q not set" +msgstr "" + #: shared-bindings/wifi/Monitor.c msgid "%q out of bounds" msgstr "" @@ -912,10 +919,6 @@ msgstr "" msgid "Error in safemode.py." msgstr "" -#: shared-bindings/ssl/SSLSocket.c -msgid "Error: Failure to bind" -msgstr "" - #: shared-bindings/alarm/__init__.c msgid "Expected a kind of %q" msgstr "" @@ -1222,6 +1225,10 @@ msgstr "" msgid "Invalid data_pins[%d]" msgstr "" +#: ports/raspberrypi/common-hal/clkio/ClkOutput.c +msgid "Invalid divisor: [1, 2-16777215]" +msgstr "" + #: shared-module/msgpack/__init__.c msgid "Invalid format" msgstr "" @@ -1230,6 +1237,11 @@ msgstr "" msgid "Invalid format chunk size" msgstr "" +#: ports/raspberrypi/common-hal/clkio/ClkInput.c +#, c-format +msgid "Invalid freqs: %u > %u" +msgstr "" + #: shared-bindings/wifi/Radio.c msgid "Invalid hex password" msgstr "" @@ -1657,6 +1669,16 @@ msgstr "" msgid "Permission denied" msgstr "" +#: ports/raspberrypi/common-hal/clkio/ClkInput.c +#, c-format +msgid "Pin %d invalid, valid pins are: 20,22" +msgstr "" + +#: ports/raspberrypi/common-hal/clkio/ClkOutput.c +#, c-format +msgid "Pin %d invalid, valid pins are: 21,23,24,25" +msgstr "" + #: ports/stm/common-hal/alarm/pin/PinAlarm.c msgid "Pin cannot wake from Deep Sleep" msgstr "" @@ -1665,6 +1687,11 @@ msgstr "" msgid "Pin count too large" msgstr "" +#: ports/raspberrypi/common-hal/clkio/ClkInput.c +#: ports/raspberrypi/common-hal/clkio/ClkOutput.c +msgid "Pin in use" +msgstr "" + #: ports/stm/common-hal/alarm/pin/PinAlarm.c #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin interrupt already in use" @@ -2730,6 +2757,10 @@ msgstr "" msgid "clip point must be (x,y) tuple" msgstr "" +#: ports/raspberrypi/common-hal/clkio/ClkInput.c +msgid "clock_configure_gpin failed!" +msgstr "" + #: shared-bindings/msgpack/ExtType.c msgid "code outside range 0~127" msgstr "" @@ -3961,6 +3992,10 @@ msgstr "" msgid "splitting with sub-captures" msgstr "" +#: ports/raspberrypi/common-hal/clkio/ClkInput.c +msgid "src freq == 0" +msgstr "" + #: py/objstr.c msgid "start/end indices" msgstr "" @@ -4014,6 +4049,10 @@ msgstr "" msgid "syntax error in uctypes descriptor" msgstr "" +#: ports/raspberrypi/common-hal/clkio/ClkInput.c +msgid "target freq == 0" +msgstr "" + #: extmod/modtime.c msgid "ticks interval overflow" msgstr "" diff --git a/ports/raspberrypi/bindings/clkio/ClkInput.c b/ports/raspberrypi/bindings/clkio/ClkInput.c index a266f5ee9582..923f86c7c7da 100644 --- a/ports/raspberrypi/bindings/clkio/ClkInput.c +++ b/ports/raspberrypi/bindings/clkio/ClkInput.c @@ -33,9 +33,9 @@ #include "bindings/clkio/ClkInput.h" #include "common-hal/clkio/ClkInput.h" -STATIC void check_for_deinit (clkio_clkinput_obj_t *self) { - if (common_hal_clkio_clkinput_deinited (self)) { - raise_deinited_error (); +STATIC void check_for_deinit(clkio_clkinput_obj_t *self) { + if (common_hal_clkio_clkinput_deinited(self)) { + raise_deinited_error(); } } @@ -48,15 +48,15 @@ STATIC void check_for_deinit (clkio_clkinput_obj_t *self) { //| src_freq: uint32, //| target_freq: uint32 //| ) -> None: -//| """Creates a clock input pin object. -//| pin: Pin to be used as clock input, allowed pins: 20,22 -//| clkindex: points to the destination clock to be connected to the input pin. -//| src_freq: External input frequency at the pin. -//| target_freq: Desired frequency for clkindex. -//| """ - -STATIC mp_obj_t clkio_clkinput_make_new (const mp_obj_type_t *type, size_t n_args, - size_t n_kw, const mp_obj_t *all_args) { +//| """Creates a clock input pin object. +//| pin: Pin to be used as clock input, allowed pins: 20,22 +//| clkindex: points to the destination clock to be connected to the input pin. +//| src_freq: External input frequency at the pin. +//| target_freq: Desired frequency for clkindex. +//| """ + +STATIC mp_obj_t clkio_clkinput_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_clkindex, ARG_src_freq, ARG_target_freq }; static const mp_arg_t allowed_args[] = { { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, @@ -72,61 +72,56 @@ STATIC mp_obj_t clkio_clkinput_make_new (const mp_obj_type_t *type, size_t n_arg // Validate pin number self->pin = args[ARG_pin].u_rom_obj; + common_hal_clkio_clkinput_validate_clkindex_pin(self->pin); // Validate pin based on clock if (args[ARG_clkindex].u_rom_obj != mp_const_none) { - common_hal_clkio_clkinput_validate_clkindex_pin (self->pin); - self->clkindex = validate_clkindex (args[ARG_clkindex].u_rom_obj, MP_QSTR_clkindex); + self->clkindex = validate_clkindex(args[ARG_clkindex].u_rom_obj, MP_QSTR_clkindex); self->src_freq = args[ARG_src_freq].u_int; self->target_freq = args[ARG_target_freq].u_int; // Validate frequencies if set - if (self->src_freq && self->target_freq) - common_hal_clkio_clkinput_validate_freqs (args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); - } - else { + if (self->src_freq && self->target_freq) { + common_hal_clkio_clkinput_validate_freqs(args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); + } + } else { self->clkindex = CLKINDEX_NONE; } self->enabled = false; - return MP_OBJ_FROM_PTR (self); + return MP_OBJ_FROM_PTR(self); }; //| def deinit(self) -> None: -//| """Releases the pin and frees any resources.""" -STATIC mp_obj_t clkio_clkinput_deinit (mp_obj_t self_in) { +//| """Releases the pin and frees any resources.""" +STATIC mp_obj_t clkio_clkinput_deinit(mp_obj_t self_in) { clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); // Release pin - common_hal_clkio_clkinput_deinit (self); + common_hal_clkio_clkinput_deinit(self); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkinput_deinit_obj, clkio_clkinput_deinit); //| def enable(self) -> None: -//| """Configures the pin and enables the internal clock""" +//| """Configures the pin and enables the internal clock""" STATIC mp_obj_t clkio_clkinput_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit (self); - common_hal_clkio_clkinput_enable (self); + check_for_deinit(self); + common_hal_clkio_clkinput_enable(self); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_enable_obj, 1, clkio_clkinput_enable); //| def disable(self) -> None: -//| """Disableds the pin and internal clock""" +//| """Disables the pin and internal clock""" STATIC mp_obj_t clkio_clkinput_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit (self); - common_hal_clkio_clkinput_disable (self); + check_for_deinit(self); + common_hal_clkio_clkinput_disable(self); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_disable_obj, 1, clkio_clkinput_disable); -//| def set_freq( -//| self, -//| src_freq -> uint32 -//| target_freq -> uint32 -//| ) -> None: +//| def set_freq(self, src_freq: uint32, target_freq: uint32) -> None: //| """Configures the src and target frequency. Must be set before enable() is called.""" -//| STATIC mp_obj_t clkio_clkinput_set_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_src_freq, ARG_target_freq }; static const mp_arg_t allowed_args[] = { @@ -137,8 +132,8 @@ STATIC mp_obj_t clkio_clkinput_set_freq(size_t n_args, const mp_obj_t *pos_args, mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit (self); - common_hal_clkio_clkinput_validate_freqs (args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); + check_for_deinit(self); + common_hal_clkio_clkinput_validate_freqs(args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); self->src_freq = args[ARG_src_freq].u_int; self->target_freq = args[ARG_target_freq].u_int; return mp_const_none; @@ -147,41 +142,42 @@ MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_set_freq_obj, 1, clkio_clkinput_set_fr //| def set_freq( //| self, -//| ) -> tuple (src_freq, target_freq): +//| ) -> tuple(src_freq, target_freq): //| """Returns the src and target frequency.""" -//| STATIC mp_obj_t clkio_clkinput_get_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit (self); + check_for_deinit(self); mp_obj_t tup[] = { - mp_obj_new_int (self->src_freq), - mp_obj_new_int (self->target_freq)}; + mp_obj_new_int(self->src_freq), + mp_obj_new_int(self->target_freq) + }; // Return tuple with (src, target) - return mp_obj_new_tuple (2, tup); + return mp_obj_new_tuple(2, tup); } MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_get_freq_obj, 1, clkio_clkinput_get_freq); -//| clkindex: clkio.ClkIndex -//| """Get clock that will be driven from external pin.""" -static mp_obj_t clkio_clkinput_clkindex_get (mp_obj_t self_in) { +//| clkindex: clkio.ClkIndex +//| """Get clock that will be driven from external pin.""" +static mp_obj_t clkio_clkinput_clkindex_get(mp_obj_t self_in) { clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit (self); - return clkindex_get_obj (self->clkindex); + check_for_deinit(self); + return clkindex_get_obj(self->clkindex); } MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkinput_clkindex_get_obj, clkio_clkinput_clkindex_get); -//| clkindex: clkio.ClkIndex -//| """Set clock that will be driven from external pin.""" -static mp_obj_t clkio_clkinput_clkindex_set (mp_obj_t self_in, mp_obj_t clkindex_obj) { +//| clkindex: clkio.ClkIndex +//| """Set clock that will be driven from external pin.""" +//| +static mp_obj_t clkio_clkinput_clkindex_set(mp_obj_t self_in, mp_obj_t clkindex_obj) { clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit (self); - self->clkindex = validate_clkindex (clkindex_obj, MP_QSTR_clkindex); + check_for_deinit(self); + self->clkindex = validate_clkindex(clkindex_obj, MP_QSTR_clkindex); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(clkio_clkinput_clkindex_set_obj, clkio_clkinput_clkindex_set); MP_PROPERTY_GETSET(clkio_clkinput_clkindex_obj, - (mp_obj_t)&clkio_clkinput_clkindex_get_obj, - (mp_obj_t)&clkio_clkinput_clkindex_set_obj); + (mp_obj_t)&clkio_clkinput_clkindex_get_obj, + (mp_obj_t)&clkio_clkinput_clkindex_set_obj); STATIC const mp_rom_map_elem_t clkio_clkinput_locals_dict_table[] = { @@ -191,7 +187,7 @@ STATIC const mp_rom_map_elem_t clkio_clkinput_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&clkio_clkinput_disable_obj) }, { MP_ROM_QSTR(MP_QSTR_set_freq), MP_ROM_PTR(&clkio_clkinput_set_freq_obj) }, { MP_ROM_QSTR(MP_QSTR_get_freq), MP_ROM_PTR(&clkio_clkinput_get_freq_obj) }, - //Properties + // Properties { MP_ROM_QSTR(MP_QSTR_clkindex), MP_ROM_PTR(&clkio_clkinput_clkindex_obj) }, }; STATIC MP_DEFINE_CONST_DICT(clkio_clkinput_locals_dict, clkio_clkinput_locals_dict_table); diff --git a/ports/raspberrypi/bindings/clkio/ClkOutput.c b/ports/raspberrypi/bindings/clkio/ClkOutput.c index bf6abaa1f5c7..8eccbe9c2c83 100644 --- a/ports/raspberrypi/bindings/clkio/ClkOutput.c +++ b/ports/raspberrypi/bindings/clkio/ClkOutput.c @@ -33,26 +33,23 @@ #include "bindings/clkio/ClkOutput.h" #include "common-hal/clkio/ClkOutput.h" -STATIC void check_for_deinit (clkio_clkoutput_obj_t *self) { - if (common_hal_clkio_clkoutput_deinited (self)) { - raise_deinited_error (); +STATIC void check_for_deinit(clkio_clkoutput_obj_t *self) { + if (common_hal_clkio_clkoutput_deinited(self)) { + raise_deinited_error(); } } //| class ClkOutput: //| def __init__( -//| self, -//| *, -//| clksrc: clkio.ClkAuxSrc, -//| divisor: float +//| self, pin: microcontroller.Pin, *, clksrc: clkio.ClkAuxSrc, divisor: float //| ) -> None: -//| """Creates a clock output pip object. -//| pin: Pin to be used as clock input, allowed pins: 22,23,24,25 -//| clksrc: points to the source clock to be connected to the output pin. -//| divisor: Divisor for clock before it's driven onto pin. -//| """ -STATIC mp_obj_t clkio_clkoutput_make_new (const mp_obj_type_t *type, size_t n_args, - size_t n_kw, const mp_obj_t *all_args) { +//| """Creates a clock output pip object. +//| pin: Pin to be used as clock input, allowed pins: 22,23,24,25 +//| clksrc: points to the source clock to be connected to the output pin. +//| divisor: Divisor for clock before it's driven onto pin. +//| """ +STATIC mp_obj_t clkio_clkoutput_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_clksrc, ARG_divisor }; static const mp_arg_t allowed_args[] = { { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, @@ -66,94 +63,94 @@ STATIC mp_obj_t clkio_clkoutput_make_new (const mp_obj_type_t *type, size_t n_ar self->base.type = &clkio_clkoutput_type; // Validate pin number - common_hal_clkio_clkoutput_validate_clksrc_pin (args[ARG_pin].u_rom_obj); + common_hal_clkio_clkoutput_validate_clksrc_pin(args[ARG_pin].u_rom_obj); self->pin = args[ARG_pin].u_rom_obj; - self->divisor = common_hal_clkio_clkoutput_validate_divisor (mp_obj_get_float(args[ARG_divisor].u_obj)); + self->divisor = common_hal_clkio_clkoutput_validate_divisor(mp_obj_get_float(args[ARG_divisor].u_obj)); // Validate pin based on clock if (args[ARG_clksrc].u_rom_obj != mp_const_none) { - self->clksrc = validate_clkauxsrc (args[ARG_clksrc].u_rom_obj, MP_QSTR_clksrc); - } - else { + self->clksrc = validate_clkauxsrc(args[ARG_clksrc].u_rom_obj, MP_QSTR_clksrc); + } else { self->clksrc = CLKAUXSRC_NONE; } self->enabled = false; - return MP_OBJ_FROM_PTR (self); -}; + return MP_OBJ_FROM_PTR(self); +} -//| def deinit(self) -> None: -//| """Releases the pin and frees any resources.""" -STATIC mp_obj_t clkio_clkoutput_deinit (mp_obj_t self_in) { +//| def deinit(self) -> None: +//| """Releases the pin and frees any resources.""" +STATIC mp_obj_t clkio_clkoutput_deinit(mp_obj_t self_in) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); // Release pin - common_hal_clkio_clkoutput_deinit (self); + common_hal_clkio_clkoutput_deinit(self); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_deinit_obj, clkio_clkoutput_deinit); //| def enable(self) -> None: -//| """Configures the pin and enables the clock output""" +//| """Configures the pin and enables the clock output""" STATIC mp_obj_t clkio_clkoutput_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit (self); - common_hal_clkio_clkoutput_enable (self); + check_for_deinit(self); + common_hal_clkio_clkoutput_enable(self); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkoutput_enable_obj, 1, clkio_clkoutput_enable); //| def disable(self) -> None: -//| """Disableds the pin and external clock""" +//| """Disableds the pin and external clock""" STATIC mp_obj_t clkio_clkoutput_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit (self); - common_hal_clkio_clkoutput_disable (self); + check_for_deinit(self); + common_hal_clkio_clkoutput_disable(self); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkoutput_disable_obj, 1, clkio_clkoutput_disable); -//| clksrc: clkio.ClkAuxSrc -//| """Gets the auxillary source clock to be driven to pin.""" -static mp_obj_t clkio_clkoutput_clksrc_get (mp_obj_t self_in) { +//| clksrc: clkio.ClkAuxSrc +//| """Gets the auxiliary source clock to be driven to pin.""" +static mp_obj_t clkio_clkoutput_clksrc_get(mp_obj_t self_in) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit (self); - return clkauxsrc_get_obj (self->clksrc); + check_for_deinit(self); + return clkauxsrc_get_obj(self->clksrc); } MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_clksrc_get_obj, clkio_clkoutput_clksrc_get); -//| clksrc: clkio.ClkAuxSrc -//| """Sets the auxillary source clock to be driven to pin.""" -static mp_obj_t clkio_clkoutput_clksrc_set (mp_obj_t self_in, mp_obj_t clksrc_obj) { +//| clksrc: clkio.ClkAuxSrc +//| """Sets the auxiliary source clock to be driven to pin.""" +static mp_obj_t clkio_clkoutput_clksrc_set(mp_obj_t self_in, mp_obj_t clksrc_obj) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit (self); - self->clksrc = validate_clkauxsrc (clksrc_obj, MP_QSTR_clksrc); + check_for_deinit(self); + self->clksrc = validate_clkauxsrc(clksrc_obj, MP_QSTR_clksrc); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(clkio_clkoutput_clksrc_set_obj, clkio_clkoutput_clksrc_set); MP_PROPERTY_GETSET(clkio_clkoutput_clksrc_obj, - (mp_obj_t)&clkio_clkoutput_clksrc_get_obj, - (mp_obj_t)&clkio_clkoutput_clksrc_set_obj); + (mp_obj_t)&clkio_clkoutput_clksrc_get_obj, + (mp_obj_t)&clkio_clkoutput_clksrc_set_obj); -//| divisor: float -//| """Gets the divisor used to divide the clock before it's output to pin.""" -static mp_obj_t clkio_clkoutput_divisor_get (mp_obj_t self_in) { +//| divisor: float +//| """Gets the divisor used to divide the clock before it's output to pin.""" +static mp_obj_t clkio_clkoutput_divisor_get(mp_obj_t self_in) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit (self); - return mp_obj_new_float (self->divisor); + check_for_deinit(self); + return mp_obj_new_float(self->divisor); } MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_divisor_get_obj, clkio_clkoutput_divisor_get); -//| divisor: float -//| """Sets the divisor used to divide the clock before it's output to pin.""" -static mp_obj_t clkio_clkoutput_divisor_set (mp_obj_t self_in, mp_obj_t divisor_obj) { +//| divisor: float +//| """Sets the divisor used to divide the clock before it's output to pin.""" +//| +static mp_obj_t clkio_clkoutput_divisor_set(mp_obj_t self_in, mp_obj_t divisor_obj) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit (self); + check_for_deinit(self); self->divisor = common_hal_clkio_clkoutput_validate_divisor(mp_obj_get_float(divisor_obj)); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(clkio_clkoutput_divisor_set_obj, clkio_clkoutput_divisor_set); MP_PROPERTY_GETSET(clkio_clkoutput_divisor_obj, - (mp_obj_t)&clkio_clkoutput_divisor_get_obj, - (mp_obj_t)&clkio_clkoutput_divisor_set_obj); + (mp_obj_t)&clkio_clkoutput_divisor_get_obj, + (mp_obj_t)&clkio_clkoutput_divisor_set_obj); STATIC const mp_rom_map_elem_t clkio_clkoutput_locals_dict_table[] = { @@ -161,7 +158,7 @@ STATIC const mp_rom_map_elem_t clkio_clkoutput_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&clkio_clkoutput_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&clkio_clkoutput_enable_obj) }, { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&clkio_clkoutput_disable_obj) }, - //Properties + // Properties { MP_ROM_QSTR(MP_QSTR_clksrc), MP_ROM_PTR(&clkio_clkoutput_clksrc_obj) }, { MP_ROM_QSTR(MP_QSTR_divisor), MP_ROM_PTR(&clkio_clkoutput_divisor_obj) }, }; diff --git a/ports/raspberrypi/common-hal/clkio/ClkInput.c b/ports/raspberrypi/common-hal/clkio/ClkInput.c index 5402ec3eccb3..ec87c0151962 100644 --- a/ports/raspberrypi/common-hal/clkio/ClkInput.c +++ b/ports/raspberrypi/common-hal/clkio/ClkInput.c @@ -31,64 +31,68 @@ #include "py/runtime.h" // GPIN must be [20,22] -void common_hal_clkio_clkinput_validate_clkindex_pin (const mcu_pin_obj_t *pin) -{ - if ((pin->number != 20) && (pin->number != 22)) - mp_raise_ValueError_varg (MP_ERROR_TEXT("Pin %d invalid, valid pins are: 20,22"), pin->number); +void common_hal_clkio_clkinput_validate_clkindex_pin(const mcu_pin_obj_t *pin) { + if ((pin->number != 20) && (pin->number != 22)) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("Pin %d invalid, valid pins are: 20,22"), pin->number); + } } -void common_hal_clkio_clkinput_validate_freqs (uint32_t src, uint32_t target) -{ - if (src == 0) - mp_raise_ValueError (MP_ERROR_TEXT("src freq == 0")); - if (target == 0) - mp_raise_ValueError (MP_ERROR_TEXT("target freq == 0")); - if (target > src) - mp_raise_ValueError_varg (MP_ERROR_TEXT("Invalid freqs: %u > %u"), target, src); +void common_hal_clkio_clkinput_validate_freqs(uint32_t src, uint32_t target) { + if (src == 0) { + mp_raise_ValueError(MP_ERROR_TEXT("src freq == 0")); + } + if (target == 0) { + mp_raise_ValueError(MP_ERROR_TEXT("target freq == 0")); + } + if (target > src) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid freqs: %u > %u"), target, src); + } } -bool common_hal_clkio_clkinput_deinited (clkio_clkinput_obj_t *self) -{ +bool common_hal_clkio_clkinput_deinited(clkio_clkinput_obj_t *self) { return self->pin == NULL; } -void common_hal_clkio_clkinput_deinit (clkio_clkinput_obj_t *self) -{ - if (common_hal_clkio_clkinput_deinited (self)) +void common_hal_clkio_clkinput_deinit(clkio_clkinput_obj_t *self) { + if (common_hal_clkio_clkinput_deinited(self)) { return; - if (self->enabled) - common_hal_clkio_clkinput_disable (self); + } + if (self->enabled) { + common_hal_clkio_clkinput_disable(self); + } self->pin = NULL; } -static void common_hal_clkio_clkinput_claim_pin (clkio_clkinput_obj_t *self) -{ +static void common_hal_clkio_clkinput_claim_pin(clkio_clkinput_obj_t *self) { // Avoid runtime error if enable already called - if (self->enabled) + if (self->enabled) { return; + } // Check pin is available - if (!common_hal_mcu_pin_is_free(self->pin)) + if (!common_hal_mcu_pin_is_free(self->pin)) { mp_raise_RuntimeError(MP_ERROR_TEXT("Pin in use")); + } // Claim pin common_hal_mcu_pin_claim(self->pin); // Store flag self->enabled = true; } -void common_hal_clkio_clkinput_enable (clkio_clkinput_obj_t *self) -{ - if (self->clkindex == CLKINDEX_NONE) +void common_hal_clkio_clkinput_enable(clkio_clkinput_obj_t *self) { + if (self->clkindex == CLKINDEX_NONE) { mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_clkindex); - common_hal_clkio_clkinput_claim_pin (self); + } + common_hal_clkio_clkinput_claim_pin(self); // Check return value - if (!clock_configure_gpin (self->clkindex, self->pin->number, self->src_freq, self->target_freq)) - mp_raise_RuntimeError (MP_ERROR_TEXT("clock_configure_gpin failed!")); + if (!clock_configure_gpin(self->clkindex, self->pin->number, self->src_freq, self->target_freq)) { + mp_raise_RuntimeError(MP_ERROR_TEXT("clock_configure_gpin failed!")); + } } -void common_hal_clkio_clkinput_disable (clkio_clkinput_obj_t *self) -{ - if (!self->enabled) +void common_hal_clkio_clkinput_disable(clkio_clkinput_obj_t *self) { + if (!self->enabled) { return; - common_hal_reset_pin (self->pin); + } + common_hal_reset_pin(self->pin); self->enabled = 0; } diff --git a/ports/raspberrypi/common-hal/clkio/ClkOutput.c b/ports/raspberrypi/common-hal/clkio/ClkOutput.c index 3978594fef56..6cacc9187cee 100644 --- a/ports/raspberrypi/common-hal/clkio/ClkOutput.c +++ b/ports/raspberrypi/common-hal/clkio/ClkOutput.c @@ -32,62 +32,63 @@ #include "py/runtime.h" // GPOUT must be [21,23,24,25] -void common_hal_clkio_clkoutput_validate_clksrc_pin (const mcu_pin_obj_t *pin) -{ +void common_hal_clkio_clkoutput_validate_clksrc_pin(const mcu_pin_obj_t *pin) { if ((pin->number != 21) && (pin->number != 23) && - (pin->number != 24) && (pin->number != 25)) - mp_raise_ValueError_varg (MP_ERROR_TEXT("Pin %d invalid, valid pins are: 21,23,24,25"), pin->number); + (pin->number != 24) && (pin->number != 25)) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("Pin %d invalid, valid pins are: 21,23,24,25"), pin->number); + } } -mp_float_t common_hal_clkio_clkoutput_validate_divisor (mp_float_t div) -{ - if (mp_obj_float_binary_op (MP_BINARY_OP_EQUAL, div, mp_obj_new_float (1.0f)) || - (div >= 2.0f && div <= 16777215.0f)) +mp_float_t common_hal_clkio_clkoutput_validate_divisor(mp_float_t div) { + if (mp_obj_float_binary_op(MP_BINARY_OP_EQUAL, div, mp_obj_new_float(1.0f)) || + (div >= 2.0f && div <= 16777215.0f)) { return div; - mp_raise_ValueError (MP_ERROR_TEXT("Invalid divisor: [1, 2-16777215]")); + } + mp_raise_ValueError(MP_ERROR_TEXT("Invalid divisor: [1, 2-16777215]")); return 1.0f; } -bool common_hal_clkio_clkoutput_deinited (clkio_clkoutput_obj_t *self) -{ +bool common_hal_clkio_clkoutput_deinited(clkio_clkoutput_obj_t *self) { return self->pin == NULL; } -void common_hal_clkio_clkoutput_deinit (clkio_clkoutput_obj_t *self) -{ - if (common_hal_clkio_clkoutput_deinited (self)) +void common_hal_clkio_clkoutput_deinit(clkio_clkoutput_obj_t *self) { + if (common_hal_clkio_clkoutput_deinited(self)) { return; - if (self->enabled) - common_hal_clkio_clkoutput_disable (self); + } + if (self->enabled) { + common_hal_clkio_clkoutput_disable(self); + } self->pin = NULL; } -static void common_hal_clkio_clkoutput_claim_pin (clkio_clkoutput_obj_t *self) -{ +static void common_hal_clkio_clkoutput_claim_pin(clkio_clkoutput_obj_t *self) { // Avoid runtime error if enable already called - if (self->enabled) + if (self->enabled) { return; + } // Check pin is available - if (!common_hal_mcu_pin_is_free(self->pin)) + if (!common_hal_mcu_pin_is_free(self->pin)) { mp_raise_RuntimeError(MP_ERROR_TEXT("Pin in use")); + } // Claim pin common_hal_mcu_pin_claim(self->pin); // Store flag self->enabled = true; } -void common_hal_clkio_clkoutput_enable (clkio_clkoutput_obj_t *self) -{ - if (self->clksrc == CLKAUXSRC_NONE) +void common_hal_clkio_clkoutput_enable(clkio_clkoutput_obj_t *self) { + if (self->clksrc == CLKAUXSRC_NONE) { mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_clksrc); - common_hal_clkio_clkoutput_claim_pin (self); - clock_gpio_init (self->pin->number, self->clksrc, self->divisor); + } + common_hal_clkio_clkoutput_claim_pin(self); + clock_gpio_init(self->pin->number, self->clksrc, self->divisor); } -void common_hal_clkio_clkoutput_disable (clkio_clkoutput_obj_t *self) -{ - if (!self->enabled) +void common_hal_clkio_clkoutput_disable(clkio_clkoutput_obj_t *self) { + if (!self->enabled) { return; - common_hal_reset_pin (self->pin); + } + common_hal_reset_pin(self->pin); self->enabled = 0; } From 2f31860c7e359c5728fccc696ed601ed04d226bd Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Wed, 6 Mar 2024 08:31:49 -0700 Subject: [PATCH 03/11] Fix code formatting --- ports/raspberrypi/bindings/clkio/ClkAuxSrc.c | 91 +++++++------------ ports/raspberrypi/bindings/clkio/ClkAuxSrc.h | 2 +- ports/raspberrypi/bindings/clkio/ClkIndex.c | 80 +++++++--------- ports/raspberrypi/bindings/clkio/ClkInput.c | 18 ++-- ports/raspberrypi/bindings/clkio/ClkOutput.c | 10 +- ports/raspberrypi/common-hal/clkio/ClkInput.h | 14 +-- .../raspberrypi/common-hal/clkio/ClkOutput.h | 12 +-- 7 files changed, 90 insertions(+), 137 deletions(-) diff --git a/ports/raspberrypi/bindings/clkio/ClkAuxSrc.c b/ports/raspberrypi/bindings/clkio/ClkAuxSrc.c index 34fa26a56821..bc3a08600a6a 100644 --- a/ports/raspberrypi/bindings/clkio/ClkAuxSrc.c +++ b/ports/raspberrypi/bindings/clkio/ClkAuxSrc.c @@ -90,35 +90,25 @@ STATIC void clkio_clkauxsrc_print(const mp_print_t *print, mp_obj_t self_in, mp_ qstr clk = MP_QSTR_INVALID; if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_sys_obj)) { clk = MP_QSTR_PLL_SYS; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj)) { clk = MP_QSTR_GPIN0; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj)) { clk = MP_QSTR_GPIN1; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj)) { clk = MP_QSTR_PLL_USB; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj)) { clk = MP_QSTR_PLL_ROSC; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj)) { clk = MP_QSTR_PLL_XOSC; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_sys_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_sys_obj)) { clk = MP_QSTR_SYS; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_usb_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_usb_obj)) { clk = MP_QSTR_USB; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_adc_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_adc_obj)) { clk = MP_QSTR_ADC; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj)) { clk = MP_QSTR_RTC; - } - else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_ref_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_ref_obj)) { clk = MP_QSTR_REF; } mp_printf(print, "%q.%q.%q", MP_QSTR_clkio, MP_QSTR_ClkAuxSrc, clk); @@ -132,68 +122,57 @@ MP_DEFINE_CONST_OBJ_TYPE( locals_dict, &clkio_clkauxsrc_locals_dict ); -mp_obj_t clkauxsrc_get_obj (clkio_clkauxsrc_t type) -{ - if (type == CLKAUXSRC_PLL_SYS) +mp_obj_t clkauxsrc_get_obj(clkio_clkauxsrc_t type) { + if (type == CLKAUXSRC_PLL_SYS) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_sys_obj); - else if (type == CLKAUXSRC_GPIN0) + } else if (type == CLKAUXSRC_GPIN0) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_gpin0_obj); - else if (type == CLKAUXSRC_GPIN1) + } else if (type == CLKAUXSRC_GPIN1) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_gpin1_obj); - else if (type == CLKAUXSRC_PLL_USB) + } else if (type == CLKAUXSRC_PLL_USB) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_usb_obj); - else if (type == CLKAUXSRC_PLL_ROSC) + } else if (type == CLKAUXSRC_PLL_ROSC) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_rosc_obj); - else if (type == CLKAUXSRC_PLL_XOSC) + } else if (type == CLKAUXSRC_PLL_XOSC) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_xosc_obj); - else if (type == CLKAUXSRC_SYS) + } else if (type == CLKAUXSRC_SYS) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_sys_obj); - else if (type == CLKAUXSRC_USB) + } else if (type == CLKAUXSRC_USB) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_usb_obj); - else if (type == CLKAUXSRC_ADC) + } else if (type == CLKAUXSRC_ADC) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_adc_obj); - else if (type == CLKAUXSRC_RTC) + } else if (type == CLKAUXSRC_RTC) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_rtc_obj); - else if (type == CLKAUXSRC_REF) + } else if (type == CLKAUXSRC_REF) { return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_ref_obj); - else + } else { return MP_ROM_NONE; + } } clkio_clkauxsrc_t validate_clkauxsrc(mp_rom_obj_t obj, qstr arg_name) { if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_sys_obj)) { return CLKAUXSRC_PLL_SYS; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj)) { return CLKAUXSRC_GPIN0; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj)) { return CLKAUXSRC_GPIN1; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj)) { return CLKAUXSRC_PLL_USB; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj)) { return CLKAUXSRC_PLL_ROSC; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj)) { return CLKAUXSRC_PLL_XOSC; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_sys_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_sys_obj)) { return CLKAUXSRC_SYS; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_usb_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_usb_obj)) { return CLKAUXSRC_USB; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_adc_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_adc_obj)) { return CLKAUXSRC_ADC; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj)) { return CLKAUXSRC_RTC; - } - else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_ref_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_ref_obj)) { return CLKAUXSRC_REF; - } - else if (obj == MP_ROM_NONE) { + } else if (obj == MP_ROM_NONE) { return CLKAUXSRC_NONE; } mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_ClkAuxSrc, MP_QSTR_None, mp_obj_get_type(obj)->name); diff --git a/ports/raspberrypi/bindings/clkio/ClkAuxSrc.h b/ports/raspberrypi/bindings/clkio/ClkAuxSrc.h index 4eeaacbbf9c8..c23838ed8e43 100644 --- a/ports/raspberrypi/bindings/clkio/ClkAuxSrc.h +++ b/ports/raspberrypi/bindings/clkio/ClkAuxSrc.h @@ -63,4 +63,4 @@ extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_rtc_obj; extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_ref_obj; clkio_clkauxsrc_t validate_clkauxsrc(mp_rom_obj_t obj, qstr arg_name); -mp_obj_t clkauxsrc_get_obj (clkio_clkauxsrc_t type); +mp_obj_t clkauxsrc_get_obj(clkio_clkauxsrc_t type); diff --git a/ports/raspberrypi/bindings/clkio/ClkIndex.c b/ports/raspberrypi/bindings/clkio/ClkIndex.c index 43f8519a394f..a158ec43e7a9 100644 --- a/ports/raspberrypi/bindings/clkio/ClkIndex.c +++ b/ports/raspberrypi/bindings/clkio/ClkIndex.c @@ -86,32 +86,23 @@ STATIC void clkio_clkindex_print(const mp_print_t *print, mp_obj_t self_in, mp_p qstr clk = MP_QSTR_INVALID; if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout0_obj)) { clk = MP_QSTR_GPOUT0; - } - else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout1_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout1_obj)) { clk = MP_QSTR_GPOUT1; - } - else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout2_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout2_obj)) { clk = MP_QSTR_GPOUT2; - } - else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout3_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout3_obj)) { clk = MP_QSTR_GPOUT3; - } - else if (self_in == MP_ROM_PTR(&clkio_clkindex_ref_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkindex_ref_obj)) { clk = MP_QSTR_REF; - } - else if (self_in == MP_ROM_PTR(&clkio_clkindex_sys_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkindex_sys_obj)) { clk = MP_QSTR_SYS; - } - else if (self_in == MP_ROM_PTR(&clkio_clkindex_peri_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkindex_peri_obj)) { clk = MP_QSTR_PERI; - } - else if (self_in == MP_ROM_PTR(&clkio_clkindex_usb_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkindex_usb_obj)) { clk = MP_QSTR_USB; - } - else if (self_in == MP_ROM_PTR(&clkio_clkindex_adc_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkindex_adc_obj)) { clk = MP_QSTR_ADC; - } - else if (self_in == MP_ROM_PTR(&clkio_clkindex_rtc_obj)) { + } else if (self_in == MP_ROM_PTR(&clkio_clkindex_rtc_obj)) { clk = MP_QSTR_RTC; } mp_printf(print, "%q.%q.%q", MP_QSTR_clkio, MP_QSTR_ClkIndex, clk); @@ -126,62 +117,53 @@ MP_DEFINE_CONST_OBJ_TYPE( ); mp_obj_t clkindex_get_obj(clkio_clkindex_t type) { - if (type == CLKINDEX_GPOUT0) + if (type == CLKINDEX_GPOUT0) { return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout0_obj); - else if (type == CLKINDEX_GPOUT1) + } else if (type == CLKINDEX_GPOUT1) { return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout1_obj); - else if (type == CLKINDEX_GPOUT2) + } else if (type == CLKINDEX_GPOUT2) { return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout2_obj); - else if (type == CLKINDEX_GPOUT3) + } else if (type == CLKINDEX_GPOUT3) { return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout3_obj); - else if (type == CLKINDEX_REF) + } else if (type == CLKINDEX_REF) { return MP_OBJ_FROM_PTR(&clkio_clkindex_ref_obj); - else if (type == CLKINDEX_SYS) + } else if (type == CLKINDEX_SYS) { return MP_OBJ_FROM_PTR(&clkio_clkindex_sys_obj); - else if (type == CLKINDEX_PERI) + } else if (type == CLKINDEX_PERI) { return MP_OBJ_FROM_PTR(&clkio_clkindex_peri_obj); - else if (type == CLKINDEX_USB) + } else if (type == CLKINDEX_USB) { return MP_OBJ_FROM_PTR(&clkio_clkindex_usb_obj); - else if (type == CLKINDEX_ADC) + } else if (type == CLKINDEX_ADC) { return MP_OBJ_FROM_PTR(&clkio_clkindex_adc_obj); - else if (type == CLKINDEX_RTC) + } else if (type == CLKINDEX_RTC) { return MP_OBJ_FROM_PTR(&clkio_clkindex_rtc_obj); - else + } else { return MP_ROM_NONE; + } } clkio_clkindex_t validate_clkindex(mp_rom_obj_t obj, qstr arg_name) { if (obj == MP_ROM_PTR(&clkio_clkindex_gpout0_obj)) { return CLKINDEX_GPOUT0; - } - else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout1_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout1_obj)) { return CLKINDEX_GPOUT1; - } - else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout2_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout2_obj)) { return CLKINDEX_GPOUT2; - } - else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout3_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout3_obj)) { return CLKINDEX_GPOUT3; - } - else if (obj == MP_ROM_PTR(&clkio_clkindex_ref_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkindex_ref_obj)) { return CLKINDEX_REF; - } - else if (obj == MP_ROM_PTR(&clkio_clkindex_sys_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkindex_sys_obj)) { return CLKINDEX_SYS; - } - else if (obj == MP_ROM_PTR(&clkio_clkindex_peri_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkindex_peri_obj)) { return CLKINDEX_PERI; - } - else if (obj == MP_ROM_PTR(&clkio_clkindex_usb_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkindex_usb_obj)) { return CLKINDEX_USB; - } - else if (obj == MP_ROM_PTR(&clkio_clkindex_adc_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkindex_adc_obj)) { return CLKINDEX_ADC; - } - else if (obj == MP_ROM_PTR(&clkio_clkindex_rtc_obj)) { + } else if (obj == MP_ROM_PTR(&clkio_clkindex_rtc_obj)) { return CLKINDEX_RTC; - } - else if (obj == MP_ROM_NONE) { + } else if (obj == MP_ROM_NONE) { return CLKINDEX_NONE; } mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_ClkIndex, MP_QSTR_None, mp_obj_get_type(obj)->name); diff --git a/ports/raspberrypi/bindings/clkio/ClkInput.c b/ports/raspberrypi/bindings/clkio/ClkInput.c index 923f86c7c7da..9c0f9205e79e 100644 --- a/ports/raspberrypi/bindings/clkio/ClkInput.c +++ b/ports/raspberrypi/bindings/clkio/ClkInput.c @@ -45,8 +45,8 @@ STATIC void check_for_deinit(clkio_clkinput_obj_t *self) { //| pin: microcontroller.Pin, //| *, //| clkindex: clkio.ClkIndex, -//| src_freq: uint32, -//| target_freq: uint32 +//| src_freq: int, +//| target_freq: int //| ) -> None: //| """Creates a clock input pin object. //| pin: Pin to be used as clock input, allowed pins: 20,22 @@ -120,7 +120,7 @@ STATIC mp_obj_t clkio_clkinput_disable(size_t n_args, const mp_obj_t *pos_args, } MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_disable_obj, 1, clkio_clkinput_disable); -//| def set_freq(self, src_freq: uint32, target_freq: uint32) -> None: +//| def set_freq(self, src_freq: int, target_freq: int) -> None: //| """Configures the src and target frequency. Must be set before enable() is called.""" STATIC mp_obj_t clkio_clkinput_set_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_src_freq, ARG_target_freq }; @@ -140,10 +140,8 @@ STATIC mp_obj_t clkio_clkinput_set_freq(size_t n_args, const mp_obj_t *pos_args, } MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_set_freq_obj, 1, clkio_clkinput_set_freq); -//| def set_freq( -//| self, -//| ) -> tuple(src_freq, target_freq): -//| """Returns the src and target frequency.""" +//| def get_freq(self) -> tuple[int, int]: +//| """Returns the (src, target) frequency as tuple.""" STATIC mp_obj_t clkio_clkinput_get_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); @@ -157,7 +155,8 @@ STATIC mp_obj_t clkio_clkinput_get_freq(size_t n_args, const mp_obj_t *pos_args, MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_get_freq_obj, 1, clkio_clkinput_get_freq); //| clkindex: clkio.ClkIndex -//| """Get clock that will be driven from external pin.""" +//| """Clock that will be driven from external pin.""" +//| static mp_obj_t clkio_clkinput_clkindex_get(mp_obj_t self_in) { clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); @@ -165,9 +164,6 @@ static mp_obj_t clkio_clkinput_clkindex_get(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkinput_clkindex_get_obj, clkio_clkinput_clkindex_get); -//| clkindex: clkio.ClkIndex -//| """Set clock that will be driven from external pin.""" -//| static mp_obj_t clkio_clkinput_clkindex_set(mp_obj_t self_in, mp_obj_t clkindex_obj) { clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); diff --git a/ports/raspberrypi/bindings/clkio/ClkOutput.c b/ports/raspberrypi/bindings/clkio/ClkOutput.c index 8eccbe9c2c83..613ac519cc87 100644 --- a/ports/raspberrypi/bindings/clkio/ClkOutput.c +++ b/ports/raspberrypi/bindings/clkio/ClkOutput.c @@ -108,7 +108,7 @@ STATIC mp_obj_t clkio_clkoutput_disable(size_t n_args, const mp_obj_t *pos_args, MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkoutput_disable_obj, 1, clkio_clkoutput_disable); //| clksrc: clkio.ClkAuxSrc -//| """Gets the auxiliary source clock to be driven to pin.""" +//| """Auxiliary source clock to be driven to pin.""" static mp_obj_t clkio_clkoutput_clksrc_get(mp_obj_t self_in) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); @@ -116,8 +116,6 @@ static mp_obj_t clkio_clkoutput_clksrc_get(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_clksrc_get_obj, clkio_clkoutput_clksrc_get); -//| clksrc: clkio.ClkAuxSrc -//| """Sets the auxiliary source clock to be driven to pin.""" static mp_obj_t clkio_clkoutput_clksrc_set(mp_obj_t self_in, mp_obj_t clksrc_obj) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); @@ -130,7 +128,8 @@ MP_PROPERTY_GETSET(clkio_clkoutput_clksrc_obj, (mp_obj_t)&clkio_clkoutput_clksrc_set_obj); //| divisor: float -//| """Gets the divisor used to divide the clock before it's output to pin.""" +//| """Divisor used to divide the clock before it's output to pin.""" +//| static mp_obj_t clkio_clkoutput_divisor_get(mp_obj_t self_in) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); @@ -138,9 +137,6 @@ static mp_obj_t clkio_clkoutput_divisor_get(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_divisor_get_obj, clkio_clkoutput_divisor_get); -//| divisor: float -//| """Sets the divisor used to divide the clock before it's output to pin.""" -//| static mp_obj_t clkio_clkoutput_divisor_set(mp_obj_t self_in, mp_obj_t divisor_obj) { clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); diff --git a/ports/raspberrypi/common-hal/clkio/ClkInput.h b/ports/raspberrypi/common-hal/clkio/ClkInput.h index 4b8bfce07268..b089ed02b9f9 100644 --- a/ports/raspberrypi/common-hal/clkio/ClkInput.h +++ b/ports/raspberrypi/common-hal/clkio/ClkInput.h @@ -32,17 +32,17 @@ typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; - clkio_clkindex_t clkindex; + clkio_clkindex_t clkindex; uint32_t src_freq; uint32_t target_freq; bool enabled; } clkio_clkinput_obj_t; -void common_hal_clkio_clkinput_validate_clkindex_pin (const mcu_pin_obj_t *pin); -bool common_hal_clkio_clkinput_deinited (clkio_clkinput_obj_t *self); -void common_hal_clkio_clkinput_deinit (clkio_clkinput_obj_t *self); -void common_hal_clkio_clkinput_validate_freqs (uint32_t src, uint32_t target); +void common_hal_clkio_clkinput_validate_clkindex_pin(const mcu_pin_obj_t *pin); +bool common_hal_clkio_clkinput_deinited(clkio_clkinput_obj_t *self); +void common_hal_clkio_clkinput_deinit(clkio_clkinput_obj_t *self); +void common_hal_clkio_clkinput_validate_freqs(uint32_t src, uint32_t target); // Configure clock in/out -void common_hal_clkio_clkinput_enable (clkio_clkinput_obj_t *self); -void common_hal_clkio_clkinput_disable (clkio_clkinput_obj_t *self); +void common_hal_clkio_clkinput_enable(clkio_clkinput_obj_t *self); +void common_hal_clkio_clkinput_disable(clkio_clkinput_obj_t *self); diff --git a/ports/raspberrypi/common-hal/clkio/ClkOutput.h b/ports/raspberrypi/common-hal/clkio/ClkOutput.h index 0ca9562cec9f..e3cdc52c1aef 100644 --- a/ports/raspberrypi/common-hal/clkio/ClkOutput.h +++ b/ports/raspberrypi/common-hal/clkio/ClkOutput.h @@ -37,11 +37,11 @@ typedef struct { bool enabled; } clkio_clkoutput_obj_t; -void common_hal_clkio_clkoutput_validate_clksrc_pin (const mcu_pin_obj_t *pin); -bool common_hal_clkio_clkoutput_deinited (clkio_clkoutput_obj_t *self); -void common_hal_clkio_clkoutput_deinit (clkio_clkoutput_obj_t *self); -mp_float_t common_hal_clkio_clkoutput_validate_divisor (mp_float_t div); +void common_hal_clkio_clkoutput_validate_clksrc_pin(const mcu_pin_obj_t *pin); +bool common_hal_clkio_clkoutput_deinited(clkio_clkoutput_obj_t *self); +void common_hal_clkio_clkoutput_deinit(clkio_clkoutput_obj_t *self); +mp_float_t common_hal_clkio_clkoutput_validate_divisor(mp_float_t div); // Configure clock out -void common_hal_clkio_clkoutput_enable (clkio_clkoutput_obj_t *self); -void common_hal_clkio_clkoutput_disable (clkio_clkoutput_obj_t *self); +void common_hal_clkio_clkoutput_enable(clkio_clkoutput_obj_t *self); +void common_hal_clkio_clkoutput_disable(clkio_clkoutput_obj_t *self); From 128b8c4ca22a8a31779df7e44500a7d802b0cfa4 Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Fri, 22 Mar 2024 10:27:13 -0600 Subject: [PATCH 04/11] Rename clkio to rp2clock --- ports/raspberrypi/Makefile | 16 ++++++++-------- .../bindings/{clkio => rp2clock}/ClkAuxSrc.c | 2 +- .../bindings/{clkio => rp2clock}/ClkAuxSrc.h | 0 .../bindings/{clkio => rp2clock}/ClkIndex.c | 2 +- .../bindings/{clkio => rp2clock}/ClkIndex.h | 0 .../bindings/{clkio => rp2clock}/ClkInput.c | 6 +++--- .../bindings/{clkio => rp2clock}/ClkInput.h | 0 .../bindings/{clkio => rp2clock}/ClkOutput.c | 4 ++-- .../bindings/{clkio => rp2clock}/ClkOutput.h | 0 .../bindings/{clkio => rp2clock}/__init__.c | 12 ++++++------ .../bindings/{clkio => rp2clock}/__init__.h | 0 .../boards/raspberry_pi_pico/mpconfigboard.mk | 1 + .../common-hal/{clkio => rp2clock}/ClkInput.c | 4 ++-- .../common-hal/{clkio => rp2clock}/ClkInput.h | 2 +- .../common-hal/{clkio => rp2clock}/ClkOutput.c | 6 +++--- .../common-hal/{clkio => rp2clock}/ClkOutput.h | 2 +- .../common-hal/{clkio => rp2clock}/__init__.c | 0 py/circuitpy_mpconfig.mk | 6 +++--- 18 files changed, 32 insertions(+), 31 deletions(-) rename ports/raspberrypi/bindings/{clkio => rp2clock}/ClkAuxSrc.c (99%) rename ports/raspberrypi/bindings/{clkio => rp2clock}/ClkAuxSrc.h (100%) rename ports/raspberrypi/bindings/{clkio => rp2clock}/ClkIndex.c (99%) rename ports/raspberrypi/bindings/{clkio => rp2clock}/ClkIndex.h (100%) rename ports/raspberrypi/bindings/{clkio => rp2clock}/ClkInput.c (98%) rename ports/raspberrypi/bindings/{clkio => rp2clock}/ClkInput.h (100%) rename ports/raspberrypi/bindings/{clkio => rp2clock}/ClkOutput.c (98%) rename ports/raspberrypi/bindings/{clkio => rp2clock}/ClkOutput.h (100%) rename ports/raspberrypi/bindings/{clkio => rp2clock}/__init__.c (88%) rename ports/raspberrypi/bindings/{clkio => rp2clock}/__init__.h (100%) rename ports/raspberrypi/common-hal/{clkio => rp2clock}/ClkInput.c (97%) rename ports/raspberrypi/common-hal/{clkio => rp2clock}/ClkInput.h (98%) rename ports/raspberrypi/common-hal/{clkio => rp2clock}/ClkOutput.c (96%) rename ports/raspberrypi/common-hal/{clkio => rp2clock}/ClkOutput.h (97%) rename ports/raspberrypi/common-hal/{clkio => rp2clock}/__init__.c (100%) diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 9930c3226bad..e3be099f1281 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -259,15 +259,15 @@ SRC_C += \ $(SRC_CYW43) \ $(SRC_LWIP) \ -ifeq ($(CIRCUITPY_RP2CLKIO),1) +ifeq ($(CIRCUITPY_RP2CLOCK),1) SRC_C += \ - bindings/clkio/__init__.c \ - bindings/clkio/ClkOutput.c \ - bindings/clkio/ClkInput.c \ - bindings/clkio/ClkAuxSrc.c \ - bindings/clkio/ClkIndex.c \ - common-hal/clkio/ClkOutput.c \ - common-hal/clkio/ClkInput.c \ + bindings/rp2clock/__init__.c \ + bindings/rp2clock/ClkOutput.c \ + bindings/rp2clock/ClkInput.c \ + bindings/rp2clock/ClkAuxSrc.c \ + bindings/rp2clock/ClkIndex.c \ + common-hal/rp2clock/ClkOutput.c \ + common-hal/rp2clock/ClkInput.c \ endif diff --git a/ports/raspberrypi/bindings/clkio/ClkAuxSrc.c b/ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.c similarity index 99% rename from ports/raspberrypi/bindings/clkio/ClkAuxSrc.c rename to ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.c index bc3a08600a6a..9a91cedf59e8 100644 --- a/ports/raspberrypi/bindings/clkio/ClkAuxSrc.c +++ b/ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.c @@ -26,7 +26,7 @@ #include "py/runtime.h" #include "supervisor/shared/translate/translate.h" -#include "bindings/clkio/ClkAuxSrc.h" +#include "bindings/rp2clock/ClkAuxSrc.h" //| class ClkAuxSrc: //| """Defines the input clock for GPOUT on RP2040""" diff --git a/ports/raspberrypi/bindings/clkio/ClkAuxSrc.h b/ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.h similarity index 100% rename from ports/raspberrypi/bindings/clkio/ClkAuxSrc.h rename to ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.h diff --git a/ports/raspberrypi/bindings/clkio/ClkIndex.c b/ports/raspberrypi/bindings/rp2clock/ClkIndex.c similarity index 99% rename from ports/raspberrypi/bindings/clkio/ClkIndex.c rename to ports/raspberrypi/bindings/rp2clock/ClkIndex.c index a158ec43e7a9..f49618e64746 100644 --- a/ports/raspberrypi/bindings/clkio/ClkIndex.c +++ b/ports/raspberrypi/bindings/rp2clock/ClkIndex.c @@ -26,7 +26,7 @@ #include "py/runtime.h" #include "supervisor/shared/translate/translate.h" -#include "bindings/clkio/ClkIndex.h" +#include "bindings/rp2clock/ClkIndex.h" //| class ClkIndex: //| """Defines the internal clock index to drive GPIN from an external pin.""" diff --git a/ports/raspberrypi/bindings/clkio/ClkIndex.h b/ports/raspberrypi/bindings/rp2clock/ClkIndex.h similarity index 100% rename from ports/raspberrypi/bindings/clkio/ClkIndex.h rename to ports/raspberrypi/bindings/rp2clock/ClkIndex.h diff --git a/ports/raspberrypi/bindings/clkio/ClkInput.c b/ports/raspberrypi/bindings/rp2clock/ClkInput.c similarity index 98% rename from ports/raspberrypi/bindings/clkio/ClkInput.c rename to ports/raspberrypi/bindings/rp2clock/ClkInput.c index 9c0f9205e79e..6f8afacfca0a 100644 --- a/ports/raspberrypi/bindings/clkio/ClkInput.c +++ b/ports/raspberrypi/bindings/rp2clock/ClkInput.c @@ -30,8 +30,8 @@ #include "py/objarray.h" #include "shared-bindings/util.h" -#include "bindings/clkio/ClkInput.h" -#include "common-hal/clkio/ClkInput.h" +#include "bindings/rp2clock/ClkInput.h" +#include "common-hal/rp2clock/ClkInput.h" STATIC void check_for_deinit(clkio_clkinput_obj_t *self) { if (common_hal_clkio_clkinput_deinited(self)) { @@ -71,8 +71,8 @@ STATIC mp_obj_t clkio_clkinput_make_new(const mp_obj_type_t *type, size_t n_args self->base.type = &clkio_clkinput_type; // Validate pin number + common_hal_clkio_clkinput_validate_clkindex_pin(args[ARG_pin].u_rom_obj); self->pin = args[ARG_pin].u_rom_obj; - common_hal_clkio_clkinput_validate_clkindex_pin(self->pin); // Validate pin based on clock if (args[ARG_clkindex].u_rom_obj != mp_const_none) { diff --git a/ports/raspberrypi/bindings/clkio/ClkInput.h b/ports/raspberrypi/bindings/rp2clock/ClkInput.h similarity index 100% rename from ports/raspberrypi/bindings/clkio/ClkInput.h rename to ports/raspberrypi/bindings/rp2clock/ClkInput.h diff --git a/ports/raspberrypi/bindings/clkio/ClkOutput.c b/ports/raspberrypi/bindings/rp2clock/ClkOutput.c similarity index 98% rename from ports/raspberrypi/bindings/clkio/ClkOutput.c rename to ports/raspberrypi/bindings/rp2clock/ClkOutput.c index 613ac519cc87..d4d4eccf3c86 100644 --- a/ports/raspberrypi/bindings/clkio/ClkOutput.c +++ b/ports/raspberrypi/bindings/rp2clock/ClkOutput.c @@ -30,8 +30,8 @@ #include "py/objarray.h" #include "shared-bindings/util.h" -#include "bindings/clkio/ClkOutput.h" -#include "common-hal/clkio/ClkOutput.h" +#include "bindings/rp2clock/ClkOutput.h" +#include "common-hal/rp2clock/ClkOutput.h" STATIC void check_for_deinit(clkio_clkoutput_obj_t *self) { if (common_hal_clkio_clkoutput_deinited(self)) { diff --git a/ports/raspberrypi/bindings/clkio/ClkOutput.h b/ports/raspberrypi/bindings/rp2clock/ClkOutput.h similarity index 100% rename from ports/raspberrypi/bindings/clkio/ClkOutput.h rename to ports/raspberrypi/bindings/rp2clock/ClkOutput.h diff --git a/ports/raspberrypi/bindings/clkio/__init__.c b/ports/raspberrypi/bindings/rp2clock/__init__.c similarity index 88% rename from ports/raspberrypi/bindings/clkio/__init__.c rename to ports/raspberrypi/bindings/rp2clock/__init__.c index cd7c8ea3c719..7c33b113f8c9 100644 --- a/ports/raspberrypi/bindings/clkio/__init__.c +++ b/ports/raspberrypi/bindings/rp2clock/__init__.c @@ -29,16 +29,16 @@ #include "shared-bindings/board/__init__.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Pin.h" -#include "bindings/clkio/__init__.h" -#include "bindings/clkio/ClkAuxSrc.h" -#include "bindings/clkio/ClkIndex.h" -#include "bindings/clkio/ClkOutput.h" -#include "bindings/clkio/ClkInput.h" +#include "bindings/rp2clock/__init__.h" +#include "bindings/rp2clock/ClkAuxSrc.h" +#include "bindings/rp2clock/ClkIndex.h" +#include "bindings/rp2clock/ClkOutput.h" +#include "bindings/rp2clock/ClkInput.h" STATIC const mp_rom_map_elem_t clkio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_clkio) }, { MP_ROM_QSTR(MP_QSTR_ClkOutput), MP_ROM_PTR(&clkio_clkoutput_type) }, - { MP_ROM_QSTR(MP_QSTR_ClkInput), MP_ROM_PTR(&clkio_clkinput_type) }, + { MP_ROM_QSTR(MP_QSTR_ClkInput), MP_ROM_PTR(&clkio_clkinput_type) }, // Enum like classes { MP_ROM_QSTR(MP_QSTR_ClkAuxSrc), MP_ROM_PTR(&clkio_clkauxsrc_type) }, diff --git a/ports/raspberrypi/bindings/clkio/__init__.h b/ports/raspberrypi/bindings/rp2clock/__init__.h similarity index 100% rename from ports/raspberrypi/bindings/clkio/__init__.h rename to ports/raspberrypi/bindings/rp2clock/__init__.h diff --git a/ports/raspberrypi/boards/raspberry_pi_pico/mpconfigboard.mk b/ports/raspberrypi/boards/raspberry_pi_pico/mpconfigboard.mk index 20607d50e41c..608289888e05 100644 --- a/ports/raspberrypi/boards/raspberry_pi_pico/mpconfigboard.mk +++ b/ports/raspberrypi/boards/raspberry_pi_pico/mpconfigboard.mk @@ -10,3 +10,4 @@ EXTERNAL_FLASH_DEVICES = "W25Q16JVxQ" CIRCUITPY__EVE = 1 CIRCUITPY_PICODVI = 1 +CIRCUITPY_RP2CLOCK = 1 diff --git a/ports/raspberrypi/common-hal/clkio/ClkInput.c b/ports/raspberrypi/common-hal/rp2clock/ClkInput.c similarity index 97% rename from ports/raspberrypi/common-hal/clkio/ClkInput.c rename to ports/raspberrypi/common-hal/rp2clock/ClkInput.c index ec87c0151962..f4bbd7d9c4d6 100644 --- a/ports/raspberrypi/common-hal/clkio/ClkInput.c +++ b/ports/raspberrypi/common-hal/rp2clock/ClkInput.c @@ -24,8 +24,8 @@ * THE SOFTWARE. */ #include "shared-bindings/microcontroller/Pin.h" -#include "common-hal/clkio/ClkInput.h" -#include "bindings/clkio/ClkIndex.h" +#include "common-hal/rp2clock/ClkInput.h" +#include "bindings/rp2clock/ClkIndex.h" #include "hardware/clocks.h" #include "hardware/gpio.h" #include "py/runtime.h" diff --git a/ports/raspberrypi/common-hal/clkio/ClkInput.h b/ports/raspberrypi/common-hal/rp2clock/ClkInput.h similarity index 98% rename from ports/raspberrypi/common-hal/clkio/ClkInput.h rename to ports/raspberrypi/common-hal/rp2clock/ClkInput.h index b089ed02b9f9..02dd66b63fcd 100644 --- a/ports/raspberrypi/common-hal/clkio/ClkInput.h +++ b/ports/raspberrypi/common-hal/rp2clock/ClkInput.h @@ -26,7 +26,7 @@ #pragma once #include "common-hal/microcontroller/Pin.h" -#include "bindings/clkio/ClkIndex.h" +#include "bindings/rp2clock/ClkIndex.h" #include "hardware/clocks.h" typedef struct { diff --git a/ports/raspberrypi/common-hal/clkio/ClkOutput.c b/ports/raspberrypi/common-hal/rp2clock/ClkOutput.c similarity index 96% rename from ports/raspberrypi/common-hal/clkio/ClkOutput.c rename to ports/raspberrypi/common-hal/rp2clock/ClkOutput.c index 6cacc9187cee..50a9f97550a2 100644 --- a/ports/raspberrypi/common-hal/clkio/ClkOutput.c +++ b/ports/raspberrypi/common-hal/rp2clock/ClkOutput.c @@ -24,9 +24,9 @@ * THE SOFTWARE. */ #include "shared-bindings/microcontroller/Pin.h" -#include "common-hal/clkio/ClkOutput.h" -#include "bindings/clkio/ClkAuxSrc.h" -#include "bindings/clkio/ClkIndex.h" +#include "common-hal/rp2clock/ClkOutput.h" +#include "bindings/rp2clock/ClkAuxSrc.h" +#include "bindings/rp2clock/ClkIndex.h" #include "hardware/clocks.h" #include "hardware/gpio.h" #include "py/runtime.h" diff --git a/ports/raspberrypi/common-hal/clkio/ClkOutput.h b/ports/raspberrypi/common-hal/rp2clock/ClkOutput.h similarity index 97% rename from ports/raspberrypi/common-hal/clkio/ClkOutput.h rename to ports/raspberrypi/common-hal/rp2clock/ClkOutput.h index e3cdc52c1aef..1ede815996a0 100644 --- a/ports/raspberrypi/common-hal/clkio/ClkOutput.h +++ b/ports/raspberrypi/common-hal/rp2clock/ClkOutput.h @@ -26,7 +26,7 @@ #pragma once #include "common-hal/microcontroller/Pin.h" -#include "bindings/clkio/ClkAuxSrc.h" +#include "bindings/rp2clock/ClkAuxSrc.h" #include "hardware/clocks.h" typedef struct { diff --git a/ports/raspberrypi/common-hal/clkio/__init__.c b/ports/raspberrypi/common-hal/rp2clock/__init__.c similarity index 100% rename from ports/raspberrypi/common-hal/clkio/__init__.c rename to ports/raspberrypi/common-hal/rp2clock/__init__.c diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index a94bb8dd2357..e06fd18372c8 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -431,11 +431,11 @@ CFLAGS += -DCIRCUITPY_REQUIRE_I2C_PULLUPS=$(CIRCUITPY_REQUIRE_I2C_PULLUPS) CIRCUITPY_RP2PIO ?= 0 CFLAGS += -DCIRCUITPY_RP2PIO=$(CIRCUITPY_RP2PIO) -# CIRCUITPY_RP2CLKIO is handled in the raspberrypi tree. +# CIRCUITPY_RP2CLOCK is handled in the raspberrypi tree. # Only for rp2 chips. # Assume not a rp2 build. -CIRCUITPY_RP2CLKIO ?= 0 -CFLAGS += -DCIRCUITPY_RP2CLKIO=$(CIRCUITPY_RP2CLKIO) +CIRCUITPY_RP2CLOCK ?= 0 +CFLAGS += -DCIRCUITPY_RP2CLOCK=$(CIRCUITPY_RP2CLOCK) CIRCUITPY_RGBMATRIX ?= 0 CFLAGS += -DCIRCUITPY_RGBMATRIX=$(CIRCUITPY_RGBMATRIX) From c60fa3df78d01c0e45994c9efa208cbf57103d7a Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Fri, 22 Mar 2024 11:27:09 -0600 Subject: [PATCH 05/11] Update class names --- ports/raspberrypi/Makefile | 12 +- ports/raspberrypi/bindings/rp2clock/AuxSrc.c | 179 ++++++++++++++++++ .../rp2clock/{ClkAuxSrc.h => AuxSrc.h} | 58 +++--- .../raspberrypi/bindings/rp2clock/ClkAuxSrc.c | 179 ------------------ .../raspberrypi/bindings/rp2clock/ClkIndex.c | 170 ----------------- .../raspberrypi/bindings/rp2clock/ClkOutput.c | 169 ----------------- ports/raspberrypi/bindings/rp2clock/Index.c | 170 +++++++++++++++++ .../bindings/rp2clock/{ClkIndex.h => Index.h} | 54 +++--- .../rp2clock/{ClkInput.c => InputPin.c} | 122 ++++++------ .../rp2clock/{ClkInput.h => InputPin.h} | 2 +- .../raspberrypi/bindings/rp2clock/OutputPin.c | 169 +++++++++++++++++ .../rp2clock/{ClkOutput.h => OutputPin.h} | 2 +- .../raspberrypi/bindings/rp2clock/__init__.c | 28 +-- .../rp2clock/{ClkInput.c => InputPin.c} | 30 +-- .../rp2clock/{ClkInput.h => InputPin.h} | 18 +- .../rp2clock/{ClkOutput.c => OutputPin.c} | 31 ++- .../rp2clock/{ClkOutput.h => OutputPin.h} | 18 +- .../common-hal/rp2clock/__init__.c | 2 +- 18 files changed, 706 insertions(+), 707 deletions(-) create mode 100644 ports/raspberrypi/bindings/rp2clock/AuxSrc.c rename ports/raspberrypi/bindings/rp2clock/{ClkAuxSrc.h => AuxSrc.h} (50%) delete mode 100644 ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.c delete mode 100644 ports/raspberrypi/bindings/rp2clock/ClkIndex.c delete mode 100644 ports/raspberrypi/bindings/rp2clock/ClkOutput.c create mode 100644 ports/raspberrypi/bindings/rp2clock/Index.c rename ports/raspberrypi/bindings/rp2clock/{ClkIndex.h => Index.h} (52%) rename ports/raspberrypi/bindings/rp2clock/{ClkInput.c => InputPin.c} (51%) rename ports/raspberrypi/bindings/rp2clock/{ClkInput.h => InputPin.h} (96%) create mode 100644 ports/raspberrypi/bindings/rp2clock/OutputPin.c rename ports/raspberrypi/bindings/rp2clock/{ClkOutput.h => OutputPin.h} (96%) rename ports/raspberrypi/common-hal/rp2clock/{ClkInput.c => InputPin.c} (74%) rename ports/raspberrypi/common-hal/rp2clock/{ClkInput.h => InputPin.h} (73%) rename ports/raspberrypi/common-hal/rp2clock/{ClkOutput.c => OutputPin.c} (73%) rename ports/raspberrypi/common-hal/rp2clock/{ClkOutput.h => OutputPin.h} (73%) diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index e3be099f1281..56d226ad0e68 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -262,12 +262,12 @@ SRC_C += \ ifeq ($(CIRCUITPY_RP2CLOCK),1) SRC_C += \ bindings/rp2clock/__init__.c \ - bindings/rp2clock/ClkOutput.c \ - bindings/rp2clock/ClkInput.c \ - bindings/rp2clock/ClkAuxSrc.c \ - bindings/rp2clock/ClkIndex.c \ - common-hal/rp2clock/ClkOutput.c \ - common-hal/rp2clock/ClkInput.c \ + bindings/rp2clock/OutputPin.c \ + bindings/rp2clock/InputPin.c \ + bindings/rp2clock/AuxSrc.c \ + bindings/rp2clock/Index.c \ + common-hal/rp2clock/OutputPin.c \ + common-hal/rp2clock/InputPin.c \ endif diff --git a/ports/raspberrypi/bindings/rp2clock/AuxSrc.c b/ports/raspberrypi/bindings/rp2clock/AuxSrc.c new file mode 100644 index 000000000000..0dc4dce0beb6 --- /dev/null +++ b/ports/raspberrypi/bindings/rp2clock/AuxSrc.c @@ -0,0 +1,179 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "py/runtime.h" +#include "supervisor/shared/translate/translate.h" +#include "bindings/rp2clock/AuxSrc.h" + +//| class AuxSrc: +//| """Defines the input clock for GPOUT on RP2040""" +//| +//| def __init__(self) -> None: +//| """Enum-like class to define the clock src.""" +//| +const mp_obj_type_t rp2clock_auxsrc_type; + +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_sys_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_gpin0_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_gpin1_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_usb_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_rosc_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_xosc_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_sys_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_usb_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_adc_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_rtc_obj = { + { &rp2clock_auxsrc_type }, +}; +const rp2clock_auxsrc_obj_t rp2clock_auxsrc_ref_obj = { + { &rp2clock_auxsrc_type }, +}; + + +STATIC const mp_rom_map_elem_t rp2clock_auxsrc_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_PLL_SYS), MP_ROM_PTR(&rp2clock_auxsrc_pll_sys_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPIN0), MP_ROM_PTR(&rp2clock_auxsrc_gpin0_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPIN1), MP_ROM_PTR(&rp2clock_auxsrc_gpin1_obj) }, + { MP_ROM_QSTR(MP_QSTR_PLL_USB), MP_ROM_PTR(&rp2clock_auxsrc_pll_usb_obj) }, + { MP_ROM_QSTR(MP_QSTR_PLL_ROSC), MP_ROM_PTR(&rp2clock_auxsrc_pll_rosc_obj) }, + { MP_ROM_QSTR(MP_QSTR_PLL_XOSC), MP_ROM_PTR(&rp2clock_auxsrc_pll_xosc_obj) }, + { MP_ROM_QSTR(MP_QSTR_SYS), MP_ROM_PTR(&rp2clock_auxsrc_sys_obj) }, + { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&rp2clock_auxsrc_usb_obj) }, + { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&rp2clock_auxsrc_adc_obj) }, + { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&rp2clock_auxsrc_rtc_obj) }, + { MP_ROM_QSTR(MP_QSTR_REF), MP_ROM_PTR(&rp2clock_auxsrc_ref_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(rp2clock_auxsrc_locals_dict, rp2clock_auxsrc_locals_dict_table); + +STATIC void rp2clock_auxsrc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr clk = MP_QSTR_INVALID; + if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_pll_sys_obj)) { + clk = MP_QSTR_PLL_SYS; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_gpin0_obj)) { + clk = MP_QSTR_GPIN0; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_gpin1_obj)) { + clk = MP_QSTR_GPIN1; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_pll_usb_obj)) { + clk = MP_QSTR_PLL_USB; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_pll_rosc_obj)) { + clk = MP_QSTR_PLL_ROSC; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_pll_xosc_obj)) { + clk = MP_QSTR_PLL_XOSC; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_sys_obj)) { + clk = MP_QSTR_SYS; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_usb_obj)) { + clk = MP_QSTR_USB; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_adc_obj)) { + clk = MP_QSTR_ADC; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_rtc_obj)) { + clk = MP_QSTR_RTC; + } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_ref_obj)) { + clk = MP_QSTR_REF; + } + mp_printf(print, "%q.%q.%q", MP_QSTR_rp2clock, MP_QSTR_AuxSrc, clk); +} + +MP_DEFINE_CONST_OBJ_TYPE( + rp2clock_auxsrc_type, + MP_QSTR_AuxSrc, + MP_TYPE_FLAG_NONE, + print, rp2clock_auxsrc_print, + locals_dict, &rp2clock_auxsrc_locals_dict + ); + +mp_obj_t auxsrc_get_obj(rp2clock_auxsrc_t type) { + if (type == AUXSRC_PLL_SYS) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_pll_sys_obj); + } else if (type == AUXSRC_GPIN0) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_gpin0_obj); + } else if (type == AUXSRC_GPIN1) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_gpin1_obj); + } else if (type == AUXSRC_PLL_USB) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_pll_usb_obj); + } else if (type == AUXSRC_PLL_ROSC) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_pll_rosc_obj); + } else if (type == AUXSRC_PLL_XOSC) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_pll_xosc_obj); + } else if (type == AUXSRC_SYS) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_sys_obj); + } else if (type == AUXSRC_USB) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_usb_obj); + } else if (type == AUXSRC_ADC) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_adc_obj); + } else if (type == AUXSRC_RTC) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_rtc_obj); + } else if (type == AUXSRC_REF) { + return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_ref_obj); + } else { + return MP_ROM_NONE; + } +} +rp2clock_auxsrc_t validate_auxsrc(mp_rom_obj_t obj, qstr arg_name) { + if (obj == MP_ROM_PTR(&rp2clock_auxsrc_pll_sys_obj)) { + return AUXSRC_PLL_SYS; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_gpin0_obj)) { + return AUXSRC_GPIN0; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_gpin1_obj)) { + return AUXSRC_GPIN1; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_pll_usb_obj)) { + return AUXSRC_PLL_USB; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_pll_rosc_obj)) { + return AUXSRC_PLL_ROSC; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_pll_xosc_obj)) { + return AUXSRC_PLL_XOSC; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_sys_obj)) { + return AUXSRC_SYS; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_usb_obj)) { + return AUXSRC_USB; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_adc_obj)) { + return AUXSRC_ADC; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_rtc_obj)) { + return AUXSRC_RTC; + } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_ref_obj)) { + return AUXSRC_REF; + } else if (obj == MP_ROM_NONE) { + return AUXSRC_NONE; + } + mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_AuxSrc, MP_QSTR_None, mp_obj_get_type(obj)->name); +} diff --git a/ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.h b/ports/raspberrypi/bindings/rp2clock/AuxSrc.h similarity index 50% rename from ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.h rename to ports/raspberrypi/bindings/rp2clock/AuxSrc.h index c23838ed8e43..2213fe4f5467 100644 --- a/ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.h +++ b/ports/raspberrypi/bindings/rp2clock/AuxSrc.h @@ -29,38 +29,38 @@ #include "py/obj.h" // Output sources -typedef enum _clkio_clkauxsrc_t { - CLKAUXSRC_PLL_SYS = 0, - CLKAUXSRC_GPIN0 = 1, - CLKAUXSRC_GPIN1 = 2, - CLKAUXSRC_PLL_USB = 3, - CLKAUXSRC_PLL_ROSC = 4, - CLKAUXSRC_PLL_XOSC = 5, - CLKAUXSRC_SYS = 6, - CLKAUXSRC_USB = 7, - CLKAUXSRC_ADC = 8, - CLKAUXSRC_RTC = 9, - CLKAUXSRC_REF = 10, - CLKAUXSRC_NONE = 11, -} clkio_clkauxsrc_t; +typedef enum _rp2clock_auxsrc_t { + AUXSRC_PLL_SYS = 0, + AUXSRC_GPIN0 = 1, + AUXSRC_GPIN1 = 2, + AUXSRC_PLL_USB = 3, + AUXSRC_PLL_ROSC = 4, + AUXSRC_PLL_XOSC = 5, + AUXSRC_SYS = 6, + AUXSRC_USB = 7, + AUXSRC_ADC = 8, + AUXSRC_RTC = 9, + AUXSRC_REF = 10, + AUXSRC_NONE = 11, +} rp2clock_auxsrc_t; -extern const mp_obj_type_t clkio_clkauxsrc_type; +extern const mp_obj_type_t rp2clock_auxsrc_type; typedef struct { mp_obj_base_t base; -} clkio_clkauxsrc_obj_t; +} rp2clock_auxsrc_obj_t; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_sys_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_gpin0_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_gpin1_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_usb_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_rosc_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_xosc_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_sys_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_usb_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_adc_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_rtc_obj; -extern const clkio_clkauxsrc_obj_t clkio_clkauxsrc_ref_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_sys_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_gpin0_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_gpin1_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_usb_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_rosc_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_xosc_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_sys_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_usb_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_adc_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_rtc_obj; +extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_ref_obj; -clkio_clkauxsrc_t validate_clkauxsrc(mp_rom_obj_t obj, qstr arg_name); -mp_obj_t clkauxsrc_get_obj(clkio_clkauxsrc_t type); +rp2clock_auxsrc_t validate_auxsrc(mp_rom_obj_t obj, qstr arg_name); +mp_obj_t auxsrc_get_obj(rp2clock_auxsrc_t type); diff --git a/ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.c b/ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.c deleted file mode 100644 index 9a91cedf59e8..000000000000 --- a/ports/raspberrypi/bindings/rp2clock/ClkAuxSrc.c +++ /dev/null @@ -1,179 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2024 Elliot Buller - * - * 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 "py/runtime.h" -#include "supervisor/shared/translate/translate.h" -#include "bindings/rp2clock/ClkAuxSrc.h" - -//| class ClkAuxSrc: -//| """Defines the input clock for GPOUT on RP2040""" -//| -//| def __init__(self) -> None: -//| """Enum-like class to define the clock src.""" -//| -const mp_obj_type_t clkio_clkauxsrc_type; - -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_sys_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_gpin0_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_gpin1_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_usb_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_rosc_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_pll_xosc_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_sys_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_usb_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_adc_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_rtc_obj = { - { &clkio_clkauxsrc_type }, -}; -const clkio_clkauxsrc_obj_t clkio_clkauxsrc_ref_obj = { - { &clkio_clkauxsrc_type }, -}; - - -STATIC const mp_rom_map_elem_t clkio_clkauxsrc_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_PLL_SYS), MP_ROM_PTR(&clkio_clkauxsrc_pll_sys_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPIN0), MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPIN1), MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj) }, - { MP_ROM_QSTR(MP_QSTR_PLL_USB), MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj) }, - { MP_ROM_QSTR(MP_QSTR_PLL_ROSC), MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj) }, - { MP_ROM_QSTR(MP_QSTR_PLL_XOSC), MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj) }, - { MP_ROM_QSTR(MP_QSTR_SYS), MP_ROM_PTR(&clkio_clkauxsrc_sys_obj) }, - { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&clkio_clkauxsrc_usb_obj) }, - { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&clkio_clkauxsrc_adc_obj) }, - { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj) }, - { MP_ROM_QSTR(MP_QSTR_REF), MP_ROM_PTR(&clkio_clkauxsrc_ref_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(clkio_clkauxsrc_locals_dict, clkio_clkauxsrc_locals_dict_table); - -STATIC void clkio_clkauxsrc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr clk = MP_QSTR_INVALID; - if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_sys_obj)) { - clk = MP_QSTR_PLL_SYS; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj)) { - clk = MP_QSTR_GPIN0; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj)) { - clk = MP_QSTR_GPIN1; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj)) { - clk = MP_QSTR_PLL_USB; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj)) { - clk = MP_QSTR_PLL_ROSC; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj)) { - clk = MP_QSTR_PLL_XOSC; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_sys_obj)) { - clk = MP_QSTR_SYS; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_usb_obj)) { - clk = MP_QSTR_USB; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_adc_obj)) { - clk = MP_QSTR_ADC; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj)) { - clk = MP_QSTR_RTC; - } else if (self_in == MP_ROM_PTR(&clkio_clkauxsrc_ref_obj)) { - clk = MP_QSTR_REF; - } - mp_printf(print, "%q.%q.%q", MP_QSTR_clkio, MP_QSTR_ClkAuxSrc, clk); -} - -MP_DEFINE_CONST_OBJ_TYPE( - clkio_clkauxsrc_type, - MP_QSTR_ClkAuxSrc, - MP_TYPE_FLAG_NONE, - print, clkio_clkauxsrc_print, - locals_dict, &clkio_clkauxsrc_locals_dict - ); - -mp_obj_t clkauxsrc_get_obj(clkio_clkauxsrc_t type) { - if (type == CLKAUXSRC_PLL_SYS) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_sys_obj); - } else if (type == CLKAUXSRC_GPIN0) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_gpin0_obj); - } else if (type == CLKAUXSRC_GPIN1) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_gpin1_obj); - } else if (type == CLKAUXSRC_PLL_USB) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_usb_obj); - } else if (type == CLKAUXSRC_PLL_ROSC) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_rosc_obj); - } else if (type == CLKAUXSRC_PLL_XOSC) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_pll_xosc_obj); - } else if (type == CLKAUXSRC_SYS) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_sys_obj); - } else if (type == CLKAUXSRC_USB) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_usb_obj); - } else if (type == CLKAUXSRC_ADC) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_adc_obj); - } else if (type == CLKAUXSRC_RTC) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_rtc_obj); - } else if (type == CLKAUXSRC_REF) { - return MP_OBJ_FROM_PTR(&clkio_clkauxsrc_ref_obj); - } else { - return MP_ROM_NONE; - } -} -clkio_clkauxsrc_t validate_clkauxsrc(mp_rom_obj_t obj, qstr arg_name) { - if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_sys_obj)) { - return CLKAUXSRC_PLL_SYS; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_gpin0_obj)) { - return CLKAUXSRC_GPIN0; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_gpin1_obj)) { - return CLKAUXSRC_GPIN1; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_usb_obj)) { - return CLKAUXSRC_PLL_USB; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_rosc_obj)) { - return CLKAUXSRC_PLL_ROSC; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_pll_xosc_obj)) { - return CLKAUXSRC_PLL_XOSC; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_sys_obj)) { - return CLKAUXSRC_SYS; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_usb_obj)) { - return CLKAUXSRC_USB; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_adc_obj)) { - return CLKAUXSRC_ADC; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_rtc_obj)) { - return CLKAUXSRC_RTC; - } else if (obj == MP_ROM_PTR(&clkio_clkauxsrc_ref_obj)) { - return CLKAUXSRC_REF; - } else if (obj == MP_ROM_NONE) { - return CLKAUXSRC_NONE; - } - mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_ClkAuxSrc, MP_QSTR_None, mp_obj_get_type(obj)->name); -} diff --git a/ports/raspberrypi/bindings/rp2clock/ClkIndex.c b/ports/raspberrypi/bindings/rp2clock/ClkIndex.c deleted file mode 100644 index f49618e64746..000000000000 --- a/ports/raspberrypi/bindings/rp2clock/ClkIndex.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2024 Elliot Buller - * - * 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 "py/runtime.h" -#include "supervisor/shared/translate/translate.h" -#include "bindings/rp2clock/ClkIndex.h" - -//| class ClkIndex: -//| """Defines the internal clock index to drive GPIN from an external pin.""" -//| -//| def __init__(self) -> None: -//| """Enum-like class to define the internal clock index.""" -//| -const mp_obj_type_t clkio_clkindex_type; - -const clkio_clkindex_obj_t clkio_clkindex_gpout0_obj = { - { &clkio_clkindex_type }, -}; -const clkio_clkindex_obj_t clkio_clkindex_gpout1_obj = { - { &clkio_clkindex_type }, -}; -const clkio_clkindex_obj_t clkio_clkindex_gpout2_obj = { - { &clkio_clkindex_type }, -}; -const clkio_clkindex_obj_t clkio_clkindex_gpout3_obj = { - { &clkio_clkindex_type }, -}; -const clkio_clkindex_obj_t clkio_clkindex_ref_obj = { - { &clkio_clkindex_type }, -}; -const clkio_clkindex_obj_t clkio_clkindex_sys_obj = { - { &clkio_clkindex_type }, -}; -const clkio_clkindex_obj_t clkio_clkindex_peri_obj = { - { &clkio_clkindex_type }, -}; -const clkio_clkindex_obj_t clkio_clkindex_usb_obj = { - { &clkio_clkindex_type }, -}; -const clkio_clkindex_obj_t clkio_clkindex_adc_obj = { - { &clkio_clkindex_type }, -}; -const clkio_clkindex_obj_t clkio_clkindex_rtc_obj = { - { &clkio_clkindex_type }, -}; - - -STATIC const mp_rom_map_elem_t clkio_clkindex_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_GPOUT0), MP_ROM_PTR(&clkio_clkindex_gpout0_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPOUT1), MP_ROM_PTR(&clkio_clkindex_gpout1_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPOUT2), MP_ROM_PTR(&clkio_clkindex_gpout2_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPOUT3), MP_ROM_PTR(&clkio_clkindex_gpout3_obj) }, - { MP_ROM_QSTR(MP_QSTR_REF), MP_ROM_PTR(&clkio_clkindex_ref_obj) }, - { MP_ROM_QSTR(MP_QSTR_SYS), MP_ROM_PTR(&clkio_clkindex_sys_obj) }, - { MP_ROM_QSTR(MP_QSTR_PERI), MP_ROM_PTR(&clkio_clkindex_peri_obj) }, - { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&clkio_clkindex_usb_obj) }, - { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&clkio_clkindex_adc_obj) }, - { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&clkio_clkindex_rtc_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(clkio_clkindex_locals_dict, clkio_clkindex_locals_dict_table); - -STATIC void clkio_clkindex_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr clk = MP_QSTR_INVALID; - if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout0_obj)) { - clk = MP_QSTR_GPOUT0; - } else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout1_obj)) { - clk = MP_QSTR_GPOUT1; - } else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout2_obj)) { - clk = MP_QSTR_GPOUT2; - } else if (self_in == MP_ROM_PTR(&clkio_clkindex_gpout3_obj)) { - clk = MP_QSTR_GPOUT3; - } else if (self_in == MP_ROM_PTR(&clkio_clkindex_ref_obj)) { - clk = MP_QSTR_REF; - } else if (self_in == MP_ROM_PTR(&clkio_clkindex_sys_obj)) { - clk = MP_QSTR_SYS; - } else if (self_in == MP_ROM_PTR(&clkio_clkindex_peri_obj)) { - clk = MP_QSTR_PERI; - } else if (self_in == MP_ROM_PTR(&clkio_clkindex_usb_obj)) { - clk = MP_QSTR_USB; - } else if (self_in == MP_ROM_PTR(&clkio_clkindex_adc_obj)) { - clk = MP_QSTR_ADC; - } else if (self_in == MP_ROM_PTR(&clkio_clkindex_rtc_obj)) { - clk = MP_QSTR_RTC; - } - mp_printf(print, "%q.%q.%q", MP_QSTR_clkio, MP_QSTR_ClkIndex, clk); -} - -MP_DEFINE_CONST_OBJ_TYPE( - clkio_clkindex_type, - MP_QSTR_ClkIndex, - MP_TYPE_FLAG_NONE, - print, clkio_clkindex_print, - locals_dict, &clkio_clkindex_locals_dict - ); - -mp_obj_t clkindex_get_obj(clkio_clkindex_t type) { - if (type == CLKINDEX_GPOUT0) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout0_obj); - } else if (type == CLKINDEX_GPOUT1) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout1_obj); - } else if (type == CLKINDEX_GPOUT2) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout2_obj); - } else if (type == CLKINDEX_GPOUT3) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_gpout3_obj); - } else if (type == CLKINDEX_REF) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_ref_obj); - } else if (type == CLKINDEX_SYS) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_sys_obj); - } else if (type == CLKINDEX_PERI) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_peri_obj); - } else if (type == CLKINDEX_USB) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_usb_obj); - } else if (type == CLKINDEX_ADC) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_adc_obj); - } else if (type == CLKINDEX_RTC) { - return MP_OBJ_FROM_PTR(&clkio_clkindex_rtc_obj); - } else { - return MP_ROM_NONE; - } -} - -clkio_clkindex_t validate_clkindex(mp_rom_obj_t obj, qstr arg_name) { - if (obj == MP_ROM_PTR(&clkio_clkindex_gpout0_obj)) { - return CLKINDEX_GPOUT0; - } else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout1_obj)) { - return CLKINDEX_GPOUT1; - } else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout2_obj)) { - return CLKINDEX_GPOUT2; - } else if (obj == MP_ROM_PTR(&clkio_clkindex_gpout3_obj)) { - return CLKINDEX_GPOUT3; - } else if (obj == MP_ROM_PTR(&clkio_clkindex_ref_obj)) { - return CLKINDEX_REF; - } else if (obj == MP_ROM_PTR(&clkio_clkindex_sys_obj)) { - return CLKINDEX_SYS; - } else if (obj == MP_ROM_PTR(&clkio_clkindex_peri_obj)) { - return CLKINDEX_PERI; - } else if (obj == MP_ROM_PTR(&clkio_clkindex_usb_obj)) { - return CLKINDEX_USB; - } else if (obj == MP_ROM_PTR(&clkio_clkindex_adc_obj)) { - return CLKINDEX_ADC; - } else if (obj == MP_ROM_PTR(&clkio_clkindex_rtc_obj)) { - return CLKINDEX_RTC; - } else if (obj == MP_ROM_NONE) { - return CLKINDEX_NONE; - } - mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_ClkIndex, MP_QSTR_None, mp_obj_get_type(obj)->name); -} diff --git a/ports/raspberrypi/bindings/rp2clock/ClkOutput.c b/ports/raspberrypi/bindings/rp2clock/ClkOutput.c deleted file mode 100644 index d4d4eccf3c86..000000000000 --- a/ports/raspberrypi/bindings/rp2clock/ClkOutput.c +++ /dev/null @@ -1,169 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2024 Elliot Buller - * - * 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 "py/obj.h" -#include "py/objproperty.h" -#include "py/runtime.h" -#include "py/objarray.h" - -#include "shared-bindings/util.h" -#include "bindings/rp2clock/ClkOutput.h" -#include "common-hal/rp2clock/ClkOutput.h" - -STATIC void check_for_deinit(clkio_clkoutput_obj_t *self) { - if (common_hal_clkio_clkoutput_deinited(self)) { - raise_deinited_error(); - } -} - -//| class ClkOutput: -//| def __init__( -//| self, pin: microcontroller.Pin, *, clksrc: clkio.ClkAuxSrc, divisor: float -//| ) -> None: -//| """Creates a clock output pip object. -//| pin: Pin to be used as clock input, allowed pins: 22,23,24,25 -//| clksrc: points to the source clock to be connected to the output pin. -//| divisor: Divisor for clock before it's driven onto pin. -//| """ -STATIC mp_obj_t clkio_clkoutput_make_new(const mp_obj_type_t *type, size_t n_args, - size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_pin, ARG_clksrc, ARG_divisor }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_clksrc, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, - { MP_QSTR_divisor, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} }, - }; - 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); - - clkio_clkoutput_obj_t *self = m_new_obj(clkio_clkoutput_obj_t); - self->base.type = &clkio_clkoutput_type; - - // Validate pin number - common_hal_clkio_clkoutput_validate_clksrc_pin(args[ARG_pin].u_rom_obj); - self->pin = args[ARG_pin].u_rom_obj; - self->divisor = common_hal_clkio_clkoutput_validate_divisor(mp_obj_get_float(args[ARG_divisor].u_obj)); - - // Validate pin based on clock - if (args[ARG_clksrc].u_rom_obj != mp_const_none) { - self->clksrc = validate_clkauxsrc(args[ARG_clksrc].u_rom_obj, MP_QSTR_clksrc); - } else { - self->clksrc = CLKAUXSRC_NONE; - } - self->enabled = false; - return MP_OBJ_FROM_PTR(self); -} - -//| def deinit(self) -> None: -//| """Releases the pin and frees any resources.""" -STATIC mp_obj_t clkio_clkoutput_deinit(mp_obj_t self_in) { - clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); - // Release pin - common_hal_clkio_clkoutput_deinit(self); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_deinit_obj, clkio_clkoutput_deinit); - -//| def enable(self) -> None: -//| """Configures the pin and enables the clock output""" -STATIC mp_obj_t clkio_clkoutput_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit(self); - common_hal_clkio_clkoutput_enable(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkoutput_enable_obj, 1, clkio_clkoutput_enable); - -//| def disable(self) -> None: -//| """Disableds the pin and external clock""" -STATIC mp_obj_t clkio_clkoutput_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit(self); - common_hal_clkio_clkoutput_disable(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkoutput_disable_obj, 1, clkio_clkoutput_disable); - -//| clksrc: clkio.ClkAuxSrc -//| """Auxiliary source clock to be driven to pin.""" -static mp_obj_t clkio_clkoutput_clksrc_get(mp_obj_t self_in) { - clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - return clkauxsrc_get_obj(self->clksrc); -} -MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_clksrc_get_obj, clkio_clkoutput_clksrc_get); - -static mp_obj_t clkio_clkoutput_clksrc_set(mp_obj_t self_in, mp_obj_t clksrc_obj) { - clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - self->clksrc = validate_clkauxsrc(clksrc_obj, MP_QSTR_clksrc); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(clkio_clkoutput_clksrc_set_obj, clkio_clkoutput_clksrc_set); -MP_PROPERTY_GETSET(clkio_clkoutput_clksrc_obj, - (mp_obj_t)&clkio_clkoutput_clksrc_get_obj, - (mp_obj_t)&clkio_clkoutput_clksrc_set_obj); - -//| divisor: float -//| """Divisor used to divide the clock before it's output to pin.""" -//| -static mp_obj_t clkio_clkoutput_divisor_get(mp_obj_t self_in) { - clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - return mp_obj_new_float(self->divisor); -} -MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkoutput_divisor_get_obj, clkio_clkoutput_divisor_get); - -static mp_obj_t clkio_clkoutput_divisor_set(mp_obj_t self_in, mp_obj_t divisor_obj) { - clkio_clkoutput_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - self->divisor = common_hal_clkio_clkoutput_validate_divisor(mp_obj_get_float(divisor_obj)); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(clkio_clkoutput_divisor_set_obj, clkio_clkoutput_divisor_set); -MP_PROPERTY_GETSET(clkio_clkoutput_divisor_obj, - (mp_obj_t)&clkio_clkoutput_divisor_get_obj, - (mp_obj_t)&clkio_clkoutput_divisor_set_obj); - - -STATIC const mp_rom_map_elem_t clkio_clkoutput_locals_dict_table[] = { - // Functions - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&clkio_clkoutput_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&clkio_clkoutput_enable_obj) }, - { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&clkio_clkoutput_disable_obj) }, - // Properties - { MP_ROM_QSTR(MP_QSTR_clksrc), MP_ROM_PTR(&clkio_clkoutput_clksrc_obj) }, - { MP_ROM_QSTR(MP_QSTR_divisor), MP_ROM_PTR(&clkio_clkoutput_divisor_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(clkio_clkoutput_locals_dict, clkio_clkoutput_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - clkio_clkoutput_type, - MP_QSTR_ClkOutput, - MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, - make_new, clkio_clkoutput_make_new, - locals_dict, &clkio_clkoutput_locals_dict - ); diff --git a/ports/raspberrypi/bindings/rp2clock/Index.c b/ports/raspberrypi/bindings/rp2clock/Index.c new file mode 100644 index 000000000000..c222f7050464 --- /dev/null +++ b/ports/raspberrypi/bindings/rp2clock/Index.c @@ -0,0 +1,170 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "py/runtime.h" +#include "supervisor/shared/translate/translate.h" +#include "bindings/rp2clock/Index.h" + +//| class Index: +//| """Defines the internal clock index to drive GPIN from an external pin.""" +//| +//| def __init__(self) -> None: +//| """Enum-like class to define the internal clock index.""" +//| +const mp_obj_type_t rp2clock_index_type; + +const rp2clock_index_obj_t rp2clock_index_gpout0_obj = { + { &rp2clock_index_type }, +}; +const rp2clock_index_obj_t rp2clock_index_gpout1_obj = { + { &rp2clock_index_type }, +}; +const rp2clock_index_obj_t rp2clock_index_gpout2_obj = { + { &rp2clock_index_type }, +}; +const rp2clock_index_obj_t rp2clock_index_gpout3_obj = { + { &rp2clock_index_type }, +}; +const rp2clock_index_obj_t rp2clock_index_ref_obj = { + { &rp2clock_index_type }, +}; +const rp2clock_index_obj_t rp2clock_index_sys_obj = { + { &rp2clock_index_type }, +}; +const rp2clock_index_obj_t rp2clock_index_peri_obj = { + { &rp2clock_index_type }, +}; +const rp2clock_index_obj_t rp2clock_index_usb_obj = { + { &rp2clock_index_type }, +}; +const rp2clock_index_obj_t rp2clock_index_adc_obj = { + { &rp2clock_index_type }, +}; +const rp2clock_index_obj_t rp2clock_index_rtc_obj = { + { &rp2clock_index_type }, +}; + + +STATIC const mp_rom_map_elem_t rp2clock_index_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_GPOUT0), MP_ROM_PTR(&rp2clock_index_gpout0_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPOUT1), MP_ROM_PTR(&rp2clock_index_gpout1_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPOUT2), MP_ROM_PTR(&rp2clock_index_gpout2_obj) }, + { MP_ROM_QSTR(MP_QSTR_GPOUT3), MP_ROM_PTR(&rp2clock_index_gpout3_obj) }, + { MP_ROM_QSTR(MP_QSTR_REF), MP_ROM_PTR(&rp2clock_index_ref_obj) }, + { MP_ROM_QSTR(MP_QSTR_SYS), MP_ROM_PTR(&rp2clock_index_sys_obj) }, + { MP_ROM_QSTR(MP_QSTR_PERI), MP_ROM_PTR(&rp2clock_index_peri_obj) }, + { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&rp2clock_index_usb_obj) }, + { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&rp2clock_index_adc_obj) }, + { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&rp2clock_index_rtc_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(rp2clock_index_locals_dict, rp2clock_index_locals_dict_table); + +STATIC void rp2clock_index_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + qstr clk = MP_QSTR_INVALID; + if (self_in == MP_ROM_PTR(&rp2clock_index_gpout0_obj)) { + clk = MP_QSTR_GPOUT0; + } else if (self_in == MP_ROM_PTR(&rp2clock_index_gpout1_obj)) { + clk = MP_QSTR_GPOUT1; + } else if (self_in == MP_ROM_PTR(&rp2clock_index_gpout2_obj)) { + clk = MP_QSTR_GPOUT2; + } else if (self_in == MP_ROM_PTR(&rp2clock_index_gpout3_obj)) { + clk = MP_QSTR_GPOUT3; + } else if (self_in == MP_ROM_PTR(&rp2clock_index_ref_obj)) { + clk = MP_QSTR_REF; + } else if (self_in == MP_ROM_PTR(&rp2clock_index_sys_obj)) { + clk = MP_QSTR_SYS; + } else if (self_in == MP_ROM_PTR(&rp2clock_index_peri_obj)) { + clk = MP_QSTR_PERI; + } else if (self_in == MP_ROM_PTR(&rp2clock_index_usb_obj)) { + clk = MP_QSTR_USB; + } else if (self_in == MP_ROM_PTR(&rp2clock_index_adc_obj)) { + clk = MP_QSTR_ADC; + } else if (self_in == MP_ROM_PTR(&rp2clock_index_rtc_obj)) { + clk = MP_QSTR_RTC; + } + mp_printf(print, "%q.%q.%q", MP_QSTR_rp2clock, MP_QSTR_Index, clk); +} + +MP_DEFINE_CONST_OBJ_TYPE( + rp2clock_index_type, + MP_QSTR_Index, + MP_TYPE_FLAG_NONE, + print, rp2clock_index_print, + locals_dict, &rp2clock_index_locals_dict + ); + +mp_obj_t index_get_obj(rp2clock_index_t type) { + if (type == INDEX_GPOUT0) { + return MP_OBJ_FROM_PTR(&rp2clock_index_gpout0_obj); + } else if (type == INDEX_GPOUT1) { + return MP_OBJ_FROM_PTR(&rp2clock_index_gpout1_obj); + } else if (type == INDEX_GPOUT2) { + return MP_OBJ_FROM_PTR(&rp2clock_index_gpout2_obj); + } else if (type == INDEX_GPOUT3) { + return MP_OBJ_FROM_PTR(&rp2clock_index_gpout3_obj); + } else if (type == INDEX_REF) { + return MP_OBJ_FROM_PTR(&rp2clock_index_ref_obj); + } else if (type == INDEX_SYS) { + return MP_OBJ_FROM_PTR(&rp2clock_index_sys_obj); + } else if (type == INDEX_PERI) { + return MP_OBJ_FROM_PTR(&rp2clock_index_peri_obj); + } else if (type == INDEX_USB) { + return MP_OBJ_FROM_PTR(&rp2clock_index_usb_obj); + } else if (type == INDEX_ADC) { + return MP_OBJ_FROM_PTR(&rp2clock_index_adc_obj); + } else if (type == INDEX_RTC) { + return MP_OBJ_FROM_PTR(&rp2clock_index_rtc_obj); + } else { + return MP_ROM_NONE; + } +} + +rp2clock_index_t validate_index(mp_rom_obj_t obj, qstr arg_name) { + if (obj == MP_ROM_PTR(&rp2clock_index_gpout0_obj)) { + return INDEX_GPOUT0; + } else if (obj == MP_ROM_PTR(&rp2clock_index_gpout1_obj)) { + return INDEX_GPOUT1; + } else if (obj == MP_ROM_PTR(&rp2clock_index_gpout2_obj)) { + return INDEX_GPOUT2; + } else if (obj == MP_ROM_PTR(&rp2clock_index_gpout3_obj)) { + return INDEX_GPOUT3; + } else if (obj == MP_ROM_PTR(&rp2clock_index_ref_obj)) { + return INDEX_REF; + } else if (obj == MP_ROM_PTR(&rp2clock_index_sys_obj)) { + return INDEX_SYS; + } else if (obj == MP_ROM_PTR(&rp2clock_index_peri_obj)) { + return INDEX_PERI; + } else if (obj == MP_ROM_PTR(&rp2clock_index_usb_obj)) { + return INDEX_USB; + } else if (obj == MP_ROM_PTR(&rp2clock_index_adc_obj)) { + return INDEX_ADC; + } else if (obj == MP_ROM_PTR(&rp2clock_index_rtc_obj)) { + return INDEX_RTC; + } else if (obj == MP_ROM_NONE) { + return INDEX_NONE; + } + mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_Index, MP_QSTR_None, mp_obj_get_type(obj)->name); +} diff --git a/ports/raspberrypi/bindings/rp2clock/ClkIndex.h b/ports/raspberrypi/bindings/rp2clock/Index.h similarity index 52% rename from ports/raspberrypi/bindings/rp2clock/ClkIndex.h rename to ports/raspberrypi/bindings/rp2clock/Index.h index 3edad726c8ab..e939c8c3d359 100644 --- a/ports/raspberrypi/bindings/rp2clock/ClkIndex.h +++ b/ports/raspberrypi/bindings/rp2clock/Index.h @@ -30,36 +30,36 @@ #include "hardware/clocks.h" // Input sources -typedef enum _clkio_clkindex_t { - CLKINDEX_GPOUT0 = clk_gpout0, - CLKINDEX_GPOUT1 = clk_gpout1, - CLKINDEX_GPOUT2 = clk_gpout2, - CLKINDEX_GPOUT3 = clk_gpout3, - CLKINDEX_REF = clk_ref, - CLKINDEX_SYS = clk_sys, - CLKINDEX_PERI = clk_peri, - CLKINDEX_USB = clk_usb, - CLKINDEX_ADC = clk_adc, - CLKINDEX_RTC = clk_rtc, - CLKINDEX_NONE = CLK_COUNT -} clkio_clkindex_t; +typedef enum _rp2clock_index_t { + INDEX_GPOUT0 = clk_gpout0, + INDEX_GPOUT1 = clk_gpout1, + INDEX_GPOUT2 = clk_gpout2, + INDEX_GPOUT3 = clk_gpout3, + INDEX_REF = clk_ref, + INDEX_SYS = clk_sys, + INDEX_PERI = clk_peri, + INDEX_USB = clk_usb, + INDEX_ADC = clk_adc, + INDEX_RTC = clk_rtc, + INDEX_NONE = CLK_COUNT +} rp2clock_index_t; -extern const mp_obj_type_t clkio_clkindex_type; +extern const mp_obj_type_t rp2clock_index_type; typedef struct { mp_obj_base_t base; -} clkio_clkindex_obj_t; +} rp2clock_index_obj_t; -extern const clkio_clkindex_obj_t clkio_clkindex_gpout0_obj; -extern const clkio_clkindex_obj_t clkio_clkindex_gpout1_obj; -extern const clkio_clkindex_obj_t clkio_clkindex_gpout2_obj; -extern const clkio_clkindex_obj_t clkio_clkindex_gpout3_obj; -extern const clkio_clkindex_obj_t clkio_clkindex_ref_obj; -extern const clkio_clkindex_obj_t clkio_clkindex_sys_obj; -extern const clkio_clkindex_obj_t clkio_clkindex_peri_obj; -extern const clkio_clkindex_obj_t clkio_clkindex_usb_obj; -extern const clkio_clkindex_obj_t clkio_clkindex_adc_obj; -extern const clkio_clkindex_obj_t clkio_clkindex_rtc_obj; +extern const rp2clock_index_obj_t rp2clock_index_gpout0_obj; +extern const rp2clock_index_obj_t rp2clock_index_gpout1_obj; +extern const rp2clock_index_obj_t rp2clock_index_gpout2_obj; +extern const rp2clock_index_obj_t rp2clock_index_gpout3_obj; +extern const rp2clock_index_obj_t rp2clock_index_ref_obj; +extern const rp2clock_index_obj_t rp2clock_index_sys_obj; +extern const rp2clock_index_obj_t rp2clock_index_peri_obj; +extern const rp2clock_index_obj_t rp2clock_index_usb_obj; +extern const rp2clock_index_obj_t rp2clock_index_adc_obj; +extern const rp2clock_index_obj_t rp2clock_index_rtc_obj; -clkio_clkindex_t validate_clkindex(mp_rom_obj_t obj, qstr arg_name); -mp_obj_t clkindex_get_obj(clkio_clkindex_t type); +rp2clock_index_t validate_index(mp_rom_obj_t obj, qstr arg_name); +mp_obj_t index_get_obj(rp2clock_index_t type); diff --git a/ports/raspberrypi/bindings/rp2clock/ClkInput.c b/ports/raspberrypi/bindings/rp2clock/InputPin.c similarity index 51% rename from ports/raspberrypi/bindings/rp2clock/ClkInput.c rename to ports/raspberrypi/bindings/rp2clock/InputPin.c index 6f8afacfca0a..70c19e239baa 100644 --- a/ports/raspberrypi/bindings/rp2clock/ClkInput.c +++ b/ports/raspberrypi/bindings/rp2clock/InputPin.c @@ -30,61 +30,61 @@ #include "py/objarray.h" #include "shared-bindings/util.h" -#include "bindings/rp2clock/ClkInput.h" -#include "common-hal/rp2clock/ClkInput.h" +#include "bindings/rp2clock/InputPin.h" +#include "common-hal/rp2clock/InputPin.h" -STATIC void check_for_deinit(clkio_clkinput_obj_t *self) { - if (common_hal_clkio_clkinput_deinited(self)) { +STATIC void check_for_deinit(rp2clock_inputpin_obj_t *self) { + if (common_hal_rp2clock_inputpin_deinited(self)) { raise_deinited_error(); } } -//| class ClkInput: +//| class InputPin: //| def __init__( //| self, //| pin: microcontroller.Pin, //| *, -//| clkindex: clkio.ClkIndex, +//| index: rp2clock.Index, //| src_freq: int, //| target_freq: int //| ) -> None: //| """Creates a clock input pin object. //| pin: Pin to be used as clock input, allowed pins: 20,22 -//| clkindex: points to the destination clock to be connected to the input pin. +//| index: points to the destination clock to be connected to the input pin. //| src_freq: External input frequency at the pin. -//| target_freq: Desired frequency for clkindex. +//| target_freq: Desired frequency for index. //| """ -STATIC mp_obj_t clkio_clkinput_make_new(const mp_obj_type_t *type, size_t n_args, +STATIC mp_obj_t rp2clock_inputpin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - enum { ARG_pin, ARG_clkindex, ARG_src_freq, ARG_target_freq }; + enum { ARG_pin, ARG_index, ARG_src_freq, ARG_target_freq }; static const mp_arg_t allowed_args[] = { { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_clkindex, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, + { MP_QSTR_index, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, { MP_QSTR_src_freq, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} }, { MP_QSTR_target_freq, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} }, }; 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); - clkio_clkinput_obj_t *self = m_new_obj(clkio_clkinput_obj_t); - self->base.type = &clkio_clkinput_type; + rp2clock_inputpin_obj_t *self = m_new_obj(rp2clock_inputpin_obj_t); + self->base.type = &rp2clock_inputpin_type; // Validate pin number - common_hal_clkio_clkinput_validate_clkindex_pin(args[ARG_pin].u_rom_obj); + common_hal_rp2clock_inputpin_validate_index_pin(args[ARG_pin].u_rom_obj); self->pin = args[ARG_pin].u_rom_obj; // Validate pin based on clock - if (args[ARG_clkindex].u_rom_obj != mp_const_none) { - self->clkindex = validate_clkindex(args[ARG_clkindex].u_rom_obj, MP_QSTR_clkindex); + if (args[ARG_index].u_rom_obj != mp_const_none) { + self->index = validate_index(args[ARG_index].u_rom_obj, MP_QSTR_index); self->src_freq = args[ARG_src_freq].u_int; self->target_freq = args[ARG_target_freq].u_int; // Validate frequencies if set if (self->src_freq && self->target_freq) { - common_hal_clkio_clkinput_validate_freqs(args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); + common_hal_rp2clock_inputpin_validate_freqs(args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); } } else { - self->clkindex = CLKINDEX_NONE; + self->index = INDEX_NONE; } self->enabled = false; return MP_OBJ_FROM_PTR(self); @@ -92,37 +92,37 @@ STATIC mp_obj_t clkio_clkinput_make_new(const mp_obj_type_t *type, size_t n_args //| def deinit(self) -> None: //| """Releases the pin and frees any resources.""" -STATIC mp_obj_t clkio_clkinput_deinit(mp_obj_t self_in) { - clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t rp2clock_inputpin_deinit(mp_obj_t self_in) { + rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); // Release pin - common_hal_clkio_clkinput_deinit(self); + common_hal_rp2clock_inputpin_deinit(self); return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkinput_deinit_obj, clkio_clkinput_deinit); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_inputpin_deinit_obj, rp2clock_inputpin_deinit); //| def enable(self) -> None: //| """Configures the pin and enables the internal clock""" -STATIC mp_obj_t clkio_clkinput_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); +STATIC mp_obj_t rp2clock_inputpin_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); - common_hal_clkio_clkinput_enable(self); + common_hal_rp2clock_inputpin_enable(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_enable_obj, 1, clkio_clkinput_enable); +MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_enable_obj, 1, rp2clock_inputpin_enable); //| def disable(self) -> None: //| """Disables the pin and internal clock""" -STATIC mp_obj_t clkio_clkinput_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); +STATIC mp_obj_t rp2clock_inputpin_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); - common_hal_clkio_clkinput_disable(self); + common_hal_rp2clock_inputpin_disable(self); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_disable_obj, 1, clkio_clkinput_disable); +MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_disable_obj, 1, rp2clock_inputpin_disable); //| def set_freq(self, src_freq: int, target_freq: int) -> None: //| """Configures the src and target frequency. Must be set before enable() is called.""" -STATIC mp_obj_t clkio_clkinput_set_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t rp2clock_inputpin_set_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_src_freq, ARG_target_freq }; static const mp_arg_t allowed_args[] = { { MP_QSTR_src_freq, MP_ARG_REQUIRED | MP_ARG_INT }, @@ -131,19 +131,19 @@ STATIC mp_obj_t clkio_clkinput_set_freq(size_t n_args, const mp_obj_t *pos_args, mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); - common_hal_clkio_clkinput_validate_freqs(args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); + common_hal_rp2clock_inputpin_validate_freqs(args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); self->src_freq = args[ARG_src_freq].u_int; self->target_freq = args[ARG_target_freq].u_int; return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_set_freq_obj, 1, clkio_clkinput_set_freq); +MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_set_freq_obj, 1, rp2clock_inputpin_set_freq); //| def get_freq(self) -> tuple[int, int]: //| """Returns the (src, target) frequency as tuple.""" -STATIC mp_obj_t clkio_clkinput_get_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); +STATIC mp_obj_t rp2clock_inputpin_get_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); mp_obj_t tup[] = { mp_obj_new_int(self->src_freq), @@ -152,46 +152,46 @@ STATIC mp_obj_t clkio_clkinput_get_freq(size_t n_args, const mp_obj_t *pos_args, // Return tuple with (src, target) return mp_obj_new_tuple(2, tup); } -MP_DEFINE_CONST_FUN_OBJ_KW(clkio_clkinput_get_freq_obj, 1, clkio_clkinput_get_freq); +MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_get_freq_obj, 1, rp2clock_inputpin_get_freq); -//| clkindex: clkio.ClkIndex +//| index: rp2clock.Index //| """Clock that will be driven from external pin.""" //| -static mp_obj_t clkio_clkinput_clkindex_get(mp_obj_t self_in) { - clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); +static mp_obj_t rp2clock_inputpin_index_get(mp_obj_t self_in) { + rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); - return clkindex_get_obj(self->clkindex); + return index_get_obj(self->index); } -MP_DEFINE_CONST_FUN_OBJ_1(clkio_clkinput_clkindex_get_obj, clkio_clkinput_clkindex_get); +MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_inputpin_index_get_obj, rp2clock_inputpin_index_get); -static mp_obj_t clkio_clkinput_clkindex_set(mp_obj_t self_in, mp_obj_t clkindex_obj) { - clkio_clkinput_obj_t *self = MP_OBJ_TO_PTR(self_in); +static mp_obj_t rp2clock_inputpin_index_set(mp_obj_t self_in, mp_obj_t index_obj) { + rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); - self->clkindex = validate_clkindex(clkindex_obj, MP_QSTR_clkindex); + self->index = validate_index(index_obj, MP_QSTR_index); return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(clkio_clkinput_clkindex_set_obj, clkio_clkinput_clkindex_set); -MP_PROPERTY_GETSET(clkio_clkinput_clkindex_obj, - (mp_obj_t)&clkio_clkinput_clkindex_get_obj, - (mp_obj_t)&clkio_clkinput_clkindex_set_obj); +MP_DEFINE_CONST_FUN_OBJ_2(rp2clock_inputpin_index_set_obj, rp2clock_inputpin_index_set); +MP_PROPERTY_GETSET(rp2clock_inputpin_index_obj, + (mp_obj_t)&rp2clock_inputpin_index_get_obj, + (mp_obj_t)&rp2clock_inputpin_index_set_obj); -STATIC const mp_rom_map_elem_t clkio_clkinput_locals_dict_table[] = { +STATIC const mp_rom_map_elem_t rp2clock_inputpin_locals_dict_table[] = { // Functions - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&clkio_clkinput_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&clkio_clkinput_enable_obj) }, - { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&clkio_clkinput_disable_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_freq), MP_ROM_PTR(&clkio_clkinput_set_freq_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_freq), MP_ROM_PTR(&clkio_clkinput_get_freq_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rp2clock_inputpin_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&rp2clock_inputpin_enable_obj) }, + { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&rp2clock_inputpin_disable_obj) }, + { MP_ROM_QSTR(MP_QSTR_set_freq), MP_ROM_PTR(&rp2clock_inputpin_set_freq_obj) }, + { MP_ROM_QSTR(MP_QSTR_get_freq), MP_ROM_PTR(&rp2clock_inputpin_get_freq_obj) }, // Properties - { MP_ROM_QSTR(MP_QSTR_clkindex), MP_ROM_PTR(&clkio_clkinput_clkindex_obj) }, + { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&rp2clock_inputpin_index_obj) }, }; -STATIC MP_DEFINE_CONST_DICT(clkio_clkinput_locals_dict, clkio_clkinput_locals_dict_table); +STATIC MP_DEFINE_CONST_DICT(rp2clock_inputpin_locals_dict, rp2clock_inputpin_locals_dict_table); MP_DEFINE_CONST_OBJ_TYPE( - clkio_clkinput_type, - MP_QSTR_ClkInput, + rp2clock_inputpin_type, + MP_QSTR_InputPin, MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, - make_new, clkio_clkinput_make_new, - locals_dict, &clkio_clkinput_locals_dict + make_new, rp2clock_inputpin_make_new, + locals_dict, &rp2clock_inputpin_locals_dict ); diff --git a/ports/raspberrypi/bindings/rp2clock/ClkInput.h b/ports/raspberrypi/bindings/rp2clock/InputPin.h similarity index 96% rename from ports/raspberrypi/bindings/rp2clock/ClkInput.h rename to ports/raspberrypi/bindings/rp2clock/InputPin.h index 3e69f15a4b16..5d1a5602855c 100644 --- a/ports/raspberrypi/bindings/rp2clock/ClkInput.h +++ b/ports/raspberrypi/bindings/rp2clock/InputPin.h @@ -28,4 +28,4 @@ #include "shared-bindings/microcontroller/Pin.h" -extern const mp_obj_type_t clkio_clkinput_type; +extern const mp_obj_type_t rp2clock_inputpin_type; diff --git a/ports/raspberrypi/bindings/rp2clock/OutputPin.c b/ports/raspberrypi/bindings/rp2clock/OutputPin.c new file mode 100644 index 000000000000..e0dacb70000f --- /dev/null +++ b/ports/raspberrypi/bindings/rp2clock/OutputPin.c @@ -0,0 +1,169 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Elliot Buller + * + * 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 "py/obj.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "py/objarray.h" + +#include "shared-bindings/util.h" +#include "bindings/rp2clock/OutputPin.h" +#include "common-hal/rp2clock/OutputPin.h" + +STATIC void check_for_deinit(rp2clock_outputpin_obj_t *self) { + if (common_hal_rp2clock_outputpin_deinited(self)) { + raise_deinited_error(); + } +} + +//| class OutputPin: +//| def __init__( +//| self, pin: microcontroller.Pin, *, src: rp2clock.AuxSrc, divisor: float +//| ) -> None: +//| """Creates a clock output pip object. +//| pin: Pin to be used as clock input, allowed pins: 22,23,24,25 +//| src: points to the source clock to be connected to the output pin. +//| divisor: Divisor for clock before it's driven onto pin. +//| """ +STATIC mp_obj_t rp2clock_outputpin_make_new(const mp_obj_type_t *type, size_t n_args, + size_t n_kw, const mp_obj_t *all_args) { + enum { ARG_pin, ARG_src, ARG_divisor }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_src, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, + { MP_QSTR_divisor, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} }, + }; + 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); + + rp2clock_outputpin_obj_t *self = m_new_obj(rp2clock_outputpin_obj_t); + self->base.type = &rp2clock_outputpin_type; + + // Validate pin number + common_hal_rp2clock_outputpin_validate_src_pin(args[ARG_pin].u_rom_obj); + self->pin = args[ARG_pin].u_rom_obj; + self->divisor = common_hal_rp2clock_outputpin_validate_divisor(mp_obj_get_float(args[ARG_divisor].u_obj)); + + // Validate pin based on clock + if (args[ARG_src].u_rom_obj != mp_const_none) { + self->src = validate_auxsrc(args[ARG_src].u_rom_obj, MP_QSTR_src); + } else { + self->src = AUXSRC_NONE; + } + self->enabled = false; + return MP_OBJ_FROM_PTR(self); +} + +//| def deinit(self) -> None: +//| """Releases the pin and frees any resources.""" +STATIC mp_obj_t rp2clock_outputpin_deinit(mp_obj_t self_in) { + rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); + // Release pin + common_hal_rp2clock_outputpin_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_outputpin_deinit_obj, rp2clock_outputpin_deinit); + +//| def enable(self) -> None: +//| """Configures the pin and enables the clock output""" +STATIC mp_obj_t rp2clock_outputpin_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit(self); + common_hal_rp2clock_outputpin_enable(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_outputpin_enable_obj, 1, rp2clock_outputpin_enable); + +//| def disable(self) -> None: +//| """Disableds the pin and external clock""" +STATIC mp_obj_t rp2clock_outputpin_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit(self); + common_hal_rp2clock_outputpin_disable(self); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_outputpin_disable_obj, 1, rp2clock_outputpin_disable); + +//| src: rp2clock.AuxSrc +//| """Auxiliary source clock to be driven to pin.""" +static mp_obj_t rp2clock_outputpin_src_get(mp_obj_t self_in) { + rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + return auxsrc_get_obj(self->src); +} +MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_outputpin_src_get_obj, rp2clock_outputpin_src_get); + +static mp_obj_t rp2clock_outputpin_src_set(mp_obj_t self_in, mp_obj_t src_obj) { + rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + self->src = validate_auxsrc(src_obj, MP_QSTR_src); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(rp2clock_outputpin_src_set_obj, rp2clock_outputpin_src_set); +MP_PROPERTY_GETSET(rp2clock_outputpin_src_obj, + (mp_obj_t)&rp2clock_outputpin_src_get_obj, + (mp_obj_t)&rp2clock_outputpin_src_set_obj); + +//| divisor: float +//| """Divisor used to divide the clock before it's output to pin.""" +//| +static mp_obj_t rp2clock_outputpin_divisor_get(mp_obj_t self_in) { + rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + return mp_obj_new_float(self->divisor); +} +MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_outputpin_divisor_get_obj, rp2clock_outputpin_divisor_get); + +static mp_obj_t rp2clock_outputpin_divisor_set(mp_obj_t self_in, mp_obj_t divisor_obj) { + rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + self->divisor = common_hal_rp2clock_outputpin_validate_divisor(mp_obj_get_float(divisor_obj)); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(rp2clock_outputpin_divisor_set_obj, rp2clock_outputpin_divisor_set); +MP_PROPERTY_GETSET(rp2clock_outputpin_divisor_obj, + (mp_obj_t)&rp2clock_outputpin_divisor_get_obj, + (mp_obj_t)&rp2clock_outputpin_divisor_set_obj); + + +STATIC const mp_rom_map_elem_t rp2clock_outputpin_locals_dict_table[] = { + // Functions + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rp2clock_outputpin_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&rp2clock_outputpin_enable_obj) }, + { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&rp2clock_outputpin_disable_obj) }, + // Properties + { MP_ROM_QSTR(MP_QSTR_src), MP_ROM_PTR(&rp2clock_outputpin_src_obj) }, + { MP_ROM_QSTR(MP_QSTR_divisor), MP_ROM_PTR(&rp2clock_outputpin_divisor_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(rp2clock_outputpin_locals_dict, rp2clock_outputpin_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + rp2clock_outputpin_type, + MP_QSTR_OutputPin, + MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, + make_new, rp2clock_outputpin_make_new, + locals_dict, &rp2clock_outputpin_locals_dict + ); diff --git a/ports/raspberrypi/bindings/rp2clock/ClkOutput.h b/ports/raspberrypi/bindings/rp2clock/OutputPin.h similarity index 96% rename from ports/raspberrypi/bindings/rp2clock/ClkOutput.h rename to ports/raspberrypi/bindings/rp2clock/OutputPin.h index 941cbb8f8627..f0d5b1121e8c 100644 --- a/ports/raspberrypi/bindings/rp2clock/ClkOutput.h +++ b/ports/raspberrypi/bindings/rp2clock/OutputPin.h @@ -28,4 +28,4 @@ #include "shared-bindings/microcontroller/Pin.h" -extern const mp_obj_type_t clkio_clkoutput_type; +extern const mp_obj_type_t rp2clock_outputpin_type; diff --git a/ports/raspberrypi/bindings/rp2clock/__init__.c b/ports/raspberrypi/bindings/rp2clock/__init__.c index 7c33b113f8c9..52491c3b25b8 100644 --- a/ports/raspberrypi/bindings/rp2clock/__init__.c +++ b/ports/raspberrypi/bindings/rp2clock/__init__.c @@ -30,26 +30,26 @@ #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Pin.h" #include "bindings/rp2clock/__init__.h" -#include "bindings/rp2clock/ClkAuxSrc.h" -#include "bindings/rp2clock/ClkIndex.h" -#include "bindings/rp2clock/ClkOutput.h" -#include "bindings/rp2clock/ClkInput.h" +#include "bindings/rp2clock/AuxSrc.h" +#include "bindings/rp2clock/Index.h" +#include "bindings/rp2clock/OutputPin.h" +#include "bindings/rp2clock/InputPin.h" -STATIC const mp_rom_map_elem_t clkio_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_clkio) }, - { MP_ROM_QSTR(MP_QSTR_ClkOutput), MP_ROM_PTR(&clkio_clkoutput_type) }, - { MP_ROM_QSTR(MP_QSTR_ClkInput), MP_ROM_PTR(&clkio_clkinput_type) }, +STATIC const mp_rom_map_elem_t rp2clock_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rp2clock) }, + { MP_ROM_QSTR(MP_QSTR_OutputPin), MP_ROM_PTR(&rp2clock_outputpin_type) }, + { MP_ROM_QSTR(MP_QSTR_InputPin), MP_ROM_PTR(&rp2clock_inputpin_type) }, // Enum like classes - { MP_ROM_QSTR(MP_QSTR_ClkAuxSrc), MP_ROM_PTR(&clkio_clkauxsrc_type) }, - { MP_ROM_QSTR(MP_QSTR_ClkIndex), MP_ROM_PTR(&clkio_clkindex_type) }, + { MP_ROM_QSTR(MP_QSTR_AuxSrc), MP_ROM_PTR(&rp2clock_auxsrc_type) }, + { MP_ROM_QSTR(MP_QSTR_Index), MP_ROM_PTR(&rp2clock_index_type) }, }; -STATIC MP_DEFINE_CONST_DICT(clkio_module_globals, clkio_module_globals_table); +STATIC MP_DEFINE_CONST_DICT(rp2clock_module_globals, rp2clock_module_globals_table); -const mp_obj_module_t clkio_module = { +const mp_obj_module_t rp2clock_module = { .base = { &mp_type_module }, - .globals = (mp_obj_dict_t *)&clkio_module_globals, + .globals = (mp_obj_dict_t *)&rp2clock_module_globals, }; -MP_REGISTER_MODULE(MP_QSTR_clkio, clkio_module); +MP_REGISTER_MODULE(MP_QSTR_rp2clock, rp2clock_module); diff --git a/ports/raspberrypi/common-hal/rp2clock/ClkInput.c b/ports/raspberrypi/common-hal/rp2clock/InputPin.c similarity index 74% rename from ports/raspberrypi/common-hal/rp2clock/ClkInput.c rename to ports/raspberrypi/common-hal/rp2clock/InputPin.c index f4bbd7d9c4d6..c428cb0cffe9 100644 --- a/ports/raspberrypi/common-hal/rp2clock/ClkInput.c +++ b/ports/raspberrypi/common-hal/rp2clock/InputPin.c @@ -24,20 +24,20 @@ * THE SOFTWARE. */ #include "shared-bindings/microcontroller/Pin.h" -#include "common-hal/rp2clock/ClkInput.h" -#include "bindings/rp2clock/ClkIndex.h" +#include "common-hal/rp2clock/InputPin.h" +#include "bindings/rp2clock/Index.h" #include "hardware/clocks.h" #include "hardware/gpio.h" #include "py/runtime.h" // GPIN must be [20,22] -void common_hal_clkio_clkinput_validate_clkindex_pin(const mcu_pin_obj_t *pin) { +void common_hal_rp2clock_inputpin_validate_index_pin(const mcu_pin_obj_t *pin) { if ((pin->number != 20) && (pin->number != 22)) { mp_raise_ValueError_varg(MP_ERROR_TEXT("Pin %d invalid, valid pins are: 20,22"), pin->number); } } -void common_hal_clkio_clkinput_validate_freqs(uint32_t src, uint32_t target) { +void common_hal_rp2clock_inputpin_validate_freqs(uint32_t src, uint32_t target) { if (src == 0) { mp_raise_ValueError(MP_ERROR_TEXT("src freq == 0")); } @@ -49,21 +49,21 @@ void common_hal_clkio_clkinput_validate_freqs(uint32_t src, uint32_t target) { } } -bool common_hal_clkio_clkinput_deinited(clkio_clkinput_obj_t *self) { +bool common_hal_rp2clock_inputpin_deinited(rp2clock_inputpin_obj_t *self) { return self->pin == NULL; } -void common_hal_clkio_clkinput_deinit(clkio_clkinput_obj_t *self) { - if (common_hal_clkio_clkinput_deinited(self)) { +void common_hal_rp2clock_inputpin_deinit(rp2clock_inputpin_obj_t *self) { + if (common_hal_rp2clock_inputpin_deinited(self)) { return; } if (self->enabled) { - common_hal_clkio_clkinput_disable(self); + common_hal_rp2clock_inputpin_disable(self); } self->pin = NULL; } -static void common_hal_clkio_clkinput_claim_pin(clkio_clkinput_obj_t *self) { +static void common_hal_rp2clock_inputpin_claim_pin(rp2clock_inputpin_obj_t *self) { // Avoid runtime error if enable already called if (self->enabled) { return; @@ -78,18 +78,18 @@ static void common_hal_clkio_clkinput_claim_pin(clkio_clkinput_obj_t *self) { self->enabled = true; } -void common_hal_clkio_clkinput_enable(clkio_clkinput_obj_t *self) { - if (self->clkindex == CLKINDEX_NONE) { - mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_clkindex); +void common_hal_rp2clock_inputpin_enable(rp2clock_inputpin_obj_t *self) { + if (self->index == INDEX_NONE) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_index); } - common_hal_clkio_clkinput_claim_pin(self); + common_hal_rp2clock_inputpin_claim_pin(self); // Check return value - if (!clock_configure_gpin(self->clkindex, self->pin->number, self->src_freq, self->target_freq)) { + if (!clock_configure_gpin(self->index, self->pin->number, self->src_freq, self->target_freq)) { mp_raise_RuntimeError(MP_ERROR_TEXT("clock_configure_gpin failed!")); } } -void common_hal_clkio_clkinput_disable(clkio_clkinput_obj_t *self) { +void common_hal_rp2clock_inputpin_disable(rp2clock_inputpin_obj_t *self) { if (!self->enabled) { return; } diff --git a/ports/raspberrypi/common-hal/rp2clock/ClkInput.h b/ports/raspberrypi/common-hal/rp2clock/InputPin.h similarity index 73% rename from ports/raspberrypi/common-hal/rp2clock/ClkInput.h rename to ports/raspberrypi/common-hal/rp2clock/InputPin.h index 02dd66b63fcd..7fd05c130da4 100644 --- a/ports/raspberrypi/common-hal/rp2clock/ClkInput.h +++ b/ports/raspberrypi/common-hal/rp2clock/InputPin.h @@ -26,23 +26,23 @@ #pragma once #include "common-hal/microcontroller/Pin.h" -#include "bindings/rp2clock/ClkIndex.h" +#include "bindings/rp2clock/Index.h" #include "hardware/clocks.h" typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; - clkio_clkindex_t clkindex; + rp2clock_index_t index; uint32_t src_freq; uint32_t target_freq; bool enabled; -} clkio_clkinput_obj_t; +} rp2clock_inputpin_obj_t; -void common_hal_clkio_clkinput_validate_clkindex_pin(const mcu_pin_obj_t *pin); -bool common_hal_clkio_clkinput_deinited(clkio_clkinput_obj_t *self); -void common_hal_clkio_clkinput_deinit(clkio_clkinput_obj_t *self); -void common_hal_clkio_clkinput_validate_freqs(uint32_t src, uint32_t target); +void common_hal_rp2clock_inputpin_validate_index_pin(const mcu_pin_obj_t *pin); +bool common_hal_rp2clock_inputpin_deinited(rp2clock_inputpin_obj_t *self); +void common_hal_rp2clock_inputpin_deinit(rp2clock_inputpin_obj_t *self); +void common_hal_rp2clock_inputpin_validate_freqs(uint32_t src, uint32_t target); // Configure clock in/out -void common_hal_clkio_clkinput_enable(clkio_clkinput_obj_t *self); -void common_hal_clkio_clkinput_disable(clkio_clkinput_obj_t *self); +void common_hal_rp2clock_inputpin_enable(rp2clock_inputpin_obj_t *self); +void common_hal_rp2clock_inputpin_disable(rp2clock_inputpin_obj_t *self); diff --git a/ports/raspberrypi/common-hal/rp2clock/ClkOutput.c b/ports/raspberrypi/common-hal/rp2clock/OutputPin.c similarity index 73% rename from ports/raspberrypi/common-hal/rp2clock/ClkOutput.c rename to ports/raspberrypi/common-hal/rp2clock/OutputPin.c index 50a9f97550a2..eb8ac8c42aaf 100644 --- a/ports/raspberrypi/common-hal/rp2clock/ClkOutput.c +++ b/ports/raspberrypi/common-hal/rp2clock/OutputPin.c @@ -24,22 +24,21 @@ * THE SOFTWARE. */ #include "shared-bindings/microcontroller/Pin.h" -#include "common-hal/rp2clock/ClkOutput.h" -#include "bindings/rp2clock/ClkAuxSrc.h" -#include "bindings/rp2clock/ClkIndex.h" +#include "common-hal/rp2clock/OutputPin.h" +#include "bindings/rp2clock/AuxSrc.h" #include "hardware/clocks.h" #include "hardware/gpio.h" #include "py/runtime.h" // GPOUT must be [21,23,24,25] -void common_hal_clkio_clkoutput_validate_clksrc_pin(const mcu_pin_obj_t *pin) { +void common_hal_rp2clock_outputpin_validate_src_pin(const mcu_pin_obj_t *pin) { if ((pin->number != 21) && (pin->number != 23) && (pin->number != 24) && (pin->number != 25)) { mp_raise_ValueError_varg(MP_ERROR_TEXT("Pin %d invalid, valid pins are: 21,23,24,25"), pin->number); } } -mp_float_t common_hal_clkio_clkoutput_validate_divisor(mp_float_t div) { +mp_float_t common_hal_rp2clock_outputpin_validate_divisor(mp_float_t div) { if (mp_obj_float_binary_op(MP_BINARY_OP_EQUAL, div, mp_obj_new_float(1.0f)) || (div >= 2.0f && div <= 16777215.0f)) { return div; @@ -48,21 +47,21 @@ mp_float_t common_hal_clkio_clkoutput_validate_divisor(mp_float_t div) { return 1.0f; } -bool common_hal_clkio_clkoutput_deinited(clkio_clkoutput_obj_t *self) { +bool common_hal_rp2clock_outputpin_deinited(rp2clock_outputpin_obj_t *self) { return self->pin == NULL; } -void common_hal_clkio_clkoutput_deinit(clkio_clkoutput_obj_t *self) { - if (common_hal_clkio_clkoutput_deinited(self)) { +void common_hal_rp2clock_outputpin_deinit(rp2clock_outputpin_obj_t *self) { + if (common_hal_rp2clock_outputpin_deinited(self)) { return; } if (self->enabled) { - common_hal_clkio_clkoutput_disable(self); + common_hal_rp2clock_outputpin_disable(self); } self->pin = NULL; } -static void common_hal_clkio_clkoutput_claim_pin(clkio_clkoutput_obj_t *self) { +static void common_hal_rp2clock_outputpin_claim_pin(rp2clock_outputpin_obj_t *self) { // Avoid runtime error if enable already called if (self->enabled) { return; @@ -77,15 +76,15 @@ static void common_hal_clkio_clkoutput_claim_pin(clkio_clkoutput_obj_t *self) { self->enabled = true; } -void common_hal_clkio_clkoutput_enable(clkio_clkoutput_obj_t *self) { - if (self->clksrc == CLKAUXSRC_NONE) { - mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_clksrc); +void common_hal_rp2clock_outputpin_enable(rp2clock_outputpin_obj_t *self) { + if (self->src == AUXSRC_NONE) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_src); } - common_hal_clkio_clkoutput_claim_pin(self); - clock_gpio_init(self->pin->number, self->clksrc, self->divisor); + common_hal_rp2clock_outputpin_claim_pin(self); + clock_gpio_init(self->pin->number, self->src, self->divisor); } -void common_hal_clkio_clkoutput_disable(clkio_clkoutput_obj_t *self) { +void common_hal_rp2clock_outputpin_disable(rp2clock_outputpin_obj_t *self) { if (!self->enabled) { return; } diff --git a/ports/raspberrypi/common-hal/rp2clock/ClkOutput.h b/ports/raspberrypi/common-hal/rp2clock/OutputPin.h similarity index 73% rename from ports/raspberrypi/common-hal/rp2clock/ClkOutput.h rename to ports/raspberrypi/common-hal/rp2clock/OutputPin.h index 1ede815996a0..d1e7bda6c195 100644 --- a/ports/raspberrypi/common-hal/rp2clock/ClkOutput.h +++ b/ports/raspberrypi/common-hal/rp2clock/OutputPin.h @@ -26,22 +26,22 @@ #pragma once #include "common-hal/microcontroller/Pin.h" -#include "bindings/rp2clock/ClkAuxSrc.h" +#include "bindings/rp2clock/AuxSrc.h" #include "hardware/clocks.h" typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; - clkio_clkauxsrc_t clksrc; + rp2clock_auxsrc_t src; mp_float_t divisor; bool enabled; -} clkio_clkoutput_obj_t; +} rp2clock_outputpin_obj_t; -void common_hal_clkio_clkoutput_validate_clksrc_pin(const mcu_pin_obj_t *pin); -bool common_hal_clkio_clkoutput_deinited(clkio_clkoutput_obj_t *self); -void common_hal_clkio_clkoutput_deinit(clkio_clkoutput_obj_t *self); -mp_float_t common_hal_clkio_clkoutput_validate_divisor(mp_float_t div); +void common_hal_rp2clock_outputpin_validate_src_pin(const mcu_pin_obj_t *pin); +bool common_hal_rp2clock_outputpin_deinited(rp2clock_outputpin_obj_t *self); +void common_hal_rp2clock_outputpin_deinit(rp2clock_outputpin_obj_t *self); +mp_float_t common_hal_rp2clock_outputpin_validate_divisor(mp_float_t div); // Configure clock out -void common_hal_clkio_clkoutput_enable(clkio_clkoutput_obj_t *self); -void common_hal_clkio_clkoutput_disable(clkio_clkoutput_obj_t *self); +void common_hal_rp2clock_outputpin_enable(rp2clock_outputpin_obj_t *self); +void common_hal_rp2clock_outputpin_disable(rp2clock_outputpin_obj_t *self); diff --git a/ports/raspberrypi/common-hal/rp2clock/__init__.c b/ports/raspberrypi/common-hal/rp2clock/__init__.c index fdb830ba8ae3..6af57cac3739 100644 --- a/ports/raspberrypi/common-hal/rp2clock/__init__.c +++ b/ports/raspberrypi/common-hal/rp2clock/__init__.c @@ -1 +1 @@ -// No clkio module functions +// No rp2clock module functions From a5dd74cec9f8f236617514ed236a498c6f033083 Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Fri, 22 Mar 2024 11:44:59 -0600 Subject: [PATCH 06/11] Update formatting --- ports/raspberrypi/bindings/rp2clock/OutputPin.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/raspberrypi/bindings/rp2clock/OutputPin.c b/ports/raspberrypi/bindings/rp2clock/OutputPin.c index e0dacb70000f..edcdde81b967 100644 --- a/ports/raspberrypi/bindings/rp2clock/OutputPin.c +++ b/ports/raspberrypi/bindings/rp2clock/OutputPin.c @@ -53,7 +53,7 @@ STATIC mp_obj_t rp2clock_outputpin_make_new(const mp_obj_type_t *type, size_t n_ enum { ARG_pin, ARG_src, ARG_divisor }; static const mp_arg_t allowed_args[] = { { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_src, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, + { MP_QSTR_src, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, { MP_QSTR_divisor, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; From ee36308a1de82ef4a852ec1a8b1b5706a8b46678 Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Fri, 22 Mar 2024 12:57:31 -0600 Subject: [PATCH 07/11] Rebuild translations --- locale/circuitpython.pot | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index b9c36f43120e..fe277a1d6228 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -206,9 +206,9 @@ msgstr "" msgid "%q must be multiple of 8." msgstr "" -#: ports/raspberrypi/bindings/clkio/ClkAuxSrc.c -#: ports/raspberrypi/bindings/clkio/ClkIndex.c -#: ports/raspberrypi/bindings/cyw43/__init__.c py/argcheck.c py/objexcept.c +#: ports/raspberrypi/bindings/cyw43/__init__.c +#: ports/raspberrypi/bindings/rp2clock/AuxSrc.c +#: ports/raspberrypi/bindings/rp2clock/Index.c py/argcheck.c py/objexcept.c #: shared-bindings/bitmapfilter/__init__.c shared-bindings/canio/CAN.c #: shared-bindings/digitalio/Pull.c shared-bindings/supervisor/__init__.c #: shared-module/synthio/Synthesizer.c @@ -228,8 +228,8 @@ msgstr "" msgid "%q must be power of 2" msgstr "" -#: ports/raspberrypi/common-hal/clkio/ClkInput.c -#: ports/raspberrypi/common-hal/clkio/ClkOutput.c +#: ports/raspberrypi/common-hal/rp2clock/InputPin.c +#: ports/raspberrypi/common-hal/rp2clock/OutputPin.c msgid "%q not set" msgstr "" @@ -1225,7 +1225,7 @@ msgstr "" msgid "Invalid data_pins[%d]" msgstr "" -#: ports/raspberrypi/common-hal/clkio/ClkOutput.c +#: ports/raspberrypi/common-hal/rp2clock/OutputPin.c msgid "Invalid divisor: [1, 2-16777215]" msgstr "" @@ -1237,7 +1237,7 @@ msgstr "" msgid "Invalid format chunk size" msgstr "" -#: ports/raspberrypi/common-hal/clkio/ClkInput.c +#: ports/raspberrypi/common-hal/rp2clock/InputPin.c #, c-format msgid "Invalid freqs: %u > %u" msgstr "" @@ -1669,12 +1669,12 @@ msgstr "" msgid "Permission denied" msgstr "" -#: ports/raspberrypi/common-hal/clkio/ClkInput.c +#: ports/raspberrypi/common-hal/rp2clock/InputPin.c #, c-format msgid "Pin %d invalid, valid pins are: 20,22" msgstr "" -#: ports/raspberrypi/common-hal/clkio/ClkOutput.c +#: ports/raspberrypi/common-hal/rp2clock/OutputPin.c #, c-format msgid "Pin %d invalid, valid pins are: 21,23,24,25" msgstr "" @@ -1687,8 +1687,8 @@ msgstr "" msgid "Pin count too large" msgstr "" -#: ports/raspberrypi/common-hal/clkio/ClkInput.c -#: ports/raspberrypi/common-hal/clkio/ClkOutput.c +#: ports/raspberrypi/common-hal/rp2clock/InputPin.c +#: ports/raspberrypi/common-hal/rp2clock/OutputPin.c msgid "Pin in use" msgstr "" @@ -2757,7 +2757,7 @@ msgstr "" msgid "clip point must be (x,y) tuple" msgstr "" -#: ports/raspberrypi/common-hal/clkio/ClkInput.c +#: ports/raspberrypi/common-hal/rp2clock/InputPin.c msgid "clock_configure_gpin failed!" msgstr "" @@ -3992,7 +3992,7 @@ msgstr "" msgid "splitting with sub-captures" msgstr "" -#: ports/raspberrypi/common-hal/clkio/ClkInput.c +#: ports/raspberrypi/common-hal/rp2clock/InputPin.c msgid "src freq == 0" msgstr "" @@ -4049,7 +4049,7 @@ msgstr "" msgid "syntax error in uctypes descriptor" msgstr "" -#: ports/raspberrypi/common-hal/clkio/ClkInput.c +#: ports/raspberrypi/common-hal/rp2clock/InputPin.c msgid "target freq == 0" msgstr "" From bb6a09e4d9ed5e3c9e407d26d71ea001eeafee77 Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Wed, 3 Apr 2024 12:09:41 -0600 Subject: [PATCH 08/11] Move to enum macro, make requested changes --- locale/circuitpython.pot | 27 +-- ports/raspberrypi/bindings/rp2clock/AuxSrc.c | 204 ++++++------------ ports/raspberrypi/bindings/rp2clock/AuxSrc.h | 45 ++-- ports/raspberrypi/bindings/rp2clock/Index.c | 190 +++++----------- ports/raspberrypi/bindings/rp2clock/Index.h | 21 +- .../raspberrypi/bindings/rp2clock/InputPin.c | 96 ++------- .../raspberrypi/bindings/rp2clock/OutputPin.c | 67 ++---- .../common-hal/rp2clock/InputPin.c | 20 +- .../common-hal/rp2clock/OutputPin.c | 14 +- 9 files changed, 189 insertions(+), 495 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index fe277a1d6228..18b4d2106577 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -206,9 +206,7 @@ msgstr "" msgid "%q must be multiple of 8." msgstr "" -#: ports/raspberrypi/bindings/cyw43/__init__.c -#: ports/raspberrypi/bindings/rp2clock/AuxSrc.c -#: ports/raspberrypi/bindings/rp2clock/Index.c py/argcheck.c py/objexcept.c +#: ports/raspberrypi/bindings/cyw43/__init__.c py/argcheck.c py/objexcept.c #: shared-bindings/bitmapfilter/__init__.c shared-bindings/canio/CAN.c #: shared-bindings/digitalio/Pull.c shared-bindings/supervisor/__init__.c #: shared-module/synthio/Synthesizer.c @@ -228,11 +226,6 @@ msgstr "" msgid "%q must be power of 2" msgstr "" -#: ports/raspberrypi/common-hal/rp2clock/InputPin.c -#: ports/raspberrypi/common-hal/rp2clock/OutputPin.c -msgid "%q not set" -msgstr "" - #: shared-bindings/wifi/Monitor.c msgid "%q out of bounds" msgstr "" @@ -1237,6 +1230,11 @@ msgstr "" msgid "Invalid format chunk size" msgstr "" +#: ports/raspberrypi/common-hal/rp2clock/InputPin.c +#, c-format +msgid "Invalid freq: %u" +msgstr "" + #: ports/raspberrypi/common-hal/rp2clock/InputPin.c #, c-format msgid "Invalid freqs: %u > %u" @@ -1687,11 +1685,6 @@ msgstr "" msgid "Pin count too large" msgstr "" -#: ports/raspberrypi/common-hal/rp2clock/InputPin.c -#: ports/raspberrypi/common-hal/rp2clock/OutputPin.c -msgid "Pin in use" -msgstr "" - #: ports/stm/common-hal/alarm/pin/PinAlarm.c #: ports/stm/common-hal/pulseio/PulseIn.c msgid "Pin interrupt already in use" @@ -3992,10 +3985,6 @@ msgstr "" msgid "splitting with sub-captures" msgstr "" -#: ports/raspberrypi/common-hal/rp2clock/InputPin.c -msgid "src freq == 0" -msgstr "" - #: py/objstr.c msgid "start/end indices" msgstr "" @@ -4049,10 +4038,6 @@ msgstr "" msgid "syntax error in uctypes descriptor" msgstr "" -#: ports/raspberrypi/common-hal/rp2clock/InputPin.c -msgid "target freq == 0" -msgstr "" - #: extmod/modtime.c msgid "ticks interval overflow" msgstr "" diff --git a/ports/raspberrypi/bindings/rp2clock/AuxSrc.c b/ports/raspberrypi/bindings/rp2clock/AuxSrc.c index 0dc4dce0beb6..0bf5dd41bf8b 100644 --- a/ports/raspberrypi/bindings/rp2clock/AuxSrc.c +++ b/ports/raspberrypi/bindings/rp2clock/AuxSrc.c @@ -25,155 +25,77 @@ */ #include "py/runtime.h" -#include "supervisor/shared/translate/translate.h" +#include "py/enum.h" #include "bindings/rp2clock/AuxSrc.h" //| class AuxSrc: -//| """Defines the input clock for GPOUT on RP2040""" +//| """Defines the input clock for GPOUT on RP2040. +//| Used with rp2clock.OutputPin instantiation. +//| """ //| -//| def __init__(self) -> None: +//| def __init__(self) -> AuxSrc: //| """Enum-like class to define the clock src.""" +//| PLL_SYS: "AuxSrc Clock" +//| """Undivided PLL used to derive SYS clock.""" +//| +//| GPIN0: "AuxSrc Clock" +//| """Input clock on GP20.""" +//| +//| GPIN1: "AuxSrc Clock" +//| """Input clock on GP22.""" +//| +//| PLL_USB: "AuxSrc Clock" +//| """Generates 48MHz USB reference clock.""" +//| +//| PLL_ROSC: "AuxSrc Clock" +//| """Ring oscillator clock. 1.8-12MHz on boot depending on PVT.""" +//| +//| PLL_XOSC: "AuxSrc Clock" +//| """External oscillator clock.""" +//| +//| SYS: "AuxSrc Clock" +//| """Derived system clock after SYS_PLL divider.""" +//| +//| USB: "AuxSrc Clock" +//| """Derived USB clock after PLL_USB divider, 48MHz.""" +//| +//| ADC: "AuxSrc Clock" +//| """Current ADC selected clock, 48MHz.""" +//| +//| RTC: "AuxSrc Clock" +//| """Current RTC selected clock.""" +//| +//| REF: "AuxSrc Clock" +//| """Current reference clock for watchdog and timers.""" //| const mp_obj_type_t rp2clock_auxsrc_type; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_sys_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_gpin0_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_gpin1_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_usb_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_rosc_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_xosc_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_sys_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_usb_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_adc_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_rtc_obj = { - { &rp2clock_auxsrc_type }, -}; -const rp2clock_auxsrc_obj_t rp2clock_auxsrc_ref_obj = { - { &rp2clock_auxsrc_type }, -}; - +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, PLL_SYS, AUXSRC_PLL_SYS); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, GPIN0, AUXSRC_GPIN0); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, GPIN1, AUXSRC_GPIN1); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, PLL_USB, AUXSRC_PLL_USB); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, PLL_ROSC, AUXSRC_PLL_ROSC); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, PLL_XOSC, AUXSRC_PLL_XOSC); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, SYS, AUXSRC_SYS); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, USB, AUXSRC_USB); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, ADC, AUXSRC_ADC); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, RTC, AUXSRC_RTC); +MAKE_ENUM_VALUE(rp2clock_auxsrc_type, rp2clock_auxsrc, REF, AUXSRC_REF); -STATIC const mp_rom_map_elem_t rp2clock_auxsrc_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_PLL_SYS), MP_ROM_PTR(&rp2clock_auxsrc_pll_sys_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPIN0), MP_ROM_PTR(&rp2clock_auxsrc_gpin0_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPIN1), MP_ROM_PTR(&rp2clock_auxsrc_gpin1_obj) }, - { MP_ROM_QSTR(MP_QSTR_PLL_USB), MP_ROM_PTR(&rp2clock_auxsrc_pll_usb_obj) }, - { MP_ROM_QSTR(MP_QSTR_PLL_ROSC), MP_ROM_PTR(&rp2clock_auxsrc_pll_rosc_obj) }, - { MP_ROM_QSTR(MP_QSTR_PLL_XOSC), MP_ROM_PTR(&rp2clock_auxsrc_pll_xosc_obj) }, - { MP_ROM_QSTR(MP_QSTR_SYS), MP_ROM_PTR(&rp2clock_auxsrc_sys_obj) }, - { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&rp2clock_auxsrc_usb_obj) }, - { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&rp2clock_auxsrc_adc_obj) }, - { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&rp2clock_auxsrc_rtc_obj) }, - { MP_ROM_QSTR(MP_QSTR_REF), MP_ROM_PTR(&rp2clock_auxsrc_ref_obj) }, +MAKE_ENUM_MAP(rp2clock_auxsrc) { + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, PLL_SYS), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, GPIN0), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, GPIN1), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, PLL_USB), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, PLL_ROSC), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, PLL_XOSC), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, SYS), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, USB), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, ADC), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, RTC), + MAKE_ENUM_MAP_ENTRY(rp2clock_auxsrc, REF), }; -STATIC MP_DEFINE_CONST_DICT(rp2clock_auxsrc_locals_dict, rp2clock_auxsrc_locals_dict_table); - -STATIC void rp2clock_auxsrc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr clk = MP_QSTR_INVALID; - if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_pll_sys_obj)) { - clk = MP_QSTR_PLL_SYS; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_gpin0_obj)) { - clk = MP_QSTR_GPIN0; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_gpin1_obj)) { - clk = MP_QSTR_GPIN1; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_pll_usb_obj)) { - clk = MP_QSTR_PLL_USB; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_pll_rosc_obj)) { - clk = MP_QSTR_PLL_ROSC; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_pll_xosc_obj)) { - clk = MP_QSTR_PLL_XOSC; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_sys_obj)) { - clk = MP_QSTR_SYS; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_usb_obj)) { - clk = MP_QSTR_USB; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_adc_obj)) { - clk = MP_QSTR_ADC; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_rtc_obj)) { - clk = MP_QSTR_RTC; - } else if (self_in == MP_ROM_PTR(&rp2clock_auxsrc_ref_obj)) { - clk = MP_QSTR_REF; - } - mp_printf(print, "%q.%q.%q", MP_QSTR_rp2clock, MP_QSTR_AuxSrc, clk); -} - -MP_DEFINE_CONST_OBJ_TYPE( - rp2clock_auxsrc_type, - MP_QSTR_AuxSrc, - MP_TYPE_FLAG_NONE, - print, rp2clock_auxsrc_print, - locals_dict, &rp2clock_auxsrc_locals_dict - ); -mp_obj_t auxsrc_get_obj(rp2clock_auxsrc_t type) { - if (type == AUXSRC_PLL_SYS) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_pll_sys_obj); - } else if (type == AUXSRC_GPIN0) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_gpin0_obj); - } else if (type == AUXSRC_GPIN1) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_gpin1_obj); - } else if (type == AUXSRC_PLL_USB) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_pll_usb_obj); - } else if (type == AUXSRC_PLL_ROSC) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_pll_rosc_obj); - } else if (type == AUXSRC_PLL_XOSC) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_pll_xosc_obj); - } else if (type == AUXSRC_SYS) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_sys_obj); - } else if (type == AUXSRC_USB) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_usb_obj); - } else if (type == AUXSRC_ADC) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_adc_obj); - } else if (type == AUXSRC_RTC) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_rtc_obj); - } else if (type == AUXSRC_REF) { - return MP_OBJ_FROM_PTR(&rp2clock_auxsrc_ref_obj); - } else { - return MP_ROM_NONE; - } -} -rp2clock_auxsrc_t validate_auxsrc(mp_rom_obj_t obj, qstr arg_name) { - if (obj == MP_ROM_PTR(&rp2clock_auxsrc_pll_sys_obj)) { - return AUXSRC_PLL_SYS; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_gpin0_obj)) { - return AUXSRC_GPIN0; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_gpin1_obj)) { - return AUXSRC_GPIN1; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_pll_usb_obj)) { - return AUXSRC_PLL_USB; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_pll_rosc_obj)) { - return AUXSRC_PLL_ROSC; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_pll_xosc_obj)) { - return AUXSRC_PLL_XOSC; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_sys_obj)) { - return AUXSRC_SYS; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_usb_obj)) { - return AUXSRC_USB; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_adc_obj)) { - return AUXSRC_ADC; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_rtc_obj)) { - return AUXSRC_RTC; - } else if (obj == MP_ROM_PTR(&rp2clock_auxsrc_ref_obj)) { - return AUXSRC_REF; - } else if (obj == MP_ROM_NONE) { - return AUXSRC_NONE; - } - mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_AuxSrc, MP_QSTR_None, mp_obj_get_type(obj)->name); -} +STATIC MP_DEFINE_CONST_DICT(rp2clock_auxsrc_locals_dict, rp2clock_auxsrc_locals_table); +MAKE_PRINTER(rp2clock, rp2clock_auxsrc); +MAKE_ENUM_TYPE(rp2clock, AuxSrc, rp2clock_auxsrc); diff --git a/ports/raspberrypi/bindings/rp2clock/AuxSrc.h b/ports/raspberrypi/bindings/rp2clock/AuxSrc.h index 2213fe4f5467..1e2a8ab4b02a 100644 --- a/ports/raspberrypi/bindings/rp2clock/AuxSrc.h +++ b/ports/raspberrypi/bindings/rp2clock/AuxSrc.h @@ -27,40 +27,21 @@ #pragma once #include "py/obj.h" +#include "hardware/regs/clocks.h" // Output sources -typedef enum _rp2clock_auxsrc_t { - AUXSRC_PLL_SYS = 0, - AUXSRC_GPIN0 = 1, - AUXSRC_GPIN1 = 2, - AUXSRC_PLL_USB = 3, - AUXSRC_PLL_ROSC = 4, - AUXSRC_PLL_XOSC = 5, - AUXSRC_SYS = 6, - AUXSRC_USB = 7, - AUXSRC_ADC = 8, - AUXSRC_RTC = 9, - AUXSRC_REF = 10, - AUXSRC_NONE = 11, +typedef enum { + AUXSRC_PLL_SYS = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS, + AUXSRC_GPIN0 = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, + AUXSRC_GPIN1 = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1, + AUXSRC_PLL_USB = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB, + AUXSRC_PLL_ROSC = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_ROSC_CLKSRC, + AUXSRC_PLL_XOSC = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_XOSC_CLKSRC, + AUXSRC_SYS = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLK_SYS, + AUXSRC_USB = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLK_USB, + AUXSRC_ADC = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLK_ADC, + AUXSRC_RTC = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLK_RTC, + AUXSRC_REF = CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLK_REF, } rp2clock_auxsrc_t; extern const mp_obj_type_t rp2clock_auxsrc_type; - -typedef struct { - mp_obj_base_t base; -} rp2clock_auxsrc_obj_t; - -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_sys_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_gpin0_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_gpin1_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_usb_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_rosc_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_pll_xosc_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_sys_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_usb_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_adc_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_rtc_obj; -extern const rp2clock_auxsrc_obj_t rp2clock_auxsrc_ref_obj; - -rp2clock_auxsrc_t validate_auxsrc(mp_rom_obj_t obj, qstr arg_name); -mp_obj_t auxsrc_get_obj(rp2clock_auxsrc_t type); diff --git a/ports/raspberrypi/bindings/rp2clock/Index.c b/ports/raspberrypi/bindings/rp2clock/Index.c index c222f7050464..8a7970775b7c 100644 --- a/ports/raspberrypi/bindings/rp2clock/Index.c +++ b/ports/raspberrypi/bindings/rp2clock/Index.c @@ -25,146 +25,72 @@ */ #include "py/runtime.h" -#include "supervisor/shared/translate/translate.h" +#include "py/enum.h" #include "bindings/rp2clock/Index.h" //| class Index: -//| """Defines the internal clock index to drive GPIN from an external pin.""" +//| """Defines the internal clock driven from GPIN external pin. +//| Used with rp2clock.InputPin instantiation. +//| """ //| -//| def __init__(self) -> None: +//| def __init__(self) -> Index: //| """Enum-like class to define the internal clock index.""" +//| GPOUT0: "Index Clock." +//| """Clock routed to GPOUT0 (GP21)""" +//| +//| GPOUT1: "Index Clock." +//| """Clock routed to GPOUT1 (GP23)""" +//| +//| GPOUT2: "Index Clock." +//| """Clock routed to GPOUT2 (GP24)""" +//| +//| GPOUT3: "Index Clock." +//| """Clock routed to GPOUT3 (GP25)""" +//| +//| REF: "Index Clock." +//| """Reference clock for watchdog and timers.""" +//| +//| SYS: "Index Clock." +//| """Main system clock for processors.""" +//| +//| PERI: "Index Clock." +//| """Peripheral clock: UART, SPI, etc. 12-125MHz""" +//| +//| USB: "Index Clock." +//| """USB clock: Must be 48MHz.""" +//| +//| ADC: "Index Clock." +//| """ADC clock: Must be 48MHz.""" +//| +//| RTC: "Index Clock." +//| """RTC clock: Nominally 46875 for 1 second ticks.""" //| const mp_obj_type_t rp2clock_index_type; -const rp2clock_index_obj_t rp2clock_index_gpout0_obj = { - { &rp2clock_index_type }, -}; -const rp2clock_index_obj_t rp2clock_index_gpout1_obj = { - { &rp2clock_index_type }, -}; -const rp2clock_index_obj_t rp2clock_index_gpout2_obj = { - { &rp2clock_index_type }, -}; -const rp2clock_index_obj_t rp2clock_index_gpout3_obj = { - { &rp2clock_index_type }, -}; -const rp2clock_index_obj_t rp2clock_index_ref_obj = { - { &rp2clock_index_type }, -}; -const rp2clock_index_obj_t rp2clock_index_sys_obj = { - { &rp2clock_index_type }, -}; -const rp2clock_index_obj_t rp2clock_index_peri_obj = { - { &rp2clock_index_type }, -}; -const rp2clock_index_obj_t rp2clock_index_usb_obj = { - { &rp2clock_index_type }, -}; -const rp2clock_index_obj_t rp2clock_index_adc_obj = { - { &rp2clock_index_type }, -}; -const rp2clock_index_obj_t rp2clock_index_rtc_obj = { - { &rp2clock_index_type }, -}; - +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, GPOUT0, INDEX_GPOUT0); +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, GPOUT1, INDEX_GPOUT1); +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, GPOUT2, INDEX_GPOUT2); +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, GPOUT3, INDEX_GPOUT3); +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, REF, INDEX_REF); +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, SYS, INDEX_SYS); +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, PERI, INDEX_PERI); +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, USB, INDEX_USB); +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, ADC, INDEX_ADC); +MAKE_ENUM_VALUE(rp2clock_index_type, rp2clock_index, RTC, INDEX_RTC); -STATIC const mp_rom_map_elem_t rp2clock_index_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_GPOUT0), MP_ROM_PTR(&rp2clock_index_gpout0_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPOUT1), MP_ROM_PTR(&rp2clock_index_gpout1_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPOUT2), MP_ROM_PTR(&rp2clock_index_gpout2_obj) }, - { MP_ROM_QSTR(MP_QSTR_GPOUT3), MP_ROM_PTR(&rp2clock_index_gpout3_obj) }, - { MP_ROM_QSTR(MP_QSTR_REF), MP_ROM_PTR(&rp2clock_index_ref_obj) }, - { MP_ROM_QSTR(MP_QSTR_SYS), MP_ROM_PTR(&rp2clock_index_sys_obj) }, - { MP_ROM_QSTR(MP_QSTR_PERI), MP_ROM_PTR(&rp2clock_index_peri_obj) }, - { MP_ROM_QSTR(MP_QSTR_USB), MP_ROM_PTR(&rp2clock_index_usb_obj) }, - { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&rp2clock_index_adc_obj) }, - { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&rp2clock_index_rtc_obj) }, +MAKE_ENUM_MAP(rp2clock_index) { + MAKE_ENUM_MAP_ENTRY(rp2clock_index, GPOUT0), + MAKE_ENUM_MAP_ENTRY(rp2clock_index, GPOUT1), + MAKE_ENUM_MAP_ENTRY(rp2clock_index, GPOUT2), + MAKE_ENUM_MAP_ENTRY(rp2clock_index, GPOUT3), + MAKE_ENUM_MAP_ENTRY(rp2clock_index, REF), + MAKE_ENUM_MAP_ENTRY(rp2clock_index, SYS), + MAKE_ENUM_MAP_ENTRY(rp2clock_index, PERI), + MAKE_ENUM_MAP_ENTRY(rp2clock_index, USB), + MAKE_ENUM_MAP_ENTRY(rp2clock_index, ADC), + MAKE_ENUM_MAP_ENTRY(rp2clock_index, RTC), }; -STATIC MP_DEFINE_CONST_DICT(rp2clock_index_locals_dict, rp2clock_index_locals_dict_table); - -STATIC void rp2clock_index_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr clk = MP_QSTR_INVALID; - if (self_in == MP_ROM_PTR(&rp2clock_index_gpout0_obj)) { - clk = MP_QSTR_GPOUT0; - } else if (self_in == MP_ROM_PTR(&rp2clock_index_gpout1_obj)) { - clk = MP_QSTR_GPOUT1; - } else if (self_in == MP_ROM_PTR(&rp2clock_index_gpout2_obj)) { - clk = MP_QSTR_GPOUT2; - } else if (self_in == MP_ROM_PTR(&rp2clock_index_gpout3_obj)) { - clk = MP_QSTR_GPOUT3; - } else if (self_in == MP_ROM_PTR(&rp2clock_index_ref_obj)) { - clk = MP_QSTR_REF; - } else if (self_in == MP_ROM_PTR(&rp2clock_index_sys_obj)) { - clk = MP_QSTR_SYS; - } else if (self_in == MP_ROM_PTR(&rp2clock_index_peri_obj)) { - clk = MP_QSTR_PERI; - } else if (self_in == MP_ROM_PTR(&rp2clock_index_usb_obj)) { - clk = MP_QSTR_USB; - } else if (self_in == MP_ROM_PTR(&rp2clock_index_adc_obj)) { - clk = MP_QSTR_ADC; - } else if (self_in == MP_ROM_PTR(&rp2clock_index_rtc_obj)) { - clk = MP_QSTR_RTC; - } - mp_printf(print, "%q.%q.%q", MP_QSTR_rp2clock, MP_QSTR_Index, clk); -} - -MP_DEFINE_CONST_OBJ_TYPE( - rp2clock_index_type, - MP_QSTR_Index, - MP_TYPE_FLAG_NONE, - print, rp2clock_index_print, - locals_dict, &rp2clock_index_locals_dict - ); - -mp_obj_t index_get_obj(rp2clock_index_t type) { - if (type == INDEX_GPOUT0) { - return MP_OBJ_FROM_PTR(&rp2clock_index_gpout0_obj); - } else if (type == INDEX_GPOUT1) { - return MP_OBJ_FROM_PTR(&rp2clock_index_gpout1_obj); - } else if (type == INDEX_GPOUT2) { - return MP_OBJ_FROM_PTR(&rp2clock_index_gpout2_obj); - } else if (type == INDEX_GPOUT3) { - return MP_OBJ_FROM_PTR(&rp2clock_index_gpout3_obj); - } else if (type == INDEX_REF) { - return MP_OBJ_FROM_PTR(&rp2clock_index_ref_obj); - } else if (type == INDEX_SYS) { - return MP_OBJ_FROM_PTR(&rp2clock_index_sys_obj); - } else if (type == INDEX_PERI) { - return MP_OBJ_FROM_PTR(&rp2clock_index_peri_obj); - } else if (type == INDEX_USB) { - return MP_OBJ_FROM_PTR(&rp2clock_index_usb_obj); - } else if (type == INDEX_ADC) { - return MP_OBJ_FROM_PTR(&rp2clock_index_adc_obj); - } else if (type == INDEX_RTC) { - return MP_OBJ_FROM_PTR(&rp2clock_index_rtc_obj); - } else { - return MP_ROM_NONE; - } -} -rp2clock_index_t validate_index(mp_rom_obj_t obj, qstr arg_name) { - if (obj == MP_ROM_PTR(&rp2clock_index_gpout0_obj)) { - return INDEX_GPOUT0; - } else if (obj == MP_ROM_PTR(&rp2clock_index_gpout1_obj)) { - return INDEX_GPOUT1; - } else if (obj == MP_ROM_PTR(&rp2clock_index_gpout2_obj)) { - return INDEX_GPOUT2; - } else if (obj == MP_ROM_PTR(&rp2clock_index_gpout3_obj)) { - return INDEX_GPOUT3; - } else if (obj == MP_ROM_PTR(&rp2clock_index_ref_obj)) { - return INDEX_REF; - } else if (obj == MP_ROM_PTR(&rp2clock_index_sys_obj)) { - return INDEX_SYS; - } else if (obj == MP_ROM_PTR(&rp2clock_index_peri_obj)) { - return INDEX_PERI; - } else if (obj == MP_ROM_PTR(&rp2clock_index_usb_obj)) { - return INDEX_USB; - } else if (obj == MP_ROM_PTR(&rp2clock_index_adc_obj)) { - return INDEX_ADC; - } else if (obj == MP_ROM_PTR(&rp2clock_index_rtc_obj)) { - return INDEX_RTC; - } else if (obj == MP_ROM_NONE) { - return INDEX_NONE; - } - mp_raise_TypeError_varg(MP_ERROR_TEXT("%q must be of type %q or %q, not %q"), arg_name, MP_QSTR_Index, MP_QSTR_None, mp_obj_get_type(obj)->name); -} +STATIC MP_DEFINE_CONST_DICT(rp2clock_index_locals_dict, rp2clock_index_locals_table); +MAKE_PRINTER(rp2clock, rp2clock_index); +MAKE_ENUM_TYPE(rp2clock, Index, rp2clock_index); diff --git a/ports/raspberrypi/bindings/rp2clock/Index.h b/ports/raspberrypi/bindings/rp2clock/Index.h index e939c8c3d359..5a9c25fd4a78 100644 --- a/ports/raspberrypi/bindings/rp2clock/Index.h +++ b/ports/raspberrypi/bindings/rp2clock/Index.h @@ -30,7 +30,7 @@ #include "hardware/clocks.h" // Input sources -typedef enum _rp2clock_index_t { +typedef enum { INDEX_GPOUT0 = clk_gpout0, INDEX_GPOUT1 = clk_gpout1, INDEX_GPOUT2 = clk_gpout2, @@ -41,25 +41,6 @@ typedef enum _rp2clock_index_t { INDEX_USB = clk_usb, INDEX_ADC = clk_adc, INDEX_RTC = clk_rtc, - INDEX_NONE = CLK_COUNT } rp2clock_index_t; extern const mp_obj_type_t rp2clock_index_type; - -typedef struct { - mp_obj_base_t base; -} rp2clock_index_obj_t; - -extern const rp2clock_index_obj_t rp2clock_index_gpout0_obj; -extern const rp2clock_index_obj_t rp2clock_index_gpout1_obj; -extern const rp2clock_index_obj_t rp2clock_index_gpout2_obj; -extern const rp2clock_index_obj_t rp2clock_index_gpout3_obj; -extern const rp2clock_index_obj_t rp2clock_index_ref_obj; -extern const rp2clock_index_obj_t rp2clock_index_sys_obj; -extern const rp2clock_index_obj_t rp2clock_index_peri_obj; -extern const rp2clock_index_obj_t rp2clock_index_usb_obj; -extern const rp2clock_index_obj_t rp2clock_index_adc_obj; -extern const rp2clock_index_obj_t rp2clock_index_rtc_obj; - -rp2clock_index_t validate_index(mp_rom_obj_t obj, qstr arg_name); -mp_obj_t index_get_obj(rp2clock_index_t type); diff --git a/ports/raspberrypi/bindings/rp2clock/InputPin.c b/ports/raspberrypi/bindings/rp2clock/InputPin.c index 70c19e239baa..f32698972216 100644 --- a/ports/raspberrypi/bindings/rp2clock/InputPin.c +++ b/ports/raspberrypi/bindings/rp2clock/InputPin.c @@ -25,6 +25,7 @@ */ #include "py/obj.h" +#include "py/enum.h" #include "py/objproperty.h" #include "py/runtime.h" #include "py/objarray.h" @@ -60,33 +61,30 @@ STATIC mp_obj_t rp2clock_inputpin_make_new(const mp_obj_type_t *type, size_t n_a enum { ARG_pin, ARG_index, ARG_src_freq, ARG_target_freq }; static const mp_arg_t allowed_args[] = { { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_index, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, - { MP_QSTR_src_freq, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} }, - { MP_QSTR_target_freq, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} }, + { MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_src_freq, MP_ARG_REQUIRED | MP_ARG_INT }, + { MP_QSTR_target_freq, MP_ARG_REQUIRED | MP_ARG_INT }, }; 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); - rp2clock_inputpin_obj_t *self = m_new_obj(rp2clock_inputpin_obj_t); + rp2clock_inputpin_obj_t *self = m_new_obj_with_finaliser(rp2clock_inputpin_obj_t); self->base.type = &rp2clock_inputpin_type; // Validate pin number common_hal_rp2clock_inputpin_validate_index_pin(args[ARG_pin].u_rom_obj); self->pin = args[ARG_pin].u_rom_obj; - // Validate pin based on clock - if (args[ARG_index].u_rom_obj != mp_const_none) { - self->index = validate_index(args[ARG_index].u_rom_obj, MP_QSTR_index); - self->src_freq = args[ARG_src_freq].u_int; - self->target_freq = args[ARG_target_freq].u_int; - // Validate frequencies if set - if (self->src_freq && self->target_freq) { - common_hal_rp2clock_inputpin_validate_freqs(args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); - } - } else { - self->index = INDEX_NONE; - } - self->enabled = false; + // Validate clock + self->index = cp_enum_value(&rp2clock_index_type, args[ARG_index].u_obj, MP_QSTR_rp2clock_index); + + // Validate frequencies + common_hal_rp2clock_inputpin_validate_freqs(args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); + self->src_freq = args[ARG_src_freq].u_int; + self->target_freq = args[ARG_target_freq].u_int; + + // Enable and return + common_hal_rp2clock_inputpin_enable(self); return MP_OBJ_FROM_PTR(self); }; @@ -112,6 +110,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_enable_obj, 1, rp2clock_inputpin_en //| def disable(self) -> None: //| """Disables the pin and internal clock""" +//| STATIC mp_obj_t rp2clock_inputpin_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); @@ -120,71 +119,22 @@ STATIC mp_obj_t rp2clock_inputpin_disable(size_t n_args, const mp_obj_t *pos_arg } MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_disable_obj, 1, rp2clock_inputpin_disable); -//| def set_freq(self, src_freq: int, target_freq: int) -> None: -//| """Configures the src and target frequency. Must be set before enable() is called.""" -STATIC mp_obj_t rp2clock_inputpin_set_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_src_freq, ARG_target_freq }; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_src_freq, MP_ARG_REQUIRED | MP_ARG_INT }, - { MP_QSTR_target_freq, MP_ARG_REQUIRED | MP_ARG_INT }, - }; - mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit(self); - common_hal_rp2clock_inputpin_validate_freqs(args[ARG_src_freq].u_int, args[ARG_target_freq].u_int); - self->src_freq = args[ARG_src_freq].u_int; - self->target_freq = args[ARG_target_freq].u_int; - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_set_freq_obj, 1, rp2clock_inputpin_set_freq); - -//| def get_freq(self) -> tuple[int, int]: -//| """Returns the (src, target) frequency as tuple.""" -STATIC mp_obj_t rp2clock_inputpin_get_freq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - check_for_deinit(self); - mp_obj_t tup[] = { - mp_obj_new_int(self->src_freq), - mp_obj_new_int(self->target_freq) - }; - // Return tuple with (src, target) - return mp_obj_new_tuple(2, tup); -} -MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_get_freq_obj, 1, rp2clock_inputpin_get_freq); - -//| index: rp2clock.Index -//| """Clock that will be driven from external pin.""" -//| -static mp_obj_t rp2clock_inputpin_index_get(mp_obj_t self_in) { - rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - return index_get_obj(self->index); -} -MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_inputpin_index_get_obj, rp2clock_inputpin_index_get); - -static mp_obj_t rp2clock_inputpin_index_set(mp_obj_t self_in, mp_obj_t index_obj) { +static mp_obj_t rp2clock_inputpin_get_enabled(mp_obj_t self_in) { rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); - self->index = validate_index(index_obj, MP_QSTR_index); - return mp_const_none; + return mp_obj_new_bool(self->enabled); } -MP_DEFINE_CONST_FUN_OBJ_2(rp2clock_inputpin_index_set_obj, rp2clock_inputpin_index_set); -MP_PROPERTY_GETSET(rp2clock_inputpin_index_obj, - (mp_obj_t)&rp2clock_inputpin_index_get_obj, - (mp_obj_t)&rp2clock_inputpin_index_set_obj); - +MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_inputpin_get_enabled_obj, rp2clock_inputpin_get_enabled); +MP_PROPERTY_GETTER(rp2clock_inputpin_enabled_obj, + (mp_obj_t)&rp2clock_inputpin_get_enabled_obj); STATIC const mp_rom_map_elem_t rp2clock_inputpin_locals_dict_table[] = { // Functions + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&rp2clock_inputpin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rp2clock_inputpin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&rp2clock_inputpin_enable_obj) }, { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&rp2clock_inputpin_disable_obj) }, - { MP_ROM_QSTR(MP_QSTR_set_freq), MP_ROM_PTR(&rp2clock_inputpin_set_freq_obj) }, - { MP_ROM_QSTR(MP_QSTR_get_freq), MP_ROM_PTR(&rp2clock_inputpin_get_freq_obj) }, - // Properties - { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&rp2clock_inputpin_index_obj) }, + { MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&rp2clock_inputpin_enabled_obj) }, }; STATIC MP_DEFINE_CONST_DICT(rp2clock_inputpin_locals_dict, rp2clock_inputpin_locals_dict_table); diff --git a/ports/raspberrypi/bindings/rp2clock/OutputPin.c b/ports/raspberrypi/bindings/rp2clock/OutputPin.c index edcdde81b967..03b76937f642 100644 --- a/ports/raspberrypi/bindings/rp2clock/OutputPin.c +++ b/ports/raspberrypi/bindings/rp2clock/OutputPin.c @@ -28,6 +28,7 @@ #include "py/objproperty.h" #include "py/runtime.h" #include "py/objarray.h" +#include "py/enum.h" #include "shared-bindings/util.h" #include "bindings/rp2clock/OutputPin.h" @@ -53,13 +54,13 @@ STATIC mp_obj_t rp2clock_outputpin_make_new(const mp_obj_type_t *type, size_t n_ enum { ARG_pin, ARG_src, ARG_divisor }; static const mp_arg_t allowed_args[] = { { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_src, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = mp_const_none} }, - { MP_QSTR_divisor, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} }, + { MP_QSTR_src, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_divisor, MP_ARG_REQUIRED | MP_ARG_OBJ }, }; 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); - rp2clock_outputpin_obj_t *self = m_new_obj(rp2clock_outputpin_obj_t); + rp2clock_outputpin_obj_t *self = m_new_obj_with_finaliser(rp2clock_outputpin_obj_t); self->base.type = &rp2clock_outputpin_type; // Validate pin number @@ -67,13 +68,11 @@ STATIC mp_obj_t rp2clock_outputpin_make_new(const mp_obj_type_t *type, size_t n_ self->pin = args[ARG_pin].u_rom_obj; self->divisor = common_hal_rp2clock_outputpin_validate_divisor(mp_obj_get_float(args[ARG_divisor].u_obj)); - // Validate pin based on clock - if (args[ARG_src].u_rom_obj != mp_const_none) { - self->src = validate_auxsrc(args[ARG_src].u_rom_obj, MP_QSTR_src); - } else { - self->src = AUXSRC_NONE; - } - self->enabled = false; + // Validate clock + self->src = cp_enum_value(&rp2clock_auxsrc_type, args[ARG_src].u_obj, MP_QSTR_rp2clock_auxsrc); + + // Enable pin + common_hal_rp2clock_outputpin_enable(self); return MP_OBJ_FROM_PTR(self); } @@ -107,56 +106,26 @@ STATIC mp_obj_t rp2clock_outputpin_disable(size_t n_args, const mp_obj_t *pos_ar } MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_outputpin_disable_obj, 1, rp2clock_outputpin_disable); -//| src: rp2clock.AuxSrc -//| """Auxiliary source clock to be driven to pin.""" -static mp_obj_t rp2clock_outputpin_src_get(mp_obj_t self_in) { - rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - return auxsrc_get_obj(self->src); -} -MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_outputpin_src_get_obj, rp2clock_outputpin_src_get); - -static mp_obj_t rp2clock_outputpin_src_set(mp_obj_t self_in, mp_obj_t src_obj) { - rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - self->src = validate_auxsrc(src_obj, MP_QSTR_src); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(rp2clock_outputpin_src_set_obj, rp2clock_outputpin_src_set); -MP_PROPERTY_GETSET(rp2clock_outputpin_src_obj, - (mp_obj_t)&rp2clock_outputpin_src_get_obj, - (mp_obj_t)&rp2clock_outputpin_src_set_obj); - -//| divisor: float -//| """Divisor used to divide the clock before it's output to pin.""" +//| enabled: bool +//| """Check if pin is enabled.""" //| -static mp_obj_t rp2clock_outputpin_divisor_get(mp_obj_t self_in) { +static mp_obj_t rp2clock_outputpin_get_enabled(mp_obj_t self_in) { rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); - return mp_obj_new_float(self->divisor); + return mp_obj_new_bool(self->enabled); } -MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_outputpin_divisor_get_obj, rp2clock_outputpin_divisor_get); - -static mp_obj_t rp2clock_outputpin_divisor_set(mp_obj_t self_in, mp_obj_t divisor_obj) { - rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - self->divisor = common_hal_rp2clock_outputpin_validate_divisor(mp_obj_get_float(divisor_obj)); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(rp2clock_outputpin_divisor_set_obj, rp2clock_outputpin_divisor_set); -MP_PROPERTY_GETSET(rp2clock_outputpin_divisor_obj, - (mp_obj_t)&rp2clock_outputpin_divisor_get_obj, - (mp_obj_t)&rp2clock_outputpin_divisor_set_obj); - +MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_outputpin_get_enabled_obj, rp2clock_outputpin_get_enabled); +MP_PROPERTY_GETTER(rp2clock_outputpin_enabled_obj, + (mp_obj_t)&rp2clock_outputpin_get_enabled_obj); STATIC const mp_rom_map_elem_t rp2clock_outputpin_locals_dict_table[] = { // Functions + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&rp2clock_outputpin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rp2clock_outputpin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&rp2clock_outputpin_enable_obj) }, { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&rp2clock_outputpin_disable_obj) }, // Properties - { MP_ROM_QSTR(MP_QSTR_src), MP_ROM_PTR(&rp2clock_outputpin_src_obj) }, - { MP_ROM_QSTR(MP_QSTR_divisor), MP_ROM_PTR(&rp2clock_outputpin_divisor_obj) }, + { MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&rp2clock_outputpin_enabled_obj) }, }; STATIC MP_DEFINE_CONST_DICT(rp2clock_outputpin_locals_dict, rp2clock_outputpin_locals_dict_table); diff --git a/ports/raspberrypi/common-hal/rp2clock/InputPin.c b/ports/raspberrypi/common-hal/rp2clock/InputPin.c index c428cb0cffe9..bc1035509af6 100644 --- a/ports/raspberrypi/common-hal/rp2clock/InputPin.c +++ b/ports/raspberrypi/common-hal/rp2clock/InputPin.c @@ -39,10 +39,10 @@ void common_hal_rp2clock_inputpin_validate_index_pin(const mcu_pin_obj_t *pin) { void common_hal_rp2clock_inputpin_validate_freqs(uint32_t src, uint32_t target) { if (src == 0) { - mp_raise_ValueError(MP_ERROR_TEXT("src freq == 0")); + mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid freq: %u"), src); } if (target == 0) { - mp_raise_ValueError(MP_ERROR_TEXT("target freq == 0")); + mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid freq: %u"), target); } if (target > src) { mp_raise_ValueError_varg(MP_ERROR_TEXT("Invalid freqs: %u > %u"), target, src); @@ -64,29 +64,19 @@ void common_hal_rp2clock_inputpin_deinit(rp2clock_inputpin_obj_t *self) { } static void common_hal_rp2clock_inputpin_claim_pin(rp2clock_inputpin_obj_t *self) { - // Avoid runtime error if enable already called - if (self->enabled) { - return; - } // Check pin is available - if (!common_hal_mcu_pin_is_free(self->pin)) { - mp_raise_RuntimeError(MP_ERROR_TEXT("Pin in use")); - } + assert_pin_free(self->pin); // Claim pin common_hal_mcu_pin_claim(self->pin); - // Store flag - self->enabled = true; } void common_hal_rp2clock_inputpin_enable(rp2clock_inputpin_obj_t *self) { - if (self->index == INDEX_NONE) { - mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_index); - } common_hal_rp2clock_inputpin_claim_pin(self); // Check return value if (!clock_configure_gpin(self->index, self->pin->number, self->src_freq, self->target_freq)) { mp_raise_RuntimeError(MP_ERROR_TEXT("clock_configure_gpin failed!")); } + self->enabled = true; } void common_hal_rp2clock_inputpin_disable(rp2clock_inputpin_obj_t *self) { @@ -94,5 +84,5 @@ void common_hal_rp2clock_inputpin_disable(rp2clock_inputpin_obj_t *self) { return; } common_hal_reset_pin(self->pin); - self->enabled = 0; + self->enabled = false; } diff --git a/ports/raspberrypi/common-hal/rp2clock/OutputPin.c b/ports/raspberrypi/common-hal/rp2clock/OutputPin.c index eb8ac8c42aaf..b7062122c2c5 100644 --- a/ports/raspberrypi/common-hal/rp2clock/OutputPin.c +++ b/ports/raspberrypi/common-hal/rp2clock/OutputPin.c @@ -62,26 +62,16 @@ void common_hal_rp2clock_outputpin_deinit(rp2clock_outputpin_obj_t *self) { } static void common_hal_rp2clock_outputpin_claim_pin(rp2clock_outputpin_obj_t *self) { - // Avoid runtime error if enable already called - if (self->enabled) { - return; - } // Check pin is available - if (!common_hal_mcu_pin_is_free(self->pin)) { - mp_raise_RuntimeError(MP_ERROR_TEXT("Pin in use")); - } + assert_pin_free(self->pin); // Claim pin common_hal_mcu_pin_claim(self->pin); - // Store flag - self->enabled = true; } void common_hal_rp2clock_outputpin_enable(rp2clock_outputpin_obj_t *self) { - if (self->src == AUXSRC_NONE) { - mp_raise_ValueError_varg(MP_ERROR_TEXT("%q not set"), MP_QSTR_src); - } common_hal_rp2clock_outputpin_claim_pin(self); clock_gpio_init(self->pin->number, self->src, self->divisor); + self->enabled = true; } void common_hal_rp2clock_outputpin_disable(rp2clock_outputpin_obj_t *self) { From 670df5c6537ac7d254bc73b1063afd771f7493b5 Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Wed, 3 Apr 2024 15:48:38 -0600 Subject: [PATCH 09/11] Fix docs --- ports/raspberrypi/bindings/rp2clock/AuxSrc.c | 28 ++++++++++---------- ports/raspberrypi/bindings/rp2clock/Index.c | 22 +++++++-------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/ports/raspberrypi/bindings/rp2clock/AuxSrc.c b/ports/raspberrypi/bindings/rp2clock/AuxSrc.c index 0bf5dd41bf8b..b4eead27f8f9 100644 --- a/ports/raspberrypi/bindings/rp2clock/AuxSrc.c +++ b/ports/raspberrypi/bindings/rp2clock/AuxSrc.c @@ -33,39 +33,39 @@ //| Used with rp2clock.OutputPin instantiation. //| """ //| -//| def __init__(self) -> AuxSrc: +//| def __init__(self) -> None: //| """Enum-like class to define the clock src.""" -//| PLL_SYS: "AuxSrc Clock" -//| """Undivided PLL used to derive SYS clock.""" +//| PLL_SYS: object +//| """PLL used to derive SYS clock.""" //| -//| GPIN0: "AuxSrc Clock" +//| GPIN0: object //| """Input clock on GP20.""" //| -//| GPIN1: "AuxSrc Clock" +//| GPIN1: object //| """Input clock on GP22.""" //| -//| PLL_USB: "AuxSrc Clock" +//| PLL_USB: object //| """Generates 48MHz USB reference clock.""" //| -//| PLL_ROSC: "AuxSrc Clock" +//| PLL_ROSC: object //| """Ring oscillator clock. 1.8-12MHz on boot depending on PVT.""" //| -//| PLL_XOSC: "AuxSrc Clock" +//| PLL_XOSC: object //| """External oscillator clock.""" //| -//| SYS: "AuxSrc Clock" -//| """Derived system clock after SYS_PLL divider.""" +//| SYS: object +//| """Derived system clock.""" //| -//| USB: "AuxSrc Clock" +//| USB: object //| """Derived USB clock after PLL_USB divider, 48MHz.""" //| -//| ADC: "AuxSrc Clock" +//| ADC: object //| """Current ADC selected clock, 48MHz.""" //| -//| RTC: "AuxSrc Clock" +//| RTC: object //| """Current RTC selected clock.""" //| -//| REF: "AuxSrc Clock" +//| REF: object //| """Current reference clock for watchdog and timers.""" //| const mp_obj_type_t rp2clock_auxsrc_type; diff --git a/ports/raspberrypi/bindings/rp2clock/Index.c b/ports/raspberrypi/bindings/rp2clock/Index.c index 8a7970775b7c..1678bd138900 100644 --- a/ports/raspberrypi/bindings/rp2clock/Index.c +++ b/ports/raspberrypi/bindings/rp2clock/Index.c @@ -33,36 +33,36 @@ //| Used with rp2clock.InputPin instantiation. //| """ //| -//| def __init__(self) -> Index: +//| def __init__(self) -> None: //| """Enum-like class to define the internal clock index.""" -//| GPOUT0: "Index Clock." +//| GPOUT0: object //| """Clock routed to GPOUT0 (GP21)""" //| -//| GPOUT1: "Index Clock." +//| GPOUT1: object //| """Clock routed to GPOUT1 (GP23)""" //| -//| GPOUT2: "Index Clock." +//| GPOUT2: object //| """Clock routed to GPOUT2 (GP24)""" //| -//| GPOUT3: "Index Clock." +//| GPOUT3: object //| """Clock routed to GPOUT3 (GP25)""" //| -//| REF: "Index Clock." +//| REF: object //| """Reference clock for watchdog and timers.""" //| -//| SYS: "Index Clock." +//| SYS: object //| """Main system clock for processors.""" //| -//| PERI: "Index Clock." +//| PERI: object //| """Peripheral clock: UART, SPI, etc. 12-125MHz""" //| -//| USB: "Index Clock." +//| USB: object //| """USB clock: Must be 48MHz.""" //| -//| ADC: "Index Clock." +//| ADC: object //| """ADC clock: Must be 48MHz.""" //| -//| RTC: "Index Clock." +//| RTC: object //| """RTC clock: Nominally 46875 for 1 second ticks.""" //| const mp_obj_type_t rp2clock_index_type; From d35cda7c5e576c5a1e1fb7af06178577b993f266 Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Wed, 3 Apr 2024 16:47:12 -0600 Subject: [PATCH 10/11] Update docs --- ports/raspberrypi/bindings/rp2clock/AuxSrc.c | 4 +-- ports/raspberrypi/bindings/rp2clock/Index.c | 4 +-- .../raspberrypi/bindings/rp2clock/InputPin.c | 31 ++++++++++--------- .../raspberrypi/bindings/rp2clock/OutputPin.c | 21 +++++++------ .../raspberrypi/bindings/rp2clock/__init__.c | 1 + 5 files changed, 30 insertions(+), 31 deletions(-) diff --git a/ports/raspberrypi/bindings/rp2clock/AuxSrc.c b/ports/raspberrypi/bindings/rp2clock/AuxSrc.c index b4eead27f8f9..701435d859aa 100644 --- a/ports/raspberrypi/bindings/rp2clock/AuxSrc.c +++ b/ports/raspberrypi/bindings/rp2clock/AuxSrc.c @@ -29,9 +29,7 @@ #include "bindings/rp2clock/AuxSrc.h" //| class AuxSrc: -//| """Defines the input clock for GPOUT on RP2040. -//| Used with rp2clock.OutputPin instantiation. -//| """ +//| """Defines the internal clock for GPOUT on RP2040.""" //| //| def __init__(self) -> None: //| """Enum-like class to define the clock src.""" diff --git a/ports/raspberrypi/bindings/rp2clock/Index.c b/ports/raspberrypi/bindings/rp2clock/Index.c index 1678bd138900..3e4ad606c376 100644 --- a/ports/raspberrypi/bindings/rp2clock/Index.c +++ b/ports/raspberrypi/bindings/rp2clock/Index.c @@ -29,9 +29,7 @@ #include "bindings/rp2clock/Index.h" //| class Index: -//| """Defines the internal clock driven from GPIN external pin. -//| Used with rp2clock.InputPin instantiation. -//| """ +//| """Defines the internal clock to be driven from GPIN external pin.""" //| //| def __init__(self) -> None: //| """Enum-like class to define the internal clock index.""" diff --git a/ports/raspberrypi/bindings/rp2clock/InputPin.c b/ports/raspberrypi/bindings/rp2clock/InputPin.c index f32698972216..93d470d176f6 100644 --- a/ports/raspberrypi/bindings/rp2clock/InputPin.c +++ b/ports/raspberrypi/bindings/rp2clock/InputPin.c @@ -41,21 +41,21 @@ STATIC void check_for_deinit(rp2clock_inputpin_obj_t *self) { } //| class InputPin: +//| """Route external clocks to internal clocks via dedicated pins.""" +//| //| def __init__( -//| self, -//| pin: microcontroller.Pin, -//| *, -//| index: rp2clock.Index, -//| src_freq: int, -//| target_freq: int +//| self, pin: microcontroller.Pin, index: rp2clock.Index, src_freq: int, target_freq: int //| ) -> None: //| """Creates a clock input pin object. -//| pin: Pin to be used as clock input, allowed pins: 20,22 -//| index: points to the destination clock to be connected to the input pin. -//| src_freq: External input frequency at the pin. -//| target_freq: Desired frequency for index. -//| """ - +//| +//| .. note:: Valid pins are: GP20, GP22. +//| :param ~microcontroller.Pin pin: Pin to be used as clock input. +//| +//| :param ~rp2clock.Index index: Destination clock to be connected to the input pin. +//| +//| :param int src_freq: Frequency of clock input at the pin. +//| +//| :param int target_freq: Desired frequency for ~rp2clock.Index clock.""" STATIC mp_obj_t rp2clock_inputpin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_index, ARG_src_freq, ARG_target_freq }; @@ -99,7 +99,7 @@ STATIC mp_obj_t rp2clock_inputpin_deinit(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_inputpin_deinit_obj, rp2clock_inputpin_deinit); //| def enable(self) -> None: -//| """Configures the pin and enables the internal clock""" +//| """Configures the pin and enables the internal clock.""" STATIC mp_obj_t rp2clock_inputpin_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); @@ -110,7 +110,6 @@ MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_enable_obj, 1, rp2clock_inputpin_en //| def disable(self) -> None: //| """Disables the pin and internal clock""" -//| STATIC mp_obj_t rp2clock_inputpin_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); @@ -119,6 +118,9 @@ STATIC mp_obj_t rp2clock_inputpin_disable(size_t n_args, const mp_obj_t *pos_arg } MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_inputpin_disable_obj, 1, rp2clock_inputpin_disable); +//| def enabled(self) -> bool: +//| """Check if pin is enabled.""" +//| static mp_obj_t rp2clock_inputpin_get_enabled(mp_obj_t self_in) { rp2clock_inputpin_obj_t *self = MP_OBJ_TO_PTR(self_in); check_for_deinit(self); @@ -129,7 +131,6 @@ MP_PROPERTY_GETTER(rp2clock_inputpin_enabled_obj, (mp_obj_t)&rp2clock_inputpin_get_enabled_obj); STATIC const mp_rom_map_elem_t rp2clock_inputpin_locals_dict_table[] = { - // Functions { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&rp2clock_inputpin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rp2clock_inputpin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&rp2clock_inputpin_enable_obj) }, diff --git a/ports/raspberrypi/bindings/rp2clock/OutputPin.c b/ports/raspberrypi/bindings/rp2clock/OutputPin.c index 03b76937f642..88c44ecd02f4 100644 --- a/ports/raspberrypi/bindings/rp2clock/OutputPin.c +++ b/ports/raspberrypi/bindings/rp2clock/OutputPin.c @@ -41,14 +41,15 @@ STATIC void check_for_deinit(rp2clock_outputpin_obj_t *self) { } //| class OutputPin: -//| def __init__( -//| self, pin: microcontroller.Pin, *, src: rp2clock.AuxSrc, divisor: float -//| ) -> None: -//| """Creates a clock output pip object. -//| pin: Pin to be used as clock input, allowed pins: 22,23,24,25 -//| src: points to the source clock to be connected to the output pin. -//| divisor: Divisor for clock before it's driven onto pin. -//| """ +//| def __init__(self, pin: microcontroller.Pin, src: rp2clock.AuxSrc, divisor: float) -> None: +//| """Creates a clock output pin object. +//| +//| .. note:: Valid pins are: GP21, GP23, GP24, GP25. +//| :param ~microcontroller.Pin pin: Pin to be used as clock output. +//| +//| :param ~rp2clock.AuxSrc src: Source clock to be connected to the output pin. +//| +//| :param float divisor: Divisor for clock before it's driven onto pin.""" STATIC mp_obj_t rp2clock_outputpin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_pin, ARG_src, ARG_divisor }; @@ -87,7 +88,7 @@ STATIC mp_obj_t rp2clock_outputpin_deinit(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(rp2clock_outputpin_deinit_obj, rp2clock_outputpin_deinit); //| def enable(self) -> None: -//| """Configures the pin and enables the clock output""" +//| """Configures the pin and enables the clock output.""" STATIC mp_obj_t rp2clock_outputpin_enable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); @@ -97,7 +98,7 @@ STATIC mp_obj_t rp2clock_outputpin_enable(size_t n_args, const mp_obj_t *pos_arg MP_DEFINE_CONST_FUN_OBJ_KW(rp2clock_outputpin_enable_obj, 1, rp2clock_outputpin_enable); //| def disable(self) -> None: -//| """Disableds the pin and external clock""" +//| """Disables the pin and external clock.""" STATIC mp_obj_t rp2clock_outputpin_disable(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { rp2clock_outputpin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); check_for_deinit(self); diff --git a/ports/raspberrypi/bindings/rp2clock/__init__.c b/ports/raspberrypi/bindings/rp2clock/__init__.c index 52491c3b25b8..f65406fb6061 100644 --- a/ports/raspberrypi/bindings/rp2clock/__init__.c +++ b/ports/raspberrypi/bindings/rp2clock/__init__.c @@ -35,6 +35,7 @@ #include "bindings/rp2clock/OutputPin.h" #include "bindings/rp2clock/InputPin.h" +//| """Hardware interface for RP2040 clock control""" STATIC const mp_rom_map_elem_t rp2clock_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_rp2clock) }, { MP_ROM_QSTR(MP_QSTR_OutputPin), MP_ROM_PTR(&rp2clock_outputpin_type) }, From 852814b1986e589bc7d00c038c9ef89b480cf612 Mon Sep 17 00:00:00 2001 From: Tiny Labs Inc Date: Wed, 3 Apr 2024 18:11:02 -0600 Subject: [PATCH 11/11] Fix doc formatting --- ports/raspberrypi/bindings/rp2clock/InputPin.c | 1 + ports/raspberrypi/bindings/rp2clock/OutputPin.c | 1 + 2 files changed, 2 insertions(+) diff --git a/ports/raspberrypi/bindings/rp2clock/InputPin.c b/ports/raspberrypi/bindings/rp2clock/InputPin.c index 93d470d176f6..5932ca683f19 100644 --- a/ports/raspberrypi/bindings/rp2clock/InputPin.c +++ b/ports/raspberrypi/bindings/rp2clock/InputPin.c @@ -49,6 +49,7 @@ STATIC void check_for_deinit(rp2clock_inputpin_obj_t *self) { //| """Creates a clock input pin object. //| //| .. note:: Valid pins are: GP20, GP22. +//| //| :param ~microcontroller.Pin pin: Pin to be used as clock input. //| //| :param ~rp2clock.Index index: Destination clock to be connected to the input pin. diff --git a/ports/raspberrypi/bindings/rp2clock/OutputPin.c b/ports/raspberrypi/bindings/rp2clock/OutputPin.c index 88c44ecd02f4..f9f7af58401e 100644 --- a/ports/raspberrypi/bindings/rp2clock/OutputPin.c +++ b/ports/raspberrypi/bindings/rp2clock/OutputPin.c @@ -45,6 +45,7 @@ STATIC void check_for_deinit(rp2clock_outputpin_obj_t *self) { //| """Creates a clock output pin object. //| //| .. note:: Valid pins are: GP21, GP23, GP24, GP25. +//| //| :param ~microcontroller.Pin pin: Pin to be used as clock output. //| //| :param ~rp2clock.AuxSrc src: Source clock to be connected to the output pin.