Răsfoiți Sursa

add:CAN获取时钟特性API,用于计算实际波特率
update:780EPM平台的CAN参数变更

alienwalker 9 luni în urmă
părinte
comite
4603b64429
3 a modificat fișierele cu 58 adăugiri și 14 ștergeri
  1. 2 1
      demo/can/main.lua
  2. 17 0
      luat/include/luat_can.h
  3. 39 13
      luat/modules/luat_lib_can.c

+ 2 - 1
demo/can/main.lua

@@ -60,7 +60,8 @@ gpio.setup(stb_pin,0)
 -- gpio.setup(stb_pin,1)	-- 如果开发板上STB信号有逻辑取反,则要配置成输出高电平
 can.init(can_id, 128)
 can.on(can_id, can_cb)
-can.timing(can_id, 1000000, 5, 4, 3, 2)
+can.timing(can_id, 1000000, 6, 6, 4, 2)
+-- can.timing(can_id, 100000, 6, 6, 3, 2)
 if SELF_TEST_FLAG then
 	can.node(can_id, tx_id, can.EXT)	-- 测试模式下,允许接收的ID和发送ID一致才会有新数据提醒
 	can.mode(can_id, can.MODE_TEST)     -- 如果只是自身测试硬件好坏,可以用测试模式来验证,如果发送成功就OK

+ 17 - 0
luat/include/luat_can.h

@@ -178,6 +178,23 @@ int luat_can_set_stb_io_level(uint8_t can_id, uint8_t on_off);
  */
 int luat_can_bus_off(uint8_t can_id);
 
+/**
+ * @brief 获取CAN控制器的时钟特性
+ * @param can_id 总线序号
+ * @param clk 基础时钟
+ * @param div_min 分频系数最小值
+ * @param div_max 分频系数最大值
+ * @param div_step 分频系数步进值
+ * @return 0成功,其他失败
+ */
+int luat_can_get_capacity(uint8_t can_id, uint32_t *clk, uint32_t *div_min, uint32_t *div_max, uint32_t *div_step);
+
+
 uint32_t luat_can_get_last_error(uint8_t can_id);
+/**
+ * @brief 设置CAN的debug状态
+ * @param on_off 0关闭,1开启
+ * @return 无
+ */
 void luat_can_set_debug(uint8_t on_off);
 #endif

+ 39 - 13
luat/modules/luat_lib_can.c

@@ -166,25 +166,20 @@ CAN总线配置时序
 @api can.timing(id, br, PTS, PBS1, PBS2, SJW)
 @int id, 如果只有一条总线写0或者留空, 有多条的,can0写0,can1写1, 如此类推, 一般情况只有1条
 @int br, 波特率, 默认1Mbps
-@int PTS, 传播时间段, 范围1~8,默认5
-@int PBS1, 相位缓冲段1,范围1~8,默认4
-@int PBS2, 相位缓冲段2,范围2~8,默认3
+@int PTS, 传播时间段, 范围1~8
+@int PBS1, 相位缓冲段1,范围1~8
+@int PBS2, 相位缓冲段2,范围2~8
 @int SJW, 同步补偿宽度值,范围1~4,默认2
 @return boolean 成功返回true,失败返回false
 @usage
-can.timing(0, 1000000, 5, 4, 3, 2)
-can.timing(0, 650000, 9, 6, 4, 2)
-can.timing(0, 500000, 5, 4, 3, 2)
-can.timing(0, 250000, 5, 4, 3, 2)
-can.timing(0, 125000, 5, 4, 3, 2)
-can.timing(0, 100000, 5, 4, 3, 2)
-can.timing(0, 50000, 9, 6, 4, 2)
-can.timing(0, 25000, 9, 6, 4, 2)
+-- air780EPM参考,不一定适合其他平台,CAN的实际波特率=基础时钟/分频系数/(1+PTS+PBS1+PBS2),详见can.capacity
+can.timing(0, 100000, 6, 6, 3, 2)
+can.timing(0, 125000, 6, 6, 4, 2)
 */
 static int l_can_timing(lua_State *L)
 {
 	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, 3, 6), luaL_optinteger(L, 4, 6), luaL_optinteger(L, 5, 4),
 			luaL_optinteger(L, 6, 2));
 	if (ret)
 	{
@@ -438,7 +433,7 @@ CAN完全关闭
 @int id, 如果只有一条总线写0或者留空, 有多条的,can0写0,can1写1, 如此类推, 一般情况只有1条
 @return boolean 成功返回true,失败返回false
 @usage
-can.state(0)
+can.deinit(0)
 */
 static int l_can_deinit(lua_State *L)
 {
@@ -468,6 +463,36 @@ static int l_can_debug(lua_State *L)
 	return 0;
 }
 
+/*
+获取CAN时钟特性,包括基础时钟,分频系数范围,CAN的实际波特率=基础时钟/分频系数/(1+PTS+PBS1+PBS2),从时钟特性里能看出对应平台是否能配置出需要的波特率
+@api can.capacity(id)
+@int id, 如果只有一条总线写0或者留空, 有多条的,can0写0,can1写1, 如此类推, 一般情况只有1条
+@return boolean 成功返回true,失败返回false,如果失败就不用看后面的参数了
+@return int,基础时钟
+@return int,最小分频系数
+@return int,最大分频系数
+@return int,分频系数步进,一般为1
+@usage
+local res, clk, div_min, div_max, div_step = can.capacity(0)
+*/
+static int l_can_capacity(lua_State *L)
+{
+	uint32_t clk, div_min, div_max, div_step;
+	if (luat_can_get_capacity(luaL_optinteger(L, 1, 0), &clk, &div_min, &div_max, &div_step))
+	{
+		lua_pushboolean(L, 0);
+	}
+	else
+	{
+		lua_pushboolean(L, 1);
+	}
+	lua_pushinteger(L, clk);
+	lua_pushinteger(L, div_min);
+	lua_pushinteger(L, div_max);
+	lua_pushinteger(L, div_step);
+	return 5;
+}
+
 static const rotable_Reg_t reg_can[] =
 {
     { "init",			ROREG_FUNC(l_can_init)},
@@ -484,6 +509,7 @@ static const rotable_Reg_t reg_can[] =
 	{ "busOff",			ROREG_FUNC(l_can_bus_off)},
 	{ "deinit",			ROREG_FUNC(l_can_deinit)},
 	{ "debug",			ROREG_FUNC(l_can_debug)},
+	{ "capacity",		ROREG_FUNC(l_can_capacity)},
 	//@const MODE_NORMAL number 正常工作模式
     { "MODE_NORMAL",        ROREG_INT(LUAT_CAN_WORK_MODE_NORMAL)},
 	//@const MODE_LISTEN number 监听模式