Просмотр исходного кода

soft_3rdpart:e24: add e24 client test code

add e24 client test code

Signed-off-by: shanlong.li <shanlong.li@starfivetech.com>
shanlong.li 2 лет назад
Родитель
Сommit
89d1d2ceca
4 измененных файлов с 426 добавлено и 0 удалено
  1. 25 0
      e24/client/Makefile
  2. 103 0
      e24/client/include/e24_host_shm.h
  3. 136 0
      e24/client/main.c
  4. 162 0
      e24/client/src/e24_host_linux.c

+ 25 - 0
e24/client/Makefile

@@ -0,0 +1,25 @@
+wrkdir := $(CURDIR)
+RISCVV := /home/lisl/sft-riscvpi-freedom-u-sdk/work/buildroot_initramfs/host/bin
+target := riscv64-buildroot-linux-gnu
+CROSS_COMPILE := $(RISCVV)/$(target)-
+
+INCLUDEDIR= -I$(wrkdir)/include
+CUR_SOURCE=${wildcard *.c src/*.c}
+#SOURCES=$(notdir $(CUR_SOURCE))
+CUR_OBJS=${patsubst %.c,%.o,$(CUR_SOURCE)}
+ROOT_DIR=$(CURDIR)/obj
+ALL_OBJ_POS=$(addprefix $(ROOT_DIR)/,$(CUR_OBJS))
+
+test_bin := $(wrkdir)/e24_share_mem
+
+$(info $(CUR_OBJS))
+.PHONY:all
+all:$(CUR_OBJS) $(test_bin)
+$(test_bin):$(ALL_OBJ_POS)
+	$(CROSS_COMPILE)gcc $(INCLUDEDIR) $^ -o $@
+
+$(CUR_OBJS):%.o:%.c
+	$(CROSS_COMPILE)gcc $(INCLUDEDIR) -c $^ -o $(ROOT_DIR)/$@
+
+clean:
+	rm -rf $(ALL_OBJ_POS) $(test_bin)

+ 103 - 0
e24/client/include/e24_host_shm.h

@@ -0,0 +1,103 @@
+/*********************************Copyright (c)*********************************************
+ **
+ **
+ **
+ **-------------------------------file info-------------------------------------------------
+ ** Vrsions:      V1.0
+ ** Filename:     e24_host_shm.h
+ ** Creator:      shanlong.li
+ ** Date:         2021/08/30
+ ** Description:  share memory and mailobx
+ **
+ **-------------------------------history----------------------------------------------
+ ** Name:         shanlong.li
+ ** Versions:     V1.0
+ ** Date:         2021/08/30
+ ** Description: 	
+ **
+ ** ----------------------------------------------------------------------------------------
+ ******************************************************************************************/
+#ifndef __E24_HOST_SHM_H__
+#define __E24_HOST_SHM_H__
+
+#include <stdlib.h>
+
+#define E24_IOCTL_MAGIC 'e'
+#define E24_IOCTL_SEND				_IO(E24_IOCTL_MAGIC, 1)
+#define E24_IOCTL_RECV		        _IO(E24_IOCTL_MAGIC, 2)
+#define E24_IOCTL_GET_CHANNEL		_IO(E24_IOCTL_MAGIC, 3)
+#define E24_IOCTL_FREE_CHANNEL		_IO(E24_IOCTL_MAGIC, 4)
+#define E24_IOCTL_ALLOC     		_IO(E24_IOCTL_MAGIC, 5)
+#define E24_IOCTL_FREE      		_IO(E24_IOCTL_MAGIC, 6)
+
+#define BUF_LEN     28
+
+typedef unsigned long _u64;
+typedef unsigned int  _u32;
+
+enum e24_status {
+	E24_STATUS_SUCCESS,
+	E24_STATUS_FAILURE,
+	E24_STATUS_PENDING,
+};
+
+struct e24_ioctl_alloc {
+    _u32 size;
+    _u32 align;
+    _u64 addr;
+};
+
+struct e24_ioctl_user {
+    _u32 flags;
+    _u32 in_data_size;
+    _u32 out_data_size;
+    _u64 in_data_addr;
+    _u64 out_data_addr;
+};
+
+struct e24_device {
+	int count;
+	int fd;
+};
+
+struct e24_host_ops {
+	void (*fn)(struct e24_device *device,struct e24_ioctl_user *data,enum e24_status *status);
+};
+
+struct e24_event {
+	struct e24_device *device;
+	struct e24_host_ops f_ops;
+};
+
+static inline void set_status(enum e24_status *status,enum e24_status v) {
+	if(status)
+		*status = v;
+}
+
+static inline void *alloc_refcounted(size_t sz) {
+	void *buf = calloc(1,sz);
+	struct e24_device *ref = buf;
+
+	if(ref)
+		ref->count = 1;
+
+	return buf;
+}
+
+struct e24_device *e24_open_device(int idx, enum e24_status *status);
+
+void e24_release_device(struct e24_device *device);
+
+struct e24_event *e24_event_init(struct e24_device *device,enum e24_status *status);
+
+void e24_event_release(struct e24_event *evt);
+
+int e24_alloc_buffer(struct e24_device *device,
+				struct e24_ioctl_alloc *alloc_buf,enum e24_status *status);
+
+void e24_release_buffer(struct e24_device * device,struct e24_ioctl_alloc *alloc_buf);
+
+void e24_run_command(struct e24_event *evt, void *in_data, size_t in_data_size,
+						void *out_data, size_t out_data_size, enum e24_status *status);
+
+#endif

+ 136 - 0
e24/client/main.c

@@ -0,0 +1,136 @@
+/*********************************Copyright (c)*********************************************
+ **
+ **
+ **
+ **-------------------------------file info-------------------------------------------------
+ ** Vrsions:      V1.0
+ ** Filename:     main.c
+ ** Creator:      shanlong.li
+ ** Date:         2021/08/30
+ ** Description:  share memory and mailbox
+ **
+ **-------------------------------history----------------------------------------------
+ ** Name:         shanlong.li
+ ** Versions:     V1.0
+ ** Date:         2021/08/30
+ ** Description: 	
+ **
+ ** ----------------------------------------------------------------------------------------
+ ******************************************************************************************/
+#include <stdio.h>
+#include <string.h>
+#include "e24_host_shm.h"
+
+int comm_local_buffer(void)
+{
+	enum e24_status status;
+	struct e24_device *device;
+	struct e24_event *evt;
+    char in_data[BUF_LEN],out_data[BUF_LEN];
+    int i;
+
+	device = e24_open_device(0,&status);
+	if(status == E24_STATUS_FAILURE) {
+		printf("open device fail\n");
+		return 0;
+	}
+
+	evt = e24_event_init(device,&status);
+	if(status == E24_STATUS_FAILURE) {
+		printf("event init fail\n");
+		goto _derro;
+	}
+	
+    printf("input data:\n");
+    for(i = 0;i< BUF_LEN; i++) {
+        in_data[i] = 27;
+        printf("%d,",in_data[i]);
+    }
+    printf("\n");
+
+	memset(out_data,0x0,BUF_LEN);
+	e24_run_command(evt,(void*)in_data,BUF_LEN,(void*)out_data,BUF_LEN,&status);
+    
+    printf("output data:\n");
+    for(i = 0;i< BUF_LEN; i++) {
+        printf("%d,",out_data[i]);
+    }
+	printf("\n");
+
+_error:
+	e24_release_buffer(device,NULL);
+	e24_event_release(evt);
+_derro:
+	e24_release_device(device);
+
+	return 0;
+}
+
+int comm_alloc_buffer(void) {
+	enum e24_status status = -1;
+	struct e24_device *device;
+	struct e24_event *evt;
+	struct e24_ioctl_alloc alloc_out_buf = {.size = BUF_LEN,};
+	struct e24_ioctl_alloc alloc_in_buf = {.size = BUF_LEN,};
+	int i;
+
+	device = e24_open_device(0,&status);
+	if(status == E24_STATUS_FAILURE) {
+		printf("open device fail\n");
+		return 0;
+	}
+
+	evt = e24_event_init(device,&status);
+	if(status == E24_STATUS_FAILURE) {
+		printf("event init fail\n");
+		goto _derror;
+	}
+
+	e24_alloc_buffer(device,&alloc_in_buf,&status);
+	if(status == E24_STATUS_FAILURE) {
+		printf("alloc in buffer failed\n");
+		goto _berror;
+	}
+
+	e24_alloc_buffer(device,&alloc_out_buf,&status);
+	if(status == E24_STATUS_FAILURE) {
+		printf("alloc out buffer failed\n");
+		goto _error;
+	}
+
+	printf("input data:\n");
+	for(i = 0;i< BUF_LEN; i++) {
+		((char*)alloc_in_buf.addr)[i] = 7;
+		printf("%d,",((char*)alloc_in_buf.addr)[i]);
+	}
+	printf("\n");
+
+	memset((char *)alloc_out_buf.addr,0x0,BUF_LEN);
+	e24_run_command(evt,(void*)alloc_in_buf.addr,BUF_LEN,(void*)alloc_out_buf.addr,BUF_LEN,&status);
+	
+	printf("output data:\n");
+	for(i = 0;i< BUF_LEN; i++) {
+		printf("%d,",((char *)alloc_out_buf.addr)[i]);
+	}
+	printf("\n");
+
+	e24_release_buffer(device,&alloc_out_buf);
+_error:
+	e24_release_buffer(device,&alloc_in_buf);
+_berror:
+	e24_event_release(evt);
+_derror:
+	e24_release_device(device);
+
+	return 0;
+
+}
+
+int main(int argc,char **argv) {
+	printf("buffer alloced from share memory:\n");
+	comm_alloc_buffer();
+	printf("local buffer:\n");
+	comm_local_buffer();
+	return 0;
+}
+

+ 162 - 0
e24/client/src/e24_host_linux.c

@@ -0,0 +1,162 @@
+/*********************************Copyright (c)*********************************************
+ **
+ **
+ **
+ **-------------------------------file info-------------------------------------------------
+ ** Vrsions:      V1.0
+ ** Filename:     e24_host_linux.c
+ ** Creator:      shanlong.li
+ ** Date:         2021/08/30
+ ** Description:  share memory and mailbox
+ **
+ **-------------------------------history----------------------------------------------
+ ** Name:         shanlong.li
+ ** Versions:     V1.0
+ ** Date:         2021/08/30
+ ** Description: 	
+ **
+ ** ----------------------------------------------------------------------------------------
+ ******************************************************************************************/
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <signal.h>
+
+#include "e24_host_shm.h"
+
+void e24_request_process(struct e24_device *device,
+					struct e24_ioctl_user *data,enum e24_status *status);
+
+volatile int msg_done = 0;
+
+void e24_irq_handler(int val)
+{
+	printf("e24 msg recv %d\n",val);
+	msg_done = 1;
+}
+
+void init_signal(int fd)
+{
+	int flags,ret;
+	
+	signal(SIGIO, e24_irq_handler);
+
+    ret = fcntl(fd, F_SETOWN, getpid());
+
+    flags = fcntl(fd, F_GETFL);
+
+    fcntl(fd, F_SETFL, flags | FASYNC);
+}
+
+struct e24_device *e24_open_device(int idx, enum e24_status *status)
+{
+	struct e24_device *device;
+
+	char name[sizeof("/dev/eboot")+sizeof(int)*4];
+	int fd;
+
+	sprintf(name,"/dev/eboot%u",idx);
+	fd = open(name,O_RDWR);
+	if(fd < 0) {
+		set_status(status,E24_STATUS_FAILURE);
+		return NULL;
+	}
+
+	device = alloc_refcounted(sizeof(*device));
+	if(!device) {
+		set_status(status,E24_STATUS_FAILURE);
+		return NULL;
+	}
+
+	device->fd = fd;
+	set_status(status, E24_STATUS_SUCCESS);
+	
+	return device;
+}
+
+void e24_release_device(struct e24_device *device) {
+	if(device) {
+		close(device->fd);
+		free(device);
+	}
+}
+
+struct e24_event *e24_event_init(struct e24_device *device,enum e24_status *status) {
+	struct e24_event *evt;
+
+	evt = malloc(sizeof(*evt));
+	if(!evt) {
+		set_status(status,E24_STATUS_FAILURE);
+		return NULL;
+	}
+
+	evt->device = device;
+	evt->f_ops.fn = e24_request_process;
+	return evt;
+}
+
+void e24_event_release(struct e24_event *evt) {
+
+    ioctl(evt->device->fd,E24_IOCTL_FREE_CHANNEL,NULL);
+
+	free(evt);
+}
+
+void e24_request_process(struct e24_device *device,
+					struct e24_ioctl_user *data,enum e24_status *status) {
+	int ret;
+	
+	data->flags = 4;
+	ret = ioctl(device->fd,E24_IOCTL_GET_CHANNEL,NULL);
+    ret = ioctl(device->fd,E24_IOCTL_SEND,data);
+
+	if(ret < 0)
+		set_status(status,E24_STATUS_FAILURE);
+	else
+		set_status(status, E24_STATUS_SUCCESS);
+}
+
+int e24_alloc_buffer(struct e24_device *device,
+				struct e24_ioctl_alloc *alloc_buf,enum e24_status *status) {
+	int ret;
+	
+    ret = ioctl(device->fd,E24_IOCTL_ALLOC,alloc_buf);
+    if(ret < 0)
+        set_status(status,E24_STATUS_FAILURE);
+	else
+		set_status(status, E24_STATUS_SUCCESS);
+
+	return 0;
+}
+
+void e24_release_buffer(struct e24_device * device,struct e24_ioctl_alloc *alloc_buf) {
+	if(alloc_buf)
+		ioctl(device->fd,E24_IOCTL_FREE,alloc_buf);
+}
+
+void e24_run_push(struct e24_event *evt,struct e24_ioctl_user *user_data, enum e24_status *status) {
+	evt->f_ops.fn(evt->device,user_data,status);
+}
+
+void e24_run_command(struct e24_event *evt, void *in_data, size_t in_data_size,
+						void *out_data, size_t out_data_size, enum e24_status *status) {
+	struct e24_ioctl_user *user_data;
+
+	user_data = malloc(sizeof(*user_data));
+	if(!user_data) {
+		set_status(status, E24_STATUS_FAILURE);
+		return;
+	}
+
+	user_data->in_data_addr = (_u64)in_data;
+	user_data->in_data_size = in_data_size;
+	user_data->out_data_addr = (_u64)out_data;
+	user_data->out_data_size = out_data_size;
+
+	e24_run_push(evt,user_data,status);
+	free(user_data);
+}
+
+