drv_flash.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2006-2021, Bluetrum Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-16 greedyhao first version
  9. */
  10. #include "board.h"
  11. #include "drv_flash.h"
  12. #ifdef BSP_USING_ON_CHIP_FLASH
  13. #if defined(RT_USING_FAL)
  14. #include "fal.h"
  15. #endif
  16. //#define DRV_DEBUG
  17. #define LOG_TAG "drv.flash"
  18. #include <drv_log.h>
  19. #if defined(RT_USING_FAL)
  20. #define AB32_FLASH_START_ADDRESS 0x00000000
  21. #define AB32_FLASH_SIZE (1024 * 1024)
  22. #define AB32_FLASH_PAGE_SIZE (0x1000)
  23. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
  24. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
  25. static int fal_flash_erase(long offset, size_t size);
  26. const struct fal_flash_dev ab32_onchip_flash =
  27. {
  28. "onchip_flash",
  29. AB32_FLASH_START_ADDRESS,
  30. AB32_FLASH_SIZE,
  31. AB32_FLASH_PAGE_SIZE,
  32. {NULL, fal_flash_read, fal_flash_write, fal_flash_erase},
  33. 256 * 8
  34. };
  35. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
  36. {
  37. return os_spiflash_read(buf, offset, size);
  38. }
  39. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
  40. {
  41. if (size % 256)
  42. {
  43. rt_kprintf("Flash write requires 256 byte alignment\n");
  44. return -1;
  45. }
  46. os_spiflash_program(buf, offset, size);
  47. return 0;
  48. }
  49. static int fal_flash_erase(long offset, size_t size)
  50. {
  51. if (size % 4096)
  52. {
  53. rt_kprintf("Flash erase requires 4096 byte alignment\n");
  54. return -1;
  55. }
  56. while (size > 0)
  57. {
  58. os_spiflash_erase(offset);
  59. offset += 4096;
  60. size -= 4096;
  61. }
  62. return 0;
  63. }
  64. int fal_ops_test(void)
  65. {
  66. int result;
  67. const struct fal_partition *part_dev = fal_partition_find("param");
  68. uint8_t *data = rt_malloc(256);
  69. int i;
  70. int size = 256;
  71. int addr = 0;
  72. for (int i = 0; i < 256; i++)
  73. {
  74. data[i] = i;
  75. }
  76. result = fal_partition_write(part_dev, 0, data, 256);
  77. if (result >= 0)
  78. {
  79. rt_kprintf("Write data success. Start from 0x%08X, size is %ld.\n", addr, size);
  80. rt_kprintf("Write data: ");
  81. for (i = 0; i < size; i++)
  82. {
  83. rt_kprintf("%d ", data[i]);
  84. }
  85. rt_kprintf(".\n");
  86. }
  87. rt_memset(data, 0, 256);
  88. result = fal_partition_read(part_dev, 0, data, 256);
  89. if (result >= 0)
  90. {
  91. rt_kprintf("Read data success. Start from 0x%08X, size is %ld.\n", addr, size);
  92. rt_kprintf("Read data: ");
  93. for (i = 0; i < size; i++)
  94. {
  95. rt_kprintf("%d ", data[i]);
  96. }
  97. rt_kprintf(".\n");
  98. }
  99. result = fal_partition_erase(part_dev, 0, 4096);
  100. if (result >= 0)
  101. {
  102. rt_kprintf("Erase data success.\n");
  103. }
  104. rt_memset(data, 0, 256);
  105. result = fal_partition_read(part_dev, 0, data, 256);
  106. if (result >= 0)
  107. {
  108. rt_kprintf("Read data success. Start from 0x%08X, size is %ld.\n", addr, size);
  109. rt_kprintf("Read data: ");
  110. for (i = 0; i < size; i++)
  111. {
  112. rt_kprintf("%d ", data[i]);
  113. }
  114. rt_kprintf(".\n");
  115. }
  116. rt_free(data);
  117. return 0;
  118. }
  119. MSH_CMD_EXPORT(fal_ops_test, "fal_ops_test");
  120. #endif
  121. #endif