8cc2e0e906dd8caadd8198839f57c19deea6c87e

Author
Joel Challis <git@zvecr.com>
Committer
GitHub <noreply@github.com>
Date

Message

Fix quantum ring_buffer for ChibiOS (#19683)

Diff

 1diff --git a/platforms/atomic_util.h b/platforms/atomic_util.h
 2index 2c95302a13c22263a976b62af2de9452b840cb25..21286d72eb52631fc0ca43a74cc3130db6bfd426 100644
 3--- a/platforms/atomic_util.h
 4+++ b/platforms/atomic_util.h
 5@@ -24,9 +24,13 @@
 6 #        define ATOMIC_BLOCK _Static_assert(0, "ATOMIC_BLOCK not implemented")
 7 #        define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE not implemented")
 8 #        define ATOMIC_BLOCK_FORCEON _Static_assert(0, "ATOMIC_BLOCK_FORCEON not implemented")
 9+#        define ATOMIC_FORCEON _Static_assert(0, "ATOMIC_FORCEON not implemented")
10+#        define ATOMIC_RESTORESTATE _Static_assert(0, "ATOMIC_RESTORESTATE not implemented")
11 #    endif
12 #else /* do nothing atomic macro */
13-#    define ATOMIC_BLOCK for (uint8_t __ToDo = 1; __ToDo; __ToDo = 0)
14-#    define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK
15-#    define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK
16+#    define ATOMIC_BLOCK(t) for (uint8_t __ToDo = 1; __ToDo; __ToDo = 0)
17+#    define ATOMIC_FORCEON
18+#    define ATOMIC_RESTORESTATE
19+#    define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
20+#    define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
21 #endif
22diff --git a/platforms/chibios/atomic_util.h b/platforms/chibios/atomic_util.h
23index 897504515326eab75fa24464e5f09e99d31ced70..234d7fd9f58fd639983b1152e187b8345faeca3d 100644
24--- a/platforms/chibios/atomic_util.h
25+++ b/platforms/chibios/atomic_util.h
26@@ -30,8 +30,19 @@ static __inline__ void __interrupt_enable__(const uint8_t *__s) {
27     (void)__s;
28 }
29 
30-#define ATOMIC_BLOCK(type) for (type, __ToDo = __interrupt_disable__(); __ToDo; __ToDo = 0)
31-#define ATOMIC_FORCEON uint8_t sreg_save __attribute__((__cleanup__(__interrupt_enable__))) = 0
32+static __inline__ syssts_t __interrupt_lock__(void) {
33+    return chSysGetStatusAndLockX();
34+}
35+
36+static __inline__ void __interrupt_unlock__(const syssts_t *__s) {
37+    chSysRestoreStatusX(*__s);
38+
39+    __asm__ volatile("" ::: "memory");
40+}
41+
42+#define ATOMIC_BLOCK(type) for (type, __ToDo = 1; __ToDo; __ToDo = 0)
43+#define ATOMIC_FORCEON uint8_t status_save __attribute__((__cleanup__(__interrupt_enable__))) = __interrupt_disable__()
44+#define ATOMIC_RESTORESTATE syssts_t status_save __attribute__((__cleanup__(__interrupt_unlock__))) = __interrupt_lock__()
45 
46-#define ATOMIC_BLOCK_RESTORESTATE _Static_assert(0, "ATOMIC_BLOCK_RESTORESTATE not implemented")
47+#define ATOMIC_BLOCK_RESTORESTATE ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
48 #define ATOMIC_BLOCK_FORCEON ATOMIC_BLOCK(ATOMIC_FORCEON)
49diff --git a/quantum/ring_buffer.h b/quantum/ring_buffer.h
50index 85419de11b800d7c0926a7f58949c62acbdcaff7..54b2bfffd1dd02c93af72c23da06d568ee176e6d 100644
51--- a/quantum/ring_buffer.h
52+++ b/quantum/ring_buffer.h
53@@ -1,8 +1,8 @@
54 #pragma once
55 
56-#include <util/atomic.h>
57 #include <stdint.h>
58 #include <stdbool.h>
59+#include "atomic_util.h"
60 
61 #ifndef RBUF_SIZE
62 #    define RBUF_SIZE 32