Skip to content

Commit 43aa4eb

Browse files
authored
Refactor self-test (#1615)
* Refactor self-test * Clean up logging on system init * Change order of init, screen first * Improve logging for self-test * Review comments
1 parent 5834081 commit 43aa4eb

File tree

11 files changed

+324
-469
lines changed

11 files changed

+324
-469
lines changed

main/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SRCS
77
"display.c"
88
"screen.c"
99
"input.c"
10+
"filesystem.c"
1011
"system.c"
1112
"work_queue.c"
1213
"lv_font_portfolio-6x8.c"

main/filesystem.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "filesystem.h"
2+
#include "global_state.h"
3+
#include "esp_log.h"
4+
#include "esp_spiffs.h"
5+
#include "esp_vfs.h"
6+
7+
static const char * TAG = "filesystem";
8+
9+
esp_err_t filesystem_init(void * pvParameters)
10+
{
11+
GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;
12+
13+
esp_vfs_spiffs_conf_t conf = {.base_path = "", .partition_label = NULL, .max_files = 5, .format_if_mount_failed = false};
14+
esp_err_t ret = esp_vfs_spiffs_register(&conf);
15+
16+
if (ret != ESP_OK) {
17+
if (ret == ESP_FAIL) {
18+
ESP_LOGE(TAG, "Failed to mount or format filesystem");
19+
} else if (ret == ESP_ERR_NOT_FOUND) {
20+
ESP_LOGE(TAG, "Failed to find SPIFFS partition");
21+
} else {
22+
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
23+
}
24+
return ESP_FAIL;
25+
}
26+
27+
GLOBAL_STATE->filesystem_is_available = true;
28+
29+
size_t total = 0, used = 0;
30+
ret = esp_spiffs_info(NULL, &total, &used);
31+
if (ret != ESP_OK) {
32+
// This shouldn't happen
33+
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
34+
} else {
35+
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
36+
}
37+
38+
return ESP_OK;
39+
}

main/filesystem.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef FILESYSTEM_H_
2+
#define FILESYSTEM_H_
3+
4+
#include "esp_err.h"
5+
6+
/**
7+
* @brief Initialize the SPIFFS filesystem
8+
*
9+
* This function mounts the SPIFFS partition and can be used
10+
* independently of the REST server for self-test mode.
11+
*
12+
* @return ESP_OK on success, ESP_FAIL on failure
13+
*/
14+
esp_err_t filesystem_init(void * pvParameters);
15+
16+
#endif /* FILESYSTEM_H_ */

main/global_state.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ typedef struct
146146

147147
bool ASIC_initalized;
148148
bool psram_is_available;
149+
bool filesystem_is_available;
149150

150151
int block_height;
151152
char scriptsig[128];

main/http_server/http_server.c

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -302,33 +302,6 @@ esp_err_t is_network_allowed(httpd_req_t * req)
302302
return ESP_FAIL;
303303
}
304304

305-
esp_err_t init_fs(void)
306-
{
307-
esp_vfs_spiffs_conf_t conf = {.base_path = "", .partition_label = NULL, .max_files = 5, .format_if_mount_failed = false};
308-
esp_err_t ret = esp_vfs_spiffs_register(&conf);
309-
310-
if (ret != ESP_OK) {
311-
if (ret == ESP_FAIL) {
312-
ESP_LOGE(TAG, "Failed to mount or format filesystem");
313-
} else if (ret == ESP_ERR_NOT_FOUND) {
314-
ESP_LOGE(TAG, "Failed to find SPIFFS partition");
315-
} else {
316-
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
317-
}
318-
return ESP_FAIL;
319-
}
320-
321-
size_t total = 0, used = 0;
322-
ret = esp_spiffs_info(NULL, &total, &used);
323-
if (ret != ESP_OK) {
324-
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
325-
} else {
326-
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
327-
}
328-
329-
return ESP_OK;
330-
}
331-
332305
/* Function for stopping the webserver */
333306
void stop_webserver(httpd_handle_t server)
334307
{
@@ -1346,13 +1319,6 @@ esp_err_t start_rest_server(void * pvParameters)
13461319
asic_api_init(GLOBAL_STATE);
13471320
const char * base_path = "";
13481321

1349-
bool enter_recovery = false;
1350-
if (init_fs() != ESP_OK) {
1351-
// Unable to initialize the web app filesystem.
1352-
// Enter recovery mode
1353-
enter_recovery = true;
1354-
}
1355-
13561322
REST_CHECK(base_path, "wrong base path", err);
13571323
rest_server_context_t * rest_context = calloc(1, sizeof(rest_server_context_t));
13581324
REST_CHECK(rest_context, "No memory for rest context", err);
@@ -1495,7 +1461,7 @@ esp_err_t start_rest_server(void * pvParameters)
14951461
};
14961462
httpd_register_uri_handler(server, &ws);
14971463

