FU68xx_5_Flash.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. ///**
  2. // * @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
  3. // * @file FU68xx_5_Flash.c
  4. // * @author leo.li
  5. // * @since 2022-10-01 22:24:50
  6. // * @date 2022-10-09 17:46:47
  7. // * @version
  8. // * <table>
  9. // * <tr><th>Date <th>Version <th>Author <th>Description </tr>
  10. // * </table>
  11. // * @brief
  12. // */
  13. ///******************************************************************************/// Including Header Files
  14. //#include <MyProject.h>
  15. //MCUFlash xdata FlashTest; //flash测试
  16. ///******************************************************************************/// Define Macro
  17. //#define SEC_LEN_LOG (8)
  18. //#define PAGE_LEN_LOG (3 + SEC_LEN_LOG)
  19. //#define FLASH_LEN (32768)
  20. //#define __SEC_LAST ((FLASH_LEN >> SEC_LEN_LOG) - 1)
  21. //#define __PAGE_LAST ((FLASH_LEN >> PAGE_LEN_LOG) - 1)
  22. ///******************************************************************************/// Define Global Symbols
  23. ///******************************************************************************/// Function Subject
  24. ///**
  25. // * @brief Flash操作函数
  26. // *
  27. // * @param[in] eCmd 操作命令
  28. // * @param[in] wAddr 编程地址
  29. // * @param[in] ucDat 编程内容
  30. // *
  31. // * @return Flash操作结果, @see ETypeFlashStatu
  32. // */
  33. //ETypeFlashStatu Flash_Ctrl(ETypeFlashCmd eCmd, unsigned short wAddr, unsigned char ucDat)
  34. //{
  35. // unsigned char data ucCmd = (unsigned char)eCmd;
  36. // unsigned short data wAddrTmp = wAddr;
  37. // unsigned char data ucDatTmp = ucDat;
  38. // unsigned char data ucKey1 = 0x5a;
  39. // unsigned char data ucKey2 = 0x1f;
  40. // bool bEA;
  41. // unsigned char data ucCfg;
  42. // ETypeFlashStatu data eStatu = FLASH_UNKNOW;
  43. //
  44. // if ((ucCmd & FLAEN) == 0)
  45. // {
  46. // return FLASH_ERR_CMD;
  47. // }
  48. // else if ((ucCmd & (FLAERS | FLAPRE)) == (FLAERS | FLAPRE))
  49. // {
  50. // return FLASH_ERR_CMD;
  51. // }
  52. // else if ((ucCmd & (FLAERS | FLAPRE | PAGE_EN)) == PAGE_EN)
  53. // {
  54. // return FLASH_ERR_CMD;
  55. // }
  56. //
  57. // if ((ucCmd & PAGE_EN) != 0 && (wAddr >> PAGE_LEN_LOG) >= __PAGE_LAST)
  58. // {
  59. // return FLASH_ERR_ADDR;
  60. // }
  61. // else if ((ucCmd & PAGE_EN) == 0 && (wAddr >> SEC_LEN_LOG) >= __SEC_LAST)
  62. // {
  63. // return FLASH_ERR_ADDR;
  64. // }
  65. //
  66. // if (FLA_KEY == 0x06)
  67. // {
  68. // return FLASH_FORZEN;
  69. // }
  70. //
  71. // bEA = EA;
  72. // EA = 0;
  73. // ucCfg = CCFG1 & (WDT_EN | 0x08);
  74. // CCFG1 &= ~(WDT_EN | 0x08);
  75. // FLA_CR = ucCmd;
  76. // FLA_KEY = ucKey1;
  77. // FLA_KEY = ucKey2;
  78. //
  79. // if (FLA_KEY != 0x03)
  80. // {
  81. // eStatu = FLASH_ERR_UNLOCK;
  82. // goto exit;
  83. // }
  84. //
  85. // *(unsigned char xdata *)wAddrTmp = ucDatTmp;
  86. // FLA_CR = 0x08;
  87. //
  88. // if ((FLA_CR & FLAERR) != 0)
  89. // {
  90. // eStatu = FLASH_ERROR;
  91. // goto exit;
  92. // }
  93. //
  94. // eStatu = FLASH_SUCCESS;
  95. //exit:
  96. // EA = bEA;
  97. // CCFG1 |= ucCfg;
  98. // return eStatu;
  99. //}
  100. ///**
  101. // * @brief Flash页擦除函数
  102. // *
  103. // * @param[in] wAddr 编程地址
  104. // *
  105. // * @return Flash操作结果, @see ETypeFlashStatu
  106. // */
  107. //ETypeFlashStatu Flash_PageErase(unsigned short wAddr)
  108. //{
  109. // ETypeFlashStatu eStatu;
  110. // eStatu = Flash_Ctrl(FLASH_PAGE_PP, wAddr, 0xff);
  111. //
  112. // if (eStatu != FLASH_SUCCESS)
  113. // {
  114. // goto exit;
  115. // }
  116. //
  117. // eStatu = Flash_Ctrl(FLASH_PAGE_ERS, wAddr, 0xff);
  118. //exit:
  119. // return eStatu;
  120. //}
  121. ///**
  122. // * @brief Flash扇区擦除函数
  123. // *
  124. // * @param[in] wAddr 编程地址
  125. // *
  126. // * @return Flash操作结果, @see ETypeFlashStatu
  127. // */
  128. //ETypeFlashStatu Flash_SectorErase(unsigned short wAddr)
  129. //{
  130. // ETypeFlashStatu eStatu;
  131. // eStatu = Flash_Ctrl(FLASH_SEC_PP, wAddr, 0xff);
  132. //
  133. // if (eStatu != FLASH_SUCCESS)
  134. // {
  135. // goto exit;
  136. // }
  137. //
  138. // eStatu = Flash_Ctrl(FLASH_SEC_ERS, wAddr, 0xff);
  139. //exit:
  140. // return eStatu;
  141. //}
  142. ///**
  143. // * @brief 写入1个字节到FLASH,
  144. // * @Input unsigned short FlashAddress:目标FLASH地址
  145. // * @Input uint8 FlashData:被写入数据
  146. // */
  147. //void Flash_byte_Write(unsigned short FlashAddress, unsigned char FlashData)
  148. //{
  149. // unsigned char Write_status = 0;
  150. // Write_status = (uint8)Flash_Ctrl(FLASH_PROGARM, FlashAddress, FlashData);
  151. // if(Write_status)
  152. // {
  153. // ;
  154. // }
  155. //}
  156. ///**
  157. // * @brief Flash 校验CRC
  158. // * @brief CRC_BEG: 扇区起始
  159. // * @brief CRC_CNT: 校验扇区数
  160. // */
  161. //void Flash_CRC_APP(void)
  162. //{
  163. // uint8 CRCL = 0;
  164. // uint8 CRCH = 0;
  165. // ClrBit(CRC_CR, AUTOINT); /* 停止计算 */
  166. // SetBit(CRC_CR, CRCVAL); /* 将 CRC 结果初始化为 0xFFFF */
  167. // SetBit(CRC_CR, CRCDINI); /* 初始化有效 */
  168. // CRC_BEG = 0;
  169. // CRC_CNT = 127;
  170. // SetBit(CRC_CR, AUTOINT); /* 开始计算 */
  171. //
  172. // while (ReadBit(CRC_CR, CRCDONE) == 0)
  173. // {
  174. // ;
  175. // }
  176. //
  177. // SetBit(CRC_CR, CRCPNT); /* 取crc高位 */
  178. // CRCH = CRC_DR;
  179. // FlashTest.FlashCRC = CRCH;
  180. // FlashTest.FlashCRC = FlashTest.FlashCRC << 8;
  181. // ClrBit(CRC_CR, CRCPNT); /* 取crc低位 */
  182. // CRCL = CRC_DR;
  183. // FlashTest.FlashCRC = FlashTest.FlashCRC + CRCL;
  184. //}
  185. ///**
  186. //* @brief 获取写入地址,必须保证写入的地址中数值全部为0
  187. //* @brief unsigned short FlashAddress:目标FLASH首地址
  188. //* @brief uint8 Write_Length:写入数值长度
  189. //*/
  190. //uint16 GetWrite_Black_Addr(unsigned short FlashAddress, uint8 Write_Length)
  191. //{
  192. // uint16 TempFlashAddress = 0;
  193. // uint16 TempFirstAddress = 0; //用来保存保存首地址
  194. // uint8 tempFlashData = 0xFF;
  195. // uint8 Length = 0;
  196. // TempFlashAddress = FlashAddress;
  197. // TempFirstAddress = FlashAddress;
  198. //
  199. // while ((Length != Write_Length) && (TempFlashAddress < (TempFirstAddress + Write_Length))) //限定读取地址为当前页
  200. // {
  201. // tempFlashData = *(uint8 code *)(TempFlashAddress); //读取目标地址中数值是否为0
  202. //
  203. // if (tempFlashData == 0)
  204. // {
  205. // Length ++;
  206. // TempFlashAddress += 1;
  207. // }
  208. // else
  209. // {
  210. // TempFirstAddress += Write_Length; //首地址跳转Write_Length字节
  211. // TempFlashAddress = TempFirstAddress;
  212. // Length = 0;
  213. // }
  214. // }
  215. //
  216. // return TempFirstAddress;
  217. //}
  218. ///**
  219. // * @brief 写入Length字节到FLASH,
  220. // * @Input unsigned short FlashAddress:写入目标FLASH首地址
  221. // * @Input uint8 *str:被写入数据
  222. // * @ruturn 1:扇区未满,写入完成 0:扇区已满,写入失败
  223. // * @note
  224. // */
  225. //uint8 Write_LengthByte(unsigned short FlashAddress, uint8 Length, uint8 * str)
  226. //{
  227. // uint8 tempFlashData = 0;
  228. // uint8 i = 0;
  229. // uint16 tempNewFlashAddress = FlashAddress;
  230. //
  231. // for (i = 0; i < Length; i++)
  232. // {
  233. // Flash_byte_Write((tempNewFlashAddress + i), str[i]);
  234. // tempFlashData = *(uint8 code *)(tempNewFlashAddress + i);
  235. //
  236. // if (tempFlashData != str[i])
  237. // {
  238. // return 0;
  239. // }
  240. // }
  241. //
  242. // return 1;
  243. //}
  244. ///**
  245. // * @brief 读取两个字节数据,
  246. // * @Input unsigned short BlockStartAddr:目标FLASH首地址
  247. // * @Input uint8 Length:数值长度
  248. // * @Input uint8 * str :读回数值写入数组
  249. // * @ruturn Flash数据
  250. // */
  251. //void ReadFromFlash(unsigned short BlockStartAddr, uint8 Length, uint8 * str)
  252. //{
  253. // uint8 i = 0;
  254. //
  255. // for (i = 0; i < Length; i++)
  256. // {
  257. // str[i] = *(uint8 code *)(BlockStartAddr + i);
  258. // }
  259. //}
  260. ///**
  261. //* @brief 获取掉电之前的错误数据,数值初始化
  262. //* @brief
  263. //* @brief
  264. //*/
  265. //void Flash_DataInit(void)
  266. //{
  267. // uint16 ReadInfor_Addr = 0; //读取信息地址
  268. // uint8 Check_code = 0;
  269. // uint8 i = 0;
  270. // memset(&FlashTest, 0, sizeof(MCUFlash)); // 变量清零
  271. // /*确认写入对应长度地址内数值全为0 */
  272. // FlashTest.FlashWriteAddr = GetWrite_Black_Addr(USERCODEADDRESS, Flash_DATA_Length);
  273. //
  274. // /* 获取上次的保存数值
  275. // FlashTest.FlashWriteAddr:写入数值首地址 ,Flash_DATA_Length:读取字节数,读取值放入FlashTest.Flash_Read_Data数组中 */
  276. // if (FlashTest.FlashWriteAddr > USERCODEADDRESS)
  277. // {
  278. // /*读取数值首地址*/
  279. // FlashTest.FlashReadAddr = FlashTest.FlashWriteAddr - Flash_DATA_Length;
  280. //
  281. // /*获取最新一次正确写入的数值*/
  282. // while ((FlashTest.FlashReadAddr > USERCODEADDRESS) && (Check_code != 0x55))
  283. // {
  284. // /*读取数值校验位,确认当前数值在写入时为正确写入*/
  285. // ReadInfor_Addr = FlashTest.FlashReadAddr + Verify_Bit;
  286. // Check_code = *(uint8 code *)(ReadInfor_Addr + i);
  287. //
  288. // if (Check_code == 0x55)
  289. // {
  290. // ReadFromFlash(FlashTest.FlashReadAddr, Flash_DATA_Length, FlashTest.Flash_Read_Data);
  291. // }
  292. // else
  293. // {
  294. // FlashTest.FlashReadAddr = FlashTest.FlashWriteAddr - Flash_DATA_Length;
  295. // }
  296. // }
  297. // }
  298. // else
  299. // {
  300. // FlashTest.FlashReadAddr = FlashTest.FlashWriteAddr;
  301. // }
  302. //
  303. // /*错误信息从Flash读取到APP*/
  304. // ReadInfor_Addr = FlashTest.FlashReadAddr + ((uint8)FaultHardOVCurrent - 1) * 2;
  305. // FlashTest.FaultHardOVCurrentCount = *(uint16 code *)(ReadInfor_Addr);
  306. // ReadInfor_Addr = FlashTest.FlashReadAddr + ((uint8)FaultSoftOVCurrent - 1) * 2;
  307. // FlashTest.FaultSoftOVCurrentCount = *(uint16 code *)(ReadInfor_Addr);
  308. // ReadInfor_Addr = FlashTest.FlashReadAddr + ((uint8)FaultOverVoltageDC - 1) * 2;
  309. // FlashTest.FaultUnderVoltageCount = *(uint16 code *)(ReadInfor_Addr);
  310. // ReadInfor_Addr = FlashTest.FlashReadAddr + ((uint8)FaultUnderVoltageDC - 1) * 2;
  311. // FlashTest.FaultOverVoltageCount = *(uint16 code *)(ReadInfor_Addr);
  312. // ReadInfor_Addr = FlashTest.FlashReadAddr + ((uint8)FaultPhaseLost - 1) * 2;
  313. // FlashTest.FaultLossPhaseCount = *(uint16 code *)(ReadInfor_Addr);
  314. // ReadInfor_Addr = FlashTest.FlashReadAddr + ((uint8)FaultStall - 1) * 2;
  315. // FlashTest.FaultStartCount = *(uint16 code *)(ReadInfor_Addr);
  316. // ReadInfor_Addr = FlashTest.FlashReadAddr + ((uint8)FaultNtcOTErr - 1) * 2;
  317. // FlashTest.FaultStartCount = *(uint16 code *)(ReadInfor_Addr);
  318. // ReadInfor_Addr = FlashTest.FlashReadAddr + ((uint8)FaultTSD - 1) * 2;
  319. // FlashTest.FaultTSDCount = *(uint16 code *)(ReadInfor_Addr);
  320. //
  321. // /*Flash如果准备写满,擦写标志位置1.准备芯片温度在低于105°进行擦写*/
  322. // if (FlashTest.FlashWriteAddr >= USERCODEADDRESS + 0x0FB0) //擦写为2页(一共4K的数值)到最后一个写入64字节尝试擦写
  323. // {
  324. // for (i = 0; i < Flash_DATA_Length; i++)
  325. // {
  326. // FlashTest.Flash_Write_Data[i] = FlashTest.Flash_Read_Data[i];
  327. // }
  328. //
  329. // FlashTest.Flag_FlashErase = 1;
  330. // }
  331. //}
  332. ///**
  333. //* @brief Flash的擦写
  334. //* @brief FlashTest.Flag_FlashErase确认是否进行
  335. //* @brief
  336. //*/
  337. //void Flash_Erase(void)
  338. //{
  339. // uint8 FlashWriteStatus = 0;
  340. //
  341. // if (((FlashTest.Flag_FlashErase == 1) && (TSDTemperature.ActualTemper < 105) && (mcFocCtrl.mcDcbusFlt > UNDER_VOLTAGE_PROTECT))
  342. // || ((FlashTest.Flag_FlashErase == 2) && (mcFocCtrl.mcDcbusFlt > UNDER_VOLTAGE_PROTECT)))
  343. // {
  344. // FlashTest.Flag_FlashErase = 0;
  345. // FlashTest.ReturnEraseStatus = (uint8)Flash_PageErase(USERCODEADDRESS); // 目标页写满,清除目标页
  346. // FlashTest.FlashWriteAddr = USERCODEADDRESS; // 重新给写入首地址
  347. //
  348. // while (!FlashWriteStatus) // 初始写入或者写入异常、换到下个地址写入
  349. // {
  350. // /* FlashTest.FlashWriteAddr:指定地址 FlashTest.Flash_Write_Data:需要写入的数值
  351. // FlashWriteStatus:返回数值 1:写入正确 0:写入错误 */
  352. // FlashWriteStatus = Write_LengthByte(FlashTest.FlashWriteAddr, Flash_DATA_Length, FlashTest.Flash_Write_Data);
  353. //
  354. // /* 写入正确时最后一位写入校验位 */
  355. // if (FlashWriteStatus == 0) //当前数值写入错误
  356. // {
  357. // Flash_byte_Write((FlashTest.FlashWriteAddr + Verify_Bit), Verify_Error);
  358. // }
  359. // else if (FlashWriteStatus) //当前数值写入正确
  360. // {
  361. // Flash_byte_Write((FlashTest.FlashWriteAddr + Verify_Bit), Verify_Ture);
  362. // }
  363. //
  364. // //更新读写首地址
  365. // FlashTest.FlashReadAddr = FlashTest.FlashWriteAddr;
  366. // FlashTest.FlashWriteAddr = FlashTest.FlashWriteAddr + Flash_DATA_Length;
  367. //
  368. // if (FlashTest.FlashWriteAddr >= USERCODEADDRESS + 0x0800)
  369. // {
  370. // FlashWriteStatus = 1; //全部页损坏,强行跳转
  371. // }
  372. // }
  373. //
  374. // FlashTest.ReturnEraseStatus = (uint8)Flash_PageErase(USERCODEADDRESS + 0x0800); //目标页写满,清除目标页
  375. // }
  376. //}
  377. ///**
  378. //* @brief 保存错误
  379. //* @brief
  380. //* @brief
  381. //*/
  382. //void SaveFault_Flash(void)
  383. //{
  384. // uint16 Array_Loca = 0;
  385. // uint8 FlashWriteStatus = 0;
  386. //
  387. // if (FlashTest.Flash_FaultSource_Updat)
  388. // {
  389. // FlashTest.Flash_FaultSource_Updat = 0;
  390. //
  391. // switch (mcFaultSource)
  392. // {
  393. // case FaultHardOVCurrent:
  394. // FlashTest.FaultHardOVCurrentCount++;
  395. // Array_Loca = ((uint8)FaultHardOVCurrent - 1) * 2;
  396. // FlashTest.Flash_Write_Data[Array_Loca] = FlashTest.FaultHardOVCurrentCount >> 8;
  397. // FlashTest.Flash_Write_Data[Array_Loca + 1] = FlashTest.FaultHardOVCurrentCount;
  398. // break;
  399. //
  400. // case FaultSoftOVCurrent:
  401. // FlashTest.FaultSoftOVCurrentCount++;
  402. // Array_Loca = ((uint8)FaultSoftOVCurrent - 1) * 2;
  403. // FlashTest.Flash_Write_Data[Array_Loca] = FlashTest.FaultSoftOVCurrentCount >> 8;
  404. // FlashTest.Flash_Write_Data[Array_Loca + 1] = FlashTest.FaultSoftOVCurrentCount;
  405. // break;
  406. //
  407. // case FaultOverVoltageDC:
  408. // FlashTest.FaultUnderVoltageCount++;
  409. // Array_Loca = ((uint8)FaultOverVoltageDC - 1) * 2;
  410. // FlashTest.Flash_Write_Data[Array_Loca] = FlashTest.FaultUnderVoltageCount >> 8;
  411. // FlashTest.Flash_Write_Data[Array_Loca + 1] = FlashTest.FaultUnderVoltageCount;
  412. // break;
  413. //
  414. // case FaultUnderVoltageDC:
  415. // FlashTest.FaultOverVoltageCount++;
  416. // Array_Loca = ((uint8)FaultUnderVoltageDC - 1) * 2;
  417. // FlashTest.Flash_Write_Data[Array_Loca] = FlashTest.FaultOverVoltageCount >> 8;
  418. // FlashTest.Flash_Write_Data[Array_Loca + 1] = FlashTest.FaultOverVoltageCount;
  419. // break;
  420. //
  421. // case FaultPhaseLost:
  422. // FlashTest.FaultLossPhaseCount++;
  423. // Array_Loca = ((uint8)FaultPhaseLost - 1) * 2;
  424. // FlashTest.Flash_Write_Data[Array_Loca] = FlashTest.FaultLossPhaseCount >> 8;
  425. // FlashTest.Flash_Write_Data[Array_Loca + 1] = FlashTest.FaultLossPhaseCount;
  426. // break;
  427. //
  428. // case FaultStall:
  429. // FlashTest.FaultStallCount++;
  430. // Array_Loca = ((uint8)FaultStall - 1) * 2;
  431. // FlashTest.Flash_Write_Data[Array_Loca] = FlashTest.FaultStallCount >> 8;
  432. // FlashTest.Flash_Write_Data[Array_Loca + 1] = FlashTest.FaultStallCount;
  433. // break;
  434. //
  435. // case FaultNtcOTErr:
  436. // FlashTest.FaultStartCount++;
  437. // Array_Loca = ((uint8)FaultNtcOTErr - 1) * 2;
  438. // FlashTest.Flash_Write_Data[Array_Loca] = FlashTest.FaultStartCount >> 8;
  439. // FlashTest.Flash_Write_Data[Array_Loca + 1] = FlashTest.FaultStartCount;
  440. // break;
  441. //
  442. // case FaultTSD:
  443. // FlashTest.FaultTSDCount++;
  444. // Array_Loca = ((uint8)FaultTSD - 1) * 2;
  445. // FlashTest.Flash_Write_Data[Array_Loca] = FlashTest.FaultTSDCount >> 8;
  446. // FlashTest.Flash_Write_Data[Array_Loca + 1] = FlashTest.FaultTSDCount;
  447. // break;
  448. //
  449. // default:
  450. // break;
  451. // }
  452. //
  453. // FlashTest.ReturnEraseStatus = 0;
  454. //
  455. // while (!FlashWriteStatus) //写入异常,换到下个地址写入
  456. // {
  457. // /* FlashTest.FlashWriteAddr:指定地址 FlashTest.Flash_Write_Data:需要写入的数值
  458. // FlashWriteStatus:返回数值 1:写入正确 0:写入错误 */
  459. // FlashWriteStatus = Write_LengthByte(FlashTest.FlashWriteAddr, Flash_DATA_Length, FlashTest.Flash_Write_Data);
  460. //
  461. // /* 写入正确时最后一位写入校验位 */
  462. // if (FlashWriteStatus == 0) //当前数值写入错误
  463. // {
  464. // Flash_byte_Write((FlashTest.FlashWriteAddr + Verify_Bit), Verify_Error);
  465. // }
  466. // else //当前数值写入正确
  467. // {
  468. // Flash_byte_Write((FlashTest.FlashWriteAddr + Verify_Bit), Verify_Ture);
  469. // }
  470. //
  471. // //更新读写首地址
  472. // FlashTest.FlashReadAddr = FlashTest.FlashWriteAddr;
  473. // FlashTest.FlashWriteAddr = FlashTest.FlashWriteAddr + Flash_DATA_Length;
  474. //
  475. // /*写到相应的空间,不管之前写入是否准确,直接进行相关的擦除
  476. // 擦除后会当即写入Flash*/
  477. // if (FlashTest.FlashWriteAddr >= USERCODEADDRESS + 0x0FD0) //&& (FlashWriteStatus == 0))
  478. // {
  479. // FlashTest.Flag_FlashErase = 1; // 置擦除标志位
  480. // FlashWriteStatus = 1; // 没有空间,强行跳转
  481. // }
  482. // }
  483. // }
  484. //}
  485. /* -------------------------- (C) COPYRIGHT 2022 Fortiortech ShenZhen ---------------------------*/
  486. /**
  487. * @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
  488. * @file
  489. * @author Fortiortech Appliction Team
  490. * @since Create:2022-07-14
  491. * @date Last modify:2022-07-14
  492. * @note Last modify author is Leo.li
  493. * @brief
  494. */
  495. /* Includes ------------------------------------------------------------------*/
  496. #include <FU68xx_5.h>
  497. #include <Myproject.h>
  498. ROM_TypeDef xdata FlashData;
  499. uint8 Flash_GetAddress(void);
  500. /**
  501. * @brief 读取固定地址值
  502. * @date 2022-07-14
  503. */
  504. uint8 ReadFlashValue(uint16 ReadAddress)
  505. {
  506. uint16 TempAddress0 = ReadAddress;
  507. __IO uint8 tevalue = 0; //临时变量
  508. tevalue = *(uint8 code *)TempAddress0; //读取回地址的值
  509. return tevalue;
  510. }
  511. /**
  512. * @brief 擦除指定扇区
  513. * @brief FlashAddress--Flash自擦除扇区内任意地址
  514. * @return 0--Flash自擦除成功,1--Flash自擦除失败
  515. * @date 2022-07-14
  516. */
  517. uint8 Flash_Sector_Erase(uint8 xdata * FlashAddress)
  518. {
  519. bool TempEA;
  520. uint16 TempFlashAddress;
  521. TempEA = EA;
  522. EA = 0;
  523. TempFlashAddress = (uint16)FlashAddress;
  524. if ((TempFlashAddress) < (0x7f00)) // 不擦除最后一个扇区
  525. {
  526. FLA_CR = 0x03; //使能自擦除
  527. FLA_KEY = 0x5a;
  528. FLA_KEY = 0x1f; //flash预编程解锁
  529. _nop_();
  530. *FlashAddress = 0xff; //写任意数据
  531. FLA_CR = 0x08; //开始预编程,完成后Flash再次上锁
  532. }
  533. EA = TempEA;
  534. if (ReadBit(FLA_CR, FLAERR))
  535. {
  536. return 1;
  537. }
  538. else
  539. {
  540. return 0;
  541. }
  542. }
  543. /**
  544. * @brief 擦除指定扇区
  545. * @brief FlashAddress--指定写入地址
  546. * @brief FlashData--写入数值
  547. * @return 0--Flash自擦除成功,1--Flash自擦除失败
  548. * @date 2022-07-14
  549. */
  550. void Flash_Sector_Write(uint8 xdata * FlashAddress, uint8 Flash_Data)
  551. {
  552. bool TempEA = 0;
  553. uint16 TempFlashAddress;
  554. uint8 idata Flash_Data_t = Flash_Data;
  555. TempEA = EA;
  556. EA = 0;
  557. TempFlashAddress = (uint16)FlashAddress;
  558. if ((TempFlashAddress) < 0x7f00) // 不编程最后一个扇区
  559. {
  560. FLA_CR = 0x01; // 使能Flash编程
  561. FLA_KEY = 0x5a;
  562. FLA_KEY = 0x1f; // flash预编程解锁
  563. _nop_();
  564. *(uint8 xdata *)FlashAddress = Flash_Data_t; // 写编程数据
  565. FLA_CR = 0x08; // 开始预编程,完成后Flash再次上锁
  566. }
  567. EA = TempEA;
  568. }
  569. /**
  570. * @brief WriteAddress--指定写入地址
  571. * @brief Length:写入数组长度
  572. * @brief value--写入数组
  573. * @date 2022-11-14
  574. */
  575. uint8 Flash_WriteValue(uint16 WriteAddress, uint8 Length, uint8 * str)
  576. {
  577. uint8 i;
  578. uint8 TempReadRomValue;
  579. uint16 FlashWriteAddr = WriteAddress;
  580. //读出有效数据
  581. for (i = 0; i < Length; i++)
  582. {
  583. Flash_Sector_Write(FlashWriteAddr + i, str[i]);
  584. TempReadRomValue = *(uint8 code *)(FlashWriteAddr + i);
  585. if (TempReadRomValue != str[i])
  586. {
  587. return 0;
  588. }
  589. }
  590. return 1;
  591. }
  592. /**
  593. * @brief 获取最后写入的地址,确认最新能写入的地址
  594. * @brief unsigned short FlashAddress:目标FLASH首地址
  595. * @brief uint8 Write_Length:写入数值长度
  596. */
  597. uint16 GetWrite_Black_Addr(unsigned short FlashAddress, uint8 Write_Length)
  598. {
  599. uint16 TempFlashAddress = 0;
  600. uint16 TempFirstAddress = 0; //用来保存保存首地址
  601. uint8 tempFlashData = 0xFF;
  602. uint8 Length = 0;
  603. TempFlashAddress = FlashAddress;
  604. TempFirstAddress = FlashAddress;
  605. while ((Length != Write_Length) && (TempFlashAddress < (TempFirstAddress + Write_Length))) //限定读取地址为当前页
  606. {
  607. tempFlashData = *(uint8 code *)(TempFlashAddress); //读取目标地址中数值是否为0
  608. if (tempFlashData == 0)
  609. {
  610. Length ++;
  611. TempFlashAddress += 1;
  612. }
  613. else
  614. {
  615. TempFirstAddress += Write_Length; //首地址跳转Write_Length字节
  616. if (TempFirstAddress > 0x7f00)
  617. {
  618. TempFirstAddress = 0x7f00;
  619. }
  620. TempFlashAddress = TempFirstAddress;
  621. Length = 0;
  622. }
  623. }
  624. return TempFirstAddress;
  625. }
  626. /**
  627. * @brief 读取Length字节数据,
  628. * @Input unsigned short BlockStartAddr:目标FLASH首地址
  629. * @Input uint8 Length:数值长度
  630. * @Input uint8 * str :读回数值写入数组
  631. * @ruturn Flash数据
  632. */
  633. void ReadFromFlash(unsigned short BlockStartAddr, uint8 Length, uint8 * str)
  634. {
  635. uint8 i = 0;
  636. for (i = 0; i < Length; i++)
  637. {
  638. str[i] = *(uint8 code *)(BlockStartAddr + i);
  639. }
  640. }
  641. /**
  642. * @brief 扇区擦除
  643. * @Input
  644. * @ruturn
  645. */
  646. void Flash_Erase(void)
  647. {
  648. uint8 FlashEraseStatus = 0;
  649. FlashEraseStatus = Flash_Sector_Erase(START_WriteADDRESS);
  650. if (!FlashEraseStatus)
  651. {
  652. /*----- 获取写入首地址 -----*/
  653. FlashData.WriteAddress = GetWrite_Black_Addr(START_WriteADDRESS, Write_Lenght);
  654. }
  655. else
  656. {
  657. FlashEraseStatus = Flash_Sector_Erase(START_WriteADDRESS);
  658. FlashData.WriteAddress = START_WriteADDRESS; //第一次擦写失败后第二次擦写,强行给地址
  659. /*----- 第二次擦写再失败,默认为Flash损坏,不进行处理 -----*/
  660. if (FlashEraseStatus)
  661. {
  662. ;
  663. }
  664. }
  665. FlashData.Flag_FlashErase = 0;
  666. }
  667. /**
  668. * @brief 保存按键
  669. * @date 2022-11-14
  670. */
  671. void Save_KeyValue(void)
  672. {
  673. uint8 FlashWriteStatus = 0; //返回值
  674. uint8 TempReadValue = 0;
  675. FlashData.WriteValue[0] = KS.KeyValuetotal;
  676. if (FlashData.ReadValue[0] != FlashData.WriteValue[0]) //确认写入前后数据不一致
  677. {
  678. EA = 0;
  679. while (!FlashWriteStatus) //写入异常,换到下个地址写入
  680. {
  681. FlashWriteStatus = Flash_WriteValue(FlashData.WriteAddress, Write_Lenght, FlashData.WriteValue);
  682. if (FlashWriteStatus)
  683. {
  684. Flash_Sector_Write(FlashData.WriteAddress + Verify_Bit, Verify_Ture);
  685. TempReadValue = *(uint8 code *)(FlashData.WriteAddress + Verify_Bit);
  686. /*----- 更新读取Flash首地址 -----*/
  687. FlashData.ReadAddress = FlashData.WriteAddress - Write_Lenght;
  688. /*----- 读取数值到FlashData.ReadValue -----*/
  689. ReadFromFlash(FlashData.ReadAddress, Write_Lenght, FlashData.ReadValue);
  690. if (TempReadValue == Verify_Ture)
  691. {
  692. goto exit;
  693. }
  694. else
  695. {
  696. FlashWriteStatus = 0;
  697. }
  698. }
  699. else
  700. {
  701. Flash_Sector_Write(FlashData.WriteAddress + Verify_Bit, Verify_Error);
  702. FlashData.WriteAddress = FlashData.WriteAddress + Write_Lenght;
  703. if (FlashData.WriteAddress > START_WriteADDRESS + 0x70)
  704. {
  705. FlashData.Flag_FlashErase = 1; //写到扇区最后字节,依然失败,直接进行数值清楚
  706. goto exit;
  707. }
  708. }
  709. }
  710. exit:
  711. EA = 1;
  712. }
  713. }