static void s_hal_spi_periph_disable(hal_spi_t id) 
{
    SPI_TypeDef* SPIx = HAL_spi_id2stmSPI(id);
    while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET);    // wait until it's free	
    //hal_SPI4ENCODER_DISA(SPIx);                                      
    SPI_Cmd(SPIx, DISABLE);
}
Example #2
0
/*
 * 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;
}
/**
  * @brief  Reset LCD control line(/CS) and Send Start-Byte
  * @param  Start_Byte: the Start-Byte to be sent
  * @retval None
  */
void LCD_nCS_StartByte(uint8_t Start_Byte)
{
	LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_RESET);
	SPI_I2S_SendData(LCD_SPI, Start_Byte);
	while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET) {
	}
}
Example #4
0
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 );
}
Example #5
0
static inline int ssp_writeable(spi_t *obj) {
    int status;
    SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
    // Check if data is transmitted
    status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0);
    return status;
}
    bool SpiPollingWriter::write(uint8_t *dataToSend,uint32_t numBytes,uint8_t *dataReceived) {

        // wait for ready to send

        while(numBytes--) {

            while(!readyToSend())
                if(_peripheral.hasError())
                    return false;

            // send the byte

            SPI_I2S_SendData(_peripheral,*dataToSend++);

            if(_duplex) {

                // in duplex mode and we want data, wait for it to come

                while(SPI_I2S_GetFlagStatus(_peripheral,SPI_I2S_FLAG_RXNE)==RESET)
                    if(_peripheral.hasError())
                        return false;

                // read the byte to clear RXNE and save/discard

                if(dataReceived!=NULL)
                    *dataReceived++=SPI_I2S_ReceiveData(_peripheral);
                else
                    SPI_I2S_ReceiveData(_peripheral);
            }
        }

        return true;
    }
