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(); }
void keyLedSet(unsigned short val) { GPIO_ResetBits(SPI_GPIO_LED_CS, SPI_GPIOPIN_LED_CS); SPI_SendData8(SPI2, val >> 8); SPI_SendData8(SPI2, val); while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET); GPIO_SetBits(SPI_GPIO_LED_CS, SPI_GPIOPIN_LED_CS); }
/*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); }
/** * Функция чтения данных по SPI1 * @param ptrData - адрес буфера чтения * @param dataLength - длинна посылки * @return */ uint8_t hal_spi_c::read_write(uint8_t tx_data) { if (mode != SPI_MODE_READY) { mode = SPI_MODE_ERROR; return 0; } #if 0 //#ifndef USE_STM32FXXX_SPI_DMA SPI_SendData8(SPIx, tx_data); while (!SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE)); // wait until transmit complete while (!SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE));// wait until receive complete while ( SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY)); // wait until SPI is not busy anymore //return SPI_ReceiveData8(SPIx); return SPI_I2S_ReceiveData16(SPIx); /*#else if (SPI_error != SPI_MODE_READY) return SPI_MODE_BUSY; Init_DMA(ptrDataRx, size, SPI_RX); // писать будем по полученному адресу Init_DMA(&DataTx, size, SPI_TX); // для чтения необходимо выдать столько же рандомных байт DMA_Cmd(SPI1_MASTER_Rx_DMA_Channel, ENABLE); // включаем прием DMA_Cmd(SPI1_MASTER_Tx_DMA_Channel, ENABLE); // подаем синхроимпульсы SPI_error = SPI_TRANSFER_ERROR_BUSY; return SPI_MODE_START; //#endif*/ #endif }
void SB(uint8_t Data, uint8_t DR){ if(DR == Dat) GPIO_SetBits(GPIOA, AOPin); else GPIO_ResetBits(GPIOA, AOPin); SPI_SendData8(SPI1, Data); while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); }
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; } }
void SPI_ManualSendByte(uint8_t byte) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //wait buffer empty SPI_SendData8(SPI1,byte); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); }
void SPI_SendByte(uint8_t byte) { GPIO_ResetBits(GPIOA,GPIO_Pin_4); SPI_SendData8(SPI1,byte); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); GPIO_SetBits(GPIOA,GPIO_Pin_4); }
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); }
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; }
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); }
static inline void ssp_write(spi_t *obj, int value) { SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_writeable(obj)); if(obj->bits == SPI_DATASIZE_8BIT) // 8 bit mode SPI_SendData8(spi, (uint8_t)value); else SPI_I2S_SendData16(spi, (uint16_t)value); }
void spi_slave_write(spi_t *obj, int value) { SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_writeable(obj)); if(obj->bits == SPI_DataSize_8b) // 8 bit mode SPI_SendData8(spi, (uint8_t)value); else SPI_I2S_SendData16(spi, (uint16_t)value); }
/***************************************************************************** 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 inline void ssp_write(spi_t *obj, int value) { SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); while (!ssp_writeable(obj)); if (obj->bits == SPI_DataSize_8b) { SPI_SendData8(spi, (uint8_t)value); } else { // 16-bit SPI_I2S_SendData16(spi, (uint16_t)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_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; }
void glcd_spi_write(uint8_t c) { GLCD_SELECT(); SPI_SendData8(CONTROLLER_SPI_NUMBER, (uint16_t) c); /* Wait until entire byte has been read (which we discard anyway) */ while(SPI_I2S_GetFlagStatus(CONTROLLER_SPI_NUMBER, SPI_I2S_FLAG_BSY) != RESET); GLCD_DESELECT(); }
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; }
void TM_NRF24L01_GetData(uint8_t* data) { /* Pull down chip select */ NRF24L01_CSN_LOW; /* Send read payload command*/ SPI_SendData8(NRF24L01_SPI, NRF24L01_R_RX_PAYLOAD_MASK); /* Read payload */ TM_SPI_SendMulti(NRF24L01_SPI, data, data, TM_NRF24L01_Struct.PayloadSize); /* Pull up chip select */ NRF24L01_CSN_HIGH; /* Reset status register, clear RX_DR interrupt flag */ TM_NRF24L01_WriteRegister(NRF24L01_REG_STATUS, (1 << NRF24L01_RX_DR)); }
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)); }
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 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); }
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; } }
/** * @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; }
/** * @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 ); }
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 }