Skip to content

Commit f933c19

Browse files
andryblacksalkinium
authored andcommitted
[example] Add RP2040 examples
1 parent db9c84c commit f933c19

File tree

13 files changed

+518
-0
lines changed

13 files changed

+518
-0
lines changed

.github/workflows/linux.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ jobs:
8080
if: always()
8181
run: |
8282
(cd examples && ../tools/scripts/examples_compile.py samv)
83+
- name: Examples RP20 Devices
84+
if: always()
85+
run: |
86+
(cd examples && ../tools/scripts/examples_compile.py rp_pico)
8387
- name: Execute Python Scripts
8488
if: always()
8589
run: |

examples/generic/delay/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<!-- <extends>modm:nucleo-l432kc</extends> -->
1818
<!-- <extends>modm:nucleo-l476rg</extends> -->
1919
<!-- <extends>modm:samd21-mini</extends> -->
20+
<!-- <extends>modm:rp-pico</extends> -->
2021
<options>
2122
<option name="modm:build:build.path">../../../build/generic/delay</option>
2223
<option name="modm:io:with_printf">true</option>

examples/generic/usb/project.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<!-- <extends>modm:disco-f303vc</extends> -->
1010
<!-- <extends>modm:disco-f407vg</extends> -->
1111
<!-- <extends>modm:disco-f746ng</extends> -->
12+
<!-- <extends>modm:rp-pico</extends> -->
1213
<options>
1314
<option name="modm:build:build.path">../../../build/generic/usb</option>
1415
<option name="modm:build:openocd.cfg">openocd.cfg</option>

examples/rp_pico/blink/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2016, Sascha Schade
3+
* Copyright (c) 2017, Niklas Hauser
4+
*
5+
* This file is part of the modm project.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
10+
*/
11+
// ----------------------------------------------------------------------------
12+
13+
#include <modm/board.hpp>
14+
15+
using namespace Board;
16+
17+
/*
18+
* Blinks the green user LED with 1 Hz.
19+
* It is on for 90% of the time and off for 10% of the time.
20+
*/
21+
22+
int
23+
main()
24+
{
25+
Board::initialize();
26+
27+
LedGreen::setOutput();
28+
29+
while (true)
30+
{
31+
LedGreen::set();
32+
modm::delay(900ms);
33+
34+
LedGreen::reset();
35+
modm::delay(100ms);
36+
}
37+
38+
return 0;
39+
}

examples/rp_pico/blink/project.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<library>
2+
<extends>modm:rp-pico</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/rp_pico/blink</option>
5+
<option name="modm:platform:cortex-m:vector_table_location">ram</option>
6+
</options>
7+
<modules>
8+
<module>modm:build:scons</module>
9+
</modules>
10+
</library>

