Skip to content

Commit

Permalink
Enable SPIRAM and use it for specific tasks (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
eandersson authored Dec 28, 2024
1 parent f7fc3d3 commit d9ee113
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
18 changes: 15 additions & 3 deletions main/http_server/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static const char * TAG = "http_server";
static GlobalState * GLOBAL_STATE;
static httpd_handle_t server = NULL;
QueueHandle_t log_queue = NULL;
StaticQueue_t log_queue_static;

static int fd = -1;

Expand Down Expand Up @@ -381,6 +382,11 @@ static esp_err_t GET_system_info(httpd_req_t * req)
cJSON_AddNumberToObject(root, "isUsingFallbackStratum", GLOBAL_STATE->SYSTEM_MODULE.is_using_fallback);

cJSON_AddNumberToObject(root, "freeHeap", esp_get_free_heap_size());
cJSON_AddNumberToObject(root, "freeInternalHeap", esp_get_free_internal_heap_size());
cJSON_AddNumberToObject(root, "minimumFreeHeap", esp_get_minimum_free_heap_size());

cJSON_AddStringToObject(root, "idfVersion", esp_get_idf_version());

cJSON_AddNumberToObject(root, "coreVoltage", nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE));
cJSON_AddNumberToObject(root, "coreVoltageActual", VCORE_get_voltage_mv(GLOBAL_STATE));
cJSON_AddNumberToObject(root, "frequency", nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY));
Expand Down Expand Up @@ -567,7 +573,7 @@ int log_to_queue(const char * format, va_list args)
va_end(args_copy);

// Allocate the buffer dynamically
char * log_buffer = (char *) calloc(needed_size + 2, sizeof(char)); // +2 for potential \n and \0
char *log_buffer = (char *) heap_caps_calloc(needed_size + 2, sizeof(char), MALLOC_CAP_SPIRAM);
if (log_buffer == NULL) {
return 0;
}
Expand Down Expand Up @@ -683,11 +689,17 @@ esp_err_t start_rest_server(void * pvParameters)
}

REST_CHECK(base_path, "wrong base path", err);
rest_server_context_t * rest_context = calloc(1, sizeof(rest_server_context_t));

rest_server_context_t *rest_context = (rest_server_context_t*) heap_caps_calloc(1, sizeof(rest_server_context_t), MALLOC_CAP_SPIRAM);

REST_CHECK(rest_context, "No memory for rest context", err);
strlcpy(rest_context->base_path, base_path, sizeof(rest_context->base_path));

log_queue = xQueueCreate(MESSAGE_QUEUE_SIZE, sizeof(char*));
size_t queue_memory_size = MESSAGE_QUEUE_SIZE * sizeof(char*);
void *queue_memory = heap_caps_malloc(queue_memory_size, MALLOC_CAP_SPIRAM);
REST_CHECK(queue_memory, "No memory for queue memory", err);
log_queue = xQueueCreateStatic(MESSAGE_QUEUE_SIZE, sizeof(char*), queue_memory, &log_queue_static);
REST_CHECK(log_queue, "No memory for log queue", err);

httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.uri_match_fn = httpd_uri_match_wildcard;
Expand Down
4 changes: 2 additions & 2 deletions main/self_test/self_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ void self_test(void * pvParameters)
tests_done(GLOBAL_STATE, TESTS_FAILED);
}

GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs = malloc(sizeof(bm_job *) * 128);
GLOBAL_STATE->valid_jobs = malloc(sizeof(uint8_t) * 128);
GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs = heap_caps_malloc(sizeof(bm_job *) * 128, MALLOC_CAP_SPIRAM);
GLOBAL_STATE->valid_jobs = heap_caps_malloc(sizeof(uint8_t) * 128, MALLOC_CAP_SPIRAM);

for (int i = 0; i < 128; i++) {
GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[i] = NULL;
Expand Down
4 changes: 2 additions & 2 deletions main/tasks/asic_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ void ASIC_task(void *pvParameters)
//initialize the semaphore
GLOBAL_STATE->ASIC_TASK_MODULE.semaphore = xSemaphoreCreateBinary();

GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs = malloc(sizeof(bm_job *) * 128);
GLOBAL_STATE->valid_jobs = malloc(sizeof(uint8_t) * 128);
GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs = heap_caps_malloc(sizeof(bm_job *) * 128, MALLOC_CAP_SPIRAM);
GLOBAL_STATE->valid_jobs = heap_caps_malloc(sizeof(uint8_t) * 128, MALLOC_CAP_SPIRAM);
for (int i = 0; i < 128; i++)
{
GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[i] = NULL;
Expand Down
4 changes: 2 additions & 2 deletions main/tasks/create_jobs_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static void process_mining_job(GlobalState *GLOBAL_STATE, mining_notify *notific

bm_job next_job = construct_bm_job(notification, merkle_root, GLOBAL_STATE->version_mask);

bm_job *queued_next_job = malloc(sizeof(bm_job));
bm_job *queued_next_job = heap_caps_malloc(sizeof(bm_job), MALLOC_CAP_SPIRAM);
if (queued_next_job == NULL) {
ESP_LOGE(TAG, "Failed to allocate memory for queued_next_job");
free(extranonce_2_str);
Expand Down Expand Up @@ -152,7 +152,7 @@ static void generate_additional_work(GlobalState *GLOBAL_STATE, mining_notify *n

bm_job next_job = construct_bm_job(notification, merkle_root, GLOBAL_STATE->version_mask);

bm_job *queued_next_job = malloc(sizeof(bm_job));
bm_job *queued_next_job = heap_caps_malloc(sizeof(bm_job), MALLOC_CAP_SPIRAM);
if (queued_next_job == NULL) {
ESP_LOGE(TAG, "Failed to allocate memory for queued_next_job");
free(extranonce_2_str);
Expand Down
4 changes: 4 additions & 0 deletions sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ CONFIG_ESP_WIFI_11KV_SUPPORT=y
CONFIG_LV_CONF_SKIP=n
CONFIG_BT_ENABLED=n
CONFIG_LWIP_MAX_SOCKETS=16
CONFIG_SPIRAM=y
CONFIG_SPIRAM_MODE_OCT=y
CONFIG_SPIRAM_USE_CAPS_ALLOC=y
CONFIG_SPIRAM_SPEED_80M=y

0 comments on commit d9ee113

Please sign in to comment.