Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9567DC5B567 for ; Wed, 12 Aug 2026 06:32:53 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wu2W9-0001sy-Lh; Wed, 12 Aug 2026 02:32:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <163wangjack@gmail.com>) id 1wu2Vp-0000zg-Pr for qemu-devel@nongnu.org; Wed, 12 Aug 2026 02:32:34 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <163wangjack@gmail.com>) id 1wu2Vk-00047m-RB for qemu-devel@nongnu.org; Wed, 12 Aug 2026 02:32:29 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-2cacb8416a1so8172675ad.1 for ; Tue, 11 Aug 2026 23:32:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1786516343; x=1787121143; darn=nongnu.org; h=references:to:cc:in-reply-to:date:subject:mime-version:content-type :message-id:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=+BHWnBbixY8f05f9HpuT54nzFAEjgiYRdNAZSYh9faI=; b=sFxvKQ4sj8V0r6El+2GTDbczBsIn8QYjPR/iTnVcsdmDRm0TBzVzDaMmwzqecrM3Yq 5gsNfd4PQCALeCRmklAy3+yzZFk2wBBVslpA8Dq2XuT3X3Z/a3VjVl1snLmU0T0ny/uj HANIGVtjgmgtG6HVLSIBcWJHvmWXG04g+ESOswfixOGFfUoK94FasPkrdVmJ1txdmFoS 1CM4p1CQJkSrSsW5vTVi5x5cPqIl2qWax1eA9F58lM+7WoThihGmN39IAheSXZEHcb3V QOofCdgzxjqtHFWqxefPk1G92RoG2iKDFFJlAEYbZEL7umLb8WEG8uVTVcq89dHyc3GJ DpnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786516343; x=1787121143; h=references:to:cc:in-reply-to:date:subject:mime-version:content-type :message-id:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+BHWnBbixY8f05f9HpuT54nzFAEjgiYRdNAZSYh9faI=; b=Cx8l4qyHWlf2nJJ1DuTVxEEwotrWDeZmgRLGx26uxIpE3ITK07QlxZjIr0yQjojl+c 0ex9ICORCgtePdCo87105qsn1ZmhAa86tzyU99vw1y3dtrbK6XnehyKx4se5IkN724bT e2Vf8iRQ6ADuuQetPzkSRS99FGIVP9cW7xWHTpb1I1UPFsMht/GaLV99paoW4RWaY51c hNSDcVspszIocg+mV5UD/p+JYUzFIrNHlGM5jli/wrCRaITN8uPka2c127mobGymbgSt +MoZtroeSy09LWzcoIqhL/oIxHo9saMAh8udoP0mmbiqoR9k/tO1WA5a5LsG2B+KRj6R tKPg== X-Gm-Message-State: AOJu0Ywux0tGpRzEPn2wXgzaPWLa8+jrZU5YE8jIMGQbYlgLqf7C3jmE 5uZB1vqAvDkvZVFnCuJoaWJiGYBbnw8I7sah1sLJAmMxgI/07F14f/1szgwGJZyT X-Gm-Gg: AR+sD12TpY2W64J4vEZKAOq+Cuw71WBBK0vFOc541HM/67onre7jq1Slfled2e8A3wd ygHZefQKUPNkbcL/bSFcRKoxdviOtxhL/8k0mJ/g42DR3tuPELlvDjeJw2+vdqLaJRXsOtkdL6i uLuBE5rU34aWp5zwnF6aZMCBiEVpYSkWWDZqDNUZAE9AnJQ3kggl5zhhmMTX9ml08lO4w1X2y89 nO7dQOQLeZimHd2UldfCTxTvqUa7jacNT7nQ6C7B/tireg63/dmTh7UgVocNsx9nYgi+M4X90dJ Qt+Lcf8mfQ7WlrZNTTfm0M8Ntq8Me1Wn61xWhQpCggH3uagn1vWpNWkkNPV+LXNDcXhc+hIsFlz pntr54wSknsWbVOaoVGQmBbNN9vSWZUxJ6xyK/jPHmwEKloIcqc2ygNRo5t7XnG9xw9THA7/HmZ da7LoMzqPbCaEsuxUtFZouvrSRg6tM0JPOGynr5H3el1hwkDk64xFXz8B8OxDkCWylkQ== X-Received: by 2002:a17:902:ec8a:b0:2c0:e2ea:6b0c with SMTP id d9443c01a7336-2d3456dfed4mr31825545ad.21.1786516343206; Tue, 11 Aug 2026 23:32:23 -0700 (PDT) Received: from smtpclient.apple ([82.40.42.115]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d350ff0772sm2055865ad.21.2026.08.11.23.32.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Aug 2026 23:32:22 -0700 (PDT) From: Jack wang <163wangjack@gmail.com> Message-Id: <776B3C22-7612-401E-B7F2-5658A942DEC8@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_6A6F271E-A998-4CFE-A70F-ECAC3DAAFC24" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Re: [RFC PATCH v1 5/5] rust/hw/gpio: add native unit tests Date: Wed, 12 Aug 2026 14:31:59 +0800 In-Reply-To: <20260726145119.19881-6-163wangjack@gmail.com> Cc: Paolo Bonzini , Peter Maydell , Alistair Francis , Fabiano Rosas , Laurent Vivier , qemu-arm@nongnu.org, qemu-rust@nongnu.org, caojunze424@gmail.com To: qemu-devel@nongnu.org References: <20260726145119.19881-1-163wangjack@gmail.com> <20260726145119.19881-6-163wangjack@gmail.com> X-Mailer: Apple Mail (2.3864.600.51.1.1) Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=163wangjack@gmail.com; helo=mail-pl1-x62f.google.com X-Spam_score_int: -13 X-Spam_score: -1.4 X-Spam_bar: - X-Spam_report: (-1.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, FROM_STARTS_WITH_NUMS=0.738, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org --Apple-Mail=_6A6F271E-A998-4CFE-A70F-ECAC3DAAFC24 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi all, A polite ping for this RFC series.Thanks advice for Philippe advice and = I am ready for v2. Could anyone have other suggestions for my patch. I am appreciative for = your advice and ready to fix it in v2 > 2026=E5=B9=B47=E6=9C=8826=E6=97=A5 22:51=EF=BC=8CJack Wang = <163wangjack@gmail.com> =E5=86=99=E9=81=93=EF=BC=9A >=20 > Add native Rust unit tests that link GpioRegisters directly and = exercise > the CRL/CRH decode and IDR update algorithm in-process, without = booting a > machine or pulling in the softmmu memory subsystem. >=20 > Signed-off-by: Jack Wang <163wangjack@gmail.com> > --- > rust/hw/gpio/tests/tests.rs | 191 ++++++++++++++++++++++++++++++++++++ > 1 file changed, 191 insertions(+) > create mode 100644 rust/hw/gpio/tests/tests.rs >=20 > diff --git a/rust/hw/gpio/tests/tests.rs b/rust/hw/gpio/tests/tests.rs > new file mode 100644 > index 0000000000..ca4d5ed4d6 > --- /dev/null > +++ b/rust/hw/gpio/tests/tests.rs > @@ -0,0 +1,191 @@ > +// Native Rust unit tests for the STM32F1xx GPIO register logic. > +// > +// Copyright 2026, Jack Wang > +// Author(s): Jack Wang <163wangjack@gmail.com> > +// SPDX-License-Identifier: GPL-2.0-or-later > +// > +// Unlike tests/qtest/stm32f103_gpio-test.c (which drives the device = over a > +// socket from an external qemu-system-arm process), these tests link = the > +// device crate directly and exercise `GpioRegisters` in-process. = This gives > +// fast, native Rust coverage of the CRL/CRH decode + IDR algorithm = without > +// booting a machine or pulling in the softmmu memory subsystem. > +// > +// `GpioRegisters` deliberately has no QOM object, no MemoryRegion = and no > +// InterruptSource, so it can be constructed with = `GpioRegisters::new()` and > +// called directly. Only the BQL mock (bql::start_test) is required, = because > +// the register cells are `BqlCell`s. > +// > +// SPDX-License-Identifier: GPL-2.0-or-later > + > +use stm32f1xx_gpio::gpio::{ > + GpioRegisters, PinUpdate, REG_BRR, REG_BSRR, REG_CRH, REG_CRL, = REG_IDR, REG_ODR, RESET_CRH, > + RESET_CRL, > +}; > + > +/// Take the mock BQL so `BqlCell` accesses are permitted. = Integration tests > +/// run with `--test-threads=3D1`, so this is safe to call from every = test. > +fn regs() -> GpioRegisters { > + bql::start_test(); > + GpioRegisters::new() > +} > + > +// 4-bit MODE[1:0]+CNF[1:0] pin configurations (RM0008). > +const CFG_INPUT_FLOATING: u32 =3D 0x4; // MODE=3D00 CNF=3D01 (reset = default) > +const CFG_INPUT_PULL: u32 =3D 0x8; // MODE=3D00 CNF=3D10, ODR selects = up/down > +const CFG_OUTPUT_PP: u32 =3D 0x1; // MODE=3D01 CNF=3D00, general = push-pull > + > +/// Set the 4-bit config nibble for `pin` in CRL/CRH via an MMIO = write. > +fn set_config(r: &GpioRegisters, pin: usize, cfg: u32) { > + let reg =3D if pin < 8 { REG_CRL } else { REG_CRH }; > + let shift =3D ((pin % 8) * 4) as u32; > + let old =3D r.read(reg); > + r.write(reg, (old & !(0xF << shift)) | (cfg << shift)); > +} > + > +fn set_odr_bit(r: &GpioRegisters, pin: usize, value: u32) { > + let old =3D r.read(REG_ODR); > + r.write(REG_ODR, (old & !(1 << pin)) | (value << pin)); > +} > + > +/// True if pin `pin` is set in a [`PinUpdate`] and its reported = level is > +/// `level`. > +fn pin_changed_to(upd: PinUpdate, pin: usize, level: bool) -> bool { > + (upd.mask & (1 << pin)) !=3D 0 && (((upd.levels >> pin) & 1) !=3D = 0) =3D=3D level > +} > + > +#[test] > +/// A freshly created register block holds the RM0008 reset values. > +fn test_reset_values() { > + let r =3D regs(); > + assert_eq!(r.read(REG_CRL), RESET_CRL); > + assert_eq!(r.read(REG_CRH), RESET_CRH); > + assert_eq!(r.read(REG_IDR), 0); > + assert_eq!(r.read(REG_ODR), 0); > +} > + > +#[test] > +/// `reset` restores the reset state after the registers are dirtied. > +fn test_reset_restores() { > + let r =3D regs(); > + r.write(REG_CRL, 0xDEAD_BEEF); > + r.write(REG_ODR, 0x0000_FFFF); > + assert_ne!(r.read(REG_CRL), RESET_CRL); > + > + r.reset(); > + > + assert_eq!(r.read(REG_CRL), RESET_CRL); > + assert_eq!(r.read(REG_CRH), RESET_CRH); > + assert_eq!(r.read(REG_IDR), 0); > + assert_eq!(r.read(REG_ODR), 0); > +} > + > +#[test] > +/// Push-pull output: IDR follows ODR; ODR written while input is = latched, > +/// and the out-pin change is reported. > +fn test_output_mode() { > + let r =3D regs(); > + let pin =3D 5; > + > + // Set ODR while still a floating input: IDR unaffected, no out = change. > + let idle =3D set_odr_and_report(&r, pin, 1); > + assert_eq!(r.read(REG_IDR), 0); > + assert_eq!(idle.mask & (1 << pin), 0); > + > + // Switch to push-pull output: pin now follows ODR (high), and = flips. > + let old =3D r.read(REG_CRL); > + let to_output =3D r.write( > + REG_CRL, > + (old & !(0xF << (pin * 4))) | (CFG_OUTPUT_PP << (pin * 4)), > + ); > + assert_eq!(r.read(REG_IDR), 1 << pin); > + assert!(pin_changed_to(to_output, pin, true)); > + > + // Clear ODR bit: pin goes low, and flips back. > + let to_low =3D set_odr_and_report(&r, pin, 0); > + assert_eq!(r.read(REG_IDR), 0); > + assert!(pin_changed_to(to_low, pin, false)); > +} > + > +/// Helper: write one ODR bit and return the resulting [`PinUpdate`]. > +fn set_odr_and_report(r: &GpioRegisters, pin: usize, value: u32) -> = PinUpdate { > + let old =3D r.read(REG_ODR); > + r.write(REG_ODR, (old & !(1 << pin)) | (value << pin)) > +} > + > +#[test] > +/// Floating input: an external drive is reflected in IDR and = reported. > +fn test_input_mode() { > + let r =3D regs(); > + let pin =3D 6usize; > + > + set_config(&r, pin, CFG_INPUT_FLOATING); > + > + let drive_high =3D r.gpio_set(pin, 1); > + assert_eq!(r.read(REG_IDR), 1 << pin); > + assert!(pin_changed_to(drive_high, pin, true)); > + > + let drive_low =3D r.gpio_set(pin, 0); > + assert_eq!(r.read(REG_IDR), 0); > + assert!(pin_changed_to(drive_low, pin, false)); > +} > + > +#[test] > +/// Input pull mode: ODR selects pull-up (IDR=3D1) vs pull-down = (IDR=3D0), > +/// with no external driver connected. > +fn test_pull_up_down() { > + let r =3D regs(); > + let pin =3D 0; > + > + set_odr_bit(&r, pin, 1); > + set_config(&r, pin, CFG_INPUT_PULL); > + assert_eq!(r.read(REG_IDR), 1 << pin); > + > + set_odr_bit(&r, pin, 0); > + assert_eq!(r.read(REG_IDR), 0); > +} > + > +#[test] > +/// BSRR sets/resets ODR bits (set has priority); BRR resets ODR = bits. > +fn test_bsrr_brr() { > + let r =3D regs(); > + let pin =3D 1; > + > + r.write(REG_BSRR, 1 << pin); > + assert_eq!(r.read(REG_ODR), 1 << pin); > + > + r.write(REG_BSRR, 1 << (pin + 16)); > + assert_eq!(r.read(REG_ODR), 0); > + > + r.write(REG_BSRR, 1 << pin); > + r.write(REG_BRR, 1 << pin); > + assert_eq!(r.read(REG_ODR), 0); > + > + // Set has priority over reset within one BSRR write. > + r.write(REG_BSRR, (1 << pin) | (1 << (pin + 16))); > + assert_eq!(r.read(REG_ODR), 1 << pin); > +} > + > +#[test] > +/// A pin driven externally, then reconfigured as push-pull output, = is > +/// disconnected: IDR follows ODR, not the stale external level. > +fn test_push_pull_disconnect() { > + let r =3D regs(); > + let pin =3D 7usize; > + > + // Drive high externally as input. > + set_config(&r, pin, CFG_INPUT_FLOATING); > + r.gpio_set(pin, 1); > + assert_eq!(r.read(REG_IDR), 1 << pin); > + assert_eq!(r.disconnected_pins() & (1 << pin), 0); // now = connected > + > + // Reconfigure as push-pull output with ODR=3D0: pin must drop to = 0, > + // and the external driver must be disconnected. > + set_odr_bit(&r, pin, 0); > + set_config(&r, pin, CFG_OUTPUT_PP); > + assert_eq!(r.read(REG_IDR), 0); > + assert_ne!(r.disconnected_pins() & (1 << pin), 0); // = disconnected again > + > + // Further external drives are ignored while output push-pull. > + r.gpio_set(pin, 1); > + assert_eq!(r.read(REG_IDR), 0); > +} > --=20 > 2.53.0 >=20 --Apple-Mail=_6A6F271E-A998-4CFE-A70F-ECAC3DAAFC24 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

Hi all,

A polite ping for this RFC = series.Thanks advice for Philippe advice and I am ready for = v2.

Could anyone have other suggestions for my patch. I am = appreciative for your advice and ready to fix it in = v2


2026=E5=B9=B47=E6=9C=88= 26=E6=97=A5 22:51=EF=BC=8CJack Wang <163wangjack@gmail.com> = =E5=86=99=E9=81=93=EF=BC=9A

Add native Rust unit tests = that link GpioRegisters directly and exercise
the CRL/CRH decode and = IDR update algorithm in-process, without booting a
machine or pulling = in the softmmu memory subsystem.

Signed-off-by: Jack Wang = <163wangjack@gmail.com>
---
rust/hw/gpio/tests/tests.rs | = 191 ++++++++++++++++++++++++++++++++++++
1 file changed, 191 = insertions(+)
create mode 100644 = rust/hw/gpio/tests/tests.rs

diff --git = a/rust/hw/gpio/tests/tests.rs b/rust/hw/gpio/tests/tests.rs
new file = mode 100644
index 0000000000..ca4d5ed4d6
--- /dev/null
+++ = b/rust/hw/gpio/tests/tests.rs
@@ -0,0 +1,191 @@
+// Native Rust = unit tests for the STM32F1xx GPIO register logic.
+//
+// = Copyright 2026, Jack Wang
+// Author(s): Jack Wang = <163wangjack@gmail.com>
+// SPDX-License-Identifier: = GPL-2.0-or-later
+//
+// Unlike tests/qtest/stm32f103_gpio-test.c = (which drives the device over a
+// socket from an external = qemu-system-arm process), these tests link the
+// device crate = directly and exercise `GpioRegisters` in-process. This gives
+// = fast, native Rust coverage of the CRL/CRH decode + IDR algorithm = without
+// booting a machine or pulling in the softmmu memory = subsystem.
+//
+// `GpioRegisters` deliberately has no QOM object, = no MemoryRegion and no
+// InterruptSource, so it can be constructed = with `GpioRegisters::new()` and
+// called directly. Only the BQL = mock (bql::start_test) is required, because
+// the register cells = are `BqlCell`s.
+//
+// SPDX-License-Identifier: = GPL-2.0-or-later
+
+use stm32f1xx_gpio::gpio::{
+ =    GpioRegisters, PinUpdate, REG_BRR, REG_BSRR, REG_CRH, = REG_CRL, REG_IDR, REG_ODR, RESET_CRH,
+ =    RESET_CRL,
+};
+
+/// Take the mock BQL so = `BqlCell` accesses are permitted. Integration tests
+/// run with = `--test-threads=3D1`, so this is safe to call from every test.
+fn = regs() -> GpioRegisters {
+ =    bql::start_test();
+ =    GpioRegisters::new()
+}
+
+// 4-bit = MODE[1:0]+CNF[1:0] pin configurations (RM0008).
+const = CFG_INPUT_FLOATING: u32 =3D 0x4; // MODE=3D00 CNF=3D01 (reset = default)
+const CFG_INPUT_PULL: u32 =3D 0x8; // MODE=3D00 CNF=3D10, = ODR selects up/down
+const CFG_OUTPUT_PP: u32 =3D 0x1; // MODE=3D01 = CNF=3D00, general push-pull
+
+/// Set the 4-bit config nibble for = `pin` in CRL/CRH via an MMIO write.
+fn set_config(r: = &GpioRegisters, pin: usize, cfg: u32) {
+    let = reg =3D if pin < 8 { REG_CRL } else { REG_CRH };
+ =    let shift =3D ((pin % 8) * 4) as u32;
+ =    let old =3D r.read(reg);
+ =    r.write(reg, (old & !(0xF << shift)) | (cfg = << shift));
+}
+
+fn set_odr_bit(r: &GpioRegisters, = pin: usize, value: u32) {
+    let old =3D = r.read(REG_ODR);
+    r.write(REG_ODR, (old & !(1 = << pin)) | (value << pin));
+}
+
+/// True if pin = `pin` is set in a [`PinUpdate`] and its reported level is
+/// = `level`.
+fn pin_changed_to(upd: PinUpdate, pin: usize, level: bool) = -> bool {
+    (upd.mask & (1 << pin)) !=3D= 0 && (((upd.levels >> pin) & 1) !=3D 0) =3D=3D = level
+}
+
+#[test]
+/// A freshly created register block = holds the RM0008 reset values.
+fn test_reset_values() {
+ =    let r =3D regs();
+ =    assert_eq!(r.read(REG_CRL), RESET_CRL);
+ =    assert_eq!(r.read(REG_CRH), RESET_CRH);
+ =    assert_eq!(r.read(REG_IDR), 0);
+ =    assert_eq!(r.read(REG_ODR), = 0);
+}
+
+#[test]
+/// `reset` restores the reset state = after the registers are dirtied.
+fn test_reset_restores() {
+ =    let r =3D regs();
+ =    r.write(REG_CRL, 0xDEAD_BEEF);
+ =    r.write(REG_ODR, 0x0000_FFFF);
+ =    assert_ne!(r.read(REG_CRL), RESET_CRL);
+
+ =    r.reset();
+
+ =    assert_eq!(r.read(REG_CRL), RESET_CRL);
+ =    assert_eq!(r.read(REG_CRH), RESET_CRH);
+ =    assert_eq!(r.read(REG_IDR), 0);
+ =    assert_eq!(r.read(REG_ODR), = 0);
+}
+
+#[test]
+/// Push-pull output: IDR follows ODR; = ODR written while input is latched,
+/// and the out-pin change is = reported.
+fn test_output_mode() {
+    let r =3D = regs();
+    let pin =3D 5;
+
+ =    // Set ODR while still a floating input: IDR = unaffected, no out change.
+    let idle =3D = set_odr_and_report(&r, pin, 1);
+ =    assert_eq!(r.read(REG_IDR), 0);
+ =    assert_eq!(idle.mask & (1 << pin), = 0);
+
+    // Switch to push-pull output: pin now = follows ODR (high), and flips.
+    let old =3D = r.read(REG_CRL);
+    let to_output =3D r.write(
+ =        REG_CRL,
+ =        (old & !(0xF << (pin = * 4))) | (CFG_OUTPUT_PP << (pin * 4)),
+ =    );
+    assert_eq!(r.read(REG_IDR), = 1 << pin);
+ =    assert!(pin_changed_to(to_output, pin, = true));
+
+    // Clear ODR bit: pin goes low, and = flips back.
+    let to_low =3D = set_odr_and_report(&r, pin, 0);
+ =    assert_eq!(r.read(REG_IDR), 0);
+ =    assert!(pin_changed_to(to_low, pin, = false));
+}
+
+/// Helper: write one ODR bit and return the = resulting [`PinUpdate`].
+fn set_odr_and_report(r: = &GpioRegisters, pin: usize, value: u32) -> PinUpdate {
+ =    let old =3D r.read(REG_ODR);
+ =    r.write(REG_ODR, (old & !(1 << pin)) | = (value << pin))
+}
+
+#[test]
+/// Floating input: an = external drive is reflected in IDR and reported.
+fn = test_input_mode() {
+    let r =3D regs();
+ =    let pin =3D 6usize;
+
+ =    set_config(&r, pin, CFG_INPUT_FLOATING);
+
+ =    let drive_high =3D r.gpio_set(pin, 1);
+ =    assert_eq!(r.read(REG_IDR), 1 << pin);
+ =    assert!(pin_changed_to(drive_high, pin, = true));
+
+    let drive_low =3D r.gpio_set(pin, = 0);
+    assert_eq!(r.read(REG_IDR), 0);
+ =    assert!(pin_changed_to(drive_low, pin, = false));
+}
+
+#[test]
+/// Input pull mode: ODR selects = pull-up (IDR=3D1) vs pull-down (IDR=3D0),
+/// with no external = driver connected.
+fn test_pull_up_down() {
+ =    let r =3D regs();
+    let pin =3D = 0;
+
+    set_odr_bit(&r, pin, 1);
+ =    set_config(&r, pin, CFG_INPUT_PULL);
+ =    assert_eq!(r.read(REG_IDR), 1 << pin);
+
+ =    set_odr_bit(&r, pin, 0);
+ =    assert_eq!(r.read(REG_IDR), = 0);
+}
+
+#[test]
+/// BSRR sets/resets ODR bits (set has = priority); BRR resets ODR bits.
+fn test_bsrr_brr() {
+ =    let r =3D regs();
+    let pin =3D = 1;
+
+    r.write(REG_BSRR, 1 << pin);
+ =    assert_eq!(r.read(REG_ODR), 1 << pin);
+
+ =    r.write(REG_BSRR, 1 << (pin + 16));
+ =    assert_eq!(r.read(REG_ODR), 0);
+
+ =    r.write(REG_BSRR, 1 << pin);
+ =    r.write(REG_BRR, 1 << pin);
+ =    assert_eq!(r.read(REG_ODR), 0);
+
+ =    // Set has priority over reset within one BSRR = write.
+    r.write(REG_BSRR, (1 << pin) | (1 = << (pin + 16)));
+ =    assert_eq!(r.read(REG_ODR), 1 << = pin);
+}
+
+#[test]
+/// A pin driven externally, then = reconfigured as push-pull output, is
+/// disconnected: IDR follows = ODR, not the stale external level.
+fn test_push_pull_disconnect() = {
+    let r =3D regs();
+    let = pin =3D 7usize;
+
+    // Drive high externally as = input.
+    set_config(&r, pin, = CFG_INPUT_FLOATING);
+    r.gpio_set(pin, 1);
+ =    assert_eq!(r.read(REG_IDR), 1 << pin);
+ =    assert_eq!(r.disconnected_pins() & (1 << = pin), 0); // now connected
+
+    // Reconfigure as = push-pull output with ODR=3D0: pin must drop to 0,
+ =    // and the external driver must be disconnected.
+ =    set_odr_bit(&r, pin, 0);
+ =    set_config(&r, pin, CFG_OUTPUT_PP);
+ =    assert_eq!(r.read(REG_IDR), 0);
+ =    assert_ne!(r.disconnected_pins() & (1 << = pin), 0); // disconnected again
+
+    // Further = external drives are ignored while output push-pull.
+ =    r.gpio_set(pin, 1);
+ =    assert_eq!(r.read(REG_IDR), 0);
+}
-- =
2.53.0


= --Apple-Mail=_6A6F271E-A998-4CFE-A70F-ECAC3DAAFC24--