Before Width: | Height: | Size: 205 KiB After Width: | Height: | Size: 27 KiB |
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 134 KiB After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 249 KiB After Width: | Height: | Size: 31 KiB |
@ -1,327 +0,0 @@ |
|||
/*
|
|||
* Copyright : (C) 2022 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: lwip_start.c |
|||
* Date: 2022-11-07 10:16:55 |
|||
* LastEditTime: 2022-12-06 11:36:50 |
|||
* Description: This file is for initiailizing a netif.The ip and other infomation can be set manually. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ------ -------- -------------------------------------- |
|||
* 1.0 liuzhihong 2022/11/15 first release |
|||
* 1.1 liuzhihong 2022/1/9 add functiuon calls in LwipTestLoop:LinkDetectLoop(netif) |
|||
*/ |
|||
|
|||
|
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include "strto.h" |
|||
#include "sdkconfig.h" |
|||
#include "ftypes.h" |
|||
#include "fassert.h" |
|||
#include "fparameters.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#error "Please include sdkconfig.h first" |
|||
#endif |
|||
|
|||
|
|||
#include "lwipopts.h" |
|||
#include "lwip_port.h" |
|||
#include "lwip/ip4_addr.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/ethernet.h" |
|||
#include "lwip/netif.h" |
|||
#include "lwip/tcpip.h" |
|||
#include "lwip/inet.h" |
|||
#include "shell_port.h" |
|||
|
|||
#if LWIP_IPV6 |
|||
#include "lwip/ip.h" |
|||
#include "lwip/ip6_addr.h" |
|||
#else |
|||
#if LWIP_DHCP |
|||
#include "lwip/dhcp.h" |
|||
#endif |
|||
#endif |
|||
|
|||
typedef struct |
|||
{ |
|||
const char *ipaddr; |
|||
const char *gateway; |
|||
const char *netmask; |
|||
} InputAddress; |
|||
|
|||
typedef struct |
|||
{ |
|||
UserConfig lwip_mac_config; |
|||
InputAddress input_address; |
|||
u32 dhcp_en; |
|||
} InputConfig; |
|||
|
|||
InputConfig input_config = {0}; |
|||
|
|||
|
|||
static boolean init_flag = FALSE; |
|||
|
|||
|
|||
void LwipTestCreate(void *args) |
|||
{ |
|||
FASSERT(args != NULL); |
|||
struct netif *netif_p = NULL; |
|||
|
|||
InputConfig *input_conf = (InputConfig *)args; |
|||
ip_addr_t ipaddr = {0}, netmask = {0}, gw = {0}; |
|||
|
|||
/* the mac address of the board. this should be unique per board */ |
|||
unsigned char mac_address[6] = |
|||
{0x98, 0x0e, 0x24, 0x00, 0x11, 0}; |
|||
|
|||
netif_p = malloc(sizeof(struct netif)); |
|||
if (netif_p == NULL) |
|||
{ |
|||
printf("Malloc netif is error.\r\n"); |
|||
goto exit; |
|||
} |
|||
printf("netif_p is %p \r\n", netif_p); |
|||
mac_address[5] = input_conf->lwip_mac_config.mac_instance; |
|||
|
|||
/* convert string to a binary address */ |
|||
if (input_conf->input_address.ipaddr) |
|||
{ |
|||
if (inet_aton(input_conf->input_address.ipaddr, &ipaddr) == 0) |
|||
{ |
|||
goto failed; |
|||
} |
|||
} |
|||
|
|||
if (input_conf->input_address.gateway) |
|||
{ |
|||
if (inet_aton(input_conf->input_address.gateway, &gw) == 0) |
|||
{ |
|||
goto failed; |
|||
} |
|||
} |
|||
|
|||
if (input_conf->input_address.netmask) |
|||
{ |
|||
if (inet_aton(input_conf->input_address.netmask, &netmask) == 0) |
|||
{ |
|||
goto failed; |
|||
} |
|||
} |
|||
|
|||
/* 初始化LwIP堆 */ |
|||
if (init_flag == FALSE) |
|||
{ |
|||
lwip_init(); /* lwip only init once */ |
|||
init_flag = TRUE; |
|||
} |
|||
|
|||
/* Add network interface to the netif_list, and set it as default */ |
|||
if (!LwipPortAdd(netif_p, &ipaddr, &netmask, &gw, mac_address, (UserConfig *)args)) |
|||
{ |
|||
printf("Error adding N/W interface\n\r"); |
|||
return ; |
|||
} |
|||
printf("LwipPortAdd is over \n\r"); |
|||
|
|||
#if LWIP_IPV6 |
|||
netif_p->ip6_autoconfig_enabled = 1; |
|||
netif_create_ip6_linklocal_address(netif_p, 1); |
|||
netif_ip6_addr_set_state(netif_p, 0, IP6_ADDR_VALID); |
|||
#endif |
|||
|
|||
netif_set_default(netif_p); |
|||
|
|||
if (netif_is_link_up(netif_p)) |
|||
{ |
|||
/* 当netif完全配置好时,必须调用该函数 */ |
|||
netif_set_up(netif_p); |
|||
if (input_conf->dhcp_en == 1) |
|||
{ |
|||
LwipPortDhcpSet(netif_p, TRUE); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
/* 当netif链接关闭时,必须调用该函数 */ |
|||
netif_set_down(netif_p); |
|||
} |
|||
|
|||
printf("Network setup complete.\n"); |
|||
|
|||
goto exit ; |
|||
failed: |
|||
printf("Failed \r\n"); |
|||
free(netif_p); |
|||
exit: |
|||
|
|||
return; |
|||
} |
|||
|
|||
void LwipTestInint(void *args) |
|||
{ |
|||
LwipTestCreate(args); |
|||
} |
|||
|
|||
|
|||
void LwipTestLoop(void) |
|||
{ |
|||
struct netif *netif; |
|||
if (init_flag == FALSE) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
netif = netif_list; |
|||
|
|||
while (netif != NULL) |
|||
{ |
|||
if (netif->state) |
|||
{ |
|||
|
|||
LwipPortInput(netif); |
|||
LinkDetectLoop(netif); |
|||
} |
|||
netif = netif->next; |
|||
} |
|||
} |
|||
|
|||
void LwipTestDhcpLoop(u32 msec) |
|||
{ |
|||
#if (LWIP_DHCP==1) |
|||
LwipPortDhcpLoop(msec); |
|||
#endif |
|||
} |
|||
|
|||
static int LwipDeviceSet(int argc, char *argv[]) |
|||
{ |
|||
u32 id = 0, interface_type = 0, driver_type = 0; |
|||
memset(&input_config, 0, sizeof(input_config)); |
|||
LWIP_PORT_CONFIG_DEFAULT_INIT(input_config.lwip_mac_config); |
|||
|
|||
if (!strcmp(argv[1], "probe")) |
|||
{ |
|||
if (argc < 6) |
|||
{ |
|||
printf("Input error: Too few parameters!\n"); |
|||
printf("All parameters will be set to 0!\n"); |
|||
} |
|||
else |
|||
{ |
|||
driver_type = (u32)simple_strtoul(argv[2], NULL, 10); |
|||
id = (u32)simple_strtoul(argv[3], NULL, 10); |
|||
interface_type = (u32)simple_strtoul(argv[4], NULL, 10); |
|||
input_config.dhcp_en = (u32)simple_strtoul(argv[5], NULL, 10); |
|||
if (input_config.dhcp_en == 0) |
|||
{ |
|||
if (argc == 9) |
|||
{ |
|||
input_config.input_address.ipaddr = argv[6]; |
|||
input_config.input_address.gateway = argv[7]; |
|||
input_config.input_address.netmask = argv[8]; |
|||
} |
|||
else |
|||
{ |
|||
printf("Input error: Missing parameters!\n"); |
|||
printf("All Ip address will be set to 0!\n"); |
|||
} |
|||
|
|||
} |
|||
else |
|||
{ |
|||
if (argc == 9) |
|||
{ |
|||
input_config.input_address.ipaddr = argv[6]; |
|||
input_config.input_address.gateway = argv[7]; |
|||
input_config.input_address.netmask = argv[8]; |
|||
} |
|||
|
|||
printf("Dhcp Open: All IP addresses will be determined by the dhcp server!\n"); |
|||
|
|||
|
|||
} |
|||
} |
|||
|
|||
input_config.lwip_mac_config.mac_instance = id; |
|||
input_config.lwip_mac_config.name[0] = 'e'; |
|||
itoa(id, &input_config.lwip_mac_config.name[1], 10); |
|||
|
|||
const char *name = input_config.lwip_mac_config.name; |
|||
if (LwipPortGetByName(name)) |
|||
{ |
|||
printf("Netif alread exist! \r\n"); |
|||
return -1; |
|||
} |
|||
|
|||
|
|||
if (interface_type == 0) |
|||
{ |
|||
input_config.lwip_mac_config.mii_interface = LWIP_PORT_INTERFACE_RGMII; |
|||
} |
|||
else |
|||
{ |
|||
input_config.lwip_mac_config.mii_interface = LWIP_PORT_INTERFACE_SGMII; |
|||
} |
|||
if (driver_type == 0) |
|||
{ |
|||
input_config.lwip_mac_config.driver_type = LWIP_PORT_TYPE_XMAC; |
|||
} |
|||
else |
|||
{ |
|||
input_config.lwip_mac_config.driver_type = LWIP_PORT_TYPE_GMAC; |
|||
} |
|||
|
|||
LwipTestInint(&input_config); |
|||
} |
|||
else if (!strcmp(argv[1], "deinit")) |
|||
{ |
|||
if (argc <= 1) |
|||
{ |
|||
printf("Please enter lwip deinit <name>\r\n") ; |
|||
printf(" -- use name to deinit neitf object\r\n"); |
|||
printf(" -- <name> is netif name\r\n"); |
|||
return -1; |
|||
} |
|||
struct netif *netif_p = NULL; |
|||
netif_p = LwipPortGetByName(argv[2]); |
|||
if (netif_p == NULL) |
|||
{ |
|||
printf("netif %s is not invalid.\r\n", argv[2]); |
|||
return -1; |
|||
} |
|||
|
|||
/* close netif */ |
|||
LwipPortStop(netif_p,input_config.dhcp_en); |
|||
free(netif_p); |
|||
} |
|||
else |
|||
{ |
|||
printf("Please enter lwip probe <dirver id> <device id> <interface id> <dhcp_en> <ipaddr> <gateway> <netmask> \r\n") ; |
|||
printf(" -- driver id is driver type set, 0 is xmac ,1 is gmac \r\n"); |
|||
printf(" -- device id is mac instance number \r\n"); |
|||
printf(" -- interface id is media independent interface , 0 is rgmii ,1 is sgmii \r\n"); |
|||
printf(" -- dhcp_en is dhcp function set ,1 is enable ,0 is disable .But this depends on whether the protocol stack supports it "); |
|||
printf(" -- <ipaddr> Ip address of netif \r\n"); |
|||
printf(" -- <gateway> Gateway of netif \r\n"); |
|||
printf(" -- <netmask> Netmask of netif \r\n"); |
|||
printf("Please enter lwip deinit <name> \r\n") ; |
|||
printf(" -- use name to deinit neitf object \r\n"); |
|||
printf(" -- <name> is netif name \r\n"); |
|||
} |
|||
return 0; |
|||
} |
|||
|
|||
SHELL_EXPORT_CMD(SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), lwip, LwipDeviceSet, Setup LWIP device test); |
|||
|
@ -1,102 +0,0 @@ |
|||
/*
|
|||
* Copyright : (C) 2022 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: raw_api_timer.c |
|||
* Date: 2022-11-01 15:52:09 |
|||
* LastEditTime: 2022-11-01 15:52:10 |
|||
* Description: This file is for create a timer.The timer can keep the netif receiveing message periodically. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ------ -------- -------------------------------------- |
|||
* 1.0 liuzhihong 2022/11/15 first release |
|||
* 1.1 liuzhihong 2022/11/23 add func sys_now() |
|||
* 1.2 liuzhihong 2022/1/9 u64 -> u32 |
|||
*/ |
|||
|
|||
#include "fgeneric_timer.h" |
|||
#include "fparameters.h" |
|||
#include "finterrupt.h" |
|||
#include "fassert.h" |
|||
#include "shell_port.h" |
|||
#include "timeouts.h" |
|||
|
|||
#define TIMER_BASE_RATE_HZ 1000 |
|||
#define TIMER_INTERRUPT_PRO IRQ_PRIORITY_VALUE_3 |
|||
|
|||
static u32 timer_base_cnt = 0; |
|||
extern void LwipTestLoop(void); |
|||
extern void LwipTestDhcpLoop(u32 msec); |
|||
|
|||
static void GenericTimerIntrHandler(s32 vector, void *param) |
|||
{ |
|||
(void)vector; |
|||
FASSERT(param != 0); |
|||
u32 timer_base_freq = (u32)(uintptr)param; |
|||
timer_base_cnt++; |
|||
GenericTimerSetTimerValue(GENERIC_TIMER_ID0, GenericTimerFrequecy() / timer_base_freq);/* clear tick interrupt */ |
|||
} |
|||
|
|||
void TimerLoopInit(void) |
|||
{ |
|||
u32 cnt_frq; |
|||
timer_base_cnt = 0; |
|||
/* disable timer and get system frequency */ |
|||
GenericTimerStop(GENERIC_TIMER_ID0); |
|||
cnt_frq = GenericTimerFrequecy(); |
|||
|
|||
/* set tick rate */ |
|||
GenericTimerSetTimerValue(GENERIC_TIMER_ID0, cnt_frq / TIMER_BASE_RATE_HZ); |
|||
GenericTimerInterruptEnable(GENERIC_TIMER_ID0); |
|||
|
|||
/* set generic timer interrupt */ |
|||
InterruptSetPriority(GENERIC_TIMER_NS_IRQ_NUM, TIMER_INTERRUPT_PRO); |
|||
|
|||
/* install tick handler */ |
|||
InterruptInstall(GENERIC_TIMER_NS_IRQ_NUM, GenericTimerIntrHandler, |
|||
(void *)TIMER_BASE_RATE_HZ, "GenericTimerTick"); |
|||
|
|||
/* enable interrupt */ |
|||
InterruptUmask(GENERIC_TIMER_NS_IRQ_NUM); |
|||
GenericTimerStart(GENERIC_TIMER_ID0); |
|||
} |
|||
|
|||
|
|||
void TimerLoop(void) |
|||
{ |
|||
static u32 _5ms_appear = 0; |
|||
TimerLoopInit(); |
|||
LSUserShellInit(); |
|||
|
|||
while (1) |
|||
{ |
|||
|
|||
LSuserShellNoWaitLoop(); |
|||
LwipTestLoop(); |
|||
|
|||
if (((timer_base_cnt % 5) == 0) && (_5ms_appear == 0)) /*5ms task */ |
|||
{ |
|||
_5ms_appear = 1; |
|||
LwipTestDhcpLoop(5); |
|||
} |
|||
else if ((timer_base_cnt % 5) != 0) |
|||
{ |
|||
_5ms_appear = 0; |
|||
} |
|||
sys_check_timeouts(); |
|||
} |
|||
} |
|||
u32_t sys_now(void) |
|||
{ |
|||
return timer_base_cnt; |
|||
} |
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 34 KiB |
Before Width: | Height: | Size: 3.3 KiB |
@ -0,0 +1,57 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: lwip_timer.h |
|||
* Created Date: 2024-06-06 11:28:12 |
|||
* Last Modified: 2024-06-12 17:38:26 |
|||
* Description: This file is for lwip timer function definition. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/06 first release |
|||
*/ |
|||
#ifndef LWIP_TIMER_H |
|||
#define LWIP_TIMER_H |
|||
|
|||
/***************************** Include Files *********************************/ |
|||
#include "ftypes.h" |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" |
|||
{ |
|||
#endif |
|||
|
|||
/************************** Constant Definitions *****************************/ |
|||
|
|||
/**************************** Type Definitions *******************************/ |
|||
|
|||
/************************** Variable Definitions *****************************/ |
|||
|
|||
/***************** Macros (Inline Functions) Definitions *********************/ |
|||
|
|||
/************************** Function Prototypes ******************************/ |
|||
/* entry function for lwip timer example */ |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
void TimerLoop(void); |
|||
#else |
|||
void TimerStaticInit(void); |
|||
void TimerStaticLoop(u32 time); |
|||
#endif |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif |
@ -0,0 +1,51 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: tcp_client_example.h |
|||
* Created Date: 2024-06-06 11:28:27 |
|||
* Last Modified: 2024-06-12 17:34:42 |
|||
* Description: This file is for tcp client example function definition. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/06 first release |
|||
*/ |
|||
#ifndef TCP_CLIENT_EXAMPLE_H |
|||
#define TCP_CLIENT_EXAMPLE_H |
|||
|
|||
/***************************** Include Files *********************************/ |
|||
#include "ftypes.h" |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" |
|||
{ |
|||
#endif |
|||
|
|||
/************************** Constant Definitions *****************************/ |
|||
|
|||
/**************************** Type Definitions *******************************/ |
|||
|
|||
/************************** Variable Definitions *****************************/ |
|||
|
|||
/***************** Macros (Inline Functions) Definitions *********************/ |
|||
|
|||
/************************** Function Prototypes ******************************/ |
|||
/* entry function for tcp server example */ |
|||
int TcpClientCreate(void); |
|||
void TcpClientDeinit(void); |
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif |
@ -1,39 +1,64 @@ |
|||
/*
|
|||
* Copyright : (C) 2022 Phytium Information Technology, Inc. |
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* |
|||
* |
|||
* FilePath: main.c |
|||
* Date: 2022-02-10 14:53:41 |
|||
* LastEditTime: 2022-02-17 17:38:03 |
|||
* Description: This file is for creating a main loop from which the programme will start. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ------ -------- -------------------------------------- |
|||
* 1.0 liuzhihong 2022/11/23 first release |
|||
* Created Date: 2024-05-30 19:05:18 |
|||
* Last Modified: 2024-06-12 17:55:22 |
|||
* Description: This file is for tcp client example main functions. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/03 first release |
|||
*/ |
|||
|
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
|
|||
#include "sdkconfig.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#warning "Please include sdkconfig.h" |
|||
#endif |
|||
#ifndef CONFIG_USE_LETTER_SHELL |
|||
#error "Please include letter shell first!!!" |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
#include "shell_port.h" |
|||
#endif |
|||
extern void TimerLoop(void); |
|||
|
|||
#include "tcp_client_example.h" |
|||
#include "lwip_timer.h" |
|||
|
|||
/************************** Constant Definitions *****************************/ |
|||
|
|||
/**************************** Type Definitions *******************************/ |
|||
|
|||
/************************** Variable Definitions *****************************/ |
|||
|
|||
/***************** Macros (Inline Functions) Definitions *********************/ |
|||
|
|||
/************************** Function Prototypes ******************************/ |
|||
|
|||
/************************** Function *****************************************/ |
|||
|
|||
int main() |
|||
{ |
|||
TimerLoop(); |
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
/* if shell command is enabled, register example entry as shell command */ |
|||
TimerLoop(); |
|||
#else |
|||
TimerStaticInit(); |
|||
|
|||
TimerStaticLoop(2); |
|||
TcpClientCreate(); |
|||
TimerStaticLoop(10); |
|||
TcpClientDeinit(); |
|||
#endif |
|||
return 0; |
|||
} |
@ -0,0 +1,68 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: cmd_tcp_client.c |
|||
* Created Date: 2024-06-06 11:30:42 |
|||
* Last Modified: 2024-06-12 17:34:52 |
|||
* Description: This file is for tcp client example cmd catalogue. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/06 first release |
|||
*/ |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
|
|||
#include "sdkconfig.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#warning "Please include sdkconfig.h" |
|||
#endif |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
#include "shell.h" |
|||
#include "strto.h" |
|||
|
|||
#include "tcp_client_example.h" |
|||
|
|||
#define EXAMPLE_IDLE 0 |
|||
#define TCP_CLIENT_EXAMPLE_RUNNING 1 |
|||
|
|||
static u32 init_flag_mask=EXAMPLE_IDLE; |
|||
|
|||
static void TcpClientExampleCheckState(void) |
|||
{ |
|||
switch(init_flag_mask) |
|||
{ |
|||
case TCP_CLIENT_EXAMPLE_RUNNING: |
|||
printf("Tcp client example is running, we need to deinitialize it first! \r\n"); |
|||
TcpClientDeinit(); |
|||
init_flag_mask=EXAMPLE_IDLE; |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
/* entry function for tcp client example */ |
|||
static int TcpClientExampleEntry(int argc, char *argv[]) |
|||
{ |
|||
int ret = 0; |
|||
TcpClientExampleCheckState(); |
|||
ret = TcpClientCreate(); |
|||
init_flag_mask = TCP_CLIENT_EXAMPLE_RUNNING; |
|||
return ret; |
|||
} |
|||
|
|||
/* register command for tcp client example */ |
|||
SHELL_EXPORT_CMD(SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), tcpclient, TcpClientExampleEntry, Start Tcp client); |
|||
#endif |
@ -0,0 +1,219 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: lwip_timer.c |
|||
* Created Date: 2024-06-06 11:28:36 |
|||
* Last Modified: 2024-06-12 17:35:00 |
|||
* Description: This file is for lwip timer function implementation. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/06 first release |
|||
*/ |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include "strto.h" |
|||
#include "fgeneric_timer.h" |
|||
#include "ftypes.h" |
|||
#include "fparameters.h" |
|||
#include "finterrupt.h" |
|||
#include "fassert.h" |
|||
#include "timeouts.h" |
|||
|
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
#include "shell_port.h" |
|||
#endif |
|||
|
|||
#include "sdkconfig.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#error "Please include sdkconfig.h first" |
|||
#endif |
|||
|
|||
#include "lwip_port.h" |
|||
#include "lwip/ip4_addr.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/ethernet.h" |
|||
#include "lwip/netif.h" |
|||
#include "lwip/tcpip.h" |
|||
#include "lwip/inet.h" |
|||
#include "lwip/dhcp.h" |
|||
#include "ifconfig.h" |
|||
|
|||
#define TIMER_BASE_RATE_HZ 1000 /* 为timer_base_cnt 提供time base */ |
|||
#define TIMER_INTERRUPT_PRO IRQ_PRIORITY_VALUE_3 |
|||
|
|||
#ifdef CONFIG_ARCH_ARMV8_AARCH64 |
|||
#define MAX_TIMER_CNT 0xffffffffffffffff |
|||
#else |
|||
#define MAX_TIMER_CNT 0xffffffff |
|||
#endif |
|||
|
|||
static u32 timer_base_cnt = 0; |
|||
|
|||
|
|||
void LwipTestLoop(void) |
|||
{ |
|||
struct netif *netif; |
|||
|
|||
netif = netif_list; |
|||
|
|||
while (netif != NULL) |
|||
{ |
|||
if (netif->state) |
|||
{ |
|||
|
|||
LwipPortInput(netif); |
|||
LinkDetectLoop(netif); |
|||
} |
|||
netif = netif->next; |
|||
} |
|||
} |
|||
|
|||
void LwipTestDhcpLoop(u32 msec) |
|||
{ |
|||
LwipPortDhcpLoop(msec); |
|||
} |
|||
|
|||
static void GenericTimerIntrHandler(s32 vector, void *param) |
|||
{ |
|||
(void)vector; |
|||
FASSERT(param != 0); |
|||
u32 timer_base_freq = (u32)(uintptr)param; |
|||
timer_base_cnt++; |
|||
/* clear tick interrupt */ |
|||
GenericTimerSetTimerValue(GENERIC_TIMER_ID0, GenericTimerFrequecy() / timer_base_freq); |
|||
} |
|||
|
|||
void TimerLoopInit(void) |
|||
{ |
|||
u32 cnt_frq; |
|||
timer_base_cnt = 0; |
|||
/* disable timer and get system frequency */ |
|||
GenericTimerStop(GENERIC_TIMER_ID0); |
|||
cnt_frq = GenericTimerFrequecy(); |
|||
|
|||
/* set tick rate */ |
|||
GenericTimerSetTimerValue(GENERIC_TIMER_ID0, cnt_frq / TIMER_BASE_RATE_HZ); |
|||
GenericTimerInterruptEnable(GENERIC_TIMER_ID0); |
|||
|
|||
/* set generic timer interrupt */ |
|||
InterruptSetPriority(GENERIC_TIMER_NS_IRQ_NUM, TIMER_INTERRUPT_PRO); |
|||
|
|||
/* install tick handler */ |
|||
InterruptInstall(GENERIC_TIMER_NS_IRQ_NUM, GenericTimerIntrHandler, |
|||
(void *)TIMER_BASE_RATE_HZ, "GenericTimerTick"); |
|||
|
|||
/* enable interrupt */ |
|||
InterruptUmask(GENERIC_TIMER_NS_IRQ_NUM); |
|||
GenericTimerStart(GENERIC_TIMER_ID0); |
|||
} |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
void TimerLoop(void) |
|||
{ |
|||
u32 _5ms_appear = 0; |
|||
u32 base_cnt_back = 0; |
|||
TimerLoopInit(); |
|||
LSUserShellInit(); |
|||
|
|||
lwip_init(); /* lwip only init once */ |
|||
|
|||
while (1) |
|||
{ |
|||
LSuserShellNoWaitLoop(); |
|||
LwipTestLoop(); |
|||
|
|||
if (((timer_base_cnt % 5) == 0) && (_5ms_appear == 0)) /*5ms task */ |
|||
{ |
|||
_5ms_appear = 1; |
|||
LwipTestDhcpLoop(5); |
|||
} |
|||
else if ((timer_base_cnt % 5) != 0) |
|||
{ |
|||
_5ms_appear = 0; |
|||
} |
|||
if(timer_base_cnt != base_cnt_back) |
|||
{ |
|||
sys_check_timeouts(); |
|||
base_cnt_back = timer_base_cnt; |
|||
} |
|||
} |
|||
} |
|||
#else |
|||
void TimerStaticInit(void) |
|||
{ |
|||
TimerLoopInit(); |
|||
lwip_init(); /* lwip only init once */ |
|||
} |
|||
|
|||
/**
|
|||
* @name: TimerStaticLoop |
|||
* @msg: 在指定的time时间内进行网卡数据收发 |
|||
* @return void |
|||
* @note: |
|||
* @param {u32} time |
|||
*/ |
|||
void TimerStaticLoop(u32 time) |
|||
{ |
|||
u32 _5ms_appear = 0; |
|||
u32 base_cnt_back = 0; |
|||
u32 timer_start=0; |
|||
u32 time_pass_cnt=0; |
|||
u32 time_cnt; |
|||
u32 old = 0; |
|||
|
|||
time_cnt = time * GenericTimerFrequecy(); |
|||
timer_start = GenericTimerRead(GENERIC_TIMER_ID0); |
|||
while (time_pass_cnt<time_cnt) |
|||
{ |
|||
LwipTestLoop(); |
|||
|
|||
if (((timer_base_cnt % 5) == 0) && (_5ms_appear == 0)) /*5ms task */ |
|||
{ |
|||
_5ms_appear = 1; |
|||
LwipTestDhcpLoop(5); |
|||
} |
|||
else if ((timer_base_cnt % 5) != 0) |
|||
{ |
|||
_5ms_appear = 0; |
|||
} |
|||
if(timer_base_cnt != base_cnt_back) |
|||
{ |
|||
sys_check_timeouts(); |
|||
base_cnt_back = timer_base_cnt; |
|||
} |
|||
|
|||
|
|||
if(GenericTimerRead(GENERIC_TIMER_ID0)>timer_start) |
|||
time_pass_cnt = GenericTimerRead(GENERIC_TIMER_ID0)-timer_start; |
|||
else |
|||
time_pass_cnt = GenericTimerRead(GENERIC_TIMER_ID0)+(MAX_TIMER_CNT-timer_start); |
|||
|
|||
if(old != time-time_pass_cnt/GenericTimerFrequecy()) |
|||
{ |
|||
printf("Only left %d seconds\r\n", time-time_pass_cnt/GenericTimerFrequecy()); |
|||
old = time-time_pass_cnt/GenericTimerFrequecy(); |
|||
} |
|||
} |
|||
|
|||
ListIf(); |
|||
|
|||
} |
|||
#endif |
|||
|
|||
u32_t sys_now(void) |
|||
{ |
|||
return timer_base_cnt; |
|||
} |
@ -1,131 +0,0 @@ |
|||
/*
|
|||
* Copyright : (C) 2023 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: tcp_client.c |
|||
* Created Date: 2022-11-07 10:16:55 |
|||
* Last Modified: 2023-06-07 11:00:17 |
|||
* Description: This file is for creating a tcp client.By Tcp Connection,The client can send a tcp packet to the destination host periodically. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2022/11/23 first release |
|||
* 1.1 liuzhihong 2023/06/06 memory double free solved |
|||
*/ |
|||
#include "lwip/netif.h" |
|||
#include "lwip/ip.h" |
|||
#include "lwip/tcp.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/etharp.h" |
|||
#include "lwip/udp.h" |
|||
#include "lwip/pbuf.h" |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
#include "shell.h" |
|||
|
|||
#define TCP_CLIENT_PORT 0 |
|||
#define TCP_SERVER_PORT 5001 |
|||
void TcpClientInit(void); |
|||
static struct tcp_pcb *client_pcb = NULL; |
|||
int init_flag=0; |
|||
static void client_err(void *arg, err_t err) |
|||
{ |
|||
/*connect failed, free client_pcb*/ |
|||
printf("Connect error! PCB Closed by Core!\n"); |
|||
if(client_pcb->state == SYN_SENT) goto exit; |
|||
|
|||
tcp_close(client_pcb); |
|||
|
|||
exit: |
|||
init_flag=0; |
|||
printf("Please input command tcp_client to connect to server again!\n"); |
|||
} |
|||
static err_t client_send(void *arg, struct tcp_pcb *tpcb) |
|||
{ |
|||
uint8_t send_buf[] = "This is a data from TCP Client.\n"; |
|||
printf("Writing data to Send buffer \n"); |
|||
return tcp_write(tpcb, send_buf, sizeof(send_buf), 1);; |
|||
} |
|||
static err_t client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) |
|||
{ |
|||
err_t ret = ERR_OK; |
|||
if (p != NULL) |
|||
{ |
|||
/*update the tcp send windows*/ |
|||
tcp_recved(tpcb, p->tot_len); |
|||
printf("We get a data from server\n"); |
|||
memset(p->payload, 0, p->tot_len); |
|||
pbuf_free(p); |
|||
} |
|||
else if (err == ERR_OK) |
|||
{ |
|||
printf("Server has been Disconnected!\n"); |
|||
} |
|||
return ret; |
|||
} |
|||
static err_t client_connected(void *arg, struct tcp_pcb *pcb, err_t err) |
|||
{ |
|||
|
|||
printf("Tcp Client Connected To Server Success!\n"); |
|||
/*register a timer func the Cycle time is 10*500ms=5s */ |
|||
tcp_poll(pcb, client_send, 10); |
|||
/*register a receive func*/ |
|||
tcp_recv(pcb, client_recv); |
|||
|
|||
init_flag=1; |
|||
return ERR_OK; |
|||
} |
|||
|
|||
void TcpClientInit(void) |
|||
{ |
|||
|
|||
if(init_flag) |
|||
{ |
|||
printf("Tcp client has been opened, Please Close it first!\n"); |
|||
return ; |
|||
} |
|||
err_t ret; |
|||
ip_addr_t tmp_ip; |
|||
/* create a tcp_pcb */ |
|||
client_pcb = tcp_new(); |
|||
IP_ADDR4(&tmp_ip, 192, 168, 4, 10); |
|||
tcp_bind(client_pcb, &tmp_ip, TCP_CLIENT_PORT); |
|||
IP_ADDR4(&tmp_ip, 192, 168, 4, 50); |
|||
printf("**************** Client Start Connectting ****************\n"); |
|||
/*return immediately, call pcb->err if connect failed*/ |
|||
ret = tcp_connect(client_pcb, &tmp_ip, TCP_SERVER_PORT, client_connected); |
|||
if (ret != ERR_OK) |
|||
{ |
|||
printf("Memory is not enough! Connect Failed!\r\n"); |
|||
tcp_close(client_pcb); |
|||
init_flag=0; |
|||
return; |
|||
} |
|||
/*register a Exception Handling Functions*/ |
|||
tcp_err(client_pcb, client_err); |
|||
} |
|||
void TcpClientDeinit(void) |
|||
{ |
|||
if (init_flag) |
|||
{ |
|||
printf("We are Closing Tcp Client!\n"); |
|||
tcp_close(client_pcb); |
|||
init_flag=0; |
|||
} |
|||
else |
|||
{ |
|||
printf("There is no alive client ,Please Open Client first!\n"); |
|||
} |
|||
} |
|||
SHELL_EXPORT_CMD(SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), tcpclient, TcpClientInit, Start Tcp client test); |
|||
SHELL_EXPORT_CMD(SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), tcpclientclose, TcpClientDeinit, Close Tcp client); |
@ -0,0 +1,291 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: tcp_client_example.c |
|||
* Created Date: 2024-05-30 19:05:18 |
|||
* Last Modified: 2024-06-12 17:35:07 |
|||
* Description: This file is for tcp client example function implementation. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/06 first release |
|||
*/ |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include "strto.h" |
|||
#include "sdkconfig.h" |
|||
#include "ftypes.h" |
|||
#include "fassert.h" |
|||
#include "fparameters.h" |
|||
#include "eth_board.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#error "Please include sdkconfig.h first" |
|||
#endif |
|||
|
|||
#include "lwip_port.h" |
|||
#include "lwip/ip4_addr.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/ethernet.h" |
|||
#include "lwip/netif.h" |
|||
#include "lwip/tcpip.h" |
|||
#include "lwip/inet.h" |
|||
#include "lwip/tcp.h" |
|||
|
|||
#define ETH_NAME_PREFIX 'e' |
|||
|
|||
#define CONFIG_DEFAULT_INIT(config,driver_config,instance_id,interface_type) \ |
|||
.config.magic_code = LWIP_PORT_CONFIG_MAGIC_CODE, \ |
|||
.config.driver_type = driver_config, \ |
|||
.config.mac_instance = instance_id, \ |
|||
.config.mii_interface = interface_type, \ |
|||
.config.autonegotiation = 1, \ |
|||
.config.phy_speed = LWIP_PORT_SPEED_1000M, \ |
|||
.config.phy_duplex = LWIP_PORT_FULL_DUPLEX, \ |
|||
.config.capability = LWIP_PORT_MODE_MULTICAST_ADDRESS_FILITER, |
|||
|
|||
#define TCP_CLIENT_PORT 5001 |
|||
#define TCP_SERVER_PORT 5001 |
|||
|
|||
typedef struct |
|||
{ |
|||
UserConfig lwip_mac_config; |
|||
u32 dhcp_en; |
|||
char* ipaddr; |
|||
char* netmask; |
|||
char* gw; |
|||
unsigned char mac_address[6]; |
|||
struct netif netif; |
|||
} BoardMacConfig; |
|||
|
|||
static BoardMacConfig board_mac_config[MAC_NUM] = |
|||
{ |
|||
#if defined(MAC_NUM0) |
|||
{ |
|||
CONFIG_DEFAULT_INIT(lwip_mac_config,MAC_NUM0_LWIP_PORT_TYPE,MAC_NUM0_CONTROLLER,MAC_NUM0_MII_INTERFACE) |
|||
.dhcp_en=0, |
|||
.ipaddr="192.168.4.10", |
|||
.gw="192.168.4.1", |
|||
.netmask="255.255.255.0", |
|||
.mac_address={0x98, 0x0e, 0x24, 0x00, 0x11, 0x0}, |
|||
}, |
|||
#endif |
|||
#if defined(MAC_NUM1) |
|||
{ |
|||
CONFIG_DEFAULT_INIT(lwip_mac_config,MAC_NUM1_LWIP_PORT_TYPE,MAC_NUM1_CONTROLLER,MAC_NUM1_MII_INTERFACE) |
|||
.dhcp_en=0, |
|||
.ipaddr="192.168.4.11", |
|||
.gw="192.168.4.1", |
|||
.netmask="255.255.255.0", |
|||
.mac_address={0x98, 0x0e, 0x24, 0x00, 0x11, 0x1}, |
|||
}, |
|||
#endif |
|||
}; |
|||
|
|||
static void SetIP(ip_addr_t* ipaddr,ip_addr_t* gw,ip_addr_t* netmask,u32 mac_id) |
|||
{ |
|||
|
|||
if(inet_aton(board_mac_config[mac_id].ipaddr,ipaddr)==0) |
|||
printf("The addr of ipaddr is wrong\r\n"); |
|||
if(inet_aton(board_mac_config[mac_id].gw,gw)==0) |
|||
printf("The addr of gw is wrong\r\n"); |
|||
if(inet_aton(board_mac_config[mac_id].netmask,netmask)==0) |
|||
printf("The addr of netmask is wrong\r\n"); |
|||
|
|||
} |
|||
|
|||
static err_t client_poll_callback(void *arg, struct tcp_pcb *tpcb) |
|||
{ |
|||
|
|||
err_t ret = ERR_OK; |
|||
if(tpcb->state == ESTABLISHED) |
|||
{ |
|||
if(arg == NULL) |
|||
{ |
|||
uint8_t send_buf[] = "This is a data from Tcp client!\n"; |
|||
printf("Writing data to Send buffer \n"); |
|||
ret = tcp_write(tpcb, send_buf, sizeof(send_buf), 1); |
|||
tcp_arg(tpcb,(void * )tpcb); |
|||
} |
|||
else |
|||
{ |
|||
printf("Sending data finish! We need to disconnect from the server now !\n"); |
|||
/*
|
|||
client send FIN packet to server |
|||
ESTABLISHED -> FIN_WAIT_1 |
|||
*/ |
|||
ret = tcp_close(tpcb); |
|||
} |
|||
} |
|||
else if(tpcb->state == CLOSE_WAIT) |
|||
{ |
|||
printf("The pcb is not in ESTABLISHED state ! Close it now!\n"); |
|||
/*
|
|||
client send FIN packet to server |
|||
CLOSE_WAIT -> LAST_ACK |
|||
*/ |
|||
ret = tcp_close(tpcb); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
static err_t client_recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) |
|||
{ |
|||
err_t ret = ERR_OK; |
|||
if (p != NULL) |
|||
{ |
|||
/*update the tcp send windows*/ |
|||
tcp_recved(tpcb, p->tot_len); |
|||
printf("We get a data from server\n"); |
|||
memset(p->payload, 0, p->tot_len); |
|||
pbuf_free(p); |
|||
} |
|||
else if (err == ERR_OK) |
|||
{ |
|||
/*
|
|||
client recived FIN packet from server,including two situations: |
|||
1. FIN_WAIT_2 -> TIME_WAIT |
|||
2. ESTABLISHED -> CLOSE_WAIT |
|||
|
|||
*/ |
|||
printf("The server disconnects from us !\n"); |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
static err_t client_connected_callback(void *arg, struct tcp_pcb *pcb, err_t err) |
|||
{ |
|||
|
|||
printf("Tcp Client Connected To Server Success!\n"); |
|||
/*register a timer func the Cycle time is 10*500ms=5s */ |
|||
tcp_poll(pcb, client_poll_callback,4); |
|||
/*register a receive func*/ |
|||
tcp_recv(pcb, client_recv_callback); |
|||
|
|||
return ERR_OK; |
|||
} |
|||
|
|||
static void client_err_callback(void *arg, err_t err) |
|||
{ |
|||
switch (err) |
|||
{ |
|||
case ERR_ABRT: |
|||
printf("Connect error! Error type is ERR_ABRT\n"); |
|||
break; |
|||
case ERR_RST: |
|||
printf("Connect error! Error type is ERR_RST\n"); |
|||
break; |
|||
case ERR_CLSD: |
|||
printf("Connect error! Error type is ERR_CLSD\n"); |
|||
break; |
|||
default: |
|||
FASSERT_MSG(0,"code can not reach here!\n"); |
|||
break; |
|||
} |
|||
} |
|||
|
|||
void TcpClient(void) |
|||
{ |
|||
err_t ret; |
|||
struct tcp_pcb *client_pcb; |
|||
ip_addr_t local_ip; |
|||
ip_addr_t remote_ip; |
|||
/* create a tcp_pcb */ |
|||
client_pcb = tcp_new(); |
|||
IP_ADDR4(&local_ip, 192, 168, 4, 10); |
|||
tcp_bind(client_pcb, &local_ip, TCP_CLIENT_PORT); |
|||
IP_ADDR4(&remote_ip, 192, 168, 4, 50); |
|||
printf("**************** Client Start Connectting ****************\n"); |
|||
/*return immediately, call pcb->err if connect failed*/ |
|||
ret = tcp_connect(client_pcb, &remote_ip, TCP_SERVER_PORT, client_connected_callback); |
|||
if (ret != ERR_OK) |
|||
{ |
|||
printf("Memory is not enough! Connect Failed!\r\n"); |
|||
tcp_close(client_pcb); |
|||
return; |
|||
} |
|||
/*register a Exception Handling Functions*/ |
|||
tcp_err(client_pcb, client_err_callback); |
|||
} |
|||
|
|||
int TcpClientCreate(void) |
|||
{ |
|||
FError ret = FT_SUCCESS; |
|||
/* mac init */ |
|||
for (int i = 0; i < MAC_NUM; i++) |
|||
{ |
|||
|
|||
struct netif *netif_p = NULL; |
|||
ip_addr_t ipaddr,netmask, gw; |
|||
board_mac_config[i].lwip_mac_config.name[0] = ETH_NAME_PREFIX; |
|||
itoa(board_mac_config[i].lwip_mac_config.mac_instance, &(board_mac_config[i].lwip_mac_config.name[1]), 10); |
|||
|
|||
/* mac ip addr set: char* -> ip_addr_t */ |
|||
SetIP(&ipaddr,&gw,&netmask,i); |
|||
/* ******************************************************************* */ |
|||
|
|||
netif_p= &board_mac_config[i].netif; |
|||
/* Add network interface to the netif_list, and set it as default */ |
|||
if (!LwipPortAdd(netif_p, &ipaddr, &netmask, &gw, board_mac_config[i].mac_address, (UserConfig *)&board_mac_config[i])) |
|||
{ |
|||
printf("Error adding N/W interface %d.\n\r",board_mac_config[i].lwip_mac_config.mac_instance); |
|||
ret = ERR_GENERAL; |
|||
goto exit; |
|||
} |
|||
printf("LwipPortAdd mac_instance %d is over.\n\r",board_mac_config[i].lwip_mac_config.mac_instance); |
|||
|
|||
netif_set_default(netif_p); |
|||
|
|||
if (netif_is_link_up(netif_p)) |
|||
{ |
|||
/* 当netif完全配置好时,必须调用该函数 */ |
|||
netif_set_up(netif_p); |
|||
if (board_mac_config[i].dhcp_en == 1) |
|||
{ |
|||
LwipPortDhcpSet(netif_p, TRUE); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
/* 当netif链接关闭时,必须调用该函数 */ |
|||
netif_set_down(netif_p); |
|||
} |
|||
|
|||
} |
|||
printf("Network setup complete.\n"); |
|||
|
|||
TcpClient(); |
|||
|
|||
exit: |
|||
if (ret == FT_SUCCESS) |
|||
{ |
|||
printf("%s@%d:Tcp client example [success].\r\n", __func__, __LINE__); |
|||
return 0; |
|||
} |
|||
else |
|||
{ |
|||
printf("%s@%d:Tcp client example [failure].\r\n", __func__, __LINE__); |
|||
return 1; |
|||
} |
|||
} |
|||
|
|||
void TcpClientDeinit(void) |
|||
{ |
|||
|
|||
for (int i = 0; i < MAC_NUM; i++) |
|||
{ |
|||
struct netif *netif_p = NULL; |
|||
netif_p=&board_mac_config[i].netif; |
|||
LwipPortStop(netif_p,board_mac_config[i].dhcp_en); |
|||
} |
|||
} |
Before Width: | Height: | Size: 31 KiB |
Before Width: | Height: | Size: 30 KiB |
Before Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 25 KiB After Width: | Height: | Size: 22 KiB |
Before Width: | Height: | Size: 5.1 KiB After Width: | Height: | Size: 9.3 KiB |
@ -0,0 +1,57 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: lwip_timer.c |
|||
* Created Date: 2024-06-05 18:42:52 |
|||
* Last Modified: 2024-06-12 17:35:26 |
|||
* Description: This file is for lwip timer function definition. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/05 first release |
|||
*/ |
|||
#ifndef LWIP_TIMER_H |
|||
#define LWIP_TIMER_H |
|||
|
|||
/***************************** Include Files *********************************/ |
|||
#include "ftypes.h" |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" |
|||
{ |
|||
#endif |
|||
|
|||
/************************** Constant Definitions *****************************/ |
|||
|
|||
/**************************** Type Definitions *******************************/ |
|||
|
|||
/************************** Variable Definitions *****************************/ |
|||
|
|||
/***************** Macros (Inline Functions) Definitions *********************/ |
|||
|
|||
/************************** Function Prototypes ******************************/ |
|||
/* entry function for lwip timer example */ |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
void TimerLoop(void); |
|||
#else |
|||
void TimerStaticInit(void); |
|||
void TimerStaticLoop(u32 time); |
|||
#endif |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif |
@ -0,0 +1,51 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: tcp_server_example.h |
|||
* Created Date: 2024-06-05 18:43:00 |
|||
* Last Modified: 2024-06-12 17:36:14 |
|||
* Description: This file is for tcp server example function definition. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/05 first release |
|||
*/ |
|||
#ifndef TCP_SERVER_EXAMPLE_H |
|||
#define TCP_SERVER_EXAMPLE_H |
|||
|
|||
/***************************** Include Files *********************************/ |
|||
#include "ftypes.h" |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" |
|||
{ |
|||
#endif |
|||
|
|||
/************************** Constant Definitions *****************************/ |
|||
|
|||
/**************************** Type Definitions *******************************/ |
|||
|
|||
/************************** Variable Definitions *****************************/ |
|||
|
|||
/***************** Macros (Inline Functions) Definitions *********************/ |
|||
|
|||
/************************** Function Prototypes ******************************/ |
|||
/* entry function for tcp server example */ |
|||
int TcpServerCreate(void); |
|||
void TcpServerDeinit(void); |
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif |
@ -1,40 +1,64 @@ |
|||
/*
|
|||
* Copyright : (C) 2022 Phytium Information Technology, Inc. |
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* |
|||
* |
|||
* FilePath: main.c |
|||
* Date: 2022-02-10 14:53:41 |
|||
* LastEditTime: 2022-02-17 17:38:03 |
|||
* Description: This file is for creating a main loop from which the programme will start. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ------ -------- -------------------------------------- |
|||
* 1.0 liuzhihong 2022/11/25 first release |
|||
* Created Date: 2024-05-30 19:05:18 |
|||
* Last Modified: 2024-06-12 17:55:33 |
|||
* Description: This file is for tcp server example main functions. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/03 first release |
|||
*/ |
|||
|
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
|
|||
/***************************** Include Files *********************************/ |
|||
#include "sdkconfig.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#warning "Please include sdkconfig.h" |
|||
#endif |
|||
#ifndef CONFIG_USE_LETTER_SHELL |
|||
#error "Please include letter shell first!!!" |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
#include "shell_port.h" |
|||
#endif |
|||
|
|||
extern void TimerLoop(void); |
|||
#include "tcp_server_example.h" |
|||
#include "lwip_timer.h" |
|||
|
|||
/************************** Constant Definitions *****************************/ |
|||
|
|||
/**************************** Type Definitions *******************************/ |
|||
|
|||
/************************** Variable Definitions *****************************/ |
|||
|
|||
/***************** Macros (Inline Functions) Definitions *********************/ |
|||
|
|||
/************************** Function Prototypes ******************************/ |
|||
|
|||
/************************** Function *****************************************/ |
|||
|
|||
int main() |
|||
{ |
|||
TimerLoop(); |
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
/* if shell command is enabled, register example entry as shell command */ |
|||
TimerLoop(); |
|||
#else |
|||
TimerStaticInit(); |
|||
|
|||
TcpServerCreate(); |
|||
TimerStaticLoop(10); |
|||
TcpServerDeinit(); |
|||
#endif |
|||
return 0; |
|||
} |
@ -0,0 +1,69 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: cmd_tcpserver. c |
|||
* Created Date: 2024-06-05 18:43:36 |
|||
* Last Modified: 2024-06-12 17:36:19 |
|||
* Description: This file is for tcp server example cmd catalogue. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/05 first release |
|||
*/ |
|||
/***************************** Include Files *********************************/ |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
|
|||
#include "sdkconfig.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#warning "Please include sdkconfig.h" |
|||
#endif |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
#include "shell.h" |
|||
#include "strto.h" |
|||
|
|||
#include "tcp_server_example.h" |
|||
|
|||
#define EXAMPLE_IDLE 0 |
|||
#define TCP_SERVER_EXAMPLE_RUNNING 1 |
|||
|
|||
static u32 init_flag_mask=EXAMPLE_IDLE; |
|||
|
|||
static void TcpServerExampleCheckState(void) |
|||
{ |
|||
switch(init_flag_mask) |
|||
{ |
|||
case TCP_SERVER_EXAMPLE_RUNNING: |
|||
printf("Tcp server example is running, we need to deinitialize it first! \r\n"); |
|||
TcpServerDeinit(); |
|||
init_flag_mask=EXAMPLE_IDLE; |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
/* entry function for tcp server example */ |
|||
static int TcpServerExampleEntry(int argc, char *argv[]) |
|||
{ |
|||
int ret = 0; |
|||
TcpServerExampleCheckState(); |
|||
ret = TcpServerCreate(); |
|||
init_flag_mask = TCP_SERVER_EXAMPLE_RUNNING; |
|||
return ret; |
|||
} |
|||
|
|||
/* register command for tcp server example */ |
|||
SHELL_EXPORT_CMD(SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), tcpserver, TcpServerExampleEntry, Start Tcp server); |
|||
#endif |
@ -0,0 +1,219 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: lwip_timer.c |
|||
* Created Date: 2024-06-05 18:43:13 |
|||
* Last Modified: 2024-06-12 17:36:26 |
|||
* Description: This file is for lwip timer function implementation. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/05 first release |
|||
*/ |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include "strto.h" |
|||
#include "fgeneric_timer.h" |
|||
#include "ftypes.h" |
|||
#include "fparameters.h" |
|||
#include "finterrupt.h" |
|||
#include "fassert.h" |
|||
#include "timeouts.h" |
|||
|
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
#include "shell_port.h" |
|||
#endif |
|||
|
|||
#include "sdkconfig.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#error "Please include sdkconfig.h first" |
|||
#endif |
|||
|
|||
#include "lwip_port.h" |
|||
#include "lwip/ip4_addr.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/ethernet.h" |
|||
#include "lwip/netif.h" |
|||
#include "lwip/tcpip.h" |
|||
#include "lwip/inet.h" |
|||
#include "lwip/dhcp.h" |
|||
#include "ifconfig.h" |
|||
|
|||
#define TIMER_BASE_RATE_HZ 1000 /* 为timer_base_cnt 提供time base */ |
|||
#define TIMER_INTERRUPT_PRO IRQ_PRIORITY_VALUE_3 |
|||
|
|||
#ifdef CONFIG_ARCH_ARMV8_AARCH64 |
|||
#define MAX_TIMER_CNT 0xffffffffffffffff |
|||
#else |
|||
#define MAX_TIMER_CNT 0xffffffff |
|||
#endif |
|||
|
|||
static u32 timer_base_cnt = 0; |
|||
|
|||
|
|||
void LwipTestLoop(void) |
|||
{ |
|||
struct netif *netif; |
|||
|
|||
netif = netif_list; |
|||
|
|||
while (netif != NULL) |
|||
{ |
|||
if (netif->state) |
|||
{ |
|||
|
|||
LwipPortInput(netif); |
|||
LinkDetectLoop(netif); |
|||
} |
|||
netif = netif->next; |
|||
} |
|||
} |
|||
|
|||
void LwipTestDhcpLoop(u32 msec) |
|||
{ |
|||
LwipPortDhcpLoop(msec); |
|||
} |
|||
|
|||
static void GenericTimerIntrHandler(s32 vector, void *param) |
|||
{ |
|||
(void)vector; |
|||
FASSERT(param != 0); |
|||
u32 timer_base_freq = (u32)(uintptr)param; |
|||
timer_base_cnt++; |
|||
/* clear tick interrupt */ |
|||
GenericTimerSetTimerValue(GENERIC_TIMER_ID0, GenericTimerFrequecy() / timer_base_freq); |
|||
} |
|||
|
|||
void TimerLoopInit(void) |
|||
{ |
|||
u32 cnt_frq; |
|||
timer_base_cnt = 0; |
|||
/* disable timer and get system frequency */ |
|||
GenericTimerStop(GENERIC_TIMER_ID0); |
|||
cnt_frq = GenericTimerFrequecy(); |
|||
|
|||
/* set tick rate */ |
|||
GenericTimerSetTimerValue(GENERIC_TIMER_ID0, cnt_frq / TIMER_BASE_RATE_HZ); |
|||
GenericTimerInterruptEnable(GENERIC_TIMER_ID0); |
|||
|
|||
/* set generic timer interrupt */ |
|||
InterruptSetPriority(GENERIC_TIMER_NS_IRQ_NUM, TIMER_INTERRUPT_PRO); |
|||
|
|||
/* install tick handler */ |
|||
InterruptInstall(GENERIC_TIMER_NS_IRQ_NUM, GenericTimerIntrHandler, |
|||
(void *)TIMER_BASE_RATE_HZ, "GenericTimerTick"); |
|||
|
|||
/* enable interrupt */ |
|||
InterruptUmask(GENERIC_TIMER_NS_IRQ_NUM); |
|||
GenericTimerStart(GENERIC_TIMER_ID0); |
|||
} |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
void TimerLoop(void) |
|||
{ |
|||
u32 _5ms_appear = 0; |
|||
u32 base_cnt_back = 0; |
|||
TimerLoopInit(); |
|||
LSUserShellInit(); |
|||
|
|||
lwip_init(); /* lwip only init once */ |
|||
|
|||
while (1) |
|||
{ |
|||
LSuserShellNoWaitLoop(); |
|||
LwipTestLoop(); |
|||
|
|||
if (((timer_base_cnt % 5) == 0) && (_5ms_appear == 0)) /*5ms task */ |
|||
{ |
|||
_5ms_appear = 1; |
|||
LwipTestDhcpLoop(5); |
|||
} |
|||
else if ((timer_base_cnt % 5) != 0) |
|||
{ |
|||
_5ms_appear = 0; |
|||
} |
|||
if(timer_base_cnt != base_cnt_back) |
|||
{ |
|||
sys_check_timeouts(); |
|||
base_cnt_back = timer_base_cnt; |
|||
} |
|||
} |
|||
} |
|||
#else |
|||
void TimerStaticInit(void) |
|||
{ |
|||
TimerLoopInit(); |
|||
lwip_init(); /* lwip only init once */ |
|||
} |
|||
|
|||
/**
|
|||
* @name: TimerStaticLoop |
|||
* @msg: 在指定的time时间内进行网卡数据收发 |
|||
* @return void |
|||
* @note: |
|||
* @param {u32} time |
|||
*/ |
|||
void TimerStaticLoop(u32 time) |
|||
{ |
|||
u32 _5ms_appear = 0; |
|||
u32 base_cnt_back = 0; |
|||
u32 timer_start=0; |
|||
u32 time_pass_cnt=0; |
|||
u32 time_cnt; |
|||
u32 old = 0; |
|||
|
|||
time_cnt = time * GenericTimerFrequecy(); |
|||
timer_start = GenericTimerRead(GENERIC_TIMER_ID0); |
|||
while (time_pass_cnt<time_cnt) |
|||
{ |
|||
LwipTestLoop(); |
|||
|
|||
if (((timer_base_cnt % 5) == 0) && (_5ms_appear == 0)) /*5ms task */ |
|||
{ |
|||
_5ms_appear = 1; |
|||
LwipTestDhcpLoop(5); |
|||
} |
|||
else if ((timer_base_cnt % 5) != 0) |
|||
{ |
|||
_5ms_appear = 0; |
|||
} |
|||
if(timer_base_cnt != base_cnt_back) |
|||
{ |
|||
sys_check_timeouts(); |
|||
base_cnt_back = timer_base_cnt; |
|||
} |
|||
|
|||
|
|||
if(GenericTimerRead(GENERIC_TIMER_ID0)>timer_start) |
|||
time_pass_cnt = GenericTimerRead(GENERIC_TIMER_ID0)-timer_start; |
|||
else |
|||
time_pass_cnt = GenericTimerRead(GENERIC_TIMER_ID0)+(MAX_TIMER_CNT-timer_start); |
|||
|
|||
if(old != time-time_pass_cnt/GenericTimerFrequecy()) |
|||
{ |
|||
printf("Only left %d seconds\r\n", time-time_pass_cnt/GenericTimerFrequecy()); |
|||
old = time-time_pass_cnt/GenericTimerFrequecy(); |
|||
} |
|||
} |
|||
|
|||
ListIf(); |
|||
|
|||
} |
|||
#endif |
|||
|
|||
u32_t sys_now(void) |
|||
{ |
|||
return timer_base_cnt; |
|||
} |
@ -1,92 +0,0 @@ |
|||
/*
|
|||
* Copyright : (C) 2022 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: tcp_server.c |
|||
* Date: 2022-10-27 18:16:52 |
|||
* LastEditTime: 2022-12-05 18:12:14 |
|||
* Description: This file is creating a tcp server.By Tcp Connection,The server can return received data to the client which send it! |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ------ -------- -------------------------------------- |
|||
* 1.0 liuzhihong 2022/11/25 first release |
|||
*/ |
|||
#include "lwip/netif.h" |
|||
#include "lwip/ip.h" |
|||
#include "lwip/tcp.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/etharp.h" |
|||
#include "lwip/udp.h" |
|||
#include "lwip/pbuf.h" |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
#include "shell.h" |
|||
static err_t tcpecho_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) |
|||
{ |
|||
|
|||
if (p != NULL) |
|||
{ |
|||
printf("We get data: "); |
|||
printf("%s", (char *)p->payload); |
|||
/* update receive buffer */ |
|||
tcp_recved(tpcb, p->tot_len); |
|||
/* Returning received data to the client */ |
|||
tcp_write(tpcb, p->payload, p->tot_len, 1); |
|||
memset(p->payload, 0, p->tot_len); |
|||
pbuf_free(p); |
|||
} |
|||
else if (err == ERR_OK) |
|||
{ |
|||
printf("Connection closed! Waiting client to connect.\n"); |
|||
return tcp_close(tpcb); |
|||
} |
|||
return ERR_OK; |
|||
} |
|||
static err_t tcpecho_accept(void *arg, struct tcp_pcb *newpcb, err_t err) |
|||
{ |
|||
printf("Connection Established!\r\n"); |
|||
tcp_recv(newpcb, tcpecho_recv); |
|||
return ERR_OK; |
|||
} |
|||
|
|||
void TcpServerInit(void) |
|||
{ |
|||
ip_addr_t local_ip; |
|||
struct tcp_pcb *pcb = NULL; |
|||
err_t ret = ERR_OK; |
|||
/* create a tcp_pcb*/ |
|||
pcb = tcp_new(); |
|||
if (pcb == NULL) |
|||
{ |
|||
printf("Tcp Server Open Failed!\r\n"); |
|||
printf("Unable to create a new tcp_pcb\r\n"); |
|||
return ; |
|||
} |
|||
/* bind the local ip and local port */ |
|||
IP_ADDR4(&local_ip, 192, 168, 4, 10); |
|||
ret = tcp_bind(pcb, &local_ip, 5003); |
|||
if (ret == ERR_USE) |
|||
{ |
|||
printf("The port and ip are already binded!\n"); |
|||
tcp_close(pcb); |
|||
return; |
|||
} |
|||
/* listening */ |
|||
pcb = tcp_listen(pcb); |
|||
/*register a connection-established function*/ |
|||
tcp_accept(pcb, tcpecho_accept); |
|||
printf("Tcp Server Open Success!\r\n"); |
|||
} |
|||
|
|||
|
|||
SHELL_EXPORT_CMD(SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), tcpserver, TcpServerInit, Start Tcp server test); |
@ -0,0 +1,232 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: tcp_server_example.c |
|||
* Created Date: 2024-05-30 19:05:18 |
|||
* Last Modified: 2024-06-12 17:36:53 |
|||
* Description: This file is for tcp server example function implementation. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/05 first release |
|||
*/ |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include "strto.h" |
|||
#include "sdkconfig.h" |
|||
#include "ftypes.h" |
|||
#include "fassert.h" |
|||
#include "fparameters.h" |
|||
#include "eth_board.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#error "Please include sdkconfig.h first" |
|||
#endif |
|||
|
|||
#include "lwip_port.h" |
|||
#include "lwip/ip4_addr.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/ethernet.h" |
|||
#include "lwip/netif.h" |
|||
#include "lwip/tcpip.h" |
|||
#include "lwip/inet.h" |
|||
#include "lwip/tcp.h" |
|||
|
|||
#define ETH_NAME_PREFIX 'e' |
|||
|
|||
#define CONFIG_DEFAULT_INIT(config,driver_config,instance_id,interface_type) \ |
|||
.config.magic_code = LWIP_PORT_CONFIG_MAGIC_CODE, \ |
|||
.config.driver_type = driver_config, \ |
|||
.config.mac_instance = instance_id, \ |
|||
.config.mii_interface = interface_type, \ |
|||
.config.autonegotiation = 1, \ |
|||
.config.phy_speed = LWIP_PORT_SPEED_1000M, \ |
|||
.config.phy_duplex = LWIP_PORT_FULL_DUPLEX, \ |
|||
.config.capability = LWIP_PORT_MODE_MULTICAST_ADDRESS_FILITER, |
|||
|
|||
#define TCP_SERVER_PORT 5001 |
|||
|
|||
typedef struct |
|||
{ |
|||
UserConfig lwip_mac_config; |
|||
u32 dhcp_en; |
|||
char* ipaddr; |
|||
char* netmask; |
|||
char* gw; |
|||
unsigned char mac_address[6]; |
|||
struct netif netif; |
|||
} BoardMacConfig; |
|||
|
|||
static BoardMacConfig board_mac_config[MAC_NUM] = |
|||
{ |
|||
#if defined(MAC_NUM0) |
|||
{ |
|||
CONFIG_DEFAULT_INIT(lwip_mac_config,MAC_NUM0_LWIP_PORT_TYPE,MAC_NUM0_CONTROLLER,MAC_NUM0_MII_INTERFACE) |
|||
.dhcp_en=0, |
|||
.ipaddr="192.168.4.10", |
|||
.gw="192.168.4.1", |
|||
.netmask="255.255.255.0", |
|||
.mac_address={0x98, 0x0e, 0x24, 0x00, 0x11, 0x0}, |
|||
}, |
|||
#endif |
|||
#if defined(MAC_NUM1) |
|||
{ |
|||
CONFIG_DEFAULT_INIT(lwip_mac_config,MAC_NUM1_LWIP_PORT_TYPE,MAC_NUM1_CONTROLLER,MAC_NUM1_MII_INTERFACE) |
|||
.dhcp_en=0, |
|||
.ipaddr="192.168.4.11", |
|||
.gw="192.168.4.1", |
|||
.netmask="255.255.255.0", |
|||
.mac_address={0x98, 0x0e, 0x24, 0x00, 0x11, 0x1}, |
|||
}, |
|||
#endif |
|||
}; |
|||
|
|||
static void SetIP(ip_addr_t* ipaddr,ip_addr_t* gw,ip_addr_t* netmask,u32 mac_id) |
|||
{ |
|||
|
|||
if(inet_aton(board_mac_config[mac_id].ipaddr,ipaddr)==0) |
|||
printf("The addr of ipaddr is wrong\r\n"); |
|||
if(inet_aton(board_mac_config[mac_id].gw,gw)==0) |
|||
printf("The addr of gw is wrong\r\n"); |
|||
if(inet_aton(board_mac_config[mac_id].netmask,netmask)==0) |
|||
printf("The addr of netmask is wrong\r\n"); |
|||
|
|||
} |
|||
static err_t tcp_recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) |
|||
{ |
|||
/* p is NULL means we received tcp packet with FIN flag */ |
|||
if (p == NULL) |
|||
{ |
|||
printf("Connection closed! Waiting client to connect.\n"); |
|||
return tcp_close(tpcb); |
|||
} |
|||
else |
|||
{ |
|||
printf("We get data: %s",(char *)p->payload); |
|||
/* update receive buffer */ |
|||
tcp_recved(tpcb, p->tot_len); |
|||
/* Returning received data to the client */ |
|||
tcp_write(tpcb, p->payload, p->tot_len, 1); |
|||
memset(p->payload, 0, p->tot_len); |
|||
pbuf_free(p); |
|||
} |
|||
return ERR_OK; |
|||
} |
|||
|
|||
static err_t tcp_accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err) |
|||
{ |
|||
printf("Connection Established!\r\n"); |
|||
tcp_recv(newpcb, tcp_recv_callback); |
|||
return ERR_OK; |
|||
} |
|||
|
|||
void TcpServerInit(void) |
|||
{ |
|||
ip_addr_t local_ip; |
|||
struct tcp_pcb *temp_pcb; |
|||
struct tcp_pcb *listen_pcb; |
|||
err_t ret = ERR_OK; |
|||
/* create a tcp_pcb*/ |
|||
temp_pcb = tcp_new(); |
|||
if (temp_pcb == NULL) |
|||
{ |
|||
printf("Unable to create a new tcp_pcb\r\n"); |
|||
return ; |
|||
} |
|||
/* bind the local ip and local port */ |
|||
IP_ADDR4(&local_ip, 192, 168, 4, 10); |
|||
ret = tcp_bind(temp_pcb, &local_ip, TCP_SERVER_PORT); |
|||
if (ret == ERR_USE) |
|||
{ |
|||
printf("The port and ip are already binded!\n"); |
|||
tcp_close(temp_pcb); |
|||
return; |
|||
} |
|||
/* listening */ |
|||
listen_pcb = tcp_listen(temp_pcb); |
|||
/*register a connection-established function*/ |
|||
tcp_accept(listen_pcb, tcp_accept_callback); |
|||
printf("Tcp Server Open Success!\r\n"); |
|||
} |
|||
|
|||
int TcpServerCreate(void) |
|||
{ |
|||
FError ret = FT_SUCCESS; |
|||
/* mac init */ |
|||
for (int i = 0; i < MAC_NUM; i++) |
|||
{ |
|||
|
|||
struct netif *netif_p = NULL; |
|||
ip_addr_t ipaddr,netmask, gw; |
|||
board_mac_config[i].lwip_mac_config.name[0] = ETH_NAME_PREFIX; |
|||
itoa(board_mac_config[i].lwip_mac_config.mac_instance, &(board_mac_config[i].lwip_mac_config.name[1]), 10); |
|||
|
|||
/* mac ip addr set: char* -> ip_addr_t */ |
|||
SetIP(&ipaddr,&gw,&netmask,i); |
|||
/* ******************************************************************* */ |
|||
|
|||
netif_p= &board_mac_config[i].netif; |
|||
/* Add network interface to the netif_list, and set it as default */ |
|||
if (!LwipPortAdd(netif_p, &ipaddr, &netmask, &gw, board_mac_config[i].mac_address, (UserConfig *)&board_mac_config[i])) |
|||
{ |
|||
printf("Error adding N/W interface %d.\n\r",board_mac_config[i].lwip_mac_config.mac_instance); |
|||
ret = ERR_GENERAL; |
|||
goto exit; |
|||
} |
|||
printf("LwipPortAdd mac_instance %d is over.\n\r",board_mac_config[i].lwip_mac_config.mac_instance); |
|||
|
|||
netif_set_default(netif_p); |
|||
|
|||
if (netif_is_link_up(netif_p)) |
|||
{ |
|||
/* 当netif完全配置好时,必须调用该函数 */ |
|||
netif_set_up(netif_p); |
|||
if (board_mac_config[i].dhcp_en == 1) |
|||
{ |
|||
LwipPortDhcpSet(netif_p, TRUE); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
/* 当netif链接关闭时,必须调用该函数 */ |
|||
netif_set_down(netif_p); |
|||
} |
|||
|
|||
} |
|||
printf("Network setup complete.\n"); |
|||
|
|||
TcpServerInit(); |
|||
|
|||
exit: |
|||
if (ret == FT_SUCCESS) |
|||
{ |
|||
printf("%s@%d:Tcp server example [success].\r\n", __func__, __LINE__); |
|||
return 0; |
|||
} |
|||
else |
|||
{ |
|||
printf("%s@%d:Tcp server example [failure].\r\n", __func__, __LINE__); |
|||
return 1; |
|||
} |
|||
} |
|||
|
|||
void TcpServerDeinit(void) |
|||
{ |
|||
|
|||
for (int i = 0; i < MAC_NUM; i++) |
|||
{ |
|||
struct netif *netif_p = NULL; |
|||
netif_p=&board_mac_config[i].netif; |
|||
LwipPortStop(netif_p,board_mac_config[i].dhcp_en); |
|||
} |
|||
} |
Before Width: | Height: | Size: 31 KiB |
Before Width: | Height: | Size: 30 KiB |
Before Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 21 KiB |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 20 KiB |
@ -0,0 +1,57 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: lwip_timer.h |
|||
* Created Date: 2024-06-05 16:55:41 |
|||
* Last Modified: 2024-06-12 17:37:29 |
|||
* Description: This file is for lwip timer function definition. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/04 first release |
|||
*/ |
|||
#ifndef LWIP_TIMER_H |
|||
#define LWIP_TIMER_H |
|||
|
|||
/***************************** Include Files *********************************/ |
|||
#include "ftypes.h" |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" |
|||
{ |
|||
#endif |
|||
|
|||
/************************** Constant Definitions *****************************/ |
|||
|
|||
/**************************** Type Definitions *******************************/ |
|||
|
|||
/************************** Variable Definitions *****************************/ |
|||
|
|||
/***************** Macros (Inline Functions) Definitions *********************/ |
|||
|
|||
/************************** Function Prototypes ******************************/ |
|||
/* entry function for lwip timer example */ |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
void TimerLoop(void); |
|||
#else |
|||
void TimerStaticInit(void); |
|||
void TimerStaticLoop(u32 time); |
|||
#endif |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif |
@ -0,0 +1,51 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: udp_client_example.h |
|||
* Created Date: 2024-06-05 16:55:56 |
|||
* Last Modified: 2024-06-12 17:37:42 |
|||
* Description: This file is for udp client example function definition. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/04 first release |
|||
*/ |
|||
#ifndef UDP_CLIENT_EXAMPLE_H |
|||
#define UDP_CLIENT_EXAMPLE_H |
|||
|
|||
/***************************** Include Files *********************************/ |
|||
#include "ftypes.h" |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" |
|||
{ |
|||
#endif |
|||
|
|||
/************************** Constant Definitions *****************************/ |
|||
|
|||
/**************************** Type Definitions *******************************/ |
|||
|
|||
/************************** Variable Definitions *****************************/ |
|||
|
|||
/***************** Macros (Inline Functions) Definitions *********************/ |
|||
|
|||
/************************** Function Prototypes ******************************/ |
|||
/* entry function for udp server example */ |
|||
int UdpClientCreate(void); |
|||
void UdpClientDeinit(void); |
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
|||
|
|||
#endif |
@ -1,40 +1,64 @@ |
|||
/*
|
|||
* Copyright : (C) 2022 Phytium Information Technology, Inc. |
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* |
|||
* |
|||
* FilePath: main.c |
|||
* Date: 2022-02-10 14:53:41 |
|||
* LastEditTime: 2022-02-17 17:38:03 |
|||
* Description: This file is for creating a main loop from which the programme will start. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ------ -------- -------------------------------------- |
|||
* 1.0 liuzhihong 2022/11/16 first release |
|||
* Created Date: 2024-05-30 19:05:18 |
|||
* Last Modified: 2024-06-12 17:55:39 |
|||
* Description: This file is for udp client example main functions. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/03 first release |
|||
*/ |
|||
|
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include "sdkconfig.h" |
|||
|
|||
#include "sdkconfig.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#warning "Please include sdkconfig.h" |
|||
#endif |
|||
#ifndef CONFIG_USE_LETTER_SHELL |
|||
#error "Please include letter shell first!!!" |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
#include "shell_port.h" |
|||
#endif |
|||
extern void TimerLoop(void); |
|||
|
|||
#include "udp_client_example.h" |
|||
#include "lwip_timer.h" |
|||
|
|||
/************************** Constant Definitions *****************************/ |
|||
|
|||
/**************************** Type Definitions *******************************/ |
|||
|
|||
/************************** Variable Definitions *****************************/ |
|||
|
|||
/***************** Macros (Inline Functions) Definitions *********************/ |
|||
|
|||
/************************** Function Prototypes ******************************/ |
|||
|
|||
/************************** Function *****************************************/ |
|||
|
|||
int main() |
|||
{ |
|||
TimerLoop(); |
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
/* if shell command is enabled, register example entry as shell command */ |
|||
TimerLoop(); |
|||
#else |
|||
TimerStaticInit(); |
|||
|
|||
TimerStaticLoop(3); |
|||
UdpClientCreate(); |
|||
TimerStaticLoop(3); |
|||
UdpClientDeinit(); |
|||
#endif |
|||
return 0; |
|||
} |
@ -0,0 +1,68 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: cmd_udp_client.c |
|||
* Created Date: 2024-06-05 16:55:34 |
|||
* Last Modified: 2024-06-12 17:37:48 |
|||
* Description: This file is for udp client example cmd catalogue. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/04 first release |
|||
*/ |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
|
|||
#include "sdkconfig.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#warning "Please include sdkconfig.h" |
|||
#endif |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
#include "shell.h" |
|||
#include "strto.h" |
|||
|
|||
#include "udp_client_example.h" |
|||
|
|||
#define EXAMPLE_IDLE 0 |
|||
#define UDP_CLIENT_EXAMPLE_RUNNING 1 |
|||
|
|||
static u32 init_flag_mask=EXAMPLE_IDLE; |
|||
|
|||
static void UdpClientExampleCheckState(void) |
|||
{ |
|||
switch(init_flag_mask) |
|||
{ |
|||
case UDP_CLIENT_EXAMPLE_RUNNING: |
|||
printf("Udp client example is running, we need to deinitialize it first! \r\n"); |
|||
UdpClientDeinit(); |
|||
init_flag_mask=EXAMPLE_IDLE; |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
/* entry function for udp client example */ |
|||
static int UdpClientExampleEntry(int argc, char *argv[]) |
|||
{ |
|||
int ret = 0; |
|||
UdpClientExampleCheckState(); |
|||
ret = UdpClientCreate(); |
|||
init_flag_mask = UDP_CLIENT_EXAMPLE_RUNNING; |
|||
return ret; |
|||
} |
|||
|
|||
/* register command for udp client example */ |
|||
SHELL_EXPORT_CMD(SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), udpclient, UdpClientExampleEntry, Start Udp client); |
|||
#endif |
@ -0,0 +1,219 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: lwip_timer.c |
|||
* Created Date: 2024-06-05 16:54:57 |
|||
* Last Modified: 2024-06-12 17:37:53 |
|||
* Description: This file is for lwip timer function implementation. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/04 first release |
|||
*/ |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include "strto.h" |
|||
#include "fgeneric_timer.h" |
|||
#include "ftypes.h" |
|||
#include "fparameters.h" |
|||
#include "finterrupt.h" |
|||
#include "fassert.h" |
|||
#include "timeouts.h" |
|||
|
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
#include "shell_port.h" |
|||
#endif |
|||
|
|||
#include "sdkconfig.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#error "Please include sdkconfig.h first" |
|||
#endif |
|||
|
|||
#include "lwip_port.h" |
|||
#include "lwip/ip4_addr.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/ethernet.h" |
|||
#include "lwip/netif.h" |
|||
#include "lwip/tcpip.h" |
|||
#include "lwip/inet.h" |
|||
#include "lwip/dhcp.h" |
|||
#include "ifconfig.h" |
|||
|
|||
#define TIMER_BASE_RATE_HZ 1000 /* 为timer_base_cnt 提供time base */ |
|||
#define TIMER_INTERRUPT_PRO IRQ_PRIORITY_VALUE_3 |
|||
|
|||
#ifdef CONFIG_ARCH_ARMV8_AARCH64 |
|||
#define MAX_TIMER_CNT 0xffffffffffffffff |
|||
#else |
|||
#define MAX_TIMER_CNT 0xffffffff |
|||
#endif |
|||
|
|||
static u32 timer_base_cnt = 0; |
|||
|
|||
|
|||
void LwipTestLoop(void) |
|||
{ |
|||
struct netif *netif; |
|||
|
|||
netif = netif_list; |
|||
|
|||
while (netif != NULL) |
|||
{ |
|||
if (netif->state) |
|||
{ |
|||
|
|||
LwipPortInput(netif); |
|||
LinkDetectLoop(netif); |
|||
} |
|||
netif = netif->next; |
|||
} |
|||
} |
|||
|
|||
void LwipTestDhcpLoop(u32 msec) |
|||
{ |
|||
LwipPortDhcpLoop(msec); |
|||
} |
|||
|
|||
static void GenericTimerIntrHandler(s32 vector, void *param) |
|||
{ |
|||
(void)vector; |
|||
FASSERT(param != 0); |
|||
u32 timer_base_freq = (u32)(uintptr)param; |
|||
timer_base_cnt++; |
|||
/* clear tick interrupt */ |
|||
GenericTimerSetTimerValue(GENERIC_TIMER_ID0, GenericTimerFrequecy() / timer_base_freq); |
|||
} |
|||
|
|||
void TimerLoopInit(void) |
|||
{ |
|||
u32 cnt_frq; |
|||
timer_base_cnt = 0; |
|||
/* disable timer and get system frequency */ |
|||
GenericTimerStop(GENERIC_TIMER_ID0); |
|||
cnt_frq = GenericTimerFrequecy(); |
|||
|
|||
/* set tick rate */ |
|||
GenericTimerSetTimerValue(GENERIC_TIMER_ID0, cnt_frq / TIMER_BASE_RATE_HZ); |
|||
GenericTimerInterruptEnable(GENERIC_TIMER_ID0); |
|||
|
|||
/* set generic timer interrupt */ |
|||
InterruptSetPriority(GENERIC_TIMER_NS_IRQ_NUM, TIMER_INTERRUPT_PRO); |
|||
|
|||
/* install tick handler */ |
|||
InterruptInstall(GENERIC_TIMER_NS_IRQ_NUM, GenericTimerIntrHandler, |
|||
(void *)TIMER_BASE_RATE_HZ, "GenericTimerTick"); |
|||
|
|||
/* enable interrupt */ |
|||
InterruptUmask(GENERIC_TIMER_NS_IRQ_NUM); |
|||
GenericTimerStart(GENERIC_TIMER_ID0); |
|||
} |
|||
|
|||
#ifdef CONFIG_USE_LETTER_SHELL |
|||
void TimerLoop(void) |
|||
{ |
|||
u32 _5ms_appear = 0; |
|||
u32 base_cnt_back = 0; |
|||
TimerLoopInit(); |
|||
LSUserShellInit(); |
|||
|
|||
lwip_init(); /* lwip only init once */ |
|||
|
|||
while (1) |
|||
{ |
|||
LSuserShellNoWaitLoop(); |
|||
LwipTestLoop(); |
|||
|
|||
if (((timer_base_cnt % 5) == 0) && (_5ms_appear == 0)) /*5ms task */ |
|||
{ |
|||
_5ms_appear = 1; |
|||
LwipTestDhcpLoop(5); |
|||
} |
|||
else if ((timer_base_cnt % 5) != 0) |
|||
{ |
|||
_5ms_appear = 0; |
|||
} |
|||
if(timer_base_cnt != base_cnt_back) |
|||
{ |
|||
sys_check_timeouts(); |
|||
base_cnt_back = timer_base_cnt; |
|||
} |
|||
} |
|||
} |
|||
#else |
|||
void TimerStaticInit(void) |
|||
{ |
|||
TimerLoopInit(); |
|||
lwip_init(); /* lwip only init once */ |
|||
} |
|||
|
|||
/**
|
|||
* @name: TimerStaticLoop |
|||
* @msg: 在指定的time时间内进行网卡数据收发 |
|||
* @return void |
|||
* @note: |
|||
* @param {u32} time |
|||
*/ |
|||
void TimerStaticLoop(u32 time) |
|||
{ |
|||
u32 _5ms_appear = 0; |
|||
u32 base_cnt_back = 0; |
|||
u32 timer_start=0; |
|||
u32 time_pass_cnt=0; |
|||
u32 time_cnt; |
|||
u32 old = 0; |
|||
|
|||
time_cnt = time * GenericTimerFrequecy(); |
|||
timer_start = GenericTimerRead(GENERIC_TIMER_ID0); |
|||
while (time_pass_cnt<time_cnt) |
|||
{ |
|||
LwipTestLoop(); |
|||
|
|||
if (((timer_base_cnt % 5) == 0) && (_5ms_appear == 0)) /*5ms task */ |
|||
{ |
|||
_5ms_appear = 1; |
|||
LwipTestDhcpLoop(5); |
|||
} |
|||
else if ((timer_base_cnt % 5) != 0) |
|||
{ |
|||
_5ms_appear = 0; |
|||
} |
|||
if(timer_base_cnt != base_cnt_back) |
|||
{ |
|||
sys_check_timeouts(); |
|||
base_cnt_back = timer_base_cnt; |
|||
} |
|||
|
|||
|
|||
if(GenericTimerRead(GENERIC_TIMER_ID0)>timer_start) |
|||
time_pass_cnt = GenericTimerRead(GENERIC_TIMER_ID0)-timer_start; |
|||
else |
|||
time_pass_cnt = GenericTimerRead(GENERIC_TIMER_ID0)+(MAX_TIMER_CNT-timer_start); |
|||
|
|||
if(old != time-time_pass_cnt/GenericTimerFrequecy()) |
|||
{ |
|||
printf("Only left %d seconds\r\n", time-time_pass_cnt/GenericTimerFrequecy()); |
|||
old = time-time_pass_cnt/GenericTimerFrequecy(); |
|||
} |
|||
} |
|||
|
|||
ListIf(); |
|||
|
|||
} |
|||
#endif |
|||
|
|||
u32_t sys_now(void) |
|||
{ |
|||
return timer_base_cnt; |
|||
} |
@ -1,83 +0,0 @@ |
|||
/*
|
|||
* Copyright : (C) 2022 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: udp_client.c |
|||
* Date: 2022-10-25 16:44:49 |
|||
* LastEditTime: 2022-12-05 18:13:22 |
|||
* Description: This file is for creating a udp client.The client can send a udp packet to the destination host. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ------ -------- -------------------------------------- |
|||
* 1.0 liuzhihong 2022/11/16 first release |
|||
*/ |
|||
#include "lwip/netif.h" |
|||
#include "lwip/ip.h" |
|||
#include "lwip/tcp.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/etharp.h" |
|||
#include "lwip/udp.h" |
|||
#include "lwip/pbuf.h" |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
#include "shell_port.h" |
|||
#define UDP_ECHO_PORT 5001 |
|||
|
|||
void UdpClient(void) |
|||
{ |
|||
struct udp_pcb *client_pcb; |
|||
struct pbuf *q = NULL; |
|||
|
|||
/* create a udp_pcb */ |
|||
client_pcb = udp_new(); |
|||
|
|||
|
|||
if (client_pcb == NULL) |
|||
{ |
|||
printf("Create new Udb_pcb falied.\n"); |
|||
return ; |
|||
} |
|||
/*bind the local port and local ip */ |
|||
ip_addr_t ipaddr; |
|||
|
|||
IP_ADDR4(&ipaddr, 192, 168, 4, 10); |
|||
udp_bind(client_pcb, &ipaddr, UDP_ECHO_PORT); |
|||
IP_ADDR4(&ipaddr, 192, 168, 4, 50); |
|||
udp_connect(client_pcb, &ipaddr, 5002); |
|||
|
|||
/*applying for memory resources*/ |
|||
const char *reply = "This is a data from Udp client!\n"; |
|||
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)); |
|||
|
|||
/*sending data*/ |
|||
printf("We sent a data to Udp Server.\n"); |
|||
udp_send(client_pcb, q); |
|||
|
|||
/*Releasing resources*/ |
|||
udp_remove(client_pcb); |
|||
pbuf_free(q); |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
SHELL_EXPORT_CMD(SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), udpclient, UdpClient, Start Udp client); |
@ -0,0 +1,215 @@ |
|||
/*
|
|||
* Copyright : (C) 2024 Phytium Information Technology, Inc. |
|||
* All Rights Reserved. |
|||
* |
|||
* This program is OPEN SOURCE software: you can redistribute it and/or modify it |
|||
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd, |
|||
* either version 1.0 of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; |
|||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|||
* See the Phytium Public License for more details. |
|||
* |
|||
* |
|||
* FilePath: udp_client_example.c |
|||
* Created Date: 2024-05-30 19:05:18 |
|||
* Last Modified: 2024-06-12 17:37:59 |
|||
* Description: This file is for udp client example function implementation. |
|||
* |
|||
* Modify History: |
|||
* Ver Who Date Changes |
|||
* ----- ---------- -------- --------------------------------- |
|||
* 1.0 liuzhihong 2024/06/04 first release |
|||
*/ |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include "strto.h" |
|||
#include "sdkconfig.h" |
|||
#include "ftypes.h" |
|||
#include "fassert.h" |
|||
#include "fparameters.h" |
|||
#include "eth_board.h" |
|||
#ifndef SDK_CONFIG_H__ |
|||
#error "Please include sdkconfig.h first" |
|||
#endif |
|||
|
|||
#include "lwip_port.h" |
|||
#include "lwip/ip4_addr.h" |
|||
#include "lwip/init.h" |
|||
#include "netif/ethernet.h" |
|||
#include "lwip/netif.h" |
|||
#include "lwip/tcpip.h" |
|||
#include "lwip/inet.h" |
|||
#include "lwip/udp.h" |
|||
|
|||
#define ETH_NAME_PREFIX 'e' |
|||
|
|||
#define CONFIG_DEFAULT_INIT(config,driver_config,instance_id,interface_type) \ |
|||
.config.magic_code = LWIP_PORT_CONFIG_MAGIC_CODE, \ |
|||
.config.driver_type = driver_config, \ |
|||
.config.mac_instance = instance_id, \ |
|||
.config.mii_interface = interface_type, \ |
|||
.config.autonegotiation = 1, \ |
|||
.config.phy_speed = LWIP_PORT_SPEED_1000M, \ |
|||
.config.phy_duplex = LWIP_PORT_FULL_DUPLEX, \ |
|||
.config.capability = LWIP_PORT_MODE_MULTICAST_ADDRESS_FILITER, |
|||
|
|||
#define UDP_LOCAL_PORT 5001 |
|||
#define UDP_REMOTE_PORT 5002 |
|||
|
|||
typedef struct |
|||
{ |
|||
UserConfig lwip_mac_config; |
|||
u32 dhcp_en; |
|||
char* ipaddr; |
|||
char* netmask; |
|||
char* gw; |
|||
unsigned char mac_address[6]; |
|||
struct netif netif; |
|||
} BoardMacConfig; |
|||
|
|||
static BoardMacConfig board_mac_config[MAC_NUM] = |
|||
{ |
|||
#if defined(MAC_NUM0) |
|||
{ |
|||
CONFIG_DEFAULT_INIT(lwip_mac_config,MAC_NUM0_LWIP_PORT_TYPE,MAC_NUM0_CONTROLLER,MAC_NUM0_MII_INTERFACE) |
|||
.dhcp_en=0, |
|||
.ipaddr="192.168.4.10", |
|||
.gw="192.168.4.1", |
|||
.netmask="255.255.255.0", |
|||
.mac_address={0x98, 0x0e, 0x24, 0x00, 0x11, 0x0}, |
|||
}, |
|||
#endif |
|||
#if defined(MAC_NUM1) |
|||
{ |
|||
CONFIG_DEFAULT_INIT(lwip_mac_config,MAC_NUM1_LWIP_PORT_TYPE,MAC_NUM1_CONTROLLER,MAC_NUM1_MII_INTERFACE) |
|||
.dhcp_en=0, |
|||
.ipaddr="192.168.4.11", |
|||
.gw="192.168.4.1", |
|||
.netmask="255.255.255.0", |
|||
.mac_address={0x98, 0x0e, 0x24, 0x00, 0x11, 0x1}, |
|||
}, |
|||
#endif |
|||
}; |
|||
|
|||
static void SetIP(ip_addr_t* ipaddr,ip_addr_t* gw,ip_addr_t* netmask,u32 mac_id) |
|||
{ |
|||
|
|||
if(inet_aton(board_mac_config[mac_id].ipaddr,ipaddr)==0) |
|||
printf("The addr of ipaddr is wrong\r\n"); |
|||
if(inet_aton(board_mac_config[mac_id].gw,gw)==0) |
|||
printf("The addr of gw is wrong\r\n"); |
|||
if(inet_aton(board_mac_config[mac_id].netmask,netmask)==0) |
|||
printf("The addr of netmask is wrong\r\n"); |
|||
|
|||
} |
|||
|
|||
|
|||
void UdpClient(void) |
|||
{ |
|||
struct udp_pcb *client_pcb; |
|||
struct pbuf *q = NULL; |
|||
|
|||
/* create a udp_pcb */ |
|||
client_pcb = udp_new(); |
|||
FASSERT(client_pcb != NULL) |
|||
|
|||
/*bind the local port and local ip */ |
|||
ip_addr_t ipaddr; |
|||
IP_ADDR4(&ipaddr, 192, 168, 4, 10); |
|||
udp_bind(client_pcb, &ipaddr, UDP_LOCAL_PORT); |
|||
IP_ADDR4(&ipaddr, 192, 168, 4, 50); |
|||
udp_connect(client_pcb, &ipaddr, UDP_REMOTE_PORT); |
|||
|
|||
/*applying for memory resources*/ |
|||
const char *reply = "This is a data from Udp client!\n"; |
|||
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)); |
|||
|
|||
/*sending data*/ |
|||
printf("We sent a data to Udp Server.\n"); |
|||
udp_send(client_pcb, q); |
|||
printf("We have been sent a data to Udp Server.\n"); |
|||
/*Releasing resources*/ |
|||
udp_remove(client_pcb); |
|||
pbuf_free(q); |
|||
} |
|||
|
|||
int UdpClientCreate(void) |
|||
{ |
|||
FError ret = FT_SUCCESS; |
|||
/* mac init */ |
|||
for (int i = 0; i < MAC_NUM; i++) |
|||
{ |
|||
|
|||
struct netif *netif_p = NULL; |
|||
ip_addr_t ipaddr,netmask, gw; |
|||
board_mac_config[i].lwip_mac_config.name[0] = ETH_NAME_PREFIX; |
|||
itoa(board_mac_config[i].lwip_mac_config.mac_instance, &(board_mac_config[i].lwip_mac_config.name[1]), 10); |
|||
|
|||
/* mac ip addr set: char* -> ip_addr_t */ |
|||
SetIP(&ipaddr,&gw,&netmask,i); |
|||
/* ******************************************************************* */ |
|||
|
|||
netif_p= &board_mac_config[i].netif; |
|||
/* Add network interface to the netif_list, and set it as default */ |
|||
if (!LwipPortAdd(netif_p, &ipaddr, &netmask, &gw, board_mac_config[i].mac_address, (UserConfig *)&board_mac_config[i])) |
|||
{ |
|||
printf("Error adding N/W interface %d.\n\r",board_mac_config[i].lwip_mac_config.mac_instance); |
|||
ret = ERR_GENERAL; |
|||
goto exit; |
|||
} |
|||
printf("LwipPortAdd mac_instance %d is over.\n\r",board_mac_config[i].lwip_mac_config.mac_instance); |
|||
|
|||
netif_set_default(netif_p); |
|||
|
|||
if (netif_is_link_up(netif_p)) |
|||
{ |
|||
/* 当netif完全配置好时,必须调用该函数 */ |
|||
netif_set_up(netif_p); |
|||
if (board_mac_config[i].dhcp_en == 1) |
|||
{ |
|||
LwipPortDhcpSet(netif_p, TRUE); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
/* 当netif链接关闭时,必须调用该函数 */ |
|||
netif_set_down(netif_p); |
|||
} |
|||
|
|||
} |
|||
printf("Network setup complete.\n"); |
|||
|
|||
UdpClient(); |
|||
|
|||
exit: |
|||
if (ret == FT_SUCCESS) |
|||
{ |
|||
printf("%s@%d:Udp client example [success].\r\n", __func__, __LINE__); |
|||
return 0; |
|||
} |
|||
else |
|||
{ |
|||
printf("%s@%d:Udp client example [failure].\r\n", __func__, __LINE__); |
|||
return 1; |
|||
} |
|||
} |
|||
|
|||
void UdpClientDeinit(void) |
|||
{ |
|||
|
|||
for (int i = 0; i < MAC_NUM; i++) |
|||
{ |
|||
struct netif *netif_p = NULL; |
|||
netif_p=&board_mac_config[i].netif; |
|||
LwipPortStop(netif_p,board_mac_config[i].dhcp_en); |
|||
} |
|||
} |