esp-hub75 0.14.0

A Rust driver for HUB75 LED matrix displays on ESP32 microcontrollers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//! Shared ISR-driven BCM refresh infrastructure.
//!
//! This module is compiled for all platforms that use an interrupt-driven
//! display refresh loop (ESP32 via I2S Parallel, ESP32-S3 via LCD_CAM,
//! ESP32-C5/C6 via PARL_IO). It contains the transfer state machine, the
//! interrupt handler, and the public [`Hub75`] driver handle with `swap()` API.

use core::cell::RefCell;
use core::sync::atomic::AtomicBool;
use core::sync::atomic::AtomicU32;
use core::sync::atomic::Ordering;
use core::task::Waker;

use critical_section::Mutex;
use esp_hal::handler;
#[cfg(feature = "iram")]
use esp_hal::ram;
use esp_hal::Blocking;

#[cfg(not(feature = "circular-dma"))]
use crate::bcm::linear::BcmBuf;
#[cfg(not(feature = "circular-dma"))]
use crate::bcm::planes_from_fb;
#[cfg(not(feature = "circular-dma"))]
use crate::bcm::PlaneInfo;
#[cfg(feature = "circular-dma")]
use crate::bcm::circular::CircularBcmBuf;
use crate::framebuffer::FrameBuffer;
#[cfg(all(hub75_use_lcd_cam, not(feature = "circular-dma")))]
use crate::framebuffer::WordSize;
use crate::Hub75Error;

// ---------------------------------------------------------------------------
// Platform-specific type aliases
// ---------------------------------------------------------------------------

#[cfg(all(hub75_use_i2s_parallel, not(feature = "circular-dma")))]
pub(crate) type TxDriver = esp_hal::i2s::parallel::I2sParallel<'static, Blocking>;

#[cfg(all(hub75_use_i2s_parallel, not(feature = "circular-dma")))]
pub(crate) type TxTransfer = esp_hal::i2s::parallel::I2sParallelTransfer<'static, BcmBuf, Blocking>;

#[cfg(all(hub75_use_i2s_parallel, feature = "circular-dma"))]
pub(crate) type TxTransfer =
    esp_hal::i2s::parallel::I2sParallelTransfer<'static, CircularBcmBuf, Blocking>;

#[cfg(all(hub75_use_parl_io, not(feature = "circular-dma")))]
pub(crate) type TxDriver = esp_hal::parl_io::ParlIoTx<'static, Blocking>;

#[cfg(all(hub75_use_parl_io, not(feature = "circular-dma")))]
pub(crate) type TxTransfer = esp_hal::parl_io::ParlIoTxTransfer<'static, BcmBuf, Blocking>;

#[cfg(all(hub75_use_parl_io, feature = "circular-dma"))]
pub(crate) type TxTransfer =
    esp_hal::parl_io::ParlIoTxTransfer<'static, CircularBcmBuf, Blocking>;

#[cfg(all(hub75_use_lcd_cam, not(feature = "circular-dma")))]
pub(crate) type TxDriver = esp_hal::lcd_cam::lcd::i8080::I8080<'static, Blocking>;

#[cfg(all(hub75_use_lcd_cam, not(feature = "circular-dma")))]
pub(crate) type TxTransfer =
    esp_hal::lcd_cam::lcd::i8080::I8080Transfer<'static, BcmBuf, Blocking>;

#[cfg(all(hub75_use_lcd_cam, feature = "circular-dma"))]
pub(crate) type TxTransfer =
    esp_hal::lcd_cam::lcd::i8080::I8080Transfer<'static, CircularBcmBuf, Blocking>;

// ---------------------------------------------------------------------------
// ISR shared state
// ---------------------------------------------------------------------------

#[cfg(not(feature = "circular-dma"))]
pub(crate) enum TransferPhase {
    Idle(TxDriver, BcmBuf),
    InFlight(TxTransfer),
    Error(Hub75Error, TxDriver, BcmBuf),
    Transitioning,
}

#[cfg(not(feature = "circular-dma"))]
pub(crate) struct IsrState {
    pub(crate) transfer: TransferPhase,
    #[cfg(hub75_use_lcd_cam)]
    pub(crate) word_size: crate::framebuffer::WordSize,

    pub(crate) current_fb_ptr: *const (),
    pub(crate) pending_planes: Option<PlaneInfo>,
    pub(crate) pending_fb_ptr: *const (),
}

