alienwalker 1 год назад
Родитель
Сommit
d847e2f95b
3 измененных файлов с 98 добавлено и 11 удалено
  1. 58 0
      demo/can/main.lua
  2. 2 0
      luat/include/luat_can.h
  3. 38 11
      luat/modules/luat_lib_can.c

+ 58 - 0
demo/can/main.lua

@@ -0,0 +1,58 @@
+PROJECT = "candemo"
+VERSION = "1.0.0"
+sys = require("sys")
+log.style(1)
+local node_a = true   -- A节点写true, B节点写false
+local can_id = 0
+local rx_id
+local tx_id
+if node_a then          -- A/B节点区分,互相传输测试
+    rx_id = 0x12345678
+    tx_id = 0x12345677
+else
+    rx_id = 0x12345677
+    tx_id = 0x12345678
+end
+local test_cnt, test_num
+local tx_buf = zbuff.create(8)
+local function can_cb(id, cb_type, param)
+    if cb_type == can.CB_MSG then
+        log.info("有新的消息")
+        local succ, id, id_type, rtr, data = can.rx(id)
+        while succ do
+            log.info(mcu.x32(id), #data, data:toHex())
+            succ, id, id_type, rtr, data = can.rx(id)
+        end
+    end
+    if cb_type == can.CB_TX then
+        if param then
+            log.info("发送成功")
+        else
+            log.info("发送失败")
+        end
+    end
+    if cb_type == can.CB_ERR then
+        log.info("CAN错误码", mcu.x32(param))
+    end
+    if cb_type == can.CB_STATE then
+        log.info("CAN新状态", param)
+    end
+end
+
+local function can_tx_test(data)
+    if node_a then
+        log.info("node a tx")
+    else
+        log.info("node b tx")
+    end
+    can.tx(can_id, tx_id, can.EXT, false, true, tx_buf)
+end
+-- can.debug(true)
+can.init(can_id, 128)
+can.on(can_id, can_cb)
+can.timing(can_id, 1000000, 5, 4, 3, 2)
+can.node(can_id, rx_id, can.EXT)
+can.mode(can_id, can.MODE_NORMAL)   -- 一旦设置mode就开始正常工作了,此时不能再设置node,timing,filter等
+-- can.mode(can_id, can.MODE_TEST)     -- 如果只是自身测试硬件好坏,可以用测试模式来验证,如果发送成功就OK
+sys.timerLoopStart(can_tx_test, 1000)
+sys.run()

+ 2 - 0
luat/include/luat_can.h

@@ -164,4 +164,6 @@ int luat_can_close(uint8_t can_id);
  */
 int luat_can_get_state(uint8_t can_id);
 
+uint32_t luat_can_get_last_error(uint8_t can_id);
+void luat_can_set_debug(uint8_t on_off);
 #endif

+ 38 - 11
luat/modules/luat_lib_can.c

@@ -7,23 +7,25 @@
 @tag LUAT_USE_CAN
 */
 #include "luat_base.h"
-#include "luat_log.h"
 #include "luat_sys.h"
 #include "luat_msgbus.h"
 #include "luat_mem.h"
 #include "luat_can.h"
 #include "luat_zbuff.h"
 #include "c_common.h"
-
+#define LUAT_LOG_TAG "can"
+#include "luat_log.h"
 #include "rotable2.h"
 #define MAX_DEVICE_COUNT 2
 static int l_can_cb[MAX_DEVICE_COUNT];
+static uint8_t l_can_debug_flag;
 static int l_can_handler(lua_State *L, void* ptr)
 {
     (void)ptr;
     rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
     lua_pop(L, 1);
     int can_id = msg->arg1;
+    if (l_can_debug_flag) LLOGD("callback %d,%d,%x", can_id, msg->arg2, msg->ptr);
     if (l_can_cb[can_id])
     {
     	lua_geti(L, LUA_REGISTRYINDEX, l_can_cb[can_id]);
@@ -82,8 +84,10 @@ can.init()
 */
 static int l_can_init(lua_State *L)
 {
-	if (luat_can_base_init(luaL_optinteger(L, 1, 0), luaL_optinteger(L, 2, 0)))
+	int ret = luat_can_base_init(luaL_optinteger(L, 1, 0), luaL_optinteger(L, 2, 0));
+	if (ret)
 	{
+		LLOGE("init failed %d",  ret);
 		lua_pushboolean(L, 0);
 	}
 	else
@@ -150,10 +154,12 @@ can.timing(0, 25000, 9, 6, 4, 2)
 */
 static int l_can_timing(lua_State *L)
 {
-	if (luat_can_set_timing(luaL_optinteger(L, 1, 0), luaL_optinteger(L, 2, 1000000),
+	int ret = luat_can_set_timing(luaL_optinteger(L, 1, 0), luaL_optinteger(L, 2, 1000000),
 			luaL_optinteger(L, 3, 5), luaL_optinteger(L, 4, 4), luaL_optinteger(L, 5, 3),
-			luaL_optinteger(L, 6, 2)))
+			luaL_optinteger(L, 6, 2));
+	if (ret)
 	{
+		LLOGE("set timing failed %d",  ret);
 		lua_pushboolean(L, 0);
 	}
 	else
@@ -174,8 +180,10 @@ can.mode(0, CAN.MODE_NORMAL)
 */
 static int l_can_mode(lua_State *L)
 {
-	if (luat_can_set_work_mode(luaL_optinteger(L, 1, 0), luaL_optinteger(L, 2, LUAT_CAN_WORK_MODE_NORMAL)))
+	int ret = luat_can_set_work_mode(luaL_optinteger(L, 1, 0), luaL_optinteger(L, 2, LUAT_CAN_WORK_MODE_NORMAL));
+	if (ret)
 	{
+		LLOGE("set mode failed %d",  ret);
 		lua_pushboolean(L, 0);
 	}
 	else
@@ -199,8 +207,10 @@ can.node(0, 0x123, CAN.STD)
 static int l_can_node(lua_State *L)
 {
 
-	if (luat_can_set_node(luaL_optinteger(L, 1, 0), luaL_optinteger(L, 2, 0x1fffffff), luaL_optinteger(L, 3, 1)))
+	int ret = luat_can_set_node(luaL_optinteger(L, 1, 0), luaL_optinteger(L, 2, 0x1fffffff), luaL_optinteger(L, 3, 1));
+	if (ret)
 	{
+		LLOGE("set node failed %d",  ret);
 		lua_pushboolean(L, 0);
 	}
 	else
@@ -231,8 +241,10 @@ static int l_can_filter(lua_State *L)
 	uint8_t dual_mode = lua_toboolean(L,2);
     BytesPutBe32(ACR, node_id);
     BytesPutBe32(AMR, mask);
-	if (luat_can_set_filter(luaL_optinteger(L, 1, 0), dual_mode, ACR, AMR))
+    int ret = luat_can_set_filter(luaL_optinteger(L, 1, 0), dual_mode, ACR, AMR);
+	if (ret)
 	{
+		LLOGE("set filter failed %d",  ret);
 		lua_pushboolean(L, 0);
 	}
 	else
@@ -277,8 +289,8 @@ static int l_can_tx(lua_State *L)
     if(lua_isuserdata(L, 6))
     {
         luat_zbuff_t *buff = ((luat_zbuff_t *)luaL_checkudata(L, 6, LUAT_ZBUFF_TYPE));
-        len = buff->len - buff->cursor;
-        buf = (const char *)(buff->addr + buff->cursor);
+        len = buff->used;
+        buf = (const char *)(buff->addr);
     }
     else
     {
@@ -390,6 +402,21 @@ static int l_can_deinit(lua_State *L)
 	return 1;
 }
 
+/*
+CAN debug开关,打开后有更详细的打印
+@api can.debug(on_off)
+@boolean true打开,false关闭
+@return nil
+@usage
+can.debug(true)
+*/
+static int l_can_debug(lua_State *L)
+{
+	l_can_debug_flag = lua_toboolean(L, 1);
+	luat_can_set_debug(l_can_debug_flag);
+	return 0;
+}
+
 static const rotable_Reg_t reg_can[] =
 {
     { "init",			ROREG_FUNC(l_can_init)},
@@ -404,7 +431,7 @@ static const rotable_Reg_t reg_can[] =
 	{ "stop", 			ROREG_FUNC(l_can_stop)},
 	{ "reset",			ROREG_FUNC(l_can_reset)},
 	{ "deinit",			ROREG_FUNC(l_can_deinit)},
-
+	{ "debug",			ROREG_FUNC(l_can_debug)},
 	//@const MODE_NORMAL number 正常工作模式
     { "MODE_NORMAL",        ROREG_INT(LUAT_CAN_WORK_MODE_NORMAL)},
 	//@const MODE_LISTEN number 监听模式