flash_master.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /************************************************************************
  24. Included File
  25. *************************************************************************/
  26. #include "flash_master.h"
  27. #include "gd32f30x.h"
  28. #include "queue.h"
  29. #include "string.h"
  30. #include "AssistCurve.h"
  31. /*************************************************************************
  32. Exported Functions (N/A)
  33. *************************************************************************/
  34. /*************************************************************************
  35. Function:
  36. Description:
  37. Call by:
  38. Input Variables:
  39. Output/Return Variables:
  40. Subroutine Call:
  41. Reference:
  42. *************************************************************************/
  43. FLASH_PARA_TYPE flash_stPara;
  44. //从指定地址开始写入多个数据
  45. void FLASH_voWriteMoreData(ULONG startAddress, const UWORD writeData[], UWORD countToWrite)
  46. {
  47. UWORD dataIndex;
  48. if (startAddress < FLASH_BASE || ((startAddress + countToWrite * 2) >= (FLASH_END)))
  49. {
  50. return; //非法地址
  51. }
  52. fmc_unlock(); //解锁写保护
  53. while (fmc_page_erase((uint32_t)startAddress) != FMC_READY)
  54. {
  55. ;
  56. } //擦除这个扇区
  57. for (dataIndex = 0; dataIndex < countToWrite; dataIndex++)
  58. {
  59. fmc_halfword_program((uint32_t)startAddress + dataIndex * 2, (uint16_t)writeData[dataIndex]);
  60. }
  61. fmc_lock();//上锁写保护
  62. }
  63. //读取指定地址的半字(16位数据)
  64. static UWORD FLASH_ReadWord(ULONG address)
  65. {
  66. return *(__IO UWORD *)address;
  67. }
  68. //从指定地址开始读取多个数据
  69. void FLASH_voReadMoreData(ULONG startAddress, UWORD readData[], UWORD countToRead)
  70. {
  71. UWORD dataIndex;
  72. for (dataIndex = 0; dataIndex < sizeof(FLASH_PARA_TYPE) / 2; dataIndex++)
  73. {
  74. readData[dataIndex] = FLASH_ReadWord(startAddress + dataIndex * 2);
  75. }
  76. }
  77. void flash_voWrite(void)
  78. {
  79. UWORD count_len = sizeof(FLASH_PARA_TYPE) / 2;
  80. FLASH_voWriteMoreData(StartServerManageFlashAddress, (UWORD *)(&flash_stPara), count_len);
  81. }
  82. void flash_voRead(void)
  83. {
  84. UWORD count_len = sizeof(FLASH_PARA_TYPE) / 2;
  85. FLASH_voReadMoreData(StartServerManageFlashAddress, (UWORD *)(&flash_stPara), count_len);
  86. }
  87. void flash_voParaInit(void)
  88. {
  89. memset(&flash_stPara, 0, sizeof(FLASH_PARA_TYPE));
  90. SLONG Flash_TorqueAssGain[15][4] = TORQUE_ASSIST_DEFAULT;
  91. SLONG Flash_CadAssGain[5][4] = CADENCE_ASSIST_DEFAULT;
  92. memcpy(&flash_stPara.slTorqAssGain[0], &Flash_TorqueAssGain[0], sizeof(Flash_TorqueAssGain));
  93. memcpy(&flash_stPara.slCadAssGain[0], &Flash_CadAssGain[0], sizeof(Flash_CadAssGain));
  94. flash_voWrite();
  95. }
  96. void flash_voErrorWrite(void)
  97. {
  98. UWORD count_len = sizeof(FLASH_ERROR_QUEUE) / 2;
  99. FLASH_voWriteMoreData(StartServerManageErrorFlashAddress, (UWORD *)(&que_stFlashErrorLog), count_len);
  100. }
  101. void flash_voErrorRead(void)
  102. {
  103. UWORD count_len = sizeof(FLASH_ERROR_QUEUE) / 2;
  104. FLASH_voReadMoreData(StartServerManageErrorFlashAddress, (UWORD *)(&que_stFlashErrorLog), count_len);
  105. if(que_stFlashErrorLog.ubHead == 0xFF)
  106. {
  107. que_voInit(&que_stFlashErrorLog);
  108. }
  109. }