SpiFlash.cpp 3.1 KB

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