Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

UART using NIOS II

Altera_Forum
Honored Contributor II
3,220 Views

Hi again,  

I already posted a topic about nios ii altera ans esp8266, I fixed all my problems with the esp8266, now i still struggling with my uart C code, my transmit function works well, but with my receive function its always a mess, its sometimes only if I don't use it, I just tried to return from my receive function two values: the received message and its size, I tried the following code but it does not work at all: 

void uart_write(char* comm) { message* mess ; int count=0; strcpy(mess->send,comm);//command mess->lenS=strlen(mess->send);//command length for (count=0;count<mess->lenS;count++) { if(ALTERA_AVALON_UART_STATUS_TRDY_MSK==TRDY)// check if transmit ready { IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,mess->send);//send character Delay(600);//wait the end of transmission } } if (ALTERA_AVALON_UART_STATUS_TRDY_MSK == TRDY)// check if transmit ready IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,0X0D);// \r if (ALTERA_AVALON_UART_STATUS_TRDY_MSK == TRDY)// check if transmit ready IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,0x0A);// \n } void receive(char *buffer, int size){ alt_u8 status; status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE); while((status & RRDY)==RRDY ){ // check if reception ready{ buffer=IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);//receive character status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE); //update status register size++; } } int main() { uart_init(); int size=0; char *buffer; uart_write("AT+CWLAP"); receive(&buffer,size); printf("%d \n %s",size,buffer); return 0; }  

please guys I need help I'm lost 

Thanks a lot
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
2,011 Views

Why don't you use IRQ for flagging reception ? I presume that the printf("%d \n %s",size,buffer) command is preventing the program to correctly receive incoming new data. Anyway, where is the infinite loop, supposed while(1), in your code ?

0 Kudos
Altera_Forum
Honored Contributor II
2,011 Views

Hi, I changed my receive function into the following one, but when I use those receive/transmit function too many times in the same main, I keep receive strange things in my nios II console. 

int receive(char *buffer){ int size=0,j=10000000; alt_u8 status; while(j){ status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE); while(status & RRDY){ // check if reception ready{ buffer=IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);//receive character status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE); //update status register size++; } j--; } return size; } int main () { char *buffer=malloc(200); int size=0; size=receive(buffer); printf("%d\n%s",size,buffer); return 0; }  

usually I use those functions in another C file.  

I don't need an infinite loop in my function, because I have to use my receive/transmit over 10 times in the main function.
0 Kudos
cyclops
Beginner
1,184 Views

you are not writing to the buffer correctly. You need to increment the buffer index as well as check that you are not attempting to write outside the bounds of the allocated memory

 

if(size < 200)

buffer[size] = IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);//receive character

else

//buffer full

0 Kudos
Altera_Forum
Honored Contributor II
2,011 Views

Hello Ayelha, 

I do not completely understand what you are trying to do. But, assuming you will be running the main function multiple times, as you say it, you might have to do with a memory leak: every time you circle through the main function, you allocate 200 bytes from the heap. It is good practice to release these bytes (malloc - free). If you do not to this, you could run out of memory after a while. Then you will get unstable program behaviour. 

Some other thing you might want to take a look at: in Your send function, there could be an error: if you want to test a flag in a register use ((value & mask) = mask) i do have the impression that you have written something else. This would explain while you need a delay in your function where as, under normal circumstances, you do need wait functions if you are using the status flags of the uart. Wait functions are not good for optimal performance and baudrate independency. 

Best Regards, 

Johi.
0 Kudos
Reply