SPI_Flash.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "esp_log.h"
  14. #include "SPI_Flash.h"
  15. #include "esp_spi_flash.h"
  16. static const char *TAG = "spi_flash";
  17. SPI_Flash::SPI_Flash()
  18. {
  19. }
  20. size_t SPI_Flash::chip_size()
  21. {
  22. return spi_flash_get_chip_size();
  23. }
  24. esp_err_t SPI_Flash::erase_sector(size_t sector)
  25. {
  26. esp_err_t result = spi_flash_erase_sector(sector);
  27. if (result == ESP_OK) {
  28. ESP_LOGV(TAG, "erase_sector - sector=0x%08x, result=0x%08x", sector, result);
  29. } else {
  30. ESP_LOGE(TAG, "erase_sector - sector=0x%08x, result=0x%08x", sector, result);
  31. }
  32. return result;
  33. }
  34. esp_err_t SPI_Flash::erase_range(size_t start_address, size_t size)
  35. {
  36. size = (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE;
  37. size = size * SPI_FLASH_SEC_SIZE;
  38. esp_err_t result = spi_flash_erase_range(start_address, size);
  39. if (result == ESP_OK) {
  40. ESP_LOGV(TAG, "erase_range - start_address=0x%08x, size=0x%08x, result=0x%08x", start_address, size, result);
  41. } else {
  42. ESP_LOGE(TAG, "erase_range - start_address=0x%08x, size=0x%08x, result=0x%08x", start_address, size, result);
  43. }
  44. return result;
  45. }
  46. esp_err_t SPI_Flash::write(size_t dest_addr, const void *src, size_t size)
  47. {
  48. esp_err_t result = spi_flash_write(dest_addr, src, size);
  49. if (result == ESP_OK) {
  50. ESP_LOGV(TAG, "write - dest_addr=0x%08x, size=0x%08x, result=0x%08x", dest_addr, size, result);
  51. } else {
  52. ESP_LOGE(TAG, "write - dest_addr=0x%08x, size=0x%08x, result=0x%08x", dest_addr, size, result);
  53. }
  54. return result;
  55. }
  56. esp_err_t SPI_Flash::read(size_t src_addr, void *dest, size_t size)
  57. {
  58. esp_err_t result = spi_flash_read(src_addr, dest, size);
  59. if (result == ESP_OK) {
  60. ESP_LOGV(TAG, "read - src_addr=0x%08x, size=0x%08x, result=0x%08x", src_addr, size, result);
  61. } else {
  62. ESP_LOGE(TAG, "read - src_addr=0x%08x, size=0x%08x, result=0x%08x", src_addr, size, result);
  63. }
  64. return result;
  65. }
  66. size_t SPI_Flash::sector_size()
  67. {
  68. return SPI_FLASH_SEC_SIZE;
  69. }
  70. SPI_Flash::~SPI_Flash()
  71. {
  72. }