2023-09-11 14:55:26 +08:00
|
|
|
#include "programmer.h"
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
|
#include "freertos/message_buffer.h"
|
|
|
|
|
#include "algo_extractor.h"
|
|
|
|
|
#include "esp_log.h"
|
2023-09-18 13:39:46 +08:00
|
|
|
#include "prog_idle.h"
|
|
|
|
|
#include "prog_online.h"
|
|
|
|
|
#include "prog_offline.h"
|
2023-09-17 17:53:11 +08:00
|
|
|
#include <sys/stat.h>
|
2023-09-11 14:55:26 +08:00
|
|
|
#include <cstring>
|
|
|
|
|
|
2025-02-19 10:43:38 +08:00
|
|
|
#include "swd_host.h"
|
|
|
|
|
|
2023-09-19 00:08:30 +08:00
|
|
|
#define TAG "programmer"
|
|
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
static ProgData s_data;
|
2023-09-19 00:08:30 +08:00
|
|
|
static Prog *s_prog = nullptr;
|
|
|
|
|
static Prog *s_last_prog = nullptr;
|
2023-09-11 14:55:26 +08:00
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
prog_err_def programmer_request_handle(char *buf, int len)
|
2023-09-11 14:55:26 +08:00
|
|
|
{
|
2023-09-18 13:39:46 +08:00
|
|
|
prog_request_swap_t swap = {buf, len};
|
2023-09-11 14:55:26 +08:00
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
if (s_data.is_busy())
|
2023-09-11 14:55:26 +08:00
|
|
|
{
|
2023-09-18 13:39:46 +08:00
|
|
|
return PROG_ERR_BUSY;
|
2023-09-11 14:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
s_data.set_swap(&swap);
|
|
|
|
|
s_data.send_event(PROG_EVT_REQUEST);
|
|
|
|
|
s_data.wait_sync();
|
|
|
|
|
|
|
|
|
|
return static_cast<prog_err_def>(reinterpret_cast<int>(s_data.get_swap()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void programmer_switch_mode(prog_mode_def mode)
|
|
|
|
|
{
|
|
|
|
|
static ProgIdle prog_idle;
|
|
|
|
|
static ProgOnline prog_online;
|
|
|
|
|
static ProgOffline prog_offline;
|
|
|
|
|
|
2023-09-19 00:08:30 +08:00
|
|
|
s_last_prog = s_prog;
|
|
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
switch (mode)
|
|
|
|
|
{
|
|
|
|
|
case PROG_ONLINE_MODE:
|
|
|
|
|
s_prog = &prog_online;
|
|
|
|
|
break;
|
|
|
|
|
case PROG_OFFLINE_MODE:
|
|
|
|
|
s_prog = &prog_offline;
|
|
|
|
|
break;
|
|
|
|
|
case PROG_IDLE_MODE:
|
|
|
|
|
s_prog = &prog_idle;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-09-19 00:08:30 +08:00
|
|
|
|
|
|
|
|
if (s_last_prog && s_prog)
|
|
|
|
|
{
|
|
|
|
|
ESP_LOGI(TAG, "%s--> %s", s_last_prog->name(), s_prog->name());
|
|
|
|
|
}
|
2023-09-11 14:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void programmer_task(void *pvParameters)
|
|
|
|
|
{
|
2023-09-18 13:39:46 +08:00
|
|
|
prog_evt_def evt = PROG_EVT_NONE;
|
|
|
|
|
ProgData &obj = *(reinterpret_cast<ProgData *>(pvParameters));
|
2023-09-16 16:50:44 +08:00
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
Prog::register_switch_mode_function(programmer_switch_mode);
|
|
|
|
|
Prog::switch_mode(PROG_IDLE_MODE);
|
2023-09-11 14:55:26 +08:00
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
2023-09-18 13:39:46 +08:00
|
|
|
evt = obj.wait_event();
|
2023-09-11 14:55:26 +08:00
|
|
|
|
2024-12-23 15:48:48 +08:00
|
|
|
ESP_LOGI(TAG, "got event:%d", evt);
|
|
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
switch (evt)
|
2023-09-11 14:55:26 +08:00
|
|
|
{
|
2023-09-18 13:39:46 +08:00
|
|
|
case PROG_EVT_REQUEST:
|
2025-02-19 10:43:38 +08:00
|
|
|
swd_set_target_state_hw(RESET_HOLD);
|
2023-09-18 13:39:46 +08:00
|
|
|
s_prog->request_handle(obj);
|
|
|
|
|
break;
|
|
|
|
|
case PROG_EVT_PROGRAM_START:
|
|
|
|
|
s_prog->program_start_handle(obj);
|
|
|
|
|
break;
|
|
|
|
|
case PROG_EVT_PROGRAM_TIMEOUT:
|
|
|
|
|
s_prog->program_timeout_handle(obj);
|
|
|
|
|
break;
|
|
|
|
|
case PROG_EVT_PROGRAM_DATA_RECVED:
|
|
|
|
|
s_prog->program_data_handle(obj);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2023-09-11 14:55:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-16 16:50:44 +08:00
|
|
|
void programmer_init(void)
|
2023-09-11 14:55:26 +08:00
|
|
|
{
|
2023-09-17 17:53:11 +08:00
|
|
|
if (FileProgrammer::is_exist(CONFIG_PROGRAMMER_ALGORITHM_ROOT) != true)
|
|
|
|
|
mkdir(CONFIG_PROGRAMMER_ALGORITHM_ROOT, 0777);
|
|
|
|
|
|
|
|
|
|
if (FileProgrammer::is_exist(CONFIG_PROGRAMMER_PROGRAM_ROOT) != true)
|
|
|
|
|
mkdir(CONFIG_PROGRAMMER_PROGRAM_ROOT, 0777);
|
|
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
s_data.init();
|
2025-02-19 10:43:38 +08:00
|
|
|
xTaskCreate(programmer_task, "programmer", 1024 * 12, &s_data, 2, NULL);
|
2023-09-11 14:55:26 +08:00
|
|
|
}
|
|
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
void programmer_get_status(char *buf, int size, int &encode_len)
|
2023-09-11 14:55:26 +08:00
|
|
|
{
|
2023-09-18 13:39:46 +08:00
|
|
|
encode_len = snprintf(buf, size, "{\"progress\": %d, \"status\": \"%s\"}", s_data.get_progress(), s_data.is_busy() ? ("busy") : ("idle"));
|
2023-09-11 14:55:26 +08:00
|
|
|
}
|
2023-09-16 16:50:44 +08:00
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
prog_err_def programmer_write_data(uint8_t *data, int len)
|
2023-09-16 16:50:44 +08:00
|
|
|
{
|
2023-09-18 13:39:46 +08:00
|
|
|
prog_data_swap_t swap = {data, len};
|
2023-09-16 16:50:44 +08:00
|
|
|
|
2023-09-18 13:39:46 +08:00
|
|
|
s_data.set_swap(&swap);
|
|
|
|
|
s_data.send_event(PROG_EVT_PROGRAM_DATA_RECVED);
|
|
|
|
|
s_data.wait_sync();
|
|
|
|
|
|
|
|
|
|
return static_cast<prog_err_def>(reinterpret_cast<int>(s_data.get_swap()));
|
|
|
|
|
}
|