hosal_flash.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __HOSAL_FLASH_H__
  31. #define __HOSAL_FLASH_H__
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /** @addtogroup hosal_flash FLASH
  36. * HOSAL FLASH API
  37. *
  38. * @{
  39. */
  40. #include <stdint.h>
  41. #define HOSAL_FLASH_FLAG_ADDR_0 0 /**< @brief Open flash prtition address 0 in prtition table */
  42. #define HOSAL_FLASH_FLAG_ADDR_1 (1 << 0) /**< @brief Open flash prtition address 1 in prtition table */
  43. /**
  44. * Open the partition table and use the bus physical address of flash.
  45. * (If it is not set, the offset address set in the partition table is used by default.)
  46. */
  47. #define HOSAL_FLASH_FLAG_BUSADDR (1 << 1)
  48. /**
  49. * @brief Hal flash partition device
  50. */
  51. typedef struct hosal_flash_dev {
  52. void *flash_dev; /**< @brief flash device */
  53. } hosal_flash_dev_t;
  54. /**
  55. * @brief Hal flash partition manage struct
  56. */
  57. typedef struct {
  58. const char *partition_description; /**< @brief name */
  59. uint32_t partition_start_addr; /**< @brief start addr */
  60. uint32_t partition_length; /**< @brief length */
  61. uint32_t partition_options; /**< @brief options */
  62. } hosal_logic_partition_t;
  63. /**
  64. * @brief Open a flash partition device
  65. *
  66. * @param[in] name flash partition name
  67. * @param[in] flags flash flags
  68. * - HOSAL_FLASH_FLAG_ADDR_0
  69. * - HOSAL_FLASH_FLAG_ADDR_1
  70. * - HOSAL_FLASH_FLAG_BUSADDR
  71. *
  72. * @return
  73. * - NULL flash open error
  74. * - otherwise is flash partition device
  75. */
  76. hosal_flash_dev_t *hosal_flash_open(const char *name, unsigned int flags);
  77. /**
  78. * @brief Get the information of the specified flash area
  79. *
  80. * @param[in] p_dev The target flash logical partition device
  81. * @param[out] partition The buffer to store partition info
  82. *
  83. * @return
  84. * - 0 On success
  85. * - otherwise is error
  86. */
  87. int hosal_flash_info_get(hosal_flash_dev_t *p_dev, hosal_logic_partition_t *partition);
  88. /**
  89. * @brief Erase an area on a Flash logical partition
  90. *
  91. * @note Erase on an address will erase all data on a sector that the
  92. * address is belonged to, this function does not save data that
  93. * beyond the address area but in the affected sector, the data
  94. * will be lost.
  95. *
  96. * @param[in] p_dev The target flash logical partition which should be erased
  97. * @param[in] off_set Start address of the erased flash area
  98. * @param[in] size Size of the erased flash area
  99. *
  100. * @return
  101. * - 0 On success
  102. * - otherwise is error
  103. */
  104. int hosal_flash_erase(hosal_flash_dev_t *p_dev, uint32_t off_set, uint32_t size);
  105. /**
  106. * @brief Write data to an area on a flash logical partition without erase
  107. *
  108. * @param[in] p_dev The target flash logical partition which should be read which should be written
  109. * @param[in/out] off_set Point to the start address that the data is written to, and
  110. * point to the last unwritten address after this function is
  111. * returned, so you can call this function serval times without
  112. * update this start address.
  113. * @param[in] in_buf point to the data buffer that will be written to flash
  114. * @param[in] in_buf_size The size of the buffer
  115. *
  116. * @return
  117. * - 0 On success
  118. * - otherwise is error
  119. */
  120. int hosal_flash_write(hosal_flash_dev_t *p_dev, uint32_t *off_set,
  121. const void *in_buf, uint32_t in_buf_size);
  122. /**
  123. * @brief Write data to an area on a flash logical partition with erase first
  124. *
  125. * @param[in] p_dev The target flash logical partition which should be read which should be written
  126. * @param[in/out] off_set Point to the start address that the data is written to, and
  127. * point to the last unwritten address after this function is
  128. * returned, so you can call this function serval times without
  129. * update this start address.
  130. * @param[in] in_buf point to the data buffer that will be written to flash
  131. * @param[in] in_buf_size The length of the buffer
  132. *
  133. * @return
  134. * - 0 On success
  135. * - otherwise is error
  136. */
  137. int hosal_flash_erase_write(hosal_flash_dev_t *p_dev, uint32_t *off_set,
  138. const void *in_buf, uint32_t in_buf_size);
  139. /**
  140. * @brief Read data from an area on a Flash to data buffer in RAM
  141. *
  142. * @param[in] p_dev The target flash logical partition which should be read
  143. * @param[in/out] off_set Point to the start address that the data is read, and
  144. * point to the last unread address after this function is
  145. * returned, so you can call this function serval times without
  146. * update this start address.
  147. * @param[in] out_buf Point to the data buffer that stores the data read from flash
  148. * @param[in] out_buf_size The length of the buffer
  149. *
  150. * @return
  151. * - 0 On success
  152. * - otherwise is error
  153. */
  154. int hosal_flash_read(hosal_flash_dev_t *p_dev, uint32_t *off_set,
  155. void *out_buf, uint32_t out_buf_size);
  156. /**
  157. * @brief Close a flash partition device
  158. *
  159. * @param[in] p_dev flash partition device
  160. *
  161. * @return
  162. * - 0 On success
  163. * - otherwise is error
  164. */
  165. int hosal_flash_close(hosal_flash_dev_t *p_dev);
  166. /**
  167. * @brief Read data from a row address on a Flash to data buffer in RAM
  168. *
  169. * @param[in] buffer Point to the data buffer that stores the data read from flash
  170. * @param[in] address Address on flash to read from
  171. * @param[in] length Length (in bytes) of data to read
  172. *
  173. * @return
  174. * - 0 On success
  175. * - otherwise is error
  176. */
  177. int hosal_flash_raw_read(void *buffer, uint32_t address, uint32_t length);
  178. /**
  179. * @brief Write data to a row address on a Flash
  180. *
  181. * @param[in] buffer Point to the data buffer that will be written to flash
  182. * @param[in] address Address on flash to write to
  183. * @param[in] length Length (in bytes) of data to write
  184. *
  185. * @return
  186. * - 0 On success
  187. * - otherwise is error
  188. */
  189. int hosal_flash_raw_write(void *buffer, uint32_t address, uint32_t length);
  190. /**
  191. * @brief Erase a region of the flash
  192. *
  193. * @param[in] start_addr Address to start erasing flash.
  194. * @param[in] length Length of region to erase.
  195. *
  196. * @return
  197. * - 0 On success
  198. * - otherwise is error
  199. */
  200. int hosal_flash_raw_erase(uint32_t start_addr, uint32_t length);
  201. /** @} */
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif /* __HOSAL_FLASH_H__ */
  206. /* end of file */