Skip to content

Commit

Permalink
新增自定义CS引脚的初始化方法
Browse files Browse the repository at this point in the history
  • Loading branch information
saviourxx committed Aug 23, 2024
1 parent 899a530 commit 6d22b57
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,26 @@ 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.
## 5. Common problems
- Assertion problem during SPI device initialization
Expand All @@ -153,12 +172,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
27 changes: 24 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,26 @@ 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引脚。
## 5、常见问题
- SPI 设备初始化时断言问题
Expand All @@ -156,12 +177,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
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
4 changes: 4 additions & 0 deletions src/wiz.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,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

0 comments on commit 6d22b57

Please sign in to comment.