STM32CubeMX:SPI

本文介绍了STM32F103C8T6微控制器上使用SPI接口实现ENC28J60网络控制器驱动的方法,并详细展示了如何将UIP协议栈移植到该平台。文中提供了SPI配置说明以及原ENC28J60驱动程序的修改过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

芯片:STM32F103C8T6

应用管脚:

SPI1

实现ENC28J60驱动及UIP移植

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


SPI配置


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;	
}
测试使用情况




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值