flash_master.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /************************************************************************
  2. Project: Welling Motor Control Paltform
  3. Filename: flash_master.c
  4. Partner Filename: flash_master.h
  5. Description: flash master driver
  6. Complier: IAR Embedded Workbench for ARM 8.40.2
  7. CPU TYPE : STM32F30x
  8. *************************************************************************
  9. Copyright (c) 20 Welling Motor Technology(Shanghai) Co. Ltd.
  10. All rights reserved.
  11. *************************************************************************
  12. *************************************************************************
  13. Revising History (ECL of this file):
  14. M0_20220410, by Ling Baizhou, create this file;
  15. ************************************************************************/
  16. /************************************************************************
  17. Beginning of File, do not put anything above here except notes
  18. Compiler Directives:
  19. *************************************************************************/
  20. #ifndef _FLASH_MASTER_C
  21. #define _FLASH_MASTER_C_
  22. #endif
  23. #ifdef RUN_ARCH_SIM
  24. #include "flash_master.h"
  25. FLASH_PARA_TYPE flash_stPara;
  26. void flash_voWrite(void)
  27. {}
  28. void flash_voRead(void)
  29. {}
  30. void flash_voParaInit(void)
  31. {}
  32. void flash_voErrorWrite(void)
  33. {}
  34. void flash_voErrorRead(void)
  35. {}
  36. void FLASH_voWriteMoreData(ULONG startAddress, const UWORD writeData[], UWORD countToWrite)
  37. {}
  38. void FLASH_voReadMoreData(ULONG startAddress, UWORD readData[], UWORD countToRead)
  39. {}
  40. #else
  41. /************************************************************************
  42. Included File
  43. *************************************************************************/
  44. #include "flash_master.h"
  45. #include "gd32f30x.h"
  46. #include "queue.h"
  47. #include "string.h"
  48. #include "AssistCurve.h"
  49. /*************************************************************************
  50. Exported Functions (N/A)
  51. *************************************************************************/
  52. /*************************************************************************
  53. Function:
  54. Description:
  55. Call by:
  56. Input Variables:
  57. Output/Return Variables:
  58. Subroutine Call:
  59. Reference:
  60. *************************************************************************/
  61. FLASH_PARA_TYPE flash_stPara;
  62. //从指定地址开始写入多个数据
  63. void FLASH_voWriteMoreData(ULONG startAddress, const UWORD writeData[], UWORD countToWrite)
  64. {
  65. UWORD dataIndex;
  66. if (startAddress < FLASH_BASE || ((startAddress + countToWrite * 2) >= (FLASH_END)))
  67. {
  68. return; //非法地址
  69. }
  70. fmc_unlock(); //解锁写保护
  71. while (fmc_page_erase((uint32_t)startAddress) != FMC_READY)
  72. {
  73. ;
  74. } //擦除这个扇区
  75. for (dataIndex = 0; dataIndex < countToWrite; dataIndex++)
  76. {
  77. fmc_halfword_program((uint32_t)startAddress + dataIndex * 2, (uint16_t)writeData[dataIndex]);
  78. }
  79. fmc_lock();//上锁写保护
  80. }
  81. //读取指定地址的半字(16位数据)
  82. static UWORD FLASH_ReadWord(ULONG address)
  83. {
  84. return *(__IO UWORD *)address;
  85. }
  86. //从指定地址开始读取多个数据
  87. void FLASH_voReadMoreData(ULONG startAddress, UWORD readData[], UWORD countToRead)
  88. {
  89. UWORD dataIndex;
  90. for (dataIndex = 0; dataIndex < countToRead; dataIndex++)
  91. {
  92. readData[dataIndex] = FLASH_ReadWord(startAddress + dataIndex * 2);
  93. }
  94. }
  95. void flash_voWrite(void)
  96. {
  97. UWORD count_len = sizeof(FLASH_PARA_TYPE) / 2;
  98. FLASH_voWriteMoreData(StartServerManageFlashAddress, (UWORD *)(&flash_stPara), count_len);
  99. }
  100. void flash_voRead(void)
  101. {
  102. UWORD count_len = sizeof(FLASH_PARA_TYPE) / 2;
  103. FLASH_voReadMoreData(StartServerManageFlashAddress, (UWORD *)(&flash_stPara), count_len);
  104. }
  105. void flash_voParaInit(void)
  106. {
  107. memset(&flash_stPara, 0, sizeof(FLASH_PARA_TYPE));
  108. SLONG Flash_TorqueAssGain[15][4] = TORQUE_ASSIST_DEFAULT;
  109. SLONG Flash_CadAssGain[5][4] = CADENCE_ASSIST_DEFAULT;
  110. memcpy(&flash_stPara.slTorqAssGain[0], &Flash_TorqueAssGain[0], sizeof(Flash_TorqueAssGain));
  111. memcpy(&flash_stPara.slCadAssGain[0], &Flash_CadAssGain[0], sizeof(Flash_CadAssGain));
  112. flash_voWrite();
  113. }
  114. void flash_voErrorWrite(void)
  115. {
  116. UWORD count_len = sizeof(ERROR_LOG) * QUEUE_SIZE/ 2;
  117. que_stFlashErrorLog.stLog[0].ubHead = que_stFlashErrorLog.ubHead;
  118. que_stFlashErrorLog.stLog[0].ubRear = que_stFlashErrorLog.ubRear;
  119. FLASH_voWriteMoreData(StartServerManageErrorFlashAddress, (UWORD *)(&que_stFlashErrorLog.stLog), count_len);
  120. }
  121. void flash_voErrorRead(void)
  122. {
  123. UWORD count_len = sizeof(ERROR_LOG) * QUEUE_SIZE/ 2;
  124. FLASH_voReadMoreData(StartServerManageErrorFlashAddress, (UWORD *)(&que_stFlashErrorLog.stLog), count_len);
  125. que_stFlashErrorLog.ubHead = que_stFlashErrorLog.stLog[0].ubHead;
  126. que_stFlashErrorLog.ubRear = que_stFlashErrorLog.stLog[0].ubRear;
  127. if(que_stFlashErrorLog.ubHead == 0xFF)
  128. {
  129. que_voInit(&que_stFlashErrorLog);
  130. }
  131. }
  132. void flash_voErrorClear(void)
  133. {
  134. fmc_unlock(); //解锁写保护
  135. while (fmc_page_erase((uint32_t)StartServerManageErrorFlashAddress) != FMC_READY)
  136. {
  137. ;
  138. } //擦除这个扇区
  139. fmc_lock();//上锁写保护
  140. //que_voInit(&que_stFlashErrorLog);
  141. }
  142. /************************************************************************
  143. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co., Ltd.
  144. All rights reserved.
  145. *************************************************************************/
  146. #ifdef _FLASH_MASTER_C
  147. #undef _FLASH_MASTER_C_
  148. #endif
  149. #endif
  150. /*************************************************************************
  151. End of this File (EOF)
  152. Do not put anything after this part!
  153. *************************************************************************/