Skip to content

Commit a8d48ca

Browse files
ota, lfs, fast connect fix
1 parent 4704a34 commit a8d48ca

File tree

12 files changed

+272
-40
lines changed

12 files changed

+272
-40
lines changed

.github/workflows/workflow.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ jobs:
449449
with:
450450
repository: Ameba-AIoT/ameba-rtos
451451
path: sdk/ameba-rtos
452-
ref: release/v1.1
452+
#ref: release/v1.1
453+
ref: 6a93a16d819707d56beac3a8b829acb41a8634fc # pin commit until stable release
453454
submodules: recursive
454455
- uses: actions/setup-python@v5
455456
with:

Makefile

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -573,19 +573,17 @@ OpenRTL8720D: prebuild_OpenRTL8720D
573573
OpenRTL8721DA: prebuild_OpenRTL8721DA
574574
cd sdk/ameba-rtos/amebadplus_gcc_project && APP_VERSION=$(APP_VERSION) OBK_VARIANT=$(OBK_VARIANT) ./build.py -a ../../../platforms/RTL8721DA
575575
mkdir -p output/$(APP_VERSION)
576-
touch /tmp/OpenRTL8721DA_$(APP_VERSION).bin
577-
dd conv=notrunc bs=1 if=sdk/ameba-rtos/amebadplus_gcc_project/km4_boot_all.bin of=/tmp/OpenRTL8721DA_$(APP_VERSION).bin seek=0
578-
dd conv=notrunc bs=1 if=sdk/ameba-rtos/amebadplus_gcc_project/km0_km4_app.bin of=/tmp/OpenRTL8721DA_$(APP_VERSION).bin seek=$(shell printf "%d" 0x14000)
576+
cp sdk/ameba-rtos/amebadplus_gcc_project/km4_boot_all.bin /tmp/OpenRTL8721DA_$(APP_VERSION).bin
577+
dd conv=notrunc bs=1K if=sdk/ameba-rtos/amebadplus_gcc_project/km0_km4_app.bin of=/tmp/OpenRTL8721DA_$(APP_VERSION).bin seek=80
579578
mv /tmp/OpenRTL8721DA_$(APP_VERSION).bin output/$(APP_VERSION)/
580579
cp sdk/ameba-rtos/amebadplus_gcc_project/ota_all.bin output/$(APP_VERSION)/OpenRTL8721DA_$(APP_VERSION)_ota.img
581580

582581
.PHONY: OpenRTL8720E
583582
OpenRTL8720E: prebuild_OpenRTL8720E
584583
cd sdk/ameba-rtos/amebalite_gcc_project && APP_VERSION=$(APP_VERSION) OBK_VARIANT=$(OBK_VARIANT) ./build.py -a ../../../platforms/RTL8720E
585584
mkdir -p output/$(APP_VERSION)
586-
touch /tmp/OpenRTL8720E_$(APP_VERSION).bin
587-
dd conv=notrunc bs=1 if=sdk/ameba-rtos/amebalite_gcc_project/km4_boot_all.bin of=/tmp/OpenRTL8720E_$(APP_VERSION).bin seek=0
588-
dd conv=notrunc bs=1 if=sdk/ameba-rtos/amebalite_gcc_project/kr4_km4_app.bin of=/tmp/OpenRTL8720E_$(APP_VERSION).bin seek=$(shell printf "%d" 0x14000)
585+
cp sdk/ameba-rtos/amebalite_gcc_project/km4_boot_all.bin /tmp/OpenRTL8720E_$(APP_VERSION).bin
586+
dd conv=notrunc bs=1K if=sdk/ameba-rtos/amebalite_gcc_project/kr4_km4_app.bin of=/tmp/OpenRTL8720E_$(APP_VERSION).bin seek=80
589587
mv /tmp/OpenRTL8720E_$(APP_VERSION).bin output/$(APP_VERSION)/
590588
cp sdk/ameba-rtos/amebalite_gcc_project/ota_all.bin output/$(APP_VERSION)/OpenRTL8720E_$(APP_VERSION)_ota.img
591589

sdk/ameba-rtos

Submodule ameba-rtos updated 57 files

