Skip to content

Commit

Permalink
AIRCOPY update
Browse files Browse the repository at this point in the history
  • Loading branch information
OneOfEleven authored and OneOfEleven committed Oct 12, 2023
1 parent 571c722 commit 17deb5a
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 131 deletions.
17 changes: 9 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENABLE_SWD := 0
ENABLE_OVERLAY := 0
ENABLE_LTO := 1
ENABLE_UART := 1
ENABLE_UART_DEBUG := 1
ENABLE_UART_DEBUG := 0
ENABLE_AIRCOPY := 1
ENABLE_AIRCOPY_FREQ := 1
ENABLE_FMRADIO := 1
Expand Down Expand Up @@ -48,6 +48,14 @@ ENABLE_COPY_CHAN_TO_VFO := 1

TARGET = firmware

GIT_HASH_TMP := $(shell git rev-parse --short HEAD)
ifeq ($(GIT_HASH_TMP),)
GIT_HASH := "NOGIT"
else
GIT_HASH := $(GIT_HASH_TMP)
endif
$(info GIT_HASH = $(GIT_HASH))

ifeq ($(ENABLE_UART), 0)
ENABLE_UART_DEBUG := 0
endif
Expand Down Expand Up @@ -195,13 +203,6 @@ endif
OBJCOPY = arm-none-eabi-objcopy
SIZE = arm-none-eabi-size

# the user might not have/want git installed
# can set own version string here (max 7 chars)
GIT_HASH := $(shell git rev-parse --short HEAD)
#GIT_HASH := 230930b

$(info GIT_HASH = $(GIT_HASH))

ASFLAGS = -c -mcpu=cortex-m0
ifeq ($(ENABLE_OVERLAY),1)
ASFLAGS += -DENABLE_OVERLAY
Expand Down
138 changes: 86 additions & 52 deletions app/aircopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,95 +27,128 @@
#include "ui/inputbox.h"
#include "ui/ui.h"

static const uint16_t Obfuscation[8] = {0x6C16, 0xE614, 0x912E, 0x400D, 0x3521, 0x40D5, 0x0313, 0x80E9};
#define AIRCOPY_MAGIC_START 0xABCD
#define AIRCOPY_MAGIC_END 0xDCBA

const uint8_t g_air_copy_block_max = 120;
uint8_t g_air_copy_block_number;
uint8_t g_errors_during_air_copy;
#define AIRCOPY_LAST_EEPROM_ADDR 0x1E00

static const uint16_t Obfuscation[] = {
0x6C16, 0xE614, 0x912E, 0x400D, 0x3521, 0x40D5, 0x0313, 0x80E9
};

const uint8_t g_aircopy_block_max = 120;
uint8_t g_aircopy_block_number;
uint8_t g_aircopy_rx_errors;
aircopy_state_t g_aircopy_state;
uint16_t g_fsk_buffer[36];
uint16_t g_aircopy_fsk_buffer[36];
uint8_t g_aircopy_send_count_down_10ms;
unsigned int g_aircopy_fsk_write_index;

void AIRCOPY_SendMessage(void)
{
unsigned int i;
unsigned int i;
const uint16_t eeprom_addr = (uint16_t)g_aircopy_block_number * 64;

// *********

g_fsk_buffer[1] = (g_air_copy_block_number & 0x3FF) << 6;
// packet start
g_aircopy_fsk_buffer[0] = AIRCOPY_MAGIC_START;

EEPROM_ReadBuffer(g_fsk_buffer[1], &g_fsk_buffer[2], 64);
// eeprom address
g_aircopy_fsk_buffer[1] = eeprom_addr;

g_fsk_buffer[34] = CRC_Calculate(&g_fsk_buffer[1], 2 + 64);
// data
EEPROM_ReadBuffer(eeprom_addr, &g_aircopy_fsk_buffer[2], 64);

for (i = 0; i < 34; i++)
g_fsk_buffer[i + 1] ^= Obfuscation[i % 8];
// data CRC
g_aircopy_fsk_buffer[34] = CRC_Calculate(&g_aircopy_fsk_buffer[1], 2 + 64);

if (++g_air_copy_block_number >= g_air_copy_block_max)
{
g_aircopy_state = AIRCOPY_TX_COMPLETE;
g_update_display = true;
}
// packet end
g_aircopy_fsk_buffer[35] = AIRCOPY_MAGIC_END;

// *********

// scramble the packet
for (i = 0; i < 34; i++)
g_aircopy_fsk_buffer[1 + i] ^= Obfuscation[i % ARRAY_SIZE(Obfuscation)];

RADIO_SetTxParameters();

BK4819_SendFSKData(g_fsk_buffer);
BK4819_SendFSKData(g_aircopy_fsk_buffer);
BK4819_SetupPowerAmplifier(0, 0);
BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1, false);

