hello there,
below is my complete code for reading 1 mbps RS-422 data and simultaneously store in 2D array.
my output data stream remain same....read same data four time. despite of reading only for one time using interrupt...
#include <stdio.h>
# include <string.h>
# include <ctype.h>
/* RS232 Related Files....*/
# include "alt_types.h"
# include "altera_avalon_pio_regs.h"
# include "sys/alt_irq.h"
# include "system.h"
# include <unistd.h>
/* MicroC/OS-II definitions */
# include "includes.h"
/* Simple Socket Server definitions */
# include "simple_socket_server.h"
/* Nichestack definitions */
# include "ipport.h"
# include "tcpport.h"
# define HIGH 1
# define LOW 0
int CheckSync(int RxSync,int IndexRxSync);
int CheckReset(int RxResetSync,int IndexRxResetSync);
/* A variable to hold the value of the button pio edge capture register. */
volatile int edge_capture;
volatile int edge_capture2;
# define client_port 90
# define client_addr "192.168.1.207" //2nd Nios-2 Board RS-422 Conversion----Tx TCP Image
# define server_port 80
# define server_addr "192.168.1.66" //Linux Machine ....Image Server
# define PktSize 640//1920 //1920
# define PktCount 400//1000//1080
unsigned int mPktSize = 640;//397;
unsigned int mPktCount = 420;//288;
unsigned int Xstart = 0; //397; to be added in mPktSize (640) 640+640=1280 //MUST BE LESS THEN 1280
unsigned int Ystart = 0; //460; to be added in mPktCount(450) 450+420=870 //MUST BE LESS THEN 440
unsigned char ImgBuff;
int strt_sync = {164,92,51};
int end_sync = {235,144,3,30};
static void handle_strt_interrupts(void* context2, alt_u32 id)
{
volatile int* edge_capture_ptr2 = (volatile int*) context2;
*edge_capture_ptr2 = IORD_ALTERA_AVALON_PIO_EDGE_CAP(STRT_OUT_BASE);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(STRT_OUT_BASE, 0);
IORD_ALTERA_AVALON_PIO_EDGE_CAP(STRT_OUT_BASE);
}
static void handle_load_interrupts(void* context, alt_u32 id)
{
volatile int* edge_capture_ptr = (volatile int*) context;
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(LOAD_BASE);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(LOAD_BASE, 0);
IORD_ALTERA_AVALON_PIO_EDGE_CAP(LOAD_BASE);
}
//------------------Function to Send Data Bytes To Remote Machine (NIOS_II)----------------
//n32sendFd :: this is socket identifier
//chLocalBuffer :: this is the local buffer used by socket to send data
//n32RemainingBytes :: total no of bytes left after each iteration
//n32BytesSend :: bytes send
fnSendFunc(int n32sendFd, char* chLocalBuffer, unsigned int n32RemainingBytes, unsigned int n32BytesSend)
{
unsigned int n32Send=0;
while (n32RemainingBytes>0)
{
if ((n32Send = send(n32sendFd, chLocalBuffer+n32BytesSend, n32RemainingBytes, 0))<=0)
{
perror ("DMS fnSendFunc:send call");
close(n32sendFd);
return(perror);
}
n32BytesSend += n32Send;
n32RemainingBytes -= n32Send;
}
return (n32BytesSend);
}
void SSSSimpleSocketServerTask()
{
int sockfd;
int rc,s1;
int LPktCount = 0,Count =0;//,mycount =0;
struct sockaddr_in servaddr,cliaddr;
unsigned int DataCount = 0;
void* edge_capture_ptr = (void*) &edge_capture;
void* edge_capture_ptr2 = (void*) &edge_capture2;
unsigned char test = 0;
ssize_t addrlen;
addrlen = sizeof(servaddr);
/* Enable LOAD interrupts. */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(LOAD_BASE, 0x1);
/* Reset the edge capture register. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(LOAD_BASE, 0x0);
alt_ic_isr_register(LOAD_IRQ_INTERRUPT_CONTROLLER_ID, LOAD_IRQ,handle_load_interrupts, edge_capture_ptr, 0x0);
/* Enable all 4 button interrupts. */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(STRT_OUT_BASE, 0xF);
/* Reset the edge capture register. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(STRT_OUT_BASE, 0x0);
alt_ic_isr_register(STRT_OUT_IRQ_INTERRUPT_CONTROLLER_ID, STRT_OUT_IRQ,handle_strt_interrupts, edge_capture_ptr2, 0x0);
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0xFF);
// RS232 Port Initialization
FILE* fp;
fp = fopen ("/dev/uart", "rb+"); //Open file for reading and writing
if(fp<0)
printf("RS232 Opening Error.!!\n");
else printf("RS232 Port Open....!!\n");
printf("\n\t\t\tReady to Receive Serial data From remote Synchronous System\n");
LPktCount = 0;
Count = 0;
fflush(stdin);
fflush(stdout);
edge_capture = 0;
edge_capture2 = 0;
while(1)
{
while(edge_capture2) //w.r.t. Start
{
edge_capture2 = 0;
LPktCount = 0;
Count = 0;
DataCount = 0;
do
{
if(edge_capture) //w.r.t. clock
{
edge_capture = 0; //reset generated interrupt
ImgBuff = IORD_ALTERA_AVALON_PIO_DATA(DATA_IN_BASE);
Count++;
if(Count == PktSize) //check end of packet-data -->640
{
Count = 0; //Point to Start of packet data
LPktCount++; //Increment packet Count
}
}
}while(LPktCount != (PktCount));
printf("\n\t#Count = %d\t LPktCount = %d",Count,LPktCount);
printf("\n\t#Synchronous Data received...!!");
printf("\n\t#Sending Data at RS232 Port at 115kbps...\n");
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0xF0);
printf("\n\t# Send Sync Word..");
for(Count = 0; Count<=2;Count++)
{
fprintf(fp,"%c",strt_sync);//Data);
printf("\t%d",strt_sync);
}
printf("\n");
Xstart = 0;
Ystart = 0;
LPktCount = 0;//mPktCount+Ystart
for(LPktCount = 0+Ystart; LPktCount<mPktCount;LPktCount++)
{
for(DataCount = 0; DataCount<mPktSize;DataCount++)
{
fprintf(fp,"%c",ImgBuff);
}
printf(">");
}
LPktCount = 0;
Count = 0;
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0xFC);
printf("\n\t#Send Done....\n\t#Ready to receive Data.\n");
}
}
printf("\n\t\t\tConnection Closed ...");
close(sockfd);
fclose(fp);
printf("\n STRT_OUT = %d\n LOAD = %d",edge_capture,edge_capture2);
} //Close Simple_SOcket_Server
int CheckSync(int RxSync,int IndexRxSync)
{
int SyncWord = {164,92,51};
int count;
int MatchCount = 0;
for(count = 0;count<3;count++)
{
if(RxSync == SyncWord)
MatchCount++;
IndexRxSync = (IndexRxSync + 1) % 3;
}
if(MatchCount == 3)
return 1;
else
return 0;
}