Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

Enable GPIO on AM335x in C

I have the following function initGPIO. The goal is to enable the GPIOs 0, 1 and 2 on beaglebone using am335x. How can I enable the corresponding GPIO set to the reg_GPIO that are given in the header file? I have the header file GPIO.h which contains the GPIO's numbers, register number and register structure. I have tried to set the GPIO in the function initGPIO. Does it make sense?

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

/*!
 * rief 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  );
}
question from:https://stackoverflow.com/questions/65863481/enable-gpio-on-am335x-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By default Beaglebones come with Debian Linux. Unless you've decided to drop that, the kernel has a GPIO driver which assumes control of all GPIO. You should not try to access raw GPIO registers directly, but rather talk to the kernel driver. The simplest way to do that is to install libgpiod (which may be installed by default on recent Beagles) and call its API.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...