// SPI basic Read/Write function u8 SPI_RW_Byte(SPI_TypeDef* SPIx,unsigned char Byte) { while( SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET) __nop(); SPI_I2S_SendData(SPIx, Byte); while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET) __nop(); return SPI_I2S_ReceiveData(SPIx); }
void SPI2_EventHandler(void) { SPI2_TimeOut=0; SPI2_Status =0; //Ricevo il dato dal MOSI RCHK += (SPI2_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPI2)); if (RxIdx++==_psize){ if(RCHK==0) { //Nuovo Messaggio ricevuto if( _SPI2_RX_Event) _SPI2_RX_Event(); } else { SPI2_Status =0x80; } // *p= (~CHK)+1; TCHK=RCHK=RxIdx=0; } //Preparo il dato sul MISO TCHK += SPI2_Buffer_Tx[RxIdx]; if (RxIdx==_psize) { SPI_I2S_SendData(SPI2, (~TCHK)+1); } else { SPI_I2S_SendData(SPI2, SPI2_Buffer_Tx[RxIdx]); } }
void caribou_spi_select(caribou_spi_t* spi) { GPIO_ClearBits(spi->select_port,1<<spi->select_pin); /* clear the receiver */ while ( SPI_I2S_GetFlagStatus(spi->select_port, SPI_I2S_FLAG_RXNE) == SET ) SPI_I2S_ReceiveData(spi->select_port); }
u8 SPI_RW(u8 dat) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, dat); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); return SPI_I2S_ReceiveData(SPI1); }
/** * Send/Receive 1 Byte through SPI * @param spi SPI index number (@ref wizpf_spi) * @param byte 1 Byte to send * @return Received 1 Byte */ uint8 wizpf_spi_byte(wizpf_spi spi, uint8 byte) { SPI_TypeDef *SPIx; switch(spi) { case WIZ_SPI1: #if defined(SPI1_SCS_PIN) && defined(SPI1_SCLK_PIN) && defined(SPI1_MISO_PIN) && defined(SPI1_MOSI_PIN) SPIx = SPI1; break; #else LOG("Not implemented"); return 0; #endif case WIZ_SPI2: #if defined(SPI2_SCS_PIN) && defined(SPI2_SCLK_PIN) && defined(SPI2_MISO_PIN) && defined(SPI2_MOSI_PIN) SPIx = SPI2; break; #else LOG("Not implemented"); return 0; #endif //case WIZ_SPI3: // break; default: ERRA("SPI(%d) is not allowed", spi); return 0; } while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPIx, byte); while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET); return (uint8)SPI_I2S_ReceiveData(SPIx); }
/** * @brief This function handles SPI interrupt request. * @param None * @retval None */ void SPIx_IRQHANDLER(void) { #ifdef SPI_SLAVE /* SPI in Slave Receiver mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE) == SET) { RxBuffer[Rx_Idx++] = SPI_I2S_ReceiveData(SPIx); } #endif #ifdef SPI_MASTER /* SPI in Master Tramitter mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_TXE) == SET) { if (CmdStatus == 0x00) { /* Send Transaction code */ SPI_I2S_SendData(SPIx, CmdTransmitted); CmdStatus = 0x01; } else { if (Tx_Idx < GetVar_NbrOfData()) { /* Send Transaction data */ SPI_I2S_SendData(SPIx, TxBuffer[Tx_Idx++]); } else { /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); } } } #endif /* SPI_SLAVE */ }
void W5500WriteByte(unsigned char byte) { while (SPI_I2S_GetFlagStatus(W5500_SPI, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(W5500_SPI, byte); while (SPI_I2S_GetFlagStatus(W5500_SPI, SPI_I2S_FLAG_RXNE) == RESET); SPI_I2S_ReceiveData(W5500_SPI); }
uint8_t SpiInOut( uint8_t outData ) { /* Send SPIy data */ SPI_I2S_SendData( SPI_INTERFACE, outData ); while( SPI_I2S_GetFlagStatus( SPI_INTERFACE, SPI_I2S_FLAG_RXNE ) == RESET ); return SPI_I2S_ReceiveData( SPI_INTERFACE ); }
/************************************************* Function: hw_spi2_writereg Description: Input: Output: Return: Others: *************************************************/ uint16_t hw_spi2_writereg(uint16_t data) { while (SPI_I2S_GetFlagStatus(SPI2_SPI, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI2_SPI, data); while (SPI_I2S_GetFlagStatus(SPI2_SPI, SPI_I2S_FLAG_RXNE) == RESET); return SPI_I2S_ReceiveData(SPI2_SPI); }
void SPI1_Set(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint8_t adress, uint8_t data){ GPIO_ResetBits(GPIOx, GPIO_Pin); //CS->0 while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); //transmit buffer empty? SPI_I2S_SendData(SPI1, adress); while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received? SPI_I2S_ReceiveData(SPI1); while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); //transmit buffer empty? SPI_I2S_SendData(SPI1, data); while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)); //data received? SPI_I2S_ReceiveData(SPI1); GPIO_SetBits(GPIOx, GPIO_Pin); //CS->1 }
static uint8_t spiWriteRead(uint8_t data) { SPI_I2S_SendData(SPI1, data); while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) {} data = (uint8_t)SPI_I2S_ReceiveData(SPI1); return data; }
/** * @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 BMA250E_SendByte(uint8_t byte) { /* Loop while DR register in not empty */ BMA250ETimeout = BMA250E_FLAG_TIMEOUT; while (SPI_I2S_GetFlagStatus(BMA250E_SPI, SPI_I2S_FLAG_TXE) == RESET) { #ifdef USE_DEFAULT_TIMEOUT_CALLBACK if((BMA250ETimeout--) == 0) return BMA250E_TIMEOUT_UserCallback(); #endif } /* Send a Byte through the SPI peripheral */ SPI_I2S_SendData(BMA250E_SPI, (uint16_t)byte); /* Wait to receive a Byte */ BMA250ETimeout = BMA250E_FLAG_TIMEOUT; while (SPI_I2S_GetFlagStatus(BMA250E_SPI, SPI_I2S_FLAG_RXNE) == RESET) { #ifdef USE_DEFAULT_TIMEOUT_CALLBACK if((BMA250ETimeout--) == 0) return BMA250E_TIMEOUT_UserCallback(); #endif } /* Return the Byte read from the SPI bus */ return (uint8_t)SPI_I2S_ReceiveData(BMA250E_SPI); }
char jbSPIReceive(SPI_TypeDef* SPIx) { while(!SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE)); // Wait until it resets itself SPI_I2S_SendData(SPIx, 0x00); while(!SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE)); return (char)SPI_I2S_ReceiveData(SPIx); }
uint8_t SPI_Read_One_Byte(SPI_TypeDef *SPIx) { /* wait untill Receive one byte */ while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET); /* Receive a Byte through the SPI peripheral */ return SPI_I2S_ReceiveData(SPIx); }
unsigned char W5500ReadByte(void) { while (SPI_I2S_GetFlagStatus(W5500_SPI, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(W5500_SPI, 0xff); while (SPI_I2S_GetFlagStatus(W5500_SPI, SPI_I2S_FLAG_RXNE) == RESET); return SPI_I2S_ReceiveData(W5500_SPI); }
/** * @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); }
uint8 wizspi_byte(uint8 byte) { while (SPI_I2S_GetFlagStatus(wizspix, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(wizspix, byte); while (SPI_I2S_GetFlagStatus(wizspix, SPI_I2S_FLAG_RXNE) == RESET); return SPI_I2S_ReceiveData(wizspix); }
/** * @brief This function handles SPI interrupt request. * @param None * @retval None */ void SPIx_IRQHANDLER(void) { /* SPI in Receiver mode */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE) == SET) { if (ubRxIndex < BUFFERSIZE) { /* Receive Transaction data */ aRxBuffer[ubRxIndex++] = SPI_I2S_ReceiveData(SPIx); } else { /* Disable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, DISABLE); } } /* SPI in Transmitter mode */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_TXE) == SET) { if (ubTxIndex < BUFFERSIZE) { /* Send Transaction data */ SPI_I2S_SendData(SPIx, aTxBuffer[ubTxIndex++]); } else { /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); } } }
static uint8_t sendByte(uint8_t byte) { while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET){}; SPI_I2S_SendData(SPI2,byte); while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET){}; return(SPI_I2S_ReceiveData(SPI2)); }
/** * @brief This function handles SPI interrupt request. * @param None * @retval None */ void BSP_SPIx_IRQHANDLER(void) { //Debug_ShowRegister( SPI2_BASE, (DebugPeripheralTypedef *)&DeBugSPI ); /* SPI in Receiver mode */ if (SPI_I2S_GetITStatus(BSP_SPIx, SPI_I2S_IT_RXNE) == SET) { if (ubRxIndex < BUFFERSIZE) { /* Receive Transaction data */ aRxBuffer[ubRxIndex++] = SPI_I2S_ReceiveData(BSP_SPIx); } else { /* Disable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(BSP_SPIx, SPI_I2S_IT_RXNE, DISABLE); } } /* SPI in Transmitter mode */ if (SPI_I2S_GetITStatus(BSP_SPIx, SPI_I2S_IT_TXE) == SET) { if (ubTxIndex < BUFFERSIZE) { /* Send Transaction data */ SPI_I2S_SendData(BSP_SPIx, aTxBuffer[ubTxIndex++]); } else { /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(BSP_SPIx, SPI_I2S_IT_TXE, DISABLE); } } }
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; }
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; }
/** * @brief Writes and receives data from the SPI * @param Data: data to be written to the SPI * @retval The received data */ uint8_t SPI1_WriteRead(uint8_t Data) { #if 0 /* This adds overhead */ /* Loop while DR register is not empty */ while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET); /* Send byte through the SPIx peripheral */ SPI_I2S_SendData(SPIx, (uint16_t)Data); /* Wait to receive a byte */ while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET); /* Return the byte read from the SPI bus */ return SPI_I2S_ReceiveData(SPIx); #endif #if 1 /* This is faster */ /* Loop while DR register is not empty */ while ((SPIx->SR & SPI_I2S_FLAG_TXE) == RESET); /* Send byte through the SPIx peripheral */ SPIx->DR = Data; /* Wait to receive a byte */ while ((SPIx->SR & SPI_I2S_FLAG_RXNE) == RESET); return SPIx->DR; #endif }
bool spiTransfer(uint8_t *out, uint8_t *in, int len) { uint8_t b; SPI2->DR; while (len--) { b = in ? *(in++) : 0xFF; while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); #ifdef STM32F303xC SPI_I2S_SendData16(SPI2, b); #endif #ifdef STM32F10X_MD SPI_I2S_SendData(SPI2, b); #endif while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); #ifdef STM32F303xC b = SPI_I2S_ReceiveData16(SPI2); #endif #ifdef STM32F10X_MD b = SPI_I2S_ReceiveData(SPI2); #endif if (out) *(out++) = b; } return true; }
uint8_t spi_read_write_byte(uint8_t txData) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, txData); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); return SPI_I2S_ReceiveData(SPI1); }
void DMA1_Stream4_IRQHandler(void) { u16 i; u8 tmp; if(DMA_GetITStatus(DMA1_Stream4, DMA_IT_TCIF4) == SET) { TP3_L; DMA_ClearITPendingBit(DMA1_Stream4, DMA_IT_TCIF4); for(i=0;i<200;i++) { __nop(); } DMA_Cmd(DMA1_Stream4,DISABLE); SPI_I2S_DMACmd(SPIFLASH_SPI,SPI_I2S_DMAReq_Tx,DISABLE); tmp=SPI_I2S_ReceiveData(SPIFLASH_SPI); SPIFLASH_CS_H; DMA_Tx_Busy = 0; TP3_H; } else if(DMA_GetITStatus(DMA1_Stream4,DMA_IT_TEIF4) == SET) { __nop(); DMA_ClearITPendingBit(DMA1_Stream4, DMA_IT_TEIF4); } }
uint8_t uSpiRead( SPI_TypeDef* SPIx ) { /* Wait until transmission complete */ while( !SPI_I2S_GetFlagStatus( SPIx, SPI_I2S_FLAG_RXNE ) ); return SPI_I2S_ReceiveData( SPIx ); }
static void WR_CMD (uint8_t cmd) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1,cmd); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); SPI_I2S_ReceiveData(SPI1); }
/*! ------------------------------------------------------------------------------------------------------------------ * Function: writetospi() * * Low level abstract function to write to the SPI * Takes two separate byte buffers for write header and write data * returns 0 for success, or -1 for error */ int writetospi ( uint16 headerLength, const uint8 *headerBuffer, uint32 bodylength, const uint8 *bodyBuffer ) { int i=0; decaIrqStatus_t stat ; stat = decamutexon() ; /* Wait for SPIx Tx buffer empty */ //while (port_SPIx_busy_sending()); dw_set_device_select(); //cs low for(i=0; i<headerLength; i++) { while (SPI_I2S_GetFlagStatus(DW1000_SPI, SPI_I2S_FLAG_TXE) == RESET){} SPI_I2S_SendData(DW1000_SPI, headerBuffer[i]); //port_SPIx_send_data(headerBuffer[i]); //send data on the SPI while (SPI_I2S_GetFlagStatus(DW1000_SPI, SPI_I2S_FLAG_RXNE) == RESET){} SPI_I2S_ReceiveData(DW1000_SPI); } for(i=0; i<bodylength; i++) { while (SPI_I2S_GetFlagStatus(DW1000_SPI, SPI_I2S_FLAG_TXE) == RESET){} SPI_I2S_SendData(DW1000_SPI, bodyBuffer[i]); //port_SPIx_send_data(bodyBuffer[i]); //send data on the SPI while (SPI_I2S_GetFlagStatus(DW1000_SPI, SPI_I2S_FLAG_RXNE) == RESET){} SPI_I2S_ReceiveData(DW1000_SPI); //while (port_SPIx_no_data()); //wait for rx buffer to fill //port_SPIx_receive_data(); //this clears RXNE bit } dw_reset_device_select(); //CS high decamutexoff(stat) ; return 0; } // end writetospi()
kick_scheduler_t spi_isr() { #ifdef SPI_IN_INTERRUPT_MODE // save the byte just received in the RX buffer switch (spi_vars.returnType) { case SPI_FIRSTBYTE: if (spi_vars.numTxedBytes==0) { *spi_vars.pNextRxByte = SPI_I2S_ReceiveData(SPI2); } break; case SPI_BUFFER: *spi_vars.pNextRxByte = SPI_I2S_ReceiveData(SPI2); spi_vars.pNextRxByte++; break; case SPI_LASTBYTE: *spi_vars.pNextRxByte = SPI_I2S_ReceiveData(SPI2); break; } // one byte less to go spi_vars.pNextTxByte++; spi_vars.numTxedBytes++; spi_vars.txBytesLeft--; if (spi_vars.txBytesLeft>0) { // write next byte to TX buffer SPI_SendData(SPI2,*spi_vars.pNextTxByte); } else { // put CS signal high to signal end of transmission to slave if (spi_vars.isLast==SPI_LAST) { GPIO_SetBits(GPIOA, GPIO_Pin_4); } // SPI is not busy anymore spi_vars.busy = 0; // SPI is done! if (spi_vars.callback!=NULL) { // call the callback spi_vars.callback(); // kick the OS return 1; } } #else while(1);// this should never happen #endif }