首页 > 解决方案 > 在 C 中启用 AM335x 上的 GPIO

问题描述

我有以下函数initGPIO。目标是使用 am335x 在 beaglebone 上启用 GPIO 0、1 和 2。如何启用相应的 GPIO 设置为头文件中给出的 reg_GPIO?我有头文件 GPIO.h,其中包含 GPIO 的编号、寄存器编号和寄存器结构。我试图在函数 initGPIO 中设置 GPIO。是否有意义?

gpio.h

#include <stdint.h>
#include <stdbool.h>

#define GPIO0 0  /*!< GPIO 0 number */
#define GPIO1 1 /*!< GPIO 1 number */
#define GPIO2 2 /*!< GPIO 2 number */
#define GPIO3 3 /*!< GPIO 3 number */

// Base address for each gpio hardware module.
#define GPIO0_REG  0x44E07000  //<! gpio0 hardware module address.
#define GPIO1_REG  0x4804C000  //<! gpio1 hardware module address.
#define GPIO2_REG  0x481AC000  //<! gpio2 hardware module address.
#define GPIO3_REG  0x481AE000  //<! gpio3 hardware module address.

// Register Structure
typedef struct
{
    volatile uint32_t irqstatus_set_0;   // Offset 0x34 - Enables specific interrupt event to trigger.
    volatile uint32_t irqstatus_set_1;   // Offset 0x38 - Enables specific interrupt event to trigger.
    volatile uint32_t irqwaken_0;        // Offset 0x44 - Enables wakeup events on an interrupt.
    volatile uint32_t irqwaken_1;        // Offset 0x48 - Enables wakeup events on an interrupt.
    volatile uint32_t ctrl;  // Offset 0x130 - Controls clock gating functionality, i.e. enables module.
    volatile uint32_t oe; // Offset 0x134 – Output Enable pin (clear bit to 0) output capability.
    volatile uint32_t datain;            // Offset 0x138 - Registers data read from the GPIO pins.
    volatile uint32_t dataout;           // Offset 0x13c - Sets value of GPIO output pins.
    volatile uint32_t cleardataout;      // Offset 0x190 - Clears to 0 bits in dataout
    volatile uint32_t setdataout;        // Offset 0x194 - Sets to 1 bits in dataout
} GPIO_REGS;


void initGPIO();

gpio.c

/*!
 * \brief Initialize GPIOs.
 *
 * Enables GPIO0, GPIO1, and GPIO2 (GPIO3 not used in IDP. Also configures the output pins
 * used in the IDP to control relays and address the ADC's on relays.
 *
 *****************************************************************************************/
void initGPIO()
{

    //enable GPIOs
    GPIO_REGS gpio_regs;
    //might need to change ctrl

    gpio_regs.datain |= (GPIO0 << GPIO0_REG  );
    gpio_regs.datain |= (GPIO1 << GPIO1_REG  );
    gpio_regs.datain |= (GPIO2 << GPIO2_REG  );
}

标签: cgpiobeagleboneblack

解决方案


默认情况下,Beaglebones 与 Debian Linux 一起提供。除非您决定放弃它,否则内核有一个 GPIO 驱动程序,它假定控制所有 GPIO。您不应尝试直接访问原始 GPIO 寄存器,而应与内核驱动程序对话。最简单的方法是安装libgpiod(可能在最近的 Beagles 上默认安装)并调用其 API。


推荐阅读