void ZigBee_WriteReg(uint8_t addr, uint8_t val) { SPI_TypeDef* SPI = MAKENAME(SPI,ZIGBEE_SPI); //read off any remaining bytes while(SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)) SPI_ReceiveData8(SPI); //enable NSS pin PullDown(); _delay_us(1); //wait until TX buffer is empty while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE)); uint8_t cmd = addr; cmd |= 0xC0; // write cmd SPI_SendData8(SPI, cmd); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); SPI_SendData8(SPI, val); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); _delay_us(1); PullUp(); }
void ZigBee_ReadFrame(uint8_t * data, uint8_t * len) { uint8_t resp; SPI_TypeDef* SPI = MAKENAME(SPI,ZIGBEE_SPI); //read off any bytes in the buffer while(SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); //enable select pin PullDown(); _delay_us(1); //wait until TX buffer is empty while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE)); //send the read command SPI_SendData8(SPI, 0x20); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); //receive the buffer length SPI_SendData8(SPI, 0); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); uint8_t nbytes = resp+3; //packet + lqi + ed + status (link quality indicatior) (energy detection) *len = nbytes; //set the return value // Datasheet says max buffer access is 5 + frame length = 132 // 2 reads come before frame, 3 after // First 2 reads not included in nbytes // Therefore, max nbytes size is 132 - 2 = 130 if (nbytes > 130) { *len = 0; _delay_us(1); PullUp(); return; } for (uint8_t ii=0; ii<nbytes; ii++) { SPI_SendData8(SPI, 0); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); //receive data *data++ = resp; } _delay_us(1); PullUp(); }
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; }
void spiTransfer(SPI_TypeDef *instance, uint8_t *out, const uint8_t *in, int len) { uint8_t b; instance->DR; while (len--) { b = in ? *(in++) : 0xFF; while (SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_TXE) == RESET) { } #ifdef STM32F303xC SPI_SendData8(instance, b); //SPI_I2S_SendData16(instance, b); #endif #ifdef STM32F10X SPI_I2S_SendData(instance, b); #endif while (SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_RXNE) == RESET) { } #ifdef STM32F303xC b = SPI_ReceiveData8(instance); //b = SPI_I2S_ReceiveData16(instance); #endif #ifdef STM32F10X b = SPI_I2S_ReceiveData(instance); #endif if (out) *(out++) = b; } }
/*sends the bytes*/ static uint8_t f3d_gyro_sendbyte(uint8_t byte) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_SendData8(SPI1, byte); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); return (uint8_t)SPI_ReceiveData8(SPI1); }
u8 SPI1_ReadWriteByte(u8 TxData) //SPI读写数据函数 { while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_SendData8(SPI1, TxData); while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); return SPI_ReceiveData8(SPI1); }
int spi_slave_read(spi_t *obj) { SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); if(obj->bits == SPI_DataSize_8b) // 8 bit mode return (int)SPI_ReceiveData8(spi); else return (int)SPI_I2S_ReceiveData16(spi); }
/***************************************************************************** Prototype : spiReadWriteByte Description : spi basic function Input : u8 data Output : None Return Value : Date : 2014/3/15 Author : Barry *****************************************************************************/ u8 GDflash_ReadWriteByte(u8 data) { while(SPI_I2S_GetFlagStatus(GDFLASH_SPI,SPI_I2S_FLAG_TXE)==RESET); SPI_SendData8(GDFLASH_SPI, data); while(SPI_I2S_GetFlagStatus(GDFLASH_SPI,SPI_I2S_FLAG_RXNE)==RESET); return (u8)(SPI_ReceiveData8(GDFLASH_SPI)); }
static uint8_t f3d_gyro_sendbyte(uint8_t byte) { // Poll to confirm that the transmitter is clear while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_SendData8(SPI1, byte); // Poll to confirm that the receiver is clear while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); return (uint8_t)SPI_ReceiveData8(SPI1); }
uint8_t rx_byte (void) { uint8_t data; CS_CLEAR(); data = SPI_ReceiveData8 (SPI1); CS_SET(); return data; }
int spi_slave_read(spi_t *obj) { SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); if (obj->bits == SPI_DataSize_8b) { return (int)SPI_ReceiveData8(spi); } else { // 16-bit return (int)SPI_I2S_ReceiveData16(spi); } }
static inline int ssp_read(spi_t *obj) { SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_readable(obj)); if(obj->bits == SPI_DataSize_8b) // 8 bit mode return (int)SPI_ReceiveData8(spi); else // 16 bit mode return (int)SPI_I2S_ReceiveData16(spi); }
uint8_t TM_NRF24L01_ReadRegister(uint8_t reg) { uint8_t value; NRF24L01_CSN_LOW; SPI_SendData8(NRF24L01_SPI, NRF24L01_READ_REGISTER_MASK(reg)); value = SPI_ReceiveData8(NRF24L01_SPI); NRF24L01_CSN_HIGH; return value; }
uint8_t SPI_TransRecieve(uint8_t data){ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_SendData8(SPI1,data); // while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); return SPI_ReceiveData8(SPI1); SPI_I2S_ClearFlag(SPI1, SPI_I2S_FLAG_RXNE); }
uint8_t TM_NRF24L01_GetStatus(void) { uint8_t status; NRF24L01_CSN_LOW; /* First received byte is always status register */ status = SPI_ReceiveData8(NRF24L01_SPI); /* Pull up chip select */ NRF24L01_CSN_HIGH; return status; }
int spiReadWrite(SPI_TypeDef *SPIx, uint8_t *rbuf, const uint8_t *tbuf, int cnt, uint16_t speed) { int i; SPIx->CR1 = (SPIx->CR1 & ~SPI_BaudRatePrescaler_256) | speed; if ((cnt > 4) && !(cnt & 1)) { return xchng_datablock(SPIx, 0, tbuf, rbuf , cnt); } else { for (i = 0; i < cnt; i++){ SPI_SendData8(SPIx, tbuf ? *tbuf++ : 0xff); while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET); if (rbuf) { *rbuf++ = SPI_ReceiveData8(SPIx); } else { SPI_ReceiveData8(SPIx); } } return i; } }
uint8_t ZigBee_WriteFrame(const void* data, uint8_t len) { if (len > MAX_ZIGBEE_PACKET_BYTES) return -1; SPI_TypeDef* SPI = MAKENAME(SPI,ZIGBEE_SPI); const uint8_t *tx_ptr = data; //clear any remaining bytes while(SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)) SPI_ReceiveData8(SPI); PullDown(); _delay_us(1); //wait until TX buffer is empty while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE)); SPI_SendData8(SPI, 0x60); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); SPI_SendData8(SPI, len+2); //two extra bytes for the 16-bit CRC while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); for(int ii = 0; ii < len; ii++) { SPI_SendData8(SPI, tx_ptr[ii]); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); } _delay_us(1); PullUp(); ZigBee_WriteReg(REG_TRX_STATE, 0x02); //alternate method for signaling to send return 0; }
int spiReadWrite(SPI_TypeDef *SPIx,uint8_t *rbuf, const uint8_t *tbuf, int cnt, uint16_t speed) { int i; int timeout; SPIx->CR1 = (SPIx->CR1&~SPI_BaudRatePrescaler_256)|speed; for (i = 0; i < cnt; i++){ if (tbuf) { SPI_SendData8(SPIx,*tbuf++); } else { SPI_SendData8(SPIx,0xff); } timeout = 100; while (SPI_I2S_GetFlagStatus(SPIx,SPI_I2S_FLAG_RXNE) == RESET); if (rbuf) { *rbuf++ = SPI_ReceiveData8(SPIx); } else { SPI_ReceiveData8(SPIx); } } return i; }
void ZigBee_ReadReg(uint8_t addr, uint8_t len, uint8_t * dest) { uint8_t resp; SPI_TypeDef* SPI = MAKENAME(SPI,ZIGBEE_SPI); //read off any remaining bytes while(SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)) resp = SPI_ReceiveData8(SPI); PullDown(); _delay_us(1); for(uint8_t ii=0; ii<len; ii++) { //wait until TX buffer is empty while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE)); uint8_t cmd = addr; cmd |= 0x80; // read cmd SPI_SendData8(SPI, cmd); //send the command while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); //this is PHY_STATUS byte from ZigBee SPI_SendData8(SPI, 0); // send dummy while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); *dest++ = resp; } _delay_us(1); PullUp(); }
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)); }
/** * @brief Write a byte on the SD. * @param Data: byte to send. * @retval None */ uint8_t SD_WriteByte(uint8_t Data) { /*!< Wait until the transmit buffer is empty */ while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET) { } /*!< Send the byte */ SPI_SendData8(SD_SPI, Data); /*!< Wait to receive a byte*/ while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET) { } /*!< Return the byte read from the SPI bus */ return SPI_ReceiveData8(SD_SPI); }
uint8_t SPI_Send(SPI_TypeDef* SPIx, uint8_t data) { /* Check if SPI is enabled */ SPI_CHECK_ENABLED_RESP(SPIx, 0); /* Wait for previous transmissions to complete if DMA TX enabled for SPI */ SPI_WAIT(SPIx); /* Fill output buffer with data */ //SPIx->DR = data; SPI_SendData8(SPIx, data); /* Wait for transmission to complete */ SPI_WAIT(SPIx); /* Return data from buffer */ //return SPIx->DR; return SPI_ReceiveData8(SPIx); }
/** * @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 L3GD20_SendByte(uint8_t byte) { /* Loop while DR register in not empty */ L3GD20Timeout = L3GD20_FLAG_TIMEOUT; while (SPI_I2S_GetFlagStatus(L3GD20_SPI, SPI_I2S_FLAG_TXE) == RESET) { if ((L3GD20Timeout--) == 0) return L3GD20_TIMEOUT_UserCallback(); } /* Send a Byte through the SPI peripheral */ SPI_SendData8(L3GD20_SPI, byte); /* Wait to receive a Byte */ L3GD20Timeout = L3GD20_FLAG_TIMEOUT; while (SPI_I2S_GetFlagStatus(L3GD20_SPI, SPI_I2S_FLAG_RXNE) == RESET) { if ((L3GD20Timeout--) == 0) return L3GD20_TIMEOUT_UserCallback(); } /* Return the Byte read from the SPI bus */ return (uint8_t) SPI_ReceiveData8(L3GD20_SPI ); }
/** * @brief Read a byte from the SD. * @param None * @retval The received byte. */ uint8_t SD_ReadByte(void) { uint8_t Data = 0; /*!< Wait until the transmit buffer is empty */ while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET) { } /*!< Send the byte */ SPI_SendData8(SD_SPI, SD_DUMMY_BYTE); /*!< Wait until a data is received */ while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET) { } /*!< Get the received data */ Data = SPI_ReceiveData8(SD_SPI); /*!< Return the shifted data */ return Data; }
uint8_t spiTransferByte(SPI_TypeDef *instance, uint8_t data) { while (SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_TXE) == RESET) { } #ifdef STM32F303xC SPI_SendData8(instance, data); #endif #ifdef STM32F10X SPI_I2S_SendData(instance, data); #endif while (SPI_I2S_GetFlagStatus(instance, SPI_I2S_FLAG_RXNE) == RESET){ } #ifdef STM32F303xC return ((uint8_t)SPI_ReceiveData8(instance)); #endif #ifdef STM32F10X return ((uint8_t)SPI_I2S_ReceiveData(instance)); #endif }
void SPI_SendMulti(SPI_TypeDef* SPIx, uint8_t* dataOut, uint8_t* dataIn, uint32_t count) { uint32_t i; /* Check if SPI is enabled */ SPI_CHECK_ENABLED(SPIx); /* Wait for previous transmissions to complete if DMA TX enabled for SPI */ SPI_WAIT(SPIx); for (i = 0; i < count; i++) { /* Fill output buffer with data */ //SPIx->DR = dataOut[i]; SPI_SendData8(SPIx, dataOut[i]); /* Wait for SPI to end everything */ SPI_WAIT(SPIx); /* Read data register */ //dataIn[i] = SPIx->DR; dataIn[i] = SPI_ReceiveData8(SPIx); } }
UI CMasterSPIHardware::transmit(UI data, ssel_t ssel) { while (SPI_I2S_GetFlagStatus(_spi, SPI_I2S_FLAG_RXNE) == SET) ; if ((_spi->CR2 & 0xf00 ) > SPI_DataSize_8b) { SPI_I2S_SendData16(_spi, data); while ((_spi->SR & 3UL << 11) != 0)//FIFO is not empty ; while (SPI_I2S_GetFlagStatus(_spi, SPI_I2S_FLAG_RXNE) != SET) ; return SPI_I2S_ReceiveData16(_spi); } else { SPI_SendData8(_spi, data); while ((_spi->SR & 3UL << 11) != 0)//FIFO is not empty ; while (SPI_I2S_GetFlagStatus(_spi, SPI_I2S_FLAG_RXNE) != SET) ; return SPI_ReceiveData8(_spi); } }
uint8_t spi_send_rcv(uint8_t *data,int len) { int i=0; uint8_t r=0; /* Enable the SPI peripheral */ // SPI_SSOutputCmd(SPI1, ENABLE); //GPIO_ResetBits(GPIOA,GPIO_Pin_4); //SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Set); //rt_thread_delay(1); for(i=0;i<len;i++) { if(check_status(SPI_I2S_IT_TXE)) SPI_SendData8(SPI1, data[i]); if(check_status(SPI_I2S_IT_RXNE)) r=SPI_ReceiveData8(SPI1); } //SPI_NSSInternalSoftwareConfig(SPI1,SPI_NSSInternalSoft_Reset); /* Disable the SPI peripheral */ // SPI_SSOutputCmd(SPI1, DISABLE); //GPIO_SetBits(GPIOA,GPIO_Pin_4); return r; }
// 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 }
/** * @brief This function handles SPI interrupt request. * @param None * @retval None */ void SPI3_IRQHandler(void) { #if defined (SPI_SLAVE) /* SPI in Slave Tramitter mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_TXE) == SET) { SPI_SendData8(SPIx, TxBuffer[Tx_Idx++]); if (Tx_Idx == GetVar_NbrOfData()) { /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); } } /* SPI in Slave Receiver mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE) == SET) { if (CmdReceived == 0x00) { CmdReceived = SPI_ReceiveData8(SPIx); CmdStatus = 0x01; } else { RxBuffer[Rx_Idx++] = SPI_ReceiveData8(SPIx); } } /* SPI Error interrupt--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_OVR) == SET) { SPI_ReceiveData8(SPIx); SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_OVR); } #endif /* SPI_SLAVE*/ #if defined (SPI_MASTER) /* SPI in Master Tramitter mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_TXE) == SET) { if (CmdStatus == 0x00) { SPI_SendData8(SPIx, CmdTransmitted); CmdStatus = 0x01; } else { SPI_SendData8(SPIx, TxBuffer[Tx_Idx++]); if (Tx_Idx == GetVar_NbrOfData()) { /* Disable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); } } } /* SPI in Master Receiver mode--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE) == SET) { if (CmdReceived == 0x00) { CmdReceived = SPI_ReceiveData8(SPIx); Rx_Idx = 0x00; } else { RxBuffer[Rx_Idx++] = SPI_ReceiveData8(SPIx); } } /* SPI Error interrupt--------------------------------------- */ if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_OVR) == SET) { SPI_ReceiveData8(SPIx); SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_OVR); } #endif /* SPI_MASTER*/ }