bool spiTransfer(SPI_TypeDef *instance, uint8_t *out, const uint8_t *in, int len) { uint16_t spiTimeout = 1000; uint8_t b; instance->DR; while (len--) { b = in ? *(in++) : 0xFF; while (SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_TXE) == RESET) { if ((spiTimeout--) == 0) return spiTimeoutUserCallback(instance); } #ifdef STM32F303xC SPI_SendData8(instance, b); #else SPI_I2S_SendData(instance, b); #endif spiTimeout = 1000; while (SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_RXNE) == RESET) { if ((spiTimeout--) == 0) return spiTimeoutUserCallback(instance); } #ifdef STM32F303xC b = SPI_ReceiveData8(instance); #else b = SPI_I2S_ReceiveData(instance); #endif if (out) *(out++) = b; } return true; }
uint8_t spiTransfer(uint8_t *out, uint8_t *in, int len) { uint16_t spiTimeout; uint8_t b; SPI_BUSE->DR; while (len--) { b = in ? *(in++) : 0xFF; spiTimeout = 1000; while (SPI_I2S_GetFlagStatus(SPI_BUSE, SPI_I2S_FLAG_TXE) == RESET) { if ((spiTimeout--) == 0) return spiTimeoutUserCallback(); } SPI_I2S_SendData(SPI_BUSE, b); spiTimeout = 1000; while (SPI_I2S_GetFlagStatus(SPI_BUSE, SPI_I2S_FLAG_RXNE) == RESET) { if ((spiTimeout--) == 0) return spiTimeoutUserCallback(); } b = SPI_I2S_ReceiveData(SPI_BUSE); if (out) *(out++) = b; } return true; }
uint8_t spiTransfer(SPI_TypeDef *SPIx, uint8_t data) { uint16_t spiTimeout; spiTimeout = 0x1000; while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET) if ((spiTimeout--) == 0) return spiTimeoutUserCallback(SPIx); SPI_SendData8(SPIx, data); spiTimeout = 0x1000; while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET) if ((spiTimeout--) == 0) return spiTimeoutUserCallback(SPIx); return((uint8_t)SPI_ReceiveData8(SPIx)); }
bool spiTransfer(SPI_TypeDef *instance, uint8_t *out, const uint8_t *in, int len) { SPIDevice device = spiDeviceByInstance(instance); HAL_StatusTypeDef status; #define SPI_DEFAULT_TIMEOUT 10 if(!out) // Tx only { status = HAL_SPI_Transmit(&spiHardwareMap[device].hspi, (uint8_t *)in, len, SPI_DEFAULT_TIMEOUT); } else if(!in) // Rx only { status = HAL_SPI_Receive(&spiHardwareMap[device].hspi, out, len, SPI_DEFAULT_TIMEOUT); } else // Tx and Rx { status = HAL_SPI_TransmitReceive(&spiHardwareMap[device].hspi, (uint8_t *)in, out, len, SPI_DEFAULT_TIMEOUT); } if( status != HAL_OK) spiTimeoutUserCallback(instance); return true; }
// return uint8_t value or -1 when failure uint8_t spiTransferByte(SPI_TypeDef *instance, uint8_t data) { uint16_t spiTimeout = 1000; while (SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_TXE) == RESET) if ((spiTimeout--) == 0) return spiTimeoutUserCallback(instance); #ifdef STM32F303xC SPI_SendData8(instance, data); #else SPI_I2S_SendData(instance, data); #endif spiTimeout = 1000; while (SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_RXNE) == RESET) if ((spiTimeout--) == 0) return spiTimeoutUserCallback(instance); #ifdef STM32F303xC return ((uint8_t)SPI_ReceiveData8(instance)); #else return ((uint8_t)SPI_I2S_ReceiveData(instance)); #endif }