1 // Copyright 2015-2017 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 #include "esp_log.h"
16 #include "SPI_Flash.h"
17 #include "esp_spi_flash.h"
18 static const char *TAG = "spi_flash";
19 
SPI_Flash()20 SPI_Flash::SPI_Flash()
21 {
22 }
23 
chip_size()24 size_t SPI_Flash::chip_size()
25 {
26     return spi_flash_get_chip_size();
27 }
28 
erase_sector(size_t sector)29 esp_err_t SPI_Flash::erase_sector(size_t sector)
30 {
31     esp_err_t result = spi_flash_erase_sector(sector);
32     if (result == ESP_OK) {
33         ESP_LOGV(TAG, "erase_sector - sector=0x%08x, result=0x%08x", sector, result);
34     } else {
35         ESP_LOGE(TAG, "erase_sector - sector=0x%08x, result=0x%08x", sector, result);
36     }
37     return result;
38 }
erase_range(size_t start_address,size_t size)39 esp_err_t SPI_Flash::erase_range(size_t start_address, size_t size)
40 {
41     size = (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
42     size = size * SPI_FLASH_SEC_SIZE;
43     esp_err_t result = spi_flash_erase_range(start_address, size);
44     if (result == ESP_OK) {
45         ESP_LOGV(TAG, "erase_range - start_address=0x%08x, size=0x%08x, result=0x%08x", start_address, size, result);
46     } else {
47         ESP_LOGE(TAG, "erase_range - start_address=0x%08x, size=0x%08x, result=0x%08x", start_address, size, result);
48     }
49     return result;
50 }
51 
write(size_t dest_addr,const void * src,size_t size)52 esp_err_t SPI_Flash::write(size_t dest_addr, const void *src, size_t size)
53 {
54     esp_err_t result = spi_flash_write(dest_addr, src, size);
55     if (result == ESP_OK) {
56         ESP_LOGV(TAG, "write - dest_addr=0x%08x, size=0x%08x, result=0x%08x", dest_addr, size, result);
57     } else {
58         ESP_LOGE(TAG, "write - dest_addr=0x%08x, size=0x%08x, result=0x%08x", dest_addr, size, result);
59     }
60     return result;
61 }
62 
read(size_t src_addr,void * dest,size_t size)63 esp_err_t SPI_Flash::read(size_t src_addr, void *dest, size_t size)
64 {
65     esp_err_t result = spi_flash_read(src_addr, dest, size);
66     if (result == ESP_OK) {
67         ESP_LOGV(TAG, "read - src_addr=0x%08x, size=0x%08x, result=0x%08x", src_addr, size, result);
68     } else {
69         ESP_LOGE(TAG, "read - src_addr=0x%08x, size=0x%08x, result=0x%08x", src_addr, size, result);
70     }
71     return result;
72 }
73 
sector_size()74 size_t SPI_Flash::sector_size()
75 {
76     return SPI_FLASH_SEC_SIZE;
77 }
78 
~SPI_Flash()79 SPI_Flash::~SPI_Flash()
80 {
81 }
82