src/hal/espidf/hal_main_espidf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void app_main(void)
4444
#if !CONFIG_IDF_TARGET_ESP32 && !PLATFORM_ESP8266
4545
temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
4646
temperature_sensor_install(&temp_sensor_config, &temp_handle);
47-
xTaskCreate(temp_func, "IntTemp", TEMP_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
47+
xTaskCreate(temp_func, "IntTemp", TEMP_STACK_SIZE, NULL, 16, NULL);
4848
#endif
4949

5050
#if PLATFORM_ESP8266

src/hal/realtek/hal_wifi_realtek.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ void HAL_PrintNetworkInfo()
109109
WiFI_GetMacAddress((char*)mac);
110110
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "+--------------- net device info ------------+\r\n");
111111
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif type : %-16s |\r\n", g_bOpenAccessPointMode == 0 ? "STA" : "AP");
112+
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif rssi = %-16i |\r\n", HAL_GetWifiStrength());
112113
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif ip = %-16s |\r\n", HAL_GetMyIPString());
113114
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif mask = %-16s |\r\n", HAL_GetMyMaskString());
114115
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif gateway = %-16s |\r\n", HAL_GetMyGatewayString());

src/hal/realtek/hal_wifi_realtek_new.c

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
#include <lwip_netconf.h>
1212
#include <dhcp/dhcps.h>
1313
#include "wifi_api.h"
14+
#include "wifi_fast_connect.h"
1415

1516
extern struct netif xnetif[NET_IF_NUM];
1617
extern void InitEasyFlashIfNeeded();
18+
extern int wifi_do_fast_connect(void);
1719

1820
typedef struct
1921
{
@@ -78,13 +80,14 @@ void HAL_PrintNetworkInfo()
7880
{
7981
uint8_t mac[6];
8082
WiFI_GetMacAddress((char*)mac);
81-
ADDLOG_INFO(LOG_FEATURE_GENERAL, "+--------------- net device info ------------+\r\n");
82-
ADDLOG_INFO(LOG_FEATURE_GENERAL, "|netif type : %-16s |\r\n", g_bOpenAccessPointMode == 0 ? "STA" : "AP");
83-
ADDLOG_INFO(LOG_FEATURE_GENERAL, "|netif ip = %-16s |\r\n", HAL_GetMyIPString());
84-
ADDLOG_INFO(LOG_FEATURE_GENERAL, "|netif mask = %-16s |\r\n", HAL_GetMyMaskString());
85-
ADDLOG_INFO(LOG_FEATURE_GENERAL, "|netif gateway = %-16s |\r\n", HAL_GetMyGatewayString());
86-
ADDLOG_INFO(LOG_FEATURE_GENERAL, "|netif mac : [%02X:%02X:%02X:%02X:%02X:%02X] %-7s |\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], "");
87-
ADDLOG_INFO(LOG_FEATURE_GENERAL, "+--------------------------------------------+\r\n");
83+
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "+--------------- net device info ------------+\r\n");
84+
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif type : %-16s |\r\n", g_bOpenAccessPointMode == 0 ? "STA" : "AP");
85+
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif rssi = %-16i |\r\n", HAL_GetWifiStrength());
86+
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif ip = %-16s |\r\n", HAL_GetMyIPString());
87+
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif mask = %-16s |\r\n", HAL_GetMyMaskString());
88+
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif gateway = %-16s |\r\n", HAL_GetMyGatewayString());
89+
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "|netif mac : [%02X:%02X:%02X:%02X:%02X:%02X] %-7s |\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], "");
90+
ADDLOG_DEBUG(LOG_FEATURE_GENERAL, "+--------------------------------------------+\r\n");
8891
}
8992

9093
int HAL_GetWifiStrength()
@@ -112,12 +115,6 @@ void obk_wifi_hdl_new(u8* buf, s32 buf_len, s32 flags, void* userdata)
112115
#if LWIP_NETIF_HOSTNAME
113116
netif_set_hostname(&xnetif[0], CFG_GetDeviceName());
114117
#endif
115-
if(!g_bStaticIP) LwIP_DHCP(0, DHCP_START);
116-
if(g_wifiStatusCallback != NULL)
117-
{
118-
g_wifiStatusCallback(WIFI_STA_CONNECTED);
119-
}
120-
if(CFG_HasFlag(OBK_FLAG_WIFI_ENHANCED_FAST_CONNECT)) p_store_fast_connect_info(0, 0);
121118
return;
122119
}
123120

@@ -164,13 +161,17 @@ void ConnectToWiFiTask(void* args)
164161
connect_param.ssid.len = strlen(wdata.ssid);
165162
connect_param.password = (unsigned char*)wdata.pwd;
166163
connect_param.password_len = strlen(wdata.pwd);
167-
wifi_reg_event_handler(RTW_EVENT_JOIN_STATUS, obk_wifi_hdl_new, NULL);
168164

