Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Valued Contributor III
707 Views

UART Help Please!

I already am able to read from the UART from my DE0 Board using NIOSII. I can hard-code a string and it will successfully send it to the PC Terminal. Now i got stuck trying to Write to the UART and have the board continuously read and send it back to terminal. What am i doing wrong? Here is my code. 

# include <stdio.h> 

 

# define RS232_UART_DATA ((volatile int*) 0x10001010)# define RS232_UART_CONTROL ((volatile int*) (0x10001010+4)) 

 

 

int main() 

 

unsigned char *pOutput; 

unsigned char *pinput; 

 

while(*pinput) 

//strings in C are zero terminated 

//if room in output buffer 

if((*RS232_UART_DATA)&0xffff0000 )  

//then read the next character 

*(pinput)= *RS232_UART_DATA;  

 

 

pOutput = pinput; 

 

while(*pOutput)  

//strings in C are zero terminated 

//if room in output buffer 

if((*RS232_UART_CONTROL)&0xffff0000 )  

//then write the next character 

*RS232_UART_DATA = (*pOutput++);  

}
0 Kudos
1 Reply
Highlighted
Valued Contributor III
5 Views

If all you're trying to do is send strings of characters over the UART, then it will probably be easier for you to just use the Hardware Abstraction Library (HAL). If you're familiar with linux programming, you'll know about stdin, stdout, and stderr. To send text to stdout, you use the printf function in C. The whole point of the Hardware Abstraction Library is to give you the same sort of API on the Nios instead of having to modify registers by hand. The BSP Editor (Right click your BSP Project->Nios II->BSP Editor) defines what your stdin, stdout, and stderr point to, which if you have a UART in your design, points to that by default. 

 

Long story short, if you have a UART in your design and all you want to do is send strings across, then go ahead and include <stdio.h> (like you're doing) and then call printf like so: 

 

printf("Some string"); 

 

This will send "Some string" across your UART. You can also use the scanf family of functions to receive data over the UART. 

 

If you don't want to use the HAL and want something somewhat lower level, the UART should include a driver library. Go into your BSP project, and there should be a "drivers" folder that contains header files and source code files. Page 7-8 in the Embedded IP Guide (https://www.altera.com/en_us/pdfs/literature/ug/ug_embedded_ip.pdf) goes over how to program using the UART driver (and basically says what I just said here). If what I said didn't help, hopefully somewhere in that document you can find something useful.
0 Kudos