Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Please ensure the driver has

Anchor
_GoBack
_GoBack
enabled in menuconfig and device tree has correct setting.

5.1 GPIO Module 

5.1.1 GPIO Module control by C code

/************************************************************/
//File name:gpiotest.c
//Function : Test linux gpio with SP7021
//Date : 2019-12-19
/************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<fcntl.h>
#include<dirent.h>

#define SYS_GPIO_PFX "/sys/class/gpio/"
#define PIN_DIR_I 0
#define PIN_DIR_O 1
typedef unsigned char uint8;
int fd = -1;
//gpio init
static uint8 gpio_init(int _pin)
{
   DIR *dp;
   char fpath[100];
   int port,num;

...


   printf("GPIO test main start \n");
   pinnum = atoi(argv[1]);
   if (argv[3] != NULL)
      value = atoi(argv[3]);
   gpio_init(pinnum);
   if(strcmp(argv[2],"state") == 0)
   {
      printf("state:%s \n", (gpio_dir_get(pinnum) == PIN_DIR_I? "IN":"OUT"));
   }
   if(strcmp(argv[2],"in") == 0)
   {
      printf("set IN ok:%d \n", gpio_dir_set(pinnum,PIN_DIR_I));
   }
   if(strcmp(argv[2],"read") == 0)
   {
      printf("read:%d \n", gpio_read(pinnum));
      sleep(5);
      printf("read:%d \n", gpio_read(pinnum));
   }
   if(strcmp(argv[2],"out") == 0)
   {
      printf("set OUT ok:%d \n", gpio_dir_set(pinnum,PIN_DIR_O));
   }
   if(strcmp(argv[2],"write") == 0)
   {
      printf("set OUT ok:%d \n", gpio_dir_set(pinnum,PIN_DIR_O));
      if(value == 1)
         printf("write done:%d \n", gpio_write(pinnum,1));
      else
         printf("write done:%d \n", gpio_write(pinnum,0));
   }
   printf("GPIO test end! \n ");
   for(i=1; i; i--){
      usleep(1000*100);
   }
}


5.1.2 GPIO Module control by Python code

5.1.2.1 Edit gpio.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<fcntl.h>
#include<dirent.h>
#include<string.h>
#include<sys/stat.h>

#define SYS_GPIO_PFX "/sys/class/gpio/"
#define PIN_DIR_I 0
#define PIN_DIR_O 1

typedef unsigned char uint8;
int fd = -1;

uint8 gpio_init(int);
uint8 gpio_read(int);
uint8 gpio_write(int, int);
uint8 gpio_dir_get(int);
uint8 gpio_dir_set(int, int);

//gpio init
uint8 gpio_init(int _pin)
{
   DIR *dp;
   char fpath[100];
   int port,num;

   port = _pin/8;
   num = _pin%8;
   printf("port=%d, num=%d \n",port,num);
   sprintf(fpath, SYS_GPIO_PFX"P%d_0%d", port, num);
   if(!(dp=opendir(fpath)))
   {
      FILE *fp;
      if((fp = fopen(SYS_GPIO_PFX"export", "w")) == NULL) return(-1);
      fprintf(fp, "%d", _pin);
      fclose(fp);
      sprintf(fpath, "sudo -S chmod 777 %sP%d_0%d/value", SYS_GPIO_PFX, port, num);
      printf ("fpath0=%s", fpath);
      system(fpath);
      //chmod(fpath, 0777);
      sprintf(fpath, "sudo -S chmod 777 %sP%d_0%d/direction", SYS_GPIO_PFX, port, num);
      printf ("fpath1=%s", fpath);
      system(fpath);
      //chmod(fpath, 0777);
   }
   else
   {
      closedir(dp);
   }
   return(0);
}

uint8 gpio_read(int _pin)
{
   char ret, ss[2];
   char fpath[100];
   int port,num;

   port = _pin/8;
   num = _pin%8;
   sprintf(fpath, SYS_GPIO_PFX"P%d_0%d/value", port, num);
   printf("fpath=%s \n", fpath);
   fd = open(fpath, O_RDWR); // open file and enable read and write
   if(fd < 0)
   {
      perror("Can't open gpio file \n"); // open i2c dev file fail
      exit(1);
   }
   printf("open gpio file success !\n"); // open i2c dev file succes
   ret = read(fd, ss, 1);
   close(fd);
   if(ret < 0) return(0);
   return((ss[0] == '0'?0:1));
}


uint8 gpio_write(int _pin, int _val)
{
   int ret;
   char x[2];
   char fpath[100];
   int port,num;

   port = _pin/8;
   num = _pin%8;
   sprintf(fpath, SYS_GPIO_PFX"P%d_0%d/value", port, num);
   printf("fpath=%s \n", fpath);
   fd = open(fpath, O_RDWR); // open file and enable read and write
   if(fd < 0)
   {
      perror("Can't open gpio file \n"); // open i2c dev file fail
      exit(1);
   }
   printf("open gpio file success !\n"); // open i2c dev file succes
   x[1] = '\0';
   x[0] = (_val>0 ? '1':'0');
   ret = write(fd, x, 1);
   close(fd);
   return(ret);
}


uint8 gpio_dir_get(int _pin)
{
   static char ss[2];
   char fpath[100];
   int ret,port,num;

   port = _pin/8;
   num = _pin%8;
   sprintf(fpath, SYS_GPIO_PFX"P%d_0%d/direction", port, num);
   printf("fpath=%s \n", fpath);
   fd = open(fpath, O_RDWR); // open file and enable read and write
   if(fd < 0)
   {
      perror("Can't open gpio file \n"); // open i2c dev file fail
      exit(1);
   }
   printf("open gpio file success !\n"); // open i2c dev file succes
   ret = read(fd, ss, 2);
   close(fd);
   if(ret < 0) return(0);
   return((ss[0] == 'i'? PIN_DIR_I:PIN_DIR_O));
}


uint8 gpio_dir_set(int _pin, int _dir)
{
   int ret=0;
   const char *ss="in";
   char fpath[100];
   int port,num;

   port = _pin/8;
   num = _pin%8;
   sprintf(fpath, SYS_GPIO_PFX"P%d_0%d/direction", port, num);
   printf("fpath=%s \n", fpath);
   fd = open(fpath, O_RDWR); // open file and enable read and write
   if(fd < 0)
   {
      perror("Can't open gpio file \n"); // open i2c dev file fail
      exit(1);
   }
   printf("open gpio file success !\n"); // open i2c dev file succes
   if(_dir == PIN_DIR_O) ss="out";
   ret = write(fd, ss, strlen(ss)*sizeof(char));
   close(fd);
   return((ret<2 ? -1:0));
}

5.1.2.2 Edit share library

$ gcc -shared -fPIC gpio.c -o gpio.so

5.1.2.3 Edit gpio.py

import time
from ctypes import *
m=cdll.LoadLibrary('./gpio.so')

k=m.gpio_init(c_int(12))
print(k)
k=m.gpio_dir_set(c_int(12),c_int(1))
print(k)
while True:
   k=m.gpio_write(c_int(12),c_int(1))
   time.sleep(0.005)
   k=m.gpio_write(c_int(12),c_int(0))
   time.sleep(0.005)


5.2 I2C Module

/************************************************************/
//File name:tea5767.c
//Function : Test linux i2c with tea5767 communication
//Date : 2019-05-29
/************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/ioctl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/select.h>
#include<sys/time.h>
#include<errno.h>
#define Address 0x60 //tea5767 ID address
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_SLAVE 0x0703
#define I2C_BUS_MODE 0x0780
typedef unsigned char uint8;
int fd = -1;
//Function declare
static uint8 tea5767_Init(void);
//tea5767 init
static uint8 tea5767_Init(void)
{
   float frequency;
   unsigned div;
   uint8 data[5];

...