drv_qspi_flash.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-27 zylx first version
  9. */
  10. #include <board.h>
  11. #include <drv_qspi.h>
  12. #include <rtdevice.h>
  13. #include <rthw.h>
  14. #include <finsh.h>
  15. #ifdef BSP_USING_QSPI_FLASH
  16. #include "spi_flash.h"
  17. #include "spi_flash_sfud.h"
  18. char w25qxx_read_status_register2(struct rt_qspi_device *device)
  19. {
  20. /* 0x35 read status register2 */
  21. char instruction = 0x35, status;
  22. rt_qspi_send_then_recv(device, &instruction, 1, &status, 1);
  23. return status;
  24. }
  25. void w25qxx_write_enable(struct rt_qspi_device *device)
  26. {
  27. /* 0x06 write enable */
  28. char instruction = 0x06;
  29. rt_qspi_send(device, &instruction, 1);
  30. }
  31. void w25qxx_enter_qspi_mode(struct rt_qspi_device *device)
  32. {
  33. char status = 0;
  34. /* 0x38 enter qspi mode */
  35. char instruction = 0x38;
  36. char write_status2_buf[2] = {0};
  37. /* 0x31 write status register2 */
  38. write_status2_buf[0] = 0x31;
  39. status = w25qxx_read_status_register2(device);
  40. if (!(status & 0x02))
  41. {
  42. status |= 1 << 1;
  43. w25qxx_write_enable(device);
  44. write_status2_buf[1] = status;
  45. rt_qspi_send(device, &write_status2_buf, 2);
  46. rt_qspi_send(device, &instruction, 1);
  47. rt_kprintf("flash already enter qspi mode\n");
  48. rt_thread_mdelay(10);
  49. }
  50. }
  51. static int rt_hw_qspi_flash_with_sfud_init(void)
  52. {
  53. stm32_qspi_bus_attach_device("qspi1", "qspi10", RT_NULL, 4, w25qxx_enter_qspi_mode, RT_NULL);
  54. /* init w25q128 */
  55. if (RT_NULL == rt_sfud_flash_probe("W25Q128", "qspi10"))
  56. {
  57. return -RT_ERROR;
  58. }
  59. return RT_EOK;
  60. }
  61. INIT_COMPONENT_EXPORT(rt_hw_qspi_flash_with_sfud_init);
  62. #if defined(RT_USING_DFS_ELMFAT) && !defined(BSP_USING_SDCARD)
  63. #include <dfs_fs.h>
  64. #define BLK_DEV_NAME "W25Q128"
  65. int mnt_init(void)
  66. {
  67. rt_thread_delay(RT_TICK_PER_SECOND);
  68. if (dfs_mount(BLK_DEV_NAME, "/", "elm", 0, 0) == 0)
  69. {
  70. rt_kprintf("file system initialization done!\n");
  71. }
  72. else
  73. {
  74. if(dfs_mkfs("elm", BLK_DEV_NAME) == 0)
  75. {
  76. if (dfs_mount(BLK_DEV_NAME, "/", "elm", 0, 0) == 0)
  77. {
  78. rt_kprintf("file system initialization done!\n");
  79. }
  80. else
  81. {
  82. rt_kprintf("file system initialization failed!\n");
  83. }
  84. }
  85. }
  86. return 0;
  87. }
  88. INIT_ENV_EXPORT(mnt_init);
  89. #endif /* defined(RT_USING_DFS_ELMFAT) && !defined(BSP_USING_SDCARD) */
  90. #endif /* BSP_USING_QSPI_FLASH */