support lcd test on xidatong-arm32 support wifi test on xidatong-arm32 support ethernet test on xidatong-arm32 on nuttx from Wang_linyu

it is OK
This commit is contained in:
xuedongliang
2022-10-20 16:13:50 +08:00
24 changed files with 12677 additions and 11312 deletions

View File

@@ -29,5 +29,9 @@ menu "test app"
config USER_TEST_SEMC
bool "Config test semc sdram"
default n
config USER_TEST_LCD
bool "Config test lcd device"
default n
endif
endmenu

View File

@@ -12,6 +12,10 @@ ifeq ($(CONFIG_USER_TEST_SEMC),y)
SRC_FILES += test_extsram.c
endif
ifeq ($(CONFIG_USER_TEST_LCD),y)
SRC_FILES +=
endif
ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
include $(KERNEL_ROOT)/compiler.mk
endif
@@ -19,7 +23,7 @@ endif
include $(KERNEL_ROOT)/.config
ifeq ($(CONFIG_ADD_NUTTX_FETURES),y)
include $(APPDIR)/Make.defs
CSRCS += test_extsram.c
CSRCS += test_extsram.c test_lcd.c
include $(APPDIR)/Application.mk
endif

View File

@@ -20,6 +20,7 @@
*/
#include <transform.h>
#include "stdio.h"
/* parameters for sram peripheral */
// /* stm32f4 Bank3:0X68000000 */
@@ -52,7 +53,7 @@ int ExtsramTest(void)
#endif
/* write data */
printf("Writing the %ld bytes data, waiting....", SRAM_SIZE);
printf("Writing the %ld bytes data, waiting....\n", SRAM_SIZE);
start_time = PrivGetTickTime();
for (i = 0; i < SRAM_SIZE / data_width; i++)
{
@@ -90,7 +91,7 @@ int ExtsramTest(void)
data = *(volatile uint32_t *)(SRAM_BANK_ADDR + i * data_width);
if (data != 0x55555555)
{
printf("SRAM test failed!");
printf("SRAM test failed!\n");
break;
}
#endif

View File

@@ -0,0 +1,522 @@
/****************************************************************************
* apps/examples/fb/fb_main.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifdef ADD_NUTTX_FETURES
#ifdef CONFIG_K210_LCD
#include "nuttx/arch.h"
#include "nuttx/lcd/lt768.h"
#include "nuttx/lcd/lt768_lib.h"
#include "nuttx/lcd/k210_lcd.h"
void LcdDemo(void)
{
int x1 = 50, y1 = 50, x2 = LCD_XSIZE_TFT - 50, y2 = LCD_YSIZE_TFT - 50;
Main_Image_Start_Address(LCD_START_ADDR);
Main_Image_Width(LCD_XSIZE_TFT);
Main_Window_Start_XY(0, 0);
Canvas_Image_Start_address(LCD_START_ADDR);
Canvas_image_width(LCD_XSIZE_TFT);
Active_Window_XY(0, 0);
Active_Window_WH(LCD_XSIZE_TFT, LCD_YSIZE_TFT);
up_mdelay(10);
Canvas_Image_Start_address(LCD_START_ADDR);
for(int i = 0; i < 3; i++)
{
x1 = 50;
y1 = 50;
x2 = LCD_XSIZE_TFT - 50;
y2 = LCD_YSIZE_TFT - 50;
syslog(LOG_NOTICE, "Disp_demo %d (%d,%d - %d,%d)\n", i, x1, y1, x2, y2);
LT768_DrawSquare_Fill(x1, y1, x2, y2, Red);
up_mdelay(2000);
x1 += 20;
y1 += 20;
x2 -= 20;
y2 -= 20;
LT768_DrawSquare_Fill(x1, y1, x2, y2, Green);
up_mdelay(2000);
x1 += 20;
y1 += 20;
x2 -= 20;
y2 -= 20;
LT768_DrawSquare_Fill(x1, y1, x2, y2, Blue);
up_mdelay(2000);
}
}
#else
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <nuttx/video/fb.h>
#include <nuttx/video/rgbcolors.h>
/****************************************************************************
* Preprocessor Definitions
****************************************************************************/
#define NCOLORS 6
#define TEST_FB_DEV "/dev/fb0"
/****************************************************************************
* Private Types
****************************************************************************/
struct fb_state_s
{
int fd;
struct fb_videoinfo_s vinfo;
struct fb_planeinfo_s pinfo;
#ifdef CONFIG_FB_OVERLAY
struct fb_overlayinfo_s oinfo;
#endif
FAR void *fbmem;
};
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_default_fbdev[] = TEST_FB_DEV;
/* Violet-Blue-Green-Yellow-Orange-Red */
static const uint32_t g_rgb24[NCOLORS] =
{
RGB24_VIOLET, RGB24_BLUE, RGB24_GREEN,
RGB24_YELLOW, RGB24_ORANGE, RGB24_RED
};
static const uint16_t g_rgb16[NCOLORS] =
{
RGB16_VIOLET, RGB16_BLUE, RGB16_GREEN,
RGB16_YELLOW, RGB16_ORANGE, RGB16_RED
};
static const uint8_t g_rgb8[NCOLORS] =
{
RGB8_VIOLET, RGB8_BLUE, RGB8_GREEN,
RGB8_YELLOW, RGB8_ORANGE, RGB8_RED
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* draw_rect
****************************************************************************/
static void draw_rect32(FAR struct fb_state_s *state,
FAR struct fb_area_s *area, int color)
{
FAR uint32_t *dest;
FAR uint8_t *row;
int x;
int y;
row = (FAR uint8_t *)state->fbmem + state->pinfo.stride * area->y;
for (y = 0; y < area->h; y++)
{
dest = ((FAR uint32_t *)row) + area->x;
for (x = 0; x < area->w; x++)
{
*dest++ = g_rgb24[color];
}
row += state->pinfo.stride;
}
}
static void draw_rect16(FAR struct fb_state_s *state,
FAR struct fb_area_s *area, int color)
{
FAR uint16_t *dest;
FAR uint8_t *row;
int x;
int y;
row = (FAR uint8_t *)state->fbmem + state->pinfo.stride * area->y;
for (y = 0; y < area->h; y++)
{
dest = ((FAR uint16_t *)row) + area->x;
for (x = 0; x < area->w; x++)
{
*dest++ = g_rgb16[color];
}
row += state->pinfo.stride;
}
}
static void draw_rect8(FAR struct fb_state_s *state,
FAR struct fb_area_s *area, int color)
{
FAR uint8_t *dest;
FAR uint8_t *row;
int x;
int y;
row = (FAR uint8_t *)state->fbmem + state->pinfo.stride * area->y;
for (y = 0; y < area->h; y++)
{
dest = row + area->x;
for (x = 0; x < area->w; x++)
{
*dest++ = g_rgb8[color];
}
row += state->pinfo.stride;
}
}
static void draw_rect1(FAR struct fb_state_s *state,
FAR struct fb_area_s *area, int color)
{
FAR uint8_t *pixel;
FAR uint8_t *row;
uint8_t color8 = (color & 1) == 0 ? 0 : 0xff;
uint8_t lmask;
uint8_t rmask;
int startx;
int endx;
int x;
int y;
/* Calculate the framebuffer address of the first row to draw on */
row = (FAR uint8_t *)state->fbmem + state->pinfo.stride * area->y;
/* Calculate the start byte position rounding down so that we get the
* first byte containing any part of the pixel sequence. Then calculate
* the last byte position with a ceil() operation so it includes any final
* final pixels of the sequence.
*/
startx = (area->x >> 3);
endx = ((area->x + area->w + 6) >> 3);
/* Calculate a mask on the first and last bytes of the sequence that may
* not be completely filled with pixel.
*/
lmask = 0xff << (8 - (area->x & 7));
rmask = 0xff >> ((area->x + area->w - 1) & 7);
/* Now draw each row, one-at-a-time */
for (y = 0; y < area->h; y++)
{
/* 'pixel' points to the 1st pixel the next row */
pixel = row + startx;
/* Special case: The row is less no more than one byte wide */
if (startx == endx)
{
uint8_t mask = lmask | rmask;
*pixel = (*pixel & mask) | (color8 & ~mask);
}
else
{
/* Special case the first byte of the row */
*pixel = (*pixel & lmask) | (color8 & ~lmask);
pixel++;
/* Handle all middle bytes in the row */
for (x = startx + 1; x < endx; x++)
{
*pixel++ = color8;
}
/* Handle the final byte of the row */
*pixel = (*pixel & rmask) | (color8 & ~rmask);
}
row += state->pinfo.stride;
}
}
static void draw_rect(FAR struct fb_state_s *state,
FAR struct fb_area_s *area, int color)
{
#ifdef CONFIG_FB_UPDATE
int ret;
#endif
switch (state->pinfo.bpp)
{
case 32:
draw_rect32(state, area, color);
break;
case 16:
draw_rect16(state, area, color);
break;
case 8:
default:
draw_rect8(state, area, color);
break;
case 1:
draw_rect1(state, area, color);
break;
}
#ifdef CONFIG_FB_UPDATE
ret = ioctl(state->fd, FBIO_UPDATE,
(unsigned long)((uintptr_t)area));
if (ret < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: ioctl(FBIO_UPDATE) failed: %d\n",
errcode);
}
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* fb_main
****************************************************************************/
int test_lcd(int argc, FAR char *argv[])
{
FAR const char *fbdev = g_default_fbdev;
struct fb_state_s state;
struct fb_area_s area;
int nsteps;
int xstep;
int ystep;
int width;
int height;
int color;
int x;
int y;
int ret;
/* There is a single required argument: The path to the framebuffer
* driver.
*/
if (argc == 2)
{
fbdev = argv[1];
}
else if (argc != 1)
{
fprintf(stderr, "ERROR: Single argument required\n");
fprintf(stderr, "USAGE: %s [<fb-driver-path>]\n", argv[0]);
return EXIT_FAILURE;
}
/* Open the framebuffer driver */
state.fd = open(fbdev, O_RDWR);
if (state.fd < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: Failed to open %s: %d\n", fbdev, errcode);
return EXIT_FAILURE;
}
/* Get the characteristics of the framebuffer */
ret = ioctl(state.fd, FBIOGET_VIDEOINFO,
(unsigned long)((uintptr_t)&state.vinfo));
if (ret < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: ioctl(FBIOGET_VIDEOINFO) failed: %d\n",
errcode);
close(state.fd);
return EXIT_FAILURE;
}
printf("2022-10-14 Mr. Wang commit LCD\n");
printf("VideoInfo:\n");
printf(" fmt: %u\n", state.vinfo.fmt);
printf(" xres: %u\n", state.vinfo.xres);
printf(" yres: %u\n", state.vinfo.yres);
printf(" nplanes: %u\n", state.vinfo.nplanes);
#ifdef CONFIG_FB_OVERLAY
printf("noverlays: %u\n", state.vinfo.noverlays);
/* Select the first overlay, which should be the composed framebuffer */
ret = ioctl(state.fd, FBIO_SELECT_OVERLAY, 0);
if (ret < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: ioctl(FBIO_SELECT_OVERLAY) failed: %d\n",
errcode);
close(state.fd);
return EXIT_FAILURE;
}
/* Get the first overlay information */
state.oinfo.overlay = 0;
ret = ioctl(state.fd, FBIOGET_OVERLAYINFO,
(unsigned long)((uintptr_t)&state.oinfo));
if (ret < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: ioctl(FBIOGET_OVERLAYINFO) failed: %d\n",
errcode);
close(state.fd);
return EXIT_FAILURE;
}
printf("OverlayInfo (overlay 0):\n");
printf(" fbmem: %p\n", state.oinfo.fbmem);
printf(" fblen: %lu\n", (unsigned long)state.oinfo.fblen);
printf(" stride: %u\n", state.oinfo.stride);
printf(" overlay: %u\n", state.oinfo.overlay);
printf(" bpp: %u\n", state.oinfo.bpp);
printf(" blank: %u\n", state.oinfo.blank);
printf("chromakey: 0x%08" PRIx32 "\n", state.oinfo.chromakey);
printf(" color: 0x%08" PRIx32 "\n", state.oinfo.color);
printf(" transp: 0x%02x\n", state.oinfo.transp.transp);
printf(" mode: %u\n", state.oinfo.transp.transp_mode);
printf(" area: (%u,%u) => (%u,%u)\n",
state.oinfo.sarea.x, state.oinfo.sarea.y,
state.oinfo.sarea.w, state.oinfo.sarea.h);
printf(" accl: %" PRIu32 "\n", state.oinfo.accl);
#endif
ret = ioctl(state.fd, FBIOGET_PLANEINFO,
(unsigned long)((uintptr_t)&state.pinfo));
if (ret < 0)
{
int errcode = errno;
fprintf(stderr, "ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n",
errcode);
close(state.fd);
return EXIT_FAILURE;
}
printf("PlaneInfo (plane 0):\n");
printf(" fbmem: %p\n", state.pinfo.fbmem);
printf(" fblen: %lu\n", (unsigned long)state.pinfo.fblen);
printf(" stride: %u\n", state.pinfo.stride);
printf(" display: %u\n", state.pinfo.display);
printf(" bpp: %u\n", state.pinfo.bpp);
/* Only these pixel depths are supported. viinfo.fmt is ignored, only
* certain color formats are supported.
*/
if (state.pinfo.bpp != 32 && state.pinfo.bpp != 16 &&
state.pinfo.bpp != 8 && state.pinfo.bpp != 1)
{
fprintf(stderr, "ERROR: bpp=%u not supported\n", state.pinfo.bpp);
close(state.fd);
return EXIT_FAILURE;
}
/* mmap() the framebuffer.
*
* NOTE: In the FLAT build the frame buffer address returned by the
* FBIOGET_PLANEINFO IOCTL command will be the same as the framebuffer
* address. mmap(), however, is the preferred way to get the framebuffer
* address because in the KERNEL build, it will perform the necessary
* address mapping to make the memory accessible to the application.
*/
state.fbmem = mmap(NULL, state.pinfo.fblen, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FILE, state.fd, 0);
if (state.fbmem == MAP_FAILED)
{
int errcode = errno;
fprintf(stderr, "ERROR: ioctl(FBIOGET_PLANEINFO) failed: %d\n",
errcode);
close(state.fd);
return EXIT_FAILURE;
}
printf("Mapped FB: %p\n", state.fbmem);
/* Draw some rectangles */
nsteps = 2 * (NCOLORS - 1) + 1;
xstep = state.vinfo.xres / nsteps;
ystep = state.vinfo.yres / nsteps;
width = state.vinfo.xres;
height = state.vinfo.yres;
for (x = 0, y = 0, color = 0;
color < NCOLORS;
x += xstep, y += ystep, color++)
{
area.x = x;
area.y = y;
area.w = width;
area.h = height;
printf("%2d: (%3d,%3d) (%3d,%3d)\n",
color, area.x, area.y, area.w, area.h);
draw_rect(&state, &area, color);
usleep(500 * 1000);
width -= (2 * xstep);
height -= (2 * ystep);
}
printf("Test finished\n");
munmap(state.fbmem, state.pinfo.fblen);
close(state.fd);
return EXIT_SUCCESS;
}
void LcdDemo(void)
{
test_lcd(1, NULL);
}
#endif
#endif

