fal_flash.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * File : fal_flash.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-05-17 armink the first version
  23. */
  24. #include <fal.h>
  25. #include <string.h>
  26. /* flash device table, must defined by user */
  27. #if !defined(FAL_FLASH_DEV_TABLE)
  28. #error "You must defined flash device table (FAL_FLASH_DEV_TABLE) on 'fal_cfg.h'"
  29. #endif
  30. static const struct fal_flash_dev * const device_table[] = FAL_FLASH_DEV_TABLE;
  31. static const size_t device_table_len = sizeof(device_table) / sizeof(device_table[0]);
  32. static uint8_t init_ok = 0;
  33. /**
  34. * Initialize all flash device on FAL flash table
  35. *
  36. * @return result
  37. */
  38. int fal_flash_init(void)
  39. {
  40. size_t i;
  41. const struct fal_flash_dev *dev;
  42. if (init_ok)
  43. {
  44. return 0;
  45. }
  46. for (i = 0; i < device_table_len; i++)
  47. {
  48. dev = device_table[i];
  49. assert(device_table[i]->ops.read);
  50. assert(device_table[i]->ops.write);
  51. assert(device_table[i]->ops.erase);
  52. /* init flash device on flash table */
  53. if (device_table[i]->ops.init)
  54. {
  55. device_table[i]->ops.init();
  56. }
  57. log_d("Flash device | %*.*s | addr: 0x%08lx | len: 0x%08x | blk_size: 0x%08x |initialized finish.",
  58. FAL_DEV_NAME_MAX, FAL_DEV_NAME_MAX, dev->name, dev->addr, dev->len,
  59. dev->blk_size);
  60. }
  61. init_ok = 1;
  62. return 0;
  63. }
  64. /**
  65. * find flash device by name
  66. *
  67. * @param name flash device name
  68. *
  69. * @return != NULL: flash device
  70. * NULL: not found
  71. */
  72. const struct fal_flash_dev *fal_flash_device_find(const char *name)
  73. {
  74. assert(init_ok);
  75. assert(name);
  76. size_t i;
  77. for (i = 0; i < device_table_len; i++)
  78. {
  79. if (!strncmp(name, device_table[i]->name, FAL_DEV_NAME_MAX)) {
  80. return device_table[i];
  81. }
  82. }
  83. return NULL;
  84. }