Skip to content

Commit ae38925

Browse files
committed
[rp2040] uart, connector api
1 parent 7f0c136 commit ae38925

File tree

20 files changed

+886
-103
lines changed

20 files changed

+886
-103
lines changed

examples/rp_pico/logger/main.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2011, Fabian Greif
3+
* Copyright (c) 2013, Kevin Läufer
4+
* Copyright (c) 2013-2017, Niklas Hauser
5+
* Copyright (c) 2014, 2016, Sascha Schade
6+
* Copyright (c) 2022, Andrey Kunistyn
7+
*
8+
* This file is part of the modm project.
9+
*
10+
* This Source Code Form is subject to the terms of the Mozilla Public
11+
* License, v. 2.0. If a copy of the MPL was not distributed with this
12+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
13+
*/
14+
// ----------------------------------------------------------------------------
15+
16+
#include <modm/board.hpp>
17+
#include <modm/debug/logger.hpp>
18+
#include <modm/processing/timer.hpp>
19+
#include <modm/processing/protothread.hpp>
20+
21+
// ----------------------------------------------------------------------------
22+
// Set the log level
23+
#undef MODM_LOG_LEVEL
24+
#define MODM_LOG_LEVEL modm::log::INFO
25+
26+
// Create an IODeviceWrapper around the Uart Peripheral we want to use
27+
modm::IODeviceWrapper< Uart0, modm::IOBuffer::BlockIfFull > loggerDevice;
28+
29+
// Set all four logger streams to use the UART
30+
modm::log::Logger modm::log::debug(loggerDevice);
31+
modm::log::Logger modm::log::info(loggerDevice);
32+
modm::log::Logger modm::log::warning(loggerDevice);
33+
modm::log::Logger modm::log::error(loggerDevice);
34+
35+
36+
class BlinkThread : public modm::pt::Protothread
37+
{
38+
public:
39+
BlinkThread()
40+
{
41+
timeout.restart(100ms);
42+
}
43+
44+
bool
45+
update()
46+
{
47+
PT_BEGIN();
48+
49+
while (true)
50+
{
51+
Board::LedGreen::reset();
52+
53+
PT_WAIT_UNTIL(timeout.isExpired());
54+
timeout.restart(100ms);
55+
56+
Board::LedGreen::set();
57+
58+
PT_WAIT_UNTIL(timeout.isExpired()) ;
59+
timeout.restart(900ms);
60+
61+
MODM_LOG_INFO << "Seconds since reboot: " << ++uptime << modm::endl;
62+
}
63+
64+
PT_END();
65+
}
66+
67+
private:
68+
modm::ShortTimeout timeout;
69+
uint32_t uptime;
70+
};
71+
72+
BlinkThread blinkThread;
73+
74+
75+
// ----------------------------------------------------------------------------
76+
int
77+
main()
78+
{
79+
Board::initialize();
80+
81+
// initialize Uart0 for MODM_LOG_*
82+
Uart0::connect<GpioOutput0::Tx>();
83+
Uart0::initialize<Board::SystemClock, 115200_Bd>();
84+
85+
// Use the logging streams to print some messages.
86+
// Change MODM_LOG_LEVEL above to enable or disable these messages
87+
MODM_LOG_DEBUG << "debug" << modm::endl;
88+
MODM_LOG_INFO << "info" << modm::endl;
89+
MODM_LOG_WARNING << "warning" << modm::endl;
90+
MODM_LOG_ERROR << "error" << modm::endl;
91+
92+
93+
while (true)
94+
{
95+
blinkThread.update();
96+
}
97+
98+
return 0;
99+
}

examples/rp_pico/logger/project.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<library>
2+
<extends>modm:rp-pico</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/rp_pico/logger</option>
5+
</options>
6+
<modules>
7+
<module>modm:debug</module>
8+
<module>modm:platform:gpio</module>
9+
<module>modm:platform:uart:0</module>
10+
<module>modm:processing:timer</module>
11+
<module>modm:processing:protothread</module>
12+
<module>modm:build:scons</module>
13+
</modules>
14+
</library>

src/modm/board/rp_pico/board.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct SystemClock
3333
static constexpr uint32_t UsbPLLMul = 40;
3434
static constexpr uint32_t RefFrequency = 12_MHz;
3535
static constexpr uint32_t UsbFrequency = 48_MHz;
36+
static constexpr uint32_t SysFrequency = Frequency;
37+
static constexpr uint32_t PeriFrequency = SysFrequency;
3638

3739
static bool inline
3840
enable()

src/modm/platform/clock/rp/clocks.hpp.in

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
#include <stdint.h>
1515
#include <modm/platform/device.hpp>
16+
#include <modm/platform/core/resets.hpp>
1617
#include <modm/architecture/interface/delay.hpp>
1718
#include <hardware/structs/clocks.h>
1819
#include <hardware/structs/pll.h>
19-
#include <hardware/structs/resets.h>
2020

