Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to memory management in fpga_download #159

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions main/fpga_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ static bool fpga_uart_download(ICE40* ice40) {

case 'D':
{ // Data block
fpga_req_add_file_data(header.fid, buffer, header.len);
int ret = fpga_req_add_file_data(header.fid, buffer, header.len, true);
buffer = NULL; // Buffer has been captured in req list
if (ret) {
fpga_display_message(0xa85a32, 0xFFFFFFFF, "FPGA download mode\nUpload failed, out of memory");
return false;
}
break;
}

Expand Down Expand Up @@ -277,7 +282,7 @@ bool fpga_host(xQueueHandle buttonQueue, ICE40* ice40, bool enable_uart, const c
}
}

void fpga_download(xQueueHandle buttonQueue, ICE40* ice40) {
bool fpga_download(xQueueHandle buttonQueue, ICE40* ice40) {
fpga_display_message(0x325aa8, 0xFFFFFFFF, "FPGA download mode\nPreparing...");

ILI9341* ili9341 = get_ili9341();
Expand Down Expand Up @@ -315,12 +320,12 @@ void fpga_download(xQueueHandle buttonQueue, ICE40* ice40) {
ili9341_init(ili9341);
}

return;
return true;

error:
vTaskDelay(1000 / portTICK_PERIOD_MS);
fpga_req_cleanup();
fpga_irq_cleanup(ice40);
fpga_uninstall_uart();
return;
return false;
}
26 changes: 19 additions & 7 deletions main/fpga_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ struct req_entry {
void *data;
size_t len;
size_t ofs;
bool external_data;
};

struct req_entry *g_req_entries;
Expand All @@ -361,6 +362,7 @@ static void _fpga_req_delete_entry(uint32_t fid) {

// Release
if (re->fh) fclose(re->fh);
if (re->external_data) free(re->data);
free(re);

// Done
Expand Down Expand Up @@ -468,6 +470,7 @@ void fpga_req_cleanup(void) {
while (re_cur) {
re_nxt = re_cur->next;
if (re_cur->fh) fclose(re_cur->fh);
if (re_cur->external_data) free(re_cur->data);
free(re_cur);
re_cur = re_nxt;
}
Expand All @@ -488,15 +491,19 @@ int fpga_req_add_file_alias(uint32_t fid, const char *path) {
return 0;
}

int fpga_req_add_file_data(uint32_t fid, void *data, size_t len) {
int fpga_req_add_file_data(uint32_t fid, void *data, size_t len,bool capture_buffer) {
struct req_entry *re;
void *buf;

// Remove any previous entries
_fpga_req_delete_entry(fid);

// Alloc new entry
buf = malloc(sizeof(struct req_entry) + len);
if (capture_buffer) {
buf = malloc(sizeof(struct req_entry));
} else {
buf = malloc(sizeof(struct req_entry) + len);
}
if (!buf) return -ENOMEM;

re = buf;
Expand All @@ -508,12 +515,17 @@ int fpga_req_add_file_data(uint32_t fid, void *data, size_t len) {

// Init fields
re->fid = fid;
re->data = buf + sizeof(struct req_entry);
re->len = len;

// Copy actual data
memcpy(re->data, data, len);

re->external_data = capture_buffer;
if (capture_buffer) {
// Set data pointer to external buffer
re->data = data;
} else {
// Set data pointer to after record
re->data = buf + sizeof(struct req_entry);
// Copy actual data
memcpy(re->data, data, len);
}
// Done
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion main/include/fpga_download.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

#include "ice40.h"

void fpga_download(xQueueHandle button_queue, ICE40* ice40);
bool fpga_download(xQueueHandle button_queue, ICE40* ice40);
bool fpga_host(xQueueHandle button_queue, ICE40* ice40, bool enable_uart, const char* prefix);
2 changes: 1 addition & 1 deletion main/include/fpga_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ bool fpga_btn_forward_events(ICE40 *ice40, xQueueHandle buttonQueue, esp_err_t *
void fpga_req_setup(void);
void fpga_req_cleanup(void);
int fpga_req_add_file_alias(uint32_t fid, const char *path);
int fpga_req_add_file_data(uint32_t fid, void *data, size_t len);
int fpga_req_add_file_data(uint32_t fid, void *data, size_t len, bool capture_buffer);
void fpga_req_del_file(uint32_t fid);

bool fpga_req_process(const char *prefix, ICE40 *ice40, TickType_t wait, esp_err_t *err);
3 changes: 2 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ void app_main(void) {
} else if (webusb_mode == 0x02) {
display_boot_screen("FPGA download mode");
while (true) {
fpga_download(rp2040->queue, ice40);
bool ok = fpga_download(rp2040->queue, ice40);
if (!ok) break;
}
} else if (webusb_mode == 0x03) {
webusb_new_main(rp2040->queue);
Expand Down