Skip to content

Commit 11ffe48

Browse files
committed
profile: move bind and vtx settings into profile
1 parent 45f75f3 commit 11ffe48

27 files changed

Lines changed: 422 additions & 536 deletions

src/control/gestures.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ void gestures() {
241241
break;
242242
}
243243
case GESTURE_UUU: {
244-
bind_storage.bind_saved = !bind_storage.bind_saved;
244+
profile.receiver.bind.bind_saved = !profile.receiver.bind.bind_saved;
245245
skip_calib = true;
246246
led_flash();
247247
break;

src/core/flash.c

Lines changed: 16 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ extern const profile_t default_profile;
1616
extern profile_t profile;
1717

1818
flash_storage_t flash_storage;
19-
rx_bind_storage_t bind_storage;
20-
21-
CBOR_START_STRUCT_ENCODER(rx_bind_storage_t)
22-
CBOR_ENCODE_MEMBER(bind_saved, uint8_t)
23-
CBOR_ENCODE_BSTR_MEMBER(raw, BIND_RAW_STORAGE_SIZE)
24-
CBOR_END_STRUCT_ENCODER()
25-
26-
CBOR_START_STRUCT_DECODER(rx_bind_storage_t)
27-
CBOR_DECODE_MEMBER(bind_saved, uint8_t)
28-
CBOR_DECODE_BSTR_MEMBER(raw, BIND_RAW_STORAGE_SIZE)
29-
CBOR_END_STRUCT_DECODER()
3019

3120
static void flash_write_magic(uint8_t *data, uint32_t magic) {
3221
*((uint32_t *)data) = magic;
@@ -39,6 +28,10 @@ static bool flash_compare_magic(uint32_t addr, uint32_t magic) {
3928
void flash_save() {
4029
rx_stop();
4130

31+
if (sizeof(profile_t) > PROFILE_STORAGE_SIZE - FMC_MAGIC_SIZE) {
32+
failloop(FAILLOOP_FAULT);
33+
}
34+
4235
__disable_irq();
4336

4437
uint8_t *buffer = (uint8_t *)malloc(PROFILE_STORAGE_SIZE);
@@ -73,54 +66,19 @@ void flash_save() {
7366
fmc_write_buf(FLASH_STORAGE_OFFSET, buffer, FLASH_STORAGE_SIZE);
7467
}
7568

76-
{
77-
memset(buffer, 0, BIND_STORAGE_SIZE);
78-
flash_write_magic(buffer, FMC_MAGIC | BIND_STORAGE_OFFSET);
79-
80-
if (bind_storage.bind_saved == 0) {
81-
// reset all bind data
82-
memset(bind_storage.raw, 0, BIND_RAW_STORAGE_SIZE);
83-
}
84-
85-
memcpy(buffer + FMC_MAGIC_SIZE, (uint8_t *)&bind_storage, sizeof(rx_bind_storage_t));
86-
fmc_write_buf(BIND_STORAGE_OFFSET, buffer, BIND_STORAGE_SIZE);
87-
}
88-
8969
{
9070
memset(buffer, 0, PROFILE_STORAGE_SIZE);
9171
flash_write_magic(buffer, FMC_MAGIC | PROFILE_STORAGE_OFFSET);
9272

93-
cbor_value_t enc;
94-
cbor_encoder_init(&enc, buffer + FMC_MAGIC_SIZE, PROFILE_STORAGE_SIZE - FMC_MAGIC_SIZE);
95-
96-
cbor_result_t res = cbor_encode_profile_t(&enc, &profile);
97-
if (res < CBOR_OK) {
98-
fmc_lock();
99-
__enable_irq();
100-
failloop(FAILLOOP_FAULT);
73+
if (profile.receiver.bind.bind_saved == 0) {
74+
// reset all bind data
75+
memset(profile.receiver.bind.raw, 0, BIND_RAW_STORAGE_SIZE);
10176
}
10277

78+
memcpy(buffer + FMC_MAGIC_SIZE, (uint8_t *)&profile, sizeof(profile_t));
10379
fmc_write_buf(PROFILE_STORAGE_OFFSET, buffer, PROFILE_STORAGE_SIZE);
10480
}
10581

106-
#ifdef USE_VTX
107-
{
108-
flash_write_magic(buffer, FMC_MAGIC | VTX_STORAGE_OFFSET);
109-
110-
cbor_value_t enc;
111-
cbor_encoder_init(&enc, buffer + FMC_MAGIC_SIZE, VTX_STORAGE_SIZE - FMC_MAGIC_SIZE);
112-
113-
cbor_result_t res = cbor_encode_vtx_settings_t(&enc, &vtx_settings);
114-
if (res < CBOR_OK) {
115-
fmc_lock();
116-
__enable_irq();
117-
failloop(FAILLOOP_FAULT);
118-
}
119-
120-
fmc_write_buf(VTX_STORAGE_OFFSET, buffer, VTX_STORAGE_SIZE);
121-
}
122-
#endif
123-
12482
fmc_lock();
12583
free(buffer);
12684
__enable_irq();
@@ -149,46 +107,21 @@ void flash_load() {
149107
memcpy((uint8_t *)&flash_storage, buffer + FMC_MAGIC_SIZE, sizeof(flash_storage_t));
150108
}
151109

152-
if (flash_compare_magic(BIND_STORAGE_OFFSET, (FMC_MAGIC | BIND_STORAGE_OFFSET))) {
153-
fmc_read_buf(BIND_STORAGE_OFFSET, buffer, BIND_STORAGE_SIZE);
154-
memcpy((uint8_t *)&bind_storage, buffer + FMC_MAGIC_SIZE, sizeof(rx_bind_storage_t));
155-
156-
} else {
157-
#ifdef EXPRESS_LRS_UID
158-
const uint8_t uid[6] = {EXPRESS_LRS_UID};
159-
bind_storage.bind_saved = 1;
160-
161-
bind_storage.elrs.is_set = 0x1;
162-
bind_storage.elrs.magic = 0x37;
163-
memcpy(bind_storage.elrs.uid, uid, 6);
164-
#endif
165-
}
166-
167110
profile_set_defaults();
168111

169112
if (flash_compare_magic(PROFILE_STORAGE_OFFSET, (FMC_MAGIC | PROFILE_STORAGE_OFFSET))) {
170113
fmc_read_buf(PROFILE_STORAGE_OFFSET, buffer, PROFILE_STORAGE_SIZE);
171-
172-
cbor_value_t dec;
173-
cbor_decoder_init(&dec, buffer + FMC_MAGIC_SIZE, PROFILE_STORAGE_SIZE - FMC_MAGIC_SIZE);
174-
175-
cbor_result_t res = cbor_decode_profile_t(&dec, &profile);
176-
if (res < CBOR_OK) {
177-
failloop(FAILLOOP_FAULT);
178-
}
114+
memcpy((uint8_t *)&profile, buffer + FMC_MAGIC_SIZE, sizeof(profile_t));
179115
}
180116

181-
#ifdef USE_VTX
182-
if (flash_compare_magic(VTX_STORAGE_OFFSET, (FMC_MAGIC | VTX_STORAGE_OFFSET))) {
183-
fmc_read_buf(VTX_STORAGE_OFFSET, buffer, VTX_STORAGE_SIZE);
184-
185-
cbor_value_t dec;
186-
cbor_decoder_init(&dec, buffer + FMC_MAGIC_SIZE, VTX_STORAGE_SIZE - FMC_MAGIC_SIZE);
117+
#ifdef EXPRESS_LRS_UID
118+
if (profile.receiver.bind.bind_saved == 0) {
119+
const uint8_t uid[6] = {EXPRESS_LRS_UID};
120+
profile.receiver.bind.bind_saved = 1;
187121

188-
cbor_result_t res = cbor_decode_vtx_settings_t(&dec, &vtx_settings);
189-
if (res < CBOR_OK) {
190-
failloop(FAILLOOP_FAULT);
191-
}
122+
profile.receiver.bind.elrs.is_set = 0x1;
123+
profile.receiver.bind.elrs.magic = 0x37;
124+
memcpy(profile.receiver.bind.elrs.uid, uid, 6);
192125
}
193126
#endif
194127

src/core/flash.h

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#pragma once
22

3-
#include "rx/express_lrs.h"
4-
#include "rx/flysky.h"
5-
#include "rx/frsky.h"
6-
#include "rx/rx.h"
7-
#include "rx/unified_serial.h"
3+
#include "core/profile.h"
4+
#include "rx/rx_spi.h"
85

96
#define FMC_MAGIC 0x12AA0001
107
#define FMC_MAGIC_SIZE 4
@@ -21,32 +18,8 @@ typedef struct {
2118

2219
extern flash_storage_t flash_storage;
2320

24-
#define BIND_STORAGE_OFFSET (FLASH_STORAGE_OFFSET + FLASH_STORAGE_SIZE)
25-
#define BIND_STORAGE_SIZE FLASH_ALIGN(128)
26-
#define BIND_RAW_STORAGE_SIZE 60
27-
28-
typedef struct {
29-
uint8_t bind_saved;
30-
uint8_t _padding[3];
31-
union {
32-
rx_frsky_bind_data_t frsky;
33-
rx_unified_bind_data_t unified;
34-
rx_elrs_bind_data_t elrs;
35-
rx_flysky_bind_data_t flysky;
36-
uint8_t raw[BIND_RAW_STORAGE_SIZE];
37-
};
38-
} rx_bind_storage_t;
39-
40-
extern rx_bind_storage_t bind_storage;
41-
42-
cbor_result_t cbor_encode_rx_bind_storage_t(cbor_value_t *enc, const rx_bind_storage_t *s);
43-
cbor_result_t cbor_decode_rx_bind_storage_t(cbor_value_t *enc, rx_bind_storage_t *s);
44-
45-
#define PROFILE_STORAGE_OFFSET (BIND_STORAGE_OFFSET + BIND_STORAGE_SIZE)
46-
#define PROFILE_STORAGE_SIZE FLASH_ALIGN(4096)
47-
48-
#define VTX_STORAGE_OFFSET (PROFILE_STORAGE_OFFSET + PROFILE_STORAGE_SIZE)
49-
#define VTX_STORAGE_SIZE FLASH_ALIGN(512)
21+
#define PROFILE_STORAGE_OFFSET (FLASH_STORAGE_OFFSET + FLASH_STORAGE_SIZE)
22+
#define PROFILE_STORAGE_SIZE FLASH_ALIGN(4096 + 128 + 512)
5023

5124
void flash_save();
5225
void flash_load();

src/core/profile.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,11 @@ const profile_t default_profile = {
577577
#endif
578578
// rest is initialized by profile_set_defaults()
579579
},
580+
.vtx = {
581+
.power_table = {
582+
.levels = 0,
583+
},
584+
},
580585
.rover = {
581586
.pid = {
582587
.kp = 70.0f,
@@ -677,6 +682,7 @@ cbor_result_t cbor_encode_profile_metadata_t(cbor_value_t *enc, const profile_me
677682
#define ARRAY_MEMBER CBOR_ENCODE_ARRAY_MEMBER
678683
#define COUNT_ARRAY_MEMBER CBOR_ENCODE_COUNT_ARRAY_MEMBER
679684
#define STR_ARRAY_MEMBER CBOR_ENCODE_STR_ARRAY_MEMBER
685+
#define BSTR_MEMBER CBOR_ENCODE_BSTR_MEMBER
680686

681687
RATE_MEMBERS
682688
PROFILE_RATE_MEMBERS
@@ -695,6 +701,7 @@ DTERM_ATTENUATION_MEMBERS
695701
PID_MEMBERS
696702
RX_ROLE_MAP_MEMBERS
697703
AUX_FUNCTION_MAP_MEMBERS
704+
PROFILE_RECEIVER_BIND_MEMBERS
698705
RECEIVER_MEMBERS
699706
BLACKBOX_MEMBERS
700707
BLACKBOX_PRESET_MEMBERS
@@ -712,6 +719,7 @@ PROFILE_MEMBERS
712719
#undef ARRAY_MEMBER
713720
#undef COUNT_ARRAY_MEMBER
714721
#undef STR_ARRAY_MEMBER
722+
#undef BSTR_MEMBER
715723

716724
cbor_result_t cbor_decode_profile_metadata_t(cbor_value_t *dec, profile_metadata_t *meta) {
717725
cbor_result_t res = CBOR_OK;
@@ -759,6 +767,7 @@ cbor_result_t cbor_decode_profile_metadata_t(cbor_value_t *dec, profile_metadata
759767
#define ARRAY_MEMBER CBOR_DECODE_ARRAY_MEMBER
760768
#define COUNT_ARRAY_MEMBER CBOR_DECODE_COUNT_ARRAY_MEMBER
761769
#define STR_ARRAY_MEMBER CBOR_DECODE_STR_ARRAY_MEMBER
770+
#define BSTR_MEMBER CBOR_DECODE_BSTR_MEMBER
762771

763772
RATE_MEMBERS
764773
PROFILE_RATE_MEMBERS
@@ -776,6 +785,7 @@ DTERM_ATTENUATION_MEMBERS
776785
PID_MEMBERS
777786
RX_ROLE_MAP_MEMBERS
778787
AUX_FUNCTION_MAP_MEMBERS
788+
PROFILE_RECEIVER_BIND_MEMBERS
779789
RECEIVER_MEMBERS
780790
BLACKBOX_MEMBERS
781791
ROVER_PID_RATE_MEMBERS
@@ -792,3 +802,4 @@ PROFILE_MEMBERS
792802
#undef ARRAY_MEMBER
793803
#undef COUNT_ARRAY_MEMBER
794804
#undef STR_ARRAY_MEMBER
805+
#undef BSTR_MEMBER

0 commit comments

Comments
 (0)