2121
namespace modm::platform
2222
{
@@ -48,15 +48,6 @@ namespace modm::platform
4848

4949
static constexpr uint32_t BootFrequency = 125'000'000; // after bootloader
5050

51-
static inline void reset_block(uint32_t bits) {
52-
hw_set_bits(&resets_hw->reset, bits);
53-
}
54-
static inline void unreset_block_wait(uint32_t bits) {
55-
hw_clear_bits(&resets_hw->reset, bits);
56-
while (~resets_hw->reset_done & bits)
57-
__NOP();
58-
}
59-
6051
public:
6152
static inline void disableResus() {
6253
clocks_hw->resus.ctrl = 0;
@@ -84,8 +75,8 @@ namespace modm::platform
8475
return;
8576
}
8677
uint32_t pll_reset = (Pll::Usb == pll_name) ? RESETS_RESET_PLL_USB_BITS : RESETS_RESET_PLL_SYS_BITS;
87-
reset_block(pll_reset);
88-
unreset_block_wait(pll_reset);
78+
Resets::reset(pll_reset);
79+
Resets::unresetWait(pll_reset);
8980

9081
// Load VCO-related dividers before starting VCO
9182
pll->cs = refdiv;

src/modm/platform/clock/systick/module.lb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def build(env):
4646
# H742/43: Prescaler not implemented in revY
4747
elif target.family == "h7" and target.name in ["42", "43", "50", "53"] and target.revision == "y":
4848
div = 1
49+
# RP2040 external reference clock need additional configuration
50+
elif target.platform == "rp":
51+
div = 1
4952

5053
env.substitutions = {
5154
"systick_frequency": env.get(":freertos:frequency", freq),

src/modm/platform/core/rp/module.lb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def build(env):
3333
env.substitutions = {"target": target}
3434
env.outbasepath = "modm/src/modm/platform/core"
3535
# startup helper code
36-
env.template("startup_platform.c.in")
36+
env.template("startup_platform.cpp.in")
3737

3838
# delay code that must be tuned for each family
3939
# (cycles per loop, setup cost in loops, max cpu frequency)
@@ -56,6 +56,7 @@ def build(env):
5656
env.template("../cortex/delay_ns.cpp.in", "delay_ns.cpp")
5757
env.template("../cortex/delay_ns.hpp.in", "delay_ns.hpp")
5858
env.template("../cortex/delay_impl.hpp.in", "delay_impl.hpp")
59+
env.copy('resets.hpp')
5960

6061

6162
def post_build(env):

src/modm/platform/core/rp/resets.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2022, Andrey Kunitsyn
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
#pragma once
12+
13+
#include <modm/platform/device.hpp>
14+
#include <modm/architecture/interface/assert.hpp>
15+
#include <hardware/structs/resets.h>
16+
17+
namespace modm::platform {
18+
19+
struct Resets {
20+
/// @todo use Peripherial enum
21+
static inline void reset(uint32_t bits) {
22+
hw_set_bits(&resets_hw->reset, bits);
23+
}
24+
static inline void unresetWait(uint32_t bits) {
25+
hw_clear_bits(&resets_hw->reset, bits);
26+
while (~resets_hw->reset_done & bits)
27+
__NOP();
28+
}
29+
};
30+
31+
32+
}

src/modm/platform/core/rp/startup_platform.c.in renamed to src/modm/platform/core/rp/startup_platform.cpp.in

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@
1010
// ----------------------------------------------------------------------------
1111

1212
#include "../device.hpp"
13+
#include <modm/platform/core/resets.hpp>
1314

14-
#include <hardware/structs/resets.h>
15-
static inline void reset_block(uint32_t bits) {
16-
hw_set_bits(&resets_hw->reset, bits);
17-
}
18-
static inline void unreset_block_wait(uint32_t bits) {
19-
hw_clear_bits(&resets_hw->reset, bits);
20-
while (~resets_hw->reset_done & bits)
21-
__NOP();
22-
}
2315
/**
2416
* This code should _only_ enable internal memories and nothing else.
2517
* Since this is the first code executed after a reset, you do not
@@ -30,15 +22,15 @@ static inline void unreset_block_wait(uint32_t bits) {
3022
* In that case, consider using inline assembly to manage stack access
3123
* manually, until the memory is enabled.
3224
*/
33-
void
25+
extern "C" void
3426
__modm_initialize_platform(void)
3527
{
3628

3729
// Reset all peripherals to put system into a known state,
3830
// - except for QSPI pads and the XIP IO bank, as this is fatal if running from flash
3931
// - and the PLLs, as this is fatal if clock muxing has not been reset on this boot
4032
// - and USB, syscfg, as this disturbs USB-to-SWD on core 1
41-
reset_block(~(
33+
modm::platform::Resets::reset(~(
4234
RESETS_RESET_IO_QSPI_BITS |
4335
RESETS_RESET_PADS_QSPI_BITS |
4436
RESETS_RESET_PLL_USB_BITS |
@@ -49,7 +41,7 @@ __modm_initialize_platform(void)
4941

5042
// Remove reset from peripherals which are clocked only by clk_sys and
5143
// clk_ref. Other peripherals stay in reset until we've configured clocks.
52-
unreset_block_wait(RESETS_RESET_BITS & ~(
44+
modm::platform::Resets::unresetWait(RESETS_RESET_BITS & ~(
5345
RESETS_RESET_ADC_BITS |
5446
RESETS_RESET_RTC_BITS |
5547
RESETS_RESET_SPI0_BITS |

0 commit comments

Comments
 (0)