Line data Source code
1 : #pragma once
2 :
3 : #include <AP_HAL/AP_HAL.h>
4 : #include <AP_Common/Bitmask.h>
5 : #include "AP_HAL_SITL_Namespace.h"
6 : #include <AP_FlashStorage/AP_FlashStorage.h>
7 : #include <AP_RAMTRON/AP_RAMTRON.h>
8 :
9 : #ifndef STORAGE_USE_FLASH
10 : #define STORAGE_USE_FLASH 1
11 : #endif
12 :
13 : #ifndef STORAGE_USE_POSIX
14 : #define STORAGE_USE_POSIX 1
15 : #endif
16 :
17 : #ifndef STORAGE_USE_FRAM
18 : #define STORAGE_USE_FRAM HAL_WITH_RAMTRON
19 : #endif
20 :
21 : #define STORAGE_LINE_SHIFT 3
22 :
23 : #define STORAGE_LINE_SIZE (1<<STORAGE_LINE_SHIFT)
24 : #define STORAGE_NUM_LINES (HAL_STORAGE_SIZE/STORAGE_LINE_SIZE)
25 :
26 : class HALSITL::Storage : public AP_HAL::Storage {
27 : public:
28 0 : void init() override {}
29 : void read_block(void *dst, uint16_t src, size_t n) override;
30 : void write_block(uint16_t dst, const void* src, size_t n) override;
31 : bool get_storage_ptr(void *&ptr, size_t &size) override;
32 :
33 : void _timer_tick(void) override;
34 : bool healthy(void) override;
35 :
36 : private:
37 : enum class StorageBackend: uint8_t {
38 : None,
39 : FRAM,
40 : Flash,
41 : SDCard, // AKA POSIX
42 : };
43 : StorageBackend _initialisedType = StorageBackend::None;
44 :
45 : void _storage_create(void);
46 : void _storage_open(void);
47 : void _save_backup(void);
48 : void _mark_dirty(uint16_t loc, uint16_t length);
49 : uint8_t _buffer[HAL_STORAGE_SIZE] __attribute__((aligned(4)));
50 : Bitmask<STORAGE_NUM_LINES> _dirty_mask;
51 :
52 : uint32_t _last_empty_ms;
53 :
54 : #if STORAGE_USE_FLASH
55 : bool _flash_write_data(uint8_t sector, uint32_t offset, const uint8_t *data, uint16_t length);
56 : bool _flash_read_data(uint8_t sector, uint32_t offset, uint8_t *data, uint16_t length);
57 : bool _flash_erase_sector(uint8_t sector);
58 : bool _flash_erase_ok(void);
59 :
60 : bool _flash_failed;
61 : uint32_t _last_re_init_ms;
62 :
63 : AP_FlashStorage _flash{_buffer,
64 : HAL_FLASH_SECTOR_SIZE,
65 : FUNCTOR_BIND_MEMBER(&Storage::_flash_write_data, bool, uint8_t, uint32_t, const uint8_t *, uint16_t),
66 : FUNCTOR_BIND_MEMBER(&Storage::_flash_read_data, bool, uint8_t, uint32_t, uint8_t *, uint16_t),
67 : FUNCTOR_BIND_MEMBER(&Storage::_flash_erase_sector, bool, uint8_t),
68 : FUNCTOR_BIND_MEMBER(&Storage::_flash_erase_ok, bool)};
69 :
70 : void _flash_load(void);
71 : void _flash_write(uint16_t line);
72 : #endif
73 :
74 : #if STORAGE_USE_POSIX
75 : int log_fd;
76 : #endif
77 :
78 : #if STORAGE_USE_FRAM
79 : AP_RAMTRON fram;
80 : #endif
81 : };
|