1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 #include "esp_err.h"
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include <driver/spi_common_internal.h>
20 #include "sdkconfig.h"
21 
22 #include "esp_flash.h"
23 
24 /** Internal API, don't use in the applications */
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 
31 /** @brief Initialise the default SPI flash chip
32  *
33  * Called by OS startup code. You do not need to call this in your own applications.
34  */
35 #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
36 #define esp_flash_init_default_chip(...) ({ESP_OK;})
37 #else
38 esp_err_t esp_flash_init_default_chip(void);
39 #endif
40 
41 /**
42  *  Enable OS-level SPI flash protections in IDF
43  *
44  *  Called by OS startup code. You do not need to call this in your own applications.
45  *
46  * @return ESP_OK if success, otherwise failed. See return value of ``esp_flash_init_os_functions``.
47  */
48 #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
49 #define esp_flash_app_init(...) ({ESP_OK;})
50 #else
51 esp_err_t esp_flash_app_init(void);
52 #endif
53 
54 /**
55  *  Disable (or enable) OS-level SPI flash protections in IDF
56  *
57  *  Called by the IDF internal code (e.g. coredump). You do not need to call this in your own applications.
58  *
59  * @return always ESP_OK.
60  */
61 #ifdef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
62 #define esp_flash_app_disable_protect(...) ({ESP_OK;})
63 #else
64 esp_err_t esp_flash_app_disable_protect(bool disable);
65 #endif
66 
67 /**
68  *  Initialize OS-level functions for a specific chip.
69  *
70  * @param chip The chip to init os functions.
71  * @param host_id Which SPI host to use, 1 for SPI1, 2 for SPI2 (HSPI), 3 for SPI3 (VSPI)
72  * @param out_dev_id Output of occupied device slot
73  *
74  * @return
75  *      - ESP_OK if success
76  *      - ESP_ERR_INVALID_ARG if host_id is invalid
77  */
78 esp_err_t esp_flash_init_os_functions(esp_flash_t *chip, int host_id, int *out_dev_id);
79 
80 /**
81  * @brief Deinitialize OS-level functions
82  *
83  * @param chip  The chip to deinit os functions
84  * @return always ESP_OK.
85  */
86 esp_err_t esp_flash_deinit_os_functions(esp_flash_t* chip);
87 
88 /**
89  * @brief Initialize the bus lock on the SPI1 bus. Should be called if drivers (including esp_flash)
90  * wants to use SPI1 bus.
91  *
92  * @note When using legacy spi flash API, the bus lock will not be available on SPI1 bus.
93  *
94  * @return esp_err_t always ESP_OK.
95  */
96 esp_err_t esp_flash_init_main_bus_lock(void);
97 
98 /**
99  *  Initialize OS-level functions for the main flash chip.
100  *
101  * @param chip The chip to init os functions. Only pointer to the default chip is supported now.
102  *
103  * @return always ESP_OK
104  */
105 esp_err_t esp_flash_app_enable_os_functions(esp_flash_t* chip);
106 
107 /**
108  *  Disable OS-level functions for the main flash chip during special phases (e.g. coredump)
109  *
110  * @param chip The chip to init os functions. Only "esp_flash_default_chip" is supported now.
111  *
112  * @return always ESP_OK
113  */
114 esp_err_t esp_flash_app_disable_os_functions(esp_flash_t* chip);
115 
116 
117 
118 #ifdef __cplusplus
119 }
120 #endif
121