APM:Libraries
Storage.h
Go to the documentation of this file.
1 /*
2  * This file is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by the
4  * Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This file is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  * Code by Andrew Tridgell and Siddharth Bharat Purohit
16  */
17 #pragma once
18 
19 #include <AP_HAL/AP_HAL.h>
21 #include <AP_Common/Bitmask.h>
23 #include "hwdef/common/flash.h"
24 #include <AP_RAMTRON/AP_RAMTRON.h>
25 
26 #define CH_STORAGE_SIZE HAL_STORAGE_SIZE
27 
28 #ifndef HAL_USE_EMPTY_STORAGE
29 
30 // when using flash storage we use a small line size to make storage
31 // compact and minimise the number of erase cycles needed
32 #define CH_STORAGE_LINE_SHIFT 3
33 
34 #define CH_STORAGE_LINE_SIZE (1<<CH_STORAGE_LINE_SHIFT)
35 #define CH_STORAGE_NUM_LINES (CH_STORAGE_SIZE/CH_STORAGE_LINE_SIZE)
36 
38 public:
39  void init() {}
40  void read_block(void *dst, uint16_t src, size_t n);
41  void write_block(uint16_t dst, const void* src, size_t n);
42 
43  void _timer_tick(void) override;
44 
45 private:
46  volatile bool _initialised;
47  void _storage_create(void);
48  void _storage_open(void);
49  void _mark_dirty(uint16_t loc, uint16_t length);
50  uint8_t _buffer[CH_STORAGE_SIZE] __attribute__((aligned(4)));
52 
53  bool _flash_write_data(uint8_t sector, uint32_t offset, const uint8_t *data, uint16_t length);
54  bool _flash_read_data(uint8_t sector, uint32_t offset, uint8_t *data, uint16_t length);
55  bool _flash_erase_sector(uint8_t sector);
56  bool _flash_erase_ok(void);
57  uint8_t _flash_page;
59  uint32_t _last_re_init_ms;
60 
62  stm32_flash_getpagesize(STORAGE_FLASH_PAGE),
63  FUNCTOR_BIND_MEMBER(&Storage::_flash_write_data, bool, uint8_t, uint32_t, const uint8_t *, uint16_t),
64  FUNCTOR_BIND_MEMBER(&Storage::_flash_read_data, bool, uint8_t, uint32_t, uint8_t *, uint16_t),
67 
68  void _flash_load(void);
69  void _flash_write(uint16_t line);
70 
71 #if HAL_WITH_RAMTRON
72  AP_RAMTRON fram;
73  bool using_fram;
74 #endif
75 };
76 
77 #endif // HAL_USE_EMPTY_STORAGE
void _storage_open(void)
Definition: Storage.cpp:29
void write_block(uint16_t dst, const void *src, size_t n)
Definition: Storage.cpp:81
#define CH_STORAGE_SIZE
Definition: Storage.h:26
volatile bool _initialised
Definition: Storage.h:46
void _mark_dirty(uint16_t loc, uint16_t length)
Definition: Storage.cpp:62
bool _flash_erase_ok(void)
Definition: Storage.cpp:194
uint8_t _flash_page
Definition: Storage.h:57
void _flash_load(void)
Definition: Storage.cpp:129
Bitmask _dirty_mask
Definition: Storage.h:51
void _storage_create(void)
bool _flash_read_data(uint8_t sector, uint32_t offset, uint8_t *data, uint16_t length)
Definition: Storage.cpp:175
AP_FlashStorage _flash
Definition: Storage.h:61
uint32_t stm32_flash_getpagesize(uint32_t page)
Definition: flash.c:180
bool _flash_erase_sector(uint8_t sector)
Definition: Storage.cpp:186
void _timer_tick(void) override
Definition: Storage.cpp:93
bool _flash_failed
Definition: Storage.h:58
void read_block(void *dst, uint16_t src, size_t n)
Definition: Storage.cpp:72
uint8_t _buffer [CH_STORAGE_SIZE] __attribute__((aligned(4)))
#define CH_STORAGE_NUM_LINES
Definition: Storage.h:35
uint32_t _last_re_init_ms
Definition: Storage.h:59
void init()
Definition: Storage.h:39
bool _flash_write_data(uint8_t sector, uint32_t offset, const uint8_t *data, uint16_t length)
Definition: Storage.cpp:154
#define FUNCTOR_BIND_MEMBER(func, rettype,...)
Definition: functor.h:31
void _flash_write(uint16_t line)
Definition: Storage.cpp:143