FlashIAP.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifndef MBED_FLASHIAP_H
  23. #define MBED_FLASHIAP_H
  24. #if defined (DEVICE_FLASH) || defined(DOXYGEN_ONLY)
  25. #include "flash_api.h"
  26. #include "platform/SingletonPtr.h"
  27. #include "platform/PlatformMutex.h"
  28. #include "platform/NonCopyable.h"
  29. namespace mbed {
  30. /** \addtogroup drivers */
  31. /** Flash IAP driver. It invokes flash HAL functions.
  32. *
  33. * @note Synchronization level: Thread safe
  34. * @ingroup drivers
  35. */
  36. class FlashIAP : private NonCopyable<FlashIAP> {
  37. public:
  38. FlashIAP();
  39. ~FlashIAP();
  40. /** Initialize a flash IAP device
  41. *
  42. * Should be called once per lifetime of the object.
  43. * @return 0 on success or a negative error code on failure
  44. */
  45. int init();
  46. /** Deinitialize a flash IAP device
  47. *
  48. * @return 0 on success or a negative error code on failure
  49. */
  50. int deinit();
  51. /** Read data from a flash device.
  52. *
  53. * This method invokes memcpy - reads number of bytes from the address
  54. *
  55. * @param buffer Buffer to write to
  56. * @param addr Flash address to begin reading from
  57. * @param size Size to read in bytes
  58. * @return 0 on success, negative error code on failure
  59. */
  60. int read(void *buffer, uint32_t addr, uint32_t size);
  61. /** Program data to pages
  62. *
  63. * The sectors must have been erased prior to being programmed
  64. *
  65. * @param buffer Buffer of data to be written
  66. * @param addr Address of a page to begin writing to
  67. * @param size Size to write in bytes, must be a multiple of program size
  68. * @return 0 on success, negative error code on failure
  69. */
  70. int program(const void *buffer, uint32_t addr, uint32_t size);
  71. /** Erase sectors
  72. *
  73. * The state of an erased sector is undefined until it has been programmed
  74. *
  75. * @param addr Address of a sector to begin erasing, must be a multiple of the sector size
  76. * @param size Size to erase in bytes, must be a multiple of the sector size
  77. * @return 0 on success, negative error code on failure
  78. */
  79. int erase(uint32_t addr, uint32_t size);
  80. /** Get the sector size at the defined address
  81. *
  82. * Sector size might differ at address ranges.
  83. * An example <0-0x1000, sector size=1024; 0x10000-0x20000, size=2048>
  84. *
  85. * @param addr Address of or inside the sector to query
  86. * @return Size of a sector in bytes or MBED_FLASH_INVALID_SIZE if not mapped
  87. */
  88. uint32_t get_sector_size(uint32_t addr) const;
  89. /** Get the flash start address
  90. *
  91. * @return Flash start address
  92. */
  93. uint32_t get_flash_start() const;
  94. /** Get the flash size
  95. *
  96. * @return Flash size
  97. */
  98. uint32_t get_flash_size() const;
  99. /** Get the program page size
  100. *
  101. * The page size defines the writable page size
  102. * @return Size of a program page in bytes
  103. */
  104. uint32_t get_page_size() const;
  105. private:
  106. /* Check if address and size are aligned to a sector
  107. *
  108. * @param addr Address of block to check for alignment
  109. * @param size Size of block to check for alignment
  110. * @return true if the block is sector aligned, false otherwise
  111. */
  112. bool is_aligned_to_sector(uint32_t addr, uint32_t size);
  113. flash_t _flash;
  114. uint8_t *_page_buf;
  115. static SingletonPtr<PlatformMutex> _mutex;
  116. };
  117. } /* namespace mbed */
  118. #endif /* DEVICE_FLASH */
  119. #endif /* MBED_FLASHIAP_H */