1498-
if (enter_recovery) {
1464+
if (!GLOBAL_STATE->filesystem_is_available) {
14991465
/* Make default route serve Recovery */
15001466
httpd_uri_t recovery_implicit_get_uri = {
15011467
.uri = "/*", .method = HTTP_GET,

main/main.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "connect.h"
2222
#include "asic_reset.h"
2323
#include "asic_init.h"
24+
#include "filesystem.h"
25+
#include "input.h"
2426

2527
static GlobalState GLOBAL_STATE;
2628

@@ -73,25 +75,30 @@ void app_main(void)
7375
return;
7476
}
7577

76-
if (self_test(&GLOBAL_STATE))
78+
if (self_test_init(&GLOBAL_STATE) != ESP_OK) {
79+
ESP_LOGE(TAG, "Failed to init self test");
7780
return;
81+
}
7882

7983
SYSTEM_init_system(&GLOBAL_STATE);
8084

81-
// init AP and connect to wifi
82-
wifi_init(&GLOBAL_STATE);
85+
if (!GLOBAL_STATE.SELF_TEST_MODULE.is_active) {
86+
wifi_init(&GLOBAL_STATE);
87+
}
8388

8489
SYSTEM_init_peripherals(&GLOBAL_STATE);
8590

8691
if (xTaskCreate(POWER_MANAGEMENT_task, "power management", 8192, (void *) &GLOBAL_STATE, 10, NULL) != pdPASS) {
8792
ESP_LOGE(TAG, "Error creating power management task");
8893
}
89-
if (xTaskCreate(FAN_CONTROLLER_task, "fan_controller", 8192, (void *) &GLOBAL_STATE, 5, NULL) != pdPASS) {
90-
ESP_LOGE(TAG, "Error creating fan controller task");
91-
}
9294

93-
// start the API for AxeOS
94-
start_rest_server((void *) &GLOBAL_STATE);
95+
if (!GLOBAL_STATE.SELF_TEST_MODULE.is_active) {
96+
if (xTaskCreate(FAN_CONTROLLER_task, "fan_controller", 8192, (void *) &GLOBAL_STATE, 5, NULL) != pdPASS) {
97+
ESP_LOGE(TAG, "Error creating fan controller task");
98+
}
99+
// start the API for AxeOS
100+
start_rest_server((void *) &GLOBAL_STATE);
101+
}
95102

96103
// After mounting SPIFFS
97104
SYSTEM_init_versions(&GLOBAL_STATE);
@@ -113,15 +120,23 @@ void app_main(void)
113120
return;
114121
}
115122

116-
if (xTaskCreate(stratum_task, "stratum admin", 8192, (void *) &GLOBAL_STATE, 5, NULL) != pdPASS) {
117-
ESP_LOGE(TAG, "Error creating stratum admin task");
118-
}
119123
if (xTaskCreate(create_jobs_task, "stratum miner", 8192, (void *) &GLOBAL_STATE, 20, NULL) != pdPASS) {
120124
ESP_LOGE(TAG, "Error creating stratum miner task");
121125
}
122126
if (xTaskCreate(ASIC_result_task, "asic result", 8192, (void *) &GLOBAL_STATE, 15, NULL) != pdPASS) {
123127
ESP_LOGE(TAG, "Error creating asic result task");
124128
}
129+
130+
if (!GLOBAL_STATE.SELF_TEST_MODULE.is_active) {
131+
if (xTaskCreate(stratum_task, "stratum admin", 8192, (void *) &GLOBAL_STATE, 5, NULL) != pdPASS) {
132+
ESP_LOGE(TAG, "Error creating stratum admin task");
133+
}
134+
} else {
135+
if (xTaskCreate(self_test_task, "self_test", 8192, (void *) &GLOBAL_STATE, 10, NULL) != pdPASS) {
136+
ESP_LOGE(TAG, "Error creating self test task");
137+
}
138+
}
139+
125140
if (xTaskCreateWithCaps(hashrate_monitor_task, "hashrate monitor", 8192, (void *) &GLOBAL_STATE, 5, NULL, MALLOC_CAP_SPIRAM) !=
126141
pdPASS) {
127142
ESP_LOGE(TAG, "Error creating hashrate monitor task");

0 commit comments

Comments
 (0)