/
5. EXAMPLE CODE OF MODULE USAGE BY USER LAYER

5. EXAMPLE CODE OF MODULE USAGE BY USER LAYER

Please ensure the driver has 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;


   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);
   }
   else
   {
      closedir(dp);
   }
   return(0);
}

static 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));
   }

static 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);
}

static 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));
   }

static 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));
}

// main
// $ ./gpiotest 10 state ==> get G_MX[10] direction
// $ ./gpiotest 10 out ==> set G_MX[10] as output
// $ ./gpiotest 10 read ==> read G_MX[10] value
// $ ./gpiotest 10 write 1 ==> mean set G_MX[10] to high
int main(int argc, char *argv[])
{
   int i,pinnum,value;


   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];


   printf(" TEA5767 init\n ");
   fd = open("/dev/i2c-0", O_RDWR); // open file of i2c-0 and enable read and write
   if(fd < 0)   {
      perror("Can't open /dev/TEA5767 \n"); // open i2c dev file fail
      exit(1);
   }
   printf("open /dev/i2c-0 success !\n"); // open i2c dev file succes
   if(ioctl(fd, I2C_SLAVE, Address)<0) { //set i2c address
      printf("fail to set i2c device slave address!\n");
      close(fd);
      return -1;
   }
   printf("set slave address to 0x%x success!\n", Address);
   frequency = 97.5;
   div = (4 * (frequency * 1000 + 225))/32.768;
   data[0] = (div >> 8)&0x3f ;
   data[1] = div & 0xff ;
   data[2] = 0xB0 ;
   data[3] = 0x50 ;
   data[4] = 0x00 ;
   write(fd, data, 5);
   read(fd, data, 5);
   printf("data0=0x%x\n", data[0]);
   printf("data1=0x%x\n", data[1]);
   printf("data2=0x%x\n", data[2]);
   printf("data3=0x%x\n", data[3]);
   printf("data4=0x%x\n", data[4]);
   return(1);
}
// main
int main(int argc, char *argv[])
{
   int i;


   tea5767_Init();
   usleep(1000*100);
   printf(" TEA5767 get \n ");
   close(fd);
}

5.3 SPI Module

