drv_spi_flash.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-07 NU-LL first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <drv_spi.h>
  13. #include <spi_flash.h>
  14. #include <spi_flash_sfud.h>
  15. #include <fal.h>
  16. #include <dfs_fs.h>
  17. #define DRV_DEBUG
  18. #define LOG_TAG "drv.spiflash"
  19. #include <drv_log.h>
  20. #define FS_PARTITION_NAME "filesystem"
  21. #define SPI_CS_GPIO GPIOD
  22. #define SPI_CS_PIN GPIO_PIN_6
  23. static int rt_hw_spi_flash_with_sfud_init(void)
  24. {
  25. rt_err_t err = RT_EOK;
  26. rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(D, 6));
  27. /* init W25Q16 , And register as a block device */
  28. if (RT_NULL == rt_sfud_flash_probe(FAL_USING_NOR_FLASH_DEV_NAME, "spi10"))
  29. {
  30. LOG_E("Failed to probe flash device "FAL_USING_NOR_FLASH_DEV_NAME);
  31. return -RT_ERROR;
  32. }
  33. return err;
  34. }
  35. INIT_DEVICE_EXPORT(rt_hw_spi_flash_with_sfud_init);
  36. static int mnt(void)
  37. {
  38. struct rt_device *mtd_dev = RT_NULL;
  39. fal_init();
  40. mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
  41. if (!mtd_dev)
  42. {
  43. LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
  44. }
  45. else
  46. {
  47. /* mount littlefs */
  48. if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == 0)
  49. {
  50. LOG_I("Filesystem initialized!");
  51. }
  52. else
  53. {
  54. dfs_mkfs("lfs", FS_PARTITION_NAME);
  55. /* mount littlefs */
  56. if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == 0)
  57. {
  58. mkdir("/qspi",0x777);
  59. LOG_I("mkdir /qspi. Filesystem initialized!");
  60. }
  61. else
  62. {
  63. LOG_E("Failed to initialize filesystem!");
  64. }
  65. }
  66. }
  67. return RT_EOK;
  68. }
  69. INIT_ENV_EXPORT(mnt);