StorageManager.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Please contribute your ideas! See http://dev.ardupilot.org for details
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. Management for hal.storage to allow for backwards compatible mapping
  16. of storage offsets to available storage
  17. */
  18. #pragma once
  19. #include <AP_HAL/AP_HAL.h>
  20. /*
  21. use just one area per storage type for boards with 4k of
  22. storage. Use larger areas for other boards
  23. */
  24. #if HAL_STORAGE_SIZE >= 16384
  25. #define STORAGE_NUM_AREAS 14
  26. #elif HAL_STORAGE_SIZE >= 15360
  27. #define STORAGE_NUM_AREAS 11
  28. #elif HAL_STORAGE_SIZE >= 8192
  29. #define STORAGE_NUM_AREAS 10
  30. #elif HAL_STORAGE_SIZE >= 4096
  31. #define STORAGE_NUM_AREAS 4
  32. #elif HAL_STORAGE_SIZE > 0
  33. #define STORAGE_NUM_AREAS 1
  34. #else
  35. #error "Unsupported storage size"
  36. #endif
  37. /*
  38. The StorageManager holds the layout of non-volatile storeage
  39. */
  40. class StorageManager {
  41. friend class StorageAccess;
  42. public:
  43. enum StorageType {
  44. StorageParam = 0,
  45. StorageFence = 1,
  46. StorageRally = 2,
  47. StorageMission = 3,
  48. StorageKeys = 4,
  49. StorageBindInfo= 5
  50. };
  51. // erase whole of storage
  52. static void erase(void);
  53. // setup for copter layout of storage
  54. static void set_layout_copter(void) { layout = layout_copter; }
  55. private:
  56. struct StorageArea {
  57. StorageType type;
  58. uint16_t offset;
  59. uint16_t length;
  60. };
  61. // available layouts
  62. static const StorageArea layout_copter[STORAGE_NUM_AREAS];
  63. static const StorageArea layout_default[STORAGE_NUM_AREAS];
  64. static const StorageArea *layout;
  65. };
  66. /*
  67. A StorageAccess object allows access to one type of storage
  68. */
  69. class StorageAccess {
  70. public:
  71. // constructor
  72. StorageAccess(StorageManager::StorageType _type);
  73. // return total size of this accessor
  74. uint16_t size(void) const { return total_size; }
  75. // base access via block functions
  76. bool read_block(void *dst, uint16_t src, size_t n) const;
  77. bool write_block(uint16_t dst, const void* src, size_t n) const;
  78. // helper functions
  79. uint8_t read_byte(uint16_t loc) const;
  80. uint16_t read_uint16(uint16_t loc) const;
  81. uint32_t read_uint32(uint16_t loc) const;
  82. void write_byte(uint16_t loc, uint8_t value) const;
  83. void write_uint16(uint16_t loc, uint16_t value) const;
  84. void write_uint32(uint16_t loc, uint32_t value) const;
  85. private:
  86. const StorageManager::StorageType type;
  87. uint16_t total_size;
  88. };