You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中通过MCP23S17内核驱动访问BeagleBone Black的中断

在BeagleBone Black Rev C上实现MCP23S17的Python中断回调

现状

  • 已通过设备树覆盖(DTO)配置MCP23系列内核驱动,可通过SPI轮询MCP23S17的引脚
  • 已完成芯片两条中断线的配置,/proc/interrupts中可查看到对应中断项:79: 0 481ac000.gpio 3 Level spi1.2(注:额外添加了一条自定义片选线)

设备树配置代码

/dts-v1/;
/plugin/;

#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/am33xx.h>
#include <dt-bindings/interrupt-controller/irq.h>

/ {
    compatible = "ti,beaglebone", "ti,beaglebone-black";
    /*
    * Free up the pins used by the cape from the pinmux helpers.
    */
    fragment@1 {
        target = <&ocp>;
        __overlay__ {
            P9_29_pinmux { status = "disabled"; };  /* D0    P9_29 (B13) spi1_d0.spi1_d0 */
            P9_30_pinmux { status = "disabled"; };  /* D1    P9_30 (D12) spi1_d1.spi1_d1 */
            P9_31_pinmux { status = "disabled"; };  /* CLK   P9_31 (A13) spi1_clk.spi1_sclk */
            P9_28_pinmux { status = "disabled"; };  /* CS0   P9_28 (A17) spi1_cs0.spi1_cs0 */
            P9_42_pinmux { status = "disabled"; };  /* CS1   P9_42 (C18) gpio0_7.spi1_cs1*/
            P8_07_pinmux { status = "disabled"; };  /* CS2   P8_07 (R7)  timer4.gpio2_2 */
            P8_08_pinmux { status = "disabled"; };  /* INTA  P8_08 (T7)  timer7.gpio2_3 */
            P8_09_pinmux { status = "disabled"; };  /* INTB  P8_09 (T6)  timer5.gpio2_5 */
            P8_10_pinmux { status = "disabled"; };  /* ~RST  P8_10 (U6)  timer6.gpio2_4 */
        };
    };

    fragment@2 {
        target = <&am33xx_pinmux>;
        __overlay__ {
            bb_spi1_pins: pinmux_bb_spi1_pins {
                pinctrl-single,pins = <
                    AM33XX_PADCONF(0x990, PIN_INPUT, MUX_MODE3) /* CLK   P9_31 (A13) spi1_clk.spi1_sclk */
                    AM33XX_PADCONF(0x994, PIN_INPUT, MUX_MODE3) /* DO    P9_29 (B13) spi1_d0.spi1_d0 */
                    AM33XX_PADCONF(0x998, PIN_INPUT, MUX_MODE3) /* DI    P9_30 (D12) spi1_d1.spi1_d1 */
                    AM33XX_PADCONF(0x99c, PIN_OUTPUT, MUX_MODE3) /* CS0  P9_28 (A17) spi1_cs0.spi1_cs0 */
                    AM33XX_PADCONF(0x964, PIN_OUTPUT, MUX_MODE2) /* CS1  P9_42 (C18) gpio0_7.spi1_cs1*/
                    AM33XX_PADCONF(0x890, PIN_OUTPUT, MUX_MODE7) /* CS2  P8_07 (R7)  timer4.gpio2_2 <- My new custom CS pin*/
                    AM33XX_PADCONF(0x894, PIN_INPUT, MUX_MODE7) /* INTA  P8_08 (T7)  timer7.gpio2_3 */
                    AM33XX_PADCONF(0x89c, PIN_INPUT, MUX_MODE7) /* INTB  P8_09 (T6)  timer5.gpio2_5 */
                    AM33XX_PADCONF(0x898, PIN_OUTPUT, MUX_MODE7)/* ~RST  P8_10 (U6)  timer6.gpio2_4 */
                >;
            };
        };
    };

    fragment@3 {
        target = <&spi1>;
        __overlay__ {
            #address-cells = <1>;
            #size-cells = <0>;

            status = "okay";
            pinctrl-names = "default";
            pinctrl-0 = <&bb_spi1_pins>;
            cs-gpios = <0>, <0>, <&gpio2 2 GPIO_ACTIVE_HIGH>;
            ti,pindir-d0-out-d1-in; //Swap D0 and D1 from their default

            gpiom1: gpio@2 {
                #address-cells = <1>;
                #size-cells = <0>;
                compatible = "microchip,mcp23s17";
                gpio-controller;
                #gpio-cells = <2>;
                microchip,spi-present-mask = <0x01>;
                reg = <0x2>;
                spi-max-frequency = <1000000>;
                //Interrupts
                #interrupt-cells = <2>;
                interrupt-controller;
                interrupt-parent = <&gpio2>; //Both interrupts are on GPIO2
                interrupts = <3 IRQ_TYPE_LEVEL_LOW>,    //P8_8 -> GPIO2_3 INTA
                             <5 IRQ_TYPE_LEVEL_LOW>;    //P8_9 -> GPIO2_5 INTB
                reset-gpios = <&gpio2 4 GPIO_ACTIVE_LOW>; //P9_10 -> GPIO2_4 ~RST
            };
        };
    };
};

需求与问题

  • 理想状态下,希望在Python中实现类似bindInterrupt(IRQ=79, callback=myFunc)的中断回调功能,但未找到可行方案
  • 已知Python无法直接处理硬件中断,可接受线程快速轮询引脚的替代方案

已有尝试

  • 试过Adafruit_BBIO,但仍需手动配置MCP23S17的中断控制寄存器
  • 当前使用gpiod轮询扩展芯片,希望了解如何将gpiod与IRQ关联以实现中断触发的回调

硬件说明

  • MCP23S17的INTA/INTB直接连接到BBB的P8_8和P8_9,保持芯片默认的低电平有效中断逻辑,需确保BBB内部对应引脚被上拉

内容的提问来源于stack exchange,提问作者lunuchrat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 10:57:07