Un-unroll some loops and save a few bytes

This commit is contained in:
Juan Antonio 2023-12-22 18:21:23 +01:00 committed by egzumer
parent 7d982f1f8d
commit bc9bb489e6
3 changed files with 142 additions and 145 deletions

View File

@ -16,6 +16,7 @@
#include "app/spectrum.h"
#include "am_fix.h"
#include "audio.h"
#include "misc.h"
#ifdef ENABLE_SCAN_RANGES
#include "chFrScanner.h"
@ -37,7 +38,6 @@ struct FrequencyBandInfo {
const uint16_t RSSI_MAX_VALUE = 65535;
static uint16_t R30, R37, R3D, R43, R47, R48, R7E;
static uint32_t initialFreq;
static char String[32];
@ -221,24 +221,29 @@ static void ToggleAFBit(bool on) {
BK4819_WriteRegister(BK4819_REG_47, reg);
}
static const BK4819_REGISTER_t registers_to_save[] ={
BK4819_REG_30,
BK4819_REG_37,
BK4819_REG_3D,
BK4819_REG_43,
BK4819_REG_47,
BK4819_REG_48,
BK4819_REG_7E,
};
static uint16_t registers_stack [sizeof(registers_to_save)];
static void BackupRegisters() {
R30 = BK4819_ReadRegister(BK4819_REG_30);
R37 = BK4819_ReadRegister(BK4819_REG_37);
R3D = BK4819_ReadRegister(BK4819_REG_3D);
R43 = BK4819_ReadRegister(BK4819_REG_43);
R47 = BK4819_ReadRegister(BK4819_REG_47);
R48 = BK4819_ReadRegister(BK4819_REG_48);
R7E = BK4819_ReadRegister(BK4819_REG_7E);
for (uint32_t i = 0; i < ARRAY_SIZE(registers_to_save); i++){
registers_stack[i] = BK4819_ReadRegister(registers_to_save[i]);
}
}
static void RestoreRegisters() {
BK4819_WriteRegister(BK4819_REG_30, R30);
BK4819_WriteRegister(BK4819_REG_37, R37);
BK4819_WriteRegister(BK4819_REG_3D, R3D);
BK4819_WriteRegister(BK4819_REG_43, R43);
BK4819_WriteRegister(BK4819_REG_47, R47);
BK4819_WriteRegister(BK4819_REG_48, R48);
BK4819_WriteRegister(BK4819_REG_7E, R7E);
for (uint32_t i = 0; i < ARRAY_SIZE(registers_to_save); i++){
BK4819_WriteRegister(registers_to_save[i], registers_stack[i]);
}
}
static void ToggleAFDAC(bool on) {
@ -272,8 +277,8 @@ bool IsCenterMode() { return settings.scanStepIndex < S_STEP_2_5kHz; }
// scan step in 0.01khz
uint16_t GetScanStep() { return scanStepValues[settings.scanStepIndex]; }
uint16_t GetStepsCount()
{
uint16_t GetStepsCount()
{
#ifdef ENABLE_SCAN_RANGES
if(gScanRangeStart) {
return (gScanRangeStop - gScanRangeStart) / GetScanStep();
@ -425,10 +430,10 @@ static void UpdatePeakInfo() {
static void SetRssiHistory(uint16_t idx, uint16_t rssi)
{
#ifdef ENABLE_SCAN_RANGES
#ifdef ENABLE_SCAN_RANGES
if(scanInfo.measurementsCount > 128) {
uint8_t i = (uint32_t)ARRAY_SIZE(rssiHistory) * 1000 / scanInfo.measurementsCount * idx / 1000;
if(rssiHistory[i] < rssi || isListening)
if(rssiHistory[i] < rssi || isListening)
rssiHistory[i] = rssi;
rssiHistory[(i+1)%128] = 0;
return;
@ -437,8 +442,8 @@ static void SetRssiHistory(uint16_t idx, uint16_t rssi)
rssiHistory[idx] = rssi;
}
static void Measure()
{
static void Measure()
{
uint16_t rssi = scanInfo.rssi = GetRssi();
SetRssiHistory(scanInfo.i, rssi);
}
@ -490,6 +495,7 @@ static void UpdateScanStep(bool inc) {
} else {
return;
}
settings.frequencyChangeStep = GetBW() >> 1;
RelaunchScan();
ResetBlacklist();
@ -846,7 +852,7 @@ static void OnKeyDown(uint8_t key) {
case KEY_5:
#ifdef ENABLE_SCAN_RANGES
if(!gScanRangeStart)
#endif
#endif
FreqInput();
break;
case KEY_0:
@ -1146,7 +1152,7 @@ static void UpdateScan() {
}
if(scanInfo.measurementsCount < 128)
memset(&rssiHistory[scanInfo.measurementsCount], 0,
memset(&rssiHistory[scanInfo.measurementsCount], 0,
sizeof(rssiHistory) - scanInfo.measurementsCount*sizeof(rssiHistory[0]));
redrawScreen = true;

View File

@ -17,6 +17,7 @@
#ifndef BK4819_REGS_H
#define BK4819_REGS_H
#include <stdint.h>
typedef struct {
const char *name;
@ -387,4 +388,3 @@ enum {
};
#endif

View File

@ -14,7 +14,10 @@
* limitations under the License.
*/
#include <stdio.h> // NULL
#include <stdint.h>
#include <stdio.h>
#include "settings.h"
#include "../audio.h"
#include "../bsp/dp32g030/gpio.h"
@ -243,7 +246,7 @@ void BK4819_SetAGC(bool enable)
if(!(regVal & (1 << 15)) == enable)
return;
BK4819_WriteRegister(BK4819_REG_7E, (regVal & ~(1 << 15) & ~(0b111 << 12))
BK4819_WriteRegister(BK4819_REG_7E, (regVal & ~(1 << 15) & ~(0b111 << 12))
| (!enable << 15) // 0 AGC fix mode
| (3u << 12) // 3 AGC fix index
);
@ -272,7 +275,7 @@ void BK4819_InitAGC(bool amModulation)
//
// <15:10> ???
//
// <9:8> LNA Gain Short
// <9:8> LNA Gain Short
// 3 = 0dB <<< 1o11 read from spectrum reference manual
// 2 = -24dB -19 -11
// 1 = -30dB -24 -16
@ -310,12 +313,12 @@ void BK4819_InitAGC(bool amModulation)
BK4819_WriteRegister(BK4819_REG_11, 0x027B); // 0x027B / 000000 10 011 11 011 / -43dB
BK4819_WriteRegister(BK4819_REG_10, 0x007A); // 0x007A / 000000 00 011 11 010 / -58dB
if(amModulation) {
BK4819_WriteRegister(BK4819_REG_14, 0x0000);
BK4819_WriteRegister(BK4819_REG_14, 0x0000);
BK4819_WriteRegister(BK4819_REG_49, (0 << 14) | (50 << 7) | (32 << 0));
}
else{
BK4819_WriteRegister(BK4819_REG_14, 0x0019); // 0x0019 / 000000 00 000 11 001 / -79dB
BK4819_WriteRegister(BK4819_REG_49, (0 << 14) | (84 << 7) | (56 << 0)); //0x2A38 / 00 1010100 0111000 / 84, 56
BK4819_WriteRegister(BK4819_REG_49, (0 << 14) | (84 << 7) | (56 << 0)); //0x2A38 / 00 1010100 0111000 / 84, 56
}
BK4819_WriteRegister(BK4819_REG_7B, 0x8420);
@ -338,7 +341,7 @@ int8_t BK4819_GetRxGain_dB(void)
struct {
uint16_t _ : 5;
uint16_t agcSigStrength : 7;
int16_t gainIdx : 3;
int16_t gainIdx : 3;
uint16_t agcEnab : 1;
};
uint16_t __raw;
@ -605,89 +608,54 @@ void BK4819_SetFilterBandwidth(const BK4819_FilterBandwidth_t Bandwidth, const b
//
// <1:0> 0 ???
uint16_t val;
uint16_t val = 0;
switch (Bandwidth)
{
default:
case BK4819_FILTER_BW_WIDE: // 25kHz
if (weak_no_different)
{ // make the RX bandwidth the same with weak signals
val =
(0u << 15) | // 0
(4u << 12) | // *3 RF filter bandwidth
(4u << 9) | // *0 RF filter bandwidth when signal is weak
(6u << 6) | // *0 AFTxLPF2 filter Band Width
(2u << 4) | // 2 BW Mode Selection
(1u << 3) | // 1
(0u << 2) | // 0 Gain after FM Demodulation
(0u << 0); // 0
}
else
{ // with weak RX signals the RX bandwidth is reduced
val = // 0x3028); // 0 011 000 000 10 1 0 00
(0u << 15) | // 0
(4u << 12) | // *3 RF filter bandwidth
(2u << 9) | // *0 RF filter bandwidth when signal is weak
(6u << 6) | // *0 AFTxLPF2 filter Band Width
(2u << 4) | // 2 BW Mode Selection
(1u << 3) | // 1
(0u << 2) | // 0 Gain after FM Demodulation
(0u << 0); // 0
val = (4u << 12) | // *3 RF filter bandwidth
(6u << 6) | // *0 AFTxLPF2 filter Band Width
(2u << 4) | // 2 BW Mode Selection
(1u << 3) | // 1
(0u << 2); // 0 Gain after FM Demodulation
if (weak_no_different) {
// make the RX bandwidth the same with weak signals
val |= (4u << 9); // *0 RF filter bandwidth when signal is weak
} else {
/// with weak RX signals the RX bandwidth is reduced
val |= (2u << 9); // *0 RF filter bandwidth when signal is weak
}
break;
case BK4819_FILTER_BW_NARROW: // 12.5kHz
if (weak_no_different)
{
val =
(0u << 15) | // 0
(4u << 12) | // *4 RF filter bandwidth
(4u << 9) | // *0 RF filter bandwidth when signal is weak
(0u << 6) | // *1 AFTxLPF2 filter Band Width
(0u << 4) | // 0 BW Mode Selection
(1u << 3) | // 1
(0u << 2) | // 0 Gain after FM Demodulation
(0u << 0); // 0
}
else
{
val = // 0x4048); // 0 100 000 001 00 1 0 00
(0u << 15) | // 0
(4u << 12) | // *4 RF filter bandwidth
(2u << 9) | // *0 RF filter bandwidth when signal is weak
(0u << 6) | // *1 AFTxLPF2 filter Band Width
(0u << 4) | // 0 BW Mode Selection
(1u << 3) | // 1
(0u << 2) | // 0 Gain after FM Demodulation
(0u << 0); // 0
val = (4u << 12) | // *4 RF filter bandwidth
(0u << 6) | // *1 AFTxLPF2 filter Band Width
(0u << 4) | // 0 BW Mode Selection
(1u << 3) | // 1
(0u << 2); // 0 Gain after FM Demodulation
if (weak_no_different) {
val |= (4u << 9); // *0 RF filter bandwidth when signal is weak
} else {
val |= (2u << 9);
}
break;
case BK4819_FILTER_BW_NARROWER: // 6.25kHz
if (weak_no_different)
{
val =
(0u << 15) | // 0
(3u << 12) | // 3 RF filter bandwidth
(3u << 9) | // *0 RF filter bandwidth when signal is weak
(1u << 6) | // 1 AFTxLPF2 filter Band Width
(1u << 4) | // 1 BW Mode Selection
(1u << 3) | // 1
(0u << 2) | // 0 Gain after FM Demodulation
(0u << 0); // 0
}
else
{
val =
(0u << 15) | // 0
(3u << 12) | // 3 RF filter bandwidth
(0u << 9) | // 0 RF filter bandwidth when signal is weak
(1u << 6) | // 1 AFTxLPF2 filter Band Width
(1u << 4) | // 1 BW Mode Selection
(1u << 3) | // 1
(0u << 2) | // 1 Gain after FM Demodulation
(0u << 0); // 0
val = (3u << 12) | // 3 RF filter bandwidth
(3u << 9) | // *0 RF filter bandwidth when signal is weak
(1u << 6) | // 1 AFTxLPF2 filter Band Width
(1u << 4) | // 1 BW Mode Selection
(1u << 3) | // 1
(0u << 2); // 0 Gain after FM Demodulation
if (weak_no_different) {
val |= (3u << 9);
} else {
val |= (0u << 9); // 0 RF filter bandwidth when signal is weak
}
break;
}
@ -843,7 +811,7 @@ void BK4819_RX_TurnOn(void)
BK4819_WriteRegister(BK4819_REG_30, 0);
BK4819_WriteRegister(BK4819_REG_30,
BK4819_WriteRegister(BK4819_REG_30,
BK4819_REG_30_ENABLE_VCO_CALIB |
BK4819_REG_30_DISABLE_UNKNOWN |
BK4819_REG_30_ENABLE_RX_LINK |
@ -1024,7 +992,7 @@ void BK4819_PlayTone(uint16_t Frequency, bool bTuningGainSwitch)
void BK4819_PlaySingleTone(const unsigned int tone_Hz, const unsigned int delay, const unsigned int level, const bool play_speaker)
{
BK4819_EnterTxMute();
if (play_speaker)
{
AUDIO_AudioPathOn();
@ -1033,7 +1001,7 @@ void BK4819_PlaySingleTone(const unsigned int tone_Hz, const unsigned int delay,
else
BK4819_SetAF(BK4819_AF_MUTE);
BK4819_WriteRegister(BK4819_REG_70, BK4819_REG_70_ENABLE_TONE1 | ((level & 0x7f) << BK4819_REG_70_SHIFT_TONE1_TUNING_GAIN));
BK4819_EnableTXLink();
@ -1050,7 +1018,7 @@ void BK4819_PlaySingleTone(const unsigned int tone_Hz, const unsigned int delay,
AUDIO_AudioPathOff();
BK4819_SetAF(BK4819_AF_MUTE);
}
BK4819_WriteRegister(BK4819_REG_70, 0x0000);
BK4819_WriteRegister(BK4819_REG_30, 0xC1FE);
BK4819_ExitTxMute();
@ -1150,7 +1118,7 @@ void BK4819_ExitBypass(void)
uint16_t regVal = BK4819_ReadRegister(BK4819_REG_7E);
// 0x302E / 0 011 000000 101 110
BK4819_WriteRegister(BK4819_REG_7E, (regVal & ~(0b111 << 3))
BK4819_WriteRegister(BK4819_REG_7E, (regVal & ~(0b111 << 3))
| (5u << 3) // 5 DC Filter band width for Tx (MIC In)
@ -1266,33 +1234,46 @@ void BK4819_EnableTXLink(void)
void BK4819_PlayDTMF(char Code)
{
uint16_t tone1 = 0;
uint16_t tone2 = 0;
struct DTMF_TonePair {
uint16_t tone1;
uint16_t tone2;
};
const struct DTMF_TonePair tones[] = {
{941, 1336},
{697, 1209},
{697, 1336},
{697, 1477},
{770, 1209},
{770, 1336},
{770, 1477},
{852, 1209},
{852, 1336},
{852, 1477},
{697, 1633},
{770, 1633},
{852, 1633},
{941, 1633},
{941, 1209},
{941, 1477},
};
const struct DTMF_TonePair *pSelectedTone = NULL;
switch (Code)
{
case '0': tone1 = 941; tone2 = 1336; break;
case '1': tone1 = 697; tone2 = 1209; break;
case '2': tone1 = 697; tone2 = 1336; break;
case '3': tone1 = 697; tone2 = 1477; break;
case '4': tone1 = 770; tone2 = 1209; break;
case '5': tone1 = 770; tone2 = 1336; break;
case '6': tone1 = 770; tone2 = 1477; break;
case '7': tone1 = 852; tone2 = 1209; break;
case '8': tone1 = 852; tone2 = 1336; break;
case '9': tone1 = 852; tone2 = 1477; break;
case 'A': tone1 = 697; tone2 = 1633; break;
case 'B': tone1 = 770; tone2 = 1633; break;
case 'C': tone1 = 852; tone2 = 1633; break;
case 'D': tone1 = 941; tone2 = 1633; break;
case '*': tone1 = 941; tone2 = 1209; break;
case '#': tone1 = 941; tone2 = 1477; break;
case '0'...'9': pSelectedTone = &tones[0 + Code - '0']; break;
case 'A'...'D': pSelectedTone = &tones[10 + Code - 'A']; break;
case '*': pSelectedTone = &tones[14]; break;
case '#': pSelectedTone = &tones[15]; break;
default: pSelectedTone = NULL;
}
if (tone1 > 0)
BK4819_WriteRegister(BK4819_REG_71, (((uint32_t)tone1 * 103244) + 5000) / 10000); // with rounding
if (tone2 > 0)
BK4819_WriteRegister(BK4819_REG_72, (((uint32_t)tone2 * 103244) + 5000) / 10000); // with rounding
if (pSelectedTone) {
BK4819_WriteRegister(BK4819_REG_71, (((uint32_t)pSelectedTone->tone1 * 103244) + 5000) / 10000); // with rounding
BK4819_WriteRegister(BK4819_REG_72, (((uint32_t)pSelectedTone->tone2 * 103244) + 5000) / 10000); // with rounding
}
}
void BK4819_PlayDTMFString(const char *pString, bool bDelayFirst, uint16_t FirstCodePersistTime, uint16_t HashCodePersistTime, uint16_t CodePersistTime, uint16_t CodeInternalTime)
@ -1707,7 +1688,7 @@ void BK4819_PrepareFSKReceive(void)
BK4819_WriteRegister(BK4819_REG_59, 0x3068);
}
void BK4819_PlayRoger(void)
void BK4819_PlayRogerNormal(void)
{
#if 0
const uint32_t tone1_Hz = 500;
@ -1744,28 +1725,38 @@ void BK4819_PlayRoger(void)
void BK4819_PlayRogerMDC(void)
{
unsigned int i;
struct reg_value {
BK4819_REGISTER_t reg;
uint16_t value;
};
struct reg_value RogerMDC_Configuration [] = {
{ BK4819_REG_58, 0x37C3 }, // FSK Enable,
// RX Bandwidth FFSK 1200/1800
// 0xAA or 0x55 Preamble
// 11 RX Gain,
// 101 RX Mode
// TX FFSK 1200/1800
{ BK4819_REG_72, 0x3065 }, // Set Tone-2 to 1200Hz
{ BK4819_REG_70, 0x00E0 }, // Enable Tone-2 and Set Tone2 Gain
{ BK4819_REG_5D, 0x0D00 }, // Set FSK data length to 13 bytes
{ BK4819_REG_59, 0x8068 }, // 4 byte sync length, 6 byte preamble, clear TX FIFO
{ BK4819_REG_59, 0x0068 }, // Same, but clear TX FIFO is now unset (clearing done)
{ BK4819_REG_5A, 0x5555 }, // First two sync bytes
{ BK4819_REG_5B, 0x55AA }, // End of sync bytes. Total 4 bytes: 555555aa
{ BK4819_REG_5C, 0xAA30 }, // Disable CRC
};
BK4819_SetAF(BK4819_AF_MUTE);
BK4819_WriteRegister(BK4819_REG_58, 0x37C3); // FSK Enable,
// RX Bandwidth FFSK 1200/1800
// 0xAA or 0x55 Preamble
// 11 RX Gain,
// 101 RX Mode
// TX FFSK 1200/1800
BK4819_WriteRegister(BK4819_REG_72, 0x3065); // Set Tone-2 to 1200Hz
BK4819_WriteRegister(BK4819_REG_70, 0x00E0); // Enable Tone-2 and Set Tone2 Gain
BK4819_WriteRegister(BK4819_REG_5D, 0x0D00); // Set FSK data length to 13 bytes
BK4819_WriteRegister(BK4819_REG_59, 0x8068); // 4 byte sync length, 6 byte preamble, clear TX FIFO
BK4819_WriteRegister(BK4819_REG_59, 0x0068); // Same, but clear TX FIFO is now unset (clearing done)
BK4819_WriteRegister(BK4819_REG_5A, 0x5555); // First two sync bytes
BK4819_WriteRegister(BK4819_REG_5B, 0x55AA); // End of sync bytes. Total 4 bytes: 555555aa
BK4819_WriteRegister(BK4819_REG_5C, 0xAA30); // Disable CRC
for (unsigned int i = 0; i < ARRAY_SIZE(RogerMDC_Configuration); i++) {
BK4819_WriteRegister(RogerMDC_Configuration[i].reg, RogerMDC_Configuration[i].value);
}
// Send the data from the roger table
for (i = 0; i < 7; i++)
for (unsigned int i = 0; i < ARRAY_SIZE(FSK_RogerTable); i++) {
BK4819_WriteRegister(BK4819_REG_5F, FSK_RogerTable[i]);
}
SYSTEM_DelayMs(20);