Select one of the symbols to view example projects that use it.
 
Outline
#include <stdarg.h>
#include <sys/param.h>
#include "esp_attr.h"
#include "esp_private/system_internal.h"
#include "esp_flash.h"
#include "esp_flash_partitions.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hal/spi_types.h"
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_compiler.h"
#include "esp_rom_sys.h"
#include "esp_private/spi_flash_os.h"
#include "esp_private/cache_utils.h"
#include "esp_private/spi_share_hw_ctrl.h"
#define SPI_FLASH_CACHE_NO_DISABLE
TAG
app_func_arg_t
cache_enable(void *)
cache_disable(void *)
acquire_spi_bus_lock(void *)
release_spi_bus_lock(void *)
spi23_start(void *)
spi23_end(void *)
spi1_start(void *)
spi1_end(void *)
spi_flash_os_check_yield(void *, uint32_t, uint32_t *)
spi_flash_os_yield(void *, uint32_t *)
delay_us(void *, uint32_t)
get_buffer_malloc(void *, size_t, size_t *)
release_buffer_malloc(void *, void *)
main_flash_region_protected(void *, size_t, size_t)
main_flash_op_status(uint32_t)
main_flash_arg
esp_flash_spi1_default_os_functions
esp_flash_spi23_default_os_functions
use_bus_lock(int)
esp_flash_init_os_functions(esp_flash_t *, int, spi_bus_lock_dev_handle_t)
esp_flash_deinit_os_functions(esp_flash_t *, spi_bus_lock_dev_handle_t *)
esp_flash_init_main_bus_lock()
esp_flash_app_enable_os_functions(esp_flash_t *)
esp_flash_set_dangerous_write_protection(esp_flash_t *, const bool)
on_spi_check_yield(app_func_arg_t *)
on_spi_released(app_func_arg_t *)
on_spi_acquired(app_func_arg_t *)
on_spi_yielded(app_func_arg_t *)
Files
loading...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/spi_flash/spi_flash_os_func_app.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <stdarg.h> #include <sys/param.h> //For max/min #include "esp_attr.h" #include "esp_private/system_internal.h" #include "esp_flash.h" #include "esp_flash_partitions.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "hal/spi_types.h" #include "sdkconfig.h" #include "esp_log.h" #include "esp_compiler.h" #include "esp_rom_sys.h" #include "esp_private/spi_flash_os.h" #include "esp_private/cache_utils.h" #include "esp_private/spi_share_hw_ctrl.h"16 includes #define SPI_FLASH_CACHE_NO_DISABLE (CONFIG_SPI_FLASH_AUTO_SUSPEND || (CONFIG_SPIRAM_FETCH_INSTRUCTIONS && CONFIG_SPIRAM_RODATA) || CONFIG_APP_BUILD_TYPE_RAM) static const char TAG[] = "spi_flash"; #if SPI_FLASH_CACHE_NO_DISABLE static _lock_t s_spi1_flash_mutex; #endif // #if SPI_FLASH_CACHE_NO_DISABLE /* * OS functions providing delay service and arbitration among chips, and with the cache. * * The cache needs to be disabled when chips on the SPI1 bus is under operation, hence these functions need to be put * into the IRAM,and their data should be put into the DRAM. *//* ... */ /* * Time yield algorithm: * Every time spi_flash_os_check_yield() is called: * * 1. If the time since last end() function is longer than CONFIG_SPI_FLASH_ERASE_YIELD_TICKS (time * to yield), all counters will be reset, as if the yield has just ends; * 2. If the time since last yield() is longer than CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS, will * return a yield request. When the yield() is called, all counters will be reset. * Note: Short intervals between start() and end() after the last yield() will not reset the * counter mentioned in #2, but still be counted into the time mentioned in #2. *//* ... */ typedef struct { spi_bus_lock_dev_handle_t dev_lock; bool no_protect; //to decide whether to check protected region (for the main chip) or not. uint32_t acquired_since_us; // Time since last explicit yield() uint32_t released_since_us; // Time since last end() (implicit yield) }{ ... } app_func_arg_t; static inline void on_spi_released(app_func_arg_t* ctx); static inline void on_spi_acquired(app_func_arg_t* ctx); static inline void on_spi_yielded(app_func_arg_t* ctx); static inline bool on_spi_check_yield(app_func_arg_t* ctx); #if !SPI_FLASH_CACHE_NO_DISABLE IRAM_ATTR static void cache_enable(void* arg) { spi_flash_enable_interrupts_caches_and_other_cpu(); }{ ... } IRAM_ATTR static void cache_disable(void* arg) { spi_flash_disable_interrupts_caches_and_other_cpu(); }{ ... } #endif/* ... */ //#if !SPI_FLASH_CACHE_NO_DISABLE static IRAM_ATTR esp_err_t acquire_spi_bus_lock(void *arg) { spi_bus_lock_dev_handle_t dev_lock = ((app_func_arg_t *)arg)->dev_lock; // wait for other devices (or cache) to finish their operation esp_err_t ret = spi_bus_lock_acquire_start(dev_lock, portMAX_DELAY); if (ret != ESP_OK) { return ret; }{...} spi_bus_lock_touch(dev_lock); return ESP_OK; }{ ... } static IRAM_ATTR esp_err_t release_spi_bus_lock(void *arg) { return spi_bus_lock_acquire_end(((app_func_arg_t *)arg)->dev_lock); }{ ... } static IRAM_ATTR esp_err_t spi23_start(void *arg){ esp_err_t ret = acquire_spi_bus_lock(arg); on_spi_acquired((app_func_arg_t*)arg); return ret; }{ ... } static IRAM_ATTR esp_err_t spi23_end(void *arg){ esp_err_t ret = release_spi_bus_lock(arg); on_spi_released((app_func_arg_t*)arg); return ret; }{ ... } static IRAM_ATTR esp_err_t spi1_start(void *arg) { esp_err_t ret = ESP_OK; /** * There are three ways for ESP Flash API lock: * 1. spi bus lock, this is used when SPI1 is shared with GPSPI Master Driver * 2. mutex, this is used when the Cache isn't need to be disabled. * 3. cache lock (from cache_utils.h), this is used when we need to disable Cache to avoid access from SPI0 * * From 1 to 3, the lock efficiency decreases. *//* ... */ #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS //use the lock to disable the cache and interrupts before using the SPI bus ret = acquire_spi_bus_lock(arg);/* ... */ #elif SPI_FLASH_CACHE_NO_DISABLE _lock_acquire(&s_spi1_flash_mutex); #else //directly disable the cache and interrupts when lock is not used cache_disable(NULL);/* ... */ #endif on_spi_acquired((app_func_arg_t*)arg); return ret; }{ ... } static IRAM_ATTR esp_err_t spi1_end(void *arg) { esp_err_t ret = ESP_OK; /** * There are three ways for ESP Flash API lock, see `spi1_start` *//* ... */ #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS ret = release_spi_bus_lock(arg); #elif SPI_FLASH_CACHE_NO_DISABLE _lock_release(&s_spi1_flash_mutex); #else cache_enable(NULL); #endif on_spi_released((app_func_arg_t*)arg); return ret; }{ ... } static IRAM_ATTR esp_err_t spi_flash_os_check_yield(void *arg, uint32_t chip_status, uint32_t* out_request) { assert (chip_status == 0); //TODO: support suspend esp_err_t ret = ESP_ERR_TIMEOUT; //Nothing happened uint32_t request = 0; if (on_spi_check_yield((app_func_arg_t *)arg)) { request = SPI_FLASH_YIELD_REQ_YIELD; ret = ESP_OK; }{...} if (out_request) { *out_request = request; }{...} return ret; }{ ... } static IRAM_ATTR esp_err_t spi_flash_os_yield(void *arg, uint32_t* out_status) { if (likely(xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)) { #ifdef CONFIG_SPI_FLASH_ERASE_YIELD_TICKS vTaskDelay(CONFIG_SPI_FLASH_ERASE_YIELD_TICKS); #else vTaskDelay(1); #endif }{...} on_spi_yielded((app_func_arg_t*)arg); return ESP_OK; }{ ... } static IRAM_ATTR esp_err_t delay_us(void *arg, uint32_t us) { esp_rom_delay_us(us); return ESP_OK; }{ ... } static IRAM_ATTR void* get_buffer_malloc(void* arg, size_t reqest_size, size_t* out_size) { /* Allocate temporary internal buffer to use for the actual read. If the preferred size doesn't fit in free internal memory, allocate the largest available free block. (May need to shrink read_chunk_size and retry due to race conditions with other tasks also allocating from the heap.) *//* ... */ void* ret = NULL; unsigned retries = 5; size_t read_chunk_size = reqest_size; while(ret == NULL && retries--) { read_chunk_size = MIN(read_chunk_size, heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)); read_chunk_size = (read_chunk_size + 3) & ~3; ret = heap_caps_malloc(read_chunk_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); }{...} ESP_LOGV(TAG, "allocate temp buffer: %p (%d)", ret, read_chunk_size); *out_size = (ret != NULL? read_chunk_size: 0); return ret; }{ ... } static IRAM_ATTR void release_buffer_malloc(void* arg, void *temp_buf) { free(temp_buf); }{ ... } static IRAM_ATTR esp_err_t main_flash_region_protected(void* arg, size_t start_addr, size_t size) { if (!esp_partition_is_flash_region_writable(start_addr, size)) { return ESP_ERR_NOT_ALLOWED; }{...} #if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED if (((app_func_arg_t*)arg)->no_protect || esp_partition_main_flash_region_safe(start_addr, size)) { //ESP_OK = 0, also means protected==0 return ESP_OK; }{...} else { return ESP_ERR_NOT_SUPPORTED; }{...} #endif/* ... */ // !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED return ESP_OK; }{ ... } static IRAM_ATTR void main_flash_op_status(uint32_t op_status) { bool is_erasing = op_status & SPI_FLASH_OS_IS_ERASING_STATUS_FLAG; spi_flash_set_erasing_flag(is_erasing); }{ ... } static DRAM_ATTR app_func_arg_t main_flash_arg = {}; //for SPI1, we have to disable the cache and interrupts before using the SPI bus static const DRAM_ATTR esp_flash_os_functions_t esp_flash_spi1_default_os_functions = { .start = spi1_start, .end = spi1_end, .region_protected = main_flash_region_protected, .delay_us = delay_us, .get_temp_buffer = get_buffer_malloc, .release_temp_buffer = release_buffer_malloc, .check_yield = spi_flash_os_check_yield, .yield = spi_flash_os_yield, #if CONFIG_SPI_FLASH_BROWNOUT_RESET .set_flash_op_status = main_flash_op_status, #else .set_flash_op_status = NULL, #endif }{...}; static const esp_flash_os_functions_t esp_flash_spi23_default_os_functions = { .start = spi23_start, .end = spi23_end, .delay_us = delay_us, .get_temp_buffer = get_buffer_malloc, .release_temp_buffer = release_buffer_malloc, .region_protected = NULL, .check_yield = spi_flash_os_check_yield, .yield = spi_flash_os_yield, .set_flash_op_status = NULL, }{...}; static bool use_bus_lock(int host_id) { if (host_id != SPI1_HOST) { return true; }{...} #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS return true; #else return false; #endif }{ ... } // This function is only called by users usually via `spi_bus_add_flash_device` to initialise os functions. // System will initialise them via `esp_flash_app_enable_os_functions` esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, spi_bus_lock_dev_handle_t dev_handle) { if (use_bus_lock(host_id) && !dev_handle) { return ESP_ERR_INVALID_ARG; }{...} chip->os_func_data = heap_caps_malloc(sizeof(app_func_arg_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (chip->os_func_data == NULL) { return ESP_ERR_NO_MEM; }{...} switch (host_id) { case SPI1_HOST: //SPI1 chip->os_func = &esp_flash_spi1_default_os_functions; break;... case SPI2_HOST: #if SOC_SPI_PERIPH_NUM > 2... case SPI3_HOST: #endif //SPI2, SPI3 chip->os_func = &esp_flash_spi23_default_os_functions; break; default: return ESP_ERR_INVALID_ARG; break;... }{...} *(app_func_arg_t*) chip->os_func_data = (app_func_arg_t) { .dev_lock = dev_handle, .no_protect = true, // This is OK because this code path isn't used for the main flash chip which requires `no_protect = false` }{...}; return ESP_OK; }{ ... } esp_err_t esp_flash_deinit_os_functions(esp_flash_t* chip, spi_bus_lock_dev_handle_t* out_dev_handle) { if (chip->os_func_data) { // SPI bus lock is possibly not used on SPI1 bus *out_dev_handle = ((app_func_arg_t*)chip->os_func_data)->dev_lock; free(chip->os_func_data); }{...} chip->os_func = NULL; chip->os_func_data = NULL; return ESP_OK; }{ ... } esp_err_t esp_flash_init_main_bus_lock(void) { /* The following called functions are only defined if CONFIG_SPI_FLASH_SHARE_SPI1_BUS * is set. Thus, we must not call them if the macro is not defined, else the linker * would trigger errors. *//* ... */ #if CONFIG_SPI_FLASH_SHARE_SPI1_BUS /* bus_lock is registered by `spi_bus_lock_init_main_bus` constructor in spi_common.c */ spi_bus_lock_set_bg_control(g_main_spi_bus_lock, cache_enable, cache_disable, NULL); esp_err_t err = spi_bus_lock_init_main_dev(); if (err != ESP_OK) { return err; }{...} return ESP_OK;/* ... */ #else return ESP_ERR_NOT_SUPPORTED; #endif }{ ... } esp_err_t esp_flash_app_enable_os_functions(esp_flash_t* chip) { main_flash_arg = (app_func_arg_t) { .dev_lock = g_spi_lock_main_flash_dev, .no_protect = false, // Required for the main flash chip }{...}; chip->os_func = &esp_flash_spi1_default_os_functions; chip->os_func_data = &main_flash_arg; return ESP_OK; }{ ... } esp_err_t esp_flash_set_dangerous_write_protection(esp_flash_t *chip, const bool protect) { #if !CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED if (chip == NULL) { return ESP_ERR_INVALID_ARG; }{...} if (chip->os_func_data != NULL) { ((app_func_arg_t*)chip->os_func_data)->no_protect = !protect; }{...} #else/* ... */ (void)chip; (void)protect;/* ... */ #endif return ESP_OK; }{ ... } // The goal of this part is to manually insert one valid task execution interval, if the time since // last valid interval exceed the limitation (CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS). // // Valid task execution interval: continuous time with the cache enabled, which is longer than // CONFIG_SPI_FLASH_ERASE_YIELD_TICKS. Yield time shorter than CONFIG_SPI_FLASH_ERASE_YIELD_TICKS is // not treated as valid interval. static inline IRAM_ATTR bool on_spi_check_yield(app_func_arg_t* ctx) { #ifdef CONFIG_SPI_FLASH_YIELD_DURING_ERASE uint32_t time = esp_system_get_time(); // We handle the reset here instead of in `on_spi_acquired()`, when acquire() and release() is // larger than CONFIG_SPI_FLASH_ERASE_YIELD_TICKS, to save one `esp_system_get_time()` call if ((time - ctx->released_since_us) >= CONFIG_SPI_FLASH_ERASE_YIELD_TICKS * portTICK_PERIOD_MS * 1000) { // Reset the acquired time as if the yield has just happened. ctx->acquired_since_us = time; }{...} else if ((time - ctx->acquired_since_us) >= CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS * 1000) { return true; }{...} /* ... */#endif return false; }{ ... } static inline IRAM_ATTR void on_spi_released(app_func_arg_t* ctx) { #ifdef CONFIG_SPI_FLASH_YIELD_DURING_ERASE ctx->released_since_us = esp_system_get_time(); #endif }{ ... } static inline IRAM_ATTR void on_spi_acquired(app_func_arg_t* ctx) { // Ideally, when the time after `on_spi_released()` before this function is called is larger // than CONFIG_SPI_FLASH_ERASE_YIELD_TICKS, the acquired time should be reset. We assume the // time after `on_spi_check_yield()` before this function is so short that we can do the reset // in that function instead. }{ ... } static inline IRAM_ATTR void on_spi_yielded(app_func_arg_t* ctx) { uint32_t time = esp_system_get_time(); ctx->acquired_since_us = time; }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.