Skip to content

Commit

Permalink
Merge branch 'master' of https://gitee.com/openLuat/LuatOS
Browse files Browse the repository at this point in the history
  • Loading branch information
allewalker committed Jun 19, 2024
2 parents e136dc1 + fd6af68 commit dbc6eba
Show file tree
Hide file tree
Showing 23 changed files with 799 additions and 668 deletions.
11 changes: 7 additions & 4 deletions components/multimedia/luat_audio_es8311.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ static uint8_t es8311_get_mute(luat_audio_codec_conf_t* conf){

// voice_vol
static uint8_t es8311_set_voice_vol(luat_audio_codec_conf_t* conf,uint8_t vol){
if(vol < 0 || vol > 100) return -1;
if(vol > 100) return -1;
es8311_write_reg(conf,ES8311_DAC_REG32,(uint8_t)(vol * 2550 / 1000));
return 0;
}
Expand All @@ -301,7 +301,7 @@ static uint8_t es8311_get_voice_vol(luat_audio_codec_conf_t* conf){

// mic_vol
static uint8_t es8311_set_mic_vol(luat_audio_codec_conf_t* conf,uint8_t vol){
if(vol < 0 || vol > 100) return -1;
if(vol > 100) return -1;
es8311_write_reg(conf,ES8311_ADC_REG17,(uint8_t)(vol * 2550 / 1000));
return 0;
}
Expand Down Expand Up @@ -438,6 +438,7 @@ static inline void es8311_reset(luat_audio_codec_conf_t* conf){
static int es8311_codec_init(luat_audio_codec_conf_t* conf,uint8_t mode){
luat_audio_power(conf->multimedia_id,1);
luat_rtos_task_sleep(50);
luat_audio_conf_t* audio_conf = luat_audio_get_config(conf->multimedia_id);
uint8_t temp1 = es8311_read_reg(conf,ES8311_CHD1_REGFD);
uint8_t temp2 = es8311_read_reg(conf,ES8311_CHD2_REGFE);
uint8_t temp3 = es8311_read_reg(conf,ES8311_CHVER_REGFF);
Expand All @@ -458,8 +459,10 @@ static int es8311_codec_init(luat_audio_codec_conf_t* conf,uint8_t mode){

es8311_write_reg(conf,ES8311_SYSTEM_REG0B, 0x00);
es8311_write_reg(conf,ES8311_SYSTEM_REG0C, 0x00);

es8311_write_reg(conf,ES8311_SYSTEM_REG10, (0x1C*ES8311_DAC_HP_ON) + (0x60*ES8311_VDDA_VOLTAGE) + 0x03);
if (audio_conf)
es8311_write_reg(conf,ES8311_SYSTEM_REG10, (0x1C*ES8311_DAC_HP_ON) + (0x60 * (audio_conf->voltage ? ES8311_VDDA_1V8 : ES8311_VDDA_3V3)) + 0x03);
else
es8311_write_reg(conf,ES8311_SYSTEM_REG10, (0x1C*ES8311_DAC_HP_ON) + (0x60 * ES8311_VDDA_VOLTAGE) + 0x03);
es8311_write_reg(conf,ES8311_SYSTEM_REG11, 0x7F);

es8311_write_reg(conf,ES8311_CLK_MANAGER_REG01,0x3F + (ES8311_MCLK_SOURCE<<7));
Expand Down
9 changes: 9 additions & 0 deletions components/multimedia/luat_lib_multimedia_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ static int l_audio_set_output_bus(lua_State *L) {
audio_conf->codec_conf.i2s_id = luaL_checknumber(L, -1);
}
lua_pop(L, 1);
lua_pushstring(L, "voltage");
if (LUA_TNUMBER == lua_gettable(L, 3)) {
audio_conf->voltage = luaL_checknumber(L, -1);
}
lua_pop(L, 1);
}
ret |= luat_audio_init(id, 0, 0);
lua_pushboolean(L, !ret);
Expand Down Expand Up @@ -795,6 +800,10 @@ static const rotable_Reg_t reg_audio[] =
{ "BUS_I2S", ROREG_INT(LUAT_AUDIO_BUS_I2S)},
//@const BUS_SOFT_DAC number 硬件输出总线,软件模式DAC类型
{ "BUS_SOFT_DAC", ROREG_INT(LUAT_AUDIO_BUS_SOFT_DAC)},
//@const VOLTAGE_1800 number 可配置的codec工作电压,1.8V
{ "VOLTAGE_1800", ROREG_INT(LUAT_AUDIO_VOLTAGE_1800)},
//@const VOLTAGE_3300 number 可配置的codec工作电压,3.3V
{ "VOLTAGE_3300", ROREG_INT(LUAT_AUDIO_VOLTAGE_3300)},
{ NULL, ROREG_INT(0)}
};

Expand Down
8 changes: 6 additions & 2 deletions components/network/websocket/luat_lib_websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ static int l_websocket_set_debug(lua_State *L)

/*
websocket客户端创建
@api websocket.create(adapter, url, keepalive)
@int 适配器序号, 只能是socket.ETH0, socket.STA, socket.AP,如果不填,会选择平台自带的方式,然后是最后一个注册的适配器
@api websocket.create(adapter, url, keepalive, use_ipv6)
@int 适配器序号, 参考socket库的常量,默认为nil,会选择平台自带的方式
@string 连接字符串,参考usage
@int 心跳间隔,默认60秒. 2024.4.28新增
@boolean 是否使用ipv6,默认false. 2024.6.17新增
@return userdata 若成功会返回websocket客户端实例,否则返回nil
@usage
-- 普通TCP链接
Expand All @@ -218,6 +219,9 @@ static int l_websocket_create(lua_State *L)
if (lua_isinteger(L, 3)) {
opts.keepalive = luaL_checkinteger(L, 3);
}
if (lua_type(L, 4) == LUA_TBOOLEAN) {
opts.use_ipv6 = lua_toboolean(L, 4);
}

luat_websocket_ctrl_t *websocket_ctrl = (luat_websocket_ctrl_t *)lua_newuserdata(L, sizeof(luat_websocket_ctrl_t));
if (!websocket_ctrl)
Expand Down
3 changes: 3 additions & 0 deletions components/network/websocket/luat_websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ int luat_websocket_set_connopts(luat_websocket_ctrl_t *websocket_ctrl, luat_webs
if (opts->keepalive > 0) {
websocket_ctrl->keepalive = opts->keepalive;
}
if (opts->use_ipv6) {
network_connect_ipv6_domain(websocket_ctrl->netc, 1);
}
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions components/network/websocket/luat_websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ typedef struct luat_websocket_connopts
{
const char *url;
uint16_t keepalive;
uint8_t use_ipv6;
} luat_websocket_connopts_t;

typedef struct luat_websocket_pkg
Expand Down
11 changes: 10 additions & 1 deletion components/nimble/src/luat_lib_nimble.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ nimble.mac(string.fromHex("1234567890AB"))
static int l_nimble_mac(lua_State *L) {
int rc = 0;
uint8_t own_addr_type = 0;
uint8_t addr_val[6] = {0};
if (lua_type(L, 1) == LUA_TSTRING) {
size_t len = 0;
const char* tmac = luaL_checklstring(L, 1, &len);
Expand All @@ -305,6 +306,14 @@ static int l_nimble_mac(lua_State *L) {
}
luat_nimble_mac_set(tmac);
}
#ifdef TLS_CONFIG_CPU_XT804
if (1) {
extern int luat_nimble_mac_get(uint8_t* mac);
luat_nimble_mac_get(addr_val);
lua_pushlstring(L, (const char*)addr_val, 6);
return 1;
}
#endif
rc = ble_hs_util_ensure_addr(0);
if (rc != 0) {
LLOGW("fail to fetch BLE MAC, rc %d", rc);
Expand All @@ -319,7 +328,7 @@ static int l_nimble_mac(lua_State *L) {
}

/* Printing ADDR */
uint8_t addr_val[6] = {0};

rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
if (rc == 0) {
lua_pushlstring(L, (const char*)addr_val, 6);
Expand Down
90 changes: 2 additions & 88 deletions demo/adc/main.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@

--[[
1. Air101,Air103 模块上的ADC0脚-PA1, 0~2.4v,不要超过范围使用!!!
2. Air101,Air103模块上的ADC1脚-PA4, 0~2.4v,不要超过范围使用!!!
3. Air103 模块上的ADC2脚-PA2, 0~2.4v,不要超过范围使用!!!
4. Air103 模块上的ADC3脚-PA3, 0~2.4v,不要超过范围使用!!!
5. Air101,Air103 adc.CH_CPU 为内部温度 ,adc.CH_VBAT为VBAT
6. Air105 adc参考电压是1.88V,所有通道一致,
7. Air105内部分压没有隔离措施,在开启内部分压后,量程有所变化,具体看寄存器手册,1~5分压后能测到3.6,6通道能接近5V,但是不能直接测5V,可以测4.2V 0通道是始终开启无法关闭分压。
8. Air780E内部ADC接口为12bits 外部直流分压为0-3.4V
9. Air780E内部具有2个ADC接口,ADC0 -- AIO3 ADC1 -- AIO4
10. 特殊通道, CPU内部温度Temp -- adc.CH_CPU 主供电脚电压 VBAT -- adc.CH_VBAT
]]

-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "adcdemo"
VERSION = "1.0.0"
Expand All @@ -27,83 +14,10 @@ if wdt then
sys.timerLoopStart(wdt.feed, 3000) -- 3s喂一次狗
end

local rtos_bsp = rtos.bsp()
function adc_pin() -- 根据不同开发板,设置ADC编号
if rtos_bsp == "AIR101" then -- Air101开发板ADC编号
return 0,1,255,255,adc.CH_CPU ,adc.CH_VBAT
elseif rtos_bsp == "AIR103" then -- Air103开发板ADC编号
return 0,1,2,3,adc.CH_CPU ,adc.CH_VBAT
elseif rtos_bsp == "AIR105" then -- Air105开发板ADC编号
-- 默认不开启分压,范围是0-1.8v精度高
-- adc.setRange(adc.ADC_RANGE_3_6)
return 0,5,6,255,255,255
elseif rtos_bsp == "ESP32C3" then -- ESP32C3开发板ADC编号
return 0,1,2,3,adc.CH_CPU , 255
elseif rtos_bsp == "ESP32C2" then -- ESP32C2开发板ADC编号
return 0,1,2,3,adc.CH_CPU , 255
elseif rtos_bsp == "ESP32S3" then -- ESP32S3开发板ADC编号
return 0,1,2,3,adc.CH_CPU , 255
elseif rtos_bsp == "EC618" then --Air780E开发板ADC编号
-- 默认不开启分压,范围是0-1.2v精度高
-- adc.setRange(adc.ADC_RANGE_3_8)
return 0,1,255,255,adc.CH_CPU ,adc.CH_VBAT
elseif rtos_bsp == "EC718P" then --Air780EP开发板ADC编号
-- 默认不开启分压,范围是0-1.6v精度高
-- 开启分压后,外部输入最大不可超过3.3V
-- adc.setRange(adc.ADC_RANGE_MAX)
return 0,1,255,255,adc.CH_CPU ,adc.CH_VBAT
elseif rtos_bsp == "UIS8850BM" then
return 0,255,255,255, adc.CH_CPU ,adc.CH_VBAT
else
log.info("main", "define ADC pin in main.lua")
return 255,255,255,255, adc.CH_CPU ,adc.CH_VBAT
end
end
local adc_pin_0,adc_pin_1,adc_pin_2,adc_pin_3,adc_pin_temp,adc_pin_vbat=adc_pin()
sys.taskInit(function()
if rtos_bsp == "AIR105" then
adc.setRange(adc.ADC_RANGE_3_6) --开启的内部分压,可以把量程扩大
end
if adc_pin_0 and adc_pin_0 ~= 255 then adc.open(adc_pin_0) end
if adc_pin_1 and adc_pin_1 ~= 255 then adc.open(adc_pin_1) end
if adc_pin_2 and adc_pin_2 ~= 255 then adc.open(adc_pin_2) end
if adc_pin_3 and adc_pin_3 ~= 255 then adc.open(adc_pin_3) end
if adc_pin_temp and adc_pin_temp ~= 255 then adc.open(adc_pin_temp) end
if adc_pin_vbat and adc_pin_vbat ~= 255 then adc.open(adc_pin_vbat) end

-- 下面是循环打印, 接地不打印0也是正常现象
-- ADC的精度都不会太高, 若需要高精度ADC, 建议额外添加adc芯片
while true do
if adc_pin_0 and adc_pin_0 ~= 255 then
log.debug("adc", "adc" .. tostring(adc_pin_0), adc.get(adc_pin_0)) -- 若adc.get报nil, 改成adc.read
end
if adc_pin_1 and adc_pin_1 ~= 255 then
log.debug("adc", "adc" .. tostring(adc_pin_1), adc.get(adc_pin_1))
end
if adc_pin_2 and adc_pin_2 ~= 255 then
log.debug("adc", "adc" .. tostring(adc_pin_2), adc.get(adc_pin_2))
end
if adc_pin_3 and adc_pin_3 ~= 255 then
log.debug("adc", "adc" .. tostring(adc_pin_3), adc.get(adc_pin_3))
end
if adc_pin_temp and adc_pin_temp ~= 255 then
log.debug("adc", "CPU TEMP", adc.get(adc_pin_temp))
end
if adc_pin_vbat and adc_pin_vbat ~= 255 then
log.debug("adc", "VBAT", adc.get(adc_pin_vbat))
end
sys.wait(1000)
end
local testAdc = require "testAdc"
sys.taskInit(testAdc.dotest)

-- 若不再读取, 可关掉adc, 降低功耗, 非必须
if adc_pin_0 and adc_pin_0 ~= 255 then adc.close(adc_pin_0) end
if adc_pin_1 and adc_pin_1 ~= 255 then adc.close(adc_pin_1) end
if adc_pin_2 and adc_pin_2 ~= 255 then adc.close(adc_pin_2) end
if adc_pin_3 and adc_pin_3 ~= 255 then adc.close(adc_pin_3) end
if adc_pin_temp and adc_pin_temp ~= 255 then adc.close(adc_pin_temp) end
if adc_pin_vbat and adc_pin_vbat ~= 255 then adc.close(adc_pin_vbat) end

end)
-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
Expand Down
113 changes: 113 additions & 0 deletions demo/adc/testAdc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@

--[[
1. Air101,Air103 模块上的ADC0脚-PA1, 0~2.4v,不要超过范围使用!!!
2. Air101,Air103模块上的ADC1脚-PA4, 0~2.4v,不要超过范围使用!!!
3. Air103 模块上的ADC2脚-PA2, 0~2.4v,不要超过范围使用!!!
4. Air103 模块上的ADC3脚-PA3, 0~2.4v,不要超过范围使用!!!
5. Air101,Air103 adc.CH_CPU 为内部温度 ,adc.CH_VBAT为VBAT
6. Air105 adc参考电压是1.88V,所有通道一致,
7. Air105内部分压没有隔离措施,在开启内部分压后,量程有所变化,具体看寄存器手册,1~5分压后能测到3.6,6通道能接近5V,但是不能直接测5V,可以测4.2V 0通道是始终开启无法关闭分压。
8. Air780E内部ADC接口为12bits 外部直流分压为0-3.4V
9. Air780E内部具有2个ADC接口,ADC0 -- AIO3 ADC1 -- AIO4
10. 特殊通道, CPU内部温度Temp -- adc.CH_CPU 主供电脚电压 VBAT -- adc.CH_VBAT
11. 设置分压(adc.setRange)要在adc.open之前设置,否则无效!!
]]

local testAdc = {}

local rtos_bsp = rtos.bsp()
function adc_pin() -- 根据不同开发板,设置ADC编号
if rtos_bsp == "AIR101" then -- Air101开发板ADC编号
return 0,1,255,255,adc.CH_CPU ,adc.CH_VBAT
elseif rtos_bsp == "AIR103" then -- Air103开发板ADC编号
return 0,1,2,3,adc.CH_CPU ,adc.CH_VBAT
elseif rtos_bsp == "AIR105" then -- Air105开发板ADC编号
-- 默认不开启分压,范围是0-1.8v精度高
-- 设置分压要在adc.open之前设置,否则无效!!
-- adc.setRange(adc.ADC_RANGE_3_6)
return 0,5,6,255,255,255
elseif rtos_bsp == "ESP32C3" then -- ESP32C3开发板ADC编号
return 0,1,2,3,adc.CH_CPU , 255
elseif rtos_bsp == "ESP32C2" then -- ESP32C2开发板ADC编号
return 0,1,2,3,adc.CH_CPU , 255
elseif rtos_bsp == "ESP32S3" then -- ESP32S3开发板ADC编号
return 0,1,2,3,adc.CH_CPU , 255
elseif rtos_bsp == "EC618" then --Air780E开发板ADC编号
-- 默认不开启分压,范围是0-1.2v精度高
-- 设置分压要在adc.open之前设置,否则无效!!
-- adc.setRange(adc.ADC_RANGE_3_8)
return 0,1,255,255,adc.CH_CPU ,adc.CH_VBAT
elseif rtos_bsp == "EC718P" then --Air780EP开发板ADC编号
-- 默认不开启分压,范围是0-1.6v精度高
-- 开启分压后,外部输入最大不可超过3.3V
-- 设置分压要在adc.open之前设置,否则无效!!
-- adc.setRange(adc.ADC_RANGE_MAX)
return 0,1,255,255,adc.CH_CPU ,adc.CH_VBAT
elseif rtos_bsp == "UIS8850BM" then
return 0,255,255,255, adc.CH_CPU ,adc.CH_VBAT
else
log.info("main", "define ADC pin in main.lua")
return 255,255,255,255, adc.CH_CPU ,adc.CH_VBAT
end
end
local adc_pin_0,adc_pin_1,adc_pin_2,adc_pin_3,adc_pin_temp,adc_pin_vbat=adc_pin()


function testAdc.dotest()
if rtos_bsp == "AIR105" then
adc.setRange(adc.ADC_RANGE_3_6) --开启的内部分压,可以把量程扩大
end
if adc_pin_0 and adc_pin_0 ~= 255 then adc.open(adc_pin_0) end
if adc_pin_1 and adc_pin_1 ~= 255 then adc.open(adc_pin_1) end
if adc_pin_2 and adc_pin_2 ~= 255 then adc.open(adc_pin_2) end
if adc_pin_3 and adc_pin_3 ~= 255 then adc.open(adc_pin_3) end
if adc_pin_temp and adc_pin_temp ~= 255 then adc.open(adc_pin_temp) end
if adc_pin_vbat and adc_pin_vbat ~= 255 then adc.open(adc_pin_vbat) end

if adc_pin_0 and adc_pin_0 ~= 255 and mcu and mcu.ticks then
sys.wait(1000)
log.info("开始读取ADC")
local ms_start = mcu.ticks()
for i = 1, 100, 1 do
adc.get(adc_pin_0)
end
local ms_end = mcu.ticks()
log.info("结束读取ADC")
log.info("adc", "读取耗时", "100次", ms_end - ms_start, "ms", "单次", (ms_end - ms_start) // 100, "ms")
end

-- 下面是循环打印, 接地不打印0也是正常现象
-- ADC的精度都不会太高, 若需要高精度ADC, 建议额外添加adc芯片
while true do
if adc_pin_0 and adc_pin_0 ~= 255 then
log.debug("adc", "adc" .. tostring(adc_pin_0), adc.get(adc_pin_0)) -- 若adc.get报nil, 改成adc.read
end
if adc_pin_1 and adc_pin_1 ~= 255 then
log.debug("adc", "adc" .. tostring(adc_pin_1), adc.get(adc_pin_1))
end
if adc_pin_2 and adc_pin_2 ~= 255 then
log.debug("adc", "adc" .. tostring(adc_pin_2), adc.get(adc_pin_2))
end
if adc_pin_3 and adc_pin_3 ~= 255 then
log.debug("adc", "adc" .. tostring(adc_pin_3), adc.get(adc_pin_3))
end
if adc_pin_temp and adc_pin_temp ~= 255 then
log.debug("adc", "CPU TEMP", adc.get(adc_pin_temp))
end
if adc_pin_vbat and adc_pin_vbat ~= 255 then
log.debug("adc", "VBAT", adc.get(adc_pin_vbat))
end
sys.wait(1000)
end

-- 若不再读取, 可关掉adc, 降低功耗, 非必须
if adc_pin_0 and adc_pin_0 ~= 255 then adc.close(adc_pin_0) end
if adc_pin_1 and adc_pin_1 ~= 255 then adc.close(adc_pin_1) end
if adc_pin_2 and adc_pin_2 ~= 255 then adc.close(adc_pin_2) end
if adc_pin_3 and adc_pin_3 ~= 255 then adc.close(adc_pin_3) end
if adc_pin_temp and adc_pin_temp ~= 255 then adc.close(adc_pin_temp) end
if adc_pin_vbat and adc_pin_vbat ~= 255 then adc.close(adc_pin_vbat) end

end

return testAdc
Loading

0 comments on commit dbc6eba

Please sign in to comment.