FlashIAP.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2017 ARM Limited
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <algorithm>
  25. #include "FlashIAP.h"
  26. #include "mbed_assert.h"
  27. #ifdef DEVICE_FLASH
  28. namespace mbed {
  29. SingletonPtr<PlatformMutex> FlashIAP::_mutex;
  30. static inline bool is_aligned(uint32_t number, uint32_t alignment)
  31. {
  32. if ((number % alignment) != 0) {
  33. return false;
  34. } else {
  35. return true;
  36. }
  37. }
  38. FlashIAP::FlashIAP()
  39. {
  40. }
  41. FlashIAP::~FlashIAP()
  42. {
  43. }
  44. int FlashIAP::init()
  45. {
  46. int ret = 0;
  47. _mutex->lock();
  48. if (flash_init(&_flash)) {
  49. ret = -1;
  50. }
  51. uint32_t page_size = get_page_size();
  52. _page_buf = new uint8_t[page_size];
  53. _mutex->unlock();
  54. return ret;
  55. }
  56. int FlashIAP::deinit()
  57. {
  58. int ret = 0;
  59. _mutex->lock();
  60. if (flash_free(&_flash)) {
  61. ret = -1;
  62. }
  63. delete[] _page_buf;
  64. _mutex->unlock();
  65. return ret;
  66. }
  67. int FlashIAP::read(void *buffer, uint32_t addr, uint32_t size)
  68. {
  69. int32_t ret = -1;
  70. _mutex->lock();
  71. ret = flash_read(&_flash, addr, (uint8_t *) buffer, size);
  72. _mutex->unlock();
  73. return ret;
  74. }
  75. int FlashIAP::program(const void *buffer, uint32_t addr, uint32_t size)
  76. {
  77. uint32_t page_size = get_page_size();
  78. uint32_t flash_size = flash_get_size(&_flash);
  79. uint32_t flash_start_addr = flash_get_start_address(&_flash);
  80. uint32_t chunk, prog_size;
  81. const uint8_t *buf = (uint8_t *) buffer;
  82. const uint8_t *prog_buf;
  83. // addr should be aligned to page size
  84. if (!is_aligned(addr, page_size) || (!buffer) ||
  85. ((addr + size) > (flash_start_addr + flash_size))) {
  86. return -1;
  87. }
  88. int ret = 0;
  89. _mutex->lock();
  90. while (size) {
  91. uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
  92. bool unaligned_src = (((size_t) buf / sizeof(uint32_t) * sizeof(uint32_t)) != (size_t) buf);
  93. chunk = std::min(current_sector_size - (addr % current_sector_size), size);
  94. // Need to use the internal page buffer in any of these two cases:
  95. // 1. Size is not page aligned
  96. // 2. Source buffer is not aligned to uint32_t. This is not supported by many targets (although
  97. // the pointer they accept is of uint8_t).
  98. if (unaligned_src || (chunk < page_size)) {
  99. chunk = std::min(chunk, page_size);
  100. memcpy(_page_buf, buf, chunk);
  101. if (chunk < page_size) {
  102. memset(_page_buf + chunk, 0xFF, page_size - chunk);
  103. }
  104. prog_buf = _page_buf;
  105. prog_size = page_size;
  106. } else {
  107. chunk = chunk / page_size * page_size;
  108. prog_buf = buf;
  109. prog_size = chunk;
  110. }
  111. if (flash_program_page(&_flash, addr, prog_buf, prog_size)) {
  112. ret = -1;
  113. break;
  114. }
  115. size -= chunk;
  116. addr += chunk;
  117. buf += chunk;
  118. }
  119. _mutex->unlock();
  120. return ret;
  121. }
  122. bool FlashIAP::is_aligned_to_sector(uint32_t addr, uint32_t size)
  123. {
  124. uint32_t current_sector_size = flash_get_sector_size(&_flash, addr);
  125. if (!is_aligned(size, current_sector_size) ||
  126. !is_aligned(addr, current_sector_size)) {
  127. return false;
  128. } else {
  129. return true;
  130. }
  131. }
  132. int FlashIAP::erase(uint32_t addr, uint32_t size)
  133. {
  134. uint32_t current_sector_size;
  135. uint32_t flash_size = flash_get_size(&_flash);
  136. uint32_t flash_start_addr = flash_get_start_address(&_flash);
  137. uint32_t flash_end_addr = flash_start_addr + flash_size;
  138. uint32_t erase_end_addr = addr + size;
  139. if (erase_end_addr > flash_end_addr) {
  140. return -1;
  141. } else if (erase_end_addr < flash_end_addr) {
  142. uint32_t following_sector_size = flash_get_sector_size(&_flash, erase_end_addr);
  143. if (!is_aligned(erase_end_addr, following_sector_size)) {
  144. return -1;
  145. }
  146. }
  147. int32_t ret = 0;
  148. _mutex->lock();
  149. while (size) {
  150. ret = flash_erase_sector(&_flash, addr);
  151. if (ret != 0) {
  152. ret = -1;
  153. break;
  154. }
  155. current_sector_size = flash_get_sector_size(&_flash, addr);
  156. size -= current_sector_size;
  157. addr += current_sector_size;
  158. }
  159. _mutex->unlock();
  160. return ret;
  161. }
  162. uint32_t FlashIAP::get_page_size() const
  163. {
  164. return flash_get_page_size(&_flash);
  165. }
  166. uint32_t FlashIAP::get_sector_size(uint32_t addr) const
  167. {
  168. return flash_get_sector_size(&_flash, addr);
  169. }
  170. uint32_t FlashIAP::get_flash_start() const
  171. {
  172. return flash_get_start_address(&_flash);
  173. }
  174. uint32_t FlashIAP::get_flash_size() const
  175. {
  176. return flash_get_size(&_flash);
  177. }
  178. }
  179. #endif