#[cfg(not(feature = "circular-dma"))]
// SAFETY: All access is serialised by `critical_section`, which disables
// interrupts and acquires a cross-core spinlock on multi-core chips.
// The raw pointers (`current_fb_ptr`, `pending_fb_ptr`)
// are only dereferenced inside `critical_section::with` blocks, so they
// are never accessed concurrently from multiple cores.
unsafe impl Send for IsrState {}

#[cfg(not(feature = "circular-dma"))]
type SharedIsrState = Mutex<RefCell<Option<IsrState>>>;

#[cfg(not(feature = "circular-dma"))]
static ISR_STATE: SharedIsrState = Mutex::new(RefCell::new(None));

// ---------------------------------------------------------------------------
// Circular-DMA shared state
// ---------------------------------------------------------------------------

#[cfg(feature = "circular-dma")]
pub(crate) struct CircularState {
    #[allow(dead_code)] // kept alive to prevent DMA from stopping
    pub(crate) transfer: Option<TxTransfer>,
    pub(crate) descriptors: *mut esp_hal::dma::DmaDescriptor,
    pub(crate) desc_count: usize,
    pub(crate) current_fb_ptr: *const (),
}

#[cfg(feature = "circular-dma")]
// SAFETY: Same justification as IsrState — all access serialised by
// critical_section.
unsafe impl Send for CircularState {}

#[cfg(feature = "circular-dma")]
type SharedCircularState = Mutex<RefCell<Option<CircularState>>>;

#[cfg(feature = "circular-dma")]
static CIRCULAR_STATE: SharedCircularState = Mutex::new(RefCell::new(None));

// ---------------------------------------------------------------------------
// Swap signalling
// ---------------------------------------------------------------------------

static SWAP_DONE: AtomicBool = AtomicBool::new(false);
#[cfg(not(feature = "circular-dma"))]
static HAS_ERROR: AtomicBool = AtomicBool::new(false);
static SWAP_WAKER: Mutex<RefCell<Option<Waker>>> = Mutex::new(RefCell::new(None));
static FRAME_COUNT: AtomicU32 = AtomicU32::new(0);

#[cfg(feature = "circular-dma")]
#[allow(clippy::type_complexity)]
static CLEAR_INTERRUPT: Mutex<RefCell<Option<fn()>>> = Mutex::new(RefCell::new(None));

fn signal_swap_done(cs: critical_section::CriticalSection) {
    SWAP_DONE.store(true, Ordering::Release);
    if let Some(waker) = SWAP_WAKER.borrow_ref_mut(cs).take() {
        waker.wake();
    }
}

// ---------------------------------------------------------------------------
// Platform-specific transfer helpers (non-circular only)
// ---------------------------------------------------------------------------

#[cfg(all(hub75_use_i2s_parallel, not(feature = "circular-dma")))]
#[cfg_attr(feature = "iram", ram)]
fn start_transfer(tx: TxDriver, buf: BcmBuf) -> Result<TxTransfer, (Hub75Error, TxDriver, BcmBuf)> {
    tx.send(buf)
        .map_err(|(err, tx, buf)| (Hub75Error::Dma(err), tx, buf))
}

#[cfg(all(hub75_use_i2s_parallel, not(feature = "circular-dma")))]
#[cfg_attr(feature = "iram", ram)]
fn finish_transfer(xfer: TxTransfer) -> (Result<(), Hub75Error>, TxDriver, BcmBuf) {
    let (tx, buf) = xfer.wait();
    (Ok(()), tx, buf)
}

/// On ESP32-C5, the GDMA EOF signal is generated by the DMA channel rather
/// than the PARL_IO byte counter, so the transfer-length field is unused.
#[cfg(all(hub75_use_parl_io, not(feature = "circular-dma"), esp32c5))]
const PARL_IO_DUMMY_TRANSFER_LEN: usize = 0;

#[cfg(all(hub75_use_parl_io, not(feature = "circular-dma")))]
#[cfg_attr(feature = "iram", ram)]
fn start_transfer(tx: TxDriver, buf: BcmBuf) -> Result<TxTransfer, (Hub75Error, TxDriver, BcmBuf)> {
    #[cfg(esp32c5)]
    let transfer_len = PARL_IO_DUMMY_TRANSFER_LEN;
    #[cfg(not(esp32c5))]
    let transfer_len = buf.current_transfer_len();

    tx.write(transfer_len, buf)
        .map_err(|(err, tx, buf)| (Hub75Error::ParlIo(err), tx, buf))
}

