您尚未登录。

楼主 #1 2020-07-24 07:20:54

sassacontrol
会员
注册时间: 2020-07-11
已发帖子: 7
积分: 2

Xboot Lua 硬件示例 F1c100s

我正在寻找一些交流的例子 Uart (serial) 适用于 Xboot 在 Lua


试试这个链接,但是没有用 https://whycan.cn/t_4894.html

离线

#2 2020-07-24 07:30:55

有梦的地方
会员
注册时间: 2020-03-17
已发帖子: 284
积分: 284

Re: Xboot Lua 硬件示例 F1c100s

上面是 xboot 作者回复的,怎么可能没有用?

现在是什么问题呢?

离线

楼主 #3 2020-07-24 07:42:34

sassacontrol
会员
注册时间: 2020-07-11
已发帖子: 7
积分: 2

Re: Xboot Lua 硬件示例 F1c100s

我不知道我要去哪裡哪裡,我把這段代碼保存為“ main.lua”,在Uart1或Uart2中我沒有任何答案。

僅在Uart0上進行Xboot調試

离线

#4 2020-07-24 07:54:40

有梦的地方
会员
注册时间: 2020-03-17
已发帖子: 284
积分: 284

Re: Xboot Lua 硬件示例 F1c100s

首先,看你有没有使能uart1? GPIO复用是否正确?

离线

楼主 #5 2020-07-24 08:03:45

sassacontrol
会员
注册时间: 2020-07-11
已发帖子: 7
积分: 2

Re: Xboot Lua 硬件示例 F1c100s

我沒有做任何更改,我以為只是編譯和使用。

如何“解僱” Uart1?

离线

#6 2020-07-24 09:31:31

xboot
会员
注册时间: 2019-10-15
已发帖子: 692
积分: 434

Re: Xboot Lua 硬件示例 F1c100s

"uart-16550@0x01c25000": {
        "clock-name": "link-uart0",
        "reset": 84,
        "txd-gpio": 129,
        "txd-gpio-config": 5,
        "rxd-gpio": 128,
        "rxd-gpio-config": 5,
        "baud-rates": 115200,
        "data-bits": 8,
        "parity-bits": 0,
        "stop-bits": 1
    },

    "uart-16550@0x01c25400": {
        "clock-name": "link-uart1",
        "reset": 85,
        "txd-gpio": 3,
        "txd-gpio-config": 5,
        "rxd-gpio": 2,
        "rxd-gpio-config": 5,
        "baud-rates": 115200,
        "data-bits": 8,
        "parity-bits": 0,
        "stop-bits": 1
    },

    "uart-16550@0x01c25800": {
        "clock-name": "link-uart2",
        "reset": 86,
        "txd-gpio": 135,
        "txd-gpio-config": 3,
        "rxd-gpio": 136,
        "rxd-gpio-config": 3,
        "baud-rates": 115200,
        "data-bits": 8,
        "parity-bits": 0,
        "stop-bits": 1
    },

F1C100S有三个串口,具体使用哪一个pin脚,可以查看具体的gpio编号,注册成功后,系统里会存在三个串口设备,名字分别为uart-16550.0, uart-16550.1,uart-16550.2,使用串口的话,就是先获取对象,然后调用操作这个对象的方法,lua接口仅仅是接口的封装,可以直接查看接口源码:

static int l_uart_new(lua_State * L)
{
    const char * name = luaL_checkstring(L, 1);
    struct uart_t * uart = search_uart(name);
    if(!uart)
        return 0;
    if(lua_gettop(L) > 1)
    {
        int baud = luaL_optinteger(L, 2, 115200);
        int data = luaL_optinteger(L, 3, 8);
        int parity = luaL_optinteger(L, 4, 0);
        int stop = luaL_optinteger(L, 5, 1);
        uart_set(uart, baud, data, parity, stop);
    }
    lua_pushlightuserdata(L, uart);
    luaL_setmetatable(L, MT_HARDWARE_UART);
    return 1;
}

static int l_uart_list(lua_State * L)
{
    struct device_t * pos, * n;
    struct uart_t * uart;

    lua_newtable(L);
    list_for_each_entry_safe(pos, n, &__device_head[DEVICE_TYPE_UART], head)
    {
        uart = (struct uart_t *)(pos->priv);
        if(!uart)
            continue;
        lua_pushlightuserdata(L, uart);
        luaL_setmetatable(L, MT_HARDWARE_UART);
        lua_setfield(L, -2, pos->name);
    }
    return 1;
}

static const luaL_Reg l_uart[] = {
    {"new",        l_uart_new},
    {"list",    l_uart_list},
    {NULL,    NULL}
};

static int m_uart_tostring(lua_State * L)
{
    struct uart_t * uart = luaL_checkudata(L, 1, MT_HARDWARE_UART);
    lua_pushstring(L, uart->name);
    return 1;
}

