فهرست منبع

add:pins.setup

alienwalker 11 ماه پیش
والد
کامیت
26ac42679b
2فایلهای تغییر یافته به همراه284 افزوده شده و 12 حذف شده
  1. 11 1
      luat/include/luat_pin.h
  2. 273 11
      luat/modules/luat_lib_pins.c

+ 11 - 1
luat/include/luat_pin.h

@@ -41,6 +41,8 @@ typedef enum
 	LUAT_PIN_SDIO_QTY,
 
 	LUAT_PIN_ONLY_ONE_QTY = 1,
+	LUAT_PIN_FUNCTION_MAX = LUAT_PIN_SDIO_QTY,
+	LUAT_PIN_ALT_FUNCTION_MAX = 8,
 }LUAT_PIN_FUNC_E;
 
 typedef struct
@@ -63,7 +65,7 @@ typedef union
 
 typedef struct
 {
-	uint16_t function_code[8];
+	uint16_t function_code[LUAT_PIN_ALT_FUNCTION_MAX];
 	uint16_t index;
 	uint8_t uid;
 }luat_pin_function_description_t;
@@ -133,6 +135,14 @@ int luat_pin_set_iomux_info(LUAT_MCU_PERIPHERAL_E type, uint8_t id, luat_pin_iom
  */
 int luat_pin_get_description_from_num(uint32_t num, luat_pin_function_description_t *pin_function);
 
+/**
+ * @brief 从芯片pin功能的详细描述找出所需功能的altfun_id
+ * @param code 功能id
+ * @param pin_function 芯片pin功能的详细描述
+ * @return 0xff失败,其他成功
+ */
+uint8_t luat_pin_get_altfun_id_from_description(uint16_t code, luat_pin_function_description_t *pin_function);
+
 void luat_pin_iomux_config(luat_pin_iomux_info pin, uint8_t use_altfunction_pull, uint8_t driver_strength);
 
 void luat_pin_iomux_print(luat_pin_iomux_info *pin_list, uint8_t num);

+ 273 - 11
luat/modules/luat_lib_pins.c

@@ -3,7 +3,7 @@
 @summary 管脚外设复用
 @version 1.0
 @date    2025.4.10
-@tag LUAT_USE_PINS
+@tag
 @usage
 
 */
@@ -16,19 +16,33 @@
 #define LUAT_LOG_TAG "pins"
 #include "luat_log.h"
 
-static uint16_t luat_pins_function_analyze(char *string)
+static int search(char *string, size_t string_len, const char **table, uint8_t len)
 {
+	for(int i = 0; i < len; i++)
+	{
+		if (strnstr(string, table[i], string_len))
+		{
+			return i;
+		}
+	}
+	return -1;
+}
+
+static luat_pin_peripheral_function_description_u luat_pin_function_analyze(char *string, size_t len)
+{
+	luat_pin_peripheral_function_description_u description;
+	size_t offset = 0;
 	const char *peripheral_names[LUAT_MCU_PERIPHERAL_QTY] = {
 			"UART","I2C","SPI","PWM","CAN","GPIO","I2S","SDIO","LCD","CAMERA","ONEWIRE","KEYBORAD"
 	};
 	const char *function0_names[3] = {
-			"RX","SCL","MOSI",
+			"RX","SCL","MOSI"
 	};
 	const char *function1_names[3] = {
-			"TX","SDA","MISO",
+			"TX","SDA","MISO"
 	};
 	const char *function2_names[4] = {
-			"RTS","CLK","STB","BCLK"
+			"RTS","SCLK","STB","BCLK"
 	};
 	const char *function3_names[3] = {
 			"CTS","CS","LRCLK"
@@ -36,7 +50,206 @@ static uint16_t luat_pins_function_analyze(char *string)
 	const char *function4_names[2] = {
 			"MCLK","CMD"
 	};
-	return 0xffff;
+	const char *function5_names[1] = {
+			"SCLK"
+	};
+	description.code = 0;
+	for(description.peripheral_type = 0; description.peripheral_type < LUAT_MCU_PERIPHERAL_QTY; description.peripheral_type++)
+	{
+		offset = strlen(peripheral_names[description.peripheral_type]);
+		if (!memcmp(string, peripheral_names[description.peripheral_type], offset))
+		{
+			int function_id;
+			description.peripheral_id = 0;
+			string += offset;
+			len -= offset;
+			while(isdigit(string[0]))
+			{
+				description.peripheral_id = description.peripheral_id * 10 + (string[0] - '0');
+				string++;
+				len--;
+			}
+			switch(description.peripheral_type)
+			{
+			case LUAT_MCU_PERIPHERAL_UART:
+				function_id = search(string, len, function0_names, sizeof(function0_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 0;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function1_names, sizeof(function1_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 1;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function2_names, sizeof(function2_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 2;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function3_names, sizeof(function3_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 3;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				break;
+			case LUAT_MCU_PERIPHERAL_I2C:
+				function_id = search(string, len, function0_names, sizeof(function0_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 0;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function1_names, sizeof(function1_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 1;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				break;
+			case LUAT_MCU_PERIPHERAL_SPI:
+				function_id = search(string, len, function0_names, sizeof(function0_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 0;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function1_names, sizeof(function1_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 1;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function2_names, sizeof(function2_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 2;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function3_names, sizeof(function3_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 3;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				break;
+			case LUAT_MCU_PERIPHERAL_PWM:
+				if (!string[0])
+				{
+					description.function_id = 0;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				if (string[0] == 'n')
+				{
+					description.function_id = 1;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				break;
+			case LUAT_MCU_PERIPHERAL_CAN:
+				function_id = search(string, len, function0_names, sizeof(function0_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 0;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function1_names, sizeof(function1_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 1;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function2_names, sizeof(function2_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 2;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				break;
+			case LUAT_MCU_PERIPHERAL_GPIO:
+				if (!string[0])
+				{
+					description.function_id = 0;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				break;
+			case LUAT_MCU_PERIPHERAL_I2S:
+				function_id = search(string, len, function0_names, sizeof(function0_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 0;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function1_names, sizeof(function1_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 1;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function2_names, sizeof(function2_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 2;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function3_names, sizeof(function3_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 3;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function4_names, sizeof(function4_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 4;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				break;
+			case LUAT_MCU_PERIPHERAL_SDIO:
+				if (strnstr(string, "_DATA", len))
+				{
+					if (string[5] >= '0' && string[5] <= '3')
+					{
+						function_id = string[5] - '0';
+					}
+					break;
+				}
+				function_id = search(string, len, function4_names, sizeof(function4_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 4;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				function_id = search(string, len, function5_names, sizeof(function5_names)/4);
+				if (function_id >= 0)
+				{
+					description.function_id = 5;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				break;
+			case LUAT_MCU_PERIPHERAL_ONEWIRE:
+				if (!string[0])
+				{
+					description.function_id = 0;
+					goto LUAT_PIN_FUNCTION_ANALYZE_DONE;
+				}
+				break;
+			default:
+				break;
+			}
+		}
+	}
+	LLOGE("%.*s不是可配置的外设功能", len, string);
+	description.code = 0xffff;
+LUAT_PIN_FUNCTION_ANALYZE_DONE:
+	if (!description.is_no_use)
+	{
+		LLOGD("%.*s find %d,%d,%d", len, string, description.peripheral_type, description.peripheral_id, description.function_id);
+	}
+	return description;
 }
 
 /**
@@ -44,7 +257,7 @@ static uint16_t luat_pins_function_analyze(char *string)
 @api pins.setup(pin, func)
 @int 管脚物理编号, 对应模组俯视图下的顺序编号, 例如 67, 68
 @string 功能说明, 例如 "GPIO18", "UART1_TX", "UART1_RX", "SPI1_CLK", "I2C1_CLK", 目前支持的外设有"UART","I2C","SPI","PWM","CAN","GPIO","ONEWIRE"
-@return boolean 配置成功,返回true, 其他情况均返回nil, 并在日志中提示失败原因
+@return boolean 配置成功,返回true, 其他情况均返回false, 并在日志中提示失败原因
 @usage
 -- 把air780epm的PIN67脚,做GPIO 18用
 --pins.setup(67, "GPIO18")
@@ -53,7 +266,56 @@ static uint16_t luat_pins_function_analyze(char *string)
 -- 把air780epm的PIN56脚,做uart2 tx用
 --pins.setup(56, "UART2_TX")
  */
-static int luat_pins_setup(lua_State *L){
+static int luat_pin_setup(lua_State *L){
+    size_t len;
+    int pin = 0;
+    int result;
+    const char* func_name = luaL_checklstring(L, 2, &len);
+    pin = luaL_optinteger(L, 1, -1);
+    if (pin < 0)
+    {
+    	LLOGE("pin序号参数错误");
+    	goto LUAT_PIN_SETUP_DONE;
+    }
+    else
+    {
+    	if (len < 2)
+    	{
+    		LLOGE("%.*s外设功能描述不正确", len, func_name);
+    		goto LUAT_PIN_SETUP_DONE;
+    	}
+    	luat_pin_function_description_t pin_description;
+    	if (luat_pin_get_description_from_num(pin, &pin_description))
+    	{
+        	LLOGE("pin%d不支持修改", pin);
+        	goto LUAT_PIN_SETUP_DONE;
+    	}
+    	luat_pin_peripheral_function_description_u func_description = luat_pin_function_analyze(func_name, len);
+    	if (func_description.code == 0xffff)
+    	{
+    		goto LUAT_PIN_SETUP_DONE;
+    	}
+    	uint8_t altfun_id = luat_pin_get_altfun_id_from_description(func_description.code, &pin_description);
+    	if (altfun_id == 0xff)
+    	{
+    		LLOGE("%.*s不是pin%d的可配置功能", len, func_name, pin);
+    		goto LUAT_PIN_SETUP_DONE;
+    	}
+    	luat_pin_iomux_info pin_list[LUAT_PIN_FUNCTION_MAX] = {0};
+    	if (!luat_pin_get_iomux_info(func_description.peripheral_type, func_description.peripheral_id, pin_list))
+    	{
+    		pin_list[func_description.function_id].altfun_id = altfun_id;
+    		pin_list[func_description.function_id].uid = pin_description.uid;
+    		if (!luat_pin_set_iomux_info(func_description.peripheral_type, func_description.peripheral_id, pin_list))
+    		{
+    			result = 1;
+    			goto LUAT_PIN_SETUP_DONE;
+    		}
+    	}
+    	LLOGE("%.*s不可配置,请确认硬件手册上该功能可以复用在2个及以上的pin", len, func_name);
+    }
+LUAT_PIN_SETUP_DONE:
+	lua_pushboolean(L, result);
 	return 1;
 }
 
@@ -66,15 +328,15 @@ static int luat_pins_setup(lua_State *L){
 pins.load()
 pins.load("/my.json")
 */
-static int luat_pins_load(lua_State *L){
+static int luat_pin_load(lua_State *L){
 	return 1;
 }
 
 #include "rotable2.h"
 static const rotable_Reg_t reg_pins[] =
 {
-    {"setup",     ROREG_FUNC(luat_pins_setup)},
-	{"load",     ROREG_FUNC(luat_pins_load)},
+    {"setup",     ROREG_FUNC(luat_pin_setup)},
+	{"load",     ROREG_FUNC(luat_pin_load)},
 	{ NULL,     ROREG_INT(0) }
 };