drv_qspi_flash.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. * 2021-01-19 wanghaijing the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <spi_flash.h>
  13. #include <drv_qspi.h>
  14. #ifdef BSP_USING_QSPI_FLASH
  15. char w25qxx_read_status_register2(struct rt_qspi_device *device)
  16. {
  17. /* 0x35 read status register2 */
  18. char instruction = 0x35, status;
  19. rt_qspi_send_then_recv(device, &instruction, 1, &status, 1);
  20. return status;
  21. }
  22. void w25qxx_write_enable(struct rt_qspi_device *device)
  23. {
  24. /* 0x06 write enable */
  25. char instruction = 0x06;
  26. rt_qspi_send(device, &instruction, 1);
  27. }
  28. void w25qxx_enter_qspi_mode(struct rt_qspi_device *device)
  29. {
  30. char status = 0;
  31. /* 0x38 enter qspi mode */
  32. char instruction = 0x38;
  33. char write_status2_buf[2] = {0};
  34. /* 0x31 write status register2 */
  35. write_status2_buf[0] = 0x31;
  36. status = w25qxx_read_status_register2(device);
  37. if (!(status & 0x02))
  38. {
  39. status |= 1 << 1;
  40. w25qxx_write_enable(device);
  41. write_status2_buf[1] = status;
  42. rt_qspi_send(device, &write_status2_buf, 2);
  43. rt_qspi_send(device, &instruction, 1);
  44. rt_kprintf("flash already enter qspi mode\n");
  45. rt_thread_mdelay(10);
  46. }
  47. }
  48. static int rt_qspi_flash_init(void)
  49. {
  50. extern rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const char *spi_dev_name);
  51. stm32_qspi_bus_attach_device("qspi1", "qspi10", RT_NULL, 4, w25qxx_enter_qspi_mode, RT_NULL);
  52. if (RT_NULL == rt_sfud_flash_probe("norflash1", "qspi10"))
  53. {
  54. return -RT_ERROR;
  55. }
  56. return RT_EOK;
  57. }
  58. INIT_ENV_EXPORT(rt_qspi_flash_init);
  59. #endif/* BSP_USING_QSPI_FLASH */