ef_fal_port.c 5.3 KB

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