Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
16 years ago

Improving my NIOS II code to improve speed.

HI, This is my NIOS II code but I think it is not that great becasue it cannot deal with data >200 Hz.

Does anyone have any idea for changes to improve the speed?

Thank you very much in advance, do you not know how much this is appericated.

# include <stdio.h># include <string.h># include <ctype.h>

# include <stdio.h>

/* Nichestack definitions */# include "ipport.h"# include "tcpport.h"# include "libport.h"# include "osport.h"# include "altera_avalon_pio_regs.h"# include "myprogram.h"# include "command.h"

int OpenSocket(int tcpip_port)

{

int fd_listen;

struct sockaddr_in addr;

if ((fd_listen = socket(PF_INET, SOCK_STREAM, 0)) < 0)

return TCPERR1;

addr.sin_family = AF_INET;

addr.sin_port = htons(tcpip_port);

addr.sin_addr.s_addr = INADDR_ANY;

if ((bind(fd_listen,(struct sockaddr *)&addr,sizeof(addr))) < 0)

return TCPERR2;

if ((listen(fd_listen,1)) < 0)

return TCPERR3;

return fd_listen;

}

void srv_tcpip()

{

int sock, fd_client, fd_tmp, nfds, status, idx;

int len, retval, n_read;

fd_set rset, wset, rset_, wset_;

struct sockaddr_in incoming_addr;

alt_u8 buf[255];

alt_u32 in_data;

int rdy = 0, rdy_old = 0;

if((sock = OpenSocket(DATA_PORT)) < 0) {

fprintf(stderr, "srv_tcpip: unable to open socket\n");

return;

}

FD_ZERO(&rset);

FD_ZERO(&wset);

FD_SET(sock, &rset);

nfds = sock+1;

fd_client = -1;

for(;;) {

wset_ = wset; rset_ = rset;

retval = select(nfds, &rset_, &wset_, NULL, NULL);

if(retval < 0) {fprintf(stderr, "srv_tcpip: failure on select\n"); return;}

if(FD_ISSET(sock, &rset_)) {

len = sizeof(incoming_addr);

fd_tmp = accept(sock, (struct sockaddr*)&incoming_addr, &len);

if(fd_client == -1) {

fd_client = fd_tmp;

FD_SET(fd_client, &rset);

FD_SET(fd_client, &wset);

nfds = fd_client+1;

} else {

close(fd_tmp);

}

} else if(fd_client != -1 && FD_ISSET(fd_client, &rset_)) {

n_read = recv(fd_client, buf, 80, 0);

if(n_read <= 0) {

close(fd_client);

FD_CLR(fd_client, &rset);

FD_CLR(fd_client, &wset);

nfds = sock + 1;

fd_client = -1;

}

} else if(fd_client != -1 && FD_ISSET(fd_client, &wset_)) {

//IOWR_ALTERA_AVALON_PIO_DATA(RRDY_PIO_BASE, 1);

rdy = IORD_ALTERA_AVALON_PIO_DATA(EMPTY_PIO_BASE);

if(rdy_old != rdy) {

rdy_old = rdy;

if(rdy == 0) {

in_data = IORD_ALTERA_AVALON_PIO_DATA(READ_PIO_BASE);

status = send(fd_client, (void *)&in_data, 4, 0);

}

}

continue;

}

}

}

/* Command thread: handle commands from tcpip client */

void cmd_tcpip()

{

int sock, fd_client, fd_tmp, nfds, status;

int len, retval, n_read;

fd_set rset, wset, rset_, wset_;

struct sockaddr_in incoming_addr;

alt_u8 buf[256];

if((sock = OpenSocket(CMD_PORT)) < 0) {

fprintf(stderr, "cmd_tcpip: unable to open socket\n");

return;

}

FD_ZERO(&rset);

FD_ZERO(&wset);

FD_SET(sock, &rset);

nfds = sock+1;

fd_client = -1;

for(;;) {

wset_ = wset; rset_ = rset;

retval = select(nfds, &rset_, &wset_, NULL, NULL);

if(retval < 0) {fprintf(stderr, "cmd_tcpip: failure on select\n"); return;}

if(FD_ISSET(sock, &rset_)) {

len = sizeof(incoming_addr);

fd_tmp = accept(sock, (struct sockaddr*)&incoming_addr, &len);

if(fd_client == -1) {

fd_client = fd_tmp;

FD_SET(fd_client, &rset);

nfds = fd_client+1;

} else {

close(fd_tmp);

}

} else if(fd_client != -1 && FD_ISSET(fd_client, &rset_)) {

n_read = recv(fd_client, buf, 80, 0);

if(n_read <= 0) {

close(fd_client);

FD_CLR(fd_client, &rset);

nfds = sock + 1;

fd_client = -1;

} else {

printf("Received %d bytes\n", n_read);

status = do_cmd(buf);

status = send(fd_client, (void *)buf, n_read, 0);

}

}

continue;

}

}
No RepliesBe the first to reply