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 all 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
32 changes: 28 additions & 4 deletions locale/circuitpython.pot
Expand Up @@ -912,10 +912,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 +1218,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 +1230,16 @@ 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"
msgstr ""

#: shared-bindings/wifi/Radio.c
msgid "Invalid hex password"
msgstr ""
Expand Down Expand Up @@ -1657,6 +1667,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 Down Expand Up @@ -2730,6 +2750,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
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
99 changes: 99 additions & 0 deletions ports/raspberrypi/bindings/rp2clock/AuxSrc.c
@@ -0,0 +1,99 @@
/*
* 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 "py/enum.h"
#include "bindings/rp2clock/AuxSrc.h"

//| class AuxSrc:
//| """Defines the internal clock for GPOUT on RP2040."""
//|
//| def __init__(self) -> None:
//| """Enum-like class to define the clock src."""
//| PLL_SYS: object
//| """PLL used to derive SYS clock."""
//|
//| GPIN0: object
//| """Input clock on GP20."""
//|
//| GPIN1: object
//| """Input clock on GP22."""
//|
//| PLL_USB: object
//| """Generates 48MHz USB reference clock."""
//|
//| PLL_ROSC: object
//| """Ring oscillator clock. 1.8-12MHz on boot depending on PVT."""
//|
//| PLL_XOSC: object
//| """External oscillator clock."""
//|
//| SYS: object
//| """Derived system clock."""
//|
//| USB: object
//| """Derived USB clock after PLL_USB divider, 48MHz."""
//|
//| ADC: object
//| """Current ADC selected clock, 48MHz."""
//|
//| RTC: object
//| """Current RTC selected clock."""
//|
//| REF: object
//| """Current reference clock for watchdog and timers."""
//|
const mp_obj_type_t 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);

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_table);
MAKE_PRINTER(rp2clock, rp2clock_auxsrc);
MAKE_ENUM_TYPE(rp2clock, AuxSrc, rp2clock_auxsrc);
47 changes: 47 additions & 0 deletions ports/raspberrypi/bindings/rp2clock/AuxSrc.h
@@ -0,0 +1,47 @@
/*
* 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/regs/clocks.h"

// Output sources
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;
94 changes: 94 additions & 0 deletions ports/raspberrypi/bindings/rp2clock/Index.c
@@ -0,0 +1,94 @@
/*
* 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 "py/enum.h"
#include "bindings/rp2clock/Index.h"

//| class Index:
//| """Defines the internal clock to be driven from GPIN external pin."""
//|
//| def __init__(self) -> None:
//| """Enum-like class to define the internal clock index."""
//| GPOUT0: object
//| """Clock routed to GPOUT0 (GP21)"""
//|
//| GPOUT1: object
//| """Clock routed to GPOUT1 (GP23)"""
//|
//| GPOUT2: object
//| """Clock routed to GPOUT2 (GP24)"""
//|
//| GPOUT3: object
//| """Clock routed to GPOUT3 (GP25)"""
//|
//| REF: object
//| """Reference clock for watchdog and timers."""
//|
//| SYS: object
//| """Main system clock for processors."""
//|
//| PERI: object
//| """Peripheral clock: UART, SPI, etc. 12-125MHz"""
//|
//| USB: object
//| """USB clock: Must be 48MHz."""
//|
//| ADC: object
//| """ADC clock: Must be 48MHz."""
//|
//| RTC: object
//| """RTC clock: Nominally 46875 for 1 second ticks."""
//|
const mp_obj_type_t 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);

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_table);
MAKE_PRINTER(rp2clock, rp2clock_index);
MAKE_ENUM_TYPE(rp2clock, Index, rp2clock_index);
46 changes: 46 additions & 0 deletions ports/raspberrypi/bindings/rp2clock/Index.h
@@ -0,0 +1,46 @@
/*
* 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 {
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,
} rp2clock_index_t;

extern const mp_obj_type_t rp2clock_index_type;