我正在编写一个使用SPI协议进行以太网通信的smartfusion cortex M3 FPGA处理器。
我想通过socket(socket编程)与pc通信。所以服务器需要一次处理两个连接,即接收和发送。显然,我想要多线程(通过使用两个并行线程一次接收和发送数据)。我尝试使用线程实现hello world示例。但是,我无法获得输出。我请你帮助我摆脱这个问题。
注意 - 仅使用多线程我需要一次只使用两个并行线程来获取输出,以便它生成发送和接收意味着在一个线程中发送并在一个线程中接收。
我尝试使用单线程并获得正确的输出。请通过以下代码向我建议执行多线程处理的步骤。
提前谢谢。
单线程代码:
#include "uip.h"
#include "psock.h"
#include"hello_world.h"
#include"pt.h"
#include "mss_gpio.h"
#include <unistd.h>
#include "./drivers/mss_spi/mss_spi.h"
#define INPUT_REG_BASE 0x40050000
#define OUTPUT_REG_BASE 0x40050000
#define OUTPUT_REG_BASE2 0x40050000
#include <stdio.h>
#include <string.h>
#include <semaphore.h>
#include <pthread.h>
#define STATE_OUTPUT 1
#define STATE_INPUT 0
#define ISO_E 0x45
const uint32_t master_tx_frame = 0xAA;
uint32_t* INPUT_IN = (uint32_t*)INPUT_REG_BASE;
uint32_t* OUTPUT_OUT = (uint32_t*)OUTPUT_REG_BASE;
uint32_t* OUTPUT_OUT2 = (uint32_t*)OUTPUT_REG_BASE2;
static void handle_connection(struct hello_world_state *s);
static
PT_THREAD(handle_input(struct hello_world_state *s)); // handling input section thread
static
PT_THREAD(handle_output(struct hello_world_state *s)); //output section
unsigned char temp = 0;
unsigned char str[128];
int size = 128;
void hello_world_init(void)
{
uip_listen(HTONS(4002));
}
void hello_world_appcall(void)
{
struct hello_world_state *s=(struct hello_world_state *)&(uip_conn->appstate);
if(uip_connected())
{
PSOCK_INIT(&s->pin, s->inputbuffer,sizeof(s->inputbuffer)); //initializing the protosockets
//PSOCK_INIT(&s->pout, s->outputbuffer,sizeof(s->outputbuffer));
}
handle_connection(s);
}
static void
handle_connection(struct hello_world_state *s) //calling function
{
if(s->state == STATE_INPUT)
{``
handle_input(s);
}
}
static
PT_THREAD(handle_input(struct hello_world_state *s)) //input function
{
PSOCK_BEGIN(&s->pin);
//PSOCK_SEND_STR(&s->p, "Hi.what is there name?\n");
//PSOCK_READTO(&s->p, '\n');
PSOCK_READBUF (&s->pin);
//PSOCK_SEND_STR(&s->pin, "HI");
PSOCK_SEND(&s->pin, s->inputbuffer,sizeof(s->inputbuffer));
if(s->inputbuffer[0] == 0xAC)
{
MSS_SPI_set_slave_select(&g_mss_spi0, MSS_SPI_SLAVE_0); //spi interfacing by using threads
MSS_SPI_transfer_frame(&g_mss_spi0, s->inputbuffer[4]);
MSS_SPI_clear_slave_select(&g_mss_spi0, MSS_SPI_SLAVE_0);
MSS_SPI_set_slave_select(&g_mss_spi1, MSS_SPI_SLAVE_0);
MSS_SPI_transfer_frame(&g_mss_spi1, s->inputbuffer[4]);
MSS_SPI_clear_slave_select(&g_mss_spi1, MSS_SPI_SLAVE_0);
}
else{
PSOCK_CLOSE(&s->pin);
PSOCK_END(&s->pin);
}
}
static PT_THREAD(handle_output(struct hello_world_state *s)) //output thread function
{
PSOCK_BEGIN(&s->pout);
PSOCK_SEND_STR(&s->pout,"HIIIIII");
PSOCK_CLOSE(&s->pout);
PSOCK_END(&s->pout);
}