From b6f6fbd79331b8467c044ebfa545ac3d4ac09116 Mon Sep 17 00:00:00 2001
From: linhuang <linhuang@eurecom.fr>
Date: Tue, 15 Oct 2013 07:17:37 +0000
Subject: [PATCH] add USRP functions

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@4191 818b1a75-f10b-46b9-bf7c-635c3b92a50f
---
 targets/ARCH/USRP/USERSPACE/LIB/Makefile.inc  |  21 +
 targets/ARCH/USRP/USERSPACE/LIB/def.h         |  18 +
 .../ARCH/USRP/USERSPACE/LIB/openair_usrp.cpp  | 365 ++++++++++++++++++
 targets/RTAI/USER/TOOLS/thread_ipc.c          | 204 ++++++++++
 targets/RTAI/USER/TOOLS/thread_ipc.h          |  62 +++
 targets/RTAI/USER/make_for_usrp.sh            |  36 ++
 6 files changed, 706 insertions(+)
 create mode 100644 targets/ARCH/USRP/USERSPACE/LIB/Makefile.inc
 create mode 100644 targets/ARCH/USRP/USERSPACE/LIB/def.h
 create mode 100644 targets/ARCH/USRP/USERSPACE/LIB/openair_usrp.cpp
 create mode 100644 targets/RTAI/USER/TOOLS/thread_ipc.c
 create mode 100644 targets/RTAI/USER/TOOLS/thread_ipc.h
 create mode 100755 targets/RTAI/USER/make_for_usrp.sh

