Exemplo n.º 1
0
/**
* @brief  Wait polling the SPI until the internal WIP flag is RESET.
*         The flag is SET when a write operation is running.
* @param  None
* @retval None
*/
void EepromWaitEndWriteOperation(void)
{
  uint8_t cmd = EEPROM_CMD_RDSR;
  uint8_t dummy = 0xFF;
  uint8_t status;
  
  /* Put the SPI chip select low to start the transaction */
  EepromSPICSLow(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin());
  
  /* Send command */
  while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET);
  SPI_SendData(Get_EepromSpiPort(), cmd);
  while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_RXNE) == RESET);
  SPI_ReceiveData(Get_EepromSpiPort());
  
  /* Polling on status register */
  do{
    while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET);
    SPI_SendData(Get_EepromSpiPort(), dummy);
    while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_RXNE) == RESET);
    status = SPI_ReceiveData(Get_EepromSpiPort());
  }while(status&EEPROM_STATUS_WIP);
  
  while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET);
  
  /* Put the SPI chip select high to end the transaction */
  EepromSPICSHigh(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin());
  
}/* end EepromWaitEndWriteOperation() */
Exemplo n.º 2
0
/**
* @brief  Wait polling the SPI until the internal WIP flag is RESET.
*         The flag is SET when a write operation is running.
* @param  None
* @retval None
*/
void EepromWaitEndWriteOperation(void)
{
  uint8_t cmd = EEPROM_CMD_RDSR;
  uint8_t dummy = 0xFF;
  uint8_t status;
  //  SPI_ENTER_CRITICAL();
  
  /* Put the SPI chip select low to start the transaction */
  EepromSPICSLow();
  
  //  for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++);
  
  /* Send command */
  while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
  SPI_SendData(s_EepromSpiPort, cmd);
  while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET);
  SPI_ReceiveData(s_EepromSpiPort);
  
  /* Polling on status register */
  do{
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_EepromSpiPort, dummy);
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET);
    status = SPI_ReceiveData(s_EepromSpiPort);
  }while(status&EEPROM_STATUS_WIP);
  
  while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
  
  /* Put the SPI chip select high to end the transaction */
  EepromSPICSHigh();
  
  //  SPI_EXIT_CRITICAL();
  
}
Exemplo n.º 3
0
/**
* @brief  Write data into TX FIFO
* @param  cNbBytes: number of bytes to be written into TX FIFO
* @param  pcBuffer: pointer to data to write
* @retval SPIRIT status
*/
SpiritStatus SpiritSpiWriteLinearFifo(uint8_t cNbBytes, uint8_t* pcBuffer)
{
  uint16_t tmpstatus = 0x0000;
  uint8_t header[2];
  int i, index;
  SpiritStatus *status=(SpiritStatus *)&tmpstatus;
  
  
  /* Built the header bytes */
  header[0]=WRITE_HEADER;
  header[1]=LINEAR_FIFO_ADDRESS;
  
  SPI_ENTER_CRITICAL();
  
  /* Put the SPI chip select low to start the transaction */
  SpiritSPICSLow();
  
  {
    volatile uint16_t iv;
    for(iv=0; iv < CS_TO_SCLK_DELAY; iv++);
  }
  
  /* Write the header bytes and read the SPIRIT status bytes */
  for(i=0; i<2; i++)
  {
    while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET);
    SPI_SendData(SPIRIT_SPI_PERIPH_NB, header[i]);
    while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == RESET);
    tmpstatus += ((uint16_t)(SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB)))<<((1-i)*8);
  }
  
  /* Write the data into the FIFO according to the number of bytes */
  for(index=0; index<cNbBytes; index++)
  {
    while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET);
    SPI_SendData(SPIRIT_SPI_PERIPH_NB, pcBuffer[index]);
  }
  
  /* To be sure to don't rise the Chip Select before the end of last sending */
  while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET);
  
  if(SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == SET)
  {
    SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB);
  }
  
  /* Put the SPI chip select high to end the transaction */
  SpiritSPICSHigh();
  
  SPI_EXIT_CRITICAL();
  
  return *status;
  
}
Exemplo n.º 4
0
/**
* @brief  Write a page of the EEPROM.
*         A page size is 32 bytes.
*         The pages are 256.
*         Page 0 address: 0x0000
*         Page 1 address: 0x0020
*         ...
*         Page 255 address: 0x1FE0
*         It is allowed to write only a page for each operation. If the bytes
*         exceed the single page location, the other bytes are written at the 
*         beginning.
* @param  None
* @retval None
*/
void EepromWrite(uint16_t nAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
{
  uint8_t cmd = EEPROM_CMD_WRITE;
  uint8_t address[2];
  
  /* Wait the end of a previous write operation */
  EepromWaitEndWriteOperation();
  
  /* SET the WREN flag */
  EepromWriteEnable();
  
  for(uint8_t k=0; k<2; k++) {
    address[k] = (uint8_t)(nAddress>>((1-k)*8));
  }
  
  //  SPI_ENTER_CRITICAL();
  
  /* Put the SPI chip select low to start the transaction */
  EepromSPICSLow();
  
  //  for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++);
  
  /* Write the header bytes and read the SPIRIT status bytes */
  while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
  SPI_SendData(s_EepromSpiPort, cmd);
  while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET);
  SPI_ReceiveData(s_EepromSpiPort);
  
  for(int i=0; i<2; i++)
  {
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_EepromSpiPort, address[i]);
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET);
    SPI_ReceiveData(s_EepromSpiPort);
  }
  
  /* Writes the registers according to the number of bytes */
  for(int index=0; index<cNbBytes; index++)
  {
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_EepromSpiPort, pcBuffer[index]);   
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET);
    SPI_ReceiveData(s_EepromSpiPort);
  }
  
  while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
  
  /* Put the SPI chip select high to end the transaction */
  EepromSPICSHigh();
  
  //  SPI_EXIT_CRITICAL();
  
}
Exemplo n.º 5
0
/**
* @brief  Read single or multiple SPIRIT register
* @param  cRegAddress: base register's address to be read
* @param  cNbBytes: number of registers and bytes to be read
* @param  pcBuffer: pointer to the buffer of registers' values read
* @retval SPIRIT status
*/
SpiritStatus
SpiritSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
{
  uint16_t tmpstatus = 0x0000;
  uint8_t header[2];
  uint8_t dummy=0xFF;
  int i, index;
  SpiritStatus *status=(SpiritStatus *)&tmpstatus;
  
  /* Built the header bytes */
  header[0]=READ_HEADER;
  header[1]=cRegAddress;
  
  SPI_ENTER_CRITICAL();
  
  /* Put the SPI chip select low to start the transaction */
  SpiritSPICSLow();
  
  {
    volatile uint16_t iv;
    for(iv = 0; iv < CS_TO_SCLK_DELAY; iv++);
  }

  /* Write the header bytes and read the SPIRIT status bytes */
  for(i=0; i<2; i++)
  {
    while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET);
    SPI_SendData(SPIRIT_SPI_PERIPH_NB, header[i]);
    while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == RESET);
    tmpstatus += ((uint16_t)(SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB)))<<((1-i)*8);
  }
  
  /* Read the registers according to the number of bytes */
  for(index=0; index<cNbBytes; index++)
  {
    while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET);
    SPI_SendData(SPIRIT_SPI_PERIPH_NB, dummy);
    while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == RESET);
    *pcBuffer = SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB);
    pcBuffer++;
  }
  
  while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET);
  
  /* Put the SPI chip select high to end the transaction */
  SpiritSPICSHigh();
  
  SPI_EXIT_CRITICAL();
  
  return *status;
  
}
Exemplo n.º 6
0
/**
* @brief  Read data from RX FIFO
* @param  cNbBytes: number of bytes to read from RX FIFO
* @param  pcBuffer: pointer to data read from RX FIFO
* @retval SPIRIT status
*/
StatusBytes SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer)
{
  uint16_t tmpstatus = 0x0000;
  StatusBytes *status=(StatusBytes *)&tmpstatus;
  
  uint8_t header[2];
  uint8_t dummy=0xFF;
  
  /* Built the header bytes */
  header[0]=READ_HEADER;
  header[1]=LINEAR_FIFO_ADDRESS;
  
  SPI_ENTER_CRITICAL();
  
  /* Put the SPI chip select low to start the transaction */
  SdkEvalSPICSLow();
  
  for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++);
  
  /* Write the header bytes and read the SPIRIT status bytes */
  for(int i=0; i<2; i++)
  {
    while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_SpiPort, header[i]);
    while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_RXNE) == RESET);
    tmpstatus += ((uint16_t)(SPI_ReceiveData(s_SpiPort)))<<((1-i)*8);
  }
  
  /* Read the data from the FIFO according to the number of bytes */
  for(int index=0; index<cNbBytes; index++)
  {
    while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_SpiPort, dummy);
    while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_RXNE) == RESET);
    *pcBuffer = SPI_ReceiveData(s_SpiPort);
    pcBuffer++;
  }
  
  /* To be sure to don't rise the Chip Select before the end of last sending */
  while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET);
  
  /* Put the SPI chip select high to end the transaction */
  SdkEvalSPICSHigh();
  
  SPI_EXIT_CRITICAL();
  
  
  return *status;
  
}
Exemplo n.º 7
0
/*******************************************************************************
 * Read a Byte from MiniSD Card     Return: Data
 *******************************************************************************/
