drv_spi_flash.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * 2022-04-28 CDT first version
  9. */
  10. #include <board.h>
  11. #include <drv_spi.h>
  12. #include <rtdevice.h>
  13. #include <rthw.h>
  14. #include <finsh.h>
  15. #include <dfs_fs.h>
  16. #include <fal.h>
  17. #ifdef BSP_USING_SPI_FLASH
  18. #include "dev_spi_flash.h"
  19. #ifdef RT_USING_SFUD
  20. #include "dev_spi_flash_sfud.h"
  21. #endif
  22. #if defined(HC32F4A0) || defined(HC32F448) || defined(HC32F4A8)
  23. #define SPI_BUS_NAME "spi1"
  24. #define SPI_FLASH_DEVICE_NAME "spi10"
  25. #define SPI_FLASH_CHIP "w25q64"
  26. #define SPI_FLASH_SS_PIN GET_PIN(C, 7)
  27. #elif defined(HC32F460)
  28. #define SPI_BUS_NAME "spi3"
  29. #define SPI_FLASH_DEVICE_NAME "spi30"
  30. #define SPI_FLASH_CHIP "w25q64"
  31. #define SPI_FLASH_SS_PIN GET_PIN(C, 7)
  32. #elif defined(HC32F472)
  33. #define SPI_BUS_NAME "spi1"
  34. #define SPI_FLASH_DEVICE_NAME "spi10"
  35. #define SPI_FLASH_CHIP "w25q64"
  36. #define SPI_FLASH_SS_PIN GET_PIN(B,12)
  37. #elif defined(HC32F334)
  38. #define SPI_BUS_NAME "spi1"
  39. #define SPI_FLASH_DEVICE_NAME "spi10"
  40. #define SPI_FLASH_CHIP "w25q64"
  41. #define SPI_FLASH_SS_PIN GET_PIN(C,1)
  42. #endif
  43. #define SPI_FLASH_CMD_ENABLE_RESET 0x66
  44. #define SPI_FLASH_CMD_RESET_DEVICE 0x99
  45. /* Partition Name */
  46. #define FS_PARTITION_NAME "filesystem"
  47. #ifdef RT_USING_SFUD
  48. static void rt_hw_spi_flash_reset(char *spi_dev_name)
  49. {
  50. struct rt_spi_device *spi_dev_w25;
  51. rt_uint8_t w25_en_reset = SPI_FLASH_CMD_ENABLE_RESET;
  52. rt_uint8_t w25_reset_dev = SPI_FLASH_CMD_RESET_DEVICE;
  53. spi_dev_w25 = (struct rt_spi_device *)rt_device_find(spi_dev_name);
  54. if (!spi_dev_w25)
  55. {
  56. rt_kprintf("Can't find %s device!\n", spi_dev_name);
  57. }
  58. else
  59. {
  60. rt_spi_send(spi_dev_w25, &w25_en_reset, 1U);
  61. rt_spi_send(spi_dev_w25, &w25_reset_dev, 1U);
  62. DDL_DelayMS(1U);
  63. rt_kprintf("Reset ext flash!\n");
  64. }
  65. }
  66. static int rt_hw_spi_flash_with_sfud_init(void)
  67. {
  68. rt_hw_spi_device_attach(SPI_BUS_NAME, SPI_FLASH_DEVICE_NAME, SPI_FLASH_SS_PIN);
  69. if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME))
  70. {
  71. rt_hw_spi_flash_reset(SPI_FLASH_DEVICE_NAME);
  72. if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_CHIP, SPI_FLASH_DEVICE_NAME))
  73. {
  74. return -RT_ERROR;
  75. }
  76. }
  77. return RT_EOK;
  78. }
  79. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init);
  80. static int rt_hw_fs_init(void)
  81. {
  82. struct rt_device *mtd_dev = RT_NULL;
  83. /* 初始化 fal */
  84. fal_init();
  85. /* 生成 mtd 设备 */
  86. mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
  87. if (!mtd_dev)
  88. {
  89. LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
  90. return -RT_ERROR;
  91. }
  92. else
  93. {
  94. /* 挂载 littlefs */
  95. if (RT_EOK == dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0))
  96. {
  97. LOG_I("Filesystem initialized!");
  98. return RT_EOK;
  99. }
  100. else
  101. {
  102. /* 格式化文件系统 */
  103. if (RT_EOK == dfs_mkfs("lfs", FS_PARTITION_NAME))
  104. {
  105. /* 挂载 littlefs */
  106. if (RT_EOK == dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0))
  107. {
  108. LOG_I("Filesystem initialized!");
  109. return RT_EOK;
  110. }
  111. else
  112. {
  113. LOG_E("Failed to initialize filesystem!");
  114. return -RT_ERROR;
  115. }
  116. }
  117. else
  118. {
  119. LOG_E("Failed to Format fs!");
  120. return -RT_ERROR;
  121. }
  122. }
  123. }
  124. }
  125. INIT_APP_EXPORT(rt_hw_fs_init);
  126. #endif /* RT_USING_SFUD */
  127. #endif /* BSP_USING_SPI_FLASH */