Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RP2040 GPIN/GPOUT (clkio) #9009

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions locale/circuitpython.pot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to reuse messages in this file instead of adding new ones. That way there is less to translate and less to store in the firmware.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried my best to combine them on latest commits.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, do all translations end up in every build regardless of the modules compiled in? I just assumed that these would only be pulled in when CIRCUITPY_RP2CLOCK=1. I can make the messages more generic to reuse them but we'd lose some information.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only translations for enabled modules are included. I assume we'll have RP2CLOCK enabled for all RP2040 boards where it'll fit. Striking the generic vs specific balance can be tricky.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ""
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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 ""
Expand All @@ -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 ""
Expand Down Expand Up @@ -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 ""
Expand All @@ -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"
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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 ""
Expand Down
11 changes: 11 additions & 0 deletions ports/raspberrypi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 += \
Expand Down
179 changes: 179 additions & 0 deletions ports/raspberrypi/bindings/clkio/ClkAuxSrc.c
Original file line number Diff line number Diff line change
@@ -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/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);
}
66 changes: 66 additions & 0 deletions ports/raspberrypi/bindings/clkio/ClkAuxSrc.h
Original file line number Diff line number Diff line change
@@ -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);
Loading