drv_qspi_flash.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 W25Q256 */
  55. if (RT_NULL == rt_sfud_flash_probe("W25Q256", "qspi10"))
  56. {
  57. return -RT_ERROR;
  58. }
  59. return RT_EOK;
  60. }
  61. INIT_DEVICE_EXPORT(rt_hw_qspi_flash_with_sfud_init);
  62. #endif/* BSP_USING_QSPI_FLASH */