diff --git a/targets/ARCH/USRP/USERSPACE/LIB/Makefile.inc b/targets/ARCH/USRP/USERSPACE/LIB/Makefile.inc
new file mode 100644
index 0000000000..eeb24e7d73
--- /dev/null
+++ b/targets/ARCH/USRP/USERSPACE/LIB/Makefile.inc
@@ -0,0 +1,21 @@
+# compiles the openair_usrp
+
+#OPENAIRTARGETS_DIR ?=../../../..
+
+#OPENAIROBJS += $(OPENAIRTARGETS_DIR)/ARCH/USRP/USERSPACE/LIB/openair0_lib.o
+#CFLAGS += -I$(OPENAIRTARGETS_DIR)/ARCH/USRP/USERSPACE/LIB -I$(OPENAIRTARGETS_DIR)/ARCH/USRP/DEFS
+
+
+#example: example.o $(OPENAIROBJS) 
+#	gcc -o $@ $(CFLAGS) -lm $(OPENAIROBJS) $<
+
+#openair_usrp.o:
+#	$(CXX) -c openair_usrp.cpp -o openair_usrp.o
+
+#clean:
+#	rm -f *.o *~
+#a	rm -f example
+USRP_OBJ += $(OPENAIR_TARGETS)/ARCH/USRP/USERSPACE/LIB/openair_usrp.o
+USRP_FILE_OBJ += $(OPENAIR_TARGETS)/ARCH/USRP/USERSPACE/LIB/openair_usrp.cpp
+USRP_CFLAGS += -I$(OPENAIR_TARGETS)/ARCH/USRP/USERSPACE/LIB/
+
diff --git a/targets/ARCH/USRP/USERSPACE/LIB/def.h b/targets/ARCH/USRP/USERSPACE/LIB/def.h
new file mode 100644
index 0000000000..107da023d3
--- /dev/null
+++ b/targets/ARCH/USRP/USERSPACE/LIB/def.h
@@ -0,0 +1,18 @@
+#include <stdarg.h>
+
+#define SAMPLES_PER_FRM 76800
+#define SAMPLES_PER_SLOT 3840
+#define HW_offset 32
+#define N_slot_offset 4
+#define T_start 3840*20*100
+
+void format_printf(int flag,const char * fmt, ...)
+{
+	if(flag)
+	{
+		va_list args;
+		va_start(args,fmt);
+		vprintf(fmt,args);
+		va_end(args);
+	}
+}
diff --git a/targets/ARCH/USRP/USERSPACE/LIB/openair_usrp.cpp b/targets/ARCH/USRP/USERSPACE/LIB/openair_usrp.cpp
new file mode 100644
index 0000000000..678824d3e9
--- /dev/null
+++ b/targets/ARCH/USRP/USERSPACE/LIB/openair_usrp.cpp
@@ -0,0 +1,365 @@
+#include <string.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <uhd/utils/thread_priority.hpp>
+#include <uhd/usrp/multi_usrp.hpp>
+#include <boost/format.hpp>
+#include <iostream>
+#include <complex>
+#include <fstream>
+#include <cmath>
+#include "def.h"
+
+using namespace std;
+using std::ofstream;
+using std::ifstream;
+
+// --------------------------------
+// contant variables
+// --------------------------------
+#define PI 3.1415926535898
+#define SAM_RATE 6.25e6 // 6.25e6 = 100e6/16
+#define DL_FREQ 880e6
+#define UL_FREQ 835e6
+
+// --------------------------------
+// variables for USRP configuration
+// --------------------------------
+uhd::usrp::multi_usrp::sptr tx_usrp;
+uhd::usrp::multi_usrp::sptr rx_usrp;
+
+//create a send streamer and a receive streamer
+uhd::stream_args_t stream_args_short("sc16");//complex short
+uhd::stream_args_t stream_args_float("fc32");//complex float
+uhd::tx_streamer::sptr tx_stream;
+uhd::rx_streamer::sptr rx_stream;
+
+uhd::tx_metadata_t tx_md;
+uhd::rx_metadata_t rx_md;
+
+//setup variables and allocate buffer
+uhd::async_metadata_t async_md;
+
+double tx_timeout;
+double rx_timeout;
+
+double rx_freq;
+
+uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
+
+// --------------------------------
+// variables for OAI buffers
+// --------------------------------
+int oai_tx_buff[SAMPLES_PER_FRM];
+int oai_rx_buff[SAMPLES_PER_FRM];
+int oai_rx_buff_temp[SAMPLES_PER_FRM];
+volatile unsigned long long tx_timestamp,rx_timestamp,clock_usrp,tmp_clock;
+volatile int w_pos_usrp_rcv = 0; // value range [0:76800-1]
+volatile int r_pos_usrp_send = 0; // value range [0:76800-1]
+volatile int w_slot_usrp_rcv, r_slot_usrp_send; // slot index for USRP thread. value range [0:19]
+volatile int r_slot_idx, t_slot_idx; // slot index for eNB thread. value range [0;19]
+volatile int send_slot_missed = 0;
+
+// --------------------------------
+// Debug and output control
+// --------------------------------
+int g_usrp_debug=0;
+int num_underflows=0;
+int num_overflows=0;
+int num_seq_errors=0;
+
+
+extern "C"
+{
+	void UHD_init(void);
+	void UHD_init_display(void);
+	void Init_send(void);
+	void Init_recv(void);
+	void send_data(int hw_frm_head_pos);
+	void recv_data(int hw_frm_head_pos);
+	void send_end(void);
+	void set_freq(int freq_offset);
+	char UE_flag;
+	void tx_errorcode_handler(void);
+	void sig_int_handler(void);
+}
+
+// --------------------------------
+// USRP initial
+// --------------------------------
+void UHD_init(void)
+{
+	uhd::set_thread_priority_safe();
+	std::string args = "send_frame_size=3840,recv_frame_size=3840";
+	uhd::device_addrs_t device_addr = uhd::device::find(args);
+	if (device_addr.size() == 0 )
+	{
+		std::cerr<<"No USRP Device Found"<<std::endl;
+		exit(1);
+	}
+	else
+		std::cout<<"USRP Device Found"<<std::endl<<device_addr[0].to_pp_string()<<std::endl;
+	tx_usrp = uhd::usrp::multi_usrp::make(args);
+	rx_usrp = uhd::usrp::multi_usrp::make(args);
+
+	tx_stream = tx_usrp->get_tx_stream(stream_args_short);
+	rx_stream = rx_usrp->get_rx_stream(stream_args_short);
+
+	//Lock mboard clocks
+	tx_usrp->set_clock_source("internal");
+	rx_usrp->set_clock_source("internal");
+
+	tx_usrp->set_tx_rate(SAM_RATE);
+	if(UE_flag)
+		tx_usrp->set_tx_freq(UL_FREQ);
+	else
+		tx_usrp->set_tx_freq(DL_FREQ);
+	tx_usrp->set_tx_gain(0);
+	tx_usrp->set_tx_bandwidth(25e6);
+	tx_usrp->set_tx_antenna("TX/RX");
+
+	rx_usrp->set_rx_rate(SAM_RATE);
+	if(UE_flag)
+	{
+		rx_usrp->set_rx_freq(DL_FREQ);
+		rx_freq = DL_FREQ;
+	}
+	else
+	{
+		rx_usrp->set_rx_freq(UL_FREQ);
+		rx_freq = UL_FREQ;
+	}
+	rx_usrp->set_rx_gain(0);
+	rx_usrp->set_rx_bandwidth(25e6);
+	rx_usrp->set_rx_antenna("RX2");
+
+	std::cout <<std::endl<< boost::format("Setting device timestamp to 0...") << std::endl;
+	tx_usrp->set_time_now(uhd::time_spec_t(0.0));
+	rx_usrp->set_time_now(uhd::time_spec_t(0.0));
+}
+
+// --------------------------------
+// USRP information display
+// --------------------------------
+void UHD_init_display(void)
+{
+	std::cout << std::endl<<boost::format("Actual TX sample rate: %fMSps...") % (tx_usrp->get_tx_rate()/1e6) << std::endl;
+	std::cout << boost::format("Actual RX sample rate: %fMSps...") % (rx_usrp->get_rx_rate()/1e6) << std::endl;
+
+	std::cout << boost::format("Actual TX frequency: %fGHz...") % (tx_usrp->get_tx_freq()/1e9) << std::endl;
+	std::cout << boost::format("Actual RX frequency: %fGHz...") % (rx_usrp->get_rx_freq()/1e9) << std::endl;
+
+	std::cout << boost::format("Actual TX gain: %f...") % (tx_usrp->get_tx_gain()) << std::endl;
+	std::cout << boost::format("Actual RX gain: %f...") % (rx_usrp->get_rx_gain()) << std::endl;
+
+	std::cout << boost::format("Actual TX bandwidth: %fM...") % (tx_usrp->get_tx_bandwidth()/1e6) << std::endl;
+	std::cout << boost::format("Actual RX bandwidth: %fM...") % (rx_usrp->get_rx_bandwidth()/1e6) << std::endl;
+
+	std::cout << boost::format("Actual TX antenna: %s...") % (tx_usrp->get_tx_antenna()) << std::endl;
+	std::cout << boost::format("Actual RX antenna: %s...") % (rx_usrp->get_rx_antenna()) << std::endl;
+
+	std::cout << boost::format("Device TX timestamp: %f...") % (tx_usrp->get_time_now().get_real_secs()) << std::endl;
+	std::cout << boost::format("Device RX timestamp: %f...") % (rx_usrp->get_time_now().get_real_secs()) << std::endl << std::endl;
+}
+
+// -----------------------
+//send init
+// -----------------------
+void Init_send(void)
+{
+	//reset send buffer
+	memset(oai_tx_buff , 0 , sizeof(oai_tx_buff));
+
+	// the first slot sent shoud be the 0+N_slot_offset.
+	r_slot_usrp_send = N_slot_offset;
+	//send constantly
+	tx_md.start_of_burst = false;
+	tx_md.end_of_burst = false;
+	tx_md.has_time_spec = true; // set the transmission start time
+	tx_timestamp = T_start + r_slot_usrp_send * SAMPLES_PER_SLOT;
+	tx_md.time_spec = uhd::time_spec_t::from_ticks(tx_timestamp,SAM_RATE);
+	tx_timeout = tx_timestamp/SAM_RATE + 0.1;
+}
+
+// -----------------------
+//receive init
+// -----------------------
+void Init_recv(void)
+{
+	//reset receive buffer
+	memset(oai_rx_buff , 0 , sizeof(oai_rx_buff));
+
+	// initialize the rx stream
+	stream_cmd.stream_now = false;//When true, the device will begin streaming ASAP. When false, the device will begin streaming at a time specified by time_spec.
+	stream_cmd.num_samps = 0;
+	rx_timestamp = T_start + HW_offset;//Device timestamp and time of send sample
+	stream_cmd.time_spec = uhd::time_spec_t::from_ticks(rx_timestamp,SAM_RATE);
+	rx_usrp->issue_stream_cmd(stream_cmd);
+
+	//the first call to recv() will block this many seconds before receiving
+	rx_timeout = rx_timestamp/SAM_RATE + 0.1; //timeout (delay before receive + padding)
+
+	// the first clock number = rx_timestamp
+	clock_usrp = rx_timestamp;
+	w_pos_usrp_rcv = 0;
+	w_slot_usrp_rcv = 19;
+}
+
+// -----------------------
+// send function
+// -----------------------
+void send_data(int hw_frm_head_pos)
+{
+
+	int diff;
+	// Handle the frame wrap-arround
+	if ((r_slot_usrp_send<4)&&(t_slot_idx>=16)){
+		diff = t_slot_idx - (r_slot_usrp_send + 20) ;
+	}
+	else if((r_slot_usrp_send>=16)&&(t_slot_idx<4)){
+		diff = (t_slot_idx + 20) - r_slot_usrp_send;
+	}
+	else{
+		diff = t_slot_idx - r_slot_usrp_send;
+	}
+	format_printf(g_usrp_debug, "[send] diff: %d\n",diff);
+	if (diff<0){} // data is not ready for sending
+	else if (diff>1) // sending is late
+	{
+		format_printf(g_usrp_debug, "Sending is late\n");
+		r_slot_usrp_send = t_slot_idx;
+		// count the missed sending slots
+		send_slot_missed += (diff - 1);
+	}
+	else // diff = 0,1 Send the data, one or two slots
+	{
+		for (int ii=0;ii<diff+1;ii++)
+		{
+
+			format_printf(g_usrp_debug,"[send] r_usrp: %d t_slot: %d w_usrp: %d w_pos_usrp_rcv: %d\n", r_slot_usrp_send,t_slot_idx,w_slot_usrp_rcv, w_pos_usrp_rcv);
+			format_printf(g_usrp_debug,"[send] clock_usrp: %llu tx_timestamp: %llu\n", clock_usrp, tx_timestamp);
+
+			// sending
+			size_t num_tx_samps;
+			r_pos_usrp_send = (t_slot_idx * SAMPLES_PER_SLOT + hw_frm_head_pos) % SAMPLES_PER_FRM;
+			if((r_pos_usrp_send + SAMPLES_PER_SLOT) <= SAMPLES_PER_FRM)
+			{
+				num_tx_samps = tx_stream->send((& oai_tx_buff[r_pos_usrp_send]),SAMPLES_PER_SLOT, tx_md, tx_timeout);
+			}
+			else
+			{
+				int tmp_len = SAMPLES_PER_FRM - r_pos_usrp_send;
+				num_tx_samps = tx_stream->send((& oai_tx_buff[r_pos_usrp_send]),tmp_len, tx_md, tx_timeout);
+				num_tx_samps = tx_stream->send((& oai_tx_buff[0]),SAMPLES_PER_SLOT - tmp_len, tx_md, tx_timeout);
+			}
+
+			tx_md.has_time_spec = false;
+
+			tx_timeout = 0.1;
+
+			r_slot_usrp_send++;
+			if(r_slot_usrp_send==20) r_slot_usrp_send=0;
+
+		}
+	}
+}
+
+// --------------------------------
+// handle TX error codes
+// --------------------------------
+void tx_errorcode_handler(void){
+	if (not tx_stream->recv_async_msg(async_md)) {}
+	else{
+		switch(async_md.event_code){
+			case uhd::async_metadata_t::EVENT_CODE_BURST_ACK:
+				return;
+			case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW:
+				format_printf(g_usrp_debug,"[send] USRP TX UNDERFLOW!\n");
+				num_underflows++;
+				break;
+			case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW_IN_PACKET:
+				format_printf(g_usrp_debug,"[send] USRP TX UNDERFLOW IN_PACKET!\n");
+				break;
+			case uhd::async_metadata_t::EVENT_CODE_SEQ_ERROR:
+			case uhd::async_metadata_t::EVENT_CODE_SEQ_ERROR_IN_BURST:
+				num_seq_errors++;
+				break;
+			default:
+				//std::cerr << "Event code: " << async_md.event_code << std::endl;
+				//std::cerr << "Unexpected event on async recv, continuing..." << std::endl;
+				break;
+		}
+	}
+}
+// -----------------------
+// send end of burst
+// -----------------------
+void send_end()
+{
+	//send a mini EOB packet
+	tx_md.end_of_burst = true;
+	tx_stream->send("", 0, tx_md);
+	tx_md.end_of_burst = false;
+	format_printf(1,"num_underflows: %d   num_overflows: %d   num_seq_errors: %d\n",num_underflows,num_overflows,num_seq_errors);
+}
+
+// -----------------------
+// receive function
+// -----------------------
+void recv_data(int hw_frm_head_pos)
+{
+	size_t num_rx_samps;
+
+	//volatile unsigned long long tmp_clock;
+	num_rx_samps = rx_stream->recv((&oai_rx_buff_temp[0]), SAMPLES_PER_SLOT,rx_md,rx_timeout);
+
+	//after the first receiving, reset the timeout value
+	rx_timeout = 0.1;
+	tmp_clock = rx_md.time_spec.to_ticks(SAM_RATE);
+
+	// Update writing position
+	w_pos_usrp_rcv = (w_pos_usrp_rcv + (int)(tmp_clock - clock_usrp))%SAMPLES_PER_FRM;
+
+	// Copy data into the PHY layer buffer
+	if((w_pos_usrp_rcv+num_rx_samps) <= SAMPLES_PER_FRM){
+		memcpy(& oai_rx_buff[w_pos_usrp_rcv],& oai_rx_buff_temp[0],num_rx_samps*sizeof(int));
+	}
+	else{
+		int tmp_len = SAMPLES_PER_FRM - w_pos_usrp_rcv;
+		memcpy(& oai_rx_buff[w_pos_usrp_rcv],& oai_rx_buff_temp[0],tmp_len*sizeof(int));
+		memcpy(& oai_rx_buff[0],& oai_rx_buff_temp[tmp_len],(num_rx_samps-tmp_len)*sizeof(int));
+	}
+
+	w_slot_usrp_rcv = (w_pos_usrp_rcv - hw_frm_head_pos + num_rx_samps - SAMPLES_PER_SLOT)%SAMPLES_PER_FRM / SAMPLES_PER_SLOT;
+
+	// update clock
+	clock_usrp = tmp_clock;
+
+	// show log
+	format_printf(g_usrp_debug,"[recv] w_pos: %d num_rx_samps=%d clock_usrp=%llu w_usrp: %d\n",w_pos_usrp_rcv,num_rx_samps,clock_usrp,w_slot_usrp_rcv);
+
+	//handle the error code
+	switch(rx_md.error_code){
+		case uhd::rx_metadata_t::ERROR_CODE_NONE:
+			break;
+		case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW:
+			format_printf(g_usrp_debug,"[recv] USRP RX OVERFLOW!\n");
+			num_overflows++;
+			break;
+		case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT:
+			format_printf(g_usrp_debug, "[recv] USRP RX TIMEOUT!\n");
+			break;
+		default:
+			format_printf(g_usrp_debug, "[recv] Unexpected error on RX, Error code: 0x%x\n",rx_md.error_code);
+			break;
+	}
+}
+
+// -----------------------
+// set RX frequency in UE
+// -----------------------
+void set_freq(int freq_offset)
+{
+	rx_usrp->set_rx_freq(rx_freq + freq_offset);
+}
diff --git a/targets/RTAI/USER/TOOLS/thread_ipc.c b/targets/RTAI/USER/TOOLS/thread_ipc.c
new file mode 100644
index 0000000000..01cb86799c
--- /dev/null
+++ b/targets/RTAI/USER/TOOLS/thread_ipc.c
@@ -0,0 +1,204 @@
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <pthread.h>
+#include <sched.h>
+
+#include "thread_ipc.h"
+
+g_thread_ipc_t thread_ipc = {0};
+
+void loop_buffer_reset(buffer_t *loop_buf)
+{
+	int i;
+
+	for (i = 0; i < BUFFERMAX; i++) {
+		loop_buf[i].subframe_num = -1;
+	}
+
+	return;
+}
+
+static void loop_buffer_init(loop_buffer_op_t *loop_buffer)
+{
+	loop_buffer->packet_num = 0;
+	loop_buffer->isfull = 0;
+	loop_buffer->isempty = 1;
+
+	pthread_mutex_init(&loop_buffer->buffer_mutex, NULL);
+
+	pthread_cond_init(&loop_buffer->full_cond, NULL);
+	pthread_cond_init(&loop_buffer->empty_cond, NULL);
+
+	loop_buffer_reset(loop_buffer->loop_buf);
+
+	return;
+}
+
+static void sync_buffer_init(sync_buffer_t *sync_buffer)
+{
+	sync_buffer->decoding_subframe_num = 0;
+	pthread_mutex_init(&sync_buffer->buffer_mutex, NULL);
+
+	return;
+}
+
+int thread_ipc_init(void)
+{
+	//printf("recv %d\n", thread_ipc.sync_buffer.decoding_subframe_num);
+	thread_ipc.ue_sync_state = 0;
+	thread_ipc.rx_timestamp = 0;
+	thread_ipc.tx_timestamp = 0;
+	thread_ipc.current_subframe = 0;
+
+	pthread_mutex_init(&thread_ipc.dl_decode_mutex, NULL);
+	pthread_mutex_lock(&thread_ipc.dl_decode_mutex);
+
+	pthread_mutex_init(&thread_ipc.ul_send_mutex, NULL);
+	pthread_mutex_lock(&thread_ipc.ul_send_mutex);
+
+	pthread_mutex_init(&thread_ipc.sync_mutex, NULL);
+	pthread_mutex_lock(&thread_ipc.sync_mutex);
+
+	loop_buffer_init(&thread_ipc.loop_buffer);
+	sync_buffer_init(&thread_ipc.sync_buffer);
+
+	return 0;
+}
+
+int thread_ipc_deinit(void)
+{
+	pthread_mutex_destroy(&thread_ipc.ul_send_mutex);
+	pthread_mutex_destroy(&thread_ipc.sync_mutex);
+	pthread_mutex_destroy(&thread_ipc.dl_decode_mutex);
+
+	pthread_mutex_destroy(&thread_ipc.loop_buffer.buffer_mutex);
+	pthread_cond_destroy(&thread_ipc.loop_buffer.full_cond);
+	pthread_cond_destroy(&thread_ipc.loop_buffer.empty_cond);
+
+	pthread_mutex_destroy(&thread_ipc.sync_buffer.buffer_mutex);
+
+	return 0;
+}
+
+int set_thread_attr(pthread_attr_t *attr, int policy, int priority, int cpuid)
+{
+	struct sched_param param;
+	cpu_set_t cpu_info;
+
+	pthread_attr_init(attr);
+
+	if (pthread_attr_setschedpolicy(attr, policy) != 0) {
+		perror("pthread_attr_setschedpolicy");
+		return -1;
+	}
+
+	param.sched_priority = priority;
+	if (pthread_attr_setschedparam(attr, &param) != 0) {
+		perror("pthread_attr_setschedparam");
+		return -1;
+	}
+
+	CPU_ZERO(&cpu_info);
+	CPU_SET(cpuid, &cpu_info);
+	if (pthread_attr_setaffinity_np(attr,sizeof(cpu_set_t),&cpu_info)) {
+		perror("pthread_attr_setaffinity_np");
+		return -1;
+	}
+
+	if (pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED) != 0) {
+		perror("pthread_attr_setinheritsched");
+		return -1;
+	}
+
+	return 0;
+}
+
+
+int find_subframe_num(unsigned long long current_subframe_num, buffer_t *buf, int *flag)
+{
+	long long tmp;
+	int i;
+	
+	tmp = current_subframe_num;
+	for ( i = 0; i < HIGHBUFFER + 1; i++)
+	{
+		if(tmp == buf[i].subframe_num)
+		{ 
+			return i;
+		} else if (tmp < buf[i].subframe_num) {
+			*flag = 1;	
+		}
+	}
+
+	return -1;
+}
+
+int ue_unsync_thread_ipc_reset(void)
+{
+	thread_ipc.ue_sync_state = 0;                 
+	
+	pthread_mutex_lock(&thread_ipc.loop_buffer.buffer_mutex);
+	if (thread_ipc.loop_buffer.isempty) {
+		pthread_cond_signal(&thread_ipc.loop_buffer.empty_cond);
+	}
+
+	if (thread_ipc.loop_buffer.isfull) {
+		pthread_cond_signal(&thread_ipc.loop_buffer.full_cond);
+	}
+
+	thread_ipc.loop_buffer.packet_num = 0;
+	thread_ipc.loop_buffer.isfull = 0;
+	thread_ipc.loop_buffer.isempty = 1;
+
+	loop_buffer_reset(thread_ipc.loop_buffer.loop_buf);
+	pthread_mutex_unlock(&thread_ipc.loop_buffer.buffer_mutex);
+
+	thread_ipc.current_subframe = 0;
+
+	return 0;
+}
+void bind_thread2kernel(int cpu_id)
+{
+        cpu_set_t mask;
+        cpu_set_t get;
+        int i;
+        int num = sysconf(_SC_NPROCESSORS_CONF);
+        //printf("system has %d processor(s) by super\n", num);
+        CPU_ZERO(&mask);
+        CPU_SET(cpu_id, &mask);
+        if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
+                fprintf(stderr, "set thread affinity failed\n");
+        }
+        /*CPU_ZERO(&get);
+        if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
+                fprintf(stderr, "get thread affinity failed\n");
+        }
+        for (i = 0; i < num; i++) {
+                if (CPU_ISSET(i, &get)) {
+                        printf("thread %d is running in processor %d\n", (int)pthread_self(), i);
+                }
+        }
+        if (CPU_ISSET(cpu_id, &get)) {
+          printf("thread %d is running in processor %d by super\n", (int)pthread_self(), cpu_id);
+          }*/
+}
+void get_thread2kernel(void)
+{
+        cpu_set_t get;
+        int i;
+        int num = sysconf(_SC_NPROCESSORS_CONF);
+        printf("system has %d processor(s) by super\n", num);
+        CPU_ZERO(&get);
+        if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
+                fprintf(stderr, "get thread affinity failed\n");
+        }
+        for (i = 0; i < num; i++) {
+                if (CPU_ISSET(i, &get)) {
+                        printf("The thread %d is running in processor %d by super\n", (int)pthread_self(), i);
+                }
+        }
+}
+
diff --git a/targets/RTAI/USER/TOOLS/thread_ipc.h b/targets/RTAI/USER/TOOLS/thread_ipc.h
new file mode 100644
index 0000000000..3377d9f043
--- /dev/null
+++ b/targets/RTAI/USER/TOOLS/thread_ipc.h
@@ -0,0 +1,62 @@
+#ifndef __THREAD_IPC_H__
+#define __THREAD_IPC_H__
+
+
+#define SUB_FRAME_LENGTH 7680
+#define FRAME_LENGTH 76800
+
+#define UE_UL_DELAY	6				/*设置上行组帧在同步时钟基础上的延时(子帧个数)*/
+#define UE_UL_SEND_DELAY 6			/*上行发送子帧号在接收子帧时间戳上的延时(子帧个数)*/		
+
+#define BUFFERMAX 5					/*环形缓冲区个数*/
+#define LOWBUFFER 3                 /*环形缓冲区下限, 不能为0*/
+#define HIGHBUFFER 4                /*环形缓冲区上限*/
+
+typedef struct {
+	long long subframe_num;			/*子帧编号*/
+	int buffer[SUB_FRAME_LENGTH * 2];	/*一子帧数据*/
+}buffer_t;
+
+typedef struct {
+	int packet_num;					/*环形缓冲区数据包计数器,表示环形缓冲区有效数据包个数*/
+	int isfull;						/*标记环形缓冲区有效数据包是否达到上限标志*/
+	int isempty;					/*标记环形缓冲区有效数据包是否达到下限标志*/
+
+	pthread_mutex_t buffer_mutex;	/*环形缓冲区操作保护锁*/ 
+	pthread_cond_t full_cond;		/*环形缓冲区上限条件变量,配合isfull使用 */
+	pthread_cond_t empty_cond;		/*环形缓冲区下限条件变量,配合isempty使用*/
+
+	buffer_t loop_buf[BUFFERMAX];	/*环形缓冲区*/
+}loop_buffer_op_t;
+
+typedef struct {
+	int decoding_subframe_num;		/*待解码子帧的编号*/
+	pthread_mutex_t buffer_mutex;	/*对sync_buffer临界区的保护锁*/
+	int sync_buffer[SUB_FRAME_LENGTH * 10];  /*同步线程与下行解码线程数据共享缓冲区*/
+}sync_buffer_t;
+
+typedef struct {
+	unsigned int rx_timestamp;		/*接收数据包第一个sample时间戳*/
+	unsigned int tx_timestamp;		/*待发送数据包的时间戳*/
+	unsigned long long current_subframe;	/*当前子帧编号*/
+
+	int ue_sync_state;				/*表示UE是否在同步状态,0表示失同步,1表示同步*/
+	pthread_mutex_t sync_mutex;		/*根据ue_sync_state标志,唤醒组帧线程*/
+
+	pthread_mutex_t ul_send_mutex;	/*用于sync线程唤醒发送线程发送数据*/
+	pthread_mutex_t dl_decode_mutex;/*下行解码保护锁,用于sync线程唤醒解码线程解码*/
+
+	loop_buffer_op_t loop_buffer;
+	sync_buffer_t	sync_buffer;
+}g_thread_ipc_t;
+
+void loop_buffer_reset(buffer_t *loop_buf);
+int thread_ipc_init(void);
+int thread_ipc_deinit(void);
+int set_thread_attr(pthread_attr_t *attr, int policy, int priority, int cpuid);
+int find_subframe_num(unsigned long long current_subframe_num, buffer_t *buf, int *flag);
+int ue_unsync_thread_ipc_reset(void);
+
+extern g_thread_ipc_t thread_ipc;
+
+#endif
diff --git a/targets/RTAI/USER/make_for_usrp.sh b/targets/RTAI/USER/make_for_usrp.sh
new file mode 100755
index 0000000000..802fd10773
--- /dev/null
+++ b/targets/RTAI/USER/make_for_usrp.sh
@@ -0,0 +1,36 @@
+#!/bin/bash
+
+############### make nasmesh.ko ###############
+sudo rmmod nasmesh
+#cd ${OPENAIR2_DIR} && make nasmesh_netlink.ko
+#cd ${OPENAIR2_DIR}/NAS/DRIVER/MESH/RB_TOOL/ && make
+#make all
+sudo insmod $OPENAIR2_DIR/NAS/DRIVER/MESH/nasmesh.ko
+
+############## Ethernet config  ####################
+sudo ifconfig eth0 mtu 4000
+sudo sysctl -w net.core.wmem_max=1048576
+sudo sysctl -w net.core.rmem_max=50000000
+
+############## rtai modules ###################
+if test \! -c /dev/rtai_shm; then
+        mknod -m 666 /dev/rtai_shm c 10 254
+fi
+for n in `seq 0 9`; do
+        f=/dev/rtf$n
+        if test \! -c $f; then
+                mknod -m 666 $f c 150 $n
+        fi
+done
+modprobe rtai_hal
+modprobe rtai_sched
+modprobe rtai_fifos
+modprobe rtai_sem
+modprobe rtai_mbx
+modprobe rtai_msg
+
+############## make  ###################
+make lte-softmodem-usrp NAS=1 USRP=1 XFORMS=1 RTAI=1 HARD_RT=1 #DRIVER2013=1
+#make lte-softmodem NAS=1 XFORMS=1 USRP=0 RTAI=1 DRIVER2013=1
+echo DONE!
+exit 0
-- 
GitLab