Initial Release
This commit is contained in:
12
src/host/hardware_sync/CMakeLists.txt
Normal file
12
src/host/hardware_sync/CMakeLists.txt
Normal file
@ -0,0 +1,12 @@
|
||||
pico_simple_hardware_headers_target(sync)
|
||||
|
||||
if (NOT TARGET hardware_sync)
|
||||
add_library(hardware_sync INTERFACE)
|
||||
|
||||
target_sources(hardware_sync INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/sync_core0_only.c
|
||||
)
|
||||
|
||||
target_link_libraries(hardware_sync INTERFACE hardware_sync_headers pico_platform)
|
||||
endif()
|
||||
|
106
src/host/hardware_sync/include/hardware/sync.h
Normal file
106
src/host/hardware_sync/include/hardware/sync.h
Normal file
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _HARDWARE_SYNC_H
|
||||
#define _HARDWARE_SYNC_H
|
||||
|
||||
#include "pico.h"
|
||||
|
||||
#ifndef __cplusplus
|
||||
|
||||
#if (__STDC_VERSION__ >= 201112L)
|
||||
#include <stdatomic.h>
|
||||
#else
|
||||
enum {
|
||||
memory_order_acquire, memory_order_release
|
||||
};
|
||||
static inline void atomic_thread_fence(uint x) {}
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef PICO_SPINLOCK_ID_TIMER
|
||||
#define PICO_SPINLOCK_ID_TIMER 10
|
||||
#endif
|
||||
|
||||
#ifndef PICO_SPINLOCK_ID_STRIPED_FIRST
|
||||
#define PICO_SPINLOCK_ID_STRIPED_FIRST 16
|
||||
#endif
|
||||
|
||||
#ifndef PICO_SPINLOCK_ID_STRIPED_LAST
|
||||
#define PICO_SPINLOCK_ID_STRIPED_LAST 23
|
||||
#endif
|
||||
|
||||
typedef struct _spin_lock_t spin_lock_t;
|
||||
|
||||
inline static void __mem_fence_acquire() {
|
||||
#ifndef __cplusplus
|
||||
atomic_thread_fence(memory_order_acquire);
|
||||
#else
|
||||
std::atomic_thread_fence(std::memory_order_acquire);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline static void __mem_fence_release() {
|
||||
#ifndef __cplusplus
|
||||
atomic_thread_fence(memory_order_release);
|
||||
#else
|
||||
std::atomic_thread_fence(std::memory_order_release);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void __sev();
|
||||
|
||||
void __wev();
|
||||
|
||||
void __wfi();
|
||||
|
||||
void __wfe();
|
||||
|
||||
uint32_t save_and_disable_interrupts();
|
||||
|
||||
void restore_interrupts(uint32_t status);
|
||||
|
||||
uint spin_lock_get_num(spin_lock_t *lock);
|
||||
|
||||
spin_lock_t *spin_lock_instance(uint lock_num);
|
||||
|
||||
void spin_lock_unsafe_blocking(spin_lock_t *lock);
|
||||
|
||||
void spin_unlock_unsafe(spin_lock_t *lock);
|
||||
|
||||
uint32_t spin_lock_blocking(spin_lock_t *lock);
|
||||
|
||||
bool is_spin_locked(const spin_lock_t *lock);
|
||||
|
||||
void spin_unlock(spin_lock_t *lock, uint32_t saved_irq);
|
||||
|
||||
uint get_core_num();
|
||||
|
||||
spin_lock_t *spin_lock_init(uint lock_num);
|
||||
|
||||
void clear_spin_locks(void);
|
||||
|
||||
uint next_striped_spin_lock_num();
|
||||
|
||||
void spin_lock_claim(uint lock_num);
|
||||
void spin_lock_claim_mask(uint32_t lock_num_mask);
|
||||
void spin_lock_unclaim(uint lock_num);
|
||||
int spin_lock_claim_unused(bool required);
|
||||
uint spin_lock_num(spin_lock_t *lock);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
140
src/host/hardware_sync/sync_core0_only.c
Normal file
140
src/host/hardware_sync/sync_core0_only.c
Normal file
@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "hardware/sync.h"
|
||||
#include "hardware/platform_defs.h"
|
||||
|
||||
// This is a dummy implementation that is single threaded
|
||||
|
||||
static struct _spin_lock_t {
|
||||
bool locked;
|
||||
} _spinlocks[NUM_SPIN_LOCKS];
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(save_and_disable_interrupts)
|
||||
|
||||
//static uint8_t striped_spin_lock_num;
|
||||
|
||||
uint32_t PICO_WEAK_FUNCTION_IMPL_NAME(save_and_disable_interrupts)() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(restore_interrupts)
|
||||
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(restore_interrupts)(uint32_t status) {
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_instance)
|
||||
|
||||
spin_lock_t *PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_instance)(uint lock_num) {
|
||||
assert(lock_num < NUM_SPIN_LOCKS);
|
||||
return &_spinlocks[lock_num];
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_get_num)
|
||||
|
||||
uint PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_get_num)(spin_lock_t *lock) {
|
||||
return lock - _spinlocks;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_init)
|
||||
|
||||
spin_lock_t *PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_init)(uint lock_num) {
|
||||
spin_lock_t *lock = spin_lock_instance(lock_num);
|
||||
spin_unlock_unsafe(lock);
|
||||
return lock;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_unsafe_blocking)
|
||||
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_unsafe_blocking)(spin_lock_t *lock) {
|
||||
lock->locked = true;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_blocking)
|
||||
|
||||
uint32_t PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_blocking)(spin_lock_t *lock) {
|
||||
spin_lock_unsafe_blocking(lock);
|
||||
return 1; // todo wrong value
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(is_spin_locked)
|
||||
|
||||
bool PICO_WEAK_FUNCTION_IMPL_NAME(is_spin_locked)(const spin_lock_t *lock) {
|
||||
return lock->locked;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_unlock_unsafe)
|
||||
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(spin_unlock_unsafe)(spin_lock_t *lock) {
|
||||
lock->locked = false;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_unlock)
|
||||
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(spin_unlock)(spin_lock_t *lock, uint32_t saved_irq) {
|
||||
spin_unlock_unsafe(lock);
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(__sev)
|
||||
|
||||
volatile bool event_fired;
|
||||
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(__sev)() {
|
||||
event_fired = true;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(__wfi)
|
||||
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(__wfi)() {
|
||||
panic("Can't wait on irq for host core0 only implementation");
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(__wfe)
|
||||
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(__wfe)() {
|
||||
while (!event_fired) tight_loop_contents();
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(get_core_num)
|
||||
|
||||
uint PICO_WEAK_FUNCTION_IMPL_NAME(get_core_num)() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(clear_spin_locks)
|
||||
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(clear_spin_locks)(void) {
|
||||
for (uint i = 0; i < NUM_SPIN_LOCKS; i++) {
|
||||
spin_unlock_unsafe(spin_lock_instance(i));
|
||||
}
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(next_striped_spin_lock_num)
|
||||
uint PICO_WEAK_FUNCTION_IMPL_NAME(next_striped_spin_lock_num)() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_claim)
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_claim)(uint lock_num) {
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_claim_mask)
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_claim_mask)(uint32_t mask) {
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_unclaim)
|
||||
void PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_unclaim)(uint lock_num) {
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_claim_unused)
|
||||
int PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_claim_unused)(bool required) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
PICO_WEAK_FUNCTION_DEF(spin_lock_num)
|
||||
uint PICO_WEAK_FUNCTION_IMPL_NAME(spin_lock_num)(spin_lock_t *lock) {
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user