g_air_copy_send_count_down = 30;
if (++g_aircopy_block_number >= g_aircopy_block_max)
{
g_aircopy_state = AIRCOPY_TX_COMPLETE;
g_update_display = true;
}

// TX pause/gap time
#if 0
g_aircopy_send_count_down_10ms = 300 / 10; // 300ms
#else
g_aircopy_send_count_down_10ms = 30 / 10; // 30ms
#endif
}

void AIRCOPY_StorePacket(void)
{
uint16_t Status;

if (g_fsk_wite_index < 36)
if (g_aircopy_fsk_write_index < ARRAY_SIZE(g_aircopy_fsk_buffer))
return;

g_fsk_wite_index = 0;
g_update_display = true;
g_aircopy_fsk_write_index = 0;
g_update_display = true;

Status = BK4819_ReadRegister(BK4819_REG_0B);

BK4819_PrepareFSKReceive();

// Doc says bit 4 should be 1 = CRC OK, 0 = CRC FAIL, but original firmware checks for FAIL.
// Doc says bit 4 should be 1 = CRC OK, 0 = CRC FAIL, but original firmware checks for FAIL

if ((Status & 0x0010U) == 0 && g_fsk_buffer[0] == 0xABCD && g_fsk_buffer[35] == 0xDCBA)
if ((Status & (1u << 4)) == 0 && g_aircopy_fsk_buffer[0] == AIRCOPY_MAGIC_START && g_aircopy_fsk_buffer[35] == AIRCOPY_MAGIC_END)
{
uint16_t CRC;
unsigned int i;

for (i = 0; i < 34; i++)
g_fsk_buffer[i + 1] ^= Obfuscation[i % 8];
g_aircopy_fsk_buffer[1 + i] ^= Obfuscation[i % ARRAY_SIZE(Obfuscation)];

CRC = CRC_Calculate(&g_fsk_buffer[1], 2 + 64);
CRC = CRC_Calculate(&g_aircopy_fsk_buffer[1], 2 + 64);

if (g_fsk_buffer[34] == CRC)
if (g_aircopy_fsk_buffer[34] == CRC)
{
const uint16_t *pData;
uint16_t Offset = g_fsk_buffer[1];
if (Offset < 0x1E00)
uint16_t eeprom_addr = g_aircopy_fsk_buffer[1];

if (eeprom_addr < AIRCOPY_LAST_EEPROM_ADDR)
{
pData = &g_fsk_buffer[2];
const uint16_t *pData = &g_aircopy_fsk_buffer[2];

for (i = 0; i < 8; i++)
{
EEPROM_WriteBuffer(Offset, pData);
pData += 4;
Offset += 8;
EEPROM_WriteBuffer(eeprom_addr, pData);
pData += 4;
eeprom_addr += 8;
}

if (Offset == 0x1E00)
{
//g_aircopy_block_number++;
g_aircopy_block_number = eeprom_addr / 64;

if (eeprom_addr >= AIRCOPY_LAST_EEPROM_ADDR)
{ // reached end of eeprom config area

g_aircopy_state = AIRCOPY_RX_COMPLETE;
g_update_display = true;
}

g_air_copy_block_number++;
return;
}
}
}

g_errors_during_air_copy++;
g_aircopy_rx_errors++;
}

static void AIRCOPY_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
Expand Down Expand Up @@ -154,7 +187,7 @@ static void AIRCOPY_Key_DIGITS(key_code_t Key, bool key_pressed, bool key_held)
// round the frequency to nearest step size
Frequency = ((Frequency + (g_rx_vfo->step_freq / 2)) / g_rx_vfo->step_freq) * g_rx_vfo->step_freq;

g_air_copy_freq = Frequency;
g_aircopy_freq = Frequency;
#ifdef ENABLE_AIRCOPY_FREQ
SETTINGS_SaveSettings(); // remeber the frequency for the next time
#endif
Expand Down Expand Up @@ -188,14 +221,14 @@ static void AIRCOPY_Key_EXIT(bool key_pressed, bool key_held)
else
{ // enter RX mode

g_aircopy_state = AIRCOPY_RX;
g_update_display = true;
g_aircopy_state = AIRCOPY_RX;
g_update_display = true;
GUI_DisplayScreen();

g_fsk_wite_index = 0;
g_air_copy_block_number = 0;
g_errors_during_air_copy = 0;
g_input_box_index = 0;
g_aircopy_fsk_write_index = 0;
g_aircopy_block_number = 0;
g_aircopy_rx_errors = 0;
g_input_box_index = 0;

BK4819_PrepareFSKReceive();
}
Expand All @@ -209,16 +242,17 @@ static void AIRCOPY_Key_MENU(bool key_pressed, bool key_held)
if (!key_held && key_pressed)
{ // enter TX mode

g_aircopy_state = AIRCOPY_TX;
g_update_display = true;
g_aircopy_state = AIRCOPY_TX;
g_update_display = true;
GUI_DisplayScreen();

g_fsk_wite_index = 0;
g_air_copy_block_number = 0;
g_input_box_index = 0;
g_fsk_buffer[0] = 0xABCD;
g_fsk_buffer[1] = 0;
g_fsk_buffer[35] = 0xDCBA;
g_input_box_index = 0;

g_aircopy_fsk_write_index = 0;
g_aircopy_block_number = 0;
g_aircopy_fsk_buffer[0] = AIRCOPY_MAGIC_START;
g_aircopy_fsk_buffer[1] = 0; // block number
g_aircopy_fsk_buffer[35] = AIRCOPY_MAGIC_END;

AIRCOPY_SendMessage();
}
Expand Down
17 changes: 7 additions & 10 deletions app/aircopy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#ifndef APP_AIRCOPY_H
#define APP_AIRCOPY_H

#ifdef ENABLE_AIRCOPY

#include "driver/keyboard.h"

enum aircopy_state_e
Expand All @@ -31,17 +29,16 @@ enum aircopy_state_e
};
typedef enum aircopy_state_e aircopy_state_t;

