I play with pure bare metal example for esp32c6 Bare-Metal Swift on M5Stack NanoC6: No C, No Assembly, No ESP-IDF - #3 by trickart from @trickart
I tried to make PWMLed and use enums:
struct PWMLedOld {
enum Timer: UInt32 {
case timer0 = 0
case timer1
case timer2
case timer3
case timerMax
}
enum Channel: UInt32 {
case channel0 = 0
case channel1
case channel2
case channel3
case channel4
case channel5
case channel6
case channel7
case max
}
......
let pin: Int
let channel: PWMLed.Channel
let timer: PWMLed.Timer
let mode: PWMLed.Mode
let maxDuty: UInt32
init?(
pin: Int,
channel: PWMLed.Channel = .channel0,
timer: PWMLed.Timer = .timer0,
frequencyHz: UInt = 1000,
mode: PWMLed.Mode = .low,
dutyResolution: PWMLed.TimerBit = .bit13,
) {
etc. to have safe nice swifty code. But completely failed.
It's enough tu use rawValue once.
func setDuty(_ duty: UInt32) {
let base = UInt32(CurrentBoard.DeviceAddress.LEDC)
let ch = UInt32(channel.rawValue) * 0x14 // <- here
regStore(base + ch + 0x08, duty << 4)
regStore(base + ch + 0x0C, (1 << 31))
regStore(base + ch + 0x00, (1 << 2) | (1 << 4))
}
It compiles and it fails right after:
TOOLCHAINS=org.swift.630202603201a swift build --triple riscv32-none-none-eabi --toolset toolset.json --product Application
Building for debugging...
error: link command failed with exit code 1 (use -v to see invocation)
clang: warning: argument unused during compilation: '-F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks' [-Wunused-command-line-argument]
ld.lld: error: undefined symbol: arc4random_buf
>>> referenced by <compiler-generated>:0 (/<compiler-generated>:0)
>>> /Users/lukasz/Swift-Exprymenty/BareMetalESP32/esp32c6-bare-experiment-ledc/.build/riscv32-none-none-eabi/debug/Application.build/PWMLed.swift.o:($es32_swift_stdlib_Hashing_parameters_WZ)
>>> referenced by <compiler-generated>:0 (/<compiler-generated>:0)
>>> /Users/lukasz/Swift-Exprymenty/BareMetalESP32/esp32c6-bare-experiment-ledc/.build/riscv32-none-none-eabi/debug/Application.build/PWMLed.swift.o:($es32_swift_stdlib_Hashing_parameters_WZ)
enums doesn't work at all. If I use Int32 it works perfect
struct PWMLed {
let pin: Int
let channel: UInt32
let timer: UInt32
let mode: UInt32
let maxDuty: UInt32
init?(
pin: Int,
channel: UInt32 = 0,
timer: UInt32 = 0,
frequencyHz: UInt = 1000,
mode: UInt32 = 1,
dutyResolution: UInt32 = 13
) {
self.pin = pin
self.channel = channel
self.timer = timer
self.mode = mode
self.maxDuty = (1 << dutyResolution) - 1
configTimer()
configChannel()
}
in such a bare metal way enums seems to not work at all. Or they somehow does?