unsigned char MSD_ReadByte(void)
{
	while (SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET);
	SPI_SendData(SPI2, DUMMY);
	while (SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET);
	return SPI_ReceiveData(SPI2);
}
Exemplo n.º 8
0
/*******************************************************************************
 * Write a Byte to MiniSD Card      Input: Data
 *******************************************************************************/
unsigned char MSD_WriteByte(u8 Data)
{
	while (SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET);
	SPI_SendData(SPI2, Data);
	while (SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET);
	return SPI_ReceiveData(SPI2);
}
PUBLIC t_spi_error SER_SPI_ReceiveData(IN t_spi_device_id      spi_device_id,
                                       IN t_uint8              *data_dest_address,
                                       IN t_uint32             elements_to_be_retrieved,
                                       IN t_spi_config         *rx_spi_config
                                       )
{
    t_spi_error spi_error = SPI_OK;
    
    
    if ((t_uint32) spi_device_id >= NUM_SPI_INSTANCES)
    {
        spi_error = SPI_INVALID_PARAMETER;
        return(spi_error);
    }

    if (NULL == data_dest_address)
    {
        spi_error = SPI_INVALID_PARAMETER;
        return(spi_error);
    }

    g_ser_spi_context[spi_device_id].receive_it_mode = TRUE;
    g_ser_spi_context[spi_device_id].rcv_flag = TRUE;
    g_ser_spi_context[spi_device_id].receive_size = elements_to_be_retrieved;

    spi_error = SPI_ReceiveData(spi_device_id,data_dest_address,elements_to_be_retrieved,rx_spi_config);

    return(spi_error);
}
Exemplo n.º 10
0
/*******************************************************************************
 SPI_FLASH_SendByte
*******************************************************************************/
u8 SPI_FLASH_SendByte(u8 byte)
{
  while(SPI_GetFlagStatus(SPI3, SPI_FLAG_TXE) == RESET);
  SPI_SendData(SPI3, byte);
  while(SPI_GetFlagStatus(SPI3, SPI_FLAG_RXNE) == RESET);
  return SPI_ReceiveData(SPI3);
}
Exemplo n.º 11
0
unsigned char SpiTxRx_Byte(unsigned char data)
{
#if 1
    u16 tmpdata;

    while(SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET);
    SPI_SendData(SPI2, data);

    while (SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET);
    tmpdata = SPI_ReceiveData(SPI2);

    return tmpdata;
#endif

#if 0
//	INT8U i,temp;
//	temp = 0;

    /*等待发送寄存器空*/
    while((SPI2->SR & (uint8_t)SPI_FLAG_TXE)==RESET);
    /*发送一个字节*/
    SPI1->DR = data;
    /* 等待接收寄存器有效*/
    while((SPI2->SR & (uint8_t) SPI_FLAG_RXNE)==RESET);

    return(SPI2->DR);
#endif

}
Exemplo n.º 12
0
/*----------------------------------------------------------------------------
  SPI Serial Device "LPC17xx"
  Write and Read a byte on SPI interface
 *----------------------------------------------------------------------------*/
