fal_flash.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-17 armink the first version
  9. */
  10. #include <fal.h>
  11. #include <string.h>
  12. /* flash device table, must defined by user */
  13. #if !defined(FAL_FLASH_DEV_TABLE)
  14. #error "You must defined flash device table (FAL_FLASH_DEV_TABLE) on 'fal_cfg.h'"
  15. #endif
  16. static const struct fal_flash_dev * const device_table[] = FAL_FLASH_DEV_TABLE;
  17. static const size_t device_table_len = sizeof(device_table) / sizeof(device_table[0]);
  18. static uint8_t init_ok = 0;
  19. /**
  20. * Initialize all flash device on FAL flash table
  21. *
  22. * @return result
  23. */
  24. int fal_flash_init(void)
  25. {
  26. size_t i;
  27. if (init_ok)
  28. {
  29. return 0;
  30. }
  31. for (i = 0; i < device_table_len; i++)
  32. {
  33. assert(device_table[i]->ops.read);
  34. assert(device_table[i]->ops.write);
  35. assert(device_table[i]->ops.erase);
  36. /* init flash device on flash table */
  37. if (device_table[i]->ops.init)
  38. {
  39. device_table[i]->ops.init();
  40. }
  41. log_d("Flash device | %*.*s | addr: 0x%08lx | len: 0x%08x | blk_size: 0x%08x |initialized finish.",
  42. FAL_DEV_NAME_MAX, FAL_DEV_NAME_MAX, device_table[i]->name, device_table[i]->addr, device_table[i]->len,
  43. device_table[i]->blk_size);
  44. }
  45. init_ok = 1;
  46. return 0;
  47. }
  48. /**
  49. * find flash device by name
  50. *
  51. * @param name flash device name
  52. *
  53. * @return != NULL: flash device
  54. * NULL: not found
  55. */
  56. const struct fal_flash_dev *fal_flash_device_find(const char *name)
  57. {
  58. assert(init_ok);
  59. assert(name);
  60. size_t i;
  61. for (i = 0; i < device_table_len; i++)
  62. {
  63. if (!strncmp(name, device_table[i]->name, FAL_DEV_NAME_MAX)) {
  64. return device_table[i];
  65. }
  66. }
  67. return NULL;
  68. }