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