浏览代码

add: 添加win32下的gpio和spi模拟实现,这样就能无顾忌的测试文件系统的代码了

Wendal Chen 4 年之前
父节点
当前提交
9d4747f152

+ 2 - 0
bsp/win32/CMakeLists.txt

@@ -50,6 +50,8 @@ add_library(luat ${TOPROOT}/luat/modules/luat_main.c
                  ${TOPROOT}/luat/modules/luat_lib_rtos.c
                  ${TOPROOT}/luat/modules/luat_lib_timer.c
                  ${TOPROOT}/luat/modules/luat_lib_log.c
+                 ${TOPROOT}/luat/modules/luat_lib_gpio.c
+                 ${TOPROOT}/luat/modules/luat_lib_spi.c
                  ${TOPROOT}/luat/modules/luat_lib_pack.c
                  ${TOPROOT}/luat/modules/luat_lib_zbuff.c
                  ${TOPROOT}/luat/modules/luat_lib_mqttcore.c

+ 1 - 0
bsp/win32/port/luat_base_win32.c

@@ -39,6 +39,7 @@ static const luaL_Reg loadedlibs[] = {
   {"fatfs", luaopen_fatfs},
   {"sfd",   luaopen_sfd},
   {"lfs2",   luaopen_lfs2},
+  {"gpio",   luaopen_gpio},
   {NULL, NULL}
 };
 

+ 66 - 0
bsp/win32/port/luat_gpio_win32.c

@@ -0,0 +1,66 @@
+#include "luat_base.h"
+#include "luat_gpio.h"
+#include "luat_msgbus.h"
+
+// 模拟GPIO在win32下的实现
+
+#define LUAT_WIN32_GPIO_COUNT (32)
+
+typedef struct gpio_state {
+    luat_gpio_t gpio;
+    uint8_t open;
+    uint8_t state;
+}gpio_state_t;
+
+gpio_state_t win32gpios[LUAT_WIN32_GPIO_COUNT] = {0};
+
+int luat_gpio_setup(luat_gpio_t* gpio) {
+    if (gpio->pin < 0 || gpio->pin >= LUAT_WIN32_GPIO_COUNT) {
+        return -1;
+    }
+    memcpy(&win32gpios[gpio->pin], gpio, sizeof(luat_gpio_t));
+    win32gpios[gpio->pin].open = 1;
+    if (gpio->mode == Luat_GPIO_OUTPUT) {
+        win32gpios[gpio->pin].state = gpio->irq;
+    }
+    else {
+        win32gpios[gpio->pin].state = 0;
+    }
+    return 0;
+}
+
+int luat_gpio_set(int pin, int level) {
+    if (pin < 0 || pin >= LUAT_WIN32_GPIO_COUNT) {
+        return -1;
+    }
+    if (win32gpios[pin].open == 0) {
+        return -1;
+    }
+    
+    if (win32gpios[pin].state != level) {
+        win32gpios[pin].state = level;
+        rtos_msg_t msg = {0};
+        msg.ptr = NULL;
+        msg.arg1 = pin;
+        msg.arg2 = level;
+        msg.handler = l_gpio_handler;
+        luat_msgbus_put(&msg, 0);
+    };
+    return 0;
+}
+
+int luat_gpio_get(int pin) {
+    if (pin < 0 || pin >= LUAT_WIN32_GPIO_COUNT) {
+        return 0;
+    }
+    if (win32gpios[pin].open == 0) {
+        return 0;
+    }
+    return win32gpios[pin].state;
+}
+void luat_gpio_close(int pin) {
+    if (pin < 0 || pin >= LUAT_WIN32_GPIO_COUNT) {
+        return;
+    }
+    win32gpios[pin].open = 0;
+}

+ 63 - 0
bsp/win32/port/luat_spi_win32.c

@@ -0,0 +1,63 @@
+
+#include "luat_base.h"
+#include "luat_gpio.h"
+#include "luat_msgbus.h"
+#include "luat_spi.h"
+
+// 模拟SPI在win32下的实现
+// TODO 当需要返回数据时, 调用lua方法获取需要返回的数据
+
+#define LUAT_WIN32_SPI_COUNT (3)
+
+typedef struct win32spi {
+    luat_spi_t spi;
+    uint8_t open;
+}win32spi_t;
+
+win32spi_t win32spis[LUAT_WIN32_SPI_COUNT] = {0};
+
+int luat_spi_setup(luat_spi_t* spi) {
+    if (spi->id < 0 || spi->id >= LUAT_WIN32_SPI_COUNT) {
+        return -1;
+    }
+    memcpy(&win32spis[spi->id].spi, spi, sizeof(luat_spi_t));
+    win32spis[spi->id].open = 1;
+    return 0;
+}
+//关闭SPI,成功返回0
+int luat_spi_close(int spi_id) {
+    if (spi_id < 0 || spi_id >= LUAT_WIN32_SPI_COUNT) {
+        return -1;
+    }
+    win32spis[spi_id].open = 0;
+    return 0;
+}
+//收发SPI数据,返回接收字节数
+int luat_spi_transfer(int spi_id, const char* send_buf, char* recv_buf, size_t length) {
+    if (spi_id < 0 || spi_id >= LUAT_WIN32_SPI_COUNT) {
+        return -1;
+    }
+    if (win32spis[spi_id].open == 0)
+        return -1;
+    memset(recv_buf, 0, length);
+    return length;
+}
+//收SPI数据,返回接收字节数
+int luat_spi_recv(int spi_id, char* recv_buf, size_t length) {
+    if (spi_id < 0 || spi_id >= LUAT_WIN32_SPI_COUNT) {
+        return -1;
+    }
+    if (win32spis[spi_id].open == 0)
+        return -1;
+    memset(recv_buf, 0, length);
+    return length;
+}
+//发SPI数据,返回发送字节数
+int luat_spi_send(int spi_id, const char* send_buf, size_t length) {
+    if (spi_id < 0 || spi_id >= LUAT_WIN32_SPI_COUNT) {
+        return -1;
+    }
+    if (win32spis[spi_id].open == 0)
+        return -1;
+    return length;
+}

+ 0 - 4
luat/modules/luat_lib_sfd.c

@@ -11,9 +11,7 @@
 #define LUAT_LOG_SDF
 #include "luat_log.h"
 
-#ifndef LUA_USE_WINDOWS
 extern const sdf_opts_t sfd_w25q_opts;
-#endif
 extern const sdf_opts_t sfd_mem_opts;
 
 /*
@@ -32,7 +30,6 @@ end
 static int l_sfd_init(lua_State *L) {
 
     const char* type = luaL_checkstring(L, 1);
-#ifndef LUA_USE_WINDOWS
     if (!strcmp("spi", type)) {
         
         int spi_id = luaL_checkinteger(L, 2);
@@ -50,7 +47,6 @@ static int l_sfd_init(lua_State *L) {
             return 1;
         }
     }
-#endif
     if (!strcmp("zbuff", type)) {
         sfd_drv_t *drv = (sfd_drv_t *)lua_newuserdata(L, sizeof(sfd_drv_t));
         memset(drv, 0, sizeof(sfd_drv_t));

+ 2 - 4
luat/modules/luat_sfd.c

@@ -12,7 +12,6 @@
 #define CS_L(pin) luat_gpio_set(pin, 0)
 
 // 针对drv的实现
-#ifndef LUA_USE_WINDOWS
 static int sfd_w25q_init (void* userdata);
 static int sfd_w25q_status (void* userdata);
 static int sfd_w25q_read (void* userdata, char* buff, size_t offset, size_t len);
@@ -81,7 +80,7 @@ static int sfd_w25q_init (void* userdata) {
     luat_gpio_set(drv->cfg.spi.cs, 0);
     char chip_id_cmd[] = {0x4B, 0x00, 0x00, 0x00, 0x00};
     luat_spi_send(drv->cfg.spi.id, chip_id_cmd, 5);
-    luat_spi_read(drv->cfg.spi.id, drv->chip_id, 8);
+    luat_spi_recv(drv->cfg.spi.id, drv->chip_id, 8);
     luat_gpio_set(drv->cfg.spi.cs, 1);
 
     return 0;
@@ -97,7 +96,7 @@ static int sfd_w25q_read (void* userdata, char* buff, size_t offset, size_t len)
     char cmd[4] = {0x03, offset >> 16, (offset >> 8) & 0xFF, offset & 0xFF};
     luat_gpio_set(drv->cfg.spi.cs, 0);
     luat_spi_send(drv->cfg.spi.id, (const char*)&cmd, 4);
-    luat_spi_read(drv->cfg.spi.id, buff, len);
+    luat_spi_recv(drv->cfg.spi.id, buff, len);
     luat_gpio_set(drv->cfg.spi.cs, 1);
     return 0;
 }
@@ -124,7 +123,6 @@ static int sfd_w25q_erase (void* userdata, size_t offset, size_t len) {
 static int sfd_w25q_ioctl (void* userdata, size_t cmd, void* buff) {
     return -1;
 }
-#endif
 
 //--------------------------------------------------------------------
 // SFD at memory ,  for test

+ 0 - 5
luat/packages/fatfs/luat_lib_fatfs.c

@@ -37,13 +37,8 @@ static int fatfs_mount(lua_State *L)
 		LLOGD("init ramdisk at FatFS");
 		diskio_open_ramdisk(0, luaL_optinteger(L, 2, 64*1024));
 	} else {
-		#ifdef LUA_USE_WINDOWS
-		LLOGE("win32/posix only support ramdisk");
-		return 0;
-		#else
 		LLOGD("init sdcard at spi=%d cs=%d", FATFS_SPI_ID, FATFS_SPI_CS);
 		diskio_open_spitf(0, FATFS_SPI_ID, FATFS_SPI_CS);
-		#endif
 	}
 
 	DRESULT re = f_mount(fs, mount_point, 0);