STM32CubeMX:SPI

芯片:STM32F103C8T6

应用管脚:

SPI1

实现ENC28J60驱动及UIP移植

配置界面,PA4为GPIO定义输出管脚

STM32CubeMX:SPI_第1张图片

SPI配置

STM32CubeMX:SPI_第2张图片

SPI提供3种接口方式,轮询、中断及DMA,本次采用轮询方式。

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) 
HAL_StatusTypeDef  HAL_SPI_TransmitReceive (SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) 
修改原ENC28J60驱动程序

原读函数程序

u8 ENC28J60_SPI_ReadWrite(u8 writedat)
{
	/* Loop while DR register in not emplty */
	while(SPI_I2S_GetFlagStatus(ENCSPIx,SPI_I2S_FLAG_TXE) == RESET);
	
	/* Send byte through the SPIx peripheral */
	SPI_I2S_SendData(ENCSPIx, writedat);
	
	/* Wait to receive a byte */
	while(SPI_I2S_GetFlagStatus(ENCSPIx, SPI_I2S_FLAG_RXNE) == RESET);
	
	/* Return the byte read from the SPI bus */
	return SPI_I2S_ReceiveData(ENCSPIx);	
}
修改后读函数程序

uint8_t ENC28J60_SPI_ReadWrite(uint8_t writedat)
{
	extern SPI_HandleTypeDef ENCSPIx;
	
	static uint8_t txdata,rxdata;
	
	txdata=writedat;
	
	if(HAL_SPI_TransmitReceive(&ENCSPIx,&txdata,&rxdata,1,ENCSPIx_Timout)!= HAL_OK)
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
	}
	
	return rxdata;	
}
测试使用情况
STM32CubeMX:SPI_第3张图片



你可能感兴趣的:(STM32CubeMX)