/******************************************************************************* * Function Name: SPIM_ClearTxBuffer ******************************************************************************** * * Summary: * Clear the TX RAM buffer by setting the read and write pointers both to zero. * * Parameters: * None. * * 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 - resets to zero. * SPIM_txBufferRead - used for the account of the bytes which * have been read from the TX 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 data not yet transmitted from the RAM buffer will be lost when * overwritten. * * Reentrant: * No. * *******************************************************************************/ void SPIM_ClearTxBuffer(void) { uint8 enableInterrupts; enableInterrupts = CyEnterCriticalSection(); /* Clear TX FIFO */ SPIM_AUX_CONTROL_DP0_REG |= ((uint8) SPIM_TX_FIFO_CLR); SPIM_AUX_CONTROL_DP0_REG &= ((uint8) ~SPIM_TX_FIFO_CLR); #if(SPIM_USE_SECOND_DATAPATH) /* Clear TX FIFO for 2nd Datapath */ SPIM_AUX_CONTROL_DP1_REG |= ((uint8) SPIM_TX_FIFO_CLR); SPIM_AUX_CONTROL_DP1_REG &= ((uint8) ~SPIM_TX_FIFO_CLR); #endif /* (SPIM_USE_SECOND_DATAPATH) */ CyExitCriticalSection(enableInterrupts); #if(SPIM_TX_SOFTWARE_BUF_ENABLED) /* Disable TX interrupt to protect global veriables */ SPIM_DisableTxInt(); SPIM_txBufferFull = 0u; SPIM_txBufferRead = 0u; SPIM_txBufferWrite = 0u; /* Buffer is EMPTY: disable TX FIFO NOT FULL interrupt */ SPIM_TX_STATUS_MASK_REG &= ((uint8) ~SPIM_STS_TX_FIFO_NOT_FULL); SPIM_EnableTxInt(); #endif /* (SPIM_TX_SOFTWARE_BUF_ENABLED) */ }
/******************************************************************************* * Function Name: SPIM_GetTxBufferSize ******************************************************************************** * * Summary: * Returns the number of bytes/words of data currently held in the TX buffer. * If TX Software Buffer not used then function return 0 - if FIFO empty, 1 - if * FIFO not full, 4 - if FIFO full. In another case function return size of TX * Software Buffer. * * Parameters: * None. * * Return: * Integer count of the number of bytes/words in the TX buffer. * * Global variables: * SPIM_txBufferWrite - used for the account of the bytes which * have been written down in the TX software buffer. * SPIM_txBufferRead - used for the account of the bytes which * have been read from the TX software buffer. * * Side Effects: * Clear status register of the component. * *******************************************************************************/ uint8 SPIM_GetTxBufferSize(void) { uint8 size = 0u; #if(SPIM_TXBUFFERSIZE > 4u) /* Disable Interrupt to protect variables that could change on interrupt. */ SPIM_DisableTxInt(); if(SPIM_txBufferRead == SPIM_txBufferWrite) { size = 0u; } else if(SPIM_txBufferRead < SPIM_txBufferWrite) { size = (SPIM_txBufferWrite - SPIM_txBufferRead); } else { size = (SPIM_TXBUFFERSIZE - SPIM_txBufferRead) + SPIM_txBufferWrite; } /* Enable Interrupt. */ SPIM_EnableTxInt(); #else /* SPIM_TXBUFFERSIZE <= 4u */ size = SPIM_TX_STATUS_REG; /* Is the fifo is full. */ if((size & SPIM_STS_TX_FIFO_EMPTY) == SPIM_STS_TX_FIFO_EMPTY) { size = 0u; } else if((size & SPIM_STS_TX_FIFO_NOT_FULL) == SPIM_STS_TX_FIFO_NOT_FULL) { size = 1u; } else { /* We only know there is data in the fifo. */ size = 4u; } #endif /* SPIM_TXBUFFERSIZE > 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_GetTxBufferSize ******************************************************************************** * * Summary: * Returns the number of bytes/words of data currently held in the TX buffer. * If TX Software Buffer not used then function return 0 - if FIFO empty, 1 - if * FIFO not full, 4 - if FIFO full. In another case function return size of TX * Software Buffer. * * Parameters: * None. * * Return: * Integer count of the number of bytes/words in the TX buffer. * * Global variables: * SPIM_txBufferWrite - used for the account of the bytes which * have been written down in the TX software buffer. * SPIM_txBufferRead - used for the account of the bytes which * have been read from the TX software buffer. * * Side Effects: * Clear status register of the component. * *******************************************************************************/ uint8 SPIM_GetTxBufferSize(void) { uint8 size; #if(SPIM_TX_SOFTWARE_BUF_ENABLED) /* Disable TX interrupt to protect global veriables */ SPIM_DisableTxInt(); if(SPIM_txBufferRead == SPIM_txBufferWrite) { size = 0u; } else if(SPIM_txBufferRead < SPIM_txBufferWrite) { size = (SPIM_txBufferWrite - SPIM_txBufferRead); } else { size = (SPIM_TX_BUFFER_SIZE - SPIM_txBufferRead) + SPIM_txBufferWrite; } SPIM_EnableTxInt(); #else size = SPIM_TX_STATUS_REG; if(0u != (size & SPIM_STS_TX_FIFO_EMPTY)) { size = 0u; } else if(0u != (size & SPIM_STS_TX_FIFO_NOT_FULL)) { size = 1u; } else { size = SPIM_FIFO_SIZE; } #endif /* (SPIM_TX_SOFTWARE_BUF_ENABLED) */ return(size); }
/******************************************************************************* * Function Name: SPIM_ClearTxBuffer ******************************************************************************** * * Summary: * Clear the TX RAM buffer by setting the read and write pointers both to zero. * * Parameters: * None. * * 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 - resets to zero. * SPIM_txBufferRead - used for the account of the bytes which * have been read from the TX 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 data not yet transmitted from the RAM buffer will be lost when * overwritten. * * Reentrant: * No. * *******************************************************************************/ void SPIM_ClearTxBuffer(void) { uint8 enableInterrupts = 0u; /* Clear Hardware TX FIFO */ enableInterrupts = CyEnterCriticalSection(); #if(SPIM_DataWidth <= 8u) /* Clear TX FIFO */ SPIM_AUX_CONTROL_DP0_REG |= SPIM_FIFO_CLR; SPIM_AUX_CONTROL_DP0_REG &= ~SPIM_FIFO_CLR; #else /* Clear TX FIFO */ SPIM_AUX_CONTROL_DP0_REG |= SPIM_FIFO_CLR; SPIM_AUX_CONTROL_DP0_REG &= ~SPIM_FIFO_CLR; SPIM_AUX_CONTROL_DP1_REG |= SPIM_FIFO_CLR; SPIM_AUX_CONTROL_DP1_REG &= ~SPIM_FIFO_CLR; #endif /* SPIM_DataWidth > 8u */ CyExitCriticalSection(enableInterrupts); #if(SPIM_TXBUFFERSIZE > 4u) /* Disable Interrupt to protect variables that could change on interrupt. */ SPIM_DisableTxInt(); SPIM_txBufferRead = 0u; SPIM_txBufferWrite = 0u; /* If Buffer is empty then disable TX FIFO status interrupt */ SPIM_TX_STATUS_MASK_REG &= ~SPIM_STS_TX_FIFO_NOT_FULL; /* Enable Interrupt. */ SPIM_EnableTxInt(); #endif /* SPIM_TXBUFFERSIZE > 4u */ }
/******************************************************************************* * 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) */ }
/******************************************************************************* * 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(); }