drv_qspi_flash.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * 2019-04-11 ZYH port from stm32f7serial
  10. */
  11. #include <board.h>
  12. #include <drv_qspi.h>
  13. #include <rtdevice.h>
  14. #include <rthw.h>
  15. #include <finsh.h>
  16. #ifdef BSP_USING_QSPI_FLASH
  17. #include "spi_flash.h"
  18. #include "spi_flash_sfud.h"
  19. char n25qxxa_read_status_register2(struct rt_qspi_device *device)
  20. {
  21. /* 0x35 read status register2 */
  22. char instruction = 0x35, status;
  23. rt_qspi_send_then_recv(device, &instruction, 1, &status, 1);
  24. return status;
  25. }
  26. void n25qxxa_write_enable(struct rt_qspi_device *device)
  27. {
  28. /* 0x06 write enable */
  29. char instruction = 0x06;
  30. rt_qspi_send(device, &instruction, 1);
  31. }
  32. void n25qxxa_enter_qspi_mode(struct rt_qspi_device *device)
  33. {
  34. char status = 0;
  35. /* 0x38 enter qspi mode */
  36. char instruction = 0x38;
  37. char write_status2_buf[2] = {0};
  38. /* 0x31 write status register2 */
  39. write_status2_buf[0] = 0x31;
  40. status = n25qxxa_read_status_register2(device);
  41. if (!(status & 0x02))
  42. {
  43. status |= 1 << 1;
  44. n25qxxa_write_enable(device);
  45. write_status2_buf[1] = status;
  46. rt_qspi_send(device, &write_status2_buf, 2);
  47. rt_qspi_send(device, &instruction, 1);
  48. rt_kprintf("flash already enter qspi mode\n");
  49. rt_thread_mdelay(10);
  50. }
  51. }
  52. static int rt_hw_qspi_flash_with_sfud_init(void)
  53. {
  54. stm32_qspi_bus_attach_device("qspi1", "qspi10", RT_NULL, 4, n25qxxa_enter_qspi_mode, RT_NULL);
  55. /* init n25qxx */
  56. if (RT_NULL == rt_sfud_flash_probe(FAL_USING_NOR_FLASH_DEV_NAME, "qspi10"))
  57. {
  58. return -RT_ERROR;
  59. }
  60. return RT_EOK;
  61. }
  62. INIT_COMPONENT_EXPORT(rt_hw_qspi_flash_with_sfud_init);
  63. #endif/* BSP_USING_QSPI_FLASH */