GCC Code Coverage Report


Directory: main/
File: adapter/memory_card.c
Date: 2025-10-04 14:03:00
Exec Total Coverage
Lines: 4 97 4.1%
Functions: 1 13 7.7%
Branches: 3 36 8.3%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2021-2025, Jacques Gagnon
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/stat.h>
9 #include <esp_heap_caps.h>
10 #include <esp_timer.h>
11 #include <soc/soc_memory_layout.h>
12 #include "xtensa/core-macros.h"
13 #include "sdkconfig.h"
14 #include "system/fs.h"
15 #include "config.h"
16 #include "memory_card.h"
17
18 /* Related to bare metal hack, something goes writing there. */
19 /* Workaround: Remove region from heap pool until I figure it out */
20 SOC_RESERVE_MEMORY_REGION(0x3FFE7D98, 0x3FFE7E28, bad_region);
21
22 static uint8_t *mc_buffer[MC_BUFFER_BLOCK_CNT] = {0};
23 static esp_timer_handle_t mc_timer_hdl = NULL;
24 static int32_t mc_block_state = 0;
25 static bool mc_ready = false;
26
27 static int32_t mc_restore(void);
28 static int32_t mc_store(void);
29 static inline void mc_store_cb(void *arg);
30
31 static void mc_start_update_timer(uint64_t timeout_us) {
32 if (mc_timer_hdl) {
33 if (esp_timer_is_active(mc_timer_hdl)) {
34 esp_timer_stop(mc_timer_hdl);
35 }
36 esp_timer_start_once(mc_timer_hdl, timeout_us);
37 }
38 }
39
40 static int32_t mc_restore(void) {
41 struct stat st;
42 int32_t ret = -1;
43
44 if (stat(MEMORY_CARD_FILE, &st) != 0) {
45 printf("# %s: No Memory Card on FS. Creating...\n", __FUNCTION__);
46 ret = mc_store();
47 }
48 else {
49 FILE *file = fopen(MEMORY_CARD_FILE, "rb");
50 if (file == NULL) {
51 printf("# %s: failed to open file for reading\n", __FUNCTION__);
52 }
53 else {
54 uint32_t count = 0;
55 for (uint32_t i = 0; i < MC_BUFFER_BLOCK_CNT; i++) {
56 count += fread((void *)mc_buffer[i], MC_BUFFER_BLOCK_SIZE, 1, file);
57 }
58 fclose(file);
59
60 if (count == MC_BUFFER_BLOCK_CNT) {
61 ret = 0;
62 printf("# %s: restore sucessful!\n", __FUNCTION__);
63 }
64 else {
65 printf("# %s: restore failed! cnt: %ld File size:%ld\n", __FUNCTION__, count, st.st_size);
66 }
67 }
68 }
69 return ret;
70 }
71
72 static int32_t mc_store(void) {
73 int32_t ret = -1;
74
75 FILE *file = fopen(MEMORY_CARD_FILE, "wb");
76 if (file == NULL) {
77 printf("# %s: failed to open file for writing\n", __FUNCTION__);
78 }
79 else {
80 uint32_t count = 0;
81 for (uint32_t i = 0; i < MC_BUFFER_BLOCK_CNT; i++) {
82 count += fwrite((void *)mc_buffer[i], MC_BUFFER_BLOCK_SIZE, 1, file);
83 }
84 fclose(file);
85
86 if (count == MC_BUFFER_BLOCK_CNT) {
87 ret = 0;
88 mc_block_state = 0;
89 }
90 printf("# %s: file updated cnt: %ld\n", __FUNCTION__, count);
91 }
92 return ret;
93 }
94
95 static int32_t mc_store_spread(void) {
96 int32_t ret = -1;
97
98 FILE *file = fopen(MEMORY_CARD_FILE, "r+b");
99 if (file == NULL) {
100 printf("# %s: failed to open file for writing\n", __FUNCTION__);
101 }
102 else {
103 uint32_t block = __builtin_ffs(mc_block_state);
104
105 if (block) {
106 uint32_t count = 0;
107 block -= 1;
108
109 fseek(file, block * MC_BUFFER_BLOCK_SIZE, SEEK_SET);
110 count = fwrite((void *)mc_buffer[block], MC_BUFFER_BLOCK_SIZE, 1, file);
111 fclose(file);
112
113 if (count == 1) {
114 ret = 0;
115 atomic_clear_bit(&mc_block_state, block);
116 }
117
118 printf("# %s: block %ld updated cnt: %ld\n", __FUNCTION__, block, count);
119
120 if (mc_block_state) {
121 mc_start_update_timer(20000);
122 }
123 }
124 }
125 return ret;
126 }
127
128 static inline void mc_store_cb(void *arg) {
129 (void)mc_store_spread();
130 }
131
132 1 int32_t mc_init_mem(void) {
133
2/2
✓ Branch 0 (9→3) taken 32 times.
✓ Branch 1 (9→10) taken 1 times.
33 for (uint32_t i = 0; i < MC_BUFFER_BLOCK_CNT; i++) {
134 32 mc_buffer[i] = heap_caps_malloc(MC_BUFFER_BLOCK_SIZE, MALLOC_CAP_DMA);
135
136
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→8) taken 32 times.
32 if (mc_buffer[i] == NULL) {
137 printf("# %s mc_buffer[%ld] alloc fail\n", __FUNCTION__, i);
138 heap_caps_dump_all();
139 return -1;
140 }
141 }
142
143 return 0;
144 }
145
146 int32_t mc_init(void) {
147 int32_t ret = -1;
148
149 if (config.global_cfg.banksel < CONFIG_BANKSEL_MAX) {
150 const esp_timer_create_args_t mc_timer_args = {
151 .callback = &mc_store_cb,
152 .arg = (void *)NULL,
153 .name = "mc_timer"
154 };
155
156 esp_timer_create(&mc_timer_args, &mc_timer_hdl);
157
158 ret = mc_restore();
159 }
160
161 if (ret == 0) {
162 mc_ready = true;
163 }
164
165 return ret;
166 }
167
168 void mc_storage_update(void) {
169 mc_start_update_timer(1000000);
170 }
171
172 /* Assume r/w size will never cross blocks boundary */
173 void IRAM_ATTR mc_read(uint32_t addr, uint8_t *data, uint32_t size) {
174 memcpy(data, mc_buffer[addr >> 12] + (addr & 0xFFF), size);
175 }
176
177 void IRAM_ATTR mc_write(uint32_t addr, uint8_t *data, uint32_t size) {
178 struct raw_fb fb_data = {0};
179 uint32_t block = addr >> 12;
180
181 memcpy(mc_buffer[block] + (addr & 0xFFF), data, size);
182
183 if (config.global_cfg.banksel < CONFIG_BANKSEL_MAX) {
184 atomic_set_bit(&mc_block_state, block);
185
186 fb_data.header.wired_id = 0;
187 fb_data.header.type = FB_TYPE_MEM_WRITE;
188 fb_data.header.data_len = 0;
189 adapter_q_fb(&fb_data);
190 }
191 }
192
193 uint8_t IRAM_ATTR *mc_get_ptr(uint32_t addr) {
194 return mc_buffer[addr >> 12] + (addr & 0xFFF);
195 }
196
197 uint32_t IRAM_ATTR mc_get_state(void) {
198 return mc_block_state;
199 }
200
201 bool IRAM_ATTR mc_get_ready(void) {
202 return mc_ready;
203 }
204