/** * * This function sends one byte using the device. This function operates in * polled mode and blocks until the data has been put into the TX FIFO register. * * @param BaseAddress contains the base address of the device. * @param Data contains the byte to be sent. * * @return None. * * @note None. * *****************************************************************************/ void XUartPs_SendByte(u32 BaseAddress, u8 Data) { /* * Wait until there is space in TX FIFO */ while (XUartPs_IsTransmitFull(BaseAddress)); /* * Write the byte into the TX FIFO */ XUartPs_WriteReg(BaseAddress, XUARTPS_FIFO_OFFSET, Data); }
/* * * This function sends a buffer that has been previously specified by setting * up the instance variables of the instance. This function is an internal * function for the XUartPs driver such that it may be called from a shell * function that sets up the buffer or from an interrupt handler. * * This function sends the specified buffer in either polled or interrupt * driven modes. This function is non-blocking. * * In a polled mode, this function only sends as much data as the TX FIFO * can buffer. The application may need to call it repeatedly to send the * entire buffer. * * In interrupt mode, this function starts the sending of the buffer, if not * the entire buffer has been sent, then the interrupt handler continues the * sending until the entire buffer has been sent. A callback function, as * specified by the application, will be called to indicate the completion of * sending. * * @param InstancePtr is a pointer to the XUartPs instance * * @return The number of bytes actually sent * * @note None. * *****************************************************************************/ u32 XUartPs_SendBuffer(XUartPs *InstancePtr) { u32 SentCount = 0U; u32 ImrRegister; /* * If the TX FIFO is full, send nothing. * Otherwise put bytes into the TX FIFO unil it is full, or all of the * data has been put into the FIFO. */ while ((!XUartPs_IsTransmitFull(InstancePtr->Config.BaseAddress)) && (InstancePtr->SendBuffer.RemainingBytes > SentCount)) { /* * Fill the FIFO from the buffer */ XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_FIFO_OFFSET, ((u32)InstancePtr->SendBuffer. NextBytePtr[SentCount])); /* * Increment the send count. */ SentCount++; } /* * Update the buffer to reflect the bytes that were sent from it */ InstancePtr->SendBuffer.NextBytePtr += SentCount; InstancePtr->SendBuffer.RemainingBytes -= SentCount; /* * If interrupts are enabled as indicated by the receive interrupt, then * enable the TX FIFO empty interrupt, so further action can be taken * for this sending. */ ImrRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress, XUARTPS_IMR_OFFSET); if (((ImrRegister & XUARTPS_IXR_RXFULL) != (u32)0) || ((ImrRegister & XUARTPS_IXR_RXEMPTY) != (u32)0)|| ((ImrRegister & XUARTPS_IXR_RXOVR) != (u32)0)) { XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_IER_OFFSET, ImrRegister | (u32)XUARTPS_IXR_TXEMPTY); } return SentCount; }
/** * * This function does a minimal test on the UART device using the hardware * interface. * * @param UartBaseAddress is the base address of the device * * @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful * * @note None. * **************************************************************************/ int UartPsEchoExample(u32 UartBaseAddress) { int Index; u32 Running; u8 RecvChar; u32 CntrlRegister; CntrlRegister = XUartPs_ReadReg(UartBaseAddress, XUARTPS_CR_OFFSET); /* * Enable TX and RX for the device */ XUartPs_WriteReg(UartBaseAddress, XUARTPS_CR_OFFSET, ((CntrlRegister & ~XUARTPS_CR_EN_DIS_MASK) | XUARTPS_CR_TX_EN | XUARTPS_CR_RX_EN)); /* * Initialize the send buffer bytes with a pattern to send and the * the receive buffer bytes to zero */ for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) { SendBuffer[Index] = Index + '0'; } /* * Send the entire transmit buffer. */ for (Index = 0; Index < TEST_BUFFER_SIZE; Index++) { /* * Wait until there is space in TX FIFO */ while (XUartPs_IsTransmitFull(UartBaseAddress)); /* * Write the byte into the TX FIFO */ XUartPs_WriteReg(UartBaseAddress, XUARTPS_FIFO_OFFSET, SendBuffer[Index]); } Running = TRUE; while (Running) { /* * Wait until there is data */ while (!XUartPs_IsReceiveData(UartBaseAddress)); RecvChar = XUartPs_ReadReg(UartBaseAddress, XUARTPS_FIFO_OFFSET); /* Change the capitalization */ if (('a' <= RecvChar) && ('z' >= RecvChar)) { // Convert the Capital letter to a small. RecvChar = RecvChar - 'a' + 'A'; } else if (('A' <= RecvChar) && ('Z' >= RecvChar)) { // Convert the small letter to a Capital. RecvChar = RecvChar - 'A' + 'a'; } else if (CHAR_ESC == RecvChar) { Running = FALSE; } /* Echo the character back */ XUartPs_WriteReg(UartBaseAddress, XUARTPS_FIFO_OFFSET, RecvChar); } return XST_SUCCESS; }