/******************************************************************************* * Function Name: UART_1_UartCyBtldrCommRead ****************************************************************************//** * * Allows the caller to read data from the bootloader host (the host writes the * data). The function handles polling to allow a block of data to be completely * received from the host device. * * \param pData: Pointer to storage for the block of data to be read from the * bootloader host * \param size: Number of bytes to be read. * \param count: Pointer to the variable to write the number of bytes actually * read. * \param timeOut Number of units in 10 ms to wait before returning * because of a timeout. * * \return * Returns CYRET_SUCCESS if no problem was encountered or returns the value * that best describes the problem. For more information refer to the * "Return Codes" section of the System Reference Guide. * *******************************************************************************/ cystatus UART_1_UartCyBtldrCommRead(uint8 pData[], uint16 size, uint16 * count, uint8 timeOut) { cystatus status; uint32 byteCount; uint32 timeoutMs; uint32 i; status = CYRET_BAD_PARAM; if ((NULL != pData) && (size > 0u)) { status = CYRET_TIMEOUT; timeoutMs = ((uint32) 10u * timeOut); /* Convert from 10mS check to 1mS checks */ /* Wait with timeout 1mS for packet end */ byteCount = 0u; do { /* Check packet start */ if (0u != UART_1_SpiUartGetRxBufferSize()) { /* Wait for end of packet */ do { byteCount = UART_1_SpiUartGetRxBufferSize(); CyDelayUs(UART_1_UART_BYTE_TO_BYTE); } while (byteCount != UART_1_SpiUartGetRxBufferSize()); byteCount = UART_1_BYTES_TO_COPY(byteCount, size); *count = (uint16) byteCount; status = CYRET_SUCCESS; break; } CyDelay(UART_1_WAIT_1_MS); --timeoutMs; } while (0u != timeoutMs); /* Get data from RX buffer into bootloader buffer */ for (i = 0u; i < byteCount; ++i) { pData[i] = (uint8) UART_1_SpiUartReadRxData(); } } return (status); }
/******************************************************************************* * Function Name: UART_1_UartGetByte ******************************************************************************** * * Summary: * Retrieves the next data element from the receive buffer, returns the * received byte and error condition. * - The RX software buffer is disabled: returns the data element retrieved * from the RX FIFO. Undefined data will be returned if the RX FIFO is * empty. * - The RX software buffer is enabled: returns data element from the * software receive buffer. * * Parameters: * None * * Return: * Bits 7-0 contain the next data element from the receive buffer and * other bits contain the error condition. * * Side Effects: * The errors bits may not correspond with reading characters due to RX FIFO * and software buffer usage. * RX software buffer is disabled: The internal software buffer overflow * is not returned as status by this function. * Check SCB_rxBufferOverflow to capture that error condition. * *******************************************************************************/ uint32 UART_1_UartGetByte(void) { uint32 rxData; uint32 tmpStatus; #if (UART_1_CHECK_RX_SW_BUFFER) { UART_1_DisableInt(); } #endif if (0u != UART_1_SpiUartGetRxBufferSize()) { /* Enables interrupt to receive more bytes: at least one byte is in * buffer. */ #if (UART_1_CHECK_RX_SW_BUFFER) { UART_1_EnableInt(); } #endif /* Get received byte */ rxData = UART_1_SpiUartReadRxData(); } else { /* Reads a byte directly from RX FIFO: underflow is raised in the case * of empty. Otherwise the first received byte will be read. */ rxData = UART_1_RX_FIFO_RD_REG; /* Enables interrupt to receive more bytes. * The RX_NOT_EMPTY interrupt is cleared by the interrupt routine * in case the byte was received and read by code above. */ #if (UART_1_CHECK_RX_SW_BUFFER) { UART_1_EnableInt(); } #endif } /* Get and clear RX error mask */ tmpStatus = (UART_1_GetRxInterruptSource() & UART_1_INTR_RX_ERR); UART_1_ClearRxInterruptSource(UART_1_INTR_RX_ERR); /* Puts together data and error status: * MP mode and accept address: 9th bit is set to notify mark. */ rxData |= ((uint32) (tmpStatus << 8u)); return (rxData); }
/******************************************************************************* * Function Name: UART_1_UartGetByte ******************************************************************************** * * Summary: * Retrieves the next data element from the receive buffer, returns the received byte * and error condition. * - The RX software buffer is disabled: returns the data element retrieved from the RX FIFO. * Undefined data will be returned if the RX FIFO is empty. * - The RX software buffer is enabled: returns data element from the software receive * buffer. * * Parameters: * None * * Return: * Bits 15-8 contains status and bits 7-0 contains the next data element from * receive buffer. If the bits 15-8 are non-zero, an error has occurred. * *******************************************************************************/ uint32 UART_1_UartGetByte(void) { uint32 rxData; uint32 tmpStatus; uint32 intSourceMask; intSourceMask = UART_1_SpiUartDisableIntRx(); if(0u != UART_1_SpiUartGetRxBufferSize()) { /* * Enable interrupt to receive more bytes: at least one byte is in * buffer. */ UART_1_SpiUartEnableIntRx(intSourceMask); /* Get received byte */ rxData = UART_1_SpiUartReadRxData(); } else { /* * Read byte directly from RX FIFO: the underflow is raised in case * of empty. In other case the first received byte will be read. */ rxData = UART_1_RX_FIFO_RD_REG; /* * Enable interrupt to receive more bytes. * The RX_NOT_EMPTY interrupt is cleared by the interrupt routine in case * the byte was received and read above. */ UART_1_SpiUartEnableIntRx(intSourceMask); } /* Get and clear RX error mask */ tmpStatus = (UART_1_GetRxInterruptSource() & UART_1_INTR_RX_ERR); UART_1_ClearRxInterruptSource(UART_1_INTR_RX_ERR); /* * Put together data and error status: * MP mode and accept address: the 9th bit is set to notify mark. */ rxData |= ((uint32) (tmpStatus << 8u)); return(rxData); }
/******************************************************************************* * Function Name: UART_1_UartGetChar ******************************************************************************** * * Summary: * Retrieves the next data element from the receive buffer. * This function is designed for ASCII characters and returns a char * where 1 to 255 are valid characters and 0 indicates an error occurred or * no data present. * - The RX software buffer is disabled: returns the data element * retrieved from the RX FIFO. * Undefined data will be returned if the RX FIFO is empty. * - The RX software buffer is enabled: returns the data element from * the software receive buffer. * * Parameters: * None * * Return: * The next data element from the receive buffer. * ASCII character values from 1 to 255 are valid. * A returned zero signifies an error condition or no data available. * * Side Effects: * The errors bits may not correspond with reading characters due to RX FIFO * and software buffer usage. * RX software buffer is enabled: The internal software buffer overflow * does not treat as an error condition. * Check SCB_rxBufferOverflow to capture that error condition. * *******************************************************************************/ uint32 UART_1_UartGetChar(void) { uint32 rxData = 0u; /* Reads data only if there is data to read */ if (0u != UART_1_SpiUartGetRxBufferSize()) { rxData = UART_1_SpiUartReadRxData(); } if (UART_1_CHECK_INTR_RX(UART_1_INTR_RX_ERR)) { rxData = 0u; /* Error occurred: returns zero */ UART_1_ClearRxInterruptSource(UART_1_INTR_RX_ERR); } return (rxData); }