Skip to content

[stm32-lvgl] Fix an interrupt safety bug that caused occasional crashes #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion stm32-lvgl/Sources/Application/Interrupts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,23 @@ func SystickTimerISR() {
uptimeInMs += 1
}

// The following interrupt set up is trickier that it looks on the surface. The
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The following interrupt set up is trickier that it looks on the surface. The
// The following interrupt set up is trickier than it looks on the surface. The

// ISR Swift code must be "trivial" directly and transitively, and namely it
// must avoid destroying any heap objects (because we could be inside malloc
// when the interrupt hits). For that, the expectation is that
// lcdInterruptVerticalSyncHandler is only ever set once, and it not changing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// lcdInterruptVerticalSyncHandler is only ever set once, and it not changing
// lcdInterruptVerticalSyncHandler is only ever set once, and it is not changing

// after boot. The code inside the lcdInterruptVerticalSyncHandler closure is
// expected to only perform trivial operations. lcdInterruptVerticalSyncEnabled
// is allowed to change.

var lcdInterruptVerticalSyncHandler: (() -> Void)? = nil
var lcdInterruptVerticalSyncEnabled: Bool = false

@_cdecl("LtdcIntHandlerISR")
func LtdcIntHandlerISR() {
let sr = ltdc.isr.read()
ltdc.icr.write { $0.storage = sr.storage }
if sr.raw.rrif != 0 {
lcdInterruptVerticalSyncHandler?()
if lcdInterruptVerticalSyncEnabled { lcdInterruptVerticalSyncHandler?() }
}
}
12 changes: 7 additions & 5 deletions stm32-lvgl/Sources/Application/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ struct Main {
// interrupt handler. Once we get our VBI, we can tell
// LVGL that we're good to go to switch again.
Lcd.setFrameBuffer(bufferToShow!)
lcdInterruptVerticalSyncHandler = {
lv_display_flush_ready(disp)
lcdInterruptVerticalSyncHandler = nil
}
lcdInterruptVerticalSyncEnabled = true
Lcd.reloadConfiguration()
// the lv_display_flush_ready() will happen in the LCD frame interrupt.
})

lcdInterruptVerticalSyncHandler = {
lv_display_flush_ready(disp)
lcdInterruptVerticalSyncEnabled = false
}

let touch = lv_indev_create()
lv_indev_set_type(touch, LV_INDEV_TYPE_POINTER)
lv_indev_set_read_cb(
Expand All @@ -152,7 +154,7 @@ struct Main {

while true {
// If we're pending a render, wait.
while lcdInterruptVerticalSyncHandler != nil { /* busy wait */ nop() }
while lcdInterruptVerticalSyncEnabled { /* busy wait */ nop() }

lv_timer_handler()

Expand Down
3 changes: 2 additions & 1 deletion stm32-lvgl/toolset.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"-llvgl", "-llvgl_demos",
"-static",
"-e", "_start_elf",
"--orphan-handling=error"
"--orphan-handling=error",
"-Map", ".build/armv7em-none-none-eabi/release/Application.map"
]
}
}
Loading