static uint8_t SpiWriteRead (uint8_t Data) {

	//dummy to clear status
	SPI_GetStatus(SpiInfo.pSpi);

	//read for empty buffer
	SPI_ReceiveData(SpiInfo.pSpi);

	// Send data...
	SPI_SendData(SpiInfo.pSpi, Data);

	// Wait for transfer complete
	while (SPI_CheckStatus(SPI_GetStatus(SpiInfo.pSpi), SPI_STAT_SPIF) == RESET);

	return ((uint8_t)SPI_ReceiveData(SpiInfo.pSpi));
}
Exemplo n.º 13
0
/**
* @brief  Reset the ERSR status bit.
* @param  None
* @retval Status
*/
uint8_t EepromResetSrwd(void)
{
  uint8_t status;
  uint8_t cmd[] = {EEPROM_CMD_WRSR, EEPROM_STATUS_SRWD};
  //  SPI_ENTER_CRITICAL();
  
  EepromWriteEnable();
  
  /* Put the SPI chip select low to start the transaction */
  EepromSPICSLow();
  
  //  for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++);
  
  /* Send command */
  for(uint8_t k=0;k<2;k++)
  {
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_EepromSpiPort, cmd[k]);
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET);
    status = SPI_ReceiveData(s_EepromSpiPort);
  }
  
  while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
  
  /* Put the SPI chip select high to end the transaction */
  EepromSPICSHigh();
  
  //  SPI_EXIT_CRITICAL();
  return status;
}
Exemplo n.º 14
0
uint8_t SPISendByte(uint8_t Data)
{

#ifdef USE_SPI_MODULE
  /*!< Wait until the transmit buffer is empty */
  while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET)
  {}

  /*!< Send the byte */
  SPI_SendData(Data);

  /*!< Wait to receive a byte*/
  while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET)
  {}

  /*!< Return the byte read from the SPI bus */
  return SPI_ReceiveData();
