spi_sfud_sample.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2022-11-20 liqiaozhong first commit
  11. * 2022-03-08 liqiaozhong add format function and mount table
  12. */
  13. #include <rtthread.h>
  14. #include <string.h>
  15. #if defined (RT_USING_SFUD) && defined(RT_USING_DFS)
  16. #include <dfs_fs.h>
  17. #include <dfs_file.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <sys/stat.h>
  21. #include <sys/statfs.h>
  22. #include "spi_flash.h"
  23. #include "spi_flash_sfud.h"
  24. #include "fdebug.h"
  25. #include "fparameters_comm.h"
  26. #include "fspim.h"
  27. /************************** Variable Definitions *****************************/
  28. sfud_flash_t spim_flash = RT_NULL;
  29. const struct dfs_mount_tbl mount_table[] =
  30. {
  31. { "flash2", "/", "elm", 0, RT_NULL },
  32. {0},
  33. };
  34. /***************** Macros (Inline Fungoctions) Definitions *********************/
  35. #define FSPIM_DEBUG_TAG "SPIM"
  36. #define FSPIM_ERROR(format, ...) FT_DEBUG_PRINT_E(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
  37. #define FSPIM_WARN(format, ...) FT_DEBUG_PRINT_W(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
  38. #define FSPIM_INFO(format, ...) FT_DEBUG_PRINT_I(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
  39. #define FSPIM_DEBUG(format, ...) FT_DEBUG_PRINT_D(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
  40. /*******************************Api Functions*********************************/
  41. static int spi_flash_sfud_init(void)
  42. {
  43. if (RT_NULL == rt_sfud_flash_probe("flash2", "spi02"))
  44. {
  45. rt_kprintf("rt_sfud_flash_probe failed\n");
  46. return RT_ERROR;
  47. }
  48. spim_flash = rt_sfud_flash_find_by_dev_name("flash2");
  49. if (RT_NULL == spim_flash)
  50. {
  51. rt_kprintf("Flash init failed -> can't find flash2 device!\n");
  52. return RT_ERROR;
  53. }
  54. rt_kprintf("Spi flash device flash2 init\n");
  55. rt_kprintf("Flash device: flash2 info\nmf_id: 0x%x\ntype_id: 0x%x\ncapacity_id: 0x%x\nerase granularity: %lu\n",
  56. spim_flash->chip.mf_id,
  57. spim_flash->chip.type_id,
  58. spim_flash->chip.capacity_id,
  59. spim_flash->chip.erase_gran);
  60. return RT_EOK;
  61. }
  62. INIT_DEVICE_EXPORT(spi_flash_sfud_init);
  63. /* format the flash with elm environment */
  64. static int flash_format_operation(void)
  65. {
  66. int result = RT_EOK;
  67. result = dfs_mkfs("elm", "flash2");
  68. return result;
  69. }
  70. INIT_ENV_EXPORT(flash_format_operation);
  71. #endif /* RT_USING_SFUD || RT_USING_DFS */