extern hal_result_t hal_spi_raw_master_writeread(hal_spi_t id, uint8_t byte, uint8_t* readbyte) { hal_spi_internal_item_t* intitem = s_hal_spi_theinternals.items[HAL_spi_id2index(id)]; hal_spi_cfg_t* cfg = NULL; volatile uint32_t timeout = 0; SPI_TypeDef* SPIx = HAL_spi_id2stmSPI(id); if(hal_false == hal_spi_hid_initted_is(id)) { return(hal_res_NOK_generic); } cfg = &intitem->config; if(hal_spi_act_raw != cfg->activity) { return(hal_res_NOK_generic); } // before we write we need to wait for the spi has txe set timeout = s_hal_spi_timeout_flag; while(RESET == SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE)) { if(0 == (timeout--)) s_hal_spi_timeoutexpired(); } // ok. we send the byte SPI_I2S_SendData(SPIx, byte); // we need to wait for a reply from the slave timeout = s_hal_spi_timeout_flag; while(RESET == SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE)) { if(0 == (timeout--)) s_hal_spi_timeoutexpired(); } // ok. here it is uint8_t rb = SPI_I2S_ReceiveData(SPIx); // if we want to retrieve it we copy into return value if(NULL != readbyte) { *readbyte = rb; } return(hal_res_OK); }
static void s_hal_spi_read_isr(hal_spi_t id) { hal_spi_internal_item_t* intitem = s_hal_spi_theinternals.items[HAL_spi_id2index(id)]; SPI_TypeDef* SPIx = HAL_spi_id2stmSPI(id); uint8_t rxbyte = SPI_I2S_ReceiveData(SPIx); if(intitem->isrrxcounter < intitem->config.sizeofframe) { intitem->isrrxframe[intitem->isrrxcounter] = rxbyte; intitem->isrrxcounter ++; } if(intitem->isrrxcounter == intitem->config.sizeofframe) { // ok. the frame is finished // 1. stop spi s_hal_spi_periph_disable(id); // disable periph s_hal_spi_rx_isr_disable(id); //hal_SPI4ENCODER_IT_RX_DISA(port); // disable interrupt rx // set back to zero the frame burst intitem->frameburstcountdown = 0; // set rx counter to zero again intitem->isrrxcounter = 0; // copy the rxframe into the rx fifo if(hal_true == hal_utility_fifo_full(&(intitem->fiforx))) { hal_utility_fifo_pop(&(intitem->fiforx)); } hal_utility_fifo_put(&(intitem->fiforx), intitem->isrrxframe); // now manage the callback hal_callback_t onframereceiv = intitem->config.onframereceiv; void *arg = intitem->config.argonframereceiv; if(NULL != onframereceiv) { onframereceiv(arg); } } else { // transmit one dummy byte to trigger yet another reception SPI_I2S_SendData(SPIx, intitem->config.dummytxvalue); } }
void GPIO_Configuration_PinB2(void){ GPIO_InitTypeDef irq ; EXTI_InitTypeDef EXTI_InitStructure ; NVIC_InitTypeDef NVIC_InitStructure ; irq.GPIO_Pin = GPIO_Pin_2; irq.GPIO_Speed = GPIO_Speed_10MHz; irq.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &irq); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource2); // Configure EXTI1 line EXTI_InitStructure.EXTI_Line = EXTI_Line2; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x07; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); void EXTI1_IRQHandler(void){ if(EXTI_GetITStatus(EXTI_Line2) != RESET){ // if(SPI_I2S_IT_TXE==1){ if(MASK_TX_DS==0){ //Active low void nRF24L01_SPI_Send_Byte(unsigned char* data,unsigned long sizeof_tx){ nRF24L01_SPI_NSS_L(); // Set CSN low, init SPI tranaction while(tx_element<=sizeof_tx){ tx_element++; /* Send byte through the SPI2 peripheral */ SPI_I2S_SendData(SPI2, data[tx_element]); } nRF24L01_SPI_NSS_H(); } } if(MASK_RX_DR==0){ void nRF24L01_SPI_receive_Byte(unsigned char* R,unsigned long sizeof_rx){ nRF24L01_SPI_NSS_L(); // Set CSN low, init SPI tranaction while(rx_element<=sizeof_rx){ R[rx_element]=SPI_I2S_ReceiveData(SPI2); rx_element++; } nRF24L01_SPI_NSS_H(); }
/** * @brief Writes data to the SPI * @param Data: data to be written to the SPI * @retval None */ void SPI1_Write(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); #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; #endif }
/** * @brief Receives 1 Byte that has been send over the SPI Bus * @param None * @retval The received Byte */ uint8_t D_SPI_ReceiveData(){ /*!< Wait until the transmit buffer is empty */ while(SPI_I2S_GetFlagStatus(D_SPI, SPI_I2S_FLAG_TXE) == RESET) { } SPI_I2S_SendData(D_SPI, DUMMYBYTE); /*!< Wait to receive a byte*/ while(SPI_I2S_GetFlagStatus(D_SPI, SPI_I2S_FLAG_RXNE) == RESET) { } return SPI_I2S_ReceiveData(D_SPI); }
/******************************************************************************* * Function Name : SPI2_IRQHandler * Description : This function handles SPI2 global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void SPI2_IRQHandler(void) { /* Check the interrupt source */ if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_TXE) == SET) { /* Send a data from I2S2 */ SPI_I2S_SendData(SPI2, I2S2_Buffer_Tx[TxIdx++]); } /* Check the end of buffer transfer */ if (RxIdx == 32) { /* Disable the I2S2 TXE interrupt to end the communication */ SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE); } }
/******************************************************************************* * Function Name : SPIGetChar * Description : Read a byte from the SPI. * Input : None. * Output : None * Return : The received byte. *******************************************************************************/ u8 SPIGetChar(void) { u8 Data = 0; /* Wait until the transmit buffer is empty */ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); /* Send the byte */ SPI_I2S_SendData(SPI1, 0xFF); /* Wait until a data is received */ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); /* Get the received data */ Data = SPI_I2S_ReceiveData(SPI1); /* 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 to send. * @retval : The value of the received byte. */ unsigned char spi_flash_send_byte(unsigned char byte) { volatile short i = 0; /* Loop while DR register in not emplty */ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); /* Send byte through the SPI1 peripheral */ SPI_I2S_SendData(SPI1, byte); /* Wait to receive a byte */ //while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); for(i=0; i<10; i++); /* Return the byte read from the SPI bus */ return SPI_I2S_ReceiveData(SPI1); }
/** * @brief Sends a Half Word through the SPI interface and return the * Half Word received from the SPI bus. * @param HalfWord : Half Word to send. * @retval : The value of the received Half Word. */ unsigned short spi_flash_send_halfword(unsigned short HalfWord) { volatile short i = 0; /* Loop while DR register in not emplty */ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); /* Send Half Word through the SPI1 peripheral */ SPI_I2S_SendData(SPI1, HalfWord); /* Wait to receive a Half Word */ //while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); for(i=0; i<10; i++); /* Return the Half Word read from the SPI bus */ return SPI_I2S_ReceiveData(SPI1); }
/** * @brief Sends a Half Word through the SPI interface and return the Half Word * received from the SPI bus. * @param HalfWord: Half Word to send. * @retval The value of the received Half Word. */ uint16_t l3gd20_SendHalfWord(uint16_t HalfWord) { /* Loop while DR register in not empty */ while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET); l3gd20_CS_LOW(); /* Send Half Word through the sFLASH peripheral */ SPI_I2S_SendData(SPIx, HalfWord); /*!< Wait to receive a Half Word */ while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET); l3gd20_CS_HIGH(); /* Return the Half Word read from the SPI bus */ return SPI_I2S_ReceiveData(SPIx); }
// Blit a sprite to screen void oled_blit(uint8_t x, uint8_t y, uint8_t w, uint8_t h, void *buffer) { oled_window(x, y, x + w - 1, y + h - 1); pin_clear(OLED_NCS); pin_clear(OLED_A0); SPI_I2S_SendData(OLED_SPI, 0xC); oled_wait_spi(); pin_set(OLED_A0); oled_dma((uint8_t*)buffer, (w * h) << 1); oled_wait_dma(); oled_wait_spi(); pin_set(OLED_NCS); }
/** * @brief Transmit data on SPI1 * @param data Sent data * @return Received data * @warning Blocking function! */ uint8_t SPI1_Transmit(uint8_t data) { uint8_t i; // Loop while transmit register in not empty while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, data); // Send byte (start transmit) // Wait for new data (transmit end) while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); i = SPI_I2S_ReceiveData(SPI1); // Received data return i; }
void HCMS3907_putBufferPolled(char *buf, uint8_t len) { HCMS3907_DATA(); HCMS3907_SELECT(); while (len) { // always make sure the transmit buffer is free while (SPI_I2S_GetFlagStatus(SPI_PORT, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI_PORT, *buf++); len--; } // be sure the last byte is sent to the shift register while (SPI_I2S_GetFlagStatus(SPI_PORT, SPI_I2S_FLAG_TXE) == RESET); // and then wait until it goes over the wire while (SPI_I2S_GetFlagStatus(SPI_PORT, SPI_I2S_FLAG_BSY) == SET); HCMS3907_DESELECT(); }
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); SPI_I2S_SendData(SPI2, b); while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); b = SPI_I2S_ReceiveData(SPI2); if (out) *(out++) = b; } return true; }
unsigned short DRV_Spi_RW_Byte( DRV_Spi_Handle Handle , unsigned char ucOut) { Spi_Device_Data *pSpiData = (Spi_Device_Data *) Handle ; if( pSpiData == NULL) return 0; /* Send byte through the SPI peripheral */ SPI_I2S_SendData(pSpiData->pCfg->pDevice, ucOut); /* Wait to receive a byte */ while (SPI_I2S_GetFlagStatus(pSpiData->pCfg->pDevice, SPI_I2S_FLAG_RXNE) == RESET) { ; } /* Return the byte read from the SPI bus */ return SPI_I2S_ReceiveData(pSpiData->pCfg->pDevice); }
/* * Write one byte to SPI and read one byte * * @param[in] data Byte to write * @return Byte read */ uint8_t spi_send_receive (uint8_t data) { uint8_t value; // wait for SPI to be ready while (SPI_I2S_GetFlagStatus (SPI, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData (SPI, data); // wait for byte to be received while (SPI_I2S_GetFlagStatus (SPI, SPI_I2S_FLAG_RXNE) == RESET); value = SPI_I2S_ReceiveData (SPI); return value; }
unsigned char SPI_ReadWrite(SPI_TypeDef* SPIx, unsigned char writedat) { unsigned char Data = 0; //Wait until the transmit buffer is empty while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE)==RESET); // Send the byte SPI_I2S_SendData(SPIx, writedat); //Wait until a data is received while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE)==RESET); // Get the received data Data = SPI_I2S_ReceiveData(SPIx); // Return the shifted data return Data; }
/******************************************************************************* * Function Name : SPI_ReadWriteByte(u8 TxData) * Description : 读写数据 * Input : TxData * Output : RxData * Return : RxData *******************************************************************************/ uint8_t SPI1_ReadWriteByte(uint8_t TxData) { uint8_t retry = 0; while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET) { retry++; if(retry>200)return 0; } SPI_I2S_SendData(SPI1,TxData); retry = 0; while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) == RESET) { retry++; if(retry>200)return 0; } return SPI_I2S_ReceiveData(SPI1); }
uint8_t at86rf231_spi_transfer_byte(uint8_t byte) { uint8_t ret; // wait for tx buffer to be empty while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, byte); // wait for rx buffer to be not empty while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); ret = SPI_I2S_ReceiveData(SPI1); // wait until it is not busy while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET); return ret; }
/** * @brief Writes to the selected LCD register. * @param LCD_Reg: address of the selected register. * @param LCD_RegValue: value to write to the selected register. * @retval None */ void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue) { /* Write 16-bit Index (then Write Reg) */ LCD_WriteRegIndex(LCD_Reg); /* Write 16-bit Reg */ /* Reset LCD control line(/CS) and Send Start-Byte */ LCD_nCS_StartByte(START_BYTE | LCD_WRITE_REG); SPI_I2S_SendData(LCD_SPI, LCD_RegValue>>8); while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET) { } SPI_I2S_SendData(LCD_SPI, (LCD_RegValue & 0xFF)); while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET) { } LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET); }
/** * @brief Read a byte from the SD. * @param None * @retval The received byte. */ uint8_t SD_ReadByte(void) { uint8_t Data = 0; /*!< Send the byte */ SPI_I2S_SendData(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_I2S_ReceiveData(SD_SPI); /*!< Return the shifted data */ return Data; }
void glcd_spi_write(uint8_t c) { //uint8_t temp; GLCD_SELECT(); /*!< Loop while DR register in not emplty */ while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPIx, (uint16_t) c); /* Wait until entire byte has been read (which we discard anyway) */ while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) != RESET); //temp = SPI_I2S_ReceiveData(SPIx); GLCD_DESELECT(); }
/** * @brief Sends a byte through the SPI interface and return the byte received * from the SPI bus. * @param byte: byte to send. * @retval The value of the received byte. */ uint8_t SpiSendByte(uint8_t byte) { /*!< Loop while DR register in not emplty */ while (SPI_I2S_GetFlagStatus(WIFI_SPI, SPI_I2S_FLAG_TXE) == RESET); /*!< Send byte through the SPI1 peripheral */ SPI_I2S_SendData(WIFI_SPI, byte); /*!< Wait to receive a byte */ //while (SPI_I2S_GetFlagStatus(WIFI_SPI, SPI_I2S_FLAG_RXNE) == RESET); /*!< Return the byte read from the SPI bus */ //return SPI_I2S_ReceiveData(WIFI_SPI); return 0; }
void LCDTFTConf::LCD_WriteData(uint8_t value) { /* Set WRX to send data */ LCD_CtrlLinesWrite(LCD_WRX_GPIO_PORT, LCD_WRX_PIN, Bit_SET); /* Reset LCD control line(/CS) and Send data */ LCD_ChipSelect(DISABLE); SPI_I2S_SendData(LCD_SPI, value); /* Wait until a data is sent(not busy), before config /CS HIGH */ while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_TXE) == RESET) ; while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET); LCD_ChipSelect(ENABLE); }
//SPIx 读写一个字节 //TxData:要写入的字节 //返回值:读取到的字节 u8 SPIx_ReadWriteByte(u8 TxData) { u8 retry=0; while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位 { retry++; if(retry>200)return 0; } SPI_I2S_SendData(SPI2, TxData); //通过外设SPIx发送一个数据 retry=0; while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //检查指定的SPI标志位设置与否:接受缓存非空标志位 { retry++; if(retry>200)return 0; } return SPI_I2S_ReceiveData(SPI2); //返回通过SPIx最近接收的数据 }
/* * 函数名:SPIx_ReadWriteByte * 描述 :SPI1 读写一个字节(全双工模式,写入数据的同时读取数据) * 输入 :TxData:写入的字节 * 输出 :返回读出的字节 * 调用 :外部调用 */ u8 SPIx_ReadWriteByte(u8 TxData) { u8 retry = 0; while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) != SET)//检测发送缓冲区是否清空 { retry++; if(retry>200) return 0; } SPI_I2S_SendData(SPI1, TxData);////通过外设SPI1发送一个数据 retry = 0; while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) != SET)//检测接受缓存是否非空 { retry++; if(retry>200) return 0; } return SPI_I2S_ReceiveData(SPI1); //返回通过SPIx最近接收的数据 }
// Pushes n pixels of specific color (to the window) void oled_push(uint16_t pixel, uint16_t count) { pin_clear(OLED_NCS); pin_clear(OLED_A0); SPI_I2S_SendData(OLED_SPI, 0xC); oled_wait_spi(); pin_set(OLED_A0); pixel = ntohs(pixel); // Convert to big-endian DMA2_Stream3->CR = repeat_cr; // Have DMA repeat word over and over DMA2_Stream3->M0AR = (uint32_t)&pixel; DMA2_Stream3->NDTR = count * 2; DMA_Cmd(DMA2_Stream3, ENABLE); // Begin transfer SPI_I2S_DMACmd(OLED_SPI, SPI_I2S_DMAReq_Tx, ENABLE); oled_wait_dma(); // Wait for transfer to finish oled_wait_spi(); DMA2_Stream3->CR = stream_cr; // Restore streaming DMA pin_set(OLED_NCS); }
/** * @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_I2S_SendData(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_I2S_ReceiveData(SD_SPI); }
u08 spi_master_SendByte(u08 data) { u16 tmp; tmp = (u16) data; // wait for last transfer compelte while(SPI_I2S_GetFlagStatus(SPI_SPI, SPI_I2S_FLAG_TXE) == RESET) ; SPI_I2S_SendData(SPI_SPI, tmp); //wait for SPI Recieve buffer has vailed data while(SPI_I2S_GetFlagStatus(SPI_SPI, SPI_I2S_FLAG_RXNE)== RESET) ; // read the Recieve buffer, RXNE is clear at the same time tmp = SPI_I2S_ReceiveData(SPI_SPI); return (u08) (tmp & 0x00ff); }
static uint16_t spi_transfer( const platform_spi_t* spi, uint16_t data ) { /* Wait until the transmit buffer is empty */ while ( SPI_I2S_GetFlagStatus( spi->port, SPI_I2S_FLAG_TXE ) == RESET ) { } /* Send the byte */ SPI_I2S_SendData( spi->port, data ); /* Wait until a data is received */ while ( SPI_I2S_GetFlagStatus( spi->port, SPI_I2S_FLAG_RXNE ) == RESET ) { } /* Get the received data */ return SPI_I2S_ReceiveData( spi->port ); }