View File

@@ -29,6 +29,7 @@
#ifdef ADD_NUTTX_FETURES
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "stdio.h"
#endif
@@ -42,21 +43,47 @@ char tcp_socket_ip[] = {192, 168, 250, 252};
#define lw_error printf
#define LWIP_DEMO_TIMES 3
/** Create u32_t value from bytes */
#define LWIP_MAKEU32(a,b,c,d) (((uint32_t)((a) & 0xff) << 24) | \
((uint32_t)((b) & 0xff) << 16) | \
((uint32_t)((c) & 0xff) << 8) | \
(uint32_t)((d) & 0xff))
#define PP_HTONL(x) ((uint32_t)(x))
#define LWIP_TARGET_PORT 6000
#define LWIP_TARGET_PORT 4840
#endif
uint16_t tcp_socket_port = LWIP_TARGET_PORT;
char tcp_ip_str[128] = {0};
/******************************************************************************/
static void TCPSocketRecvTask(void *arg)
void TcpSocketConfigParam(char *ip_str)
{
int ip1, ip2, ip3, ip4, port = 0;
if(ip_str == NULL)
{
return;
}
if(sscanf(ip_str, "%d.%d.%d.%d:%d", &ip1, &ip2, &ip3, &ip4, &port))
{
printf("config ip %s port %d\n", ip_str, port);
strcpy(tcp_ip_str, ip_str);
tcp_socket_ip[0] = ip1;
tcp_socket_ip[1] = ip2;
tcp_socket_ip[2] = ip3;
tcp_socket_ip[3] = ip4;
if(port)
tcp_socket_port = port;
return;
}
if(sscanf(ip_str, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4))
{
printf("config ip %s\n", ip_str);
tcp_socket_ip[0] = ip1;
tcp_socket_ip[1] = ip2;
tcp_socket_ip[2] = ip3;
tcp_socket_ip[3] = ip4;
strcpy(tcp_ip_str, ip_str);
}
}
static void TcpSocketRecvTask(void *arg)
{
int fd = -1, clientfd;
int recv_len;
@@ -64,15 +91,18 @@ static void TCPSocketRecvTask(void *arg)
struct sockaddr_in tcp_addr;
socklen_t addr_len;
while(1) {
while(1)
{
recv_buf = (char *)malloc(TCP_DEMO_BUF_SIZE);
if (recv_buf == NULL) {
if (recv_buf == NULL)
{
lw_error("No memory\n");
continue;
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
if (fd < 0)
{
lw_error("Socket error\n");
free(recv_buf);
continue;
@@ -83,7 +113,8 @@ static void TCPSocketRecvTask(void *arg)
tcp_addr.sin_port = htons(tcp_socket_port);
memset(&(tcp_addr.sin_zero), 0, sizeof(tcp_addr.sin_zero));
if (bind(fd, (struct sockaddr *)&tcp_addr, sizeof(struct sockaddr)) == -1) {
if (bind(fd, (struct sockaddr *)&tcp_addr, sizeof(struct sockaddr)) == -1)
{
lw_error("Unable to bind\n");
close(fd);
free(recv_buf);
@@ -91,10 +122,11 @@ static void TCPSocketRecvTask(void *arg)
}
lw_print("tcp bind success, start to receive.\n");
lw_notice("\n\nLocal Port:%d\n\n", tcp_socket_port);
lw_notice("\nLocal Port:%d\n", tcp_socket_port);
// setup socket fd as listening mode
if (listen(fd, 5) != 0 ) {
if (listen(fd, 5) != 0 )
{
lw_error("Unable to listen\n");
close(fd);
free(recv_buf);
@@ -105,10 +137,13 @@ static void TCPSocketRecvTask(void *arg)
clientfd = accept(fd, (struct sockaddr *)&tcp_addr, (socklen_t*)&addr_len);
lw_notice("client %s connected\n", inet_ntoa(tcp_addr.sin_addr));
while(1) {
while(1)
{
memset(recv_buf, 0, TCP_DEMO_BUF_SIZE);
recv_len = recvfrom(clientfd, recv_buf, TCP_DEMO_BUF_SIZE, 0, (struct sockaddr *)&tcp_addr, &addr_len);
if(recv_len > 0) {
recv_len = recvfrom(clientfd, recv_buf, TCP_DEMO_BUF_SIZE, 0,
(struct sockaddr *)&tcp_addr, &addr_len);
if(recv_len > 0)
{
lw_notice("Receive from : %s\n", inet_ntoa(tcp_addr.sin_addr));
lw_notice("Receive data : %d - %s\n\n", recv_len, recv_buf);
}
@@ -120,35 +155,39 @@ static void TCPSocketRecvTask(void *arg)
free(recv_buf);
}
#ifdef ADD_XIZI_FETURES
void TCPSocketRecvTest(int argc, char *argv[])
void TcpSocketRecvTest(int argc, char *argv[])
{
int result = 0;
if(argc >= 2) {
if(argc >= 2)
{
lw_print("lw: [%s] target ip %s\n", __func__, argv[1]);
if(sscanf(argv[1], "%d.%d.%d.%d:%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3], &tcp_socket_port) == 0) {
sscanf(argv[1], "%d.%d.%d.%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3]);
}
TcpSocketConfigParam(argv[1]);
}
#ifdef ADD_XIZI_FETURES
lwip_config_tcp(lwip_ipaddr, lwip_netmask, tcp_socket_ip);
sys_thread_new("TCPSocketRecvTask", TCPSocketRecvTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
}
PRIV_SHELL_CMD_FUNCTION(TCPSocketRecvTest, a tcp receive sample, PRIV_SHELL_CMD_MAIN_ATTR);
sys_thread_new("TcpSocketRecvTask", TcpSocketRecvTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
#endif
static void TCPSocketSendTask(void *arg)
#ifdef ADD_NUTTX_FETURES
TcpSocketRecvTask(NULL);
#endif
}
PRIV_SHELL_CMD_FUNCTION(TcpSocketRecvTest, a tcp receive sample, PRIV_SHELL_CMD_MAIN_ATTR);
static void TcpSocketSendTask(void *arg)
{
int cnt = LWIP_DEMO_TIMES;
int fd = -1;
int ret;
char send_msg[128];
lw_print("2022-10-14 Mr. Wang commit TCP\n");
lw_print("%s start\n", __func__);
memset(send_msg, 0, sizeof(send_msg));
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
if (fd < 0)
{
lw_print("Socket error\n");
return;
}
@@ -156,21 +195,25 @@ static void TCPSocketSendTask(void *arg)
struct sockaddr_in tcp_sock;
tcp_sock.sin_family = AF_INET;
tcp_sock.sin_port = htons(tcp_socket_port);
tcp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(tcp_socket_ip[0], tcp_socket_ip[1], tcp_socket_ip[2], tcp_socket_ip[3]));
tcp_sock.sin_addr.s_addr = inet_addr(tcp_ip_str);
memset(&(tcp_sock.sin_zero), 0, sizeof(tcp_sock.sin_zero));
if (connect(fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr))) {
lw_print("Unable to connect\n");
ret = connect(fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr));
if (ret)
{
lw_print("Unable to connect %s = %d\n", tcp_ip_str, ret);
close(fd);
return;
}
lw_notice("\n\nTarget Port:%d\n\n", tcp_socket_port);
lw_print("TCP connect %s:%d success, start to send.\n", tcp_ip_str, tcp_socket_port);
while (cnt --) {
while (cnt --)
{
lw_print("Lwip client is running.\n");
snprintf(send_msg, sizeof(send_msg), "TCP test package times %d\r\n", cnt);
sendto(fd, send_msg, strlen(send_msg), 0, (struct sockaddr*)&tcp_sock, sizeof(struct sockaddr));
send(fd, send_msg, strlen(send_msg), 0);
lw_notice("Send tcp msg: %s ", send_msg);
PrivTaskDelay(1000);
}
@@ -179,32 +222,21 @@ static void TCPSocketSendTask(void *arg)
return;
}
#ifdef ADD_XIZI_FETURES
void TCPSocketSendTest(int argc, char *argv[])
void TcpSocketSendTest(int argc, char *argv[])
{
if(argc >= 2) {
if(argc >= 2)
{
lw_print("lw: [%s] target ip %s\n", __func__, argv[1]);
if(sscanf(argv[1], "%d.%d.%d.%d:%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3], &tcp_socket_port) == 0)
{
sscanf(argv[1], "%d.%d.%d.%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3]);
}
TcpSocketConfigParam(argv[1]);
}
#ifdef ADD_XIZI_FETURES
lwip_config_tcp(lwip_ipaddr, lwip_netmask, tcp_socket_ip);
sys_thread_new("TCP Socket Send", TCPSocketSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
}
PRIV_SHELL_CMD_FUNCTION(TCPSocketSendTest, a tcp send sample, PRIV_SHELL_CMD_MAIN_ATTR);
sys_thread_new("Tcp Socket Send", TcpSocketSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
#endif
#ifdef ADD_NUTTX_FETURES
void tcp_recv_demo(void)
{
TCPSocketRecvTask(NULL);
}
void tcp_send_demo(void)
{
TCPSocketSendTask(NULL);
}
TcpSocketSendTask(NULL);
#endif
}
PRIV_SHELL_CMD_FUNCTION(TcpSocketSendTest, a tcp send sample, PRIV_SHELL_CMD_MAIN_ATTR);

View File

@@ -31,45 +31,72 @@
#include <stdio.h>
#define LWIP_DEMO_TIMES 3
#define LWIP_LOCAL_PORT 6000
#define LWIP_LOCAL_PORT 4840
#define lw_error printf
#define lw_notice printf
#define lw_print printf
/** Create u32_t value from bytes */
#define LWIP_MAKEU32(a,b,c,d) (((uint32_t)((a) & 0xff) << 24) | \
((uint32_t)((b) & 0xff) << 16) | \
((uint32_t)((c) & 0xff) << 8) | \
(uint32_t)((d) & 0xff))
#define PP_HTONL(x) ((uint32_t)(x))
#endif
#define UDP_BUF_SIZE 65536
char udp_socket_ip[] = {192, 168, 250, 252};
char udp_ip_str[128] = {0};
uint16_t udp_socket_port = LWIP_LOCAL_PORT;
/*****************************************************************************/
void UdpSocketConfigParam(char *ip_str)
{
int ip1, ip2, ip3, ip4, port = 0;
if(ip_str == NULL)
{
return;
}
if(sscanf(ip_str, "%d.%d.%d.%d:%d", &ip1, &ip2, &ip3, &ip4, &port))
{
printf("config ip %s port %d\n", ip_str, port);
strcpy(udp_ip_str, ip_str);
udp_socket_ip[0] = ip1;
udp_socket_ip[1] = ip2;
udp_socket_ip[2] = ip3;
udp_socket_ip[3] = ip4;
if(port)
udp_socket_port = port;
return;
}
if(sscanf(ip_str, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4))
{
printf("config ip %s\n", ip_str);
udp_socket_ip[0] = ip1;
udp_socket_ip[1] = ip2;
udp_socket_ip[2] = ip3;
udp_socket_ip[3] = ip4;
strcpy(udp_ip_str, ip_str);
}
}
static void UdpSocketRecvTask(void *arg)
{
int fd = -1;
char *recv_buf;
struct sockaddr_in udp_addr, server_addr;
int recv_len;
socklen_t addr_len;
while(1) {
while(1)
{
recv_buf = (char *)malloc(UDP_BUF_SIZE);
if(recv_buf == NULL) {
if(recv_buf == NULL)
{
lw_error("No memory\n");
continue;
}
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0) {
if(fd < 0)
{
lw_error("Socket error\n");
free(recv_buf);
continue;
@@ -80,25 +107,27 @@ static void UdpSocketRecvTask(void *arg)
udp_addr.sin_port = htons(udp_socket_port);
memset(&(udp_addr.sin_zero), 0, sizeof(udp_addr.sin_zero));
if(bind(fd, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr)) == -1) {
if(bind(fd, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr)) == -1)
{
lw_error("Unable to bind\n");
close(fd);
free(recv_buf);
continue;
}
lw_notice("UDP bind sucess, start to receive.\n");
lw_notice("UDP bind success, start to receive.\n");
lw_notice("\n\nLocal Port:%d\n\n", udp_socket_port);
while(1) {
while(1)
{
memset(recv_buf, 0, UDP_BUF_SIZE);
recv_len = recvfrom(fd, recv_buf, UDP_BUF_SIZE, 0, (struct sockaddr *)&server_addr, &addr_len);
recv_len = recv(fd, recv_buf, UDP_BUF_SIZE, 0);
if(recv_len > 0)
{
lw_notice("Receive from : %s\n", inet_ntoa(server_addr.sin_addr));
lw_notice("Receive data : %s\n\n", recv_buf);
}
sendto(fd, recv_buf, recv_len, 0, (struct sockaddr*)&server_addr, addr_len);
send(fd, recv_buf, recv_len, 0);
}
close(fd);
@@ -106,21 +135,24 @@ static void UdpSocketRecvTask(void *arg)
}
}
#ifdef ADD_XIZI_FETURES
void UdpSocketRecvTest(int argc, char *argv[])
{
if(argc >= 2) {
if(argc >= 2)
{
lw_notice("lw: [%s] target ip %s\n", __func__, argv[1]);
if(sscanf(argv[1], "%d.%d.%d.%d:%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3], &udp_socket_port) == 0) {
sscanf(argv[1], "%d.%d.%d.%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3]);
}
UdpSocketConfigParam(argv[1]);
}
#ifdef ADD_XIZI_FETURES
lwip_config_tcp(lwip_ipaddr, lwip_netmask, udp_socket_ip);
sys_thread_new("UdpSocketRecvTask", UdpSocketRecvTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
sys_thread_new("UdpSocketRecvTask", UdpSocketRecvTask, NULL,
LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
#endif
#ifdef ADD_NUTTX_FETURES
UdpSocketRecvTask(NULL);
#endif
}
PRIV_SHELL_CMD_FUNCTION(UdpSocketRecvTest, a udp receive sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif
static void UdpSocketSendTask(void *arg)
{
@@ -131,7 +163,8 @@ static void UdpSocketSendTask(void *arg)
memset(send_str, 0, sizeof(send_str));
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0) {
if(fd < 0)
{
lw_error("Socket error\n");
return;
}
@@ -139,21 +172,24 @@ static void UdpSocketSendTask(void *arg)
struct sockaddr_in udp_sock;
udp_sock.sin_family = AF_INET;
udp_sock.sin_port = htons(udp_socket_port);
udp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(udp_socket_ip[0], udp_socket_ip[1], udp_socket_ip[2], udp_socket_ip[3]));
udp_sock.sin_addr.s_addr = inet_addr(udp_ip_str);
memset(&(udp_sock.sin_zero), 0, sizeof(udp_sock.sin_zero));
if(connect(fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr))) {
if(connect(fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr)))
{
lw_error("Unable to connect\n");
close(fd);
return;
}
lw_print("UDP connect success, start to send.\n");
lw_notice("\n\nTarget Port:%d\n\n", udp_sock.sin_port);
lw_print("UDP connect %s:%d success, start to send.\n",
udp_ip_str,
udp_socket_port);
while (cnt --) {
while(cnt --)
{
snprintf(send_str, sizeof(send_str), "UDP test package times %d\r\n", cnt);
sendto(fd, send_str, strlen(send_str), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr));
send(fd, send_str, strlen(send_str), 0);
lw_notice("Send UDP msg: %s ", send_str);
PrivTaskDelay(1000);
}
@@ -162,30 +198,22 @@ static void UdpSocketSendTask(void *arg)
return;
}
#ifdef ADD_XIZI_FETURES
void UdpSocketSendTest(int argc, char *argv[])
{
if(argc >= 2) {
if(argc >= 2)
{
lw_notice("lw: [%s] target ip %s\n", __func__, argv[1]);
if(sscanf(argv[1], "%d.%d.%d.%d:%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3], &udp_socket_port) == 0) {
sscanf(argv[1], "%d.%d.%d.%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3]);
}
UdpSocketConfigParam(argv[1]);
}
#ifdef ADD_XIZI_FETURES
lwip_config_tcp(lwip_ipaddr, lwip_netmask, udp_socket_ip);
sys_thread_new("UdpSocketSendTask", UdpSocketSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO);
sys_thread_new("UdpSocketSendTask", UdpSocketSendTask, NULL, LWIP_TASK_STACK_SIZE,
LWIP_DEMO_TASK_PRIO);
#endif
#ifdef ADD_NUTTX_FETURES
UdpSocketSendTask(NULL);
#endif
}
PRIV_SHELL_CMD_FUNCTION(UdpSocketSendTest, a udp send sample, PRIV_SHELL_CMD_MAIN_ATTR);
#endif
#ifdef ADD_NUTTX_FETURES
void udp_recv_demo(void)
{
UdpSocketRecvTask(NULL);
}
void udp_send_demo(void)
{
UdpSocketSendTask(NULL);
}
#endif