test_fal.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-30 CDT first version
  9. */
  10. /*
  11. * 程序清单: FAL使用例程
  12. * 例程导出了 fal_sample 命令到控制终端
  13. * 命令调用格式:fal_sample
  14. * 1)配置RTT工程
  15. * menuconfig:
  16. * RT-Thread Components ---> FAL: flash abstraction layer
  17. * ---> Device Drivers ---> Using SPI Bus/Device device drivers ---> Using Serial Flash Universal Driver
  18. * Hardware Drivers Config ---> Onboard Peripheral Drivers ----> Enable on-chip FLASH
  19. */
  20. #include <rtthread.h>
  21. #include <rtdevice.h>
  22. #if defined(RT_USING_FAL) && defined(BSP_USING_ON_CHIP_FLASH)
  23. #include "board.h"
  24. #include <fal.h>
  25. #define FAL_PART_NAME "app"
  26. #define TEST_BUF_SIZE 1024UL
  27. #define TEST_RW_CNT 32UL
  28. #define TEST_RW_START_ADDR HC32_FLASH_END_ADDRESS - (TEST_BUF_SIZE * TEST_RW_CNT)
  29. static uint8_t write_buffer[TEST_BUF_SIZE] = {0};
  30. static uint8_t read_buffer[TEST_BUF_SIZE] = {0};
  31. static int fal_sample(int argc, char **argv)
  32. {
  33. const struct fal_partition *param;
  34. int ret;
  35. uint32_t Address;
  36. uint8_t errFlag = 0;
  37. fal_init(); //抽象层初始化
  38. /* Set write buffer, clear read buffer */
  39. for (uint32_t index = 0; index < TEST_BUF_SIZE; index++)
  40. {
  41. write_buffer[index] = index;
  42. }
  43. param = fal_partition_find(FAL_PART_NAME);
  44. if (param == RT_NULL)
  45. {
  46. rt_kprintf("not find partition app!\r\n");
  47. return -1;
  48. }
  49. for (int j = 0; j < TEST_RW_CNT; j++)
  50. {
  51. errFlag = 0;
  52. Address = TEST_RW_START_ADDR + j * TEST_BUF_SIZE;
  53. rt_kprintf("........test %d address 0x%08x........\r\n", j + 1, Address);
  54. /* erase process */
  55. if (j == 31)
  56. {
  57. rt_kprintf(".......");
  58. }
  59. ret = fal_partition_erase(param, Address, TEST_BUF_SIZE);
  60. if (ret >= 0)
  61. {
  62. rt_kprintf("Erase succeeded!\r\n");
  63. }
  64. else
  65. {
  66. rt_kprintf("Erase failed!\r\n");
  67. return ret;
  68. }
  69. /* write process */
  70. ret = fal_partition_write(param, Address, write_buffer, TEST_BUF_SIZE);
  71. if (ret >= 0)
  72. {
  73. rt_kprintf("Write succeeded!\r\n");
  74. }
  75. else
  76. {
  77. rt_kprintf("Write failed!\r\n");
  78. return ret;
  79. }
  80. /* read process */
  81. for (uint32_t index = 0; index < TEST_BUF_SIZE; index++)
  82. {
  83. read_buffer[index] = 0;
  84. }
  85. ret = fal_partition_read(param, Address, read_buffer, TEST_BUF_SIZE);
  86. if (ret >= 0)
  87. {
  88. rt_kprintf("Read succeeded!\r\n");
  89. }
  90. else
  91. {
  92. rt_kprintf("Read failed!\r\n");
  93. return ret;
  94. }
  95. /* compare process */
  96. for (int i = 0; i < TEST_BUF_SIZE; i++)
  97. {
  98. #if defined(HC32F460)
  99. if ((j == (TEST_RW_CNT - 1)) && (i >= (TEST_BUF_SIZE - 32)) ?
  100. (read_buffer[i] != 0xFF) : (read_buffer[i] != write_buffer[i]))
  101. #else
  102. if (read_buffer[i] != write_buffer[i])
  103. #endif
  104. {
  105. rt_kprintf("Data verification failed:\r\n");
  106. rt_kprintf("NUM: %d Write: %x Read: %x \r\n", i, write_buffer[i], read_buffer[i]);
  107. errFlag = 1;
  108. ret = -1;
  109. }
  110. }
  111. if (0 == errFlag)
  112. {
  113. rt_kprintf("Data verification OK!\r\n");
  114. }
  115. }
  116. return ret;
  117. }
  118. /* 导出到 msh 命令列表中 */
  119. MSH_CMD_EXPORT(fal_sample, fal sample);
  120. #endif