#[cfg(all(hub75_use_parl_io, not(feature = "circular-dma")))]
#[cfg_attr(feature = "iram", ram)]
fn finish_transfer(xfer: TxTransfer) -> (Result<(), Hub75Error>, TxDriver, BcmBuf) {
    let (result, tx, buf) = xfer.wait();
    (result.map_err(Hub75Error::Dma), tx, buf)
}

#[cfg(all(hub75_use_lcd_cam, not(feature = "circular-dma")))]
#[cfg_attr(feature = "iram", ram)]
fn start_transfer(
    tx: TxDriver,
    buf: BcmBuf,
    word_size: WordSize,
) -> Result<TxTransfer, (Hub75Error, TxDriver, BcmBuf)> {
    use esp_hal::lcd_cam::lcd::i8080::Command;

    let result = match word_size {
        WordSize::Eight => tx.send(Command::<u8>::None, 0, buf),
        WordSize::Sixteen => tx.send(Command::<u16>::None, 0, buf),
    };
    result.map_err(|(err, tx, buf)| (Hub75Error::Dma(err), tx, buf))
}

#[cfg(all(hub75_use_lcd_cam, not(feature = "circular-dma")))]
#[cfg_attr(feature = "iram", ram)]
fn finish_transfer(xfer: TxTransfer) -> (Result<(), Hub75Error>, TxDriver, BcmBuf) {
    let (result, tx, buf) = xfer.wait();
    (result.map_err(Hub75Error::Dma), tx, buf)
}

// ---------------------------------------------------------------------------
// Interrupt handler (non-circular)
// ---------------------------------------------------------------------------

#[cfg(not(feature = "circular-dma"))]
#[handler]
#[cfg_attr(feature = "iram", ram)]
pub(crate) fn hub75_isr() {
    critical_section::with(|cs| {
        let mut borrow = ISR_STATE.borrow_ref_mut(cs);
        let state = match borrow.as_mut() {
            Some(s) => s,
            None => return,
        };

        let xfer = match core::mem::replace(&mut state.transfer, TransferPhase::Transitioning) {
            TransferPhase::InFlight(xfer) => xfer,
            other => {
                state.transfer = other;
                return;
            }
        };

        // .wait() returns instantly — the interrupt already fired.
        let (result, tx, mut buf) = finish_transfer(xfer);

        if let Err(err) = result {
            state.transfer = TransferPhase::Error(err, tx, buf);
            HAS_ERROR.store(true, Ordering::Release);
            signal_swap_done(cs);
            return;
        }

        let frame_boundary = buf.advance();

        if frame_boundary {
            FRAME_COUNT.fetch_add(1, Ordering::Relaxed);

            if let Some(pending) = state.pending_planes.take() {
                state.current_fb_ptr = state.pending_fb_ptr;
                state.pending_fb_ptr = core::ptr::null();
                buf.update_planes(pending);
                signal_swap_done(cs);
            }
        }

        #[cfg(hub75_use_lcd_cam)]
        let xfer_result = start_transfer(tx, buf, state.word_size);
        #[cfg(not(hub75_use_lcd_cam))]
        let xfer_result = start_transfer(tx, buf);

        match xfer_result {
            Ok(new_xfer) => {
                state.transfer = TransferPhase::InFlight(new_xfer);
            }
            Err((hub_err, tx, buf)) => {
                state.transfer = TransferPhase::Error(hub_err, tx, buf);
                HAS_ERROR.store(true, Ordering::Release);
                signal_swap_done(cs);
            }
        }
    });
}

// ---------------------------------------------------------------------------
// Frame-boundary ISR (circular-dma)
// ---------------------------------------------------------------------------

#[cfg(feature = "circular-dma")]
#[handler]
#[cfg_attr(feature = "iram", ram)]
pub(crate) fn hub75_frame_count_isr() {
    critical_section::with(|cs| {
        if let Some(clear) = CLEAR_INTERRUPT.borrow_ref(cs).as_ref() {
            (clear)();
        }
        FRAME_COUNT.fetch_add(1, Ordering::Relaxed);
        signal_swap_done(cs);
    });
}

// ---------------------------------------------------------------------------
// ISR state initialisation (called by platform constructors)
// ---------------------------------------------------------------------------

