You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
4.0 KiB
176 lines
4.0 KiB
#include "bsp_config.h"
|
|
#ifdef USE_LUA_ST_UART
|
|
#include "lua_base.h"
|
|
#include "bsp_uart.h"
|
|
#ifdef PRINT_ID
|
|
#ifdef USE_USB
|
|
#include "bsp_usb.h"
|
|
#endif
|
|
#endif
|
|
|
|
// 串口对象结构体
|
|
typedef struct
|
|
{
|
|
TComCtrl *m_ptComCtrl;
|
|
} stm32_com_t;
|
|
|
|
TComCtrl *g_ptCom1;
|
|
TComCtrl *g_ptCom0;
|
|
|
|
int COM1_Send(char *_pBuffer, uint32_t _iSize)
|
|
{
|
|
TUartUserData *ptUartUserData = (TUartUserData *)g_ptCom1->m_pUserData;
|
|
return HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100);
|
|
}
|
|
|
|
int COM0_Send(char *_pBuffer, uint32_t _iSize)
|
|
{
|
|
TUartUserData *ptUartUserData = (TUartUserData *)g_ptCom0->m_pUserData;
|
|
return HAL_UART_Transmit(ptUartUserData->m_uart, (uint8_t *)_pBuffer, _iSize, 100);
|
|
}
|
|
|
|
void COM1_IRQHandler(void)
|
|
{
|
|
UART_DMA_RX_IRQHandler(g_ptCom1);
|
|
}
|
|
|
|
void COM0_IRQHandler(void)
|
|
{
|
|
#ifndef PRINT_ID
|
|
UART_DMA_RX_IRQHandler(g_ptCom0);
|
|
#endif
|
|
}
|
|
|
|
// 构造函数: st.COM(1, 115200)
|
|
static int l_com_new(lua_State *L)
|
|
{
|
|
int id = luaL_checkinteger(L, 1);
|
|
int baudrate = luaL_optinteger(L, 2, 9600); // 默认波特率9600
|
|
|
|
// 创建 userdata
|
|
stm32_com_t *u = (stm32_com_t*)lua_newuserdata(L, sizeof(stm32_com_t));
|
|
|
|
if (id == 1 || id == 2)
|
|
{
|
|
TUartUserData *ptUartUserData = UART_userdata_init(LUACOM_ID, baudrate, 0);
|
|
g_ptCom1 = rd_ComCreate(NULL, NULL, COM1_Send, ptUartUserData->m_buf_size, ptUartUserData);
|
|
UART_IT_init(g_ptCom1);
|
|
u->m_ptComCtrl = g_ptCom1;
|
|
}
|
|
else if (id == 0)
|
|
{
|
|
#ifdef PRINT_ID
|
|
#ifdef USE_USB
|
|
g_ptCom0 = bsp_usb_init();
|
|
#endif
|
|
#else
|
|
TUartUserData *ptUartUserData = UART_userdata_init(0, baudrate, 0);
|
|
g_ptCom0 = rd_ComCreate(NULL, NULL, COM0_Send, ptUartUserData->m_buf_size, ptUartUserData);
|
|
UART_IT_init(g_ptCom0);
|
|
#endif
|
|
u->m_ptComCtrl = g_ptCom0;
|
|
}
|
|
|
|
// 设置 Metatable
|
|
luaL_getmetatable(L, "stm32.COMMT");
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
}
|
|
|
|
// 方法: COM:write(str)
|
|
static int l_com_write(lua_State *L)
|
|
{
|
|
stm32_com_t *u = (stm32_com_t*)luaL_checkudata(L, 1, "stm32.COMMT");
|
|
size_t len;
|
|
const char *str = luaL_checklstring(L, 2, &len); // 支持二进制数据
|
|
|
|
rd_ComSend(u->m_ptComCtrl, (char *)str, len);
|
|
lua_pushinteger(L, len); // 返回发送的字节数
|
|
return 1;
|
|
}
|
|
|
|
// 方法: COM:read(mode)
|
|
static int l_com_read(lua_State *L)
|
|
{
|
|
int iRet = 1;
|
|
char ch = 0;
|
|
stm32_com_t *u = (stm32_com_t*)luaL_checkudata(L, 1, "stm32.COMMT");
|
|
int mode = luaL_optinteger(L, 2, 0); // 默认不使用16进制
|
|
|
|
char *buf = (char*)RD_MALLOC(CONFIG_UART_BUFFER_SIZE);
|
|
int iLen = 0;
|
|
if (!buf) return luaL_error(L, "Memory alloc failed");
|
|
|
|
while(iRet)
|
|
{
|
|
__disable_irq();
|
|
iRet = rd_ComRead(u->m_ptComCtrl, &ch, 1);
|
|
__enable_irq();
|
|
|
|
if (iRet > 0)
|
|
{
|
|
if (3 == mode)
|
|
{
|
|
lua_print("%02X ", ch);
|
|
}
|
|
else
|
|
{
|
|
lua_print("%c", ch);
|
|
}
|
|
if (iLen >= CONFIG_UART_BUFFER_SIZE) break;
|
|
buf[iLen++] = ch;
|
|
}
|
|
}
|
|
|
|
if (iLen > 0)
|
|
{
|
|
lua_pushlstring(L, buf, iLen);
|
|
}
|
|
else
|
|
{
|
|
lua_pushstring(L, "");
|
|
}
|
|
|
|
RD_FREE(buf);
|
|
return 1;
|
|
}
|
|
|
|
|
|
// 模块注册
|
|
int luaopen_stm32_com(lua_State *L)
|
|
{
|
|
// 1. 注册 Metatable
|
|
if (luaL_newmetatable(L, "stm32.COMMT"))
|
|
{
|
|
lua_pushvalue(L, -1);
|
|
lua_setfield(L, -2, "__index");
|
|
|
|
const luaL_Reg uart_methods[] =
|
|
{
|
|
{"write", l_com_write},
|
|
{"read", l_com_read},
|
|
{NULL, NULL}
|
|
};
|
|
const luaL_Reg *l;
|
|
for (l = uart_methods; l->name != NULL; l++)
|
|
|
|
{
|
|
lua_pushcfunction(L, l->func);
|
|
lua_setfield(L, -2, l->name);
|
|
}
|
|
}
|
|
lua_pop(L, 1);
|
|
|
|
// 2. 创建模块表
|
|
lua_newtable(L);
|
|
|
|
// 3. 注册构造函数
|
|
lua_pushcfunction(L, l_com_new);
|
|
lua_setfield(L, -2, "new");
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
#endif
|
|
|