drv_qspi_flash.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * 2018-11-27 zylx first version
  9. * 2022-03-16 Miaowulue add dfs mount
  10. */
  11. #include <board.h>
  12. #include <drv_qspi.h>
  13. #include <rtdevice.h>
  14. #include <rthw.h>
  15. #include <finsh.h>
  16. #include <dfs_elm.h>
  17. #include <dfs_fs.h>
  18. #ifdef BSP_USING_QSPI_FLASH
  19. #include "spi_flash.h"
  20. #include "spi_flash_sfud.h"
  21. char w25qxx_read_status_register2(struct rt_qspi_device *device)
  22. {
  23. /* 0x35 read status register2 */
  24. char instruction = 0x35, status;
  25. rt_qspi_send_then_recv(device, &instruction, 1, &status, 1);
  26. return status;
  27. }
  28. void w25qxx_write_enable(struct rt_qspi_device *device)
  29. {
  30. /* 0x06 write enable */
  31. char instruction = 0x06;
  32. rt_qspi_send(device, &instruction, 1);
  33. }
  34. void w25qxx_enter_qspi_mode(struct rt_qspi_device *device)
  35. {
  36. char status = 0;
  37. /* 0x38 enter qspi mode */
  38. char instruction = 0x38;
  39. char write_status2_buf[2] = {0};
  40. /* 0x31 write status register2 */
  41. write_status2_buf[0] = 0x31;
  42. status = w25qxx_read_status_register2(device);
  43. if (!(status & 0x02))
  44. {
  45. status |= 1 << 1;
  46. w25qxx_write_enable(device);
  47. write_status2_buf[1] = status;
  48. rt_qspi_send(device, &write_status2_buf, 2);
  49. rt_qspi_send(device, &instruction, 1);
  50. rt_kprintf("flash already enter qspi mode\n");
  51. rt_thread_mdelay(10);
  52. }
  53. }
  54. static int rt_hw_qspi_flash_with_sfud_init(void)
  55. {
  56. stm32_qspi_bus_attach_device("qspi1", "qspi10", RT_NULL, 4, w25qxx_enter_qspi_mode, RT_NULL);
  57. /* init W25Q256 */
  58. if (RT_NULL == rt_sfud_flash_probe("W25Q256", "qspi10"))
  59. {
  60. return -RT_ERROR;
  61. }
  62. return RT_EOK;
  63. }
  64. INIT_DEVICE_EXPORT(rt_hw_qspi_flash_with_sfud_init);
  65. static int mnt_qspi_flash_init(void)
  66. {
  67. if (dfs_mount("W25Q256", "/", "elm", 0, 0) == RT_EOK)
  68. {
  69. rt_kprintf("Mount spi flash successfully!\n");
  70. return RT_EOK;
  71. }
  72. else
  73. {
  74. rt_kprintf("Mount spi flash fail!\n");
  75. return -RT_ERROR;
  76. }
  77. }
  78. INIT_APP_EXPORT(mnt_qspi_flash_init);
  79. #endif/* BSP_USING_QSPI_FLASH */