#else
  uint8_t i;
  for(i=0;i<8;i++)
  {
	GPIO_WriteLow(ENC_SCK_PORT,ENC_SCK_PIN);
        if(Data & 0x80)
          GPIO_WriteHigh(ENC_MOSI_PORT,ENC_MOSI_PIN);
        else
          GPIO_WriteLow(ENC_MOSI_PORT,ENC_MOSI_PIN);
	GPIO_WriteHigh(ENC_SCK_PORT,ENC_SCK_PIN);
	Data <<=1;
  }
  GPIO_WriteLow(ENC_SCK_PORT,ENC_SCK_PIN);
  
  return 0;
#endif
  
}
Exemplo n.º 15
0
/**
* @brief  Reset the ERSR status bit.
* @param  None
* @retval Status
*/
uint8_t EepromResetSrwd(void)
{
  uint8_t status;
  uint8_t cmd[] = {EEPROM_CMD_WRSR, EEPROM_STATUS_SRWD};
  
  EepromWriteEnable();
  
  /* Put the SPI chip select low to start the transaction */
  EepromSPICSLow(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin());
  
  /* Send command */
  for(uint8_t k=0;k<2;k++)
  {
    while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET);
    SPI_SendData(Get_EepromSpiPort(), cmd[k]);
    while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_RXNE) == RESET);
    status = SPI_ReceiveData(Get_EepromSpiPort());
  }
  
  while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET);
  
  /* Put the SPI chip select high to end the transaction */
  EepromSPICSHigh(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin());
  
  return status;
}/* end EepromResetSrwd() */
Exemplo n.º 16
0
uint16_t
spirit1_arch_refresh_status(void)
{
  volatile uint16_t mcstate = 0x0000;
  uint8_t header[2];
  int i;
  
  header[0]=0x01;
  header[1]=0;
  
  /* CS is active low */
  SPIRIT_SPI_PERIPH_CS_PORT->BSRRH = SPIRIT_SPI_PERIPH_CS_PIN;
  {
    volatile uint32_t iv;
    for(iv = 0; iv < CS_TO_SCLK_DELAY; iv++);
  }  
  
  /* send arbitrary header bytes and read out MC_STATE from SPI rx buffer */
  for(i = 0; i < 2; i++) {
    while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET);
    SPI_SendData(SPIRIT_SPI_PERIPH_NB, header[i]);
    while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == RESET);
    mcstate += ((uint16_t)(SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB)))<<((1-i)*8);
  }
  
  /* finish up */
  while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET);
  SPIRIT_SPI_PERIPH_CS_PORT->BSRRL = SPIRIT_SPI_PERIPH_CS_PIN;
  return mcstate;
}
Exemplo n.º 17
0
// return true or -1 when failure
uint8_t spiTransfer2(uint8_t *out, uint8_t *in, int len)
{
    uint16_t spiTimeout;
    uint8_t b;

    while (len--) {
        b = in ? *(in++) : 0xFF;
        spiTimeout = SPI2_TIMEOUT;
        while (SPI_I2S_GetFlagStatus(SPI_BUSE2, SPI_I2S_FLAG_TXE) == RESET) {
            if ((spiTimeout--) == 0)
                return spiTimeoutUserCallback2();
        }
        SPI_SendData(SPI_BUSE2, b);
        spiTimeout = SPI2_TIMEOUT;
        while (SPI_I2S_GetFlagStatus(SPI_BUSE2, SPI_I2S_FLAG_RXNE) == RESET) {
            if ((spiTimeout--) == 0)
                return spiTimeoutUserCallback2();
        }

        b = SPI_ReceiveData(SPI_BUSE2);
        if (out)
            *(out++) = b;
    }
    return 1;
}
Exemplo n.º 18
0
unsigned char SPI2_SendByte(unsigned char data)
{
	while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
	SPI_SendData(SPI2, data);
	while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
	return SPI_ReceiveData(SPI2);
}
Exemplo n.º 19
0
/**
 * @brief  通过SPI0发送、读取一个字节
 * @param  txData 要发送的字节
 * @retval 读取的字节
 */
