[4.4.2] Remove 15 m/s limit on estimated vario (#12788)
[betaflight.git] / src / main / pg / pg.c
blob1da2fafc49f01be8d65e5a951a4ddc29eee65858
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdint.h>
25 #include "platform.h"
27 #include "common/crc.h"
28 #include "common/maths.h"
30 #include "pg.h"
32 const pgRegistry_t* pgFind(pgn_t pgn)
34 PG_FOREACH(reg) {
35 if (pgN(reg) == pgn) {
36 return reg;
39 return NULL;
42 static uint8_t *pgOffset(const pgRegistry_t* reg)
44 return reg->address;
47 void pgResetInstance(const pgRegistry_t *reg, uint8_t *base)
49 const uint16_t regSize = pgSize(reg);
51 memset(base, 0, regSize);
52 if (reg->reset.ptr >= (void*)__pg_resetdata_start && reg->reset.ptr < (void*)__pg_resetdata_end) {
53 // pointer points to resetdata section, to it is data template
54 memcpy(base, reg->reset.ptr, regSize);
55 } else if (reg->reset.fn) {
56 // reset function, call it
57 reg->reset.fn(base);
61 void pgReset(const pgRegistry_t* reg)
63 pgResetInstance(reg, pgOffset(reg));
66 bool pgResetCopy(void *copy, pgn_t pgn)
68 const pgRegistry_t *reg = pgFind(pgn);
69 if (reg) {
70 pgResetInstance(reg, copy);
71 return true;
73 return false;
76 bool pgLoad(const pgRegistry_t* reg, const void *from, int size, int version)
78 pgResetInstance(reg, pgOffset(reg));
79 // restore only matching version, keep defaults otherwise
80 if (version == pgVersion(reg)) {
81 const int take = MIN(size, pgSize(reg));
82 memcpy(pgOffset(reg), from, take);
84 *reg->fnv_hash = fnv_update(FNV_OFFSET_BASIS, from, take);
86 return true;
89 return false;
92 int pgStore(const pgRegistry_t* reg, void *to, int size)
94 const int take = MIN(size, pgSize(reg));
95 memcpy(to, pgOffset(reg), take);
96 return take;
99 void pgResetAll(void)
101 PG_FOREACH(reg) {
102 pgReset(reg);