/******************************************************************************* * Function Name: SPIM_GetRxBufferSize ******************************************************************************** * * Summary: * Returns the number of bytes/words of data currently held in the RX buffer. * If RX Software Buffer not used then function return 0 if FIFO empty or 1 if * FIFO not empty. In another case function return size of RX Software Buffer. * * Parameters: * None. * * Return: * Integer count of the number of bytes/words in the RX buffer. * * Global variables: * SPIM_rxBufferWrite - used for the account of the bytes which * have been written down in the RX software buffer. * SPIM_rxBufferRead - used for the account of the bytes which * have been read from the RX software buffer. * * Side Effects: * Clear status register of the component. * *******************************************************************************/ uint8 SPIM_GetRxBufferSize(void) { uint8 size; #if(SPIM_RX_SOFTWARE_BUF_ENABLED) /* Disable RX interrupt to protect global veriables */ SPIM_DisableRxInt(); if(SPIM_rxBufferRead == SPIM_rxBufferWrite) { size = 0u; } else if(SPIM_rxBufferRead < SPIM_rxBufferWrite) { size = (SPIM_rxBufferWrite - SPIM_rxBufferRead); } else { size = (SPIM_RX_BUFFER_SIZE - SPIM_rxBufferRead) + SPIM_rxBufferWrite; } SPIM_EnableRxInt(); #else /* We can only know if there is data in the RX FIFO */ size = (0u != (SPIM_RX_STATUS_REG & SPIM_STS_RX_FIFO_NOT_EMPTY)) ? 1u : 0u; #endif /* (SPIM_TX_SOFTWARE_BUF_ENABLED) */ return(size); }
/******************************************************************************* * Function Name: SPIM_GetRxBufferSize ******************************************************************************** * * Summary: * Returns the number of bytes/words of data currently held in the RX buffer. * If RX Software Buffer not used then function return 0 if FIFO empty or 1 if * FIFO not empty. In another case function return size of RX Software Buffer. * * Parameters: * None. * * Return: * Integer count of the number of bytes/words in the RX buffer. * * Global variables: * SPIM_rxBufferWrite - used for the account of the bytes which * have been written down in the RX software buffer. * SPIM_rxBufferRead - used for the account of the bytes which * have been read from the RX software buffer. * * Side Effects: * Clear status register of the component. * *******************************************************************************/ uint8 SPIM_GetRxBufferSize(void) { uint8 size = 0u; #if(SPIM_RXBUFFERSIZE > 4u) /* Disable Interrupt to protect variables that could change on interrupt. */ SPIM_DisableRxInt(); if(SPIM_rxBufferRead == SPIM_rxBufferWrite) { size = 0u; /* No data in RX buffer */ } else if(SPIM_rxBufferRead < SPIM_rxBufferWrite) { size = (SPIM_rxBufferWrite - SPIM_rxBufferRead); } else { size = (SPIM_RXBUFFERSIZE - SPIM_rxBufferRead) + SPIM_rxBufferWrite; } /* Enable interrupt. */ SPIM_EnableRxInt(); #else /* SPIM_RXBUFFERSIZE <= 4u */ /* We can only know if there is data in the fifo. */ size = ((SPIM_RX_STATUS_REG & SPIM_STS_RX_FIFO_NOT_EMPTY) == SPIM_STS_RX_FIFO_NOT_EMPTY) ? 1u : 0u; #endif /* SPIM_RXBUFFERSIZE < 4u */ return (size); }
/******************************************************************************* * Function Name: SPIM_Enable ******************************************************************************** * * Summary: * Enable SPIM component. * * Parameters: * None. * * Return: * None. * *******************************************************************************/ void SPIM_Enable(void) { uint8 enableInterrupts; enableInterrupts = CyEnterCriticalSection(); SPIM_COUNTER_CONTROL_REG |= SPIM_CNTR_ENABLE; SPIM_TX_STATUS_ACTL_REG |= SPIM_INT_ENABLE; SPIM_RX_STATUS_ACTL_REG |= SPIM_INT_ENABLE; CyExitCriticalSection(enableInterrupts); #if(0u != SPIM_INTERNAL_CLOCK) SPIM_IntClock_Enable(); #endif /* (0u != SPIM_INTERNAL_CLOCK) */ SPIM_EnableTxInt(); SPIM_EnableRxInt(); }
/******************************************************************************* * Function Name: SPIM_ClearRxBuffer ******************************************************************************** * * Summary: * Clear the RX RAM buffer by setting the read and write pointers both to zero. * * Parameters: * None. * * Return: * None. * * Global variables: * SPIM_rxBufferWrite - used for the account of the bytes which * have been written down in the RX software buffer, modified every function * call - resets to zero. * SPIM_rxBufferRead - used for the account of the bytes which * have been read from the RX software buffer, modified every function call - * resets to zero. * * Theory: * Setting the pointers to zero makes the system believe there is no data to * read and writing will resume at address 0 overwriting any data that may have * remained in the RAM. * * Side Effects: * Any received data not read from the RAM buffer will be lost when overwritten. * * Reentrant: * No. * *******************************************************************************/ void SPIM_ClearRxBuffer(void) { /* Clear Hardware RX FIFO */ while(0u !=(SPIM_RX_STATUS_REG & SPIM_STS_RX_FIFO_NOT_EMPTY)) { (void) CY_GET_REG8(SPIM_RXDATA_PTR); } #if(SPIM_RX_SOFTWARE_BUF_ENABLED) /* Disable RX interrupt to protect global veriables */ SPIM_DisableRxInt(); SPIM_rxBufferFull = 0u; SPIM_rxBufferRead = 0u; SPIM_rxBufferWrite = 0u; SPIM_EnableRxInt(); #endif /* (SPIM_RX_SOFTWARE_BUF_ENABLED) */ }
/******************************************************************************* * Function Name: SPIM_ClearRxBuffer ******************************************************************************** * * Summary: * Clear the RX RAM buffer by setting the read and write pointers both to zero. * * Parameters: * None. * * Return: * None. * * Global variables: * SPIM_rxBufferWrite - used for the account of the bytes which * have been written down in the RX software buffer, modified every function * call - resets to zero. * SPIM_rxBufferRead - used for the account of the bytes which * have been read from the RX software buffer, modified every function call - * resets to zero. * * Theory: * Setting the pointers to zero makes the system believe there is no data to * read and writing will resume at address 0 overwriting any data that may have * remained in the RAM. * * Side Effects: * Any received data not read from the RAM buffer will be lost when overwritten. * * Reentrant: * No. * *******************************************************************************/ void SPIM_ClearRxBuffer(void) { /* Clear Hardware RX FIFO */ while((!(SPIM_RX_STATUS_REG & SPIM_STS_RX_FIFO_NOT_EMPTY)) == 0u) { CY_GET_REG8(SPIM_RXDATA_PTR); } #if(SPIM_RXBUFFERSIZE > 4u) /* Disable interrupt to protect variables that could change on interrupt. */ SPIM_DisableRxInt(); SPIM_rxBufferRead = 0u; SPIM_rxBufferWrite = 0u; /* Enable Rx interrupt. */ SPIM_EnableRxInt(); #endif /* SPIM_RXBUFFERSIZE > 4u */ }
/******************************************************************************* * Function Name: SPIM_ReadRxStatus ******************************************************************************** * * Summary: * Read the Rx status register for the component. * * Parameters: * None. * * Return: * Contents of the Rx status register. * * Global variables: * SPIM_swStatusRx - used to store in software Rx status register, * modified every function call - resets to zero. * * Theory: * Allows the user and the API to read the Rx status register for error * detection and flow control. * * Side Effects: * Clear Rx status register of the component. * * Reentrant: * No. * *******************************************************************************/ uint8 SPIM_ReadRxStatus(void) { uint8 tmpStatus; #if(SPIM_RX_SOFTWARE_BUF_ENABLED) /* Disable RX interrupt to protect global veriables */ SPIM_DisableRxInt(); tmpStatus = SPIM_GET_STATUS_RX(SPIM_swStatusRx); SPIM_swStatusRx = 0u; SPIM_EnableRxInt(); #else tmpStatus = SPIM_RX_STATUS_REG; #endif /* (SPIM_RX_SOFTWARE_BUF_ENABLED) */ return(tmpStatus); }
/******************************************************************************* * Function Name: SPIM_ReadRxData ******************************************************************************** * * Summary: * Read the next byte of data received across the SPI. * * Parameters: * None. * * Return: * The next byte of data read from the FIFO. * * Global variables: * SPIM_rxBufferWrite - used for the account of the bytes which * have been written down in the RX software buffer. * SPIM_rxBufferRead - used for the account of the bytes which * have been read from the RX software buffer, modified every function * call if RX Software Buffer is used. * SPIM_RXBUFFER[SPIM_RXBUFFERSIZE] - used to store * received data. * * Theory: * Allows the user to read a byte of data received. * * Side Effects: * Will return invalid data if the FIFO is empty. The user should Call * GetRxBufferSize() and if it returns a non-zero value then it is safe to call * ReadByte() function. * * Reentrant: * No. * *******************************************************************************/ uint8 SPIM_ReadRxData(void) { uint8 rxData = 0u; #if(SPIM_RXBUFFERSIZE > 4u) /* Disable Interrupt to protect variables that could change on interrupt. */ SPIM_DisableRxInt(); if(SPIM_rxBufferRead != SPIM_rxBufferWrite) { if(SPIM_rxBufferFull == 0u) { SPIM_rxBufferRead++; if(SPIM_rxBufferRead >= SPIM_RXBUFFERSIZE) { SPIM_rxBufferRead = 0u; } } else { SPIM_rxBufferFull = 0u; } } rxData = SPIM_RXBUFFER[SPIM_rxBufferRead]; /* Enable Interrupt. */ SPIM_EnableRxInt(); #else /* SPIM_RXBUFFERSIZE <= 4u */ rxData = CY_GET_REG8(SPIM_RXDATA_PTR); #endif /* SPIM_RXBUFFERSIZE > 4u */ return (rxData); }
/******************************************************************************* * Function Name: SPIM_ReadRxStatus ******************************************************************************** * * Summary: * Read the Rx status register for the component. * * Parameters: * None. * * Return: * Contents of the Rx status register. * * Global variables: * SPIM_swStatusRx - used to store in software Rx status register, * modified every function call - resets to zero. * * Theory: * Allows the user and the API to read the Rx status register for error * detection and flow control. * * Side Effects: * Clear Rx status register of the component. * * Reentrant: * No. * *******************************************************************************/ uint8 SPIM_ReadRxStatus(void) { uint8 tmpStatus = 0u; #if (SPIM_RXBUFFERSIZE > 4u) SPIM_DisableRxInt(); tmpStatus = SPIM_GET_STATUS_RX(SPIM_swStatusRx); SPIM_swStatusRx = 0u; /* Enable Interrupts */ SPIM_EnableRxInt(); #else /* (SPIM_RXBUFFERSIZE < 4u) */ tmpStatus = SPIM_RX_STATUS_REG; #endif /* (SPIM_RXBUFFERSIZE > 4u) */ return(tmpStatus); }
/******************************************************************************* * Function Name: SPIM_ReadRxData ******************************************************************************** * * Summary: * Read the next byte of data received across the SPI. * * Parameters: * None. * * Return: * The next byte of data read from the FIFO. * * Global variables: * SPIM_rxBufferWrite - used for the account of the bytes which * have been written down in the RX software buffer. * SPIM_rxBufferRead - used for the account of the bytes which * have been read from the RX software buffer, modified every function * call if RX Software Buffer is used. * SPIM_rxBuffer[SPIM_RX_BUFFER_SIZE] - used to store * received data. * * Theory: * Allows the user to read a byte of data received. * * Side Effects: * Will return invalid data if the FIFO is empty. The user should Call * GetRxBufferSize() and if it returns a non-zero value then it is safe to call * ReadByte() function. * * Reentrant: * No. * *******************************************************************************/ uint8 SPIM_ReadRxData(void) { uint8 rxData; #if(SPIM_RX_SOFTWARE_BUF_ENABLED) /* Disable RX interrupt to protect global veriables */ SPIM_DisableRxInt(); if(SPIM_rxBufferRead != SPIM_rxBufferWrite) { if(0u == SPIM_rxBufferFull) { SPIM_rxBufferRead++; if(SPIM_rxBufferRead >= SPIM_RX_BUFFER_SIZE) { SPIM_rxBufferRead = 0u; } } else { SPIM_rxBufferFull = 0u; } } rxData = SPIM_rxBuffer[SPIM_rxBufferRead]; SPIM_EnableRxInt(); #else rxData = CY_GET_REG8(SPIM_RXDATA_PTR); #endif /* (SPIM_RX_SOFTWARE_BUF_ENABLED) */ return(rxData); }
/******************************************************************************* * Function Name: SPIM_EnableInt ******************************************************************************** * * Summary: * Enable internal interrupt generation. * * Parameters: * None. * * Return: * None. * * Theory: * Enable the internal interrupt output -or- the interrupt component itself. * *******************************************************************************/ void SPIM_EnableInt(void) { SPIM_EnableRxInt(); SPIM_EnableTxInt(); }