新手上路
- 积分
- 36
- 金钱
- 36
- 注册时间
- 2019-10-16
- 在线时间
- 16 小时
|
20金钱
遇到的问题:STM32H750 (从机)能正确收到主机的消息,但是不知道为什么回复给主机的不正确,请大佬们不吝解答,感谢!!!
连接方式:其他MCU(主机)<--SPI1--> STM32H750 (从机)
出错部分代码:STM32H750通过HAL_SPI_TransmitReceive()的方式接收,buf2接收到的内容正确,buf1应该是[1,100],但是主机收到的是100个0x02(通过逻辑分析仪证实)
此函数返回HAL_TIMEOUT,通过调试发现__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP)一直为0,可能压根没往发送寄存器里面写
- for (size_t i = 0; i < 100; i++)
- {
- buf1[i] = i + 1;
- }
- HAL_SPI_TransmitReceive(&hspi1, buf1, buf2, 100, 1000);
复制代码
- while ((initial_TxXferCount > 0UL) || (initial_RxXferCount > 0UL))
- {
- /* Check the TXP flag */
- if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP)) && (initial_TxXferCount > 0UL))
- {
- *((__IO uint8_t *)&hspi->Instance->TXDR) = *((const uint8_t *)hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint8_t);
- hspi->TxXferCount--;
- initial_TxXferCount = hspi->TxXferCount;
- }
- /* Check the RXP flag */
- if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXP)) && (initial_RxXferCount > 0UL))
- {
- *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
- hspi->pRxBuffPtr += sizeof(uint8_t);
- hspi->RxXferCount--;
- initial_RxXferCount = hspi->RxXferCount;
- }
- /* Timeout management */
- if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
- {
- /* Call standard close procedure with error check */
- SPI_CloseTransfer(hspi);
- /* Unlock the process */
- __HAL_UNLOCK(hspi);
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
- hspi->State = HAL_SPI_STATE_READY;
- return HAL_TIMEOUT;
- }
- }
复制代码
初始化代码:
- void MX_SPI1_Slave_Init(void)
- {
- hspi1.Instance = SPI1;
- hspi1.Init.Mode = SPI_MODE_SLAVE;
- hspi1.Init.Direction = SPI_DIRECTION_2LINES;
- hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
- hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
- hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
- hspi1.Init.NSS = SPI_NSS_HARD_INPUT;
- hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
- hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
- hspi1.Init.CRCPolynomial = 0x0;
- hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
- hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
- hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
- hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
- hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
- hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
- hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
- hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
- hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
- hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
- if (HAL_SPI_Init(&hspi1) != HAL_OK)
- {
- Error_Handler();
- }
- }
复制代码
|
|