Skip to content

Commit 56df37a

Browse files
f5sohfilnet
authored andcommitted
Merged in f5soh/librepilot/LP-585_refine_OPLM_channel_generator (pull request #502)
LP-585 refine OPLM channel generator Approved-by: Lalanne Laurent <f5soh@free.fr> Approved-by: Philippe Renon <philippe_renon@yahoo.fr>
2 parents 7953343 + cfd7751 commit 56df37a

2 files changed

Lines changed: 42 additions & 17 deletions

File tree

flight/pios/common/pios_openlrs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,13 +1276,13 @@ static void pios_openlrs_task(void *parameters)
12761276
}
12771277
} else {
12781278
// We timed out because packet was missed
1279-
DEBUG_PRINTF(3, "ISR Timeout. Missed packet: %d %d %d\r\n", delay, getInterval(&openlrs_dev->bind_data), time_since_packet_us);
1279+
// DEBUG_PRINTF(3, "ISR Timeout. Missed packet: %d %d %d\r\n", delay_ms, getInterval(&openlrs_dev->bind_data), time_since_packet_us);
12801280
pios_openlrs_rx_loop(openlrs_dev);
12811281
}
12821282

12831283
rssi_sampled = true;
12841284
} else {
1285-
// DEBUG_PRINTF(3, "ISR %d %d %d\r\n", delay, getInterval(&openlrs_dev->bind_data), time_since_packet_us);
1285+
// DEBUG_PRINTF(3, "ISR %d %d %d\r\n", delay_ms, getInterval(&openlrs_dev->bind_data), time_since_packet_us);
12861286

12871287
// Process incoming data
12881288
pios_openlrs_rx_loop(openlrs_dev);

flight/pios/common/pios_rfm22b.c

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,26 @@ static const uint32_t data_rate[] = {
339339

340340
static const uint8_t channel_spacing[] = {
341341
1, /* 9.6kbps */
342-
2, /* 19.2kps */
343-
2, /* 32kps */
344-
2, /* 57.6kps */
345-
2, /* 64kps */
346-
3, /* 100kps */
347-
4, /* 128kps */
348-
4, /* 192kps */
349-
4, /* 256kps */
342+
2, /* 19.2kbps */
343+
2, /* 32kbps */
344+
2, /* 57.6kbps */
345+
2, /* 64kbps */
346+
3, /* 100kbps */
347+
4, /* 128kbps */
348+
4, /* 192kbps */
349+
5, /* 256kbps */
350+
};
351+
352+
static const uint8_t channel_limits[] = {
353+
1, /* 9.6kbps */
354+
1, /* 19.2kbps */
355+
1, /* 32kbps */
356+
1, /* 57.6kbps */
357+
1, /* 64kbps */
358+
1, /* 100kbps */
359+
2, /* 128kbps */
360+
2, /* 192kbps */
361+
2, /* 256kbps */
350362
};
351363

352364
static const uint8_t reg_1C[] = { 0x01, 0x05, 0x06, 0x95, 0x95, 0x81, 0x88, 0x8B, 0x8D }; // rfm22_if_filter_bandwidth
@@ -2669,9 +2681,16 @@ static void rfm22_hmac_sha1(const uint8_t *data, size_t len,
26692681
static bool rfm22_gen_channels(uint32_t coordid, enum rfm22b_datarate rate, uint8_t min,
26702682
uint8_t max, uint8_t channels[MAX_CHANNELS], uint8_t *clen)
26712683
{
2684+
// Define first and last channel to be used within min/max values
2685+
// according to the frequency deviation, without up/down overflow.
2686+
uint8_t chan_min_limit = min + channel_limits[rate];
2687+
uint8_t chan_max_limit = max - channel_limits[rate];
2688+
2689+
// Define how many channels we can use according to the spacing.
2690+
uint8_t chan_count = ((chan_max_limit - chan_min_limit) / channel_spacing[rate]) + 1;
2691+
26722692
uint32_t data = 0;
26732693
uint8_t cpos = 0;
2674-
uint8_t chan_range = (max / channel_spacing[rate] - min / channel_spacing[rate]) + 1;
26752694
uint8_t key[SHA1_DIGEST_LENGTH] = { 0 };
26762695
uint8_t digest[SHA1_DIGEST_LENGTH];
26772696
uint8_t *all_channels;
@@ -2680,12 +2699,16 @@ static bool rfm22_gen_channels(uint32_t coordid, enum rfm22b_datarate rate, uint
26802699

26812700
memcpy(key, &coordid, sizeof(coordid));
26822701

2683-
for (int i = 0; i < chan_range; i++) {
2684-
all_channels[i] = min / channel_spacing[rate] + i;
2702+
// Fill all_channels[] with usable channels
2703+
for (int i = 0; i < chan_count; i++) {
2704+
all_channels[i] = chan_min_limit + (i * channel_spacing[rate]);
26852705
}
26862706

2707+
// DEBUG_PRINTF(3, "\r\nChannel Min: %d Max:%d - Spacing: %d Limits: %d\r\n", min, max, channel_spacing[rate], channel_limits[rate]);
2708+
// DEBUG_PRINTF(3, "Result: Channel count: %d - Usable channels from ch%d to ch%d\r\n", chan_count, all_channels[0], all_channels[chan_count - 1]);
2709+
26872710
int j = SHA1_DIGEST_LENGTH;
2688-
for (int i = 0; i < chan_range && i < MAX_CHANNELS; i++) {
2711+
for (int i = 0; i < chan_count && i < MAX_CHANNELS; i++) {
26892712
uint8_t rnd;
26902713
uint8_t r;
26912714
uint8_t tmp;
@@ -2697,14 +2720,16 @@ static bool rfm22_gen_channels(uint32_t coordid, enum rfm22b_datarate rate, uint
26972720
}
26982721
rnd = digest[j];
26992722
j++;
2700-
r = rnd % (chan_range - i) + i;
2723+
r = rnd % (chan_count - i) + i;
27012724
tmp = all_channels[i];
27022725
all_channels[i] = all_channels[r];
27032726
all_channels[r] = tmp;
27042727
}
27052728

2706-
for (int i = 0; i < chan_range && cpos < MAX_CHANNELS; i++, cpos++) {
2707-
channels[cpos] = all_channels[i] * channel_spacing[rate];
2729+
// DEBUG_PRINTF(3, "Final channel list:");
2730+
for (int i = 0; i < chan_count && cpos < MAX_CHANNELS; i++, cpos++) {
2731+
channels[cpos] = all_channels[i];
2732+
// DEBUG_PRINTF(3, " %d ", all_channels[i]);
27082733
}
27092734

27102735
*clen = cpos & 0xfe;

0 commit comments

Comments
 (0)