From 8cf3bb5a1ce01ce69792a5eeeb904a2bb5b366e5 Mon Sep 17 00:00:00 2001 From: Rodrigo Alfonso Date: Sun, 5 Jan 2025 08:43:51 -0300 Subject: [PATCH] Adding volatile to variables that could change during interrupts and are read in main thread to prevent issues with compiler optimizations --- lib/LinkCable.hpp | 5 +++-- lib/LinkMobile.hpp | 4 ++-- lib/LinkRawCable.hpp | 2 +- lib/LinkRawWireless.hpp | 9 +++++---- lib/LinkSPI.hpp | 4 ++-- lib/LinkWireless.hpp | 2 +- lib/_link_common.hpp | 2 ++ 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/LinkCable.hpp b/lib/LinkCable.hpp index dc46976..c3fd990 100644 --- a/lib/LinkCable.hpp +++ b/lib/LinkCable.hpp @@ -83,6 +83,7 @@ class LinkCable { using u8 = unsigned char; using vu32 = volatile unsigned int; using vs32 = volatile signed int; + using vu8 = volatile unsigned char; using U16Queue = Link::Queue; static constexpr auto BASE_FREQUENCY = Link::_TM_FREQ_1024; @@ -373,8 +374,8 @@ class LinkCable { private: struct ExternalState { U16Queue syncedIncomingMessages[LINK_CABLE_MAX_PLAYERS]; - u8 playerCount; - u8 currentPlayerId; + vu8 playerCount; + vu8 currentPlayerId; }; struct InternalState { diff --git a/lib/LinkMobile.hpp b/lib/LinkMobile.hpp index d3199f0..53eaaab 100644 --- a/lib/LinkMobile.hpp +++ b/lib/LinkMobile.hpp @@ -892,8 +892,8 @@ class LinkMobile { u32 waitFrames = 0; u32 timeoutStateFrames = 0; u32 pingFrameCount = 0; - Role role = Role::NO_P2P_CONNECTION; - State state = NEEDS_RESET; + volatile Role role = Role::NO_P2P_CONNECTION; + volatile State state = NEEDS_RESET; PacketData nextCommandData; u32 nextCommandDataSize = 0; bool hasPendingTransfer = false; diff --git a/lib/LinkRawCable.hpp b/lib/LinkRawCable.hpp index 31145a1..6922427 100644 --- a/lib/LinkRawCable.hpp +++ b/lib/LinkRawCable.hpp @@ -258,7 +258,7 @@ class LinkRawCable { private: BaudRate baudRate = BaudRate::BAUD_RATE_1; - AsyncState asyncState = IDLE; + volatile AsyncState asyncState = IDLE; Response asyncData = EMPTY_RESPONSE; volatile bool isEnabled = false; diff --git a/lib/LinkRawWireless.hpp b/lib/LinkRawWireless.hpp index b087186..945918f 100644 --- a/lib/LinkRawWireless.hpp +++ b/lib/LinkRawWireless.hpp @@ -51,6 +51,7 @@ class LinkRawWireless { using u32 = unsigned int; using u16 = unsigned short; using u8 = unsigned char; + using vu8 = volatile unsigned char; public: static constexpr int PING_WAIT = 50; @@ -953,9 +954,9 @@ class LinkRawWireless { [[nodiscard]] u8 currentPlayerId() { return sessionState.currentPlayerId; } struct SessionState { - u8 playerCount = 1; - u8 currentPlayerId = 0; - bool isServerClosed = false; + vu8 playerCount = 1; + vu8 currentPlayerId = 0; + volatile bool isServerClosed = false; }; struct LoginMemory { @@ -966,7 +967,7 @@ class LinkRawWireless { LinkSPI linkSPI; LinkGPIO linkGPIO; SessionState sessionState; - State state = NEEDS_RESET; + volatile State state = NEEDS_RESET; volatile bool isEnabled = false; /** diff --git a/lib/LinkSPI.hpp b/lib/LinkSPI.hpp index 35e9784..2bdc374 100644 --- a/lib/LinkSPI.hpp +++ b/lib/LinkSPI.hpp @@ -295,8 +295,8 @@ class LinkSPI { Mode mode = Mode::SLAVE; DataSize dataSize = DataSize::SIZE_32BIT; bool waitMode = false; - AsyncState asyncState = IDLE; - u32 asyncData = 0; + volatile AsyncState asyncState = IDLE; + volatile u32 asyncData = 0; volatile bool isEnabled = false; void setNormalMode() { diff --git a/lib/LinkWireless.hpp b/lib/LinkWireless.hpp index 0432b4d..c38f890 100644 --- a/lib/LinkWireless.hpp +++ b/lib/LinkWireless.hpp @@ -981,7 +981,7 @@ class LinkWireless { u32 nextAsyncCommandData[LINK_WIRELESS_MAX_COMMAND_TRANSFER_LENGTH]; u32 nextAsyncCommandDataSize = 0; volatile bool isSendingSyncCommand = false; - Error lastError = NONE; + volatile Error lastError = NONE; volatile bool isEnabled = false; #ifdef LINK_WIRELESS_PUT_ISR_IN_IWRAM diff --git a/lib/_link_common.hpp b/lib/_link_common.hpp index f7ca18c..86ccc62 100644 --- a/lib/_link_common.hpp +++ b/lib/_link_common.hpp @@ -52,6 +52,8 @@ using vu32 = volatile unsigned int; using vs32 = volatile signed int; using vu16 = volatile unsigned short; using vs16 = volatile signed short; +using vu8 = volatile unsigned char; +using vs8 = volatile signed char; // Structs