ef_fal_port.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2026-02-05 RuoFeng the first version
  9. */
  10. /*
  11. * This file is part of the EasyFlash Library.
  12. *
  13. * Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining
  16. * a copy of this software and associated documentation files (the
  17. * 'Software'), to deal in the Software without restriction, including
  18. * without limitation the rights to use, copy, modify, merge, publish,
  19. * distribute, sublicense, and/or sell copies of the Software, and to
  20. * permit persons to whom the Software is furnished to do so, subject to
  21. * the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be
  24. * included in all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  29. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  30. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  31. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  32. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. *
  34. * Function: Portable interface for FAL (Flash Abstraction Layer) partition.
  35. * Created on: 2018-05-19
  36. */
  37. #include <easyflash.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <stdarg.h>
  41. #include <rthw.h>
  42. #include <rtthread.h>
  43. #include <fal.h>
  44. /* EasyFlash partition name on FAL partition table */
  45. #define FAL_EF_PART_NAME "easyflash"
  46. /* default ENV set for user */
  47. static const ef_env default_env_set[] = {
  48. {"boot_times", "0"}
  49. };
  50. static char log_buf[RT_CONSOLEBUF_SIZE];
  51. static struct rt_semaphore env_cache_lock;
  52. static const struct fal_partition *part = NULL;
  53. /**
  54. * Flash port for hardware initialize.
  55. *
  56. * @param default_env default ENV set for user
  57. * @param default_env_size default ENV size
  58. *
  59. * @return result
  60. */
  61. EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size)
  62. {
  63. EfErrCode result = EF_NO_ERR;
  64. *default_env = default_env_set;
  65. *default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);
  66. rt_sem_init(&env_cache_lock, "env lock", 1, RT_IPC_FLAG_PRIO);
  67. // rt_kprintf("ef_port_init %d \n\n" , rt_sem_init(&env_cache_lock, "env lock", 1, RT_IPC_FLAG_PRIO));
  68. part = fal_partition_find(FAL_EF_PART_NAME);
  69. EF_ASSERT(part);
  70. return result;
  71. }
  72. /**
  73. * Read data from flash.
  74. * @note This operation's units is word.
  75. *
  76. * @param addr flash address
  77. * @param buf buffer to store read data
  78. * @param size read bytes size
  79. *
  80. * @return result
  81. */
  82. EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size)
  83. {
  84. EfErrCode result = EF_NO_ERR;
  85. fal_partition_read(part, addr, (uint8_t *)buf, size);
  86. return result;
  87. }
  88. /**
  89. * Erase data on flash.
  90. * @note This operation is irreversible.
  91. * @note This operation's units is different which on many chips.
  92. *
  93. * @param addr flash address
  94. * @param size erase bytes size
  95. *
  96. * @return result
  97. */
  98. EfErrCode ef_port_erase(uint32_t addr, size_t size)
  99. {
  100. EfErrCode result = EF_NO_ERR;
  101. /* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */
  102. EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);
  103. if (fal_partition_erase(part, addr, size) < 0)
  104. {
  105. result = EF_ERASE_ERR;
  106. }
  107. return result;
  108. }
  109. /**
  110. * Write data to flash.
  111. * @note This operation's units is word.
  112. * @note This operation must after erase. @see flash_erase.
  113. *
  114. * @param addr flash address
  115. * @param buf the write data buffer
  116. * @param size write bytes size
  117. *
  118. * @return result
  119. */
  120. EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size)
  121. {
  122. EfErrCode result = EF_NO_ERR;
  123. if (fal_partition_write(part, addr, (uint8_t *)buf, size) < 0)
  124. {
  125. result = EF_WRITE_ERR;
  126. }
  127. return result;
  128. }
  129. /**
  130. * lock the ENV ram cache
  131. */
  132. void ef_port_env_lock(void)
  133. {
  134. rt_sem_take(&env_cache_lock, RT_WAITING_FOREVER);
  135. }
  136. /**
  137. * unlock the ENV ram cache
  138. */
  139. void ef_port_env_unlock(void)
  140. {
  141. rt_sem_release(&env_cache_lock);
  142. }
  143. /**
  144. * This function is print flash debug info.
  145. *
  146. * @param file the file which has call this function
  147. * @param line the line number which has call this function
  148. * @param format output format
  149. * @param ... args
  150. *
  151. */
  152. void ef_log_debug(const char *file, const long line, const char *format, ...)
  153. {
  154. #ifdef PRINT_DEBUG
  155. va_list args;
  156. /* args point to the first variable parameter */
  157. va_start(args, format);
  158. ef_print("[Flash] (%s:%ld) ", file, line);
  159. /* must use vprintf to print */
  160. rt_vsprintf(log_buf, format, args);
  161. ef_print("%s", log_buf);
  162. va_end(args);
  163. #endif
  164. }
  165. /**
  166. * This function is print flash routine info.
  167. *
  168. * @param format output format
  169. * @param ... args
  170. */
  171. void ef_log_info(const char *format, ...)
  172. {
  173. va_list args;
  174. /* args point to the first variable parameter */
  175. va_start(args, format);
  176. ef_print("[Flash] ");
  177. /* must use vprintf to print */
  178. rt_vsprintf(log_buf, format, args);
  179. ef_print("%s", log_buf);
  180. va_end(args);
  181. }
  182. /**
  183. * This function is print flash non-package info.
  184. *
  185. * @param format output format
  186. * @param ... args
  187. */
  188. void ef_print(const char *format, ...)
  189. {
  190. va_list args;
  191. /* args point to the first variable parameter */
  192. va_start(args, format);
  193. /* must use vprintf to print */
  194. rt_vsprintf(log_buf, format, args);
  195. rt_kprintf("%s", log_buf);
  196. va_end(args);
  197. }