我有一块F103C8T6的板子,有例程,可实现正常TCP客户端通信。发送函数为:
void SPI1_Send_Byte(unsigned char dat)
{
SPI_I2S_SendData(SPI1,dat);//写1个字节数据
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);//等待数据寄存器空
}
void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)
{
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
/* Write in the DR register the data to be sent */
SPIx->DR = Data;
}
FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));
/* Check the status of the specified SPI/I2S flag */
if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)
{
/* SPI_I2S_FLAG is set */
bitstatus = SET;
}
else
{
/* SPI_I2S_FLAG is reset */
bitstatus = RESET;
}
/* Return the SPI_I2S_FLAG status */
return bitstatus;
}
接收函数
uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)
{
/* Check the parameters */
assert_param(IS_SPI_ALL_PERIPH(SPIx));
/* Return the data in the DR register */
return SPIx->DR;
}
unsigned char Read_W5500_1Byte(unsigned short reg)
{
unsigned char i;
GPIO_ResetBits(W5500_SCS_PORT, W5500_SCS);//置W5500的SCS为低电平
SPI1_Send_Short(reg);//通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM1|RWB_READ|COMMON_R);//通过SPI1写控制字节,1个字节数据长度,读数据,选择通用寄存器
i=SPI_I2S_ReceiveData(SPI1);
SPI1_Send_Byte(0x00);//发送一个哑数据
i=SPI_I2S_ReceiveData(SPI1);//读取1个字节数据
GPIO_SetBits(W5500_SCS_PORT, W5500_SCS);//置W5500的SCS为高电平
return i;//返回读取到的寄存器数据
}
我另一块板子F303ZET6,用STM32CubeMX生成TCP客户端初始化代码。
生成的发送函数原型:
HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
接收函数原型:
HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
我的发送函数:
void SPI1_Send_Byte(unsigned char dat)
{
unsigned char Data[1];
Data[0]=dat;
HAL_SPI_Transmit(&hspi1, Data, 1, 1000);
}
接收函数
unsigned char Read_W5500_1Byte(unsigned short reg)
{
unsigned char i;
uint8_t pData[1]={0};
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS,GPIO_PIN_RESET);//置W5500的SCS为低电平
SPI1_Send_Short(reg);//通过SPI1写16位寄存器地址
SPI1_Send_Byte(FDM1|RWB_READ|COMMON_R);//通过SPI1写控制字节,1个字节数据长度,读数据,选择通用寄存器
HAL_SPI_Receive(&hspi1, pData, 1, 1000);
HAL_SPI_Transmit(&hspi1, 0x00, 1, 1000);
HAL_SPI_Receive(&hspi1, pData, 1, 1000);
i=pData[0];
HAL_GPIO_WritePin(W5500_SCS_PORT, W5500_SCS,GPIO_PIN_SET);
return i;//返回读取到的寄存器数据
}
程序下载进去,拼不通。
仿真:
卡在这条语句。
请问高手,如何解决呀?谢谢!
|