From 3ac8d3bd7fe0e2bc4b2e424bcc7a13c4596aa97d Mon Sep 17 00:00:00 2001 From: luojects Date: Fri, 26 Aug 2022 18:14:17 +0800 Subject: [PATCH] openamp demo: zephyr shell support multi-thread process * delete API bringup_endpoint, and move APIs to openamp_module.h * add thread mutex lock to protect communication with zephyr * use ctrl-d to exit current thread, and release pty device * add a new zephyr shell user: userB Signed-off-by: luojects --- mcs/README.md | 8 +++----- mcs/modules/openamp_module.c | 4 ++++ mcs/modules/openamp_module.h | 9 +++++++++ mcs/modules/rpmsg_module.c | 17 ++++------------- mcs/modules/rpmsg_module.h | 9 --------- mcs/openamp_demo/rpmsg_main.c | 6 ------ mcs/rpmsg_pty_demo/rpmsg_main.c | 12 +++++++++++- mcs/rpmsg_pty_demo/rpmsg_pty.c | 31 +++++++++++-------------------- mcs/rpmsg_pty_demo/rpmsg_pty.h | 2 ++ 9 files changed, 44 insertions(+), 54 deletions(-) diff --git a/mcs/README.md b/mcs/README.md index fd83a205..128cbc22 100644 --- a/mcs/README.md +++ b/mcs/README.md @@ -88,10 +88,8 @@ OpenAMP包括如下三大重要组件: #### 用户开发 参考demo实现流程,当前提供了多个API,包含在libopenamp.a中,供用户使用和二次开发,用户无需感知openamp实现细节。 -1. 调用openamp_init,初始化remoteproc、virtio、rpmsg。 +1. 调用openamp_init,初始化remoteproc、virtio、rpmsg,Linux端与Client OS建立配对的endpoint,供消息收发使用。 -2. 调用bringup_endpoint,启动endpoint动态配对,Linux端与Client OS建立配对的endpoint,供消息收发使用。 +2. 调用receive_message、send_message收发消息。 -3. 调用receive_message、send_message收发消息。 - -4. 调用openamp_deinit,释放openamp资源。 \ No newline at end of file +3. 调用openamp_deinit,释放openamp资源。 \ No newline at end of file diff --git a/mcs/modules/openamp_module.c b/mcs/modules/openamp_module.c index e23a85b8..20ce22ae 100644 --- a/mcs/modules/openamp_module.c +++ b/mcs/modules/openamp_module.c @@ -39,6 +39,8 @@ int openamp_init(void) { int ret; struct remoteproc *rproc; + unsigned char message[100]; + int len; rproc = create_remoteproc(); if (!rproc) { @@ -61,6 +63,8 @@ int openamp_init(void) sleep(5); /* wait for zephyr booting */ virtio_init(); + (void)receive_message(message, sizeof(message), &len); /* name service: endpoint matching */ + return 0; } diff --git a/mcs/modules/openamp_module.h b/mcs/modules/openamp_module.h index f5be2bf0..69abb2c9 100644 --- a/mcs/modules/openamp_module.h +++ b/mcs/modules/openamp_module.h @@ -9,7 +9,16 @@ extern char *target_binfile; extern char *target_binaddr; +/* initialize openamp module, including remoteproc, virtio, rpmsg */ int openamp_init(void); + +/* release openamp resource */ int openamp_deinit(void); +/* message standard receive interface */ +int receive_message(unsigned char *message, int message_len, int *real_len); + +/* message standard send interface */ +int send_message(unsigned char *message, int len); + #endif \ No newline at end of file diff --git a/mcs/modules/rpmsg_module.c b/mcs/modules/rpmsg_module.c index 5078f14e..a97c34bb 100644 --- a/mcs/modules/rpmsg_module.c +++ b/mcs/modules/rpmsg_module.c @@ -6,16 +6,7 @@ static unsigned char received_data[2048] = {0}; static unsigned int received_len = 0; -struct rpmsg_endpoint *ep; /* current using endpoint */ - -void bringup_endpoint(struct rpmsg_endpoint *ept_inst) -{ - unsigned char message[100]; - int len; - - ep = ept_inst; - (void)receive_message(message, sizeof(message), &len); /* name service: endpoint matching */ -} +struct rpmsg_endpoint ept_inst; int endpoint_cb(struct rpmsg_endpoint *ept, void *data, size_t len, uint32_t src, void *priv) @@ -29,12 +20,12 @@ int endpoint_cb(struct rpmsg_endpoint *ept, void *data, static void rpmsg_service_unbind(struct rpmsg_endpoint *ept) { (void)ept; - rpmsg_destroy_ept(ep); + rpmsg_destroy_ept(&ept_inst); } void ns_bind_cb(struct rpmsg_device *rdev, const char *name, uint32_t dest) { - (void)rpmsg_create_ept(ep, rdev, name, + (void)rpmsg_create_ept(&ept_inst, rdev, name, RPMSG_ADDR_ANY, dest, endpoint_cb, rpmsg_service_unbind); @@ -94,5 +85,5 @@ _cleanup: int send_message(unsigned char *message, int len) { - return rpmsg_send(ep, message, len); + return rpmsg_send(&ept_inst, message, len); } diff --git a/mcs/modules/rpmsg_module.h b/mcs/modules/rpmsg_module.h index ddeee24c..cb700df2 100644 --- a/mcs/modules/rpmsg_module.h +++ b/mcs/modules/rpmsg_module.h @@ -8,13 +8,4 @@ /* callback of ns */ void ns_bind_cb(struct rpmsg_device *rdev, const char *name, uint32_t dest); -/* bringup a endpoint, receive_message and send_message are based on this */ -void bringup_endpoint(struct rpmsg_endpoint *ept_inst); - -/* message standard receive interface */ -int receive_message(unsigned char *message, int message_len, int *real_len); - -/* message standard send interface */ -int send_message(unsigned char *message, int len); - #endif \ No newline at end of file diff --git a/mcs/openamp_demo/rpmsg_main.c b/mcs/openamp_demo/rpmsg_main.c index 857327d2..cd467e42 100644 --- a/mcs/openamp_demo/rpmsg_main.c +++ b/mcs/openamp_demo/rpmsg_main.c @@ -10,15 +10,9 @@ char *target_binaddr; int rpmsg_app_master(void) { int ret; - unsigned int message = 0; - int len; - struct rpmsg_endpoint ept_inst; printf("start processing OpenAMP demo...\n"); - /* To ensure thread saftety, endpoint instance should be a local variable */ - bringup_endpoint(&ept_inst); - while (message < 99) { ret = send_message((unsigned char*)&message, sizeof(message)); if (ret < 0) { diff --git a/mcs/rpmsg_pty_demo/rpmsg_main.c b/mcs/rpmsg_pty_demo/rpmsg_main.c index 0ca15596..3bf642ed 100644 --- a/mcs/rpmsg_pty_demo/rpmsg_main.c +++ b/mcs/rpmsg_pty_demo/rpmsg_main.c @@ -1,5 +1,6 @@ #include #include +#include #include "rpmsg_pty.h" #include "openamp_module.h" @@ -14,13 +15,22 @@ int rpmsg_app_master(void) printf("Multi-thread processing user requests...\n"); - /* zephyr shell, open with screen */ + pthread_mutex_init(&mutex, NULL); + + /* userA, zephyr shell, open with screen */ if (pthread_create(&tida, NULL, shell_user, NULL) < 0) { perror("userA pthread_create"); return -1; } pthread_detach(tida); + /* userB, zephyr shell, open with screen */ + if (pthread_create(&tidb, NULL, shell_user, NULL) < 0) { + perror("userB pthread_create"); + return -1; + } + pthread_detach(tidb); + while (1); return 0; diff --git a/mcs/rpmsg_pty_demo/rpmsg_pty.c b/mcs/rpmsg_pty_demo/rpmsg_pty.c index 90e1d840..900e6342 100644 --- a/mcs/rpmsg_pty_demo/rpmsg_pty.c +++ b/mcs/rpmsg_pty_demo/rpmsg_pty.c @@ -5,10 +5,13 @@ #include #include #include +#include #include "openamp_module.h" /* define the keys according to your terminfo */ -#define KEY_CTRL_C 3 +#define KEY_CTRL_D 4 + +pthread_mutex_t mutex; void open_pty(int *pfdm, int *pfds, const char *pty_name) { @@ -43,10 +46,6 @@ void *shell_user(void *arg) unsigned char cmd[1]; unsigned char reply[2048]; int reply_len; - struct rpmsg_endpoint ept_inst; - - /* To ensure thread saftety, endpoint instance should be defined in thread */ - bringup_endpoint(&ept_inst); open_pty(&fdm, &fds, "shell"); @@ -57,21 +56,21 @@ void *shell_user(void *arg) return (void*)-1; } - if (cmd[0] == KEY_CTRL_C) { /* special key: ctrl+c */ + if (cmd[0] == KEY_CTRL_D) { /* special key: ctrl+d */ close(fds); close(fdm); return (void*)0; /* exit this thread, the same as pthread_exit */ } + pthread_mutex_lock(&mutex); + ret = send_message(cmd, 1); /* send command to rtos */ - if (ret < 0) { - printf("shell_user: send_message(%s) failed: %d\n", cmd, ret); - return (void*)-1; - } + ret |= receive_message(reply, sizeof(reply), &reply_len); /* receive reply from rtos */ + + pthread_mutex_unlock(&mutex); - ret = receive_message(reply, sizeof(reply), &reply_len); /* receive reply from rtos */ if (ret < 0) { - printf("shell_user: receive_message failed: %d\n", ret); + printf("shell_user: send(%s)/receive(%s) message failed: %d\n", cmd, reply, ret); return (void*)-1; } @@ -91,10 +90,6 @@ void *console_user(void *arg) int fdm, fds; unsigned char reply[2048]; int reply_len; - struct rpmsg_endpoint ept_inst; - - /* To ensure thread saftety, endpoint instance should be defined in thread */ - bringup_endpoint(&ept_inst); /* open PTY, pts binds to terminal, using screen to open pts */ open_pty(&fdm, &fds, "console"); @@ -123,10 +118,6 @@ void *log_user(void *arg) unsigned char log[2048] = {0}; int log_len; const char *log_file = "/tmp/zephyr_log.txt"; - struct rpmsg_endpoint ept_inst; - - /* To ensure thread saftety, endpoint instance should be defined in thread */ - bringup_endpoint(&ept_inst); ret = receive_message(log, sizeof(log), &log_len); /* receive log from rtos */ if (ret < 0) { diff --git a/mcs/rpmsg_pty_demo/rpmsg_pty.h b/mcs/rpmsg_pty_demo/rpmsg_pty.h index b3f3399e..3668849d 100644 --- a/mcs/rpmsg_pty_demo/rpmsg_pty.h +++ b/mcs/rpmsg_pty_demo/rpmsg_pty.h @@ -5,4 +5,6 @@ void *shell_user(void *arg); void *console_user(void *arg); void *log_user(void *arg); +extern pthread_mutex_t mutex; + #endif \ No newline at end of file -- Gitee