/************************************************************/
//File name : ms5611.c
//Function : Test linux SPI with ms5611 communication
//Date : 2019-05-29
/************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <tgmath.h>
#include <math.h>
#include <fcntl.h>
#include <linux/types.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#define DIG_START 0x88
#define TEMP_START 0xFA
#define CTRL_MEAS 0xF4
#define TEMP_ONLY_NORMAL_MODE 0xE3 // 111 000 11
#define MS5611_CMD_ADC_READ (0x00)
#define MS5611_CMD_RESET (0x1E)
#define MS5611_CMD_CONV_D1_256 (0x40)
#define MS5611_CMD_CONV_D1_512 (0x42)
#define MS5611_CMD_CONV_D1_1024 (0x44)
#define MS5611_CMD_CONV_D1_2048 (0x46)
#define MS5611_CMD_CONV_D1_4096 (0x48)
#define MS5611_CMD_CONV_D2_256 (0x50)
#define MS5611_CMD_CONV_D2_512 (0x52)
#define MS5611_CMD_CONV_D2_1024 (0x54)
#define MS5611_CMD_CONV_D2_2048 (0x56)
#define MS5611_CMD_CONV_D2_4096 (0x58)
#define MS5611_CMD_READ_PROM_RESERVE (0xA0)
#define MS5611_CMD_READ_PROM_1 (0xA2)
#define MS5611_CMD_READ_PROM_2 (0xA4)
#define MS5611_CMD_READ_PROM_3 (0xA6)
#define MS5611_CMD_READ_PROM_4 (0xA8)
#define MS5611_CMD_READ_PROM_5 (0xAA)
#define MS5611_CMD_READ_PROM_6 (0xAC)
#define MS5611_CMD_READ_PROM_CRC (0xAE)
char buf[10];
char buf2[10];
struct spi_ioc_transfer xfer[10];
spi_init(int file)
{
   __u8 lsb = 0;
   __u8 mode = 0;
   __u8 bits = 8;
   __u32 speed = 20000000;


   if (ioctl(file, SPI_IOC_RD_MODE, &mode) < 0) {
      perror("SPI rd_mode");
      return;
   }
   if (ioctl(file, SPI_IOC_WR_MAX_SPEED_HZ, &speed)<0) {
      perror("can't set max speed hz");
      return;
   }
   if (ioctl(file, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) {
      perror("SPI max_speed_hz");
      return;
   }
   printf("spi max_speed_hz: %d\n", speed);
}
char * spi_rw(int addr,int nbytes,int file)
{
   int status;


   memset(buf, 0, sizeof buf);
   memset(buf2, 0, sizeof buf2);
   buf[0] = addr;
   xfer[0].tx_buf = (unsigned long)buf;
   xfer[0].len = 1;
   xfer[1].rx_buf = (unsigned long) buf2;
   xfer[1].len = nbytes;
   printf("spi_start_rw\n");
   status = ioctl(file, SPI_IOC_MESSAGE(2), xfer);
   if (status < 0) {
      perror("SPI_IOC_MESSAGE");
      return;
   }
   return buf2;
}
char * spi_read(int addr,int nbytes,int file)
{
   int status;


   memset(buf, 0, sizeof buf);
   memset(buf2, 0, sizeof buf2);
   buf[0] = addr | 128;
   xfer[0].tx_buf = (unsigned long)buf;
   xfer[0].len = 1;
   xfer[1].rx_buf = (unsigned long) buf2;
   xfer[1].len = nbytes;
   printf("before buf[0] = 0x0%x\n" ,buf[0]);
   status = ioctl(file, SPI_IOC_MESSAGE(2), xfer);
   if (status < 0) {
      perror("SPI_IOC_MESSAGE");
      return;
   }
   return buf2;
}
void spi_write_com(char value, int file)
{
   int status;


   memset(buf, 0, sizeof buf);
   buf[0] = value;
   xfer[0].tx_buf = (unsigned long)buf;
   xfer[0].len = 1;
   status = ioctl(file, SPI_IOC_MESSAGE(1), xfer);
   if (status < 0) {
      perror("SPI_IOC_MESSAGE");
   }
}
int main()
{
   int file,i;
   char * id;
   char tmp_buff[2];
   char buff[3];
   uint16_t coef[7]={0};
   int status;
   float ms_data[3];
   uint32_t D1,D2;
   int dT,T2,TEMP;
   int64_t OFF,SENS,OFF2,SENS2;


   printf("open spi device\n");
   file = open("/dev/spidev0.0",O_RDWR);
   spi_init(file);
   if(file < 0) {
      perror("Can't open /dev/spidev0.0\n"); // open i2c dev file fail
      exit(1);
   }
   printf("open /dev/spidev0.0 success !\n"); // open i2c dev file succes
   spi_write_com(MS5611_CMD_RESET,file);
   usleep(100*28); //delay 2.8 ms
   for(i=0;i<8;i++){ // read PROM data
      usleep(1000*1); //delay 1 ms
      memcpy(tmp_buff, spi_rw((MS5611_CMD_READ_PROM_RESERVE+2*i), 2, file), 2);
      coef[i] = tmp_buff[0]*256+tmp_buff[1];
   }
   spi_write_com(MS5611_CMD_CONV_D2_4096,file);
   usleep(10*822); //delay 8.22 ms
   memcpy(buff, spi_rw(MS5611_CMD_ADC_READ, 3, file), 3);
   D2 = buff[0]*65536+buff[1]*256+buff[2];
   dT = D2 - ((uint32_t)coef[5])*256;
   TEMP = 2000 + dT*((float)coef[6])/8388608;
   spi_write_com(MS5611_CMD_CONV_D1_4096,file);
   usleep(10*822); //delay 8.22 ms
   memcpy(buff, spi_rw(MS5611_CMD_ADC_READ, 3, file), 3);
   D1 = buff[0]*65536+buff[1]*256+buff[2];
   if(TEMP<2000){
      T2 = (float)(dT*dT)/2147483648;
      OFF2 = 2.5f*(TEMP-2000)*(TEMP-2000);
      SENS2 = 1.25f*(TEMP-2000)*(TEMP-2000);
      i f(TEMP<-1500){
         OFF2 = OFF2 +7*(TEMP+1500)*(TEMP+1500);
         SENS2 = SENS2 +5.5f*(TEMP+1500)*(TEMP+1500);
      }
   }
   else
   {
      T2 = 0;
      OFF2 = 0;
      SENS2 = 0;
   }
   ms_data[0]=(TEMP-T2)/100.0;
   OFF = ((int64_t)coef[2])*65536 + (((int64_t)coef[4])*dT)/128 - OFF2;
   SENS = ((int64_t)coef[1])*32768 + (((int64_t)coef[3])*dT)/256 - SENS2;
   ms_data[1] = ((D1*SENS/2097152 - OFF)/32768);
   printf("Temperature : %f \n", ms_data[0]);
   printf("Pressure : %f \n", ms_data[1]);
   return 0;
}

5.4 ETHERNET Module

/************************************************************/
//File name : test.c
//Function : Source code list of simple server program
//Date : 2019-05-29
/************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVER_PORT 3490 /* Server port for client connection */
#define MAX_CONNECTIONS 10 /* Max allowable connections */
main()
{
   int server_fd; /* server file descriptor */
   int client_fd; /* client file descriptor */
   struct sockaddr_in server_addr; /* server ip address */
   struct sockaddr_in client_addr; /* client's ip address */
   int sin_size;


/* Get a file descriptor for network communication */
   if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("socket");
      exit(1);
   }
   /* Setup server address */
   server_addr.sin_family = AF_INET; /* Address family: internet */
   server_addr.sin_port = htons(SERVER_PORT); /* Server port number */
   server_addr.sin_addr.s_addr = INADDR_ANY; /* Allow any IP to connect */
   bzero(&(server_addr.sin_zero); /* Zero the rest of the structure. */
   /* Assign IP address & port to the file descriptor of server. */
   if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
      perror("bind");
      exit(1);
   }
   /* Set server to accept incoming connections. */
   if (listen(server_fd, MAX_CONNECTIONS) == -1) {
      perror("listen");
      exit(1);
   }
   /* main accept() loop */
   while(1) {
      /* Accept connection request from client. */
      sin_size = sizeof(struct sockaddr_in);
      if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &sin_size)) == -1) {
         perror("accept");
         continue;
      }
      printf("Server: Got connection from %s\n", inet_ntoa(client_addr.sin_addr));
      /* Create a child process */
      if (!fork()) {
         /* This is the child process */
         if (send(client_fd, "Hello, world!\n", 14, 0) == -1)
            perror("send");
         close(client_fd);
         exit(0); /* Child process exits */
      }
      /* Parent process continues here. */
      close(client_fd); /* Parent doesn't need this. */
      /* Wait for child process exits. */
      while (waitpid(-1, NULL, WNOHANG) > 0);
   }
}

