FLASH.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef __FLASH_H_
  2. #define __FLASH_H_
  3. #include "stm32l4xx_hal.h"
  4. #include "calib_model.h"
  5. #include "calib_piecewise_linear.h"
  6. /* ========================
  7. * STM32L431CCT6 256KB Flash 自定义三段式 IAP升级系统
  8. * 结构:APP1 + APP2 + IAPDataBuffer
  9. * 通信方式:UART1 (RS485)
  10. * 校验方式:MD5
  11. * APP1:存放出厂固件,作为恢复镜像,加上写保护。
  12. * APP2:与 APP1 构成双镜像,支持回滚机制。
  13. * App2缓存区:OTA 升级时下载新固件的临时存储区,大小与应用程序分区一致。
  14. * 备用区:可存储日志、临时数据或未来扩展功能。
  15. * Param(MD5) 和 APP应用参数 固定在 Flash 末尾(最后 4KB),避免被应用程序擦写覆盖。
  16. * ========================
  17. */
  18. /* ========== Flash 分区方案 ==========
  19. * STM32L431CCT6 Flash: 256KB (0x08000000 - 0x0803FFFF) 0x40000字节
  20. *
  21. * APP1 : 0x08000000 - 0x08013FFF (80KB)
  22. * APP2 : 0x08014000 - 0x08027FFF (80KB)
  23. * IAPDataBuffer : 0x08028000 - 0x0803BFFF (80KB)
  24. * 系统配置 :0x0803C000 - 0x0803C7FF (2KB) (通讯地址 mac地址)
  25. * 标定参数 :0x0803C800 - 0x0803C7FF (2KB) (ADC标定值 温度校正表)
  26. * 快照参数 :0x0803D000 - 0x0803D7FF (2KB) (统计类、刻度类参数)
  27. * 日志文件 :0x0803D800 - 0x0803FFFF (10KB)
  28. */
  29. //------------------------宏定义-----------------------------//
  30. #define APP1_ADDR 0x08000000 // 烧录区 0x14000
  31. #define APP2_ADDR 0x08014000 // APP2
  32. #define IAPDataBuffer_ADDR 0x08028000 // IAPDataBuffer
  33. #define FLASH_PARAM_ADDR 0x0803C000 // STM32L431CCT6 最后一页起始地址 系统配置
  34. #define IAP_PARAM_ADDR 0x0803D000 // 升级标志位区 快照参数
  35. #define CALIB_PARAM_ADDR 0x0803C800 // 传感器标定区域
  36. #define UART_TIMEOUT 1000
  37. #define APP_SIZE (80 * 1024) // 80KB
  38. #define PAGE_SIZE 2048 // 2KB
  39. #define PACKET_SIZE 256 // 一包数据256字节
  40. #define FLASH_PARAMS_MAGIC 0xA5A5
  41. #define FLASH_ADDR_MAX 0x08040000 //写入数据地址小于STM32L431CC的flash地址最大值
  42. #define SUCCESS 0
  43. #define ERROR 1
  44. /**************************************************************/
  45. /**************************************************************/
  46. /************** FLASH操作接口 *****************/
  47. uint32_t Flash_ReadOneWord(uint32_t faddr);
  48. void Flash_ErasePages(uint32_t faddr,uint32_t fdataNum);
  49. uint8_t Flash_WriteOneWord(uint32_t faddr,uint32_t fdata32);
  50. uint8_t Flash_WriteDoubleWord(uint32_t faddr,uint64_t fdata64);
  51. void Flash_Write32(uint32_t faddr,uint32_t *fdata32, uint32_t fdata32Num);
  52. void Flash_Write64(uint32_t faddr,uint64_t *fdata64, uint32_t fdata64Num);
  53. void Flash_Read32(uint32_t faddr,uint32_t *fdata32,uint32_t fdata32Num);
  54. void Flash_ReadBytes(uint32_t faddr,uint8_t *fdata8,uint32_t fdata8Num);
  55. void Flash_ReadBytesto32(uint32_t faddr, uint8_t *fdata8, uint32_t fdata8Num);
  56. uint8_t Flash_WriteRead(uint32_t fAdress, uint8_t *wData, uint32_t wDataLen, uint8_t *rData);
  57. void save_params_to_flash(void);
  58. void load_params_from_flash(void);
  59. void LoadBootloaderParams(void);
  60. void SaveBootloaderParams(void) ;
  61. void print_device_params(void);
  62. void PrintBootloaderParams(void);
  63. void Save_Calib_Coeffs_ToFlash(CalibCoeffs_t *coeffs);
  64. void Load_Calib_Coeffs_FromFlash(CalibCoeffs_t *coeffs);
  65. //void Save_CalibPoints_ToFlash(const CalibPoints_t *points);
  66. void Save_CalibPoints_ToFlash(void);
  67. void Load_CalibPoints_FromFlash(void);
  68. void Save_Calib_residualheight_ToFlash(void);
  69. void Save_Calib_rodvol_ToFlash(void) ;
  70. void print_calibPoints_params(void);
  71. #endif
  72. /**************************END OF FILE*************************/