#[cfg(all(not(hub75_use_lcd_cam), not(feature = "circular-dma")))]
pub(crate) fn init_isr_state(tx: TxDriver, buf: BcmBuf) {
    critical_section::with(|cs| {
        *ISR_STATE.borrow_ref_mut(cs) = Some(IsrState {
            transfer: TransferPhase::Idle(tx, buf),
            current_fb_ptr: core::ptr::null(),
            pending_planes: None,
            pending_fb_ptr: core::ptr::null(),
        });
    });
}

#[cfg(all(hub75_use_lcd_cam, not(feature = "circular-dma")))]
pub(crate) fn init_isr_state(tx: TxDriver, buf: BcmBuf, word_size: WordSize) {
    critical_section::with(|cs| {
        *ISR_STATE.borrow_ref_mut(cs) = Some(IsrState {
            transfer: TransferPhase::Idle(tx, buf),
            word_size,
            current_fb_ptr: core::ptr::null(),
            pending_planes: None,
            pending_fb_ptr: core::ptr::null(),
        });
    });
}

// ---------------------------------------------------------------------------
// Circular-DMA state storage (called by platform constructors after starting DMA)
// ---------------------------------------------------------------------------

#[cfg(feature = "circular-dma")]
pub(crate) fn store_circular_state(
    xfer: TxTransfer,
    desc_ptr: *mut esp_hal::dma::DmaDescriptor,
    desc_count: usize,
    fb_ptr: *const (),
) {
    critical_section::with(|cs| {
        *CIRCULAR_STATE.borrow_ref_mut(cs) = Some(CircularState {
            transfer: Some(xfer),
            descriptors: desc_ptr,
            desc_count,
            current_fb_ptr: fb_ptr,
        });
    });
}

#[cfg(feature = "circular-dma")]
pub(crate) fn store_clear_interrupt(clear: fn()) {
    critical_section::with(|cs| {
        *CLEAR_INTERRUPT.borrow_ref_mut(cs) = Some(clear);
    });
}

// ---------------------------------------------------------------------------
// Public driver handle
// ---------------------------------------------------------------------------

/// HUB75 display controller driven by an interrupt-based BCM refresh loop.
///
/// Created via [`Hub75::new`] (blocking) or [`Hub75::new_async`] (async).
/// The constructor configures the peripheral, applies pin assignments, and
/// immediately starts DMA-driven display refresh with the provided
/// framebuffer.
///
/// The pin configuration's [`Hub75Pins::Word`](crate::Hub75Pins) type must
/// match the framebuffer's
/// [`FrameBuffer::Word`](crate::framebuffer::FrameBuffer::Word) — mismatches
/// are caught at compile time.
///
/// # Type Parameters
///
/// * `DM` — Driver mode ([`Blocking`](esp_hal::Blocking) or
///   [`Async`](esp_hal::Async)).
/// * `FB` — The concrete framebuffer type.
///
/// # Buffer Swapping
///
/// Call [`swap()`](Hub75::swap) to exchange framebuffers. It returns a
/// [`Hub75Swap`] transfer object that can be waited on:
/// - [`Hub75Swap::wait()`] — spin-loops until the DMA is guaranteed to no
///   longer read from the old buffer, then returns it.
/// - [`Hub75Swap::wait_for_done()`] — yields to the executor (async contexts).
///   Call [`Hub75Swap::wait()`] afterwards to get the result.
/// - [`Hub75Swap::is_done()`] — non-blocking completion check.
///
/// # Limitations
///
/// Only **one** `Hub75` instance may exist at a time. The driver uses
/// module-level statics for the ISR state machine, so creating a second
/// instance would overwrite the first.
///
/// `Hub75` does not implement [`Drop`]. The ISR-driven display refresh is
/// designed to run for the lifetime of the program.
pub struct Hub75<DM: esp_hal::DriverMode, FB> {
    _dm: core::marker::PhantomData<DM>,
    _fb: core::marker::PhantomData<fn() -> FB>,
    _not_sync: core::marker::PhantomData<*const ()>,
}

// SAFETY: Hub75 is a zero-sized handle; all mutable state lives in statics
// guarded by critical_section, so it is safe to send across threads.
// Hub75 is intentionally `!Sync` because concurrent `swap()` calls from
// multiple threads would race on the shared ISR state.
unsafe impl<DM: esp_hal::DriverMode, FB> Send for Hub75<DM, FB> {}

