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. rt_kprintf("ef_port_read \n");
  86. fal_partition_read(part, addr, (uint8_t *)buf, size);
  87. return result;
  88. }
  89. /**
  90. * Erase data on flash.
  91. * @note This operation is irreversible.
  92. * @note This operation's units is different which on many chips.
  93. *
  94. * @param addr flash address
  95. * @param size erase bytes size
  96. *
  97. * @return result
  98. */
  99. EfErrCode ef_port_erase(uint32_t addr, size_t size)
  100. {
  101. EfErrCode result = EF_NO_ERR;
  102. /* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */
  103. EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);
  104. if (fal_partition_erase(part, addr, size) < 0)
  105. {
  106. result = EF_ERASE_ERR;
  107. }
  108. return result;
  109. }
  110. /**
  111. * Write data to flash.
  112. * @note This operation's units is word.
  113. * @note This operation must after erase. @see flash_erase.
  114. *
  115. * @param addr flash address
  116. * @param buf the write data buffer
  117. * @param size write bytes size
  118. *
  119. * @return result
  120. */
  121. EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size)
  122. {
  123. EfErrCode result = EF_NO_ERR;
  124. if (fal_partition_write(part, addr, (uint8_t *)buf, size) < 0)
  125. {
  126. result = EF_WRITE_ERR;
  127. }
  128. return result;
  129. }
  130. /**
  131. * lock the ENV ram cache
  132. */
  133. void ef_port_env_lock(void)
  134. {
  135. rt_sem_take(&env_cache_lock, RT_WAITING_FOREVER);
  136. }
  137. /**
  138. * unlock the ENV ram cache
  139. */
  140. void ef_port_env_unlock(void)
  141. {
  142. rt_sem_release(&env_cache_lock);
  143. }
  144. /**
  145. * This function is print flash debug info.
  146. *
  147. * @param file the file which has call this function
  148. * @param line the line number which has call this function
  149. * @param format output format
  150. * @param ... args
  151. *
  152. */
  153. void ef_log_debug(const char *file, const long line, const char *format, ...)
  154. {
  155. #ifdef PRINT_DEBUG
  156. va_list args;
  157. /* args point to the first variable parameter */
  158. va_start(args, format);
  159. ef_print("[Flash] (%s:%ld) ", file, line);
  160. /* must use vprintf to print */
  161. rt_vsprintf(log_buf, format, args);
  162. ef_print("%s", log_buf);
  163. va_end(args);
  164. #endif
  165. }
  166. /**
  167. * This function is print flash routine info.
  168. *
  169. * @param format output format
  170. * @param ... args
  171. */
  172. void ef_log_info(const char *format, ...)
  173. {
  174. va_list args;
  175. /* args point to the first variable parameter */
  176. va_start(args, format);
  177. ef_print("[Flash] ");
  178. /* must use vprintf to print */
  179. rt_vsprintf(log_buf, format, args);
  180. ef_print("%s", log_buf);
  181. va_end(args);
  182. }
  183. /**
  184. * This function is print flash non-package info.
  185. *
  186. * @param format output format
  187. * @param ... args
  188. */
  189. void ef_print(const char *format, ...)
  190. {
  191. va_list args;
  192. /* args point to the first variable parameter */
  193. va_start(args, format);
  194. /* must use vprintf to print */
  195. rt_vsprintf(log_buf, format, args);
  196. rt_kprintf("%s", log_buf);
  197. va_end(args);
  198. }