From 9eaef51835034da31bf81deaef5ff3585f84b9b3 Mon Sep 17 00:00:00 2001 From: alienwalker Date: Tue, 25 Apr 2023 12:24:37 +0800 Subject: [PATCH] =?UTF-8?q?add:air780E=E7=9A=84=E9=83=A8=E5=88=86=E5=A4=96?= =?UTF-8?q?=E8=AE=BEiomux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- luat/include/luat_i2c.h | 2 +- luat/include/luat_mcu.h | 7 +++++++ luat/include/luat_uart.h | 9 +++++++++ luat/modules/luat_lib_mcu.c | 39 +++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/luat/include/luat_i2c.h b/luat/include/luat_i2c.h index 3781588e6..2e0fc6f1c 100644 --- a/luat/include/luat_i2c.h +++ b/luat/include/luat_i2c.h @@ -14,5 +14,5 @@ int luat_i2c_read_reg(int id, int addr, int reg, uint16_t* value); int luat_i2c_transfer(int id, int addr, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len); int luat_i2c_no_block_transfer(int id, int addr, uint8_t is_read, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len, uint16_t Toms, void *CB, void *pParam); - +int luat_i2c_set_iomux(int id, uint8_t value); #endif diff --git a/luat/include/luat_mcu.h b/luat/include/luat_mcu.h index 5f678cc8f..dc3b7b34f 100644 --- a/luat/include/luat_mcu.h +++ b/luat/include/luat_mcu.h @@ -2,6 +2,13 @@ #define LUAT_MCU_H #include "luat_base.h" +enum +{ + LUAT_MCU_PERIPHERAL_UART, + LUAT_MCU_PERIPHERAL_I2C, + LUAT_MCU_PERIPHERAL_SPI, +}; + int luat_mcu_set_clk(size_t mhz); int luat_mcu_get_clk(void); diff --git a/luat/include/luat_uart.h b/luat/include/luat_uart.h index 50bd0f007..dbb17158b 100644 --- a/luat/include/luat_uart.h +++ b/luat/include/luat_uart.h @@ -155,6 +155,15 @@ typedef struct luat_uart_ctrl_param */ int luat_uart_ctrl(int uart_id, LUAT_UART_CTRL_CMD_E cmd, void* param); +/** + * @brief 串口复用函数,目前支持UART0,UART2 + * + * @param uart_id 串口id + * @param use_alt_type 如果为1,UART0,复用到GPIO16,GPIO17;UART2复用到GPIO12 GPIO13 + * @return int 0 失败,其他成功 + */ +int luat_uart_pre_setup(int uart_id, uint8_t use_alt_type); + #ifdef LUAT_USE_SOFT_UART #ifndef __BSP_COMMON_H__ #include "c_common.h" diff --git a/luat/modules/luat_lib_mcu.c b/luat/modules/luat_lib_mcu.c index 3f506358b..1af5010f3 100644 --- a/luat/modules/luat_lib_mcu.c +++ b/luat/modules/luat_lib_mcu.c @@ -230,6 +230,38 @@ static int l_mcu_set_hardfault_mode(lua_State* L) return 0; } +LUAT_WEAK int luat_uart_pre_setup(int uart_id, uint8_t use_alt_type){;} +LUAT_WEAK int luat_i2c_set_iomux(int id, uint8_t value){;} +/* +在外设打开前,将外设IO复用到非默认配置上,目前只支持Air780E的部分外设复用到其他配置,这是一个临时接口,如果后续有更合适的api,本接口将不再更新 +@api mcu.iomux(type, channel, value) +@int 外设类型,目前只有mcu.UART,mcu.I2C +@int 总线序号,0~N, +@int 新的配置,这个需要根据具体平台决定 +@usage +mcu.iomux(mcu.UART, 2, 1) -- Air780E的UART2复用到gpio12和gpio13(Air780EG默认是这个复用,不要动) +mcu.iomux(mcu.UART, 2, 2) -- Air780E的UART2复用到gpio6和gpio7 +mcu.iomux(mcu.I2C, 0, 1) -- Air780E的I2C0复用到gpio12和gpio13 +mcu.iomux(mcu.I2C, 0, 2) -- Air780E的I2C0复用到gpio16和gpio17 +mcu.iomux(mcu.I2C, 1, 1) -- Air780E的I2C1复用到gpio4和gpio5 + */ +static int l_mcu_iomux(lua_State* L) +{ + int type = luaL_optinteger(L, 1, 0xff); + int channel = luaL_optinteger(L, 2, 0xff); + int value = luaL_optinteger(L, 3, 0); + LLOGD("mcu iomux %d,%d,%d", type, channel, value); + switch(type) + { + case LUAT_MCU_PERIPHERAL_UART: + luat_uart_pre_setup(channel, value); + break; + case LUAT_MCU_PERIPHERAL_I2C: + luat_i2c_set_iomux(channel, value); + break; + } + return 0; +} #include "rotable2.h" static const rotable_Reg_t reg_mcu[] = { @@ -245,7 +277,14 @@ static const rotable_Reg_t reg_mcu[] = { "dtick64", ROREG_FUNC(l_mcu_hw_diff_tick64)}, { "setXTAL", ROREG_FUNC(l_mcu_set_xtal)}, { "hardfault", ROREG_FUNC(l_mcu_set_hardfault_mode)}, + { "iomux", ROREG_FUNC(l_mcu_iomux)}, // #endif + //@const UART number 外设类型-串口 + { "UART", ROREG_INT(LUAT_MCU_PERIPHERAL_UART) }, + //@const UART number 外设类型-I2C + { "I2C", ROREG_INT(LUAT_MCU_PERIPHERAL_I2C) }, + //@const UART number 外设类型-SPI + { "SPI", ROREG_INT(LUAT_MCU_PERIPHERAL_SPI) }, { NULL, ROREG_INT(0) } };