From 8cc2e0e906dd8caadd8198839f57c19deea6c87e Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sat, 28 Jan 2023 21:07:51 +0000 Subject: [PATCH] Fix quantum ring_buffer for ChibiOS (#19683) --- platforms/atomic_util.h | 10 +++++++--- platforms/chibios/atomic_util.h | 17 ++++++++++++++--- quantum/ring_buffer.h | 2 +- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/platforms/atomic_util.h b/platforms/atomic_util.h index 2c95302a13c..21286d72eb5 100644 --- a/platforms/atomic_util.h +++ b/platforms/atomic_util.h @@ -24,9 +24,13 @@ # define ATOMIC_BLOCK _Static_assert(0, "ATOMIC_BLOCK not implemented") # define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE not implemented") # define ATOMIC_BLOCK_FORCEON _Static_assert(0, "ATOMIC_BLOCK_FORCEON not implemented") +# define ATOMIC_FORCEON _Static_assert(0, "ATOMIC_FORCEON not implemented") +# define ATOMIC_RESTORESTATE _Static_assert(0, "ATOMIC_RESTORESTATE not implemented") # endif #else /* do nothing atomic macro */ -# define ATOMIC_BLOCK for (uint8_t __ToDo = 1; __ToDo; __ToDo = 0) -# define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK -# define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK +# define ATOMIC_BLOCK(t) for (uint8_t __ToDo = 1; __ToDo; __ToDo = 0) +# define ATOMIC_FORCEON +# define ATOMIC_RESTORESTATE +# define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE) +# define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON) #endif diff --git a/platforms/chibios/atomic_util.h b/platforms/chibios/atomic_util.h index 89750451532..234d7fd9f58 100644 --- a/platforms/chibios/atomic_util.h +++ b/platforms/chibios/atomic_util.h @@ -30,8 +30,19 @@ static __inline__ void __interrupt_enable__(const uint8_t *__s) { (void)__s; } -#define ATOMIC_BLOCK(type) for (type, __ToDo = __interrupt_disable__(); __ToDo; __ToDo = 0) -#define ATOMIC_FORCEON uint8_t sreg_save __attribute__((__cleanup__(__interrupt_enable__))) = 0 +static __inline__ syssts_t __interrupt_lock__(void) { + return chSysGetStatusAndLockX(); +} -#define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE not implemented") +static __inline__ void __interrupt_unlock__(const syssts_t *__s) { + chSysRestoreStatusX(*__s); + + __asm__ volatile("" ::: "memory"); +} + +#define ATOMIC_BLOCK(type) for (type, __ToDo = 1; __ToDo; __ToDo = 0) +#define ATOMIC_FORCEON uint8_t status_save __attribute__((__cleanup__(__interrupt_enable__))) = __interrupt_disable__() +#define ATOMIC_RESTORESTATE syssts_t status_save __attribute__((__cleanup__(__interrupt_unlock__))) = __interrupt_lock__() + +#define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE) #define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON) diff --git a/quantum/ring_buffer.h b/quantum/ring_buffer.h index 85419de11b8..54b2bfffd1d 100644 --- a/quantum/ring_buffer.h +++ b/quantum/ring_buffer.h @@ -1,8 +1,8 @@ #pragma once -#include #include #include +#include "atomic_util.h" #ifndef RBUF_SIZE # define RBUF_SIZE 32