Explorar o código

适配RT5.x版本,log优化

ryancw hai 1 ano
pai
achega
dd7cc4deb4

+ 2 - 2
W5500Client/RyanW5500.c

@@ -1,5 +1,5 @@
-#define rlogEnable 1               // 是否使能日志
-#define rlogColorEnable 1          // 是否使能日志颜色
+#define rlogEnable                 // 是否使能日志
+#define rlogColorEnable            // 是否使能日志颜色
 #define rlogLevel (rlogLvlWarning) // 日志打印等级
 #define rlogTag "W5500"            // 日志tag
 

+ 2 - 2
W5500Client/RyanW5500Ping.c

@@ -1,5 +1,5 @@
-#define rlogEnable 1               // 是否使能日志
-#define rlogColorEnable 1          // 是否使能日志颜色
+#define rlogEnable                 // 是否使能日志
+#define rlogColorEnable            // 是否使能日志颜色
 #define rlogLevel (rlogLvlWarning) // 日志打印等级
 #define rlogTag "W5500Ping"        // 日志tag
 

+ 13 - 5
W5500Client/RyanW5500Socket.c

@@ -1,5 +1,5 @@
-#define rlogEnable 1               // 是否使能日志
-#define rlogColorEnable 1          // 是否使能日志颜色
+#define rlogEnable                 // 是否使能日志
+#define rlogColorEnable            // 是否使能日志颜色
 #define rlogLevel (rlogLvlWarning) // 日志打印等级
 #define rlogTag "W5500Socket"      // 日志tag
 
@@ -11,12 +11,20 @@
         uint8_t linkState = 0;                  \
         ctlwizchip(CW_GET_PHYLINK, &linkState); \
         if (PHY_LINK_ON != linkState)           \
+        {                                       \
+            rt_set_errno(EHOSTUNREACH);         \
             return value;                       \
+        }                                       \
     } while (0);
 
 // 可用套接字的全局数组
-static volatile RyanW5500Socket RyanW5500Sockets[RyanW5500MaxSocketNum] = {0};
-static volatile uint16_t wiz_port = 15500; // 用户可以自定义
+static RyanW5500Socket RyanW5500Sockets[RyanW5500MaxSocketNum] = {0};
+static uint16_t wiz_port = 15500; // 用户可以自定义
+
+static int _isalpha(char ch)
+{
+    return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
+}
 
 /**
  * @brief
@@ -1360,7 +1368,7 @@ static int RyanW5500_gethostbyname(const char *name, ip_addr_t *addr)
     char ipStrArr[16] = {0};
 
     // 检查域名 / ip地址
-    for (idx = 0; idx < nameLen && !isalpha(name[idx]); idx++)
+    for (idx = 0; idx < nameLen && !_isalpha(name[idx]); idx++)
         ;
 
     // 输入名称为ip地址

+ 8 - 11
W5500Client/RyanW5500Store.h

@@ -11,30 +11,27 @@ extern "C"
 #include <stdint.h>
 #include <string.h>
 #include <stdlib.h>
-#include <errno.h>
 
-#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/errno.h>
 #include <sys/time.h>
+#include <netdb.h>
 
 #include <rtthread.h>
-#include <rthw.h>
-#include <rtdevice.h>
 
-#include <sal_socket.h>
-#include <sal_netdb.h>
-#if (RTTHREAD_VERSION > 40100)
+#if (RT_VER_NUM > 40100)
 #include <sal_low_lvl.h>
 #else
 #include <sal.h>
 #endif
 
+#include "netdev_ipaddr.h"
+#include "netdev.h"
+
 #ifdef SAL_USING_POSIX
 #include <poll.h>
 #endif
 
-#include "netdev_ipaddr.h"
-#include "netdev.h"
-
 #include "RyanList.h"
 #include "RyanW5500Log.h"
 #include "platformTimer.h"
@@ -79,7 +76,7 @@ extern "C"
                                                                    \
         Ryanlevel("ErrorCode: %d, strError: %d",                   \
                   ErrorCode, (ErrorCode));                         \
-        errno = (ErrorCode);                                       \
+        rt_set_errno(ErrorCode);                                   \
         {code};                                                    \
     }
 

+ 7 - 1
W5500Client/RyanW5500netDev.c

@@ -109,7 +109,13 @@ static int RyanW5500NetdevSetDhcp(struct netdev *netdev, rt_bool_t is_enabled)
  * @param ping_resp
  * @return int
  */
