STM32F030F4P6硬件SPI调试记录

最近在调试STM32F030F4的芯片,想使用硬件SPI与外部芯片通信,使用的是标准库V1.6.0版本,本以为和F103的SPI配置程序差不多,结果遇到以下两个问题,记录一下。

1、发送数据8位,但是却有16个时钟,比如发送A5,结果变成了A5_00。

经过一番查找 【单片机】STM32F030硬件SPI的坑_哐哐哐 Quan的博客-CSDN博客

 给出了详细解答。

  * @brief  Transmits a Data through the SPIx/I2Sx peripheral.
  * @param  SPIx: where x can be 1 or 2 in SPI mode to select the SPI peripheral.
  * @param  Data: Data to be transmitted.
  * @retval None
  */
void SPI_SendData8(SPI_TypeDef* SPIx, uint8_t Data)
{
  uint32_t spixbase = 0x00;

  /* Check the parameters */
  assert_param(IS_SPI_ALL_PERIPH(SPIx));

  spixbase = (uint32_t)SPIx; 
  spixbase += 0x0C;
  
  *(__IO uint8_t *) spixbase = Data;
}

使用这个函数可以解决问题,16位数据发送用下面函数

  * @brief  Transmits a Data through the SPIx/I2Sx peripheral.
  * @param  SPIx: where x can be 1 or 2 in SPI mode or 1 in I2S mode to select 
  *   the SPI peripheral. 
  * @param  Data: Data to be transmitted.
  * @retval None
  */
void SPI_I2S_SendData16(SPI_TypeDef* SPIx, uint16_t Data)
{
  /* Check the parameters */
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
  
  SPIx->DR = (uint16_t)Data;

}

原来F0的标准库把8位和16位的分开了。

2、接收数据时每次前8位数据错误,准确来说是数据凑满16位才有标志位。

以为是SPI数据超频了,使用逻辑分析仪查看。没有超频,且没有时序错误。在线调试时发现数据缓存寄存器有16位数据,原来是16位数据时才有标志位。查看库函数,发现一个新的函数:

  * @brief  Configures the FIFO reception threshold for the selected SPI.
  * @param  SPIx: where x can be 1 or 2 to select the SPI peripheral.
  * @param  SPI_RxFIFOThreshold: specifies the FIFO reception threshold.
  *   This parameter can be one of the following values:
  *     @arg SPI_RxFIFOThreshold_HF: RXNE event is generated if the FIFO 
  *          level is greater or equal to 1/2. 
  *     @arg SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO 
  *          level is greater or equal to 1/4. 
  * @retval None
  */
void SPI_RxFIFOThresholdConfig(SPI_TypeDef* SPIx, uint16_t SPI_RxFIFOThreshold)
{
  /* Check the parameters */
  assert_param(IS_SPI_ALL_PERIPH(SPIx));
  assert_param(IS_SPI_RX_FIFO_THRESHOLD(SPI_RxFIFOThreshold));

  /* Clear FRXTH bit */
  SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_FRXTH);

  /* Set new FRXTH bit value */
  SPIx->CR2 |= SPI_RxFIFOThreshold;
}

STM32F030xx硬件SPI调试记录_spi_rxfifothresholdconfig_知否,知否的博客-CSDN博客解答了。这是一个SPI接收FIFO的阈值设置函数,用来设置FIFO接收阈值(RXNE阈值)。可配置为两种模式SPI_RxFIFOThreshold_HF(16 bit)和SPI_RxFIFOThreshold_QF(8 bit),此处的配置必须和读DR寄存器长度保持对齐。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值