c - 在 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 );
}
解决方案
默认情况下,Beaglebones 与 Debian Linux 一起提供。除非您决定放弃它,否则内核有一个 GPIO 驱动程序,它假定控制所有 GPIO。您不应尝试直接访问原始 GPIO 寄存器,而应与内核驱动程序对话。最简单的方法是安装libgpiod
(可能在最近的 Beagles 上默认安装)并调用其 API。
推荐阅读
- css - 将“重复”与“自动调整”和固定宽度列组合在一个“网格模板列”中
- flutter - google ml 套件安装包:google_ml_kit_example
- php - PHP读取在线Excel文件而不下载文件
- python - 看不到 2 个数据帧之间有什么区别
- reactjs - React 中的条件列表渲染
- c++ - OpenGL项目不会打开显示窗口
- python - 多处理查找进程索引?
- python - 在 Django 中,根据模型中其他字段中选择的值删除选择字段下拉列表中的选项
- python-3.7 - sudo python3 无法获得 root 访问权限
- python-3.x - 解析两个 JSON 文件