| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- /*
- * This file is part of the EasyFlash Library.
- *
- * Copyright (c) 2015, Armink, <armink.ztl@gmail.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * 'Software'), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * Function: Portable interface for stm32f10x platform.
- * Created on: 2015-01-16
- */
- #include <easyflash.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include "stm32g0xx_hal.h"
- #include "main.h"
- #include <rtthread.h>
- #include "Host.h"
- #include "driver_spiflash.h"
- extern void MX_SPIFlash_Init(void);
- extern uint8_t wwdg_state ; //标记看门后状态 初始化后置One 开始喂狗
- extern WWDG_HandleTypeDef hwwdg;
- /*********** 需要备份的重要参数 ***********/
- extern SYS_INFO sys_info; // 备份系统信息
- extern Sensor sensor[MAX_SENSOR_NUM]; // 备份要素配置信息
- extern Loar_Device Slave_list[MAX_Slave_NUM + 1]; // 备份从机参数
- /*********** 需要备份的重要参数 ***********/
- /* default environment variables set for user */
- static const ef_env default_env_set[] = {
- {"sys_info", &sys_info, sizeof(SYS_INFO)},
- {"sensor", &sensor, sizeof(Sensor) * MAX_SENSOR_NUM},
- {"Slave_list", &Slave_list, sizeof(Loar_Device) * (MAX_Slave_NUM + 1)},
- };
- static char log_buf[128];
- /**
- * Flash port for hardware initialize.
- *
- * @param default_env default ENV set for user
- * @param default_env_size default ENV size
- *
- * @return result
- */
- EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size)
- {
- EfErrCode result = EF_NO_ERR;
- *default_env = default_env_set;
- *default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);
- MX_SPIFlash_Init();
- return result;
- }
- /**
- * Read data from flash.
- * @note This operation's units is word.
- *
- * @param addr flash address
- * @param buf buffer to store read data
- * @param size read bytes size
- *
- * @return result
- */
- EfErrCode ef_port_read(uint32_t addr, uint32_t *buf, size_t size)
- {
- EfErrCode result = EF_NO_ERR;
- SPI_FLASH_BufferRead((uint8_t *)buf, addr, size);
- //rt_kprintf("ef_port_read %X > %d\n",addr , size);
- if (wwdg_state == One)
- {
- HAL_WWDG_Refresh(&hwwdg);
- }
- return result;
- }
- /**
- * Erase data on flash.
- * @note This operation is irreversible.
- * @note This operation's units is different which on many chips.
- *
- * @param addr flash address
- * @param size erase bytes size
- *
- * @return result
- */
- EfErrCode ef_port_erase(uint32_t addr, size_t size)
- {
- EfErrCode result = EF_NO_ERR;
- /* make sure the start address is a multiple of FLASH_ERASE_MIN_SIZE */
- EF_ASSERT(addr % EF_ERASE_MIN_SIZE == 0);
- //rt_kprintf("ef_port_erase %X > %d\n",addr , size);
- for (uint16_t a = 0; a < size / EF_ERASE_MIN_SIZE; a++)
- {
- SPI_FLASH_SectorErase(addr + a * EF_ERASE_MIN_SIZE);
- if (wwdg_state == One)
- {
- HAL_WWDG_Refresh(&hwwdg);
- }
- }
- return result;
- }
- /**
- * Write data to flash.
- * @note This operation's units is word.
- * @note This operation must after erase. @see flash_erase.
- *
- * @param addr flash address
- * @param buf the write data buffer
- * @param size write bytes size
- *
- * @return result
- */
- EfErrCode ef_port_write(uint32_t addr, const uint32_t *buf, size_t size)
- {
- EfErrCode result = EF_NO_ERR;
- SPI_FLASH_BufferWrite((uint8_t *)buf, addr, size);
- if (wwdg_state == One)
- {
- HAL_WWDG_Refresh(&hwwdg);
- }
- //rt_kprintf("ef_port_write %X > %d\n",addr , size);
- return result;
- }
- /**
- * lock the ENV ram cache
- */
- void ef_port_env_lock(void)
- {
- __disable_irq();
- }
- /**
- * unlock the ENV ram cache
- */
- void ef_port_env_unlock(void)
- {
- __enable_irq();
- }
- /**
- * This function is print flash debug info.
- *
- * @param file the file which has call this function
- * @param line the line number which has call this function
- * @param format output format
- * @param ... args
- *
- */
- void ef_log_debug(const char *file, const long line, const char *format, ...)
- {
- #ifdef PRINT_DEBUG
- va_list args;
- /* args point to the first variable parameter */
- va_start(args, format);
- ef_print("[Flash](%s:%ld) ", file, line);
- /* must use vprintf to print */
- rt_vsprintf(log_buf, format, args);
- ef_print("%s", log_buf);
- rt_kprintf("\r");
- va_end(args);
- #endif
- }
- /**
- * This function is print flash routine info.
- *
- * @param format output format
- * @param ... args
- */
- void ef_log_info(const char *format, ...)
- {
- va_list args;
- /* args point to the first variable parameter */
- va_start(args, format);
- ef_print("[Flash]");
- /* must use vprintf to print */
- rt_vsprintf(log_buf, format, args);
- ef_print("%s", log_buf);
- rt_kprintf("\r");
- va_end(args);
- }
- /**
- * This function is print flash non-package info.
- *
- * @param format output format
- * @param ... args
- */
- void ef_print(const char *format, ...)
- {
- va_list args;
- /* args point to the first variable parameter */
- va_start(args, format);
- /* must use vprintf to print */
- rt_vsprintf(log_buf, format, args);
- rt_kprintf("%s", log_buf);
- va_end(args);
- }
|