Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ wiznet
│ └───Internet // WIZnet official network function realization
│ │ └───DHCP // DHCP function implementation
│ └───────DNS // DNS function realization
├───ports // Port folder
│ └───spi_wiz_init.c // WIZnet custom SPI initialization file
├───src // RT_Thread transplant source code file
│ └───wiz_af_inet.c // WIZnet BSD Socket registered to SAL
│ │ wiz_device.c // WIZnet device initialization
Expand Down Expand Up @@ -134,7 +136,30 @@ msh />wiz_ping baidu.com

The normal test of the `ping` command indicates that the WIZnet device is successfully connected to the network, and then you can use the standard BSD Socket APIs abstracted by SAL (Socket Abstraction Layer) for network development (MQTT, HTTP, MbedTLS, NTP, Iperf, etc.), WIZnet software package The supported protocol cluster types are: the primary protocol cluster is **AF_WIZ**, and the secondary protocol cluster is **AF_INET** (for specific differences and usage, please refer to [SAL Programming Guide](https://www.rt-thread.org/document/site/submodules/rtthread-manual-doc/zh/1chapters/13-chapter_sal/) ).

## 4. Common problems

## 4. Transplant files

In practical applications, we often mount multiple devices on an SPI bus, but each device requires a CS pin to enable it.
At this point, we need to configure it before SPI initialization:

- Copy the ports/spiw_iz_init. c file to the board/ports folder in the bsp working directory;

- Modify the board/ports/SConscript file and add:

```c
if GetDepend(['PKG_USING_WIZNET','WIZ_USING_SPI_ATTACH']):
src += Glob('ports/spi_wiz_init.c')
```

- Open the macro WIZ_SING_SPI_STTACH;

- Open the board/ports/spiw_iz_init. c file, configure the SPI bus number ("spi1"), and the actual CS pin:

```c
rt_hw_spi_device_attach("spi1", WIZ_SPI_DEVICE, GPIOB, GPIO_PIN_6);
```

## 5. Common problems

- Assertion problem during SPI device initialization

Expand All @@ -153,12 +178,12 @@ The normal test of the `ping` command indicates that the WIZnet device is succes
- When applying for a socket, the error is ```0x22```. Note that the development branch of wiznet is in the master version or a version greater than V1.1.0. Please pay attention to the execution order of ```wiz_socket_init()```, because the ```sal_check_netdev_internet_up``` networking detection function will actively apply for a socket to determine whether the w5500 has network capabilities, and network status changes will cause ```sal_check_netdev_internet_up``` was called, causing ```0x22``` error.


## 5. Matters needing attention
## 6. Matters needing attention

- When obtaining the software package, you need to pay attention to the correct configuration of the SPI device name, reset pin number and interrupt pin number used;
- After the initialization is complete, it is recommended to use the `wiz_set_mac()` function to set the device MAC address to prevent conflicts with the default MAC address;

## 6. Contact & Thanks
## 7. Contact & Thanks

- Maintenance: RT-Thread development team
- Homepage: https://github.com/RT-Thread-packages/wiznet
31 changes: 28 additions & 3 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ wiznet
│ └───Internet // WIZnet 官方网络功能实现
│ │ └───DHCP // DHCP 功能实现
│ └───────DNS // DNS 功能实现
├───ports // Ports 移植文件夹
│ └───spi_wiz_init.c // WIZnet 自定义SPI初始化文件
├───src // RT_Thread 移植源码文件
│ └───wiz_af_inet.c // WIZnet BSD Socket 注册到 SAL
│ │ wiz_device.c // WIZnet 设备初始化
Expand Down Expand Up @@ -137,7 +139,30 @@ msh />wiz_ping baidu.com

`ping` 命令测试正常说明 WIZnet 设备网络连接成功,之后可以使用 SAL(套接字抽象层) 抽象出来的标准 BSD Socket APIs 进行网络开发(MQTT、HTTP、MbedTLS、NTP、Iperf 等),WIZnet 软件包支持的协议簇类型为:主协议簇为 **AF_WIZ**、次协议簇为 **AF_INET**(具体区别和使用方式可查看 [SAL 编程指南](https://www.rt-thread.org/document/site/submodules/rtthread-manual-doc/zh/1chapters/13-chapter_sal/) )。

## 4、常见问题
## 4、移植文件

实际应用中,我们时常在一条SPI总线上挂载多个设备,然而每个设备都需要一个CS引脚去使能。

此时我们要在SPI初始化之前配置它:

- ports/spi_wiz_init.c 文件拷贝到bsp工作目录的 board/ports 文件夹下;

- 修改 board/ports/SConscript 文件,添加:

```c
if GetDepend(['PKG_USING_WIZNET','WIZ_USING_SPI_ATTACH']):
src += Glob('ports/spi_wiz_init.c')
```

- 打开宏 WIZ_USING_SPI_ATTACH;

- 打开 board/ports/spi_wiz_init.c 文件,配置SPI总线号("spi1"),还有实际的CS引脚:

```c
rt_hw_spi_device_attach("spi1", WIZ_SPI_DEVICE, GPIOB, GPIO_PIN_6);
```

## 5、常见问题

- SPI 设备初始化时断言问题

Expand All @@ -156,12 +181,12 @@ msh />wiz_ping baidu.com
- 当出现申请 socket 时错误为 ```0x22``` 错误,注意 wiznet 的开发分支处于 master 版本或者大于 V1.1.0 的版本。请留意 ```wiz_socket_init()``` 的执行顺序,因为 ```sal_check_netdev_internet_up``` 联网检测函数,会主动申请 socket 以判断 w5500 是否具有网络能力,而网络状态变更会导致 ```sal_check_netdev_internet_up``` 被调用,造成 ```0x22``` 错误。


## 5、注意事项
## 6、注意事项

- 获取软件包时,需要注意正确配置使用的 SPI 设备名称、复位引脚号和中断引脚号;
- 初始化完成之后,建议使用 `wiz_set_mac()` 函数设置设备 MAC 地址,防止使用默认 MAC 地址产生冲突;

## 6、联系方式 & 感谢
## 7、联系方式 & 感谢

- 维护:RT-Thread 开发团队
- 主页:https://github.com/RT-Thread-packages/wiznet
4 changes: 4 additions & 0 deletions inc/wiz.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ extern "C" {
#define WIZ_RX_MBOX_NUM 10
#endif

#ifndef WIZ_TIM_IRQ_FREQ_MS
#define WIZ_TIM_IRQ_FREQ_MS 10
#endif

/* WIZnet set chip MAC address */
void wiz_user_config_mac(char *mac_buf, rt_uint8_t buf_len);
/* WIZnet initialize device and network */
Expand Down
19 changes: 13 additions & 6 deletions ioLibrary/Internet/DNS/wizchip_dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
//
//*****************************************************************************

#include <rtthread.h>
#include <string.h>
#include <stdlib.h>
#include <wizchip_socket.h>
Expand Down Expand Up @@ -367,20 +368,26 @@ int8_t parseDNSMSG(struct dhdr * pdhdr, uint8_t * pbuf, uint8_t * ip_from_dns)
for (i = 0; i < pdhdr->qdcount; i++)
{
cp = dns_question(msg, cp);
#ifdef _DNS_DEUBG_
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
if(!cp)
{
#ifdef _DNS_DEBUG_
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h\n");
#endif
if(!cp) return -1;
return -1;
}
}

/* Answer section */
for (i = 0; i < pdhdr->ancount; i++)
{
cp = dns_answer(msg, cp, ip_from_dns);
#ifdef _DNS_DEUBG_
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h");
if(!cp)
{
#ifdef _DNS_DEBUG_
printf("MAX_DOMAIN_NAME is too small, it should be redfine in dns.h\n");
#endif
if(!cp) return -1;
return -1;
}
}

/* Name server (authority) section */
Expand Down
10 changes: 8 additions & 2 deletions ioLibrary/Internet/DNS/wizchip_dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,21 @@ extern "C" {
* @brief Define it for Debug & Monitor DNS processing.
* @note If defined, it dependens on <stdio.h>
*/
//#define _DNS_DEBUG_
#ifdef WIZ_DNS_DEBUG
#define _DNS_DEBUG_
#endif

#define MAX_DNS_BUF_SIZE 256 ///< maximum size of DNS buffer. */
/*
* @brief Maxium length of your queried Domain name
* @todo SHOULD BE defined it equal as or greater than your Domain name lenght + null character(1)
* @note SHOULD BE careful to stack overflow because it is allocated 1.5 times as MAX_DOMAIN_NAME in stack.
*/
#define MAX_DOMAIN_NAME 16 // for example "www.google.com"
#ifdef WIZ_DOMAIN_NAME_MAX_SIZE
#define MAX_DOMAIN_NAME WIZ_DOMAIN_NAME_MAX_SIZE
#else
#define MAX_DOMAIN_NAME 16 // for example "www.google.com"
#endif

#define MAX_DNS_RETRY 2 ///< Requery Count
#define DNS_WAIT_TIME 3 ///< Wait response time. unit 1s.
Expand Down
27 changes: 27 additions & 0 deletions ports/spi_wiz_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-08-23 LiangZeHao first version
*/

#include <rtthread.h>
#include "drv_spi.h"



#if defined(PKG_USING_WIZNET) && defined(WIZ_USING_SPI_ATTACH)

extern int rt_hw_spi_wiz_init(void);

int rt_hw_spi_wiz_init(void)
{
rt_hw_spi_device_attach("spi1", WIZ_SPI_DEVICE, GPIOB, GPIO_PIN_6);

return RT_EOK;
}

#endif
23 changes: 23 additions & 0 deletions src/wiz.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#error "please config SPI device name, reset pin and irq pin in menuconfig."
#endif

#if (RT_VER_NUM >= 0x50000) && !defined(RT_USING_LEGACY)
#warning "Please turn on the RT_USING_LEGACY for versions v5.0.0 and above to ensure compatibility with lower versions, \
otherwise compilation errors will occur"
#endif

#define DBG_ENABLE
#define DBG_SECTION_NAME "wiz"
#ifdef WIZ_DEBUG
Expand Down Expand Up @@ -78,6 +83,11 @@ static void wiz_isr(void)
rt_interrupt_leave();
}

static void wiz_tim_isr(void)
{
rt_mb_send(wiz_rx_mb, (rt_ubase_t) wiz_device);
}

static void wiz_data_thread_entry(void *parameter)
{
#define IR_SOCK(ch) (0x01 << ch) /**< check socket interrupt */
Expand Down Expand Up @@ -873,10 +883,19 @@ static int wiz_interrupt_init(rt_base_t isr_pin)
rt_thread_startup(tid);
}

#if (WIZ_IRQ_PIN != -1)
/* initialize interrupt pin */
rt_pin_mode(isr_pin, PIN_MODE_INPUT_PULLUP);
rt_pin_attach_irq(isr_pin, PIN_IRQ_MODE_FALLING, (void (*)(void*)) wiz_isr, RT_NULL);
rt_pin_irq_enable(isr_pin, PIN_IRQ_ENABLE);
#else
static rt_timer_t wiz_tim;
/* initialize timer */
wiz_tim = rt_timer_create("wiz_tim", (void (*)(void*))wiz_tim_isr, RT_NULL, WIZ_TIM_IRQ_FREQ_MS, RT_TIMER_FLAG_PERIODIC);
if (wiz_tim != RT_NULL){
rt_timer_start(wiz_tim);
}
#endif

return 0;
}
Expand Down Expand Up @@ -913,6 +932,10 @@ int wiz_init(void)

/* I think you can attach w5500 into spi bus at here. You can use this function to realize.*/
/* extern rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, GPIO_TypeDef *cs_gpiox, uint16_t cs_gpio_pin); */
#if defined(WIZ_USING_SPI_ATTACH)
extern int rt_hw_spi_wiz_init(void);
rt_hw_spi_wiz_init();
#endif

/* WIZnet SPI device and pin initialize */
result = wiz_device_init(WIZ_SPI_DEVICE);
Expand Down
34 changes: 19 additions & 15 deletions src/wiz_af_inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
#endif

#ifdef SAL_USING_POSIX
#if (RT_VER_NUM >= 0x50100)
static int wiz_poll(struct dfs_file *file, struct rt_pollreq *req)
#else
static int wiz_poll(struct dfs_fd *file, struct rt_pollreq *req)
#endif
{
int mask = 0;
struct wiz_socket *sock;
Expand Down Expand Up @@ -67,22 +71,22 @@ static int wiz_poll(struct dfs_fd *file, struct rt_pollreq *req)

static const struct sal_socket_ops wiz_socket_ops =
{
wiz_socket,
wiz_closesocket,
wiz_bind,
wiz_listen,
wiz_connect,
wiz_accept,
wiz_sendto,
wiz_recvfrom,
wiz_getsockopt,
wiz_setsockopt,
wiz_shutdown,
NULL,
NULL,
NULL,
.socket = wiz_socket,
.closesocket = wiz_closesocket,
.bind = wiz_bind,
.listen = wiz_listen,
.connect = wiz_connect,
.accept = wiz_accept,
.sendto = wiz_sendto,
.recvfrom = wiz_recvfrom,
.getsockopt = wiz_getsockopt,
.setsockopt = wiz_setsockopt,
.shutdown = wiz_shutdown,
.getpeername = NULL,
.getsockname = NULL,
.ioctlsocket = NULL,
#ifdef SAL_USING_POSIX
wiz_poll,
.poll = wiz_poll,
#endif /* SAL_USING_POSIX */
};

Expand Down
4 changes: 2 additions & 2 deletions src/wiz_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ int wiz_setsockopt(int socket, int level, int optname, const void *optval, sockl
ret = wizchip_setsockopt(socket, (sockopt_type)optname, (void *)optval);
if (ret != SOCK_OK)
{
LOG_E("WIZnet getsocketopt input level(%d) error.", level);
LOG_E("WIZnet wiz_setsockopt input level(%d) error.", level);
return ret;
}
break;
Expand Down Expand Up @@ -1631,7 +1631,7 @@ int wiz_getaddrinfo(const char *nodename, const char *servname, const struct add
ret = DNS_run(net_info.dns, (uint8_t *)nodename, remote_ip);
if (ret == -1)
{
LOG_E("WIZnet MAX_DOMAIN_NAME is too small, should be redefined it.");
LOG_E("WIZnet getaddrinfo MAX_DOMAIN_NAME is too small, should be redefined it.");
return EAI_FAIL;
}
else if (ret < 0)
Expand Down