fal_flash_sfud_port.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * File : fal_flash_sfud_port.c
  3. * This file is part of FAL (Flash Abstraction Layer) package
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-01-26 armink the first version
  23. */
  24. #include <fal.h>
  25. #include <sfud.h>
  26. #ifdef FAL_USING_SFUD_PORT
  27. #ifdef RT_USING_SFUD
  28. #include <spi_flash_sfud.h>
  29. #endif
  30. #ifndef FAL_USING_NOR_FLASH_DEV_NAME
  31. #define FAL_USING_NOR_FLASH_DEV_NAME "norflash0"
  32. #endif
  33. static int init(void);
  34. static int read(long offset, uint8_t *buf, size_t size);
  35. static int write(long offset, const uint8_t *buf, size_t size);
  36. static int erase(long offset, size_t size);
  37. static sfud_flash_t sfud_dev = NULL;
  38. struct fal_flash_dev nor_flash0 =
  39. {
  40. .name = FAL_USING_NOR_FLASH_DEV_NAME,
  41. .addr = 0,
  42. .len = 8 * 1024 * 1024,
  43. .blk_size = 4096,
  44. .ops = {init, read, write, erase},
  45. .write_gran = 1
  46. };
  47. static int init(void)
  48. {
  49. #ifdef RT_USING_SFUD
  50. /* RT-Thread RTOS platform */
  51. sfud_dev = rt_sfud_flash_find_by_dev_name(FAL_USING_NOR_FLASH_DEV_NAME);
  52. #else
  53. /* bare metal platform */
  54. extern sfud_flash sfud_norflash0;
  55. sfud_dev = &sfud_norflash0;
  56. #endif
  57. if (NULL == sfud_dev)
  58. {
  59. return -1;
  60. }
  61. /* update the flash chip information */
  62. nor_flash0.blk_size = sfud_dev->chip.erase_gran;
  63. nor_flash0.len = sfud_dev->chip.capacity;
  64. return 0;
  65. }
  66. static int read(long offset, uint8_t *buf, size_t size)
  67. {
  68. assert(sfud_dev);
  69. assert(sfud_dev->init_ok);
  70. sfud_read(sfud_dev, nor_flash0.addr + offset, size, buf);
  71. return size;
  72. }
  73. static int write(long offset, const uint8_t *buf, size_t size)
  74. {
  75. assert(sfud_dev);
  76. assert(sfud_dev->init_ok);
  77. if (sfud_write(sfud_dev, nor_flash0.addr + offset, size, buf) != SFUD_SUCCESS)
  78. {
  79. return -1;
  80. }
  81. return size;
  82. }
  83. static int erase(long offset, size_t size)
  84. {
  85. assert(sfud_dev);
  86. assert(sfud_dev->init_ok);
  87. if (sfud_erase(sfud_dev, nor_flash0.addr + offset, size) != SFUD_SUCCESS)
  88. {
  89. return -1;
  90. }
  91. return size;
  92. }
  93. #endif /* FAL_USING_SFUD_PORT */