impl<DM: esp_hal::DriverMode, FB> Hub75<DM, FB> {
    pub(crate) fn from_phantom() -> Self {
        Self {
            _dm: core::marker::PhantomData,
            _fb: core::marker::PhantomData,
            _not_sync: core::marker::PhantomData,
        }
    }
}

impl<DM: esp_hal::DriverMode, FB: FrameBuffer + 'static> Hub75<DM, FB> {
    /// Restart display refresh after an error.
    ///
    /// Callable after [`Hub75::swap`] returned an error. Sets up the BCM
    /// plane data and kicks off the first DMA transfer with the same
    /// framebuffer type.
    ///
    /// This method is only available when `circular-dma` is **not** enabled.
    /// In circular-DMA mode the DMA engine never stops, so there is no
    /// restart path.
    ///
    /// # Panics
    ///
    /// Panics if called while a transfer is already in flight.
    #[cfg(not(feature = "circular-dma"))]
    pub fn restart(&self, fb: &'static FB) -> Result<(), Hub75Error> {
        start_internal(fb)
    }

    /// Returns the number of complete BCM frames rendered since driver
    /// creation.
    pub fn frame_count(&self) -> u32 {
        FRAME_COUNT.load(Ordering::Relaxed)
    }
}

#[cfg(not(feature = "circular-dma"))]
pub(crate) fn start_internal(fb: &'static impl FrameBuffer) -> Result<(), Hub75Error> {
    let planes = planes_from_fb(fb);
    let plane_count = fb.plane_count();

    critical_section::with(|cs| {
        let mut borrow = ISR_STATE.borrow_ref_mut(cs);
        let state = borrow.as_mut().expect("Hub75 not initialised");

        let (tx, mut buf) =
            match core::mem::replace(&mut state.transfer, TransferPhase::Transitioning) {
                TransferPhase::Idle(tx, buf) | TransferPhase::Error(_, tx, buf) => (tx, buf),
                other => {
                    state.transfer = other;
                    panic!("start() called while already running");
                }
            };

        buf.reset_with_planes(planes, plane_count);
        state.current_fb_ptr = fb as *const _ as *const ();
        state.pending_planes = None;
        state.pending_fb_ptr = core::ptr::null();

        #[cfg(hub75_use_lcd_cam)]
        let xfer_result = start_transfer(tx, buf, state.word_size);
        #[cfg(not(hub75_use_lcd_cam))]
        let xfer_result = start_transfer(tx, buf);

        match xfer_result {
            Ok(xfer) => {
                state.transfer = TransferPhase::InFlight(xfer);
                HAS_ERROR.store(false, Ordering::Release);
                SWAP_DONE.store(false, Ordering::Relaxed);
                Ok(())
            }
            Err((hub_err, tx, buf)) => {
                state.transfer = TransferPhase::Error(hub_err, tx, buf);
                HAS_ERROR.store(true, Ordering::Release);
                Err(hub_err)
            }
        }
    })
}

// ---------------------------------------------------------------------------
// Hub75Swap — transfer object returned by swap()
// ---------------------------------------------------------------------------

/// A pending framebuffer swap.
///
/// Returned by [`Hub75::swap`]. The old framebuffer is not safe to reuse until
/// the DMA is guaranteed to no longer be reading from it. Call
/// [`wait_for_done()`](Self::wait_for_done) (async) to yield until safe, then
/// [`wait()`](Self::wait) to obtain the old framebuffer. Or call `wait()`
/// directly for a blocking spin-loop.
///
/// In non-circular mode, "safe" means the ISR has hit a frame boundary and
/// completed the swap. In circular-DMA mode, "safe" means at least one
/// `suc_eof` interrupt has fired after the pointer update, guaranteeing the
/// DMA has completed a full pass and is reading exclusively from the new buffer.
pub struct Hub75Swap<FB: 'static> {
    old_fb_ptr: *mut FB,
    #[cfg(not(feature = "circular-dma"))]
    new_fb_ptr: *mut FB,
}

// SAFETY: The raw pointer always originates from a `&'static mut FB`. Only
// one `Hub75Swap` exists at a time (enforced by the single-instance Hub75
// constraint and the fact that `swap()` takes `&self` on a `!Sync` type).
unsafe impl<FB: 'static> Send for Hub75Swap<FB> {}

