Skip to content

Commit 1159c14

Browse files
committed
Docs: _Volatile instead of @inline(never)
1 parent 75a3e14 commit 1159c14

File tree

1 file changed

+9
-18
lines changed
  • Sources/EmbeddedSwift/Documentation.docc/SDKSupport

1 file changed

+9
-18
lines changed

Sources/EmbeddedSwift/Documentation.docc/SDKSupport/Baremetal.md

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,17 @@ gpioDataRegister.pointee |= (1 << 5) // Set bit 5
4646
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:
4747

4848
```swift
49-
// ✅ Correct approach using volatile operations
50-
@inline(never)
51-
func volatileStore(_ value: UInt32, to address: UInt) {
52-
UnsafeMutablePointer<UInt32>(bitPattern: address)!.pointee = value
53-
}
54-
55-
@inline(never)
56-
func volatileLoad(from address: UInt) -> UInt32 {
57-
return UnsafeMutablePointer<UInt32>(bitPattern: address)!.pointee
58-
}
59-
60-
// Using the volatile operations
61-
let gpioBase = 0x40010000
62-
let currentValue = volatileLoad(from: gpioBase)
63-
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)
6457
```
6558

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.
6960

7061
### 2. Using Swift MMIO for type-safe register access
7162

0 commit comments

Comments
 (0)