extern const uint8_t g_air_copy_block_max;
extern uint8_t g_air_copy_block_number;
extern uint8_t g_errors_during_air_copy;
extern const uint8_t g_aircopy_block_max;
extern uint8_t g_aircopy_block_number;
extern uint8_t g_aircopy_rx_errors;
extern aircopy_state_t g_aircopy_state;
extern uint16_t g_fsk_buffer[36];
extern uint16_t g_aircopy_fsk_buffer[36];
extern uint8_t g_aircopy_send_count_down_10ms;
extern unsigned int g_aircopy_fsk_write_index;

void AIRCOPY_SendMessage(void);
void AIRCOPY_StorePacket(void);
void AIRCOPY_ProcessKeys(key_code_t Key, bool bKeyPressed, bool bKeyHeld);

#endif
void AIRCOPY_ProcessKeys(key_code_t key, bool key_pressed, bool key_held);

#endif

47 changes: 25 additions & 22 deletions app/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,14 +926,15 @@ void APP_CheckRadioInterrupts(void)
}

#ifdef ENABLE_AIRCOPY
if (interrupt_status_bits & BK4819_REG_02_FSK_FIFO_ALMOST_FULL &&
g_screen_to_display == DISPLAY_AIRCOPY &&
g_aircopy_state == AIRCOPY_RX)
if (interrupt_status_bits & BK4819_REG_02_FSK_FIFO_ALMOST_FULL)
{
unsigned int i;
for (i = 0; i < 4; i++)
g_fsk_buffer[g_fsk_wite_index++] = BK4819_ReadRegister(BK4819_REG_5F);
AIRCOPY_StorePacket();
if (g_screen_to_display == DISPLAY_AIRCOPY && g_aircopy_state == AIRCOPY_RX)
{
unsigned int i;
for (i = 0; i < 4; i++)
g_aircopy_fsk_buffer[g_aircopy_fsk_write_index++] = BK4819_ReadRegister(BK4819_REG_5F);
AIRCOPY_StorePacket();
}
}
#endif
}
Expand Down Expand Up @@ -1324,7 +1325,8 @@ void APP_CheckKeys(void)
key_code_t key;