impl<FB: FrameBuffer + 'static> Hub75Swap<FB> {
    /// Check whether the swap is complete without blocking.
    ///
    /// Returns `true` once the DMA is guaranteed to no longer be reading
    /// from the old framebuffer.
    pub fn is_done(&self) -> bool {
        #[cfg(not(feature = "circular-dma"))]
        {
            SWAP_DONE.load(Ordering::Acquire) || HAS_ERROR.load(Ordering::Acquire)
        }
        #[cfg(feature = "circular-dma")]
        {
            SWAP_DONE.load(Ordering::Acquire)
        }
    }

    /// Spin-loops until the DMA is guaranteed to no longer be reading from
    /// the old framebuffer, then returns it for reuse.
    ///
    /// In non-circular mode this waits for the next frame boundary. In
    /// circular-DMA mode this waits for one complete frame to be rendered
    /// after the pointer update.
    ///
    /// If [`wait_for_done()`](Self::wait_for_done) was already awaited, this
    /// returns immediately.
    #[cfg(not(feature = "circular-dma"))]
    pub fn wait(self) -> Result<&'static mut FB, (Hub75Error, &'static mut FB)> {
        loop {
            if HAS_ERROR.load(Ordering::Acquire) {
                return critical_section::with(|cs| {
                    let mut borrow = ISR_STATE.borrow_ref_mut(cs);
                    let state = borrow.as_mut().unwrap();
                    state.pending_planes = None;
                    state.pending_fb_ptr = core::ptr::null();
                    let err = match &state.transfer {
                        TransferPhase::Error(err, _, _) => *err,
                        _ => Hub75Error::Dma(esp_hal::dma::DmaError::DescriptorError),
                    };
                    Err((err, unsafe { &mut *self.new_fb_ptr }))
                });
            }
            if SWAP_DONE.load(Ordering::Acquire) {
                break;
            }
            core::hint::spin_loop();
        }
        Ok(unsafe { &mut *self.old_fb_ptr })
    }

    /// Spin-loops until the DMA is guaranteed to no longer be reading from
    /// the old framebuffer, then returns it for reuse.
    ///
    /// If [`wait_for_done()`](Self::wait_for_done) was already awaited, this
    /// returns immediately.
    #[cfg(feature = "circular-dma")]
    pub fn wait(self) -> Result<&'static mut FB, (Hub75Error, &'static mut FB)> {
        while !SWAP_DONE.load(Ordering::Acquire) {
            core::hint::spin_loop();
        }
        Ok(unsafe { &mut *self.old_fb_ptr })
    }

    /// Yields to the executor until the swap is complete.
    ///
    /// After this resolves, call [`wait()`](Self::wait) to obtain the old
    /// framebuffer. This mirrors the esp-hal transfer pattern:
    ///
    /// ```rust,ignore
    /// let mut xfer = hub75.swap(fb)?;
    /// xfer.wait_for_done().await;
    /// fb = xfer.wait()?;
    /// ```
    #[cfg(not(feature = "circular-dma"))]
    pub async fn wait_for_done(&mut self) {
        core::future::poll_fn(|cx| {
            if SWAP_DONE.load(Ordering::Acquire) || HAS_ERROR.load(Ordering::Acquire) {
                return core::task::Poll::Ready(());
            }
            critical_section::with(|cs| {
                if SWAP_DONE.load(Ordering::Relaxed) || HAS_ERROR.load(Ordering::Relaxed) {
                    return core::task::Poll::Ready(());
                }
                *SWAP_WAKER.borrow_ref_mut(cs) = Some(cx.waker().clone());
                core::task::Poll::Pending
            })
        })
        .await;
    }

    /// Yields to the executor until the swap is complete.
    ///
    /// After this resolves, call [`wait()`](Self::wait) to obtain the old
    /// framebuffer.
    #[cfg(feature = "circular-dma")]
    pub async fn wait_for_done(&mut self) {
        core::future::poll_fn(|cx| {
            if SWAP_DONE.load(Ordering::Acquire) {
                return core::task::Poll::Ready(());
            }
            critical_section::with(|cs| {
                if SWAP_DONE.load(Ordering::Relaxed) {
                    return core::task::Poll::Ready(());
                }
                *SWAP_WAKER.borrow_ref_mut(cs) = Some(cx.waker().clone());
                core::task::Poll::Pending
            })
        })
        .await;
    }
}