examples/rp_pico/fiber/main.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
12+
#include <modm/board.hpp>
13+
#include <modm/debug/logger.hpp>
14+
#include <modm/processing.hpp>
15+
#include <mutex>
16+
17+
using namespace Board;
18+
using namespace std::chrono_literals;
19+
20+
// Create an IODeviceWrapper around the Uart Peripheral we want to use
21+
modm::IODeviceWrapper<Uart0, modm::IOBuffer::BlockIfFull> loggerDevice;
22+
23+
// Set all four logger streams to use the UART
24+
modm::log::Logger modm::log::debug(loggerDevice);
25+
modm::log::Logger modm::log::info(loggerDevice);
26+
modm::log::Logger modm::log::warning(loggerDevice);
27+
modm::log::Logger modm::log::error(loggerDevice);
28+
29+
constexpr uint32_t cycles = 1'000'000;
30+
31+
static multicore::Mutex log_mutex;
32+
33+
struct CoreData
34+
{
35+
uint32_t total_counter = 0;
36+
uint32_t f1counter = 0;
37+
uint32_t f2counter = 0;
38+
};
39+
40+
void
41+
fiber_function1(CoreData& d)
42+
{
43+
while (++d.f1counter < cycles)
44+
{
45+
modm::fiber::yield();
46+
d.total_counter++;
47+
}
48+
}
49+
50+
void
51+
fiber_function2(CoreData& d)
52+
{
53+
while (++d.f2counter < cycles)
54+
{
55+
modm::fiber::yield();
56+
d.total_counter++;
57+
}
58+
}
59+
60+
// put cores to mostly equalent environment
61+
modm_core0_data CoreData d0;
62+
modm_core1_data CoreData d1;
63+
64+
modm_core0_noinit modm::fiber::Stack<384> stack01;
65+
modm_core0_noinit modm::fiber::Stack<384> stack02;
66+
modm_core1_noinit modm::fiber::Stack<384> stack11;
67+
modm_core1_noinit modm::fiber::Stack<384> stack12;
68+
69+
modm_core0_data
70+
modm::Fiber fiber01(stack01, []() { fiber_function1(d0); }, 0);
71+
modm_core0_data
72+
modm::Fiber fiber02(stack02, []() { fiber_function2(d0); }, 0);
73+
modm_core1_data
74+
modm::Fiber fiber11(stack11, []() { fiber_function1(d1); }, 1);
75+
modm_core1_data
76+
modm::Fiber fiber12(stack12, []() { fiber_function2(d1); }, 1);
77+
78+
template<typename TimeDiff>
79+
static void
80+
print_result(const CoreData& d, TimeDiff diff)
81+
{
82+
std::lock_guard<multicore::Mutex> g(log_mutex);
83+
MODM_LOG_INFO << "Benchmark for core" << multicore::Core::cpuId() << " done!" << modm::endl;
84+
MODM_LOG_INFO << "Executed " << d.total_counter << " yields in " << diff << modm::endl;
85+
MODM_LOG_INFO << ((d.total_counter * 1'000'000ull) / std::chrono::microseconds(diff).count());
86+
MODM_LOG_INFO << " yields per second, ";
87+
MODM_LOG_INFO << (std::chrono::nanoseconds(diff).count() / d.total_counter);
88+
MODM_LOG_INFO << "ns per yield" << modm::endl;
89+
MODM_LOG_INFO.flush();
90+
}
91+
92+
void
93+
core1_main()
94+
{
95+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
96+
modm::fiber::Scheduler::run();
97+
const auto diff = (modm::PreciseClock::now() - start);
98+
99+
print_result(d1, diff);
100+
while (true) __NOP();
101+
}
102+
103+
int
104+
main()
105+
{
106+
Board::initialize();
107+
108+
// initialize Uart0 for MODM_LOG_*
109+
Uart0::connect<GpioOutput0::Tx>();
110+
Uart0::initialize<Board::SystemClock, 115200_Bd>();
111+
112+
MODM_LOG_INFO << "Starting fiber modm::yield benchmark..." << modm::endl;
113+
MODM_LOG_INFO.flush();
114+
115+
multicore::Core1::run(core1_main);
116+
117+
const modm::PreciseTimestamp start = modm::PreciseClock::now();
118+
modm::fiber::Scheduler::run();
119+
const auto diff = (modm::PreciseClock::now() - start);
120+
121+
print_result(d0, diff);
122+
while (true) __NOP();
123+
124+
return 0;
125+
}

examples/rp_pico/fiber/project.xml

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

examples/rp_pico/logger/main.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 Kunitsyn
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/protothread.hpp>
19+
#include <modm/processing/timer.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+
class BlinkThread : public modm::pt::Protothread
36+
{
37+
public:
38+
BlinkThread() { timeout.restart(100ms); }
39+
40+
bool
41+
update()
42+
{
43+
PT_BEGIN();
44+
45+
while (true)
46+
{
47+
Board::LedGreen::reset();
48+
49+
PT_WAIT_UNTIL(timeout.isExpired());
50+
timeout.restart(100ms);
51+
52+
Board::LedGreen::set();
53+
54+
PT_WAIT_UNTIL(timeout.isExpired());
55+
timeout.restart(900ms);
56+
57+
MODM_LOG_INFO << "Seconds since reboot: " << ++uptime << modm::endl;
58+
}
59+
60+
PT_END();
61+
}
62+
63+
private:
64+
modm::ShortTimeout timeout;
65+
uint32_t uptime;
66+
};
67+
68+
BlinkThread blinkThread;
69+
70+
// ----------------------------------------------------------------------------
71+
int
72+
main()
73+
{
74+
Board::initialize();
75+
76+
// initialize Uart0 for MODM_LOG_*
77+
Uart0::connect<GpioOutput0::Tx>();
78+
Uart0::initialize<Board::SystemClock, 115200_Bd>();
79+
80+
// Use the logging streams to print some messages.
81+
// Change MODM_LOG_LEVEL above to enable or disable these messages
82+
MODM_LOG_DEBUG << "debug" << modm::endl;
83+
MODM_LOG_INFO << "info" << modm::endl;
84+
MODM_LOG_WARNING << "warning" << modm::endl;
85+
MODM_LOG_ERROR << "error" << modm::endl;
86+
87+
while (true) { blinkThread.update(); }
88+
89+
return 0;
90+
}

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>

0 commit comments

Comments
 (0)