ETH 设置
注意要手动改GPIO的速度
LWIP设置
内核设置
初始化代码
放在main初始化中
SCB_CleanInvalidateDCache();
HAL_GPIO_WritePin(ETH_RST_GPIO_Port,ETH_RST_Pin,GPIO_PIN_RESET);
HAL_Delay(50);
HAL_GPIO_WritePin(ETH_RST_GPIO_Port,ETH_RST_Pin,GPIO_PIN_SET);
HAL_Delay(50);
任务中
MX_LWIP_Init();
初始化
void udp_connect(void)
{
err_t err;
/* Create a new UDP control block */
g_upcb = udp_new();
if(g_upcb)
{
/* Bind the upcb to the UDP_PORT port */
/* Using IP_ADDR_ANY allow the upcb to be used by any local interface */
err = udp_bind(g_upcb, IP_ADDR_ANY, 6688);
if(err == ERR_OK)
{
/* Set a receive callback for the upcb */
udp_recv(g_upcb, udp_receive_callback, NULL);
}
else
{
udp_remove(g_upcb);
}
}
}
发送
void udp_echoclient_send(void)
{
struct pbuf* q;
char reply[] = "012345674567890123456=5=";
q = pbuf_alloc(PBUF_TRANSPORT, strlen(reply) + 1, PBUF_RAM);
if(!q)
{
printf("out of PBUF_RAM\n");
return;
}
memset(q->payload, 0, q->len);
memcpy(q->payload, reply, strlen(reply));
udp_sendto(g_upcb, q, &dist_addr, 6688);
pbuf_free(q);
}
接收
void udp_receive_callback(void* arg, struct udp_pcb* upcb, struct pbuf* p, const ip_addr_t* addr, u16_t port)
{
/* Tell the client that we have accepted it */
udp_sendto(upcb, p,addr,port);
/* Free the p buffer */
pbuf_free(p);
}
注意
- 要把Micrl LIB 选上,否则会一真 BKPT
并重写
int fputc(int ch, FILE* f)
{
return ch;
}