#ifdef ENABLE_AIRCOPY
if (g_setting_killed || (g_screen_to_display == DISPLAY_AIRCOPY && g_aircopy_state != AIRCOPY_READY))
if (g_setting_killed ||
(g_screen_to_display == DISPLAY_AIRCOPY && g_aircopy_state != AIRCOPY_READY))
return;
#else
if (g_setting_killed)
Expand Down Expand Up @@ -1552,6 +1554,21 @@ void APP_TimeSlice10ms(void)
if (g_reduced_service)
return;


#ifdef ENABLE_AIRCOPY
if (g_screen_to_display == DISPLAY_AIRCOPY && g_aircopy_state == AIRCOPY_TX)
{
if (g_aircopy_send_count_down_10ms > 0)
{
if (--g_aircopy_send_count_down_10ms == 0)
{
AIRCOPY_SendMessage();
GUI_DisplayScreen();
}
}
}
#endif

#ifdef ENABLE_AM_FIX
// if (g_eeprom.vfo_info[g_eeprom.rx_vfo].am_mode && g_setting_am_fix)
if (g_rx_vfo->am_mode && g_setting_am_fix)
Expand Down Expand Up @@ -1831,20 +1848,6 @@ void APP_TimeSlice10ms(void)
}
}

#ifdef ENABLE_AIRCOPY
if (g_screen_to_display == DISPLAY_AIRCOPY && g_aircopy_state == AIRCOPY_TX)
{
if (g_air_copy_send_count_down > 0)
{
if (--g_air_copy_send_count_down == 0)
{
AIRCOPY_SendMessage();
GUI_DisplayScreen();
}
}
}
#endif

APP_CheckKeys();
}

Expand Down
2 changes: 1 addition & 1 deletion board.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ void BOARD_EEPROM_load(void)
{
if (array.air_copy_freq >= FREQ_BAND_TABLE[i].lower && array.air_copy_freq < FREQ_BAND_TABLE[i].upper)
{
g_air_copy_freq = array.air_copy_freq;
g_aircopy_freq = array.air_copy_freq;
break;
}
}
Expand Down
Binary file modified firmware.bin
Binary file not shown.
Binary file modified firmware.packed.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion frequencies.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "settings.h"

// the initial AIRCOPY frequency to use
uint32_t g_air_copy_freq = 41002500;
uint32_t g_aircopy_freq = 41002500;

// the BK4819 has 2 bands it covers, 18MHz ~ 630MHz and 760MHz ~ 1300MHz
const freq_band_table_t BX4819_band1 = { 1800000, 63000000};
Expand Down
2 changes: 1 addition & 1 deletion frequencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef struct {
const uint32_t upper;
} freq_band_table_t;

extern uint32_t g_air_copy_freq;
extern uint32_t g_aircopy_freq;

extern const freq_band_table_t BX4819_band1;
extern const freq_band_table_t BX4819_band2;
Expand Down
Loading

0 comments on commit 17deb5a

Please sign in to comment.