You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/EmbeddedSwift/Documentation.docc/SDKSupport/Baremetal.md
+9-18Lines changed: 9 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -46,26 +46,17 @@ gpioDataRegister.pointee |= (1 << 5) // Set bit 5
46
46
Hardware registers are volatile - their values can change independently of your program's execution (due to hardware events, interrupts, or peripheral operation). To ensure correct behavior, you must inform the compiler that these memory locations are volatile, preventing unwanted optimizations:
volatileStore(currentValue | (1<<5), to: gpioBase)
49
+
// Need to use these flags: -enable-experimental-feature Volatile
50
+
import_Volatile
51
+
52
+
// ✅ Use VolatileMappedRegister for volatile semantics
53
+
let gpioBase: UInt=0x40010000
54
+
let gpioDataRegister = VolatileMappedRegister<UInt32>(unsafeBitPattern: gpioBase)
55
+
...= gpioDataRegister.load()
56
+
gpioDataRegister.store(1<<5)
64
57
```
65
58
66
-
The `@inline(never)` attribute prevents the compiler from inlining these functions, which helps ensure the memory accesses actually occur.
67
-
68
-
Consider using Swift MMIO (see below) which uses compiler intrinsics for true volatile semantics and abstracts this problem away from the user.
59
+
Whenever possible, consider using Swift MMIO (see below) which does also provide proper volatile semantics, but more importantly provides type safety on individual registers and their fields.
69
60
70
61
### 2. Using Swift MMIO for type-safe register access
0 commit comments