Example #7
0
/**
  * @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);
}
Example #8
0
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;
}
Example #9
0
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);
}
Example #10
0
void SPI2_IRQHandler(void)
{
  	if (SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)!=RESET)
  	{     	
    	SPI_I2S_SendData(SPI2, 0);//??????I2S 
  	}
}
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);
}
Example #12
0
static uint8_t read_fom_reg(uint8_t addr) {

#warning "Needs porting to libopencm3 or use the real driver!"

#if 0
  AccSelect();
  SPI_I2S_SendData(SPI2, (1<<7|addr));
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
  SPI_I2S_SendData(SPI2, 0x00);
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
  uint8_t ret = SPI_I2S_ReceiveData(SPI2);
  AccUnselect();
#endif
  return ret;
}
Example #13
0
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
}
Example #14
0
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;
}
Example #15
0
uint8_t uSpiRead( SPI_TypeDef* SPIx )
{
    /* Wait until transmission complete */
    while( !SPI_I2S_GetFlagStatus( SPIx, SPI_I2S_FLAG_RXNE ) );

    return SPI_I2S_ReceiveData( SPIx );
}
Example #16
0
void write_command(unsigned char c)
{
  SPI_LCD_RS_LOW();

  SPI_LCD_CS_LOW();

  /* Send "Write Enable" instruction */
#if USE_SPI
  /* Send byte thROTATIONugh the LCD_SPIx peripheral */
  SPI_I2S_SendData(LCD_SPIx, c);
    /* Loop while DR register in not emplty */
  while (SPI_I2S_GetFlagStatus(LCD_SPIx, SPI_I2S_FLAG_TXE) == RESET);
#else
{	
	int i;
	for (i=0; i<8; i++) {
		SPI_LCD_DAT(0x01&(c>>(7-i)));
		SPI_LCD_CLK(0);
		SPI_LCD_CLK(1);	
	}
}
#endif

  /* Deselect the FLASH: Chip Select high */
  SPI_LCD_CS_HIGH();	
}
Example #17
0
void max7456_dma_irq_handler(dmaChannelDescriptor_t* descriptor) {
    if (DMA_GET_FLAG_STATUS(descriptor, DMA_IT_TCIF)) {
#ifdef MAX7456_DMA_CHANNEL_RX
        DMA_Cmd(MAX7456_DMA_CHANNEL_RX, DISABLE);
#else
        //Empty RX buffer. RX DMA takes care of it if enabled
        while (SPI_I2S_GetFlagStatus(MAX7456_SPI_INSTANCE, SPI_I2S_FLAG_RXNE) == SET) {
            MAX7456_SPI_INSTANCE->DR;
        }
#endif
        DMA_Cmd(MAX7456_DMA_CHANNEL_TX, DISABLE);

        DMA_CLEAR_FLAG(descriptor, DMA_IT_TCIF);

        SPI_I2S_DMACmd(MAX7456_SPI_INSTANCE,
#ifdef MAX7456_DMA_CHANNEL_RX
                SPI_I2S_DMAReq_Rx |
#endif
                SPI_I2S_DMAReq_Tx, DISABLE);

        DISABLE_MAX7456;
        for (uint16_t x = 0; x < max_screen_size; x++)
            max7456_screen[x + 3] = MAX7456_CHAR(' ');
        dma_transaction_in_progress = 0;
    }
}
Example #18
0
static inline int ssp_readable(spi_t *obj) {
    int status;
    SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
    // Check if data is received
    status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
    return status;
}
Example #19
0
void spi_send (uint8_t data)
{
	// wait for SPI to be ready
	while (SPI_I2S_GetFlagStatus (SPI, SPI_I2S_FLAG_TXE) == RESET);

	SPI_I2S_SendData (SPI, data);
}
Example #20
0
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);
}
Example #21
0
/*! ------------------------------------------------------------------------------------------------------------------
 * 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()
Example #22
0
/*******************************************************************************
* Function Name  : LCD_WriteRegIndex
* Description    : Writes index to select the LCD register.
* Input          : - LCD_Reg: address of the selected register.
* Output         : None
* Return         : None
*******************************************************************************/
void LCD_WriteRegIndex(u8 LCD_Reg)
{
    /* Reset LCD control line(/CS) and Send Start-Byte */
    LCD_nCS_StartByte(START_BYTE | SET_INDEX);

    /* Write 16-bit Reg Index (High Byte is 0) */
    SPI_I2S_SendData(SPI3, 0x00);
    while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) != RESET) {
        vBlockToWait( 1 );
    }
    SPI_I2S_SendData(SPI3, LCD_Reg);
    while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) != RESET) {
        vBlockToWait( 1 );
    }

    LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_SET);
}
Example #23
0
spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data )
{
  SPI_I2S_SendData( spi[ id ], data );
  
  while ( SPI_I2S_GetFlagStatus( spi[ id ], SPI_I2S_FLAG_RXNE ) == RESET );
  
  return SPI_I2S_ReceiveData( spi[ id ] );
}
Example #24
0
// return uint8_t value or -1 when failure
uint8_t spiTransferByte2(uint8_t data)
{
    uint16_t spiTimeout = SPI2_TIMEOUT;

    while (SPI_I2S_GetFlagStatus(SPI_BUSE2, SPI_I2S_FLAG_TXE) == RESET)
        if ((spiTimeout--) == 0)
            return spiTimeoutUserCallback2();

    SPI_SendData(SPI_BUSE2, data);

    spiTimeout = SPI2_TIMEOUT;
    while (SPI_I2S_GetFlagStatus(SPI_BUSE2, SPI_I2S_FLAG_RXNE) == RESET)
        if ((spiTimeout--) == 0)
            return spiTimeoutUserCallback2();

    return ((uint8_t)SPI_ReceiveData(SPI_BUSE2));
}
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);
}
/**
  * @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);
}
//Golden function sendata spi1
void mySPI_SendData(uint8_t address, uint8_t data){			// put in address and data to write to

	GPIO_ResetBits(GPIOE, GPIO_Pin_3);

	while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));  //transmit buffer empty?
	SPI_I2S_SendData(SPI1, address);
	
	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(GPIOE, GPIO_Pin_3);
	}
Example #28
0
/******************************************************************************* 
* 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;
}
Example #30
0
/**
 * @brief	Writes data to the SPI
 * @param	Data: data to be written to the SPI
 * @retval	None
 */
void RF_SPI1_Write(uint8_t Data)
{
	/* 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);
}