5.5 CRYPTO Module

/************************************************************/
//File name : crypto.c
//Function : Demo on how to use /dev/crypto device for ciphering
//Date : 2019-05-29
/************************************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <crypto/cryptodev.h>
#include "sha.h"
int sha_ctx_init(struct cryptodev_ctx* ctx, int cfd, const uint8_t *key, unsigned int key_size)
{
#ifdef CIOCGSESSINFO
   struct session_info_op siop;
#endif


   memset(ctx, 0, sizeof(*ctx));
   ctx->cfd = cfd;
   if (key == NULL)
      ctx->sess.mac = CRYPTO_SHA1;
   else {
      ctx->sess.mac = CRYPTO_SHA1_HMAC;
      ctx->sess.mackeylen = key_size;
      ctx->sess.mackey = (void*)key;
   }
   if (ioctl(ctx->cfd, CIOCGSESSION, &ctx->sess)) {
      perror("ioctl(CIOCGSESSION)");
      return -1;
   }
#ifdef CIOCGSESSINFO
   siop.ses = ctx->sess.ses;
   if (ioctl(ctx->cfd, CIOCGSESSINFO, &siop)) {
      perror("ioctl(CIOCGSESSINFO)");
     return -1;
   }
   printf("Got %s with driver %s\n",
   siop.hash_info.cra_name, siop.hash_info.cra_driver_name);
   if (!(siop.flags & SIOP_FLAG_KERNEL_DRIVER_ONLY)) {
      printf("Note: This is not an accelerated cipher\n");
   }
   /printf("Alignmask is %x\n", (unsigned int)siop.alignmask);/
   ctx->alignmask = siop.alignmask;
#endif
   return 0;
}
void sha_ctx_deinit(struct cryptodev_ctx* ctx)
{
   if (ioctl(ctx->cfd, CIOCFSESSION, &ctx->sess.ses)) {
      perror("ioctl(CIOCFSESSION)");
   }
}
int sha_hash(struct cryptodev_ctx* ctx, const void* text, size_t size, void* digest)
{
   struct crypt_op cryp;
   void* p;


   /* check text and ciphertext alignment */
   if (ctx->alignmask) {
      p = (void*)(((unsigned long)text + ctx->alignmask) & ~ctx->alignmask);
      if (text != p) {
         fprintf(stderr, "text is not aligned\n");
         return -1;
      }
   }
   memset(&cryp, 0, sizeof(cryp));
   /* Encrypt data.in to data.encrypted */
   cryp.ses = ctx->sess.ses;
   cryp.len = size;
   cryp.src = (void*)text;
   cryp.mac = digest;
   if (ioctl(ctx->cfd, CIOCCRYPT, &cryp)) {
      perror("ioctl(CIOCCRYPT)");
      return -1;
   }
   return 0;
}
int main()
{
   int cfd = -1, i;
   struct cryptodev_ctx ctx;
   uint8_t digest[20];
   char text[] = "The quick brown fox jumps over the lazy dog";
   uint8_t expected[] = "\x2f\xd4\xe1\xc6\x7a\x2d\x28\xfc\xed\x84\x9e\xe1\xbb\x76\xe7\x39\x1b\x93\xeb\x12";


   /* Open the crypto device */
   cfd = open("/dev/crypto", O_RDWR, 0);
   if (cfd < 0) {
      perror("open(/dev/crypto)");
      return 1;
   }
   /* Set close-on-exec (not really neede here) */
   if (fcntl(cfd, F_SETFD, 1) == -1) {
      perror("fcntl(F_SETFD)");
      return 1;
   }
   sha_ctx_init(&ctx, cfd, NULL, 0);
   sha_hash(&ctx, text, strlen(text), digest);
   sha_ctx_deinit(&ctx);
   printf("digest: ");
   for (i = 0; i < 20; i++) {
      printf("%02x:", digest[i]);
   }
   printf("\n");
   if (memcmp(digest, expected, 20) != 0) {
      fprintf(stderr, "SHA1 hashing failed\n");
      return 1;
   }
   /* Close the original descriptor */
   if (close(cfd)) {
      perror("close(cfd)");
      return 1;
   }
   return 0;
}