u8 SPI0_ReadWriteByte(u8 txData)
{		
	u8 timeout = 200;			
	
	/* 检查SPI是否可发送 */
	while (!SPI_GetFlagStatus(HT_SPI0, SPI_FLAG_TXBE))
	{
		if(--timeout == 0)
			return 0;
	}	
	
	/* 通过SPI0发送一个字节 */
	SPI_SendData(HT_SPI0, (u32)txData); 
	
	timeout = 200;

	/* 检查SPI是否可接收 */
	while (!SPI_GetFlagStatus(HT_SPI0, SPI_FLAG_RXBNE))
	{
		if(--timeout == 0)
			return 0;
	}	
	
	/* 返回通过SPI0接收的字节 */
	return SPI_ReceiveData(HT_SPI0);  			    
}
Exemplo n.º 20
0
/**
* @brief  Write single or multiple SPIRIT register
* @param  cRegAddress: base register's address to be write
* @param  cNbBytes: number of registers and bytes to be write
* @param  pcBuffer: pointer to the buffer of values have to be written into registers
* @retval SPIRIT status
*/
StatusBytes SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
{
  uint8_t header[2];
  uint16_t tmpstatus = 0x0000;
  StatusBytes *status=(StatusBytes *)&tmpstatus;
  
  /* Built the header bytes */
  header[0]=WRITE_HEADER;
  header[1]=cRegAddress;
  
  
  SPI_ENTER_CRITICAL();
  
  /* Puts the SPI chip select low to start the transaction */
  SdkEvalSPICSLow();
  
  for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++);
  
  /* Writes the header bytes and read the SPIRIT status bytes */
  for(int i=0; i<2; i++)
  {
    while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_SpiPort, header[i]);
    while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_RXNE) == RESET);
    tmpstatus += ((uint16_t)(SPI_ReceiveData(s_SpiPort)))<<((1-i)*8);
  }
  
  /* Writes the registers according to the number of bytes */
  for(int index=0; index<cNbBytes; index++)
  {
    while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_SpiPort, pcBuffer[index]);   
    while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_RXNE) == RESET);
    SPI_ReceiveData(s_SpiPort);
  }
  
  /* To be sure to don't rise the Chip Select before the end of last sending */
  while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET);
  
  /* Puts the SPI chip select high to end the transaction */
  SdkEvalSPICSHigh();
  
  SPI_EXIT_CRITICAL();
  
  return *status;
  
}
Exemplo n.º 21
0
//spi basic read/write function
u8 EF_spiFlashRW(u8 data)
{
	while( DMA_Tx_Busy == 1) __nop();
	while( SPI_GetFlagStatus(SPIFLASH_SPI,SPI_I2S_FLAG_TXE)==RESET ) 		__nop();
	SPI_SendData(SPIFLASH_SPI,(u16)data);
	while( SPI_GetFlagStatus(SPIFLASH_SPI,SPI_I2S_FLAG_RXNE)==RESET ) 	__nop();
	return (u8)SPI_ReceiveData(SPIFLASH_SPI);
}
Exemplo n.º 22
0
void WriteReg(char add, char data) 
{
	unsigned char y;
	ss_low;    //lo

        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_TXE)== RESET);
	SPI_SendData(SPI1, add); // putcSPI1(add);         			//SEND ADR BYTE
        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE) == RESET);
	y = SPI_ReceiveData(SPI1);

        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_TXE)== RESET);
	SPI_SendData(SPI1, data); //SEND PROXY DATA
        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE) == RESET);
	y = SPI_ReceiveData(SPI1); //getcSPI1();

	ss_high;	// hi
}
Exemplo n.º 23
0
/**
* @brief  Read a page of the EEPROM.
*         A page size is 32 bytes.
*         The pages are 256.
*         Page 0 address: 0x0000
*         Page 1 address: 0x0020
*         ...
*         Page 255 address: 0x1FE0
* @param  None
* @retval None
*/
void EepromRead(uint16_t nAddress, uint8_t cNbBytes, uint8_t* pcBuffer)
{
  uint8_t cmd[3];
  cmd[0] = EEPROM_CMD_READ;

  for(uint8_t k=0; k<2; k++) {
    cmd[k+1] = (uint8_t)(nAddress>>((1-k)*8));
  }  
  
  /* Wait the end of a previous write operation */
  EepromWaitEndWriteOperation();
  
  //  SPI_ENTER_CRITICAL();
  
  /* Put the SPI chip select low to start the transaction */
  EepromSPICSLow();
  
  //  for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++);
  
  /* Write the header bytes and read the SPIRIT status bytes */
  for(uint8_t i=0; i<3; i++) {
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_EepromSpiPort, cmd[i]);
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET);
    SPI_ReceiveData(s_EepromSpiPort);
  }
  
  /* Read the registers according to the number of bytes */
  for(int index=0; index<cNbBytes; index++)
  {
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
    SPI_SendData(s_EepromSpiPort, 0xFF);
    while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET);
    *pcBuffer = SPI_ReceiveData(s_EepromSpiPort);
    pcBuffer++;
  }
  
  while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET);
  
  /* Put the SPI chip select high to end the transaction */
  EepromSPICSHigh();
  
  //  SPI_EXIT_CRITICAL();
  
}
Exemplo n.º 24
0
char Read(char add) 
{
	unsigned char y;
	add = add + 0x80; //MSB OF ADR IS HIGH FOR READ

	ss_low;    //lo

        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_TXE)== RESET);
	SPI_SendData(SPI1, add); // putcSPI1(add);         			//SEND ADR BYTE
        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE) == RESET);
	y = SPI_ReceiveData(SPI1);

        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_TXE)== RESET);
	SPI_SendData(SPI1, 0); //SEND PROXY DATA
        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE) == RESET);
	y = SPI_ReceiveData(SPI1); //getcSPI1();

	ss_high;	// hi
	return y;
}
Exemplo n.º 25
0
uint8_t HardwareSPI::transfer(uint8_t data)
{
    // Wait for TX empty
    while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)
		;
    SPI_SendData(SPIx, data);
    // Wait for RX not empty
    while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET)
		;
    return SPI_ReceiveData(SPIx);
}
Exemplo n.º 26
0
/***************************************************************************
Declaration : char spi_byte(char data_byte)

Description : Transmit and receive one byte on the SPI port
***************************************************************************/
char spi_byte(char data_byte)
{
  while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
  /* Send byte through the SPI1 peripheral */
  SPI_SendData(SPI1, data_byte);

  /* Wait to receive a byte */
  while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
  data_byte=SPI_ReceiveData(SPI1);
  /* Return the byte read from the SPI bus */
  return data_byte;	
}
Exemplo n.º 27
0
char SendStrobe(char strobe) 
{
	unsigned char out;
	ss_low;    //lo

        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_TXE)== RESET);
	SPI_SendData(SPI1, strobe); //SEND STROBE BYTE
        while (SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE) == RESET);
	out = SPI_ReceiveData(SPI1); 

	ss_high;	// hi
	return out;
}
Exemplo n.º 28
0
/*************************************************************************
 * Function Name: MmcReceiveBlock
 * Parameters: pInt8U pData, Int32U Size
 *
 * Return: none
 *
 * Description: Read block by SPI
 *
 *************************************************************************/