169165
if(g_wifiStatusCallback != NULL)
170166
{
171167
g_wifiStatusCallback(WIFI_STA_CONNECTING);
172168
}
173169
wifi_connect(&connect_param, 1);
170+
if(!g_bStaticIP) LwIP_DHCP(0, DHCP_START);
171+
if(g_wifiStatusCallback != NULL)
172+
{
173+
g_wifiStatusCallback(WIFI_STA_CONNECTED);
174+
}
174175
vTaskDelete(NULL);
175176
}
176177

@@ -182,6 +183,7 @@ void ConfigureSTA(obkStaticIP_t* ip)
182183
struct ip_addr dnsserver;
183184

184185
wifi_set_autoreconnect(0);
186+
wifi_reg_event_handler(RTW_EVENT_JOIN_STATUS, obk_wifi_hdl_new, NULL);
185187

186188
if(ip->localIPAddr[0] == 0)
187189
{
@@ -252,9 +254,48 @@ int HAL_SetupWiFiOpenAccessPoint(const char* ssid)
252254
return 0;
253255
}
254256

255-
void HAL_FastConnectToWiFi(const char* oob_ssid, const char* connect_key, obkStaticIP_t* ip)
257+
void FastConnectToWiFiTask(void* args)
256258
{
259+
struct wlan_fast_reconnect* data = (struct wlan_fast_reconnect*)malloc(sizeof(struct wlan_fast_reconnect));
260+
struct wlan_fast_reconnect* empty_data = (struct wlan_fast_reconnect*)malloc(sizeof(struct wlan_fast_reconnect));
261+
memset(data, 0xff, sizeof(struct wlan_fast_reconnect));
262+
memset(empty_data, 0xff, sizeof(struct wlan_fast_reconnect));
263+
int ret = rt_kv_get("wlan_data", (uint8_t*)data, sizeof(struct wlan_fast_reconnect));
264+
if(ret < 0 || memcmp(empty_data, data, sizeof(struct wlan_fast_reconnect)) == 0)
265+
{
266+
ConnectToWiFiTask(args);
267+
goto exit;
268+
}
269+
if(g_wifiStatusCallback != NULL)
270+
{
271+
g_wifiStatusCallback(WIFI_STA_CONNECTING);
272+
}
257273
wifi_do_fast_connect();
274+
if(g_wifiStatusCallback != NULL)
275+
{
276+
g_wifiStatusCallback(WIFI_STA_CONNECTED);
277+
}
278+
exit:
279+
free(data);
280+
free(empty_data);
281+
vTaskDelete(NULL);
282+
}
283+
284+
void HAL_FastConnectToWiFi(const char* oob_ssid, const char* connect_key, obkStaticIP_t* ip)
285+
{
286+
g_bOpenAccessPointMode = 0;
287+
strcpy((char*)&wdata.ssid, oob_ssid);
288+
strncpy((char*)&wdata.pwd, connect_key, 64);
289+
290+
ConfigureSTA(ip);
291+
292+
xTaskCreate(
293+
(TaskFunction_t)FastConnectToWiFiTask,
294+
"WFC",
295+
4096 / sizeof(StackType_t),
296+
NULL,
297+
9,
298+
NULL);
258299
}
259300

260301
void HAL_DisableEnhancedFastConnect()

src/hal/realtek/rtl8721da/hal_main_rtl8721da.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,37 @@
55
#include "wifi_api_ext.h"
66
#include "flash_api.h"
77
#include "ameba_ota.h"
8+
#include "ameba_soc.h"
9+
#include "os_wrapper.h"
810

9-
//rtw_mode_t wifi_mode = RTW_MODE_STA;
1011
TaskHandle_t g_sys_task_handle1;
1112
uint32_t current_fw_idx = 0;
1213
uint8_t wmac[6] = { 0 };
14+
uint8_t flash_size_8720;
1315
unsigned char ap_ip[4] = { 192, 168, 4, 1 }, ap_netmask[4] = { 255, 255, 255, 0 }, ap_gw[4] = { 192, 168, 4, 1 };
1416
extern void wifi_fast_connect_enable(unsigned char enable);
1517
extern int (*p_wifi_do_fast_connect)(void);
1618
__attribute__((weak)) flash_t flash;
1719