-static int RyanW5500NetdevPing(struct netdev *netdev, const char *host, size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
+//    int (*ping)(struct netdev *netdev, const char *host, size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp, rt_bool_t isbind);
+static int RyanW5500NetdevPing(struct netdev *netdev, const char *host, size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
+#if RT_VER_NUM >= 0x50100
+                               ,
+                               rt_bool_t is_bind
+#endif
+)
 {
     RT_ASSERT(netdev);
     RT_ASSERT(host);

+ 11 - 20
common/RyanW5500Log.h

@@ -1,12 +1,12 @@
-#ifndef __RyanMqttLog__
-#define __RyanMqttLog__
+#ifndef __RyanW5500Log__
+#define __RyanW5500Log__
 
 #include <stdio.h>
 #include <stdint.h>
 #include <stdarg.h>
 #include "rtthread.h"
 
-#define platformPrint rt_kprintf
+#define platformPrint(str, strLen) rt_kprintf("%.*s", strLen, str);
 
 // 日志等级
 #define rlogLvlError 0
@@ -14,16 +14,6 @@
 #define rlogLvlInfo 2
 #define rlogLvlDebug 3
 
-// 是否使能日志
-#ifndef rlogEnable
-#define rlogEnable 1
-#endif
-
-// 是否使能日志颜色
-#ifndef rlogColorEnable
-#define rlogColorEnable 1
-#endif
-
 // 日志打印等级
 #ifndef rlogLevel
 #define rlogLevel (rlogLvlDebug)
@@ -38,21 +28,24 @@
  * @brief 日志相关
  *
  */
-#if rlogEnable > 0
+#ifdef rlogEnable
 static void rlog_output(char *lvl, uint8_t color_n, char *const fmt, ...)
 {
     // RyanLogPrintf("\033[字背景颜色;字体颜色m  用户字符串 \033[0m" );
-    char dbgBuffer[256] = {0};
+    char dbgBuffer[160] = {0};
     uint16_t len = 0;
 
 // 打印颜色
-#if rlogColorEnable > 0
+#ifdef rlogColorEnable
     len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, "\033[%dm", color_n);
 #endif
 
     // 打印提示符
     len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, "[%s/%s]", lvl, rlogTag);
 
+    platformPrint(dbgBuffer, len);
+    len = 0;
+
     // 打印用户输入
     va_list args;
     va_start(args, fmt);
@@ -60,7 +53,7 @@ static void rlog_output(char *lvl, uint8_t color_n, char *const fmt, ...)
     va_end(args);
 
 // 打印颜色
-#if rlogColorEnable > 0
+#ifdef rlogColorEnable
     len += snprintf(dbgBuffer + len, sizeof(dbgBuffer) - len, "\033[0m");
 #endif
 
@@ -71,7 +64,7 @@ static void rlog_output(char *lvl, uint8_t color_n, char *const fmt, ...)
 
 static void rlog_output_raw(char *const fmt, ...)
 {
-    char dbgBuffer[256];
+    char dbgBuffer[160];
     uint16_t len;
 
     va_list args;
@@ -115,8 +108,6 @@ static void rlog_output_raw(char *const fmt, ...)
 #define rlog_e(...)
 #endif
 
-#define log_d rlog_d
-
 #define rlog_raw(...) rlog_output_raw(__VA_ARGS__)
 
 #endif

+ 32 - 21
example/RyanW5500Test.c

@@ -3,20 +3,27 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <string.h>
-#include <errno.h>
 
-#include <board.h>
+#include <sys/socket.h>
+#include <sys/errno.h>
+#include <netdb.h>
+
 #include <rtthread.h>
 #include <rtdevice.h>
 #include <rtdbg.h>
+#include <board.h>
+
 #include "netdev_ipaddr.h"
 #include "netdev.h"
-#include "netdb.h"
-#include "sys/socket.h"
-#include "sal_socket.h"
-#include "sal_netdb.h"
+
+#include "drv_gpio.h"
 #include "drv_spi.h"
 
+#define rlogEnable              // 是否使能日志
+#define rlogColorEnable         // 是否使能日志颜色
+#define rlogLevel (rlogLvlInfo) // 日志打印等级
+#define rlogTag "W5500Test"     // 日志tag
+
 #include "RyanW5500.h"
 #include "RyanW5500Log.h"
 
@@ -26,12 +33,11 @@ static struct netdev *RyanNetdev = NULL;
 
 static void neDevStatusChangeCallback(struct netdev *netdev, enum netdev_cb_type type)
 {
-    rlog_i("w5500 nedev state: %d, threadName: %s", type, rt_thread_self()->name);
+    rlog_i("w5500 nedev state: %d", type);
 }
 
 static int w5500Start(int argc, char *argv[])
 {
-
     if (NULL != RyanNetdev)
     {
         rlog_w("w5500已经启动,不要重复选择");
@@ -45,7 +51,7 @@ static int w5500Start(int argc, char *argv[])
     // mac地址首位偶数单播,首位奇数为多播地址,多播作为设备地址是无效(第48bit 0 单播, 1 多播)
     // 广播mac地址:FF-FF-FF-FF-FF-FF
     // 第一个字节一般为00
-    uint8_t myMac[6] = {0x00, 0x08, 0xdc, 0x2f, 0x0c, 0x37};
+    uint8_t myMac[6] = {0x00, 0x08, 0xdc, 0x2f, 0x0c, 0x39};
 
     // stm32可以使用唯一96Bit芯片序列号
     // myMac[3] = *(uint8_t *)(UID_BASE + 0);
@@ -73,8 +79,8 @@ static int w5500Start(int argc, char *argv[])
     inet_pton(AF_INET, "192.168.1.1", &ipStrArr);
     memcpy(netInfo.gw, ipStrArr, 4);
 
-    // inet_pton(AF_INET, "192.168.1.1", &ipStrArr);
-    inet_pton(AF_INET, "114.114.114.114", &ipStrArr);
+    inet_pton(AF_INET, "192.168.1.1", &ipStrArr);
+    // inet_pton(AF_INET, "114.114.114.114", &ipStrArr);
     memcpy(netInfo.dns, ipStrArr, 4);
 
     netInfo.dhcp = NETINFO_DHCP; // 使能dhcp
@@ -307,7 +313,7 @@ static int w5500Static(int argc, char *argv[])
     netdev_dhcp_enabled(RyanNetdev, RT_FALSE);
 
     //  设置网卡 IP 地址
-    uint32_t addr = inet_addr("192.168.8.69");
+    uint32_t addr = inet_addr("192.168.3.69");
     netdev_set_ipaddr(RyanNetdev, (const ip_addr_t *)&addr);
 
     addr = inet_addr("192.168.1.1");
@@ -796,15 +802,20 @@ static int RyanW5500Msh(int argc, char *argv[])
 // stm32用户需要更改此代码为自己w5500实际挂载的spi总线
 // 非stm32用户可以调用rt_spi_bus_attach_device,
 // 参考连接:https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/spi/spi?id=%e6%8c%82%e8%bd%bd-spi-%e8%ae%be%e5%a4%87
-// static int RyanW5500SpiArrach(void)
-//{
-//    rt_err_t result = rt_hw_spi_device_attach("spi2", RYANW5500_SPI_DEVICE, GPIOE, GPIO_PIN_15);
-//    if (RT_EOK != result)
-//        rt_kprintf("RyanW5500 SPI init fail!!!!!");
-//
-//    return result;
-//}
-// INIT_DEVICE_EXPORT(RyanW5500SpiArrach); // spi总线挂载
+static int RyanW5500SpiArrach(void)
+{
+#if (RT_VER_NUM >= 50000)
+    rt_err_t result = rt_hw_spi_device_attach("spi1", RYANW5500_SPI_DEVICE, GET_PIN(A, 7));
+#else
+    rt_err_t result = rt_hw_spi_device_attach("spi1", RYANW5500_SPI_DEVICE, GPIOA, GPIO_PIN_7);
+#endif
+
+    if (RT_EOK != result)
+        rt_kprintf("RyanW5500 SPI init fail!!!!!");
+
+    return result;
+}
+INIT_DEVICE_EXPORT(RyanW5500SpiArrach); // spi总线挂载
 
 #if defined(RT_USING_MSH)
 MSH_CMD_EXPORT_ALIAS(RyanW5500Msh, w5500, RyanW5500 command);