void MmcReceiveBlock (pInt8U pData, Int32U Size)
{
#if SPI_DMA_ENA > 0
  SPI2_DmaTransfer(pData,Size,SPI_RECEIVE);
#else
Int32U InCount = Size;
  while (InCount--)
  {
    SPI_SendData(SPI2, 0xFF);
    while(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET);
    *pData++ = SPI_ReceiveData(SPI2);
  }
#endif // SPI_DMA_ENA > 0
}
Exemplo n.º 29
0
/*************************************************************************
 * Function Name: MmcSendBlock
 * Parameters: pInt8U pData, Int32U Size
 *
 * Return: none
 *
 * Description: Send block by SPI
 *
 *************************************************************************/
void MmcSendBlock (pInt8U pData, Int32U Size)
{
#if SPI_DMA_ENA > 0
  SPI2_DmaTransfer(pData,Size,SPI_TRANSMIT);
#else
Int32U OutCount = Size;
  while (OutCount--)
  {
    SPI_SendData(SPI2, *pData++);
    while(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET);
    volatile Int32U Dummy = SPI_ReceiveData(SPI2);
  }
#endif // SPI_DMA_ENA > 0
}
Exemplo n.º 30
0
/**
  * @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 sEE_SendByte(uint8_t byte)
{
    /*!< Loop while DR register in not empty */
    while (SPI_I2S_GetFlagStatus(sEE_SPI, SPI_I2S_FLAG_TXE) == RESET);

    /*!< Send byte through the SPI peripheral */
    SPI_SendData(sEE_SPI, byte);

    /*!< Wait to receive a byte */
    while (SPI_I2S_GetFlagStatus(sEE_SPI, SPI_I2S_FLAG_RXNE) == RESET);

    /*!< Return the byte read from the SPI bus */
    return (uint8_t)SPI_ReceiveData(sEE_SPI);
}