Browse Source

add: spi接口加上zbuff类型的支持

chenxuuu 4 years ago
parent
commit
5d523be3b3

+ 2 - 1
bsp/air302/src/luat_air302_base.c

@@ -46,6 +46,7 @@ static const luaL_Reg loadedlibs[] = {
   // {"eink",  luaopen_eink},              // 电子墨水屏,试验阶段
   //{"iconv", luaopen_iconv},             // 编码转换,暂不可用
   //{"fatfs",   luaopen_fatfs},             // 挂载sdcard
+  {"zbuff",luaopen_zbuff},            // zbuff库
 //------------------------------------------------------------------------
 // 联网及NBIOT特有的库
   {"socket",  luaopen_socket},            // 套接字操作
@@ -74,4 +75,4 @@ const char* luat_os_bsp(void) {
 
 // 如需调整Lua VM的内存大小, 可用实现luat_air302_vmheap_size函数
 // 默认值是72kb, 总内存有100kb左右(取决于启用的库),务必留足内存给系统本身
-size_t luat_air302_vmheap_size(void); 
+size_t luat_air302_vmheap_size(void);

+ 2 - 0
luat/include/luat_zbuff.h

@@ -3,6 +3,8 @@
 
 #include "luat_msgbus.h"
 
+#define LUAT_ZBUFF_TYPE "ZBUFF*"
+
 #define ZBUFF_SEEK_SET 0
 #define ZBUFF_SEEK_CUR 1
 #define ZBUFF_SEEK_END 2

+ 56 - 5
luat/modules/luat_lib_spi.c

@@ -11,6 +11,9 @@
 #include "luat_timer.h"
 #include "luat_malloc.h"
 #include "luat_spi.h"
+#include "luat_zbuff.h"
+
+#define LUAT_LOG_TAG "luat.spi"
 
 /**
 设置并启用SPI
@@ -25,6 +28,9 @@
 @int 主从设置, 默认主1, 可选从机0. 通常只支持主机模式
 @int 工作模式, 全双工1, 半双工0, 默认全双工
 @return int 成功返回0,否则返回其他值
+@usage
+-- 初始化spi
+spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
 */
 static int l_spi_setup(lua_State *L) {
     luat_spi_t spi_config = {0};
@@ -49,6 +55,9 @@ static int l_spi_setup(lua_State *L) {
 @api spi.close(id)
 @int SPI号,例如0
 @return int 成功返回0,否则返回其他值
+@usage
+-- 初始化spi
+spi.close(0)
 */
 static int l_spi_close(lua_State *L) {
     int id = luaL_checkinteger(L, 1);
@@ -58,16 +67,36 @@ static int l_spi_close(lua_State *L) {
 
 /**
 传输SPI数据
-@api spi.transfer(id, send_data)
+@api spi.transfer(id, send_data[, len])
 @int SPI号,例如0
-@string 待发送的数据
+@string/zbuff 待发送的数据,如果为zbuff数据,则会从对象所处的指针处开始读
+@int 可选。待发送数据的长度,默认为data长度
 @return string 读取成功返回字符串,否则返回nil
+@usage
+-- 初始化spi
+spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
+local recv = spi.transfer(0, "123")--发送123,并读取数据
+
+local buff = zbuff.create(1024, 0x33) --创建一个初值全为0x33的内存区域
+local recv = spi.transfer(0, buff)--把zbuff数据从指针开始,全发出去,并读取数据
 */
 static int l_spi_transfer(lua_State *L) {
     int id = luaL_checkinteger(L, 1);
     size_t len;
     const char* send_buff;
-    send_buff = lua_tolstring(L, 2, &len);
+    if(lua_isuserdata(L, 2)){//zbuff对象特殊处理
+        luat_zbuff *buff = ((luat_zbuff *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
+        send_buff = buff->addr+buff->cursor;
+        len = buff->len - buff->cursor;
+        LLOGD("buff:%d,%d,%p",buff->len,buff->cursor,buff);
+    }else{
+        send_buff = lua_tolstring(L, 2, &len);
+    }
+    if(lua_isinteger(L,3)){//长度参数
+        size_t len_temp = luaL_checkinteger(L,3);
+        if(len_temp < len)
+            len = len_temp;
+    }
     //长度为0时,直接返回空字符串
     if(len <= 0){
         lua_pushlstring(L,NULL,0);
@@ -92,6 +121,10 @@ static int l_spi_transfer(lua_State *L) {
 @int SPI号,例如0
 @int 数据长度
 @return string 读取成功返回字符串,否则返回nil
+@usage
+-- 初始化spi
+spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
+local recv = spi.recv(0, 4)--接收4字节数据
 */
 static int l_spi_recv(lua_State *L) {
     int id = luaL_checkinteger(L, 1);
@@ -115,15 +148,33 @@ static int l_spi_recv(lua_State *L) {
 @int SPI号,例如0
 @string 待发送的数据
 @return int 发送结果
+@usage
+-- 初始化spi
+spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
+local result = spi.send(0, "123")--发送123
+
+local buff = zbuff.create(1024, 0x33) --创建一个初值全为0x33的内存区域
+local result = spi.send(0, buff)--把zbuff数据从指针开始,全发出去
 */
 static int l_spi_send(lua_State *L) {
     int id = luaL_checkinteger(L, 1);
     size_t len;
     const char* send_buff;
-    send_buff = lua_tolstring(L, 2, &len);
+    if(lua_isuserdata(L, 2)){//zbuff对象特殊处理
+        luat_zbuff *buff = ((luat_zbuff *)luaL_checkudata(L, 2, LUAT_ZBUFF_TYPE));
+        send_buff = buff->addr+buff->cursor;
+        len = buff->len - buff->cursor;
+    }else{
+        send_buff = lua_tolstring(L, 2, &len);
+    }
+    if(lua_isinteger(L,3)){//长度参数
+        size_t len_temp = luaL_checkinteger(L,3);
+        if(len_temp < len)
+            len = len_temp;
+    }
     //长度为0时,直接返回
     if(len <= 0){
-        lua_pushinteger(L, 0);
+        lua_pushinteger(L,0);
         return 1;
     }
     lua_pushinteger(L, luat_spi_send(id, send_buff, len));

+ 0 - 2
luat/modules/luat_lib_zbuff.c

@@ -11,8 +11,6 @@
 #define LUAT_LOG_TAG "luat.zbuff"
 #include "luat_log.h"
 
-#define LUAT_ZBUFF_TYPE "ZBUFF*"
-
 #define tozbuff(L) ((luat_zbuff *)luaL_checkudata(L, 1, LUAT_ZBUFF_TYPE))
 
 //在buff对象后添加数据,返回增加的字节数