ef_fal_port.c 5.6 KB

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