Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Altera_Forum
Honored Contributor I
1,586 Views

UART using NIOS II

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
3 Replies
Altera_Forum
Honored Contributor I
377 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 ?

Altera_Forum
Honored Contributor I
377 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.
Altera_Forum
Honored Contributor I
377 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.
Reply