Hey guys,
Thanks in getting back to me I have posted the code that I used to enable a GPIO to act as input or an output below;
#include <stdlib.h>
#include <stdio.h>
#include <hw/inout.h>
#include <sys/mman.h>
#include <sys/neutrino.h>
#include <stdint.h>
#include <BeagleBoneIO.h>
uintptr_t MapIO(uintptr_t gpio_base, uint32_t BaseAddress)
{
gpio_base = mmap_device_io(AM335X_GPIO_SIZE, BaseAddress);
if(gpio_base == MAP_DEVICE_FAILED)
{
perror(“Can’t map device I/O for GPIO”);
printf(“Main Terminated…!\n”);
return 0;
}
return gpio_base;
}
int WriteIO(uintptr_t gpio_base, int value, uint32_t BitsToModify)
{
uint32_t val = 0;
val = in32(gpio_base + GPIO_DATAOUT); // value that is currently on the GPIO port
if (value==0)
{
val &= ~BitsToModify; // clear the bits
}
else
{
val |= BitsToModify; // set the bits
}
out32(gpio_base + GPIO_DATAOUT, val);
return 0;
}
int SetDDR(uintptr_t gpio_port, int Direction, uint32_t BitsToSet)
{
uint32_t val = 0;
// Read GPIO output enable register
// 0 The corresponding GPIO port is configured as an output.
// 1 The corresponding GPIO port is configured as an input.
val = in32(gpio_port + GPIO_OE);
printf(“value of register output enable register= %#010x\n”, val);
if(Direction== 0)
val &= ~(BitsToSet); // make sure they are 0
else
val |= BitsToSet; // make sure they are set to 1
out32(gpio_port + GPIO_OE, val); // write value to output enable
val = in32(gpio_port + GPIO_OE);
printf(“Modified value of register output enable register= %#010x\n”, val);
return 0;
}
uint32_t ReadIO(uintptr_t gpio_base, uint32_t BitsToRead)
{
uint32_t val = 0;
val = in32(gpio_base + GPIO_DATAIN); // value that is currently on the GPIO port
printf("\nvalue of data register = %#010x\n", val);
val &= BitsToRead; // mask bit
printf(“value of data register after masking it = %#010x\n”, val);
return val;
}
int main(int argc, char *argv[])
{
printf(“Welcome to the QNX Momentics BeagleBone GPIO Reader\n”);
uintptr_t gpio0_port = 0;
uintptr_t gpio1_port = 0;
uintptr_t gpio2_port = 0;
uintptr_t gpio3_port = 0;
uint32_t val = 0;
ThreadCtl(_NTO_TCTL_IO,NULL); // Request I/O privileges; let the thread execute the I/O opcodes
// in, ins, out, outs, cli, sti on architectures where it has the
// appropriate privilege, and let it attach IRQ handlers. You need
// root permissions to use this command. If a thread attempts to use
// faults with a SIGSEGV when the opcode is attempted.
uintptr_t gpio_base;
gpio_base = mmap_device_io(0x08, 0x44e10844);
if(gpio_base == MAP_DEVICE_FAILED)
{
perror(“Can’t map Control Base Module”);
printf(“Main Terminated…!\n”);
return 0;
}
printf(“Device gpio_base\t = %#010x\n”, gpio_base);
gpio1_port = MapIO(gpio1_port, AM335X_GPIO1_BASE);
// set the data direction
SetDDR(gpio1_port,1, GPIO1_28); // Main function of setting up pin 28 as an input
munmap_device_io(gpio1_port, AM335X_GPIO_SIZE);
printf("\nAll good - Main Terminated…!\n");
return EXIT_SUCCESS;
}
So what I’ve done above is, writing up a data direction register function and writing to GPIO 28 to be an input(1). If I were to set GPIO 28 as an output, I have to to just change SetDDR(gpio1_port, 0, GPIO1_28).
Hope it helps!