Beispiel #1
0
int spiReadWrite16(SPI_TypeDef *SPIx,uint8_t *rbuf, const uint16_t *tbuf, int cnt, uint16_t speed) {
  int i;
  
  SPIx->CR1 = (SPIx->CR1&~SPI_BaudRatePrescaler_256)|speed;
  SPI_DataSizeConfig(SPIx, SPI_DataSize_16b);

  for (i = 0; i < cnt; i++){
    if (tbuf) {
      //      printf("data=0x%4x\n\r",*tbuf);
      SPI_I2S_SendData16(SPIx,*tbuf++);
    } 
    else {
      SPI_I2S_SendData16(SPIx,0xffff);
    }
    while (SPI_I2S_GetFlagStatus(SPIx,SPI_I2S_FLAG_RXNE) == RESET);
    if (rbuf) {
      *rbuf++ = SPI_I2S_ReceiveData16(SPIx);
    } 
    else {
      SPI_I2S_ReceiveData16(SPIx);
    }
  }
  SPI_DataSizeConfig(SPIx, SPI_DataSize_8b);

  return i;
}
Beispiel #2
0
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);
}
Beispiel #3
0
/**
 * Функция чтения данных по 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
}
/**
  * @brief  This function handles SPI1 global interrupt request
  * @param  None
  * @retval : None
  */
void SPI1_IRQHandler(void)
{
  uint32_t tmpreg = 0x00;
  tmpreg = SPI1->SR;
#if defined (I2S_SLAVE_RECEIVER)
  if ((tmpreg&0x01)==0x01)
  {
    I2S_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData16(SPI1);
    /* Disable the Interrupt when transfer is complete */
    if(RxIdx == 32)
    {
      /* Disable the I2S1 RXNE Interrupt */
      SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, DISABLE);
    }
  }
#elif defined (I2S_MASTER_TRANSMITTER)
  if((tmpreg&0x02)==0x02)
  {
    SPI_I2S_SendData16(SPI1, I2S_Buffer_Tx[TxIdx++]);
    if(TxIdx == 32)
    {
      /* Disable the I2S1 TXE Interrupt */
      SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);
    }
  }
#endif
}
Beispiel #5
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);
#ifdef STM32F303xC
        SPI_I2S_SendData16(SPI2, b);
#endif
#ifdef STM32F10X_MD
        SPI_I2S_SendData(SPI2, b);
#endif
        while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
#ifdef STM32F303xC
        b = SPI_I2S_ReceiveData16(SPI2);
#endif
#ifdef STM32F10X_MD
        b = SPI_I2S_ReceiveData(SPI2);
#endif
        if (out)
            *(out++) = b;
    }

    return true;
}
Beispiel #6
0
void monitoring_pga(void) {
	int i = 3;
	char isResetPga = 0;
	if(driver_stat.mut)
		return;
	driver_stat.mut = 1;
	GPIO_WriteBit(GPIOD, GPIO_Pin_15, Bit_RESET);
	for(; i >= 0; i--) {
		while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
		SPI_I2S_SendData16(SPI2, driver_stat.gain[i]);
		while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
		if((driver_stat.rec_message[i] = SPI_I2S_ReceiveData16(SPI2)) != driver_stat.gain[i]) {
			isResetPga = 1;
		}
	}
	while(SPI_GetTransmissionFIFOStatus(SPI2) != SPI_TransmissionFIFOStatus_Empty);
	while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
	GPIO_WriteBit(GPIOD, GPIO_Pin_15, Bit_SET);
	if(isResetPga) {
		GPIO_WriteBit(GPIOE, GPIO_Pin_12, Bit_SET);
		driver_stat.is_overload = 1;
	}
	driver_stat.mut = 0;
	return;
}
Beispiel #7
0
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);
    }
}
Beispiel #8
0
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);
}
Beispiel #9
0
void SPI1_IRQHandler(void)
{
	uint16_t result;
  if (SPI_I2S_GetFlagStatus(SPIx,SPI_I2S_FLAG_RXNE) == SET) // Word has been received
	{
		*CS = 1;
		result = SPI_I2S_ReceiveData16(SPIx);
    Spi_cb(result);
	}
}
Beispiel #10
0
int16_t ReadEXTADC(void)
{
	uint16_t data1;
	uint16_t data2;
	SPI_I2S_ReceiveData16(SPI3);
	//SPI_DataSizeConfig(SPI3, SPI_DataSize_5b);
	GPIO_ResetBits(GPIOA, GPIO_Pin_4);
	SPI_I2S_SendData16(SPI3, 0x00);

	while(!SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE));
	data1 = SPI_I2S_ReceiveData16(SPI3);

	//SPI_DataSizeConfig(SPI3, SPI_DataSize_16b);
	SPI_I2S_SendData16(SPI3, 0x00);
	while(!SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE));
	data2 = SPI_I2S_ReceiveData16(SPI3);
	GPIO_SetBits(GPIOA, GPIO_Pin_4);
	return ((data2 >> 10) & 0x3F) + ((data1 << 6) & 0xFFC0);
}
Beispiel #11
0
int spiReadWrite16(SPI_TypeDef *SPIx, uint16_t *rbuf, const uint16_t *tbuf, int cnt, uint16_t speed) {
  int i;
  SPIx->CR1 = (SPIx->CR1 & ~SPI_BaudRatePrescaler_256) | speed;
  SPI_DataSizeConfig(SPIx, SPI_DataSize_16b);
  if ((cnt > 4) && !(cnt & 3)) {
    i =  xchng_datablock(SPIx, 1, tbuf, rbuf , cnt);
  }
  else {
    for (i = 0; i < cnt; i++){
      SPI_I2S_SendData16(SPIx, tbuf ? *tbuf++ : 0xffff);
      while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
      if (rbuf) {
    *rbuf++ = SPI_I2S_ReceiveData16(SPIx);
      } else {
    SPI_I2S_ReceiveData16(SPIx);
      }
    }
  }
  SPI_DataSizeConfig(SPIx, SPI_DataSize_8b);
  return i;
}
Beispiel #12
0
void pga2505_write(void) {
	int i =3;
	GPIO_WriteBit(GPIOD, GPIO_Pin_15, Bit_RESET);
	for(; i >= 0; i--) {
		while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
		SPI_I2S_SendData16(SPI2, driver_stat.gain[i]);
		while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
		SPI_I2S_ReceiveData16(SPI2);
	}
	while(SPI_GetTransmissionFIFOStatus(SPI2) != SPI_TransmissionFIFOStatus_Empty);
	while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);
	GPIO_WriteBit(GPIOD, GPIO_Pin_15, Bit_SET);
	return;
}
Beispiel #13
0
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);
	}	
}
Beispiel #14
0
uint16_t SPIClass::spiRX() {
  if (dataSize == SPI_DataSize_16b)
    return SPI_I2S_ReceiveData16(SPIx);
  else
    return SPI_ReceiveData8(SPIx);
}