fal_flash.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2006-2024 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. #define DBG_TAG "FAL"
  13. #ifdef FAL_USING_DEBUG
  14. #define DBG_LVL DBG_LOG
  15. #else
  16. #define DBG_LVL DBG_WARNING
  17. #endif
  18. #include <rtdbg.h>
  19. /* flash device table, must defined by user */
  20. #if !defined(FAL_FLASH_DEV_TABLE)
  21. #error "You must defined flash device table (FAL_FLASH_DEV_TABLE) on 'fal_cfg.h'"
  22. #endif
  23. static const struct fal_flash_dev * const device_table[] = FAL_FLASH_DEV_TABLE;
  24. static const rt_size_t device_table_len = sizeof(device_table) / sizeof(device_table[0]);
  25. static rt_uint8_t init_ok = 0;
  26. /**
  27. * Initialize all flash device on FAL flash table
  28. *
  29. * @return result
  30. */
  31. int fal_flash_init(void)
  32. {
  33. rt_size_t i, j, offset;
  34. if (init_ok)
  35. {
  36. return 0;
  37. }
  38. for (i = 0; i < device_table_len; i++)
  39. {
  40. RT_ASSERT(device_table[i]->ops.read);
  41. RT_ASSERT(device_table[i]->ops.write);
  42. RT_ASSERT(device_table[i]->ops.erase);
  43. /* init flash device on flash table */
  44. if (device_table[i]->ops.init)
  45. {
  46. device_table[i]->ops.init();
  47. }
  48. LOG_D("Flash device | %*.*s | addr: 0x%08lx | len: 0x%08x | blk_size: 0x%08x |initialized finish.",
  49. FAL_DEV_NAME_MAX, FAL_DEV_NAME_MAX, device_table[i]->name, device_table[i]->addr, device_table[i]->len,
  50. device_table[i]->blk_size);
  51. offset = 0;
  52. for (j = 0; j < FAL_DEV_BLK_MAX; j ++)
  53. {
  54. const struct flash_blk *blk = &device_table[i]->blocks[j];
  55. rt_size_t blk_len = blk->count * blk->size;
  56. if (blk->count == 0 || blk->size == 0)
  57. break;
  58. if(offset > device_table[i]->len)
  59. {
  60. LOG_I("Flash device %*.*s: add block failed, offset %d > len %d.",
  61. FAL_DEV_NAME_MAX, FAL_DEV_NAME_MAX, device_table[i]->name, device_table[i]->addr, offset, device_table[i]->len);
  62. break;
  63. }
  64. LOG_D(" blk%2d | addr: 0x%08lx | len: 0x%08x | blk_size: 0x%08x |initialized finish.",
  65. j, device_table[i]->addr + offset, blk_len, blk->size);
  66. offset += blk_len;
  67. }
  68. }
  69. init_ok = 1;
  70. return 0;
  71. }
  72. /**
  73. * find flash device by name
  74. *
  75. * @param name flash device name
  76. *
  77. * @return != NULL: flash device
  78. * NULL: not found
  79. */
  80. const struct fal_flash_dev *fal_flash_device_find(const char *name)
  81. {
  82. RT_ASSERT(init_ok);
  83. RT_ASSERT(name);
  84. rt_size_t i;
  85. for (i = 0; i < device_table_len; i++)
  86. {
  87. if (!strncmp(name, device_table[i]->name, FAL_DEV_NAME_MAX)) {
  88. return device_table[i];
  89. }
  90. }
  91. return NULL;
  92. }