mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-21 18:35:54 +08:00
更新硬件SDK
This commit is contained in:
@@ -0,0 +1,372 @@
|
||||
/*
|
||||
* Copyright (c) 2022 OpenLuat & AirM2M
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
#include "bsp_common.h"
|
||||
#include "luat_audio.h"
|
||||
#include "luat_i2s.h"
|
||||
#include "common_api.h"
|
||||
#include "audio_play.h"
|
||||
#include "driver_gpio.h"
|
||||
#include "luat_msgbus.h"
|
||||
#include "driver_gpio.h"
|
||||
#include "soc_spi.h"
|
||||
#include "ivTTSSDKID_all.h"
|
||||
#include "ivTTS.h"
|
||||
#include "luat_spi.h"
|
||||
#include "sfud.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_gpio.h"
|
||||
//#include "luat_multimedia.h"
|
||||
typedef struct
|
||||
{
|
||||
uint32_t dac_delay_len;
|
||||
uint32_t pa_delay_time;
|
||||
HANDLE pa_delay_timer;
|
||||
int pa_pin;
|
||||
int dac_pin;
|
||||
uint16_t vol;
|
||||
uint8_t pa_on_level;
|
||||
uint8_t dac_on_level;
|
||||
uint8_t raw_mode;
|
||||
}luat_audio_hardware_t;
|
||||
|
||||
static luat_audio_hardware_t g_s_audio_hardware;
|
||||
|
||||
extern int l_multimedia_raw_handler(lua_State *L, void* ptr);
|
||||
//extern void audio_play_file_default_fun(void *param);
|
||||
//extern void audio_play_TTS_default_fun(void *param);
|
||||
//extern void audio_play_tts_set_resource_ex(void *address, void *sdk_id, void *read_resource_fun);
|
||||
//extern void audio_play_global_init_ex(audio_play_event_cb_fun_t event_cb, audio_play_data_cb_fun_t data_cb, audio_play_default_fun_t play_file_fun, audio_play_default_fun_t play_tts_fun, void *user_param);
|
||||
//extern int audio_play_write_blank_raw_ex(uint8_t multimedia_id, uint8_t cnt, uint8_t add_font);
|
||||
|
||||
static void app_pa_on(uint32_t arg)
|
||||
{
|
||||
luat_gpio_set(g_s_audio_hardware.pa_pin, g_s_audio_hardware.pa_on_level);
|
||||
}
|
||||
|
||||
static void audio_event_cb(uint32_t event, void *param)
|
||||
{
|
||||
//DBG("%d", event);
|
||||
rtos_msg_t msg = {0};
|
||||
switch(event)
|
||||
{
|
||||
case MULTIMEDIA_CB_AUDIO_DECODE_START:
|
||||
luat_gpio_set(g_s_audio_hardware.dac_pin, g_s_audio_hardware.dac_on_level);
|
||||
audio_play_write_blank_raw_ex(0, g_s_audio_hardware.dac_delay_len, 1);
|
||||
break;
|
||||
case MULTIMEDIA_CB_AUDIO_OUTPUT_START:
|
||||
luat_rtos_timer_start(g_s_audio_hardware.pa_delay_timer, g_s_audio_hardware.pa_delay_time, 0, app_pa_on, NULL);
|
||||
break;
|
||||
case MULTIMEDIA_CB_AUDIO_NEED_DATA:
|
||||
if (g_s_audio_hardware.raw_mode)
|
||||
{
|
||||
msg.handler = l_multimedia_raw_handler;
|
||||
msg.arg1 = MULTIMEDIA_CB_AUDIO_NEED_DATA;
|
||||
msg.arg2 = (int)param;
|
||||
luat_msgbus_put(&msg, 1);
|
||||
}
|
||||
break;
|
||||
case MULTIMEDIA_CB_TTS_INIT:
|
||||
break;
|
||||
case MULTIMEDIA_CB_TTS_DONE:
|
||||
if (!audio_play_get_last_error(0))
|
||||
{
|
||||
audio_play_write_blank_raw_ex(0, 1, 0);
|
||||
}
|
||||
break;
|
||||
case MULTIMEDIA_CB_AUDIO_DONE:
|
||||
luat_rtos_timer_stop(g_s_audio_hardware.pa_delay_timer);
|
||||
// luat_audio_play_get_last_error(0);
|
||||
luat_gpio_set(g_s_audio_hardware.pa_pin, !g_s_audio_hardware.pa_on_level);
|
||||
luat_gpio_set(g_s_audio_hardware.dac_pin, !g_s_audio_hardware.dac_on_level);
|
||||
msg.handler = l_multimedia_raw_handler;
|
||||
msg.arg1 = MULTIMEDIA_CB_AUDIO_DONE;
|
||||
msg.arg2 = (int)param;
|
||||
luat_msgbus_put(&msg, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void audio_data_cb(uint8_t *data, uint32_t len, uint8_t bits, uint8_t channels)
|
||||
{
|
||||
//这里可以对音频数据进行软件音量缩放,或者直接清空来静音
|
||||
if (g_s_audio_hardware.vol != 100)
|
||||
{
|
||||
int16_t *i16 = (int16_t *)data;
|
||||
uint32_t i = 0;
|
||||
uint32_t pos = 0;
|
||||
int32_t temp;
|
||||
while(pos < len)
|
||||
{
|
||||
temp = i16[i];
|
||||
temp = temp * g_s_audio_hardware.vol / 100;
|
||||
if (temp > 32767)
|
||||
{
|
||||
temp = 32767;
|
||||
}
|
||||
else if (temp < -32768)
|
||||
{
|
||||
temp = -32768;
|
||||
}
|
||||
i16[i] = temp;
|
||||
i++;
|
||||
pos += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef LUAT_USE_TTS
|
||||
#define FLASH_TTS_ADDR (64 * 1024)
|
||||
extern luat_sfud_flash_t luat_sfud;
|
||||
static PV_Union g_s_spi_config;
|
||||
static ivBool tts_read_data(
|
||||
ivPointer pParameter, /* [in] user callback parameter */
|
||||
ivPointer pBuffer, /* [out] read resource buffer */
|
||||
ivResAddress iPos, /* [in] read start position */
|
||||
ivResSize nSize ) /* [in] read size */
|
||||
{
|
||||
iPos += FLASH_TTS_ADDR;
|
||||
GPIO_FastOutput(g_s_spi_config.u8[1], 0);
|
||||
char cmd[4] = {0x03, iPos >> 16, (iPos >> 8) & 0xFF, iPos & 0xFF};
|
||||
SPI_FastTransfer(g_s_spi_config.u8[0], cmd, cmd, 4);
|
||||
if (nSize >= 4096) {
|
||||
SPI_BlockTransfer(g_s_spi_config.u8[0], pBuffer, pBuffer, nSize);
|
||||
}
|
||||
else {
|
||||
SPI_FastTransfer(g_s_spi_config.u8[0], pBuffer, pBuffer, nSize);
|
||||
}
|
||||
|
||||
GPIO_FastOutput(g_s_spi_config.u8[1], 1);
|
||||
// if (memcmp(buff, ivtts_16k + offset, len)) {
|
||||
// LUAT_DEBUG_PRINT("tts data NOT match %04X %04X", offset, len);
|
||||
// }
|
||||
return ivTrue;
|
||||
}
|
||||
#endif
|
||||
HANDLE soc_audio_fopen(const char *fname, const char *mode)
|
||||
{
|
||||
return luat_fs_fopen(fname, mode);
|
||||
}
|
||||
|
||||
int soc_audio_fread(void *buffer, uint32_t size, uint32_t num, void *fp)
|
||||
{
|
||||
return luat_fs_fread(buffer, size, num, fp);
|
||||
}
|
||||
|
||||
int soc_audio_fseek(void *fp, long offset, int origin)
|
||||
{
|
||||
return luat_fs_fseek(fp, offset, origin);
|
||||
}
|
||||
|
||||
int soc_audio_fclose(void *fp)
|
||||
{
|
||||
return luat_fs_fclose(fp);
|
||||
}
|
||||
|
||||
void luat_audio_global_init(void)
|
||||
{
|
||||
|
||||
#ifdef LUAT_USE_TTS
|
||||
audio_play_global_init_ex(audio_event_cb, audio_data_cb, audio_play_file_default_fun, audio_play_TTS_default_fun, NULL);
|
||||
#ifdef LUAT_USE_TTS_16K
|
||||
ivCStrA sdk_id = AISOUND_SDK_USERID_16K;
|
||||
#else
|
||||
ivCStrA sdk_id = AISOUND_SDK_USERID_8K;
|
||||
#endif
|
||||
audio_play_tts_set_resource_ex(NULL, sdk_id, tts_read_data);
|
||||
#else
|
||||
audio_play_global_init_ex(audio_event_cb, audio_data_cb, audio_play_file_default_fun, NULL, NULL);
|
||||
#endif
|
||||
g_s_audio_hardware.dac_delay_len = 6;
|
||||
g_s_audio_hardware.pa_delay_time = 200;
|
||||
g_s_audio_hardware.pa_delay_timer = luat_create_rtos_timer(app_pa_on, NULL, NULL);
|
||||
g_s_audio_hardware.vol = 100;
|
||||
g_s_audio_hardware.dac_pin = -1;
|
||||
g_s_audio_hardware.pa_pin = -1;
|
||||
}
|
||||
|
||||
int luat_audio_play_multi_files(uint8_t multimedia_id, uData_t *info, uint32_t files_num, uint8_t error_stop)
|
||||
{
|
||||
g_s_audio_hardware.raw_mode = 0;
|
||||
uint32_t i;
|
||||
audio_play_info_t play_info[files_num];
|
||||
for(i = 0; i < files_num; i++)
|
||||
{
|
||||
play_info[i].path = info[i].value.asBuffer.buffer;
|
||||
play_info[i].address = 0;
|
||||
}
|
||||
return audio_play_multi_files(multimedia_id, play_info, files_num);
|
||||
}
|
||||
|
||||
int luat_audio_play_file(uint8_t multimedia_id, const char *path)
|
||||
{
|
||||
g_s_audio_hardware.raw_mode = 0;
|
||||
audio_play_info_t play_info[1];
|
||||
play_info[0].path = path;
|
||||
play_info[0].fail_continue = 0;
|
||||
play_info[0].address = NULL;
|
||||
return audio_play_multi_files(multimedia_id, play_info, 1);
|
||||
}
|
||||
|
||||
uint8_t luat_audio_is_finish(uint8_t multimedia_id)
|
||||
{
|
||||
return audio_play_is_finish(multimedia_id);
|
||||
}
|
||||
|
||||
int luat_audio_play_stop(uint8_t multimedia_id)
|
||||
{
|
||||
return audio_play_stop(multimedia_id);
|
||||
}
|
||||
|
||||
int luat_audio_pause_raw(uint8_t multimedia_id, uint8_t is_pause)
|
||||
{
|
||||
return audio_play_pause_raw(multimedia_id, is_pause);
|
||||
}
|
||||
|
||||
int luat_audio_play_get_last_error(uint8_t multimedia_id)
|
||||
{
|
||||
return audio_play_get_last_error(multimedia_id);
|
||||
}
|
||||
|
||||
int luat_audio_start_raw(uint8_t multimedia_id, uint8_t audio_format, uint8_t num_channels, uint32_t sample_rate, uint8_t bits_per_sample, uint8_t is_signed)
|
||||
{
|
||||
return audio_play_start_raw(multimedia_id, audio_format, num_channels, sample_rate, bits_per_sample, is_signed);
|
||||
}
|
||||
|
||||
int luat_audio_write_raw(uint8_t multimedia_id, uint8_t *data, uint32_t len)
|
||||
{
|
||||
int result = audio_play_write_raw(multimedia_id, data, len);
|
||||
if (!result)
|
||||
{
|
||||
g_s_audio_hardware.raw_mode = 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int luat_audio_stop_raw(uint8_t multimedia_id)
|
||||
{
|
||||
return audio_play_stop_raw(multimedia_id);
|
||||
}
|
||||
|
||||
void luat_audio_config_pa(uint8_t multimedia_id, uint32_t pin, int level, uint32_t dummy_time_len, uint32_t pa_delay_time)
|
||||
{
|
||||
if (pin < HAL_GPIO_MAX)
|
||||
{
|
||||
g_s_audio_hardware.pa_pin = pin;
|
||||
g_s_audio_hardware.pa_on_level = level;
|
||||
GPIO_Config(pin, 0, !level);
|
||||
GPIO_IomuxEC618(GPIO_ToPadEC618(pin, 0), 0, 0, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_s_audio_hardware.pa_pin = -1;
|
||||
}
|
||||
g_s_audio_hardware.dac_delay_len = dummy_time_len;
|
||||
g_s_audio_hardware.pa_delay_time = pa_delay_time;
|
||||
}
|
||||
|
||||
void luat_audio_config_dac(uint8_t multimedia_id, int pin, int level)
|
||||
{
|
||||
if (pin < 0)
|
||||
{ g_s_audio_hardware.dac_pin = HAL_GPIO_12;
|
||||
g_s_audio_hardware.dac_on_level = 1;
|
||||
GPIO_IomuxEC618(GPIO_ToPadEC618(g_s_audio_hardware.dac_pin, 4), 4, 0, 0);
|
||||
GPIO_Config(g_s_audio_hardware.dac_pin, 0, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pin < HAL_GPIO_MAX)
|
||||
{
|
||||
g_s_audio_hardware.dac_pin = pin;
|
||||
g_s_audio_hardware.dac_on_level = level;
|
||||
GPIO_Config(pin, 0, !level);
|
||||
GPIO_IomuxEC618(GPIO_ToPadEC618(g_s_audio_hardware.dac_pin, 0), 0, 0, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_s_audio_hardware.dac_pin = -1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
uint16_t luat_audio_vol(uint8_t multimedia_id, uint16_t vol)
|
||||
{
|
||||
g_s_audio_hardware.vol = vol;
|
||||
return g_s_audio_hardware.vol;
|
||||
}
|
||||
|
||||
int luat_i2s_setup(luat_i2s_conf_t *conf)
|
||||
{
|
||||
luat_i2s_base_setup(0, conf->communication_format, I2S_FRAME_SIZE_16_16);
|
||||
return 0;
|
||||
}
|
||||
int luat_i2s_send(uint8_t id, char* buff, size_t len)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int luat_i2s_recv(uint8_t id, char* buff, size_t len)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int luat_i2s_close(uint8_t id)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int l_i2s_play(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_i2s_pause(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_i2s_stop(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
#ifdef LUAT_USE_TTS
|
||||
int luat_audio_play_tts_text(uint32_t multimedia_id, void *text, uint32_t text_bytes)
|
||||
{
|
||||
if (!g_s_spi_config.u32)
|
||||
{
|
||||
if (LUAT_TYPE_SPI == luat_sfud.luat_spi)
|
||||
{
|
||||
luat_spi_t *spi = (luat_spi_t *)luat_sfud.user_data;
|
||||
g_s_spi_config.u8[0] = spi->id;
|
||||
g_s_spi_config.u8[1] = spi->cs;
|
||||
}
|
||||
else if (LUAT_TYPE_SPI_DEVICE == luat_sfud.luat_spi)
|
||||
{
|
||||
luat_spi_device_t* spi_device = (luat_spi_t *)luat_sfud.user_data;
|
||||
g_s_spi_config.u8[0] = spi_device->bus_id;
|
||||
g_s_spi_config.u8[1] = spi_device->spi_config.cs;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return audio_play_tts_text(multimedia_id, text, text_bytes);
|
||||
}
|
||||
|
||||
int luat_audio_play_tts_set_param(uint32_t multimedia_id, uint32_t param_id, uint32_t param_value)
|
||||
{
|
||||
return audio_play_tts_set_param(multimedia_id, param_id, param_value);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,364 @@
|
||||
#include "common_api.h"
|
||||
#include "luat_base.h"
|
||||
#include "luat_msgbus.h"
|
||||
#include "luat_malloc.h"
|
||||
#include "luat_fs.h"
|
||||
#include "luat_timer.h"
|
||||
#include <stdlib.h>
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "mbedtls/cipher.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "mbedtls/sha512.h"
|
||||
#include "mbedtls/md5.h"
|
||||
|
||||
#define LUAT_LOG_TAG "base"
|
||||
#include "luat_log.h"
|
||||
|
||||
#ifdef LUAT_USE_LVGL
|
||||
#include "lvgl.h"
|
||||
void luat_lv_fs_init(void);
|
||||
void lv_bmp_init(void);
|
||||
void lv_png_init(void);
|
||||
void lv_split_jpeg_init(void);
|
||||
#endif
|
||||
|
||||
static const luaL_Reg loadedlibs[] = {
|
||||
{"_G", luaopen_base}, // _G
|
||||
{LUA_LOADLIBNAME, luaopen_package}, // require
|
||||
{LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
|
||||
{LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构
|
||||
{LUA_IOLIBNAME, luaopen_io}, // io库,操作文件
|
||||
{LUA_OSLIBNAME, luaopen_os}, // os库,已精简
|
||||
{LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作
|
||||
{LUA_MATHLIBNAME, luaopen_math}, // math 数值计算
|
||||
{LUA_UTF8LIBNAME, luaopen_utf8},
|
||||
{LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简
|
||||
#ifdef LUAT_USE_DBG
|
||||
#ifndef LUAT_USE_SHELL
|
||||
#define LUAT_USE_SHELL
|
||||
#endif
|
||||
{"dbg", luaopen_dbg}, // 调试库
|
||||
#endif
|
||||
#if defined(LUA_COMPAT_BITLIB)
|
||||
{LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
|
||||
#endif
|
||||
// 往下是LuatOS定制的库, 如需精简请仔细测试
|
||||
//----------------------------------------------------------------------
|
||||
// 核心支撑库, 不可禁用!!
|
||||
{"rtos", luaopen_rtos}, // rtos底层库, 核心功能是队列和定时器
|
||||
{"log", luaopen_log}, // 日志库
|
||||
{"timer", luaopen_timer}, // 延时库
|
||||
//-----------------------------------------------------------------------
|
||||
{"mobile", luaopen_mobile},
|
||||
{"sms", luaopen_sms},
|
||||
{"errDump",luaopen_errdump},
|
||||
#ifdef LUAT_USE_NETWORK
|
||||
{"socket", luaopen_socket_adapter},
|
||||
{"mqtt", luaopen_mqtt},
|
||||
{"websocket", luaopen_websocket},
|
||||
{"http", luaopen_http},
|
||||
#ifdef LUAT_USE_FTP
|
||||
{"ftp", luaopen_ftp},
|
||||
#endif
|
||||
#endif
|
||||
#ifdef LUAT_USE_WLAN
|
||||
{"wlan", luaopen_wlan},
|
||||
#endif
|
||||
// 设备驱动类, 可按实际情况删减. 即使最精简的固件, 也强烈建议保留uart库
|
||||
#ifdef LUAT_USE_UART
|
||||
{"uart", luaopen_uart}, // 串口操作
|
||||
#endif
|
||||
#ifdef LUAT_USE_GPIO
|
||||
{"gpio", luaopen_gpio}, // GPIO脚的操作
|
||||
#endif
|
||||
#ifdef LUAT_USE_I2C
|
||||
{"i2c", luaopen_i2c}, // I2C操作
|
||||
#endif
|
||||
#ifdef LUAT_USE_SPI
|
||||
{"spi", luaopen_spi}, // SPI操作
|
||||
#endif
|
||||
#ifdef LUAT_USE_ADC
|
||||
{"adc", luaopen_adc}, // ADC模块
|
||||
#endif
|
||||
#ifdef LUAT_USE_PWM
|
||||
{"pwm", luaopen_pwm}, // PWM模块
|
||||
#endif
|
||||
#ifdef LUAT_USE_WDT
|
||||
{"wdt", luaopen_wdt}, // watchdog模块
|
||||
#endif
|
||||
#ifdef LUAT_USE_PM
|
||||
{"pm", luaopen_pm}, // 电源管理模块
|
||||
#endif
|
||||
#ifdef LUAT_USE_MCU
|
||||
{"mcu", luaopen_mcu}, // MCU特有的一些操作
|
||||
#endif
|
||||
#ifdef LUAT_USE_RTC
|
||||
{"rtc", luaopen_rtc}, // 实时时钟
|
||||
#endif
|
||||
#ifdef LUAT_USE_OTP
|
||||
{"otp", luaopen_otp}, // OTP
|
||||
#endif
|
||||
//-----------------------------------------------------------------------
|
||||
// 工具库, 按需选用
|
||||
#ifdef LUAT_USE_CRYPTO
|
||||
{"crypto",luaopen_crypto}, // 加密和hash模块
|
||||
#endif
|
||||
#ifdef LUAT_USE_CJSON
|
||||
{"json", luaopen_cjson}, // json的序列化和反序列化
|
||||
#endif
|
||||
#ifdef LUAT_USE_ZBUFF
|
||||
{"zbuff", luaopen_zbuff}, // 像C语言语言操作内存块
|
||||
#endif
|
||||
#ifdef LUAT_USE_PACK
|
||||
{"pack", luaopen_pack}, // pack.pack/pack.unpack
|
||||
#endif
|
||||
// {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
|
||||
// {"libcoap", luaopen_libcoap}, // 处理COAP消息
|
||||
|
||||
#ifdef LUAT_USE_LIBGNSS
|
||||
{"libgnss", luaopen_libgnss}, // 处理GNSS定位数据
|
||||
#endif
|
||||
#ifdef LUAT_USE_FS
|
||||
{"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法
|
||||
#endif
|
||||
#ifdef LUAT_USE_SENSOR
|
||||
{"sensor", luaopen_sensor}, // 传感器库,支持DS18B20
|
||||
#endif
|
||||
#ifdef LUAT_USE_SFUD
|
||||
{"sfud", luaopen_sfud}, // sfud
|
||||
#endif
|
||||
#ifdef LUAT_USE_SFD
|
||||
{"sfd", luaopen_sfd}, // sfud
|
||||
#endif
|
||||
#ifdef LUAT_USE_DISP
|
||||
{"disp", luaopen_disp}, // OLED显示模块
|
||||
#endif
|
||||
#ifdef LUAT_USE_U8G2
|
||||
{"u8g2", luaopen_u8g2}, // u8g2
|
||||
#endif
|
||||
|
||||
#ifdef LUAT_USE_EINK
|
||||
{"eink", luaopen_eink}, // 电子墨水屏
|
||||
#endif
|
||||
#ifdef LUAT_USE_FATFS
|
||||
{"fatfs", luaopen_fatfs}, // SD卡/tf卡
|
||||
#endif
|
||||
|
||||
#ifdef LUAT_USE_LVGL
|
||||
// #ifndef LUAT_USE_LCD
|
||||
// #define LUAT_USE_LCD
|
||||
// #endif
|
||||
{"lvgl", luaopen_lvgl},
|
||||
#endif
|
||||
|
||||
#ifdef LUAT_USE_LCD
|
||||
{"lcd", luaopen_lcd},
|
||||
#endif
|
||||
#ifdef LUAT_USE_STATEM
|
||||
{"statem", luaopen_statem},
|
||||
#endif
|
||||
#ifdef LUAT_USE_GTFONT
|
||||
{"gtfont", luaopen_gtfont},
|
||||
#endif
|
||||
#ifdef LUAT_USE_FSKV
|
||||
{"fskv", luaopen_fskv},
|
||||
// 启用FSKV的时候,自动禁用FDB
|
||||
#ifdef LUAT_USE_FDB
|
||||
#undef LUAT_USE_FDB
|
||||
#endif
|
||||
#endif
|
||||
#ifdef LUAT_USE_FDB
|
||||
{"fdb", luaopen_fdb},
|
||||
#endif
|
||||
#ifdef LUAT_USE_VMX
|
||||
{"vmx", luaopen_vmx},
|
||||
#endif
|
||||
#ifdef LUAT_USE_COREMARK
|
||||
{"coremark", luaopen_coremark},
|
||||
#endif
|
||||
#ifdef LUAT_USE_FONTS
|
||||
{"fonts", luaopen_fonts},
|
||||
#endif
|
||||
#ifdef LUAT_USE_ZLIB
|
||||
{"zlib", luaopen_zlib},
|
||||
#endif
|
||||
#ifdef LUAT_USE_MLX90640
|
||||
{"mlx90640", luaopen_mlx90640},
|
||||
#endif
|
||||
#ifdef LUAT_USE_IR
|
||||
{"ir", luaopen_ir},
|
||||
#endif
|
||||
#ifdef LUAT_USE_YMODEM
|
||||
{"ymodem", luaopen_ymodem},
|
||||
#endif
|
||||
#ifdef LUAT_USE_LORA
|
||||
{"lora", luaopen_lora},
|
||||
#endif
|
||||
#ifdef LUAT_USE_MINIZ
|
||||
{"miniz", luaopen_miniz},
|
||||
#endif
|
||||
#ifdef LUAT_USE_PROTOBUF
|
||||
{"protobuf", luaopen_protobuf},
|
||||
#endif
|
||||
#ifdef LUAT_USE_IOTAUTH
|
||||
{"iotauth", luaopen_iotauth},
|
||||
#endif
|
||||
#ifdef LUAT_USE_HTTPSRV
|
||||
{"httpsrv", luaopen_httpsrv},
|
||||
#endif
|
||||
#ifdef LUAT_USE_RSA
|
||||
{"rsa", luaopen_rsa},
|
||||
#endif
|
||||
#ifdef LUAT_USE_MEDIA
|
||||
{"i2s", luaopen_i2s},
|
||||
{"audio", luaopen_multimedia_audio},
|
||||
#ifndef LUAT_USE_TTS
|
||||
{"codec", luaopen_multimedia_codec},
|
||||
#endif
|
||||
#endif
|
||||
#ifdef LUAT_USE_HMETA
|
||||
{"hmeta", luaopen_hmeta},
|
||||
#endif
|
||||
#ifdef LUAT_USE_FOTA
|
||||
{"fota", luaopen_fota},
|
||||
#endif
|
||||
#ifdef LUAT_USE_PROFILER
|
||||
{"profiler", luaopen_profiler},
|
||||
#endif
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
#if MBEDTLS_VERSION_NUMBER >= 0x03000000
|
||||
#else
|
||||
void mbedtls_md5( const unsigned char *input,
|
||||
size_t ilen,
|
||||
unsigned char output[16] )
|
||||
{
|
||||
mbedtls_md5_ret( input, ilen, output );
|
||||
}
|
||||
#endif
|
||||
|
||||
// 按不同的rtconfig加载不同的库函数
|
||||
void luat_openlibs(lua_State *L) {
|
||||
// 初始化队列服务
|
||||
luat_msgbus_init();
|
||||
//print_list_mem("done>luat_msgbus_init");
|
||||
// 加载系统库
|
||||
const luaL_Reg *lib;
|
||||
/* "require" functions from 'loadedlibs' and set results to global table */
|
||||
for (lib = loadedlibs; lib->func; lib++) {
|
||||
luaL_requiref(L, lib->name, lib->func, 1);
|
||||
lua_pop(L, 1); /* remove lib */
|
||||
//extern void print_list_mem(const char* name);
|
||||
//print_list_mem(lib->name);
|
||||
}
|
||||
}
|
||||
|
||||
void ResetECSystemReset(void);
|
||||
|
||||
void luat_os_reboot(int code){
|
||||
(void)code;
|
||||
ResetECSystemReset();
|
||||
}
|
||||
|
||||
const char* luat_os_bsp(void) {
|
||||
return "EC618";
|
||||
}
|
||||
|
||||
/** 设备进入待机模式 */
|
||||
void luat_os_standy(int timeout) {
|
||||
(void)timeout;
|
||||
return; // nop
|
||||
}
|
||||
|
||||
// from reset.h
|
||||
|
||||
//void luat_ota_reboot(int timeout_ms) {
|
||||
// if (timeout_ms > 0)
|
||||
// luat_timer_mdelay(timeout_ms);
|
||||
// // nop
|
||||
//}
|
||||
|
||||
uint32_t luat_get_utc(uint32_t *tamp)
|
||||
{
|
||||
uint32_t sec = soc_get_utc();
|
||||
if (tamp)
|
||||
{
|
||||
*tamp = sec;
|
||||
}
|
||||
return sec;
|
||||
}
|
||||
|
||||
void luat_os_entry_cri(void) {
|
||||
portENTER_CRITICAL();
|
||||
}
|
||||
|
||||
void luat_os_exit_cri(void) {
|
||||
portEXIT_CRITICAL();
|
||||
}
|
||||
|
||||
// delay_us 是系统函数
|
||||
extern void delay_us(uint32_t us);
|
||||
void luat_timer_us_delay(size_t us) {
|
||||
if (us > 0)
|
||||
delay_us(us);
|
||||
}
|
||||
|
||||
extern void soc_vsprintf(uint8_t no_print, const char *fmt, va_list ap);
|
||||
void DBG_Printf(const char* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
soc_vsprintf(0, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
extern const uint8_t ByteToAsciiTable[16];
|
||||
void DBG_HexPrintf(void *Data, unsigned int len)
|
||||
{
|
||||
uint8_t *data = (uint8_t *)Data;
|
||||
uint8_t *uart_buf;
|
||||
uint32_t i,j;
|
||||
j = 0;
|
||||
if (!len) return;
|
||||
uart_buf = luat_heap_malloc(len * 3 + 2);
|
||||
if (!uart_buf) return;
|
||||
memset(uart_buf, 0, len * 3 + 2);
|
||||
for (i = 0; i < len; i++){
|
||||
uart_buf[j++] = ByteToAsciiTable[(data[i] & 0xf0) >> 4];
|
||||
uart_buf[j++] = ByteToAsciiTable[data[i] & 0x0f];
|
||||
uart_buf[j++] = ' ';
|
||||
}
|
||||
uart_buf[j++] = '\r';
|
||||
uart_buf[j++] = '\n';
|
||||
luat_log_write((char*)uart_buf, len * 3 + 2);
|
||||
luat_heap_free(uart_buf);
|
||||
}
|
||||
|
||||
struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt,
|
||||
struct tm *tm_buf )
|
||||
{
|
||||
Date_UserDataStruct Date;
|
||||
Time_UserDataStruct Time;
|
||||
Tamp2UTC(*tt, &Date, &Time, 0);
|
||||
tm_buf->tm_year = Date.Year - 1900;
|
||||
tm_buf->tm_mon = Date.Mon - 1;
|
||||
tm_buf->tm_mday = Date.Day;
|
||||
tm_buf->tm_hour = Time.Hour;
|
||||
tm_buf->tm_min = Time.Min;
|
||||
tm_buf->tm_sec = Time.Sec;
|
||||
return tm_buf;
|
||||
|
||||
}
|
||||
|
||||
#include "osasys.h"
|
||||
|
||||
time_t luat_time(time_t *_Time) {
|
||||
utc_timer_value_t *timeUtc = OsaSystemTimeReadUtc();
|
||||
if (_Time != NULL) {
|
||||
*_Time = timeUtc->UTCsecs;
|
||||
}
|
||||
return timeUtc->UTCsecs;
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
/*
|
||||
* Copyright (c) 2022 OpenLuat & AirM2M
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "luat_base.h"
|
||||
#include "luat_mcu.h"
|
||||
#include "luat_malloc.h"
|
||||
#include "luat_spi.h"
|
||||
#include "luat_fota.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "bsp_common.h"
|
||||
#include "platform_define.h"
|
||||
#define LUAT_LOG_TAG "fota"
|
||||
#include "luat_log.h"
|
||||
#include "mbedtls/md5.h"
|
||||
#include "fota_nvm.h"
|
||||
extern void fotaNvmNfsPeInit(uint8_t isSmall);
|
||||
#define __SOC_OTA_COMMON_DATA_LOAD_ADDRESS__ (LUA_SCRIPT_OTA_ADDR + AP_FLASH_XIP_ADDR)
|
||||
#define __SOC_OTA_COMMON_DATA_SAVE_ADDRESS__ (LUA_SCRIPT_OTA_ADDR)
|
||||
#define __SOC_OTA_SDK_DATA_LOAD_ADDRESS__ (FLASH_FOTA_REGION_START + AP_FLASH_XIP_ADDR)
|
||||
#define __SOC_OTA_SDK_DATA_SAVE_ADDRESS__ (FLASH_FOTA_REGION_START)
|
||||
typedef struct
|
||||
{
|
||||
Buffer_Struct data_buffer;
|
||||
mbedtls_md5_context *md5_ctx;
|
||||
CoreUpgrade_FileHeadCalMD5Struct *p_fota_file_head;
|
||||
uint32_t *crc32_table;
|
||||
uint32_t ota_done_len;
|
||||
uint8_t ota_state;
|
||||
}luat_fota_ctrl_t;
|
||||
|
||||
enum
|
||||
{
|
||||
OTA_STATE_IDLE,
|
||||
OTA_STATE_WRITE_COMMON_DATA,
|
||||
OTA_STATE_WRITE_SDK_DATA,
|
||||
OTA_STATE_OK,
|
||||
};
|
||||
|
||||
static luat_fota_ctrl_t g_s_fota;
|
||||
|
||||
static void luat_fota_finish(void)
|
||||
{
|
||||
CoreUpgrade_HeadCalMD5Struct Head = {0};
|
||||
PV_Union uPV;
|
||||
Head.MaigcNum = __APP_START_MAGIC__;
|
||||
Head.DataStartAddress = __SOC_OTA_COMMON_DATA_LOAD_ADDRESS__;
|
||||
uPV.u8[0] = CORE_OTA_MODE_FULL;
|
||||
uPV.u8[1] = CORE_OTA_IN_FLASH;
|
||||
Head.Param1 = uPV.u32;
|
||||
Head.DataLen = g_s_fota.p_fota_file_head->CommonDataLen;
|
||||
memcpy(Head.CommonMD5, g_s_fota.p_fota_file_head->CommonMD5, 16);
|
||||
Head.CRC32 = CRC32_Cal(g_s_fota.crc32_table, (uint8_t *)&Head.Param1, sizeof(Head) - 8, 0xffffffff);
|
||||
BSP_QSPI_Erase_Safe(__SOC_OTA_INFO_DATA_SAVE_ADDRESS__, __FLASH_SECTOR_SIZE__);
|
||||
BSP_QSPI_Write_Safe((uint8_t *)&Head, __SOC_OTA_INFO_DATA_SAVE_ADDRESS__, sizeof(Head));
|
||||
g_s_fota.ota_state = OTA_STATE_OK;
|
||||
luat_heap_free(g_s_fota.crc32_table);
|
||||
g_s_fota.crc32_table = NULL;
|
||||
luat_heap_free(g_s_fota.md5_ctx);
|
||||
g_s_fota.md5_ctx = NULL;
|
||||
luat_heap_free(g_s_fota.p_fota_file_head);
|
||||
g_s_fota.p_fota_file_head = NULL;
|
||||
OS_DeInitBuffer(&g_s_fota.data_buffer);
|
||||
LLOGI("fota ok!, wait reboot");
|
||||
}
|
||||
|
||||
int luat_fota_init(uint32_t start_address, uint32_t len, luat_spi_device_t* spi_device, const char *path, uint32_t pathlen)
|
||||
{
|
||||
if (!g_s_fota.crc32_table)
|
||||
{
|
||||
g_s_fota.p_fota_file_head = luat_heap_malloc(sizeof(CoreUpgrade_FileHeadCalMD5Struct));
|
||||
g_s_fota.crc32_table = luat_heap_malloc(256 * sizeof(uint32_t));
|
||||
memset(g_s_fota.crc32_table, 0, 1024);
|
||||
g_s_fota.md5_ctx = luat_heap_malloc(sizeof(mbedtls_md5_context));
|
||||
CRC32_CreateTable(g_s_fota.crc32_table, CRC32_GEN);
|
||||
}
|
||||
memset(g_s_fota.p_fota_file_head, 0, sizeof(CoreUpgrade_FileHeadCalMD5Struct));
|
||||
g_s_fota.ota_state = OTA_STATE_IDLE;
|
||||
BSP_QSPI_Erase_Safe(__SOC_OTA_INFO_DATA_SAVE_ADDRESS__, __FLASH_SECTOR_SIZE__);
|
||||
OS_ReInitBuffer(&g_s_fota.data_buffer, __FLASH_SECTOR_SIZE__ * 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_fota_write(uint8_t *data, uint32_t len)
|
||||
{
|
||||
uint32_t save_len;
|
||||
OS_BufferWrite(&g_s_fota.data_buffer, data, len);
|
||||
REPEAT:
|
||||
switch(g_s_fota.ota_state)
|
||||
{
|
||||
case OTA_STATE_IDLE:
|
||||
if (g_s_fota.data_buffer.Pos > sizeof(CoreUpgrade_FileHeadCalMD5Struct))
|
||||
{
|
||||
memcpy(g_s_fota.p_fota_file_head, g_s_fota.data_buffer.Data, sizeof(CoreUpgrade_FileHeadCalMD5Struct));
|
||||
OS_BufferRemove(&g_s_fota.data_buffer, sizeof(CoreUpgrade_FileHeadCalMD5Struct));
|
||||
if (g_s_fota.p_fota_file_head->MaigcNum != __APP_START_MAGIC__)
|
||||
{
|
||||
LLOGI("magic num error %x", g_s_fota.p_fota_file_head->MaigcNum);
|
||||
g_s_fota.data_buffer.Pos = 0;
|
||||
return -1;
|
||||
}
|
||||
uint32_t crc32 = CRC32_Cal(g_s_fota.crc32_table, g_s_fota.p_fota_file_head->MainVersion, sizeof(CoreUpgrade_FileHeadCalMD5Struct) - 8, 0xffffffff);
|
||||
if (crc32 != g_s_fota.p_fota_file_head->CRC32)
|
||||
{
|
||||
LLOGI("file head crc32 error %x,%x", crc32, g_s_fota.p_fota_file_head->CRC32);
|
||||
g_s_fota.data_buffer.Pos = 0;
|
||||
return -1;
|
||||
}
|
||||
g_s_fota.ota_done_len = 0;
|
||||
mbedtls_md5_init(g_s_fota.md5_ctx);
|
||||
mbedtls_md5_starts_ret(g_s_fota.md5_ctx);
|
||||
if (g_s_fota.p_fota_file_head->CommonDataLen)
|
||||
{
|
||||
g_s_fota.ota_state = OTA_STATE_WRITE_COMMON_DATA;
|
||||
fotaNvmNfsPeInit(1);
|
||||
LLOGI("write stript data");
|
||||
goto REPEAT;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_s_fota.ota_state = OTA_STATE_WRITE_SDK_DATA;
|
||||
LLOGI("write core data");
|
||||
goto REPEAT;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case OTA_STATE_WRITE_COMMON_DATA:
|
||||
save_len = ((g_s_fota.ota_done_len + __FLASH_SECTOR_SIZE__) < g_s_fota.p_fota_file_head->CommonDataLen)?__FLASH_SECTOR_SIZE__:(g_s_fota.p_fota_file_head->CommonDataLen - g_s_fota.ota_done_len);
|
||||
if (g_s_fota.data_buffer.Pos >= save_len)
|
||||
{
|
||||
BSP_QSPI_Erase_Safe(__SOC_OTA_COMMON_DATA_SAVE_ADDRESS__ + g_s_fota.ota_done_len, __FLASH_SECTOR_SIZE__);
|
||||
BSP_QSPI_Write_Safe(g_s_fota.data_buffer.Data, __SOC_OTA_COMMON_DATA_SAVE_ADDRESS__ + g_s_fota.ota_done_len, save_len);
|
||||
mbedtls_md5_update_ret(g_s_fota.md5_ctx, (uint8_t *)(__SOC_OTA_COMMON_DATA_LOAD_ADDRESS__ + g_s_fota.ota_done_len), save_len );
|
||||
OS_BufferRemove(&g_s_fota.data_buffer, save_len);
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
g_s_fota.ota_done_len += save_len;
|
||||
if (g_s_fota.ota_done_len >= g_s_fota.p_fota_file_head->CommonDataLen)
|
||||
{
|
||||
LLOGI("stript data done, now checking");
|
||||
fotaNvmNfsPeInit(0);
|
||||
uint8_t md5[16];
|
||||
mbedtls_md5_finish_ret(g_s_fota.md5_ctx, md5);
|
||||
if (memcmp(md5, g_s_fota.p_fota_file_head->CommonMD5, 16))
|
||||
{
|
||||
LLOGI("stript data md5 check failed");
|
||||
g_s_fota.ota_state = OTA_STATE_IDLE;
|
||||
g_s_fota.data_buffer.Pos = 0;
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
LLOGI("stript data md5 ok");
|
||||
if (g_s_fota.p_fota_file_head->SDKDataLen)
|
||||
{
|
||||
g_s_fota.ota_state = OTA_STATE_WRITE_SDK_DATA;
|
||||
g_s_fota.ota_done_len = 0;
|
||||
LLOGI("write core data");
|
||||
goto REPEAT;
|
||||
}
|
||||
else
|
||||
{
|
||||
LLOGI("stript data md5 ok");
|
||||
luat_fota_finish();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
goto REPEAT;
|
||||
}
|
||||
break;
|
||||
case OTA_STATE_WRITE_SDK_DATA:
|
||||
save_len = ((g_s_fota.ota_done_len + __FLASH_SECTOR_SIZE__) < (g_s_fota.p_fota_file_head->SDKDataLen))?__FLASH_SECTOR_SIZE__:(g_s_fota.p_fota_file_head->SDKDataLen - g_s_fota.ota_done_len);
|
||||
if (g_s_fota.data_buffer.Pos >= save_len)
|
||||
{
|
||||
BSP_QSPI_Erase_Safe(__SOC_OTA_SDK_DATA_SAVE_ADDRESS__ + g_s_fota.ota_done_len, __FLASH_SECTOR_SIZE__);
|
||||
BSP_QSPI_Write_Safe(g_s_fota.data_buffer.Data, __SOC_OTA_SDK_DATA_SAVE_ADDRESS__ + g_s_fota.ota_done_len, save_len);
|
||||
OS_BufferRemove(&g_s_fota.data_buffer, save_len);
|
||||
// mbedtls_md5_update_ret(g_s_fota.md5_ctx, (uint8_t *)(__SOC_OTA_COMMON_DATA_SAVE_ADDRESS__ + g_s_fota.ota_done_len), save_len );
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
g_s_fota.ota_done_len += save_len;
|
||||
if (g_s_fota.ota_done_len >= g_s_fota.p_fota_file_head->SDKDataLen)
|
||||
{
|
||||
fotaNvmInit();
|
||||
FotaDefChkDeltaState_t chkDelta = {0};
|
||||
FotaDefChkBaseImage_t chkBase = {0};
|
||||
|
||||
fotaNvmDoExtension(FOTA_DEF_CHK_DELTA_STATE, (void*)&chkDelta);
|
||||
if(!chkDelta.isValid)
|
||||
{
|
||||
LLOGI("validate delta err! errno(%d)", chkDelta.state);
|
||||
g_s_fota.ota_state = OTA_STATE_IDLE;
|
||||
g_s_fota.data_buffer.Pos = 0;
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
LLOGI("validate delta ok!");
|
||||
fotaNvmDoExtension(FOTA_DEF_CHK_BASE_IMAGE, (void*)&chkBase);
|
||||
if(!chkBase.isMatched)
|
||||
{
|
||||
LLOGI("however, base fw is unmatched!");
|
||||
fotaNvmClearDelta(0, 4096);
|
||||
g_s_fota.ota_state = OTA_STATE_IDLE;
|
||||
g_s_fota.data_buffer.Pos = 0;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
LLOGI("sdk data ok!");
|
||||
luat_fota_finish();
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto REPEAT;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int luat_fota_done(void)
|
||||
{
|
||||
switch(g_s_fota.ota_state)
|
||||
{
|
||||
case OTA_STATE_IDLE:
|
||||
return -1;
|
||||
case OTA_STATE_OK:
|
||||
return 0;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int luat_fota_end(uint8_t is_ok)
|
||||
{
|
||||
if (g_s_fota.ota_state != OTA_STATE_OK)
|
||||
{
|
||||
luat_heap_free(g_s_fota.crc32_table);
|
||||
g_s_fota.crc32_table = NULL;
|
||||
luat_heap_free(g_s_fota.md5_ctx);
|
||||
g_s_fota.md5_ctx = NULL;
|
||||
luat_heap_free(g_s_fota.p_fota_file_head);
|
||||
g_s_fota.p_fota_file_head = NULL;
|
||||
OS_DeInitBuffer(&g_s_fota.data_buffer);
|
||||
OS_DeInitBuffer(&g_s_fota.data_buffer);
|
||||
BSP_QSPI_Erase_Safe(__SOC_OTA_INFO_DATA_SAVE_ADDRESS__, __FLASH_SECTOR_SIZE__);
|
||||
LLOGI("fota failed");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t luat_fota_wait_ready(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "luat_base.h"
|
||||
#include "luat_hmeta.h"
|
||||
|
||||
extern int soc_get_model_name(char *model);
|
||||
|
||||
int luat_hmeta_model_name(char* buff) {
|
||||
soc_get_model_name(buff);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
|
||||
#include "common_api.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
#include "luat_base.h"
|
||||
#include "luat_log.h"
|
||||
#include "luat_uart.h"
|
||||
#include "printf.h"
|
||||
#include "luat_cmux.h"
|
||||
#include "luat_conf_bsp.h"
|
||||
|
||||
#include "cms_def.h"
|
||||
|
||||
extern luat_cmux_t cmux_ctx;
|
||||
extern void soc_debug_out(char *string, uint32_t size);
|
||||
static uint32_t luat_log_uart_port = 0;
|
||||
static uint32_t luat_log_level_cur = LUAT_LOG_DEBUG;
|
||||
|
||||
#define LOGLOG_SIZE 1024
|
||||
static char log_printf_buff[LOGLOG_SIZE] __attribute__((aligned (16)));
|
||||
|
||||
void luat_log_set_uart_port(int port) {
|
||||
luat_log_uart_port = port;
|
||||
}
|
||||
|
||||
uint8_t luat_log_get_uart_port(void) {
|
||||
return luat_log_uart_port;
|
||||
}
|
||||
|
||||
void luat_nprint(char *s, size_t l) {
|
||||
#ifdef LUAT_USE_SHELL
|
||||
if (cmux_ctx.state == 1 && cmux_ctx.log_state ==1){
|
||||
luat_cmux_write(LUAT_CMUX_CH_LOG, CMUX_FRAME_UIH & ~ CMUX_CONTROL_PF,s, l);
|
||||
}else
|
||||
#endif
|
||||
soc_debug_out(s, l);
|
||||
}
|
||||
|
||||
void luat_log_write(char *s, size_t l) {
|
||||
#ifdef LUAT_USE_SHELL
|
||||
if (cmux_ctx.state == 1 && cmux_ctx.log_state ==1){
|
||||
luat_cmux_write(LUAT_CMUX_CH_LOG, CMUX_FRAME_UIH & ~ CMUX_CONTROL_PF,s, l);
|
||||
}else
|
||||
#endif
|
||||
soc_debug_out(s, l);
|
||||
}
|
||||
|
||||
void luat_log_set_level(int level) {
|
||||
luat_log_level_cur = level;
|
||||
}
|
||||
|
||||
int luat_log_get_level() {
|
||||
return luat_log_level_cur;
|
||||
}
|
||||
|
||||
void luat_log_log(int level, const char* tag, const char* _fmt, ...) {
|
||||
if (luat_log_level_cur > level) return;
|
||||
char *tmp = (char *)log_printf_buff;
|
||||
switch (level)
|
||||
{
|
||||
case LUAT_LOG_DEBUG:
|
||||
log_printf_buff[0] = 'D';
|
||||
break;
|
||||
case LUAT_LOG_INFO:
|
||||
log_printf_buff[0] = 'I';
|
||||
break;
|
||||
case LUAT_LOG_WARN:
|
||||
log_printf_buff[0] = 'W';
|
||||
break;
|
||||
case LUAT_LOG_ERROR:
|
||||
log_printf_buff[0] = 'E';
|
||||
break;
|
||||
default:
|
||||
log_printf_buff[0] = '?';
|
||||
break;
|
||||
}
|
||||
log_printf_buff[1] = '/';
|
||||
tmp += 2;
|
||||
memcpy(tmp, tag, strlen(tag));
|
||||
log_printf_buff[2+strlen(tag)] = ' ';
|
||||
tmp += strlen(tag) + 1;
|
||||
|
||||
va_list args;
|
||||
va_start(args, _fmt);
|
||||
size_t len = vsnprintf_(tmp, LOGLOG_SIZE - (2 + strlen(tag) + 1 + 1 + 1), _fmt, args);
|
||||
va_end(args);
|
||||
if (len > 0) {
|
||||
len += 2 + strlen(tag) + 1;
|
||||
log_printf_buff[len] = 0;
|
||||
luat_log_write(log_printf_buff, len+1);
|
||||
}
|
||||
}
|
||||
|
||||
void luat_log_printf(int level, const char* _fmt, ...) {
|
||||
size_t len;
|
||||
va_list args;
|
||||
if (luat_log_level_cur > level) return;
|
||||
va_start(args, _fmt);
|
||||
len = vsnprintf_(log_printf_buff, LOGLOG_SIZE, _fmt, args);
|
||||
va_end(args);
|
||||
if (len > 0) {
|
||||
luat_log_write(log_printf_buff, len);
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* Copyright (c) 2022 OpenLuat & AirM2M
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
#include "common_api.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "wdt.h"
|
||||
#include "luat_pm.h"
|
||||
#include "luat_rtos.h"
|
||||
#include "luat_mobile.h"
|
||||
#include "luat_sms.h"
|
||||
#include "luat_network_adapter.h"
|
||||
#include "ps_event_callback.h"
|
||||
#include "networkmgr.h"
|
||||
#include "plat_config.h"
|
||||
#include "driver_gpio.h"
|
||||
#ifdef LUAT_USE_LVGL
|
||||
#include "lvgl.h"
|
||||
#include "luat_lvgl.h"
|
||||
#endif
|
||||
#include "luat_errdump.h"
|
||||
|
||||
extern int luat_main(void);
|
||||
extern void luat_heap_init(void);
|
||||
const char *soc_get_sdk_type(void)
|
||||
{
|
||||
return "LuatOS-SoC";
|
||||
}
|
||||
const char *soc_get_sdk_version(void)
|
||||
{
|
||||
return LUAT_BSP_VERSION;
|
||||
}
|
||||
luat_rtos_timer_t lvgl_timer_handle;
|
||||
#ifdef LUAT_USE_LVGL
|
||||
#define LVGL_TICK_PERIOD 10
|
||||
unsigned int g_lvgl_flash_time;
|
||||
static uint32_t lvgl_tick_cnt;
|
||||
|
||||
static int luat_lvg_handler(lua_State* L, void* ptr) {
|
||||
// DBG("%u", lv_tick_get());
|
||||
if (lvgl_tick_cnt) lvgl_tick_cnt--;
|
||||
lv_task_handler();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void luat_lvgl_callback(void *param){
|
||||
if (lvgl_tick_cnt < 5)
|
||||
{
|
||||
lvgl_tick_cnt++;
|
||||
rtos_msg_t msg = {0};
|
||||
msg.handler = luat_lvg_handler;
|
||||
luat_msgbus_put(&msg, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void luat_lvgl_tick_sleep(uint8_t OnOff)
|
||||
{
|
||||
if (!OnOff)
|
||||
{
|
||||
luat_rtos_timer_start(lvgl_timer_handle, LVGL_TICK_PERIOD, true, luat_lvgl_callback, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
luat_rtos_timer_stop(lvgl_timer_handle);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
void luat_lvgl_tick_sleep(uint8_t OnOff)
|
||||
{
|
||||
|
||||
}
|
||||
#endif
|
||||
extern int soc_mobile_get_default_pdp_part_info(uint8_t *ip_type, uint8_t *apn,uint8_t *apn_len, uint8_t *dns_num, ip_addr_t *dns_ip);
|
||||
|
||||
|
||||
//static INT32 ps_callback(PsEventID eventID, void *param, UINT32 paramLen)
|
||||
//{
|
||||
// NmAtiNetInfoInd *net_info = (NmAtiNetInfoInd *)param;
|
||||
// ip_addr_t dns_ip;
|
||||
// if(PS_URC_ID_PS_NETINFO == eventID)
|
||||
// {
|
||||
// if (NM_NETIF_ACTIVATED == net_info->netifInfo.netStatus)
|
||||
// {
|
||||
// if (net_info->netifInfo.ipv4Cid != 0xFF)
|
||||
// {
|
||||
// dns_ip.type = IPADDR_TYPE_V4;
|
||||
// dns_ip.u_addr.ip4 = net_info->netifInfo.ipv4Info.dns[0];
|
||||
// network_set_dns_server(NW_ADAPTER_INDEX_LWIP_GPRS, 2, &dns_ip);
|
||||
// dns_ip.u_addr.ip4 = net_info->netifInfo.ipv4Info.dns[1];
|
||||
// network_set_dns_server(NW_ADAPTER_INDEX_LWIP_GPRS, 3, &dns_ip);
|
||||
//
|
||||
// }
|
||||
// else if (net_info->netifInfo.ipv6Cid != 0xFF)
|
||||
// {
|
||||
// // dns_ip.type = IPADDR_TYPE_V6;
|
||||
// // dns_ip.u_addr.ip6 = net_info->netifInfo.ipv6Info.dns[0];
|
||||
// // network_set_dns_server(NW_ADAPTER_INDEX_LWIP_GPRS, 3, &dns_ip);
|
||||
// //dns_ip.u_addr.ip6 = net_info->netifInfo.ipv6Info.dns[1];
|
||||
// //network_set_dns_server(NW_ADAPTER_INDEX_LWIP_GPRS, 3, &dns_ip);
|
||||
// }
|
||||
// net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_GPRS, 1);
|
||||
// }
|
||||
// }
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
//static void dft_usb_recv_cb(uint8_t channel, uint8_t *input, uint32_t len){
|
||||
// if (input == NULL) {
|
||||
// switch(len){
|
||||
// case 0:
|
||||
// LLOGD("usb serial connected");
|
||||
// break;
|
||||
// default:
|
||||
// LLOGD("usb serial disconnected");
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//#ifdef LUAT_USE_SHELL
|
||||
// else {
|
||||
// luat_shell_push(input, len);
|
||||
// }
|
||||
//#endif
|
||||
//}
|
||||
extern int soc_get_model_name(char *model);
|
||||
|
||||
static void luat_main_print_model(void)
|
||||
{
|
||||
char temp[40] = {0};
|
||||
soc_get_model_name(temp);
|
||||
DBG("model %s", temp);
|
||||
}
|
||||
|
||||
static void luatos_task(void *param)
|
||||
{
|
||||
BSP_SetPlatConfigItemValue(PLAT_CONFIG_ITEM_FAULT_ACTION, EXCEP_OPTION_DUMP_FLASH_EPAT_RESET);
|
||||
if(BSP_GetPlatConfigItemValue(PLAT_CONFIG_ITEM_FAULT_ACTION) == EXCEP_OPTION_SILENT_RESET)
|
||||
ResetLockupCfg(true, true);
|
||||
else
|
||||
ResetLockupCfg(false, false);
|
||||
|
||||
luat_heap_init();
|
||||
luat_main_print_model();
|
||||
#ifdef LUAT_USE_MEDIA
|
||||
luat_audio_global_init();
|
||||
#endif
|
||||
// set_usb_serial_input_callback(dft_usb_recv_cb);
|
||||
//DBG("LuatOS starting ...");
|
||||
|
||||
#ifdef LUAT_USE_LVGL
|
||||
g_lvgl_flash_time = 33;
|
||||
lv_init();
|
||||
luat_rtos_timer_create(&lvgl_timer_handle);
|
||||
|
||||
#ifdef __LVGL_SLEEP_ENABLE__
|
||||
luat_lvgl_tick_sleep(1);
|
||||
#else
|
||||
luat_rtos_timer_start(lvgl_timer_handle, LVGL_TICK_PERIOD, true, luat_lvgl_callback, NULL);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
luat_pm_init();
|
||||
luat_main();
|
||||
while (1) {
|
||||
DBG("LuatOS exit"); // TODO 咋就没重启呢
|
||||
luat_rtos_task_sleep(15000);
|
||||
luat_os_reboot(0);
|
||||
}
|
||||
}
|
||||
|
||||
void luat_mobile_event_cb(LUAT_MOBILE_EVENT_E event, uint8_t index, uint8_t status);
|
||||
void luat_sms_recv_cb(uint32_t event, void *param);
|
||||
|
||||
static void luatos_mobile_event_callback(LUAT_MOBILE_EVENT_E event, uint8_t index, uint8_t status)
|
||||
{
|
||||
if (LUAT_MOBILE_EVENT_NETIF == event)
|
||||
{
|
||||
if (LUAT_MOBILE_NETIF_LINK_ON == status)
|
||||
{
|
||||
ip_addr_t dns_ip[2];
|
||||
uint8_t type, dns_num;
|
||||
dns_num = 2;
|
||||
soc_mobile_get_default_pdp_part_info(&type, NULL, NULL, &dns_num, dns_ip);
|
||||
if (dns_num > 0)
|
||||
{
|
||||
network_set_dns_server(NW_ADAPTER_INDEX_LWIP_GPRS, 2, &dns_ip[0]);
|
||||
if (dns_num > 1)
|
||||
{
|
||||
network_set_dns_server(NW_ADAPTER_INDEX_LWIP_GPRS, 3, &dns_ip[1]);
|
||||
}
|
||||
}
|
||||
net_lwip_set_link_state(NW_ADAPTER_INDEX_LWIP_GPRS, 1);
|
||||
}
|
||||
}
|
||||
luat_mobile_event_cb(event, index, status);
|
||||
}
|
||||
|
||||
static void luatos_task_init(void)
|
||||
{
|
||||
GPIO_GlobalInit(NULL);
|
||||
luat_mobile_event_register_handler(luatos_mobile_event_callback);
|
||||
luat_sms_init();
|
||||
luat_sms_recv_msg_register_handler(luat_sms_recv_cb);
|
||||
net_lwip_init();
|
||||
net_lwip_register_adapter(NW_ADAPTER_INDEX_LWIP_GPRS);
|
||||
network_register_set_default(NW_ADAPTER_INDEX_LWIP_GPRS);
|
||||
luat_rtos_task_handle task_handle;
|
||||
// xTaskCreateStatic(task1, "luatos", VM_STACK_SIZE, NULL, 20, s_vm_stackbuff, pxVMTaskTCBBuffer);
|
||||
luat_rtos_task_create(&task_handle, 16 * 1024, 80, "luatos", luatos_task, NULL, 0);
|
||||
|
||||
}
|
||||
extern void luat_pm_preinit(void);
|
||||
INIT_DRV_EXPORT(luat_pm_preinit, "1");
|
||||
INIT_HW_EXPORT(luatos_task_init, "1");
|
||||
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
|
||||
#include "common_api.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>//add for memset
|
||||
#include "bget.h"
|
||||
#include "luat_base.h"
|
||||
#include "luat_malloc.h"
|
||||
|
||||
#define LUAT_LOG_TAG "vmheap"
|
||||
#include "luat_log.h"
|
||||
|
||||
|
||||
#include "mm_debug.h"//add for memory leak debug
|
||||
#include "exception_process.h"
|
||||
#include "cmsis_compiler.h"
|
||||
#include "tlsf.h"
|
||||
#include "mem_map.h"
|
||||
#ifndef LUAT_HEAP_SIZE
|
||||
#ifdef LOW_SPEED_SERVICE_ONLY
|
||||
#define LUAT_HEAP_SIZE (200*1024)
|
||||
#else
|
||||
#define LUAT_HEAP_SIZE (128*1024)
|
||||
#endif
|
||||
#endif
|
||||
static uint8_t vmheap[LUAT_HEAP_SIZE] __attribute__((aligned(8)));
|
||||
|
||||
|
||||
//------------------------------------------------
|
||||
// ---------- 管理 LuaVM所使用的内存----------------
|
||||
|
||||
#if 1
|
||||
void luat_heap_init(void) {
|
||||
bpool(vmheap, LUAT_HEAP_SIZE);
|
||||
}
|
||||
|
||||
void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
if (ptr == NULL && nsize == 0)
|
||||
return NULL;
|
||||
#if LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
|
||||
if (ptr != NULL) {
|
||||
uint32_t addr = (uint32_t) ptr;
|
||||
if (addr <= (uint32_t)vmheap || addr >= (uint32_t)(vmheap + LUAT_HEAP_SIZE)) {
|
||||
//LLOGD("skip ROM free %p", ptr);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (nsize)
|
||||
{
|
||||
void* ptmp = bgetr(ptr, nsize);
|
||||
if(ptmp == NULL && osize >= nsize)
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
return ptmp;
|
||||
}
|
||||
brel(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
|
||||
long curalloc, totfree, maxfree;
|
||||
unsigned long nget, nrel;
|
||||
bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
|
||||
*used = curalloc;
|
||||
*max_used = bstatsmaxget();
|
||||
*total = curalloc + totfree;
|
||||
}
|
||||
|
||||
#else
|
||||
static tlsf_t luavm_tlsf;
|
||||
|
||||
void luat_heap_init(void) {
|
||||
luavm_tlsf = tlsf_create_with_pool((void*)vmheap, LUAT_HEAP_SIZE);
|
||||
}
|
||||
|
||||
void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
if (ptr == NULL && nsize == 0)
|
||||
return NULL;
|
||||
#if LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
|
||||
if (ptr != NULL && nsize == 0) {
|
||||
uint32_t addr = (uint32_t) ptr;
|
||||
if (addr <= (uint32_t)vmheap || addr >= (uint32_t)(vmheap + LUAT_HEAP_SIZE)) {
|
||||
//LLOGD("skip ROM free %p", ptr);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return tlsf_realloc(luavm_tlsf, ptr, nsize, (size_t)luat_heap_alloc);
|
||||
}
|
||||
void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
|
||||
*total = LUAT_HEAP_SIZE;
|
||||
*used = LUAT_HEAP_SIZE - tlsf_mem_size_free(luavm_tlsf);
|
||||
*max_used = LUAT_HEAP_SIZE - tlsf_mem_size_ever_min(luavm_tlsf);
|
||||
}
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
|
||||
#include "luat_msgbus.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "queue.h"
|
||||
|
||||
#define QUEUE_LENGTH 0xFF
|
||||
#define ITEM_SIZE sizeof(rtos_msg_t)
|
||||
|
||||
static StaticQueue_t xStaticQueue = {0};
|
||||
static QueueHandle_t xQueue = {0};
|
||||
|
||||
#if configSUPPORT_STATIC_ALLOCATION
|
||||
static uint8_t ucQueueStorageArea[ QUEUE_LENGTH * ITEM_SIZE ] __attribute__((aligned (16)));
|
||||
#endif
|
||||
|
||||
void luat_msgbus_init(void) {
|
||||
if (!xQueue) {
|
||||
#if configSUPPORT_STATIC_ALLOCATION
|
||||
xQueue = xQueueCreateStatic( QUEUE_LENGTH,
|
||||
ITEM_SIZE,
|
||||
ucQueueStorageArea,
|
||||
&xStaticQueue );
|
||||
#else
|
||||
xQueue = xQueueCreate(QUEUE_LENGTH, ITEM_SIZE);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
|
||||
if (xQueue == NULL)
|
||||
return 1;
|
||||
return xQueueSendFromISR(xQueue, msg, NULL) == pdTRUE ? 0 : 1;
|
||||
}
|
||||
uint32_t luat_msgbus_get(rtos_msg_t* msg, size_t timeout) {
|
||||
if (xQueue == NULL)
|
||||
return 1;
|
||||
return xQueueReceive(xQueue, msg, timeout) == pdTRUE ? 0 : 1;
|
||||
}
|
||||
uint32_t luat_msgbus_freesize(void) {
|
||||
if (xQueue == NULL)
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
#include "luat_base.h"
|
||||
#include "luat_pm.h"
|
||||
#include "luat_msgbus.h"
|
||||
|
||||
#include "bsp.h"
|
||||
#include "bsp_custom.h"
|
||||
#include "osasys.h"
|
||||
#include "ostask.h"
|
||||
#include "slpman.h"
|
||||
#include "reset.h"
|
||||
//#include "psproxytask.h"
|
||||
#include "driver_gpio.h"
|
||||
#include "common_api.h"
|
||||
#include "plat_config.h"
|
||||
#include "ps_event_callback.h"
|
||||
#include "cmips.h"
|
||||
#include "ps_lib_api.h"
|
||||
|
||||
#define LUAT_LOG_TAG "luat.pm"
|
||||
#include "luat_log.h"
|
||||
|
||||
static uint8_t lastRequestMode = SLP_IDLE_STATE; // 在APP启动时设置
|
||||
static uint8_t wakeupSrc = 0;
|
||||
static uint8_t firstSlpstate;
|
||||
static uint8_t wakeup_deeptimer_id = 0xFF;
|
||||
|
||||
static const char slpStateText[5][5]={{"Actv"},{"Idle"},{"Slp1"},{"Slp2"},{"Hibn"}};
|
||||
static const char wakeupSrcStr[3][4] = {{"POR"}, {"RTC"}, {"IO"}};
|
||||
extern void soc_set_usb_sleep(uint8_t onoff);
|
||||
uint32_t inParam = 0xAABBCCDD;
|
||||
|
||||
static int luat_dtimer_cb(lua_State *L, void* ptr) {
|
||||
rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
|
||||
lua_getglobal(L, "sys_pub");
|
||||
if (lua_isfunction(L, -1)) {
|
||||
lua_pushstring(L, "DTIMER_WAKEUP");
|
||||
lua_pushinteger(L, msg->arg1);
|
||||
lua_call(L, 2, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void appTimerExpFunc(uint8_t id) {
|
||||
wakeup_deeptimer_id = id;
|
||||
LLOGI("DeepTimer Wakeup by id=%d", id);
|
||||
}
|
||||
|
||||
static slpManSlpState_t luat_user_slp_state(void)
|
||||
{
|
||||
return lastRequestMode;
|
||||
}
|
||||
|
||||
int luat_pm_request(int mode) {
|
||||
|
||||
LLOGI("request mode=%ld, prev mode=%ld", mode, lastRequestMode);
|
||||
if (mode < 0 || mode > LUAT_PM_SLEEP_MODE_STANDBY) {
|
||||
LLOGW("bad mode=%ld", mode);
|
||||
return -2;
|
||||
}
|
||||
lastRequestMode = mode;
|
||||
soc_set_usb_sleep(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_pm_release(int mode) {
|
||||
soc_set_usb_sleep(0);
|
||||
lastRequestMode = LUAT_PM_SLEEP_MODE_IDLE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_pm_dtimer_start(int id, size_t timeout) {
|
||||
if (id < 0 || id > DEEPSLP_TIMER_ID6) {
|
||||
return -1;
|
||||
}
|
||||
slpManDeepSlpTimerStart(id, timeout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_pm_dtimer_stop(int id) {
|
||||
if (id < 0 || id > DEEPSLP_TIMER_ID6) {
|
||||
return -1;
|
||||
}
|
||||
slpManDeepSlpTimerDel(id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_pm_dtimer_check(int id) {
|
||||
if (id < 0 || id > DEEPSLP_TIMER_ID6) {
|
||||
return false;
|
||||
}
|
||||
LLOGD("dtimer check id %d, remain %d ms", id, slpManDeepSlpTimerRemainMs(id));
|
||||
if (slpManDeepSlpTimerRemainMs(id) <= 500)
|
||||
{
|
||||
slpManDeepSlpTimerDel(id);
|
||||
}
|
||||
return slpManDeepSlpTimerIsRunning(id);
|
||||
}
|
||||
|
||||
int luat_pm_last_state(int *lastState, int *rtcOrPad) {
|
||||
*lastState = firstSlpstate;
|
||||
*rtcOrPad = wakeupSrc;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_pm_force(int mode) {
|
||||
if (mode < 0 || mode > LUAT_PM_SLEEP_MODE_STANDBY) {
|
||||
LLOGW("bad mode=%ld", mode);
|
||||
return -2;
|
||||
}
|
||||
LLOGI("request mode=%ld, prev mode=%ld", mode, lastRequestMode);
|
||||
lastRequestMode = mode;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_pm_check(void) {
|
||||
|
||||
return lastRequestMode;
|
||||
}
|
||||
|
||||
int luat_pm_dtimer_list(size_t* c, size_t* dlist) {
|
||||
for (uint8_t i = 0; i<= DEEPSLP_TIMER_ID6; i++) {
|
||||
if (slpManDeepSlpTimerIsRunning(i)) {
|
||||
uint32_t retime = slpManDeepSlpTimerRemainMs(i);
|
||||
if (retime != 0xffffffff) {
|
||||
*(dlist+i) = retime;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_pm_dtimer_wakeup_id(int* id) {
|
||||
if (wakeup_deeptimer_id != 0xFF) {
|
||||
*id = wakeup_deeptimer_id;
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------
|
||||
void luat_pm_preinit(void)
|
||||
{
|
||||
for(uint8_t i = 0; i <= DEEPSLP_TIMER_ID6; i++)
|
||||
{
|
||||
slpManDeepSlpTimerRegisterExpCb(i, appTimerExpFunc);
|
||||
}
|
||||
}
|
||||
|
||||
void luat_pm_init(void) {
|
||||
LLOGI("pm mode %d", apmuGetDeepestSleepMode());
|
||||
apmuSetDeepestSleepMode(AP_STATE_HIBERNATE);
|
||||
soc_set_usb_sleep(0);
|
||||
slpManSlpState_t slpstate = slpManGetLastSlpState();
|
||||
slpManWakeSrc_e src = slpManGetWakeupSrc();
|
||||
wakeupSrc = (uint8_t)src;
|
||||
if (src > WAKEUP_FROM_PAD)
|
||||
{
|
||||
src = WAKEUP_FROM_PAD;
|
||||
}
|
||||
if (slpstate == SLP_SLP2_STATE) {
|
||||
LLOGI("poweron: Wakup Sleep2 by %s %d", wakeupSrcStr[src], wakeup_deeptimer_id);
|
||||
firstSlpstate = LUAT_PM_SLEEP_MODE_DEEP;
|
||||
}
|
||||
else if (slpstate == SLP_HIB_STATE) {
|
||||
LLOGI("poweron: Wakup Hib by %s %d", wakeupSrcStr[src], wakeup_deeptimer_id);
|
||||
firstSlpstate = LUAT_PM_SLEEP_MODE_STANDBY;
|
||||
}
|
||||
else {
|
||||
firstSlpstate = LUAT_PM_SLEEP_MODE_NONE;
|
||||
LLOGI("poweron: Power/Reset");
|
||||
}
|
||||
slpManRegisterUsrSlpDepthCb(luat_user_slp_state);
|
||||
if (wakeup_deeptimer_id != 0xff)
|
||||
{
|
||||
rtos_msg_t msg = {0};
|
||||
msg.handler = luat_dtimer_cb;
|
||||
msg.arg1 = wakeup_deeptimer_id;
|
||||
luat_msgbus_put(&msg, 0);
|
||||
}
|
||||
}
|
||||
|
||||
int luat_pm_get_poweron_reason(void)
|
||||
{
|
||||
LastResetState_e apRstState,cpRstState;
|
||||
ResetStateGet(&apRstState, &cpRstState);
|
||||
int id = 0;
|
||||
switch(apRstState)
|
||||
{
|
||||
case LAST_RESET_POR:
|
||||
case LAST_RESET_NORMAL:
|
||||
id = 0;
|
||||
break;
|
||||
case LAST_RESET_SWRESET:
|
||||
id = 3;
|
||||
break;
|
||||
case LAST_RESET_HARDFAULT:
|
||||
case LAST_RESET_ASSERT:
|
||||
id = 6;
|
||||
break;
|
||||
case LAST_RESET_WDTSW:
|
||||
case LAST_RESET_WDTHW:
|
||||
case LAST_RESET_LOCKUP:
|
||||
case LAST_RESET_AONWDT:
|
||||
id = 8;
|
||||
break;
|
||||
case LAST_RESET_BATLOW:
|
||||
case LAST_RESET_TEMPHI:
|
||||
id = 9;
|
||||
break;
|
||||
case LAST_RESET_FOTA:
|
||||
id = 1;
|
||||
break;
|
||||
default:
|
||||
id = 4;
|
||||
break;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
///---------------------------------------
|
||||
|
||||
extern void pwrKeyStartPowerOff(void);
|
||||
int luat_pm_poweroff(void)
|
||||
{
|
||||
pwrKeyStartPowerOff();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_pm_power_ctrl(int id, uint8_t onoff)
|
||||
{
|
||||
switch(id)
|
||||
{
|
||||
case LUAT_PM_POWER_USB:
|
||||
soc_set_usb_sleep(!onoff);
|
||||
soc_usb_onoff(onoff);
|
||||
break;
|
||||
case LUAT_PM_POWER_GPS:
|
||||
GPIO_IomuxEC618(GPIO_ToPadEC618(HAL_GPIO_13, 4), 4, 0, 0);
|
||||
GPIO_Config(HAL_GPIO_13, 0, onoff);
|
||||
break;
|
||||
case LUAT_PM_POWER_GPS_ANT:
|
||||
case LUAT_PM_POWER_DAC_EN_PIN:
|
||||
GPIO_IomuxEC618(GPIO_ToPadEC618(HAL_GPIO_12, 4), 4, 0, 0);
|
||||
GPIO_Config(HAL_GPIO_12, 0, onoff);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#include "common_api.h"
|
||||
#include "luat_base.h"
|
||||
#include "luat_pm.h"
|
||||
#include "luat_msgbus.h"
|
||||
|
||||
#include "flash_rt.h"
|
||||
#include "mem_map.h"
|
||||
|
||||
|
||||
#define LUAT_LOG_TAG "sfd"
|
||||
#include "luat_log.h"
|
||||
|
||||
int sfd_onchip_init (void* userdata) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sfd_onchip_status (void* userdata) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sfd_onchip_read (void* userdata, char* buf, size_t offset, size_t size) {
|
||||
int ret = 0;
|
||||
if (size == 0)
|
||||
return 0;
|
||||
ret = BSP_QSPI_Read_Safe((uint8_t *)buf, (FLASH_FDB_REGION_START + offset), size);
|
||||
return ret == 0 ? size : -1;
|
||||
}
|
||||
|
||||
int sfd_onchip_write (void* userdata, const char* buf, size_t offset, size_t size) {
|
||||
int ret = 0;
|
||||
if (size == 0)
|
||||
return 0;
|
||||
// 注意, BSP_QSPI_Write_Safe 的buf不能是flash上的常量数据
|
||||
// 写入flash时XIP会关闭, 导致buf值肯定读不到
|
||||
// 下面的各种判断, 就是把常量数据拷贝到ram, 然后写入
|
||||
uint8_t tmp_small[256]; // 这里改成256的原因是fskv总是按256字节写
|
||||
uint8_t *tmp = NULL;
|
||||
uint32_t addr = (uint32_t)buf;
|
||||
if (size <= 256) {
|
||||
// 对于较小的数据, 直接在栈内存里拷贝即可,不必判断
|
||||
memcpy(tmp_small, buf, size);
|
||||
ret = BSP_QSPI_Write_Safe((uint8_t *)tmp_small, (FLASH_FDB_REGION_START + offset), size);
|
||||
}
|
||||
else if (addr >= 0x00400000 && addr <= 0x00500000) {
|
||||
// 数据已经处于ram, 可以直接写入
|
||||
ret = BSP_QSPI_Write_Safe((uint8_t *)buf, (FLASH_FDB_REGION_START + offset), size);
|
||||
}
|
||||
else {
|
||||
// 超过256字节的常量数据, 应该是不存在的吧, 下面的逻辑主要是防御代码.
|
||||
tmp = malloc(size);
|
||||
if (tmp == NULL) {
|
||||
DBG("out of memory when malloc flash write buff");
|
||||
return -1;
|
||||
}
|
||||
memcpy(tmp, buf, size);
|
||||
ret = BSP_QSPI_Write_Safe((uint8_t *)tmp, (FLASH_FDB_REGION_START + offset), size);
|
||||
free(tmp);
|
||||
}
|
||||
return ret == 0 ? size : -1;
|
||||
}
|
||||
int sfd_onchip_erase (void* userdata, size_t offset, size_t size) {
|
||||
int ret = 0;
|
||||
ret = BSP_QSPI_Erase_Safe((FLASH_FDB_REGION_START + offset), size);
|
||||
return ret == 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
int sfd_onchip_ioctl (void* userdata, size_t cmd, void* buff) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// #endif
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
#include "common_api.h"
|
||||
|
||||
#include "luat_base.h"
|
||||
#include "luat_uart.h"
|
||||
#include "luat_msgbus.h"
|
||||
#include "luat_shell.h"
|
||||
|
||||
#define LUAT_LOG_TAG "usb"
|
||||
#include "luat_log.h"
|
||||
|
||||
void luat_shell_write(char* buff, size_t len) {
|
||||
luat_uart_write(LUAT_VUART_ID_0, buff, len);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "luat_base.h"
|
||||
#include "luat_gpio.h"
|
||||
#include "luat_spi.h"
|
||||
|
||||
#define LUAT_WEAK __attribute__((weak))
|
||||
#define LUAT_SPI_CS_SELECT 0
|
||||
#define LUAT_SPI_CS_CLEAR 1
|
||||
|
||||
|
||||
// luat_spi_device_t* 在lua层看到的是一个userdata
|
||||
LUAT_WEAK int luat_spi_device_setup(luat_spi_device_t* spi_dev) {
|
||||
luat_spi_bus_setup(spi_dev);
|
||||
if (spi_dev->spi_config.cs != 255)
|
||||
luat_gpio_mode(spi_dev->spi_config.cs, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // CS
|
||||
return 0;
|
||||
}
|
||||
|
||||
//关闭SPI设备,成功返回0
|
||||
LUAT_WEAK int luat_spi_device_close(luat_spi_device_t* spi_dev) {
|
||||
return luat_spi_close(spi_dev->bus_id);
|
||||
}
|
||||
|
||||
//收发SPI数据,返回接收字节数
|
||||
LUAT_WEAK int luat_spi_device_transfer(luat_spi_device_t* spi_dev, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) {
|
||||
int ret = -1;
|
||||
luat_spi_device_config(spi_dev);
|
||||
if (spi_dev->spi_config.cs != 255)
|
||||
luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
|
||||
if (spi_dev->spi_config.mode){
|
||||
ret = luat_spi_transfer(spi_dev->bus_id, send_buf, send_length, recv_buf, recv_length);
|
||||
}else{
|
||||
ret = luat_spi_send(spi_dev->bus_id, send_buf, send_length);
|
||||
ret = luat_spi_recv(spi_dev->bus_id, recv_buf, recv_length);
|
||||
}
|
||||
if (spi_dev->spi_config.cs != 255)
|
||||
luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//收SPI数据,返回接收字节数
|
||||
LUAT_WEAK int luat_spi_device_recv(luat_spi_device_t* spi_dev, char* recv_buf, size_t length) {
|
||||
luat_spi_device_config(spi_dev);
|
||||
if (spi_dev->spi_config.cs != 255)
|
||||
luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
|
||||
int ret = luat_spi_recv(spi_dev->bus_id, recv_buf, length);
|
||||
if (spi_dev->spi_config.cs != 255)
|
||||
luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//发SPI数据,返回发送字节数
|
||||
LUAT_WEAK int luat_spi_device_send(luat_spi_device_t* spi_dev, const char* send_buf, size_t length) {
|
||||
luat_spi_device_config(spi_dev);
|
||||
if (spi_dev->spi_config.cs != 255)
|
||||
luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
|
||||
int ret = luat_spi_send(spi_dev->bus_id, send_buf, length);
|
||||
if (spi_dev->spi_config.cs != 255)
|
||||
luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "luat_network_adapter.h"
|
||||
#include "common_api.h"
|
||||
#include "luat_rtos.h"
|
||||
|
||||
static luat_rtos_task_handle g_s_task_handle;
|
||||
static network_ctrl_t *g_s_network_ctrl;
|
||||
static int32_t luat_test_socket_callback(void *pdata, void *param)
|
||||
{
|
||||
OS_EVENT *event = (OS_EVENT *)pdata;
|
||||
DBG("%x", event->ID);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void luat_test_task(void *param)
|
||||
{
|
||||
g_s_network_ctrl = network_alloc_ctrl(NW_ADAPTER_INDEX_LWIP_GPRS);
|
||||
network_init_ctrl(g_s_network_ctrl, g_s_task_handle, luat_test_socket_callback, NULL);
|
||||
network_set_base_mode(g_s_network_ctrl, 1, 15000, 1, 300, 5, 9);
|
||||
g_s_network_ctrl->is_debug = 1;
|
||||
const char remote_ip[] = "112.125.89.8";
|
||||
const char hello[] = "hello, luatos!";
|
||||
uint8_t *rx_data = malloc(1024);
|
||||
uint32_t tx_len, rx_len;
|
||||
int result;
|
||||
uint8_t is_break,is_timeout;
|
||||
while(1)
|
||||
{
|
||||
result = network_connect(g_s_network_ctrl, remote_ip, sizeof(remote_ip), NULL, 35948, 30000);
|
||||
DBG("%d", result);
|
||||
if (!result)
|
||||
{
|
||||
result = network_tx(g_s_network_ctrl, hello, sizeof(hello), 0, NULL, 0, &tx_len, 15000);
|
||||
DBG("%d", result);
|
||||
if (!result)
|
||||
{
|
||||
while(!result)
|
||||
{
|
||||
result = network_wait_rx(g_s_network_ctrl, 30000, &is_break, &is_timeout);
|
||||
DBG("%d", result);
|
||||
if (!result)
|
||||
{
|
||||
if (!is_timeout && !is_break)
|
||||
{
|
||||
do
|
||||
{
|
||||
result = network_rx(g_s_network_ctrl, rx_data, 1024, 0, NULL, NULL, &rx_len);
|
||||
DBG("%d", result);
|
||||
if (rx_len > 0)
|
||||
{
|
||||
network_tx(g_s_network_ctrl, rx_data, rx_len, 0, NULL, 0, &tx_len, 0);
|
||||
}
|
||||
}while(!result && rx_len > 0);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
network_close(g_s_network_ctrl, 5000);
|
||||
luat_rtos_task_sleep(15000);
|
||||
}
|
||||
}
|
||||
|
||||
static void luat_test_init(void)
|
||||
{
|
||||
luat_rtos_task_create(&g_s_task_handle, 2 * 1024, 10, "test", luat_test_task, NULL, 16);
|
||||
}
|
||||
|
||||
//INIT_TASK_EXPORT(luat_test_init, "1");
|
||||
@@ -0,0 +1,102 @@
|
||||
|
||||
#include "luat_base.h"
|
||||
#include "luat_malloc.h"
|
||||
#include "luat_timer.h"
|
||||
#include "luat_msgbus.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "timers.h"
|
||||
#define LUAT_LOG_TAG "timer"
|
||||
#include "luat_log.h"
|
||||
|
||||
#define FREERTOS_TIMER_COUNT 64
|
||||
static luat_timer_t* timers[FREERTOS_TIMER_COUNT] __attribute__((aligned (16)));
|
||||
|
||||
static void luat_timer_callback(TimerHandle_t xTimer) {
|
||||
//LLOGD("timer callback");
|
||||
rtos_msg_t msg;
|
||||
size_t timer_id = (size_t)pvTimerGetTimerID(xTimer);
|
||||
luat_timer_t *timer = luat_timer_get(timer_id);
|
||||
if (timer == NULL)
|
||||
return;
|
||||
msg.handler = timer->func;
|
||||
msg.ptr = timer;
|
||||
msg.arg1 = timer_id;
|
||||
msg.arg2 = 0;
|
||||
luat_msgbus_put(&msg, 0);
|
||||
// int re = luat_msgbus_put(&msg, 0);
|
||||
//LLOGD("timer msgbus re=%ld", re);
|
||||
}
|
||||
|
||||
static int nextTimerSlot() {
|
||||
for (size_t i = 0; i < FREERTOS_TIMER_COUNT; i++)
|
||||
{
|
||||
if (timers[i] == NULL) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int luat_timer_start(luat_timer_t* timer) {
|
||||
TimerHandle_t os_timer;
|
||||
int timerIndex;
|
||||
//LLOGD(">>luat_timer_start timeout=%ld", timer->timeout);
|
||||
timerIndex = nextTimerSlot();
|
||||
if (timerIndex < 0) {
|
||||
LLOGE("too many timers");
|
||||
return 1; // too many timer!!
|
||||
}
|
||||
os_timer = xTimerCreate("luat_timer", timer->timeout, 0 == timer->repeat ? 0 : 1, (void*)(timer->id), luat_timer_callback);
|
||||
//LLOGD("timer id=%ld, osTimerNew=%p", timerIndex, os_timer);
|
||||
if (!os_timer) {
|
||||
LLOGE("xTimerCreate FAIL");
|
||||
return -1;
|
||||
}
|
||||
timers[timerIndex] = timer;
|
||||
|
||||
timer->os_timer = os_timer;
|
||||
int re = xTimerStart(os_timer, 5);
|
||||
//LLOGD("timer id=%ld timeout=%ld start=%ld", timerIndex, timer->timeout, re);
|
||||
if (re != pdPASS) {
|
||||
LLOGE("xTimerStart FAIL");
|
||||
xTimerDelete(os_timer, 5);
|
||||
timers[timerIndex] = NULL;
|
||||
}
|
||||
return re == pdPASS ? 0 : -1;
|
||||
}
|
||||
|
||||
int luat_timer_stop(luat_timer_t* timer) {
|
||||
if (timer == NULL || timer->os_timer == NULL)
|
||||
return 1;
|
||||
for (size_t i = 0; i < FREERTOS_TIMER_COUNT; i++)
|
||||
{
|
||||
if (timers[i] == timer) {
|
||||
timers[i] = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
xTimerStop((TimerHandle_t)timer->os_timer, 1);
|
||||
xTimerDelete((TimerHandle_t)timer->os_timer, 1);
|
||||
timer->os_timer = NULL;
|
||||
return 0;
|
||||
};
|
||||
|
||||
luat_timer_t* luat_timer_get(size_t timer_id) {
|
||||
for (size_t i = 0; i < FREERTOS_TIMER_COUNT; i++)
|
||||
{
|
||||
if (timers[i] && timers[i]->id == timer_id) {
|
||||
return timers[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
int luat_timer_mdelay(size_t ms) {
|
||||
if (ms > 0) {
|
||||
vTaskDelay(ms);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "luat_base.h"
|
||||
#include "u8g2.h"
|
||||
#include "luat_u8g2.h"
|
||||
|
||||
uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
|
||||
uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
|
||||
uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
|
||||
|
||||
uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
|
||||
uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
|
||||
uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr);
|
||||
|
||||
int luat_u8g2_setup_default(luat_u8g2_conf_t *conf);
|
||||
|
||||
int luat_u8g2_setup(luat_u8g2_conf_t *conf) {
|
||||
return luat_u8g2_setup_default(conf);
|
||||
}
|
||||
|
||||
uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
|
||||
return u8x8_luat_gpio_and_delay_default(u8x8, msg, arg_int, arg_ptr);
|
||||
}
|
||||
|
||||
uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
|
||||
return u8x8_luat_byte_hw_i2c_default(u8x8, msg, arg_int, arg_ptr);
|
||||
}
|
||||
|
||||
uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
|
||||
return u8x8_luat_byte_4wire_hw_spi_default(u8x8, msg, arg_int, arg_ptr);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "luat_base.h"
|
||||
#include "luat_wdt.h"
|
||||
|
||||
|
||||
int luat_wdt_setup(size_t timeout);
|
||||
|
||||
int luat_wdt_init(size_t timeout) {
|
||||
return luat_wdt_setup(timeout);
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#include "luat_base.h"
|
||||
#include "luat_wlan.h"
|
||||
#include "luat_rtos.h"
|
||||
|
||||
#include "common_api.h"
|
||||
#include "ps_lib_api.h"
|
||||
|
||||
#define WLAN_SCAN_DONE 1
|
||||
|
||||
static const SetWifiScanParams wifiscanreq = {
|
||||
.maxTimeOut = 10000,
|
||||
.round = 1,
|
||||
.maxBssidNum = CMI_DEV_MAX_WIFI_BSSID_NUM,
|
||||
.scanTimeOut = 5,
|
||||
.wifiPriority = 0
|
||||
};
|
||||
|
||||
static GetWifiScanInfo *pWifiScanInfo = PNULL;
|
||||
|
||||
static luat_rtos_task_handle wlan_task_handle;
|
||||
|
||||
static int l_wlan_handler(lua_State *L, void* ptr) {
|
||||
rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
|
||||
int32_t event_id = msg->arg1;
|
||||
lua_getglobal(L, "sys_pub");
|
||||
switch (event_id)
|
||||
{
|
||||
case WLAN_SCAN_DONE:
|
||||
DBG("wifi scan done");
|
||||
lua_pushstring(L, "WLAN_SCAN_DONE");
|
||||
lua_call(L, 1, 0);
|
||||
break;
|
||||
default:
|
||||
DBG("unkown event %d", event_id);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void wlan_task(void *param){
|
||||
rtos_msg_t msg = {0};
|
||||
msg.handler = l_wlan_handler;
|
||||
msg.arg1 = WLAN_SCAN_DONE;
|
||||
if (pWifiScanInfo == NULL)
|
||||
pWifiScanInfo = (GetWifiScanInfo *)malloc(sizeof(GetWifiScanInfo));
|
||||
if (pWifiScanInfo) {
|
||||
memset(pWifiScanInfo, 0, sizeof(GetWifiScanInfo));
|
||||
appGetWifiScanInfo(&wifiscanreq, pWifiScanInfo);
|
||||
luat_msgbus_put(&msg, 0);
|
||||
}
|
||||
else {
|
||||
DBG("out of memory when malloc GetWifiScanInfo");
|
||||
}
|
||||
luat_rtos_task_delete(wlan_task_handle);
|
||||
}
|
||||
|
||||
int luat_wlan_init(luat_wlan_config_t *conf){
|
||||
DBG("wifi support scan only");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_wlan_scan(void){
|
||||
if (luat_rtos_task_create(&wlan_task_handle, 2048, 20, "wlan", wlan_task, NULL, NULL)){
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int luat_wlan_scan_get_result(luat_wlan_scan_result_t *results, int ap_limit){
|
||||
if (pWifiScanInfo == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if (ap_limit > pWifiScanInfo->bssidNum){
|
||||
ap_limit = pWifiScanInfo->bssidNum;
|
||||
}
|
||||
for (size_t i = 0; i < ap_limit; i++){
|
||||
memcpy(results[i].ssid, pWifiScanInfo->ssidHex[i], pWifiScanInfo->ssidHexLen[i]);
|
||||
memcpy(results[i].bssid, pWifiScanInfo->bssid[i], 6);
|
||||
results[i].rssi = pWifiScanInfo->rssi[i];
|
||||
}
|
||||
return ap_limit;
|
||||
}
|
||||
Reference in New Issue
Block a user