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 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 44 additions & 5 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.

Expand Up @@ -206,7 +206,9 @@ msgstr ""
msgid "%q must be multiple of 8."
msgstr ""

#: 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
Expand All @@ -226,6 +228,11 @@ 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 ""
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/rp2clock/OutputPin.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/rp2clock/InputPin.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/rp2clock/InputPin.c
#, c-format
msgid "Pin %d invalid, valid pins are: 20,22"
msgstr ""

#: ports/raspberrypi/common-hal/rp2clock/OutputPin.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/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"
Expand Down Expand Up @@ -2730,6 +2757,10 @@ msgstr ""
msgid "clip point must be (x,y) tuple"
msgstr ""

#: ports/raspberrypi/common-hal/rp2clock/InputPin.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/rp2clock/InputPin.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/rp2clock/InputPin.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
Expand Up @@ -259,6 +259,17 @@ SRC_C += \
$(SRC_CYW43) \
$(SRC_LWIP) \

ifeq ($(CIRCUITPY_RP2CLOCK),1)
SRC_C += \
bindings/rp2clock/__init__.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

ifeq ($(CIRCUITPY_USB_HOST), 1)
SRC_C += \
Expand Down
179 changes: 179 additions & 0 deletions 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) },
};
tannewt marked this conversation as resolved.
Show resolved Hide resolved
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);
}
66 changes: 66 additions & 0 deletions ports/raspberrypi/bindings/rp2clock/AuxSrc.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 _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 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);