20+
#if PLATFORM_RTL8720E
21+
22+
extern float g_wifi_temperature;
23+
void temp_func(void* pvParameters)
24+
{
25+
RCC_PeriphClockCmd(APBPeriph_ATIM, APBPeriph_ATIM_CLOCK, ENABLE);
26+
TM_Cmd(ENABLE);
27+
TM_InitTypeDef TM_InitStruct;
28+
TM_StructInit(&TM_InitStruct);
29+
TM_Init(&TM_InitStruct);
30+
for(;;)
31+
{
32+
g_wifi_temperature = TM_GetCdegree(TM_GetTempResult());
33+
vTaskDelay(10000 / portTICK_PERIOD_MS);
34+
}
35+
}
36+
37+
#endif
38+
1839
void sys_task1(void* pvParameters)
1940
{
2041
if(p_wifi_do_fast_connect) p_wifi_do_fast_connect = NULL;
@@ -30,16 +51,29 @@ void sys_task1(void* pvParameters)
3051

3152
void app_pre_example(void)
3253
{
33-
wifi_fast_connect_enable(0);
54+
u8 flash_ID[4];
55+
FLASH_RxCmd(flash_init_para.FLASH_cmd_rd_id, 3, flash_ID);
56+
flash_size_8720 = (1 << (flash_ID[2] - 0x11)) / 8;
3457
current_fw_idx = ota_get_cur_index(1);
35-
//if(p_wifi_do_fast_connect) p_wifi_do_fast_connect = NULL;
3658
xTaskCreate(
3759
sys_task1,
3860
"OpenBeken",
3961
8 * 256,
4062
NULL,
4163
3,
4264
&g_sys_task_handle1);
65+
66+
#if PLATFORM_RTL8720E
67+
68+
xTaskCreate(
69+
temp_func,
70+
"TempFunc",
71+
1024,
72+
NULL,
73+
13,
74+
NULL);
75+
76+
#endif
4377
}
4478

4579
#endif // PLATFORM_RTL8721DA

src/httpserver/rest_interface.c

Lines changed: 122 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,14 @@ extern uint8_t flash_size_8720;
103103
#undef DEFAULT_FLASH_LEN
104104
#define DEFAULT_FLASH_LEN (flash_size_8720 << 20)
105105

106-
#elif PLATFORM_RTL8721DA
106+
#elif PLATFORM_REALTEK_NEW
107107

108108
#include "ameba_ota.h"
109109
extern uint32_t current_fw_idx;
110110
extern uint32_t IMG_ADDR[OTA_IMGID_MAX][2];
111+
extern uint8_t flash_size_8720;
112+
#undef DEFAULT_FLASH_LEN
113+
#define DEFAULT_FLASH_LEN (flash_size_8720 << 20)
111114

112115
#endif
113116

@@ -3196,13 +3199,127 @@ ota_status_t ota_update_rest_get(uint8_t* buf, uint32_t buf_size, uint32_t* recv
31963199
ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed.");
31973200
return http_rest_error(request, ret, "error");
31983201
}
3199-
#elif PLATFORM_RTL8721DA
3202+
#elif PLATFORM_REALTEK_NEW
3203+
3204+
// Bad implementation. While it somewhat works, it is not recommended to use. HTTP OTA is preferable.
3205+
int ret = 0;
3206+
3207+
if(request->contentLength > 0)
3208+
{
3209+
towrite = request->contentLength;
3210+
}
3211+
else
3212+
{
3213+
ret = -1;
3214+
ADDLOG_ERROR(LOG_FEATURE_OTA, "Content-length is 0");
3215+
goto update_ota_exit;
3216+
}
3217+
3218+
flash_get_layout_info(IMG_BOOT, &IMG_ADDR[OTA_IMGID_BOOT][OTA_INDEX_1], NULL);
3219+
flash_get_layout_info(IMG_BOOT_OTA2, &IMG_ADDR[OTA_IMGID_BOOT][OTA_INDEX_2], NULL);
3220+
flash_get_layout_info(IMG_APP_OTA1, &IMG_ADDR[OTA_IMGID_APP][OTA_INDEX_1], NULL);
3221+
flash_get_layout_info(IMG_APP_OTA2, &IMG_ADDR[OTA_IMGID_APP][OTA_INDEX_2], NULL);
3222+
3223+
ota_context ctx;
3224+
memset(&ctx, 0, sizeof(ota_context));
3225+
ctx.otactrl = malloc(sizeof(update_ota_ctrl_info));
3226+
memset(ctx.otactrl, 0, sizeof(update_ota_ctrl_info));
3227+
3228+
ctx.otaTargetHdr = malloc(sizeof(update_ota_target_hdr));
3229+
memset(ctx.otaTargetHdr, 0, sizeof(update_ota_target_hdr));
3230+
writebuf = request->received;
3231+
if(!writelen) writelen = recv(request->fd, writebuf, request->receivedLenmax, 0);
3232+
// for some reason we receive data along with HTTP header. Skip it.
3233+
int skip = 0;
3234+
for(; skip < writelen - 5; skip++)
3235+
{
3236+
if(*(uint32_t*)&writebuf[skip] == 0xFFFFFFFF && writebuf[skip + 4] == 0x01) break;
3237+
}
3238+
writebuf += skip;
3239+
writelen -= skip;
3240+
towrite -= skip;
3241+
update_file_hdr* pOtaFileHdr = (update_file_hdr*)(writebuf);
3242+
ctx.otaTargetHdr->FileHdr.FwVer = pOtaFileHdr->FwVer;
3243+
ctx.otaTargetHdr->FileHdr.HdrNum = pOtaFileHdr->HdrNum;
3244+
3245+
uint32_t RevHdrLen = pOtaFileHdr->HdrNum * SUB_HEADER_LEN + HEADER_LEN;
3246+
if(writelen < RevHdrLen)
3247+
{
3248+
ADDLOG_ERROR(LOG_FEATURE_OTA, "failed to recv file header");
3249+
ret = -1;
3250+
goto update_ota_exit;
3251+
}
3252+
3253+
if(!get_ota_tartget_header(&ctx, writebuf, RevHdrLen))
3254+
{
3255+
ADDLOG_ERROR(LOG_FEATURE_OTA, "Get OTA header failed");
3256+
ret = -1;
3257+
goto update_ota_exit;
3258+
}
3259+
if(!ota_checkimage_layout(ctx.otaTargetHdr))
3260+
{
3261+
ADDLOG_ERROR(LOG_FEATURE_OTA, "ota_checkimage_layout failed");
3262+
ret = -1;
3263+
goto update_ota_exit;
3264+
}
3265+
ctx.otactrl->IsGetOTAHdr = 1;
3266+
writebuf += RevHdrLen;
3267+
// magic values to make it somewhat work.
3268+
writelen -= RevHdrLen + 5 - 384;
3269+
towrite -= RevHdrLen + 5 - 384;
3270+
ctx.otactrl->NextImgLen = towrite;
3271+
do
3272+
{
3273+
int size = download_packet_process(&ctx, writebuf, writelen);
3274+
download_percentage(&ctx, size, ctx.otactrl->ImageLen);
3275+
rtos_delay_milliseconds(10);
3276+
ADDLOG_DEBUG(LOG_FEATURE_OTA, "Writelen %i at %i", writelen, total);
3277+
total += writelen;
3278+
towrite -= writelen;
32003279

3201-
return http_rest_error(request, -20, "error");
3280+
if(towrite > 0)
3281+
{
3282+
writebuf = request->received;
3283+
writelen = recv(request->fd, writebuf, 2048 < towrite ? 2048 : towrite, 0);
3284+
if(writelen < 0)
3285+
{
3286+
ADDLOG_ERROR(LOG_FEATURE_OTA, "recv returned %d - end of data - remaining %d", writelen, towrite);
3287+
ret = -1;
3288+
goto update_ota_exit;
3289+
}
3290+
}
3291+
} while((towrite > 0) && (writelen >= 0));
32023292

3203-
#elif PLATFORM_RTL8720E
3293+
//erase manifest
3294+
flash_erase_sector(&flash, ctx.otactrl->FlashAddr);
32043295

3205-
return http_rest_error(request, -20, "error");
3296+
if(!verify_ota_checksum(ctx.otaTargetHdr, ctx.otactrl->targetIdx, ctx.otactrl->index))
3297+
{
3298+
ADDLOG_ERROR(LOG_FEATURE_OTA, "The checksum is wrong!");
3299+
ret = -1;
3300+
goto update_ota_exit;
3301+
}
3302+
3303+
if(!ota_update_manifest(ctx.otaTargetHdr, ctx.otactrl->targetIdx, ctx.otactrl->index))
3304+
{
3305+
ADDLOG_ERROR(LOG_FEATURE_OTA, "Change signature failed!");
3306+
ret = -1;
3307+
goto update_ota_exit;
3308+
}
3309+
3310+
update_ota_exit:
3311+
ota_update_deinit(&ctx);
3312+
if(ret != -1)
3313+
{
3314+
ADDLOG_INFO(LOG_FEATURE_OTA, "OTA is successful");
3315+
sys_clear_ota_signature();
3316+
//sys_recover_ota_signature();
3317+
}
3318+
else
3319+
{
3320+
ADDLOG_ERROR(LOG_FEATURE_OTA, "OTA failed.");
3321+
return http_rest_error(request, ret, "error");
3322+
}
32063323

32073324
#else
32083325

0 commit comments

Comments
 (0)