static int m_uart_set(lua_State * L)
{
    struct uart_t * uart = luaL_checkudata(L, 1, MT_HARDWARE_UART);
    int baud = luaL_optinteger(L, 2, 115200);
    int data = luaL_optinteger(L, 3, 8);
    int parity = luaL_optinteger(L, 4, 0);
    int stop = luaL_optinteger(L, 5, 1);
    uart_set(uart, baud, data, parity, stop);
    lua_settop(L, 1);
    return 1;
}

static int m_uart_get(lua_State * L)
{
    struct uart_t * uart = luaL_checkudata(L, 1, MT_HARDWARE_UART);
    int baud, data, parity, stop;
    uart_get(uart, &baud, &data, &parity, &stop);
    lua_pushinteger(L, baud);
    lua_pushinteger(L, data);
    lua_pushinteger(L, parity);
    lua_pushinteger(L, stop);
    return 4;
}

static int m_uart_read(lua_State * L)
{
    struct uart_t * uart = luaL_checkudata(L, 1, MT_HARDWARE_UART);
    size_t count = luaL_checkinteger(L, 2);
    if(count <= 0)
    {
        lua_pushnil(L);
    }
    else if(count <= SZ_4K)
    {
        char buf[SZ_4K];
        if(uart_read(uart, (u8_t *)buf, count) == count)
            lua_pushlstring(L, buf, count);
        else
            lua_pushnil(L);
    }
    else
    {
        char * p = malloc(count);
        if(p && uart_read(uart, (u8_t *)p, count) == count)
            lua_pushlstring(L, p, count);
        else
            lua_pushnil(L);
        free(p);
    }
    return 1;
}

static int m_uart_write(lua_State * L)
{
    struct uart_t * uart = luaL_checkudata(L, 1, MT_HARDWARE_UART);
    size_t count;
    const char * buf = luaL_checklstring(L, 2, &count);
    if(count > 0)
        lua_pushboolean(L, (uart_write(uart, (const u8_t *)buf, count) == count));
    else
        lua_pushboolean(L, 0);
    return 1;
}

static const luaL_Reg m_uart[] = {
    {"__tostring",    m_uart_tostring},
    {"set",            m_uart_set},
    {"get",            m_uart_get},
    {"read",        m_uart_read},
    {"write",        m_uart_write},
    {NULL,    NULL}
};

int luaopen_hardware_uart(lua_State * L)
{
    luaL_newlib(L, l_uart);
    luahelper_create_metatable(L, MT_HARDWARE_UART, m_uart);
    return 1;
}

按照这个流程跟踪下去,就可以了解所有细节了,xboot整个抽象层是很薄的,不会有叠罗汉现象,花点时间就可以理解透的。

离线

楼主 #7 2020-07-25 00:14:36

sassacontrol
会员
注册时间: 2020-07-11
已发帖子: 7
积分: 2

Re: Xboot Lua 硬件示例 F1c100s

我相信我的Lichee Nano遇到麻煩了。


"uart-16550@0x01c25000": {
        "clock-name": "link-uart0",
        "reset": 84,
        "txd-gpio": 129,
        "txd-gpio-config": 5,
        "rxd-gpio": 128,
        "rxd-gpio-config": 5,
        "baud-rates": 115200,
        "data-bits": 8,
        "parity-bits": 0,
        "stop-bits": 1
    },

    "uart-16550@0x01c25400": {
        "clock-name": "link-uart1",
        "reset": 85,
        "txd-gpio": 3,
        "txd-gpio-config": 5,
        "rxd-gpio": 2,
        "rxd-gpio-config": 5,
        "baud-rates": 115200,
        "data-bits": 8,
        "parity-bits": 0,
        "stop-bits": 1
    },

    "uart-16550@0x01c25800": {
        "clock-name": "link-uart2",
        "reset": 86,
        "txd-gpio": 135,
        "txd-gpio-config": 3,
        "rxd-gpio": 136,
        "rxd-gpio-config": 3,
        "baud-rates": 115200,
        "data-bits": 8,
        "parity-bits": 0,
        "stop-bits": 1
    },


一切都好



我嘗試了不同的組合 Lua:

-----------------------------------------

local uart1 = Uart.new("uart-16550.1", 115200, 8, 0, 1)

uart1.write("Hello World")


------------------------------------------


local uart1 = Uart.new("uart1", 115200, 8, 0, 1)

uart1.write("Hello World")


------------------------------------------


local uart1 = Uart.new("link-uart1", 115200, 8, 0, 1)

uart1.write("Hello World")


------------------------------------------


local uart1 = Uart.new("uart-16550@0x01c25400", 115200, 8, 0, 1)

uart1.write("Hello World")


E etc...


我想讓串行通訊(Uart)在 Lua no Xboot

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn