/******************************************************************************* * Function Name: SPIM_ReadStatus ******************************************************************************** * * Summary: * Read the status register for the component. * * Parameters: * None. * * Return: * Contents of the status register. * * Global variables: * SPIM_swStatus - used to store in software status register, * modified every function call - resets to zero. * * Theory: * Allows the user and the API to read the status register for error detection * and flow control. * * Side Effects: * Clear status register of the component. * * Reentrant: * No. * *******************************************************************************/ uint8 SPIM_ReadStatus(void) { uint8 tmpStatus; #if ((SPIM_TXBUFFERSIZE > 4u) || (SPIM_RXBUFFERSIZE > 4u)) SPIM_DisableInt(); tmpStatus = (SPIM_GET_STATUS_TX(SPIM_swStatusTx) & ~(1u << SPIM_STS_SPI_IDLE_SHIFT)) | SPIM_GET_STATUS_RX(SPIM_swStatusRx); SPIM_swStatusTx = 0u; SPIM_swStatusRx = 0u; /* Enable Interrupts */ SPIM_EnableInt(); #else /* (SPIM_TXBUFFERSIZE < 4u) && (SPIM_RXBUFFERSIZE < 4u) */ tmpStatus = (SPIM_TX_STATUS_REG & ~(1u << SPIM_STS_SPI_IDLE_SHIFT)) | SPIM_RX_STATUS_REG; #endif /* (SPIM_TXBUFFERSIZE > 4u) || (SPIM_RXBUFFERSIZE > 4u) */ return(tmpStatus); }
/******************************************************************************* * Function Name: SPIM_ReadStatus ******************************************************************************** * * Summary: * Read the status register for the component. * * Parameters: * None. * * Return: * Contents of the status register. * * Global variables: * SPIM_swStatus - used to store in software status register, * modified every function call - resets to zero. * * Theory: * Allows the user and the API to read the status register for error detection * and flow control. * * Side Effects: * Clear status register of the component. * * Reentrant: * No. * *******************************************************************************/ uint8 SPIM_ReadStatus(void) { uint8 tmpStatus; #if(SPIM_TX_SOFTWARE_BUF_ENABLED || SPIM_RX_SOFTWARE_BUF_ENABLED) SPIM_DisableInt(); tmpStatus = SPIM_GET_STATUS_RX(SPIM_swStatusRx); tmpStatus |= SPIM_GET_STATUS_TX(SPIM_swStatusTx); tmpStatus &= ((uint8) ~SPIM_STS_SPI_IDLE); SPIM_swStatusTx = 0u; SPIM_swStatusRx = 0u; SPIM_EnableInt(); #else tmpStatus = SPIM_RX_STATUS_REG; tmpStatus |= SPIM_TX_STATUS_REG; tmpStatus &= ((uint8) ~SPIM_STS_SPI_IDLE); #endif /* (SPIM_TX_SOFTWARE_BUF_ENABLED || SPIM_RX_SOFTWARE_BUF_ENABLED) */ return(tmpStatus); }
/******************************************************************************* * Function Name: SPIM_ReadTxStatus ******************************************************************************** * * Summary: * Read the Tx status register for the component. * * Parameters: * None. * * Return: * Contents of the Tx status register. * * Global variables: * SPIM_swStatusTx - used to store in software status register, * modified every function call - resets to zero. * * Theory: * Allows the user and the API to read the Tx status register for error * detection and flow control. * * Side Effects: * Clear Tx status register of the component. * * Reentrant: * No. * *******************************************************************************/ uint8 SPIM_ReadTxStatus(void) { uint8 tmpStatus; #if(SPIM_TX_SOFTWARE_BUF_ENABLED) /* Disable TX interrupt to protect global veriables */ SPIM_DisableTxInt(); tmpStatus = SPIM_GET_STATUS_TX(SPIM_swStatusTx); SPIM_swStatusTx = 0u; SPIM_EnableTxInt(); #else tmpStatus = SPIM_TX_STATUS_REG; #endif /* (SPIM_TX_SOFTWARE_BUF_ENABLED) */ return(tmpStatus); }
/******************************************************************************* * Function Name: SPIM_ReadTxStatus ******************************************************************************** * * Summary: * Read the Tx status register for the component. * * Parameters: * None. * * Return: * Contents of the Tx status register. * * Global variables: * SPIM_swStatusTx - used to store in software status register, * modified every function call - resets to zero. * * Theory: * Allows the user and the API to read the Tx status register for error * detection and flow control. * * Side Effects: * Clear Tx status register of the component. * * Reentrant: * No. * *******************************************************************************/ uint8 SPIM_ReadTxStatus(void) { uint8 tmpStatus = 0u; #if (SPIM_TXBUFFERSIZE > 4u) SPIM_DisableTxInt(); tmpStatus = SPIM_GET_STATUS_TX(SPIM_swStatusTx); SPIM_swStatusTx = 0u; /* Enable Interrupts */ SPIM_EnableTxInt(); #else /* (SPIM_TXBUFFERSIZE < 4u) */ tmpStatus = SPIM_TX_STATUS_REG; #endif /* (SPIM_TXBUFFERSIZE > 4u) */ return(tmpStatus); }
/******************************************************************************* * Function Name: SPIM_WriteTxData ******************************************************************************** * * Summary: * Write a byte of data to be sent across the SPI. * * Parameters: * txDataByte: The data value to send across the SPI. * * Return: * None. * * Global variables: * SPIM_txBufferWrite - used for the account of the bytes which * have been written down in the TX software buffer, modified every function * call if TX Software Buffer is used. * SPIM_txBufferRead - used for the account of the bytes which * have been read from the TX software buffer. * SPIM_TXBUFFER[SPIM_TXBUFFERSIZE] - used to store * data to sending, modified every function call if TX Software Buffer is used. * * Theory: * Allows the user to transmit any byte of data in a single transfer. * * Side Effects: * If this function is called again before the previous byte is finished then * the next byte will be appended to the transfer with no time between * the byte transfers. Clear Tx status register of the component. * * Reentrant: * No. * *******************************************************************************/ void SPIM_WriteTxData(uint8 txData) { #if(SPIM_TXBUFFERSIZE > 4u) int16 tmpTxBufferRead = 0u; /* Block if buffer is full, so we don't overwrite. */ do { tmpTxBufferRead = SPIM_txBufferRead - 1u; if (tmpTxBufferRead < 0u) { tmpTxBufferRead = SPIM_TXBUFFERSIZE - 1u; } } while(tmpTxBufferRead == SPIM_txBufferWrite); /* Disable Interrupt to protect variables that could change on interrupt. */ SPIM_DisableTxInt(); SPIM_swStatusTx = SPIM_GET_STATUS_TX(SPIM_swStatusTx); if((SPIM_txBufferRead == SPIM_txBufferWrite) && ((SPIM_swStatusTx & SPIM_STS_TX_FIFO_NOT_FULL) != 0u)) { /* Add directly to the FIFO. */ CY_SET_REG8(SPIM_TXDATA_PTR, txData); } else { /* Add to the software buffer. */ SPIM_txBufferWrite++; if(SPIM_txBufferWrite >= SPIM_TXBUFFERSIZE) { SPIM_txBufferWrite = 0u; } if(SPIM_txBufferWrite == SPIM_txBufferRead) { SPIM_txBufferRead++; if(SPIM_txBufferRead >= SPIM_RXBUFFERSIZE) { SPIM_txBufferRead = 0u; } SPIM_txBufferFull = 1u; } SPIM_TXBUFFER[SPIM_txBufferWrite] = txData; SPIM_TX_STATUS_MASK_REG |= SPIM_STS_TX_FIFO_NOT_FULL; } /* Enable Interrupt. */ SPIM_EnableTxInt(); #else /* SPIM_TXBUFFERSIZE <= 4u */ /* Block while FIFO is full */ while((SPIM_TX_STATUS_REG & SPIM_STS_TX_FIFO_NOT_FULL) == 0u); /* Then write the byte */ CY_SET_REG8(SPIM_TXDATA_PTR, txData); #endif /* SPIM_TXBUFFERSIZE > 4u */ }
/******************************************************************************* * Function Name: SPIM_WriteTxData ******************************************************************************** * * Summary: * Write a byte of data to be sent across the SPI. * * Parameters: * txDataByte: The data value to send across the SPI. * * Return: * None. * * Global variables: * SPIM_txBufferWrite - used for the account of the bytes which * have been written down in the TX software buffer, modified every function * call if TX Software Buffer is used. * SPIM_txBufferRead - used for the account of the bytes which * have been read from the TX software buffer. * SPIM_txBuffer[SPIM_TX_BUFFER_SIZE] - used to store * data to sending, modified every function call if TX Software Buffer is used. * * Theory: * Allows the user to transmit any byte of data in a single transfer. * * Side Effects: * If this function is called again before the previous byte is finished then * the next byte will be appended to the transfer with no time between * the byte transfers. Clear Tx status register of the component. * * Reentrant: * No. * *******************************************************************************/ void SPIM_WriteTxData(uint8 txData) { #if(SPIM_TX_SOFTWARE_BUF_ENABLED) uint8 tempStatus; uint8 tmpTxBufferRead; /* Block if TX buffer is FULL: don't overwrite */ do { tmpTxBufferRead = SPIM_txBufferRead; if(0u == tmpTxBufferRead) { tmpTxBufferRead = (SPIM_TX_BUFFER_SIZE - 1u); } else { tmpTxBufferRead--; } }while(tmpTxBufferRead == SPIM_txBufferWrite); /* Disable TX interrupt to protect global veriables */ SPIM_DisableTxInt(); tempStatus = SPIM_GET_STATUS_TX(SPIM_swStatusTx); SPIM_swStatusTx = tempStatus; if((SPIM_txBufferRead == SPIM_txBufferWrite) && (0u != (SPIM_swStatusTx & SPIM_STS_TX_FIFO_NOT_FULL))) { /* Add directly to the TX FIFO */ CY_SET_REG8(SPIM_TXDATA_PTR, txData); } else { /* Add to the TX software buffer */ SPIM_txBufferWrite++; if(SPIM_txBufferWrite >= SPIM_TX_BUFFER_SIZE) { SPIM_txBufferWrite = 0u; } if(SPIM_txBufferWrite == SPIM_txBufferRead) { SPIM_txBufferRead++; if(SPIM_txBufferRead >= SPIM_TX_BUFFER_SIZE) { SPIM_txBufferRead = 0u; } SPIM_txBufferFull = 1u; } SPIM_txBuffer[SPIM_txBufferWrite] = txData; SPIM_TX_STATUS_MASK_REG |= SPIM_STS_TX_FIFO_NOT_FULL; } SPIM_EnableTxInt(); #else while(0u == (SPIM_TX_STATUS_REG & SPIM_STS_TX_FIFO_NOT_FULL)) { ; /* Wait for room in FIFO */ } /* Put byte in TX FIFO */ CY_SET_REG8(SPIM_TXDATA_PTR, txData); #endif /* (SPIM_TX_SOFTWARE_BUF_ENABLED) */ }