5.6 IOP8051 Module

/************************************************************/
//File name : iop8051.c
//Function : IOP8051 demo code
//Date : 2019-05-29
/************************************************************/
#include <errno.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <getopt.h>
#include "iop_ioctl.h"
int main(int argc, char** argv)
{
   int fd;
   int i,ret;
   int para;
   char data[3];


   for( ; ; )
   {
   int c;
   c = getopt(argc, argv, "abcd");
   if(c==-1)
      break;
   switch(c){
   case 'a':
      fd=open("/dev/sp_iop",O_RDWR);
      if(fd ==-1) {
         perror("update normal code fail");
      }
      ret = ioctl(fd, IOCTL_VALGET, 1);
      if(ret ==-1){
         perror("ioctl error ");
      }
      perror("update normal code success");
      printf("close fd");
      close(fd);
      break;
   case 'b':
      fd=open("/dev/sp_iop",O_RDWR);
      if(fd ==-1){
         perror("update standby code fail");
      }
      ret = ioctl(fd, IOCTL_VALGET, 2);
      if(ret ==-1){
         perror("ioctl error ");
      }
      perror("update standby code success");
      printf("close fd");
      close(fd);
      break;
   case 'c':
      fd=open("/dev/sp_iop",O_RDWR);
      if(fd ==-1){
         perror("get data fail");
      }
      ret = ioctl(fd, IOCTL_VALGET, 3);
      if(ret ==-1){
         perror("ioctl error ");
      }
      perror("get data success");
      printf("close fd");
      close(fd);
      break;
   case 'd':
      fd=open("/dev/sp_iop",O_RDWR);
      if(fd ==-1){
         perror("set data fail");
      }
      data[0] = 0x01;
      data[1] = 0x51;
      data[2] = 0x15;
      ret = write(fd, data, 3);
      if(ret ==-1){
         perror("write error ");
      }
      perror("set data success");
      printf("close fd");
      close(fd);
      break;
   }
   }
}