ef_port.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 stm32f10x platform.
  26. * Created on: 2015-01-16
  27. */
  28. #include <easyflash.h>
  29. #include <stdlib.h>
  30. #include <stdarg.h>
  31. #include "stm32g0xx_hal.h"
  32. #include "main.h"
  33. #include <rtthread.h>
  34. #include "Host.h"
  35. #include "driver_spiflash.h"
  36. extern void MX_SPIFlash_Init(void);
  37. extern uint8_t wwdg_state ; //标记看门后状态 初始化后置One 开始喂狗
  38. extern WWDG_HandleTypeDef hwwdg;
  39. /*********** 需要备份的重要参数 ***********/
  40. extern SYS_INFO sys_info; // 备份系统信息
  41. extern Sensor sensor[MAX_SENSOR_NUM]; // 备份要素配置信息
  42. extern Loar_Device Slave_list[MAX_Slave_NUM + 1]; // 备份从机参数
  43. /*********** 需要备份的重要参数 ***********/
  44. /* default environment variables set for user */
  45. static const ef_env default_env_set[] = {
  46. {"sys_info", &sys_info, sizeof(SYS_INFO)},
  47. {"sensor", &sensor, sizeof(Sensor) * MAX_SENSOR_NUM},
  48. {"Slave_list", &Slave_list, sizeof(Loar_Device) * (MAX_Slave_NUM + 1)},
  49. };
  50. static char log_buf[128];
  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. {
  61. EfErrCode result = EF_NO_ERR;
  62. *default_env = default_env_set;
  63. *default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);
  64. MX_SPIFlash_Init();
  65. return result;
  66. }
  67. /**
  68. * Read data from flash.
  69. * @note This operation's units is word.
  70. *
  71. * @param addr flash address
  72. * @param buf buffer to store read data
  73. * @param size read bytes size
  74. *
  75. * @return result
  76. */
  77. EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size)
  78. {
  79. EfErrCode result = EF_NO_ERR;
  80. SPI_FLASH_BufferRead((uint8_t *)buf, addr, size);
  81. //rt_kprintf("ef_port_read %X > %d\n",addr , size);
  82. if (wwdg_state == One)
  83. {
  84. HAL_WWDG_Refresh(&hwwdg);
  85. }
  86. return result;
  87. }
  88. /**
  89. * Erase data on flash.
  90. * @note This operation is irreversible.
  91. * @note This operation's units is different which on many chips.
  92. *
  93. * @param addr flash address
  94. * @param size erase bytes size
  95. *
  96. * @return result
  97. */
  98. EfErrCode ef_port_erase(uint32_t addr, size_t size)
  99. {
  100. EfErrCode result = EF_NO_ERR;
  101. /* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */
  102. EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);
  103. //rt_kprintf("ef_port_erase %X > %d\n",addr , size);
  104. for (uint16_t a = 0; a < size / EF_ERASE_MIN_SIZE; a++)
  105. {
  106. SPI_FLASH_SectorErase(addr + a * EF_ERASE_MIN_SIZE);
  107. if (wwdg_state == One)
  108. {
  109. HAL_WWDG_Refresh(&hwwdg);
  110. }
  111. }
  112. return result;
  113. }
  114. /**
  115. * Write data to flash.
  116. * @note This operation's units is word.
  117. * @note This operation must after erase. @see flash_erase.
  118. *
  119. * @param addr flash address
  120. * @param buf the write data buffer
  121. * @param size write bytes size
  122. *
  123. * @return result
  124. */
  125. EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size)
  126. {
  127. EfErrCode result = EF_NO_ERR;
  128. SPI_FLASH_BufferWrite((uint8_t *)buf, addr, size);
  129. if (wwdg_state == One)
  130. {
  131. HAL_WWDG_Refresh(&hwwdg);
  132. }
  133. //rt_kprintf("ef_port_write %X > %d\n",addr , size);
  134. return result;
  135. }
  136. /**
  137. * lock the ENV ram cache
  138. */
  139. void ef_port_env_lock(void)
  140. {
  141. __disable_irq();
  142. }
  143. /**
  144. * unlock the ENV ram cache
  145. */
  146. void ef_port_env_unlock(void)
  147. {
  148. __enable_irq();
  149. }
  150. /**
  151. * This function is print flash debug info.
  152. *
  153. * @param file the file which has call this function
  154. * @param line the line number which has call this function
  155. * @param format output format
  156. * @param ... args
  157. *
  158. */
  159. void ef_log_debug(const char *file, const long line, const char *format, ...)
  160. {
  161. #ifdef PRINT_DEBUG
  162. va_list args;
  163. /* args point to the first variable parameter */
  164. va_start(args, format);
  165. ef_print("[Flash](%s:%ld) ", file, line);
  166. /* must use vprintf to print */
  167. rt_vsprintf(log_buf, format, args);
  168. ef_print("%s", log_buf);
  169. rt_kprintf("\r");
  170. va_end(args);
  171. #endif
  172. }
  173. /**
  174. * This function is print flash routine info.
  175. *
  176. * @param format output format
  177. * @param ... args
  178. */
  179. void ef_log_info(const char *format, ...)
  180. {
  181. va_list args;
  182. /* args point to the first variable parameter */
  183. va_start(args, format);
  184. ef_print("[Flash]");
  185. /* must use vprintf to print */
  186. rt_vsprintf(log_buf, format, args);
  187. ef_print("%s", log_buf);
  188. rt_kprintf("\r");
  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. {
  199. va_list args;
  200. /* args point to the first variable parameter */
  201. va_start(args, format);
  202. /* must use vprintf to print */
  203. rt_vsprintf(log_buf, format, args);
  204. rt_kprintf("%s", log_buf);
  205. va_end(args);
  206. }