Adding volatile to variables that could change during interrupts and are read in main thread to prevent issues with compiler optimizations

This commit is contained in:
Rodrigo Alfonso
2025-01-05 08:43:51 -03:00
parent 2129c77377
commit 8cf3bb5a1c
7 changed files with 16 additions and 12 deletions

View File

@@ -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<u16, LINK_CABLE_QUEUE_SIZE>;
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 {

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
/**

View File

@@ -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() {

View File

@@ -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

View File

@@ -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