//***************************************************************************** // //! Reads data from a ring buffer. //! //! \param psRingBuf points to the ring buffer to be read from. //! \param pui8Data points to where the data should be stored. //! \param ui32Length is the number of bytes to be read. //! //! This function reads a sequence of bytes from a ring buffer. //! //! \return None. // //***************************************************************************** void RingBufRead(tRingBufObject *psRingBuf, uint8_t *pui8Data, uint32_t ui32Length) { uint32_t ui32Temp; // // Check the arguments. // ASSERT(psRingBuf != NULL); ASSERT(pui8Data != NULL); ASSERT(ui32Length != 0); // // Verify that data is available in the buffer. // ASSERT(ui32Length <= RingBufUsed(psRingBuf)); // // Read the data from the ring buffer. // for(ui32Temp = 0; ui32Temp < ui32Length; ui32Temp++) { pui8Data[ui32Temp] = RingBufReadOne(psRingBuf); } }
//***************************************************************************** // //! Reads data from a ring buffer. //! //! \param ptRingBuf points to the ring buffer to be read from. //! \param pucData points to where the data should be stored. //! \param ulLength is the number of bytes to be read. //! //! This function reads a sequence of bytes from a ring buffer. //! //! \return None. // //***************************************************************************** void RingBufRead(tRingBufObject *ptRingBuf, unsigned char *pucData, unsigned long ulLength) { unsigned long ulTemp; // // Check the arguments. // ASSERT(ptRingBuf != NULL); ASSERT(pucData != NULL); ASSERT(ulLength != 0); // // Verify that data is available in the buffer. // ASSERT(ulLength <= RingBufUsed(ptRingBuf)); // // Read the data from the ring buffer. // for(ulTemp = 0; ulTemp < ulLength; ulTemp++) { pucData[ulTemp] = RingBufReadOne(ptRingBuf); } }
//***************************************************************************** // //! Reads a single byte of data from a ring buffer. //! //! \param psRingBuf points to the ring buffer to be written to. //! //! This function reads a single byte of data from a ring buffer. //! //! \return The byte read from the ring buffer. // //***************************************************************************** uint8_t RingBufReadOne(tRingBufObject *psRingBuf) { uint8_t ui8Temp; // // Check the arguments. // ASSERT(psRingBuf != NULL); // // Verify that space is available in the buffer. // ASSERT(RingBufUsed(psRingBuf) != 0); // // Write the data byte. // ui8Temp = psRingBuf->pui8Buf[psRingBuf->ui32ReadIndex]; // // Increment the read index. // UpdateIndexAtomic(&psRingBuf->ui32ReadIndex, 1, psRingBuf->ui32Size); // // Return the character read. // return(ui8Temp); }
//***************************************************************************** // //! Reads a single byte of data from a ring buffer. //! //! \param ptRingBuf points to the ring buffer to be written to. //! //! This function reads a single byte of data from a ring buffer. //! //! \return The byte read from the ring buffer. // //***************************************************************************** unsigned char RingBufReadOne(tRingBufObject *ptRingBuf) { unsigned char ucTemp; // // Check the arguments. // ASSERT(ptRingBuf != NULL); // // Verify that space is available in the buffer. // ASSERT(RingBufUsed(ptRingBuf) != 0); // // Write the data byte. // ucTemp = ptRingBuf->pucBuf[ptRingBuf->ulReadIndex]; // // Increment the read index. // UpdateIndexAtomic(&ptRingBuf->ulReadIndex, 1, ptRingBuf->ulSize); // // Return the character read. // return(ucTemp); }
//***************************************************************************** // //! Returns number of bytes available in a ring buffer. //! //! \param psRingBuf is the ring buffer object to check. //! //! This function returns the number of bytes available in the ring buffer. //! //! \return Returns the number of bytes available in the ring buffer. // //***************************************************************************** uint32_t RingBufFree(tRingBufObject *psRingBuf) { // // Check the arguments. // ASSERT(psRingBuf != NULL); // // Return the number of bytes available in the ring buffer. // return((psRingBuf->ui32Size - 1) - RingBufUsed(psRingBuf)); }
// UART interrupt handler // For each received byte, pushes it into the buffer. // For each transmitted byte, read the next available from the buffer. void USART2_IRQHandler() { // TX the next available char on the buffer if (USART_GetITStatus(USART2, USART_IT_TC)) { USART_ClearITPendingBit(USART2, USART_IT_TC); if (RingBufUsed(&txBuffer)) USART_SendData(USART2, (uint8_t)RingBufReadOne(&txBuffer)); } // RX and copy the data to buffer if (USART_GetITStatus(USART2, USART_IT_RXNE)) { if (!RingBufFull(&rxBuffer)) { RingBufWriteOne(&rxBuffer, (uint8_t)USART_ReceiveData(USART2)); } } }
//***************************************************************************** // //! Remove bytes from the ring buffer by advancing the read index. //! //! \param psRingBuf points to the ring buffer from which bytes are to be //! removed. //! \param ui32NumBytes is the number of bytes to be removed from the buffer. //! //! This function advances the ring buffer read index by a given number of //! bytes, removing that number of bytes of data from the buffer. If //! \e ui32NumBytes is larger than the number of bytes currently in the buffer, //! the buffer is emptied. //! //! \return None. // //***************************************************************************** void RingBufAdvanceRead(tRingBufObject *psRingBuf, uint32_t ui32NumBytes) { uint32_t ui32Count; // // Check the arguments. // ASSERT(psRingBuf != NULL); // // Make sure that we are not being asked to remove more data than is // there to be removed. // ui32Count = RingBufUsed(psRingBuf); ui32Count = (ui32Count < ui32NumBytes) ? ui32Count : ui32NumBytes; // // Advance the buffer read index by the required number of bytes. // UpdateIndexAtomic(&psRingBuf->ui32ReadIndex, ui32Count, psRingBuf->ui32Size); }
//***************************************************************************** // //! Remove bytes from the ring buffer by advancing the read index. //! //! \param ptRingBuf points to the ring buffer from which bytes are to be //! removed. //! \param ulNumBytes is the number of bytes to be removed from the buffer. //! //! This function advances the ring buffer read index by a given number of //! bytes, removing that number of bytes of data from the buffer. If \e //! ulNumBytes is larger than the number of bytes currently in the buffer, the //! buffer is emptied. //! //! \return None. // //***************************************************************************** void RingBufAdvanceRead(tRingBufObject *ptRingBuf, unsigned long ulNumBytes) { unsigned long ulCount; // // Check the arguments. // ASSERT(ptRingBuf != NULL); // // Make sure that we are not being asked to remove more data than is // there to be removed. // ulCount = RingBufUsed(ptRingBuf); ulCount = (ulCount < ulNumBytes) ? ulCount : ulNumBytes; // // Advance the buffer read index by the required number of bytes. // UpdateIndexAtomic(&ptRingBuf->ulReadIndex, ulCount, ptRingBuf->ulSize); }
//***************************************************************************** // // UART interrupt handler. // // This is the interrupt handler for the UART interrupts from the UART // peripheral that has been associated with the remote network processor. // // \return None. //***************************************************************************** void RemoTIUARTIntHandler(void) { uint8_t ui8TxByte; // // Process all available interrupts while we are in this routine. // do { // // Check if a receive interrupt is pending. // if(UARTIntStatus(g_ui32UARTBase, 1) & UART_INT_RX) { // // A char was receieved, process it // Do this first so it does not get overwritten by future bytes. // RemoTIUARTRxHandler(); UARTIntClear(g_ui32UARTBase, UART_INT_RX); } // // Check if a transmit interrupt is pending. // if(UARTIntStatus(g_ui32UARTBase, 1) & UART_INT_TX) { // // A byte transmission completed so load another byte or turn off // tx interrupts. // if(RingBufUsed(&g_rbRemoTITxRingBuf)) { // // We still have more stuff to transfer so read the next byte // from the buffer and load it into the UART. Finally clear // the pending interrupt status. // ui8TxByte = RingBufReadOne(&g_rbRemoTITxRingBuf); UARTCharPutNonBlocking(g_ui32UARTBase, ui8TxByte); UARTIntClear(g_ui32UARTBase, UART_INT_TX); } else { // // Transmission is complete the internal buffer is empty. // Therefore, disable TX interrupts until next transmit is // started by the user. // UARTIntDisable(g_ui32UARTBase, UART_INT_TX); UARTIntClear(g_ui32UARTBase, UART_INT_TX); // // Clear the transmitter busy flag. // g_bTxBusy = false; // // callback to the TX Complete callback function // if(g_pfnTxCallback) { g_pfnTxCallback(0); } } } // // Continue to process the interrupts until there are no more pending. // }while(UARTIntStatus(g_ui32UARTBase, 1) & (UART_INT_RX | UART_INT_TX)); // // Finished. // }