ef_port.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 SFUD flash driver.
  26. * Created on: 2015-01-16
  27. */
  28. #include <easyflash.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdarg.h>
  32. #include <sfud.h>
  33. #include <FreeRTOS.h>
  34. #include "semphr.h"
  35. #include "board.h"
  36. #include "mmcsd_core.h"
  37. #ifdef ULOG_EASYFLASH_BACKEND_ENABLE
  38. /* default ENV set for user */
  39. static const ef_env default_env_set[] = {
  40. {"iap_need_copy_app", "0"},
  41. {"iap_need_crc32_check", "0"},
  42. {"iap_copy_app_size", "0"},
  43. {"stop_in_bootloader", "0"},
  44. };
  45. #define CONSOLEBUF_SIZE 256
  46. static char log_buf[CONSOLEBUF_SIZE];
  47. static SemaphoreHandle_t env_cache_lock;
  48. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  49. static const sfud_flash *flash;
  50. #endif
  51. /**
  52. * Flash port for hardware initialize.
  53. *
  54. * @param default_env default ENV set for user
  55. * @param default_env_size default ENV size
  56. *
  57. * @return result
  58. */
  59. EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size) {
  60. EfErrCode result = EF_NO_ERR;
  61. *default_env = default_env_set;
  62. *default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);
  63. env_cache_lock = xSemaphoreCreateMutex();
  64. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  65. flash = sfud_get_device(0);
  66. #endif
  67. return result;
  68. }
  69. /**
  70. * Read data from flash.
  71. * @note This operation's units is word.
  72. *
  73. * @param addr flash address
  74. * @param buf buffer to store read data
  75. * @param size read bytes size
  76. *
  77. * @return result
  78. */
  79. EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size) {
  80. EfErrCode result = EF_NO_ERR;
  81. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  82. sfud_read(flash, addr, size, (uint8_t *)buf);
  83. #else
  84. if (emmc_read(addr, size, (uint8_t*)buf))
  85. result = EF_READ_ERR;
  86. #endif
  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. EfErrCode result = EF_NO_ERR;
  101. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  102. sfud_err sfud_result = SFUD_SUCCESS;
  103. #endif
  104. /* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */
  105. EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);
  106. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  107. sfud_result = sfud_erase(flash, addr, size);
  108. if(sfud_result != SFUD_SUCCESS) {
  109. result = EF_ERASE_ERR;
  110. }
  111. #else
  112. if (emmc_write(addr, size, NULL))
  113. result = EF_ERASE_ERR;
  114. #endif
  115. return result;
  116. }
  117. /**
  118. * Write data to flash.
  119. * @note This operation's units is word.
  120. * @note This operation must after erase. @see flash_erase.
  121. *
  122. * @param addr flash address
  123. * @param buf the write data buffer
  124. * @param size write bytes size
  125. *
  126. * @return result
  127. */
  128. EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size) {
  129. EfErrCode result = EF_NO_ERR;
  130. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  131. sfud_err sfud_result = SFUD_SUCCESS;
  132. sfud_result = sfud_write(flash, addr, size, (const uint8_t *)buf);
  133. if(sfud_result != SFUD_SUCCESS) {
  134. result = EF_WRITE_ERR;
  135. }
  136. #else
  137. if (emmc_write(addr, size, (uint8_t*)buf))
  138. result = EF_WRITE_ERR;
  139. #endif
  140. return result;
  141. }
  142. /**
  143. * lock the ENV ram cache
  144. */
  145. void ef_port_env_lock(void) {
  146. xSemaphoreTake(env_cache_lock, portMAX_DELAY);
  147. }
  148. /**
  149. * unlock the ENV ram cache
  150. */
  151. void ef_port_env_unlock(void) {
  152. xSemaphoreGive(env_cache_lock);
  153. }
  154. /**
  155. * This function is print flash debug info.
  156. *
  157. * @param file the file which has call this function
  158. * @param line the line number which has call this function
  159. * @param format output format
  160. * @param ... args
  161. *
  162. */
  163. void ef_log_debug(const char *file, const long line, const char *format, ...) {
  164. #ifdef PRINT_DEBUG
  165. va_list args;
  166. /* args point to the first variable parameter */
  167. va_start(args, format);
  168. ef_print("[Flash] (%s:%ld) ", file, line);
  169. /* must use vprintf to print */
  170. vsprintf(log_buf, format, args);
  171. ef_print("%s", log_buf);
  172. va_end(args);
  173. #endif
  174. }
  175. /**
  176. * This function is print flash routine info.
  177. *
  178. * @param format output format
  179. * @param ... args
  180. */
  181. void ef_log_info(const char *format, ...) {
  182. va_list args;
  183. /* args point to the first variable parameter */
  184. va_start(args, format);
  185. ef_print("[Flash] ");
  186. /* must use vprintf to print */
  187. vsprintf(log_buf, format, args);
  188. ef_print("%s", log_buf);
  189. va_end(args);
  190. }
  191. /**
  192. * This function is print flash non-package info.
  193. *
  194. * @param format output format
  195. * @param ... args
  196. */
  197. void ef_print(const char *format, ...) {
  198. va_list args;
  199. /* args point to the first variable parameter */
  200. va_start(args, format);
  201. /* must use vprintf to print */
  202. vsprintf(log_buf, format, args);
  203. printf("%s", log_buf);
  204. va_end(args);
  205. }
  206. #endif /* ULOG_EASYFLASH_BACKEND_ENABLE */