// ---------------------------------------------------------------------------
// Swap (non-circular)
// ---------------------------------------------------------------------------

#[cfg(not(feature = "circular-dma"))]
impl<DM: esp_hal::DriverMode, FB: FrameBuffer + 'static> Hub75<DM, FB> {
    /// Initiate a framebuffer swap.
    ///
    /// Registers `new_fb` as the pending buffer. The actual swap occurs at
    /// the next frame boundary (handled by the ISR). Returns a [`Hub75Swap`]
    /// transfer object — call [`.wait_for_done()`](Hub75Swap::wait_for_done)
    /// then [`.wait()`](Hub75Swap::wait), or just `.wait()` directly for
    /// blocking.
    ///
    /// # Panics
    ///
    /// Panics if the driver has not been initialised (no `Hub75` instance
    /// was created).
    pub fn swap(&self, new_fb: &'static mut FB) -> Hub75Swap<FB> {
        let new_planes = planes_from_fb(new_fb);
        let new_fb_ptr = new_fb as *mut FB;

        let old_fb_ptr = critical_section::with(|cs| {
            let mut borrow = ISR_STATE.borrow_ref_mut(cs);
            let state = borrow.as_mut().expect("Hub75 not initialised");
            let old = state.current_fb_ptr;
            state.pending_planes = Some(new_planes);
            state.pending_fb_ptr = new_fb_ptr as *const ();
            SWAP_DONE.store(false, Ordering::Relaxed);
            old
        });

        Hub75Swap {
            old_fb_ptr: old_fb_ptr as *mut FB,
            new_fb_ptr,
        }
    }
}

// ---------------------------------------------------------------------------
// Swap (circular-dma)
// ---------------------------------------------------------------------------

#[cfg(feature = "circular-dma")]
impl<DM: esp_hal::DriverMode, FB: FrameBuffer + 'static> Hub75<DM, FB> {
    /// Initiate a framebuffer swap (circular-DMA mode).
    ///
    /// Updates all DMA descriptor buffer pointers immediately and returns a
    /// [`Hub75Swap`] transfer object. The DMA engine's internal register may
    /// still be pointing into the old buffer for the currently in-flight
    /// descriptor. Call [`.wait_for_done()`](Hub75Swap::wait_for_done) then
    /// [`.wait()`](Hub75Swap::wait), or just `.wait()` directly for blocking.
    ///
    /// # Panics
    ///
    /// Panics if the driver has not been initialised (no `Hub75` instance
    /// was created).
    pub fn swap(&self, new_fb: &'static mut FB) -> Hub75Swap<FB> {
        let new_fb_ptr = new_fb as *mut FB;
        let old_fb_ptr = critical_section::with(|cs| {
            let mut borrow = CIRCULAR_STATE.borrow_ref_mut(cs);
            let state = borrow.as_mut().expect("Hub75 not initialised");
            let delta = new_fb_ptr as isize - state.current_fb_ptr as isize;
            // SAFETY: `descriptors` points to a `&'static mut` descriptor
            // array that outlives everything. The DMA engine reads descriptor
            // fields concurrently while we update `buffer` pointers here.
            // This is safe because:
            //  1. Each `buffer` field is a naturally-aligned 32-bit pointer.
            //     On Xtensa (ESP32/S3) aligned 32-bit stores are atomic with
            //     respect to the DMA bus master, so the DMA never observes a
            //     half-written pointer value.
            //  2. `circular-dma` is blocked at compile time on RISC-V targets
            //     (ESP32-C5/C6) where PARL_IO does not support circular chains.
            //  3. The pointer delta is valid because all plane data resides
            //     within a single contiguous `FB` allocation — both old and
            //     new framebuffers have identical internal layout.
            //  4. The worst-case visual artifact is one partially-mixed frame
            //     (tearing), which is acceptable for a display use-case.
            unsafe {
                for i in 0..state.desc_count {
                    let desc = &mut *state.descriptors.add(i);
                    desc.buffer = desc.buffer.wrapping_byte_offset(delta);
                }
            }
            let old_ptr = state.current_fb_ptr;
            state.current_fb_ptr = new_fb_ptr as *const ();
            SWAP_DONE.store(false, Ordering::Release);
            old_ptr
        });
        Hub75Swap {
            old_fb_ptr: old_fb_ptr as *mut FB,
        }
    }
}