Skip to content

Commit c1fa2fd

Browse files
committed
[rp2040] mutlicore lock
1 parent fa14cbc commit c1fa2fd

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

src/modm/platform/core/cortex/atomic_lock_impl.hpp.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616

1717
#include "../device.hpp"
1818
#include <modm/architecture/utils.hpp>
19+
%% if multicore
20+
#include "multicore.hpp"
21+
%% endif
1922

2023
/// @cond
2124
namespace modm::atomic
2225
{
2326

2427
class Lock
28+
%% if multicore
29+
: public modm::platform::multicore::CoreLock
30+
%% endif
2531
{
2632
public:
2733
modm_always_inline

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ def build(env):
294294
"with_memory_traits": env.has_module(":architecture:memory"),
295295
"with_assert": env.has_module(":architecture:assert"),
296296
"with_fpu": env.get("float-abi", "soft") != "soft",
297+
"multicore": env.has_module(":platform:multicore"),
297298
})
298299
env.outbasepath = "modm/src/modm/platform/core"
299300

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def prepare(module, options):
2424

2525
module.depends(":platform:cortex-m")
2626
module.depends(":platform:clockgen")
27+
module.depends(":platform:multicore")
2728
module.add_option(
2829
EnumerationOption(
2930
name="boot2",
@@ -62,8 +63,6 @@ def build(env):
6263
env.template("../cortex/delay_ns.hpp.in", "delay_ns.hpp")
6364
env.template("../cortex/delay_impl.hpp.in", "delay_impl.hpp")
6465
env.copy('resets.hpp')
65-
env.copy('multicore.hpp')
66-
env.copy('multicore.cpp')
6766

6867
flash_variant = env.get("boot2")
6968
env.copy(repopath("ext/rp/pico-sdk/src/boot2_" + flash_variant + '.cpp'),'src/boot2.cpp')

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,24 @@ namespace modm::platform::multicore {
8080
static void init() {
8181
unlock();
8282
}
83-
static void lock() {
83+
static modm_always_inline void lock() {
8484
auto l = getInstance();
8585
while (*l == 0);
8686
std::atomic_thread_fence(std::memory_order_acquire);
8787
}
88-
static void unlock() {
88+
static modm_always_inline void unlock() {
8989
auto l = getInstance();
9090
std::atomic_thread_fence(std::memory_order_release);
9191
*l = 0;
9292
}
9393
};
9494

95+
/* for use in modm::atomic::Lock */
96+
struct CoreLock {
97+
CoreLock() { SpinLockUnsafe<0>::lock(); }
98+
~CoreLock() { SpinLockUnsafe<1>::unlock(); }
99+
};
100+
95101
/**
96102
*
97103
* This class will disable interrupts prior to acquiring the spinlock
@@ -100,20 +106,21 @@ namespace modm::platform::multicore {
100106
class SpinLockBlocking : public SpinLockUnsafe<instance>{
101107
using Base = SpinLockUnsafe<instance>;
102108
public:
103-
static uint32_t lock() {
109+
static modm_always_inline uint32_t lock() {
104110
uint32_t is = __get_PRIMASK();
105111
__disable_irq();
106112
Base::lock();
107113
return is;
108114
}
109-
static void unlock(uint32_t is) {
115+
static modm_always_inline void unlock(uint32_t is) {
110116
Base::unlock();
111117
__set_PRIMASK(is);
112118
}
113119
};
114120

115121
using SystemSpinLock = SpinLockBlocking<0>;
116122

123+
117124
/**
118125
*
119126
* RAI lock acquiring and releasing
@@ -124,8 +131,8 @@ namespace modm::platform::multicore {
124131
SpinLockGuard(const SpinLockGuard& ) = delete;
125132
SpinLockGuard(SpinLockGuard&&) = delete;
126133
public:
127-
SpinLockGuard() { SpinLock::lock(); }
128-
~SpinLockGuard() { SpinLock::unlock(); }
134+
modm_always_inline SpinLockGuard() { SpinLock::lock(); }
135+
modm_always_inline ~SpinLockGuard() { SpinLock::unlock(); }
129136
};
130137

131138
template <uint8_t instance>
@@ -136,15 +143,15 @@ namespace modm::platform::multicore {
136143
SpinLockGuard(const SpinLockGuard& ) = delete;
137144
SpinLockGuard(SpinLockGuard&&) = delete;
138145
public:
139-
SpinLockGuard() : is(SpinLock::lock()) {}
140-
~SpinLockGuard() { SpinLock::unlock(is); }
146+
modm_always_inline SpinLockGuard() : is(SpinLock::lock()) {}
147+
modm_always_inline ~SpinLockGuard() { SpinLock::unlock(is); }
141148
};
142149

143150
using SystemSpinLockGuard = SpinLockGuard<SystemSpinLock>;
144151

145152
class Mutex {
146153
private:
147-
std::atomic<bool> locked = false;
154+
volatile bool locked = false;
148155
Mutex(const Mutex& ) = delete;
149156
Mutex(Mutex&&) = delete;
150157
public:
@@ -171,7 +178,7 @@ namespace modm::platform::multicore {
171178
};
172179

173180
struct Core {
174-
static uint32_t getNum() {
181+
static modm_always_inline uint32_t getNum() {
175182
return sio_hw->cpuid;
176183
}
177184
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2022, Andrey Kunitsyn
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
# -----------------------------------------------------------------------------
12+
13+
import math
14+
15+
def init(module):
16+
module.name = ":platform:multicore"
17+
module.description = "Multiple cores inplementation"
18+
19+
def prepare(module, options):
20+
if options[":target"].identifier.platform != "rp":
21+
return False
22+
return True
23+
24+
25+
def build(env):
26+
env.outbasepath = "modm/src/modm/platform/core"
27+
env.copy('multicore.hpp')
28+
env.copy('multicore.cpp')

0 commit comments

Comments
 (0)