Usage
This driver is used in the same way as most other drivers: using open()
, write()
, read()
, ioctl()
, and close()
. The tms570ls1224 microcontroller has 16 GPIO pins - therefore you always need to read/write 2 bytes. An example program:
#include <fcntl.h> // open()
#include <unistd.h> // read(), write(), close()
#include <stdio.h> // printf()
#include <errno.h> // errno, strerror()
int main()
{
int fd = open("/dev/gpio", O_RDWR);
if (fd < 0)
{
printf("Could not open: %s\n", strerror(errno));
return -1;
}
unsigned short data = 0xFFFF;
write(fd, &data, 2);
unsigned short r_data = 0x0000;
read(fd, &r_data, 2);
if (r_data != data)
{
printf("Error in %s(): Data written not equal to data read!\n", __func__);
return -1;
}
printf("Success!\n");
return 0;
}
Configuration of GPIOs
To use this driver to its full extent, one needs to include files from the kernel. Assuming the variable EOS holds the path to the 'erlend_os' folder, add -I $(EOS)$/kernel/kernel/inc
to your makefile.
The gpio-tms570.h
file contains this struct, which will be used for configuration by the user.
// Inside gpio-tms570.h
struct gpio_config
{
unsigned short directions; /* Data directions: (1) output, (0) input*/
unsigned short open_drains; /* Whether pins are open drain (1) or not (0) */
unsigned short pull_selects; /* Use pullup and pulldowns (1) or not (0) */
unsigned short pullups; /* Pullup resistor enabled (1) or pulldown resistor enabled (0) */
};
How to configure, specifically:
#include <drivers/gpio/gpio-tms570.h> // Gives access to the struct above
#include <ioctl/ioctl.h> // Gives access to GPIOSETCONF to be used for ioctl() function
struct gpio_config my_conf;
// Configure GPIOs by changing 'my_conf'.
ioctl(fd, GPIOSETCONF, &my_conf);
About the hardware
The tms570ls1224 has 16 GPIO pins divided into two modules A and B, each with eight pins. Upon a read or write operation the first eight bits of the data passed through read()
or write()
correspond to the first eight pins of the A module. The next eight bits correspond to the next eight pins of the B module.
An example follows below:
// To set/clear
// [pin15, pin14, pin13, ..., pin1, pin0] -> [0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0]
unsigned short data = 0x6E56;
write(fd, &data, 2);
// To configure data directions
// [pin15, pin14, pin13, ..., pin1, pin0] -> [0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1]
#include <drivers/gpio/gpio-tms570.h> // As discussed above
struct gpio_config my_config;
get_standard_config(&my_config); // Assuming this function exists
my_config.directions = 0x0F53;
ioctl(fd, GPIOSETCONF, &my_conf);