Forum Discussion
Altera_Forum
Honored Contributor
10 years agoUART 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 wit...
Altera_Forum
Honored Contributor
10 years agoHi, 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.cyclops
New Contributor
3 years agoyou 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