Skip to content

Commit c7e2c2b

Browse files
committed
新增自定义CS引脚的初始化方法
1 parent 899a530 commit c7e2c2b

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

README.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ wiznet
2121
│ └───Internet // WIZnet official network function realization
2222
│ │ └───DHCP // DHCP function implementation
2323
│ └───────DNS // DNS function realization
24+
├───ports // Port folder
25+
│ └───spi_wiz_init.c // WIZnet custom SPI initialization file
2426
├───src // RT_Thread transplant source code file
2527
│ └───wiz_af_inet.c // WIZnet BSD Socket registered to SAL
2628
│ │ wiz_device.c // WIZnet device initialization
@@ -134,7 +136,30 @@ msh />wiz_ping baidu.com
134136

135137
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/) ).
136138

137-
## 4. Common problems
139+
140+
## 4. Transplant files
141+
142+
In practical applications, we often mount multiple devices on an SPI bus, but each device requires a CS pin to enable it.
143+
At this point, we need to configure it before SPI initialization:
144+
145+
- Copy the ports/spiw_iz_init. c file to the board/ports folder in the bsp working directory;
146+
147+
- Modify the board/ports/SConscript file and add:
148+
149+
```c
150+
if GetDepend(['PKG_USING_WIZNET','WIZ_USING_SPI_ATTACH']):
151+
src += Glob('ports/spi_wiz_init.c')
152+
```
153+
154+
- Open the macro WIZ_SING_SPI_STTACH;
155+
156+
- Open the board/ports/spiw_iz_init. c file, configure the SPI bus number ("spi1"), and the actual CS pin:
157+
158+
```c
159+
rt_hw_spi_device_attach("spi1", WIZ_SPI_DEVICE, GPIOB, GPIO_PIN_6);
160+
```
161+
162+
## 5. Common problems
138163

139164
- Assertion problem during SPI device initialization
140165

@@ -153,12 +178,12 @@ The normal test of the `ping` command indicates that the WIZnet device is succes
153178
- 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.
154179

155180

156-
## 5. Matters needing attention
181+
## 6. Matters needing attention
157182

158183
- 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;
159184
- 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;
160185

161-
## 6. Contact & Thanks
186+
## 7. Contact & Thanks
162187

163188
- Maintenance: RT-Thread development team
164189
- Homepage: https://github.com/RT-Thread-packages/wiznet

README_ZH.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ wiznet
2121
│ └───Internet // WIZnet 官方网络功能实现
2222
│ │ └───DHCP // DHCP 功能实现
2323
│ └───────DNS // DNS 功能实现
24+
├───ports // Ports 移植文件夹
25+
│ └───spi_wiz_init.c // WIZnet 自定义SPI初始化文件
2426
├───src // RT_Thread 移植源码文件
2527
│ └───wiz_af_inet.c // WIZnet BSD Socket 注册到 SAL
2628
│ │ wiz_device.c // WIZnet 设备初始化
@@ -137,7 +139,30 @@ msh />wiz_ping baidu.com
137139

138140
`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/) )。
139141

140-
## 4、常见问题
142+
## 4、移植文件
143+
144+
实际应用中,我们时常在一条SPI总线上挂载多个设备,然而每个设备都需要一个CS引脚去使能。
145+
146+
此时我们要在SPI初始化之前配置它:
147+
148+
- ports/spi_wiz_init.c 文件拷贝到bsp工作目录的 board/ports 文件夹下;
149+
150+
- 修改 board/ports/SConscript 文件,添加:
151+
152+
```c
153+
if GetDepend(['PKG_USING_WIZNET','WIZ_USING_SPI_ATTACH']):
154+
src += Glob('ports/spi_wiz_init.c')
155+
```
156+
157+
- 打开宏 WIZ_USING_SPI_ATTACH;
158+
159+
- 打开 board/ports/spi_wiz_init.c 文件,配置SPI总线号("spi1"),还有实际的CS引脚:
160+
161+
```c
162+
rt_hw_spi_device_attach("spi1", WIZ_SPI_DEVICE, GPIOB, GPIO_PIN_6);
163+
```
164+
165+
## 5、常见问题
141166

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

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

158183

159-
## 5、注意事项
184+
## 6、注意事项
160185

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

164-
## 6、联系方式 & 感谢
189+
## 7、联系方式 & 感谢
165190

166191
- 维护:RT-Thread 开发团队
167192
- 主页:https://github.com/RT-Thread-packages/wiznet

ports/spi_wiz_init.c

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2006-2023, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024-08-23 LiangZeHao first version
9+
*/
10+
11+
#include <rtthread.h>
12+
#include "drv_spi.h"
13+
14+
15+
16+
#if defined(PKG_USING_WIZNET) && defined(WIZ_USING_SPI_ATTACH)
17+
18+
extern int rt_hw_spi_wiz_init(void);
19+
20+
int rt_hw_spi_wiz_init(void)
21+
{
22+
rt_hw_spi_device_attach("spi1", WIZ_SPI_DEVICE, GPIOB, GPIO_PIN_6);
23+
24+
return RT_EOK;
25+
}
26+
27+
#endif

src/wiz.c

+4
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,10 @@ int wiz_init(void)
918918

919919
/* I think you can attach w5500 into spi bus at here. You can use this function to realize.*/
920920
/* 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); */
921+
#if defined(WIZ_USING_SPI_ATTACH)
922+
extern int rt_hw_spi_wiz_init(void);
923+
rt_hw_spi_wiz_init();
924+
#endif
921925

922926
/* WIZnet SPI device and pin initialize */
923927
result = wiz_device_init(WIZ_SPI_DEVICE);

0 commit comments

Comments
 (0)