Esempio n. 1
0
/**
 * @brief  Send one Byte through the SPI interface and return the received Byte
 * @param  uint8_t byte  The byte to send
 * @retval uint8_t The received byte value
 */
static uint8_t _LIS302DL_SendByte(SPI_TypeDef* spi, uint8_t byte) {
	volatile uint32_t _LIS302DL_Timeout = LIS302DL_MAX_TIMEOUT;
	
	// Loop while DR register in not empty; or we ran into a timeout
	_LIS302DL_Timeout = LIS302DL_MAX_TIMEOUT;
	while (SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) == RESET) {
		if ((_LIS302DL_Timeout--) == 0) {
			return LIS302DL_TIMEOUT_UserCallback();
		}
	}
	
	// Send a Byte through the SPI peripheral
	SPI_I2S_SendData(spi, byte);
	
	// Wait to receive a Byte; or we ran into a timeout
	_LIS302DL_Timeout = LIS302DL_MAX_TIMEOUT;
	while (SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) == RESET) {
		if ((_LIS302DL_Timeout--) == 0) {
			return LIS302DL_TIMEOUT_UserCallback();
		}
	}
	
	/* Return the Byte read from the SPI bus */
	return (uint8_t)SPI_I2S_ReceiveData(spi);
}
/**
 * @brief  Sends a Byte through the SPI interface and return the Byte received 
 *         from the SPI bus.
 * @param  Byte : Byte send.
 * @retval The received byte value
 */
static uint8_t LIS302DL_SendByte(uint8_t byte) {
    /* Loop while DR register in not emplty */
    LIS302DLTimeout = LIS302DL_FLAG_TIMEOUT;
    while (SPI_I2S_GetFlagStatus(LIS302DL_SPI, SPI_I2S_FLAG_TXE) == RESET) {
        if ((LIS302DLTimeout--) == 0) return LIS302DL_TIMEOUT_UserCallback();
    }

    /* Send a Byte through the SPI peripheral */
    SPI_I2S_SendData(LIS302DL_SPI, byte);

    /* Wait to receive a Byte */
    LIS302DLTimeout = LIS302DL_FLAG_TIMEOUT;
    while (SPI_I2S_GetFlagStatus(LIS302DL_SPI, SPI_I2S_FLAG_RXNE) == RESET) {
        if ((LIS302DLTimeout--) == 0) return LIS302DL_TIMEOUT_UserCallback();
    }

    /* Return the Byte read from the SPI bus */
    return (uint8_t) SPI_I2S_ReceiveData(LIS302DL_SPI);
}