eeprom_iap.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2021 IOsetting <iosetting(at)outlook.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /***
  15. * Demo: EEPROM IAP read, write, erase and software reset
  16. */
  17. #include "fw_hal.h"
  18. #define TEST_ADDR 0x0400
  19. #define TEST_SIZE 32
  20. void TestIAPRead(uint16_t addr, uint8_t size)
  21. {
  22. uint8_t pos = 0;
  23. UART1_TxString("IAP_CmdRead: ");
  24. for (pos = 0; pos < size; pos++)
  25. {
  26. IAP_CmdRead(addr + pos);
  27. if (IAP_IsCmdFailed())
  28. {
  29. UART1_TxString("__ ");
  30. IAP_ClearCmdFailFlag();
  31. }
  32. else
  33. {
  34. UART1_TxHex(IAP_ReadData());
  35. UART1_TxChar(' ');
  36. }
  37. }
  38. UART1_TxString("\r\n");
  39. }
  40. void TestIAPWrite(uint16_t addr, uint8_t size)
  41. {
  42. uint8_t pos = 0;
  43. UART1_TxString("IAP_CmdWrite: ");
  44. for (pos = 0; pos < size; pos++)
  45. {
  46. IAP_WriteData(pos);
  47. IAP_CmdWrite(addr + pos);
  48. if (IAP_IsCmdFailed())
  49. {
  50. UART1_TxString("__ ");
  51. IAP_ClearCmdFailFlag();
  52. }
  53. else
  54. {
  55. UART1_TxHex(pos);
  56. UART1_TxChar(' ');
  57. }
  58. }
  59. UART1_TxString("\r\n");
  60. }
  61. void TestIAPErase(uint16_t addr)
  62. {
  63. uint8_t pos = 0;
  64. UART1_TxString("IAP_CmdErase: ");
  65. IAP_CmdErase(addr);
  66. if (IAP_IsCmdFailed())
  67. {
  68. UART1_TxString("failed.\r\n");
  69. IAP_ClearCmdFailFlag();
  70. }
  71. else
  72. {
  73. UART1_TxString("succeeded.\r\n");
  74. }
  75. }
  76. int main(void)
  77. {
  78. SYS_SetClock();
  79. UART1_Config8bitUart(UART1_BaudSource_Timer2, HAL_State_ON, 115200);
  80. UART1_TxString("Test restarted\r\n");
  81. IAP_SetWaitTime();
  82. IAP_SetEnabled(HAL_State_ON);
  83. TestIAPRead(TEST_ADDR, TEST_SIZE);
  84. SYS_Delay(500);
  85. TestIAPWrite(TEST_ADDR, TEST_SIZE);
  86. SYS_Delay(500);
  87. TestIAPRead(TEST_ADDR, TEST_SIZE);
  88. SYS_Delay(500);
  89. TestIAPErase(TEST_ADDR);
  90. SYS_Delay(500);
  91. TestIAPRead(TEST_ADDR, TEST_SIZE);
  92. IAP_SetEnabled(HAL_State_OFF);
  93. SYS_Delay(2000);
  94. UART1_TxString("Software reset\r\n");
  95. SYS_Delay(1000);
  96. IAP_SoftReset();
  97. return 0;
  98. }