SpiFlash.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "SpiFlash.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <fstream>
  18. #include <iostream>
  19. #include <vector>
  20. #include <string>
  21. #include "sdkconfig.h"
  22. #include "esp_flash_data_types.h"
  23. using namespace std;
  24. SpiFlash::SpiFlash()
  25. {
  26. return;
  27. }
  28. SpiFlash::~SpiFlash()
  29. {
  30. deinit();
  31. }
  32. void SpiFlash::init(size_t chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partitions_bin)
  33. {
  34. // De-initialize first
  35. deinit();
  36. this->chip_size = chip_size;
  37. this->block_size = block_size;
  38. this->sector_size = sector_size;
  39. this->page_size = page_size;
  40. this->memory = (uint8_t *) malloc(this->chip_size);
  41. memset(this->memory, 0xFF, this->chip_size);
  42. ifstream ifd(partitions_bin, ios::binary | ios::ate);
  43. int size = ifd.tellg();
  44. ifd.seekg(0, ios::beg);
  45. vector<char> buffer;
  46. buffer.resize(size);
  47. ifd.read(buffer.data(), size);
  48. memcpy(&this->memory[CONFIG_PARTITION_TABLE_OFFSET], buffer.data(), buffer.size());
  49. }
  50. void SpiFlash::deinit()
  51. {
  52. if(inited)
  53. {
  54. free(this->memory);
  55. }
  56. }
  57. size_t SpiFlash::get_chip_size()
  58. {
  59. return this->chip_size;
  60. }
  61. size_t SpiFlash::get_sector_size()
  62. {
  63. return this->sector_size;
  64. }
  65. esp_rom_spiflash_result_t SpiFlash::erase_block(uint32_t block)
  66. {
  67. memset(&this->memory[block * this->block_size], 0xFF, this->block_size);
  68. return ESP_ROM_SPIFLASH_RESULT_OK;
  69. }
  70. esp_rom_spiflash_result_t SpiFlash::erase_sector(size_t sector)
  71. {
  72. memset(&this->memory[sector * this->sector_size], 0xFF, this->sector_size);
  73. return ESP_ROM_SPIFLASH_RESULT_OK;
  74. }
  75. esp_rom_spiflash_result_t SpiFlash::erase_page(uint32_t page)
  76. {
  77. memset(&this->memory[page * this->page_size], 0xFF, this->page_size);
  78. return ESP_ROM_SPIFLASH_RESULT_OK;
  79. }
  80. esp_rom_spiflash_result_t SpiFlash::write(size_t dest_addr, const void *src, size_t size)
  81. {
  82. // Emulate inability to set programmed bits without erasing
  83. for(uint32_t ctr = 0; ctr < size; ctr++)
  84. {
  85. uint8_t data = ((uint8_t*)src)[ctr];
  86. uint8_t written = this->memory[dest_addr + ctr];
  87. data &= written;
  88. this->memory[dest_addr + ctr] = data;
  89. }
  90. return ESP_ROM_SPIFLASH_RESULT_OK;
  91. }
  92. esp_rom_spiflash_result_t SpiFlash::read(size_t src_addr, void *dest, size_t size)
  93. {
  94. memcpy(dest, &this->memory[src_addr], size);
  95. return ESP_ROM_SPIFLASH_RESULT_OK;
  96. }
  97. uint8_t* SpiFlash::get_memory_ptr(uint32_t src_address)
  98. {
  99. return &this->memory[src_address];
  100. }