Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************************
- * Description: hal_imx6_gpio.c
- * Driver for iMX6 GPIO pins
- *
- * Author: Paul Setchko <[email protected]>
- * License: GPL Version 2
- * Copyright (c) 2014.
- *
- ********************************************************************/
- #include "rtapi.h"
- #include "rtapi_app.h"
- #include "hal.h"
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdint.h>
- #include "imx6_gpio.h"
- #if !defined(BUILD_SYS_USER_DSO)
- #error "This driver is for usermode threads only"
- #endif
- #define MODNAME "hal_imx6_gpio"
- MODULE_AUTHOR("Paul Setchko");
- MODULE_DESCRIPTION("Driver for iMX6 GPIO pins");
- MODULE_LICENSE("GPL");
- #define PINS 11
- typedef struct {
- hal_bit_t* input_pins[PINS]; // array of pointers to bits
- hal_bit_t* output_pins[PINS]; // array of pointers to bits
- } port_data_t;
- static port_data_t *port_data;
- static const char *modname = MODNAME;
- static void write_port(void *arg, long period);
- static void read_port(void *arg, long period);
- static off_t start_addr_for_port(int port);
- static void configure_pin(imx_gpio_pin *pin, char mode);
- static int comp_id;
- static int num_ports;
- static char *input_pins;
- RTAPI_MP_STRING(input_pins, "input pins, comma separated");
- static char *output_pins;
- RTAPI_MP_STRING(output_pins, "output pins, comma separated");
- int rtapi_app_main(void) {
- char name[HAL_NAME_LEN + 1];
- int n, retval;
- char *data, *token;
- num_ports = 1;
- n = 0; // port number... only one for now
- // init driver
- comp_id = hal_init(modname);
- if(comp_id < 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: hal_init() failed\n", modname);
- return -1;
- }
- // allocate port memory
- port_data = hal_malloc(sizeof(port_data_t));
- if(port_data == 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: hal_malloc() failed\n", modname);
- hal_exit(comp_id);
- return -1;
- }
- // configure input pins
- if(input_pins != NULL) {
- data = input_pins;
- while((token = strtok(data, ",")) != NULL) {
- int pin = strtol(token, NULL, 10);
- imx_gpio_pin *imxpin = &x54_pins[pin];
- if(imxpin->claimed != 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: pin %02d is not available as a GPIO.\n", modname, pin);
- hal_exit(comp_id);
- return -1;
- }
- // map control module memory
- configure_control_module(imxpin);
- data = NULL; // after the first call, subsequent calls to strtok need to be on NULL
- // Add HAL pin
- retval = hal_pin_bit_newf(HAL_OUT, &(port_data->input_pins[pin]), comp_id, "imx6_gpio.in-%02d", pin);
- if(retval < 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: could not export pin %02d, err: %d\n", modname, pin, retval);
- hal_exit(comp_id);
- return -1;
- }
- configure_pin(imxpin, 'I');
- rtapi_print("pin %d maps to pin %d, mode %d\n", pin, imxpin->pin_num, imxpin->claimed);
- }
- }
- // configure output pins
- if(output_pins != NULL) {
- data = output_pins;
- while((token = strtok(data, ",")) != NULL) {
- int pin = strtol(token, NULL, 10);
- imx_gpio_pin *imxpin = &x54_pins;
- // map control module memory
- configure_control_module(imxpin);
- if(imxpin->claimed != 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: pin %02d is not available as a GPIO.\n", modname, pin);
- hal_exit(comp_id);
- return -1;
- }
- data = NULL; // after the first call, subsequent calls to strtok need to be on NULL
- // Add HAL pin
- retval = hal_pin_bit_newf(HAL_IN, &(port_data->output_pins[pin]), comp_id, "imx6_gpio.out-%02d", pin);
- if(retval < 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: could not export pin %02d, err: %d\n", modname, pin, retval);
- hal_exit(comp_id);
- return -1;
- }
- configure_pin(imxpin, 'O');
- rtapi_print("pin %d maps to pin %d, mode %d\n", pin, imxpin->pin_num, imxpin->claimed);
- }
- }
- // export functions
- rtapi_snprintf(name, sizeof(name), "imx6_gpio.write");
- retval = hal_export_funct(name, write_port, port_data, 0, 0, comp_id);
- if(retval < 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: port %d write funct export failed\n", modname, n);
- hal_exit(comp_id);
- return -1;
- }
- rtapi_snprintf(name, sizeof(name), "imx6_gpio.read");
- retval = hal_export_funct(name, read_port, port_data, 0, 0, comp_id);
- if(retval < 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: port %d read funct export failed\n", modname, n);
- hal_exit(comp_id);
- return -1;
- }
- rtapi_print_msg(RTAPI_MSG_INFO, "%s: installed driver\n", modname);
- hal_ready(comp_id);
- return 0;
- }
- void rtapi_app_exit(void) {
- hal_exit(comp_id);
- }
- static void write_port(void *arg, long period) {
- int i;
- port_data_t *port = (port_data_t *)arg;
- // set output states
- for(i = 0; i < PINS; i++) {
- if(port->output_pins[i] == NULL)
- continue; // short circuit if hal hasn't malloc'd a bit at this location
- imx_gpio_pin pin = x54_pins[i];
- int fd = open("/dev/mem", O_RDWR);
- gpio = (uint32_t *)mmap( 0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, (uint32_t *)(pin.reg_addr) );
- if(gpio == MAP_FAILED) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: Unable to map Control Module: %s", modname, strerror(errno));
- exit(1);
- }
- close(fd);
- if( pin.claimed != 'O' )
- continue; // if we somehow get here but the pin isn't claimed as output, short circuit
- if(*port->output_pins[i] == 0)
- *(gpio) &= ~( 1 << (pin.pin_num) );
- else
- *(gpio) |= ( 1 << (pin.pin_num) );
- }
- }
- static void read_port(void *arg, long period) {
- int i;
- port_data_t *port = (port_data_t *)arg;
- // read input states
- for( i = 0; i < PINS; i++ ) {
- if(port->input_pins[i] == NULL)
- continue; // short circuit if hal hasn't malloc'd a bit at this location
- imx_gpio_pin pin = x54_pins[i];
- int fd = open("/dev/mem", O_RDWR);
- gpio = (uint32_t *)mmap( 0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, (uint32_t *)(pin.reg_addr) );
- if(gpio == MAP_FAILED) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: Unable to map Control Module: %s", modname, strerror(errno));
- exit(1);
- }
- close(fd);
- if( !(pin.claimed == 'I') )
- continue; // if we get here but the pin isn't claimed as input, short circuit
- *port->input_pins[i] = ((*(gpio) & (1 << pin.pin_num)) >> pin.pin_num);
- }
- }
- void configure_pin(imx_gpio_pin *pin, char mode) {
- pin->claimed = mode;
- int fd = open("/dev/mem", O_RDWR);
- gpio = (uint32_t *)mmap( 0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, (uint32_t *)(pin->reg_addr) );
- if(gpio == MAP_FAILED) {
- rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: Unable to map Control Module: %s", modname, strerror(errno));
- exit(1);
- }
- close(fd);
- switch(mode) {
- case 'O':
- *((gpio) + 4) |= (1 << pin->pin_num); // 0 in OE is output enable
- break;
- case 'I':
- *((gpio) + 4) &= ~(1 << pin->pin_num); // 1 in OE is input
- break;
- default:
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment