flash_master.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. void flash_voWrite(void)
  26. {}
  27. void flash_voRead(void)
  28. {}
  29. void flash_voParaInit(void)
  30. {}
  31. void flash_voErrorWrite(void)
  32. {}
  33. void flash_voErrorRead(void)
  34. {}
  35. void FLASH_voWriteMoreData(ULONG startAddress, const UWORD writeData[], UWORD countToWrite)
  36. {}
  37. void FLASH_voReadMoreData(ULONG startAddress, UWORD readData[], UWORD countToRead)
  38. {}
  39. #else
  40. /************************************************************************
  41. Included File
  42. *************************************************************************/
  43. #include "flash_master.h"
  44. #include "gd32f30x.h"
  45. #include "queue.h"
  46. #include "string.h"
  47. #include "AssistCurve.h"
  48. /*************************************************************************
  49. Exported Functions (N/A)
  50. *************************************************************************/
  51. /*************************************************************************
  52. Function:
  53. Description:
  54. Call by:
  55. Input Variables:
  56. Output/Return Variables:
  57. Subroutine Call:
  58. Reference:
  59. *************************************************************************/
  60. FLASH_PARA_TYPE flash_stPara;
  61. //从指定地址开始写入多个数据
  62. void FLASH_voWriteMoreData(ULONG startAddress, const UWORD writeData[], UWORD countToWrite)
  63. {
  64. UWORD dataIndex;
  65. if (startAddress < FLASH_BASE || ((startAddress + countToWrite * 2) >= (FLASH_END)))
  66. {
  67. return; //非法地址
  68. }
  69. fmc_unlock(); //解锁写保护
  70. while (fmc_page_erase((uint32_t)startAddress) != FMC_READY)
  71. {
  72. ;
  73. } //擦除这个扇区
  74. for (dataIndex = 0; dataIndex < countToWrite; dataIndex++)
  75. {
  76. fmc_halfword_program((uint32_t)startAddress + dataIndex * 2, (uint16_t)writeData[dataIndex]);
  77. }
  78. fmc_lock();//上锁写保护
  79. }
  80. //读取指定地址的半字(16位数据)
  81. static UWORD FLASH_ReadWord(ULONG address)
  82. {
  83. return *(__IO UWORD *)address;
  84. }
  85. //从指定地址开始读取多个数据
  86. void FLASH_voReadMoreData(ULONG startAddress, UWORD readData[], UWORD countToRead)
  87. {
  88. UWORD dataIndex;
  89. for (dataIndex = 0; dataIndex < sizeof(FLASH_PARA_TYPE) / 2; dataIndex++)
  90. {
  91. readData[dataIndex] = FLASH_ReadWord(startAddress + dataIndex * 2);
  92. }
  93. }
  94. void flash_voWrite(void)
  95. {
  96. UWORD count_len = sizeof(FLASH_PARA_TYPE) / 2;
  97. FLASH_voWriteMoreData(StartServerManageFlashAddress, (UWORD *)(&flash_stPara), count_len);
  98. }
  99. void flash_voRead(void)
  100. {
  101. UWORD count_len = sizeof(FLASH_PARA_TYPE) / 2;
  102. FLASH_voReadMoreData(StartServerManageFlashAddress, (UWORD *)(&flash_stPara), count_len);
  103. }
  104. void flash_voParaInit(void)
  105. {
  106. memset(&flash_stPara, 0, sizeof(FLASH_PARA_TYPE));
  107. SLONG Flash_TorqueAssGain[15][4] = TORQUE_ASSIST_DEFAULT;
  108. SLONG Flash_CadAssGain[5][4] = CADENCE_ASSIST_DEFAULT;
  109. memcpy(&flash_stPara.slTorqAssGain[0], &Flash_TorqueAssGain[0], sizeof(Flash_TorqueAssGain));
  110. memcpy(&flash_stPara.slCadAssGain[0], &Flash_CadAssGain[0], sizeof(Flash_CadAssGain));
  111. flash_voWrite();
  112. }
  113. void flash_voErrorWrite(void)
  114. {
  115. UWORD count_len = sizeof(FLASH_ERROR_QUEUE) / 2;
  116. FLASH_voWriteMoreData(StartServerManageErrorFlashAddress, (UWORD *)(&que_stFlashErrorLog), count_len);
  117. }
  118. void flash_voErrorRead(void)
  119. {
  120. UWORD count_len = sizeof(FLASH_ERROR_QUEUE) / 2;
  121. FLASH_voReadMoreData(StartServerManageErrorFlashAddress, (UWORD *)(&que_stFlashErrorLog), count_len);
  122. if(que_stFlashErrorLog.ubHead == 0xFF)
  123. {
  124. que_voInit(&que_stFlashErrorLog);
  125. }
  126. }
  127. /************************************************************************
  128. Copyright (c) 2018 Welling Motor Technology(Shanghai) Co., Ltd.
  129. All rights reserved.
  130. *************************************************************************/
  131. #ifdef _FLASH_MASTER_C
  132. #undef _FLASH_MASTER_C_
  133. #endif
  134. #endif
  135. /*************************************************************************
  136. End of this File (EOF)
  137. Do not put anything after this part!
  138. *************************************************************************/