STM32H723ZGT6移植LWIP协议栈
前言
本次使用最新的STM32H723芯片,在这块板子上移植LWIP协议栈,记录相关的操作步骤,以便后续进行学习和方便查看。
一、操作步骤
1.根据我们的底板原理图配置相关的RMII引脚
我们使用STM32CUBEIDE来进行配置相关的引脚。
2.使能数据缓存和指令缓存
3.打开LWIP的配置
然后配置地址
PHY芯片选择,这里只有一个选项,我们可以配置成默认的即可,然后在后面生成的代码中去修改一些参数即可。
同时需要配置MPU
配置完成之后在STM32H723ZGTX_FLASH.ld文件中添加这几行语句
.lwip_sec (NOLOAD):
{
. = ABSOLUTE(0X30000000);
*(.RxDecripSection)
. = ABSOLUTE(0X30000200);
*(.TxDecripSection)
. = ABSOLUTE(0X30004000);
*(.Rx_PoolSection)
} >RAM_D2
4.然后使能D2区域的时钟
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
__HAL_RCC_D2SRAM1_CLK_ENABLE();
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LWIP_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
MX_LWIP_Process();
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_0);
HAL_Delay(500);
}
/* USER CODE END 3 */
}
5.将PHY芯片进行复位
void MX_LWIP_Init(void)
{
/* IP addresses initialization */
IP_ADDRESS[0] = 192;
IP_ADDRESS[1] = 168;
IP_ADDRESS[2] = 13;
IP_ADDRESS[3] = 15;
NETMASK_ADDRESS[0] = 255;
NETMASK_ADDRESS[1] = 255;
NETMASK_ADDRESS[2] = 255;
NETMASK_ADDRESS[3] = 0;
GATEWAY_ADDRESS[0] = 192;
GATEWAY_ADDRESS[1] = 168;
GATEWAY_ADDRESS[2] = 13;
GATEWAY_ADDRESS[3] = 1;
/* USER CODE BEGIN IP_ADDRESSES */
HAL_GPIO_WritePin(GPIOF,11,GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOF,11,GPIO_PIN_SET);
HAL_Delay(100);
/* USER CODE END IP_ADDRESSES */
/* Initilialize the LwIP stack without RTOS */
lwip_init();
/* IP addresses initialization without DHCP (IPv4) */
IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
/* add the network interface (IPv4/IPv6) without RTOS */
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
/* Registers the default network interface */
netif_set_default(&gnetif);
if (netif_is_link_up(&gnetif))
{
/* When the netif is fully configured this function must be called */
netif_set_up(&gnetif);
}
else
{
/* When the netif link is down this function must be called */
netif_set_down(&gnetif);
}
/* Set the link callback function, this function is called on change of link status*/
netif_set_link_callback(&gnetif, ethernet_link_status_updated);
/* Create the Ethernet link handler thread */
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
}
6.修改PHY芯片的地址
代码如下(示例):
int32_t LAN8742_Init(lan8742_Object_t *pObj)
{
uint32_t regvalue = 0, addr = 0;
int32_t status = LAN8742_STATUS_OK;
if(pObj->Is_Initialized == 0)
{
if(pObj->IO.Init != 0)
{
/* GPIO and Clocks initialization */
pObj->IO.Init();
}
/* for later check */
pObj->DevAddr = LAN8742_MAX_DEV_ADDR + 1;
/* Get the device address from special mode register */
for(addr = 0; addr <= LAN8742_MAX_DEV_ADDR; addr ++)
{
if(pObj->IO.ReadReg(addr, LAN8742_SMR, ®value) < 0)
{
status = LAN8742_STATUS_READ_ERROR;
/* Can't read from this device address
continue with next address */
continue;
}
if((regvalue & LAN8742_SMR_PHY_ADDR) == addr)
{
pObj->DevAddr = addr;
status = LAN8742_STATUS_OK;
break;
}
}
if(pObj->DevAddr > LAN8742_MAX_DEV_ADDR)
{
status = LAN8742_STATUS_ADDRESS_ERROR;
}
/* if device address is matched */
if(status == LAN8742_STATUS_OK)
{
pObj->Is_Initialized = 1;
}
}
return status;
}
7.生成代码和编译代码
8.连接网线验证能够正常ping通数据
总结
本次通过STM32官方提供的STM32CUBEIDE来进行配置工程和生成代码,将LWIP协议栈成功融入
工程代码中。
作者:学习和思考