I had some problems when I studied Nios's UART and did some sharing.
Nios II You can use the round-the-clock method to read information in two ways.
Pre-Test:
1. Add a UART to the Qsys and add the new UART Rx, TX pair in the Quartus.
Question one: Use the round-of-the-way reader program
When I use the following code to go to the wheel, it is time to wait until the 62nd pen is finished, and the status Register's Rrdy will be judged normally.
1 /*2 * UART test:polling test, design by Shih-an Li3 * 4 */5 6#include <stdio.h>7#include <system.h>8#include"altera_avalon_uart.h"9#include"Altera_avalon_uart_regs.h"Ten#include"Alt_types.h" One#include"sys/alt_irq.h" A#include <unistd.h> -#include <io.h> - the - intMainvoid) - { - intRxdata=0, k=0, t=0; +Unsignedintstatus; - +Iowr_altera_avalon_uart_control (Uart_tx_base,0x1c0);//Clear Status A at while(1) - { -Status =Iord_altera_avalon_uart_status (uart_tx_base); Read Status Register - if(Status & Altera_avalon_uart_status_trdy_msk) {//check Trdy flag - if(Status &0x040){//Transmit shift Register is empty -printf"Uart Ready (0x%2x) and send%3d", status, T); inIOWR (Uart_tx_base,1, t++);//Send TX Data - } to } + -Usleep100000); theStatus =Iord_altera_avalon_uart_status (uart_tx_base); *printf"status=0x%x%d\n", status,k++); $ if(Status &Altera_avalon_uart_status_rrdy_msk)//check Rrdy flagPanax Notoginseng { -Rxdata = Iord (Uart_tx_base,0); theprintf"Your character Rxd is:\t%d%d\n", Rxdata, k); + } A the } + return 0; -}
The results are as follows:
1Uart Ready (0x60) and send -status=0x60 -2Uart Ready (0x60) and send -status=0x60 -3Uart Ready (0x60) and send Astatus=0x60 A4Uart Ready (0x60) and send +status=0xe0 +5Your character Rxd is: + the6Uart Ready (0x60) and send thestatus=0xe0 the7Your character Rxd is: the -8Uart Ready (0x60) and send -status=0xe0 -9Your character Rxd is: - $TenUart Ready (0x60) and send $
And what I'm curious about is catching him. Hard waveform to see, only read once
Status =Iord_altera_avalon_uart_status (uart_tx_base);
The result of the hard body actually sent several times the chip_select message, and Fig.1 the last time Chip_select incredibly is to read addr 0 position rxdata Register, so the Rx_char_ready message will be pulled down, After that FIG2 will continue to read several times in Address 2 position of the status register, which has become 0x60.
It is only after 62 strokes that the normal value will be collected.
Fig 1. UART Waveform
Fig 2. Continued fig 1. Waveform
Method Two: Use the middle mode
The code is as follows, using the Nios II HAL mode to read and write the information.
This mode uses the ISR function to judge whether the Rrdy flag is 1, which is to collect the value, so it is judged to be more accurate.
1 /*2 * Nios uses the HAL command to open and read the UART setup, design by Shih-an Li3 */4 5#include <stdio.h>6#include <system.h>7#include <unistd.h>//define USLEEP ()8#include <stddef.h>//define NULL9#include <fcntl.h>//define O_nonblockTen #defineBuf_size 128 One A - intMain () - { the intICount, Iuart_rxcount; - intFdterm;//FileDescriptor returned by OPEN -FILE *fpterm;//FILE pointer returned by Fdopen -UnsignedCharucuart1_rxbuffer[255], uctx[ -]; + //O_accmode All Access mode -Fdterm = open ("/dev/uart_tx", O_accmode |o_nonblock); +Fpterm = Fdopen (Fdterm,"rw+"); A if(fpterm) { atfprintf (Fpterm,"Uart_tx started\r\n");//Check Initial output -Iuart_rxcount = Fread (&ucuart1_rxbuffer,1,sizeof(Ucuart1_rxbuffer), fpterm); -Usleep100000); -printf"received:%s\r\n", Ucuart1_rxbuffer); - } -uctx[0]=0; in while(1) { -Write (Fdterm, &uctx[0],1);//Write TX value to UART touctx[0]++;//TX value + 1 + //read UART data to Rxbuffer and return receive data number -Iuart_rxcount = Read (Fdterm, &ucuart1_rxbuffer,sizeof(Ucuart1_rxbuffer)); the for(icount=0; icount<iuart_rxcount; icount++) { * //fprintf (fpterm, "received:%s\r\n", uart1_rxbuffer); $printf"%d\n", Ucuart1_rxbuffer[icount]);Panax Notoginseng } -Usleep10000); the } + return 0; A}
Perform the following results
Received:uart_tx STARTED 0 1 2 3 4 5 6 7 8
So the recommended or Hal way to read the periphery of the material is more than the information lost problem.
[Nios] [UART] Some questions about using the UART?