ef_fal_port.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <rthw.h>
  33. #include <rtthread.h>
  34. #include <fal.h>
  35. /* EasyFlash partition name on FAL partition table */
  36. #define FAL_EF_PART_NAME "easyflash"
  37. /* default ENV set for user */
  38. static const ef_env default_env_set[] = {
  39. {"iap_need_copy_app", "0"},
  40. {"iap_need_crc32_check", "0"},
  41. {"iap_copy_app_size", "0"},
  42. {"stop_in_bootloader", "0"},
  43. };
  44. static char log_buf[RT_CONSOLEBUF_SIZE];
  45. static struct rt_semaphore env_cache_lock;
  46. static const struct fal_partition *part = NULL;
  47. /**
  48. * Flash port for hardware initialize.
  49. *
  50. * @param default_env default ENV set for user
  51. * @param default_env_size default ENV size
  52. *
  53. * @return result
  54. */
  55. EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size) {
  56. EfErrCode result = EF_NO_ERR;
  57. *default_env = default_env_set;
  58. *default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);
  59. rt_sem_init(&env_cache_lock, "env lock", 1, RT_IPC_FLAG_PRIO);
  60. part = fal_partition_find(FAL_EF_PART_NAME);
  61. EF_ASSERT(part);
  62. return result;
  63. }
  64. /**
  65. * Read data from flash.
  66. * @note This operation's units is word.
  67. *
  68. * @param addr flash address
  69. * @param buf buffer to store read data
  70. * @param size read bytes size
  71. *
  72. * @return result
  73. */
  74. EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size) {
  75. EfErrCode result = EF_NO_ERR;
  76. fal_partition_read(part, addr, (uint8_t *)buf, size);
  77. return result;
  78. }
  79. /**
  80. * Erase data on flash.
  81. * @note This operation is irreversible.
  82. * @note This operation's units is different which on many chips.
  83. *
  84. * @param addr flash address
  85. * @param size erase bytes size
  86. *
  87. * @return result
  88. */
  89. EfErrCode ef_port_erase(uint32_t addr, size_t size) {
  90. EfErrCode result = EF_NO_ERR;
  91. /* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */
  92. EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);
  93. if (fal_partition_erase(part, addr, size) < 0)
  94. {
  95. result = EF_ERASE_ERR;
  96. }
  97. return result;
  98. }
  99. /**
  100. * Write data to flash.
  101. * @note This operation's units is word.
  102. * @note This operation must after erase. @see flash_erase.
  103. *
  104. * @param addr flash address
  105. * @param buf the write data buffer
  106. * @param size write bytes size
  107. *
  108. * @return result
  109. */
  110. EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size) {
  111. EfErrCode result = EF_NO_ERR;
  112. if (fal_partition_write(part, addr, (uint8_t *)buf, size) < 0)
  113. {
  114. result = EF_WRITE_ERR;
  115. }
  116. return result;
  117. }
  118. /**
  119. * lock the ENV ram cache
  120. */
  121. void ef_port_env_lock(void) {
  122. rt_sem_take(&env_cache_lock, RT_WAITING_FOREVER);
  123. }
  124. /**
  125. * unlock the ENV ram cache
  126. */
  127. void ef_port_env_unlock(void) {
  128. rt_sem_release(&env_cache_lock);
  129. }
  130. /**
  131. * This function is print flash debug info.
  132. *
  133. * @param file the file which has call this function
  134. * @param line the line number which has call this function
  135. * @param format output format
  136. * @param ... args
  137. *
  138. */
  139. void ef_log_debug(const char *file, const long line, const char *format, ...) {
  140. #ifdef PRINT_DEBUG
  141. va_list args;
  142. /* args point to the first variable parameter */
  143. va_start(args, format);
  144. ef_print("[Flash] (%s:%ld) ", file, line);
  145. /* must use vprintf to print */
  146. rt_vsprintf(log_buf, format, args);
  147. ef_print("%s", log_buf);
  148. va_end(args);
  149. #endif
  150. }
  151. /**
  152. * This function is print flash routine info.
  153. *
  154. * @param format output format
  155. * @param ... args
  156. */
  157. void ef_log_info(const char *format, ...) {
  158. va_list args;
  159. /* args point to the first variable parameter */
  160. va_start(args, format);
  161. ef_print("[Flash] ");
  162. /* must use vprintf to print */
  163. rt_vsprintf(log_buf, format, args);
  164. ef_print("%s", log_buf);
  165. va_end(args);
  166. }
  167. /**
  168. * This function is print flash non-package info.
  169. *
  170. * @param format output format
  171. * @param ... args
  172. */
  173. void ef_print(const char *format, ...) {
  174. va_list args;
  175. /* args point to the first variable parameter */
  176. va_start(args, format);
  177. /* must use vprintf to print */
  178. rt_vsprintf(log_buf, format, args);
  179. rt_kprintf("%s", log_buf);
  180. va_end(args);
  181. }