drv_spi_flash.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * File : stm32f20x_40x_spi.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-01-01 aozima first implementation.
  13. * 2018-03-30 misonyo porting to gd32f30x
  14. */
  15. #include <board.h>
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. #include "spi_flash.h"
  19. #include "spi_flash_sfud.h"
  20. #define SPI_BUS_NAME "spi0"
  21. #define SPI_DEVICE_NAME "spi01"
  22. #define SPI_FLASH_DEVICE_NAME "gd25q"
  23. #define GD25Q_SPI_CS_PIN 2 //PE3,在 drv_gpio.c 文件 pin_index pins[]中查到 PE3 编号为 2
  24. static int rt_hw_gd25q40_init(void)
  25. {
  26. rt_err_t res;
  27. static struct rt_spi_device spi_dev_gd25q; /* SPI设备对象 */
  28. static rt_base_t gd25q_cs_pin; /* SPI设备CS片选引脚 */
  29. gd25q_cs_pin = GD25Q_SPI_CS_PIN;
  30. rt_pin_mode(GD25Q_SPI_CS_PIN, GPIO_MODE_OUT_PP);
  31. res = rt_spi_bus_attach_device(&spi_dev_gd25q, SPI_DEVICE_NAME, SPI_BUS_NAME, (void*)gd25q_cs_pin);
  32. if (res != RT_EOK)
  33. {
  34. rt_kprintf("rt_spi_bus_attach_device() run failed!\n");
  35. return res;
  36. }
  37. return RT_EOK;
  38. }
  39. INIT_DEVICE_EXPORT(rt_hw_gd25q40_init);
  40. static int rt_hw_spi_flash_with_sfud_init(void)
  41. {
  42. if (RT_NULL == rt_sfud_flash_probe(SPI_FLASH_DEVICE_NAME, SPI_DEVICE_NAME))
  43. {
  44. return RT_ERROR;
  45. }
  46. return RT_EOK;
  47. }
  48. INIT_COMPONENT_EXPORT(rt_hw_spi_flash_with_sfud_init);
  49. #ifdef RT_USING_DFS
  50. #include <dfs_fs.h>
  51. int mnt_init(void)
  52. {
  53. if (dfs_mount(SPI_FLASH_DEVICE_NAME, "/", "elm", 0, 0) == 0)
  54. {
  55. rt_kprintf("spi flash mount success !\n");
  56. }
  57. else
  58. {
  59. rt_kprintf("spi flash mount failed!\n");
  60. }
  61. return 0;
  62. }
  63. MSH_CMD_EXPORT(mnt_init, mount spi flash to file system);
  64. #endif