another attempt

This commit is contained in:
2026-11-15 00:58:12 +01:00
parent e7ae3d3ee8
commit 7832c1968e
14 changed files with 332 additions and 126 deletions

No files matched your search

+96 -32
View File
@@ -15,7 +15,7 @@ DataPacket dataPacket;
DataPacket inBoundPacket;
uint8_t *dataPTR = dataPacket.data;
char *dataPTR = gMessageText;
SMSEnteringState gEnteringSMS = SMS_NOT_ENTERING;
@@ -67,7 +67,20 @@ uint8_t SMSResponseCounterTarget = 6;
#define POCSAG_REG58 0x03C1U // FSK 1.2K TX/RX, 1.2K RX BW, 0xAA/0x55 preamble, RX gain 3, FSK on
#define POCSAG_REG59 ((6U << 4) | FSK_SYNC_LEN_BIT) // 7 byte chip preamble, 4 byte sync, no scramble
#define POCSAG_REG5C 0xAA30U // CRC off
#define POCSAG_REG72 0x3065U // 1200 baud (1200 * 10.32444)
// POCSAG 512 / 1200 / 2400 baud, selected by the modem mode
static uint16_t POCSAG_Baud(void)
{
switch (gEeprom.FSKMode) {
case MOD_POCSAG_512: return 512;
case MOD_POCSAG_2400: return 2400;
default: return 1200;
}
}
// REG_72 = baud * 10.32444 (1200 -> 0x3065)
#define POCSAG_REG72(baud) ((uint16_t) (((uint32_t) (baud) * 103244UL + 5000UL) / 10000UL))
// wider FSK RX bandwidth for 2400 baud
#define POCSAG_REG58_FOR(baud) (POCSAG_REG58 | ((baud) > 1200U ? FSK_RX_BW_2_4K_FFSK_1200_2400 : FSK_RX_BW_1_2K))
#define POCSAG_RX_BYTES POCSAG_MAX_FRAME_SIZE // captured after the chip locks on the preamble
#define POCSAG_CHIP_SYNC 0xAAAAU // logical preamble pattern
@@ -108,8 +121,9 @@ static void POCSAG_ConfigureModem(const bool rx)
// The FSK modulator is driven through Tone2: with Tone2 disabled / gain 0 the
// carrier barely shifts. The gain (together with REG_40) sets the FSK deviation.
BK4819_WriteRegister(BK4819_REG_70, TONE2_ENABLE_BIT | (POCSAG_TONE2_GAIN & TONE2_GAIN_MASK));
BK4819_WriteRegister(BK4819_REG_72, POCSAG_REG72);
BK4819_WriteRegister(BK4819_REG_58, POCSAG_REG58);
const uint16_t baud = POCSAG_Baud();
BK4819_WriteRegister(BK4819_REG_72, POCSAG_REG72(baud));
BK4819_WriteRegister(BK4819_REG_58, POCSAG_REG58_FOR(baud));
if (rx) {
// "FSK" channel bandwidth: BK4819_SetFilterBandwidth() leaves REG_43 alone,
@@ -129,7 +143,8 @@ static void POCSAG_ConfigureModem(const bool rx)
BK4819_WriteRegister(BK4819_REG_59, FSK_CLEAR_TX_FIFO | FSK_CLEAR_RX_FIFO | POCSAG_REG59);
BK4819_WriteRegister(BK4819_REG_59, POCSAG_REG59);
BK4819_WriteRegister(BK4819_REG_02, 0);
// No REG_02 write here: this also runs from inside the interrupt loop (RX re-arm)
// and clearing REG_02 there drops pending events such as squelch close.
}
static uint8_t pocsagRxBuffer[POCSAG_RX_BYTES];
@@ -138,10 +153,40 @@ static uint32_t lastPocsagSender;
static char lastPocsagMessage[POCSAG_MAX_MESSAGE_LENGTH + 1U];
static uint8_t pocsagDuplicateTimeout;
// New page alert: yellow (red + green) LED flashing and a speaker beep. The beep
// waits until RX has ended, AUDIO_PlayBeep() does nothing while receiving.
#define POCSAG_ALERT_FLASHES 10U // 500 ms half periods
static uint8_t pocsagAlertTicks;
static bool pocsagBeepPending;
static void POCSAG_StartAlert(void)
{
pocsagAlertTicks = POCSAG_ALERT_FLASHES;
pocsagBeepPending = true;
BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1_RED, true);
BK4819_ToggleGpioOut(BK4819_GPIO6_PIN2_GREEN, true);
}
static void POCSAG_AlertTimeSlice500ms(void)
{
if (pocsagAlertTicks > 0) {
pocsagAlertTicks--;
const bool on = (pocsagAlertTicks & 1U) != 0;
BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1_RED, on);
BK4819_ToggleGpioOut(BK4819_GPIO6_PIN2_GREEN, on);
}
if (pocsagBeepPending && !FUNCTION_IsRx() && gCurrentFunction != FUNCTION_TRANSMIT) {
pocsagBeepPending = false;
AUDIO_PlayBeep(BEEP_880HZ_60MS_DOUBLE_BEEP); // not affected by the key beep setting
}
}
void FSKModem_TimeSlice500ms(void) {
if (gEeprom.FSKMode == MOD_POCSAG_1200) {
if (MODEM_IS_POCSAG(gEeprom.FSKMode)) {
if (pocsagDuplicateTimeout > 0)
pocsagDuplicateTimeout--;
POCSAG_AlertTimeSlice500ms();
return;
}
@@ -175,7 +220,7 @@ void FSKModem_TimeSlice500ms(void) {
}
void MSG_ConfigureFSK(bool rx) {
if (gEeprom.FSKMode == MOD_POCSAG_1200) {
if (MODEM_IS_POCSAG(gEeprom.FSKMode)) {
POCSAG_ConfigureModem(rx);
return;
}
@@ -438,10 +483,11 @@ void processReceivedPacket(DataPacket *packet) {
strcpy(String, "SMS by ");
itoa(packet->src, numBuf); // Convert number to string
strcat(String, numBuf);
MESSAGES_SAVE();
MESSAGES_SAVE(inBoundPacket.src, inBoundPacket.seq, inBoundPacket.ttl, inBoundPacket.flags,
inBoundPacket.data, DataPacketDataSize);
gRequestDisplayScreen = DISPLAY_MESSAGES;
GUI_SelectNextDisplay(gRequestDisplayScreen);
gActiveMessageBank = MESSAGES_COUNT / 3 - 1;
MESSAGES_ShowNewest();
UI_DisplayPopup(String);
inBoundPacket.flags |= 0x40;
inBoundPacket.dest = inBoundPacket.src;
@@ -464,7 +510,24 @@ void processReceivedPacket(DataPacket *packet) {
}
}
static void MSG_POCSAGStoreReceived(void);
// The chip locks somewhere in the preamble, so it usually has not collected all
// POCSAG_RX_BYTES when the carrier drops. Squelch close / VFO switching re-arms
// RX through here: decode what was captured before it is thrown away.
static void POCSAG_FlushCapture(void)
{
if (!MODEM_IS_POCSAG(gEeprom.FSKMode) || rxState != Receiving)
return;
rxState = Ready;
if (pocsagRxSize >= 4U * 3U) // at least sync + address + one message word
MSG_POCSAGStoreReceived();
pocsagRxSize = 0;
}
void MSG_EnableRX(const bool enable) {
POCSAG_FlushCapture();
if (enable) {
MSG_ConfigureFSK(true);
@@ -477,17 +540,14 @@ void MSG_EnableRX(const bool enable) {
}
}
static void MSG_POCSAGSendData(DataPacket *packet)
void MSG_POCSAGSend(const uint32_t ric, const char *text)
{
uint8_t frame[POCSAG_MAX_FRAME_SIZE];
char message[DataPacketDataSize + 1U];
size_t frameSize;
const unsigned int vfo = (gEeprom.CROSS_BAND_RX_TX == CROSS_BAND_OFF) ? gEeprom.RX_VFO
: gEeprom.TX_VFO;
memcpy(message, packet->data, DataPacketDataSize);
message[DataPacketDataSize] = '\0';
if (!POCSAG_Encode(packet->dest, message, frame, sizeof(frame), &frameSize)) {
if (!POCSAG_Encode(ric, text, frame, sizeof(frame), &frameSize)) {
AUDIO_PlayBeep(BEEP_500HZ_60MS_DOUBLE_BEEP_OPTIONAL);
return;
}
@@ -533,8 +593,10 @@ static void MSG_POCSAGSendData(DataPacket *packet)
SYSTEM_DelayMs(20);
BK4819_FskEnableTx();
// ~(208 + 11) bytes at 1200 baud = 1.46 s; allow 3 s.
for (unsigned int timeout = 0; timeout < 600U; timeout++) {
// chip preamble/sync + frame, plus 1 s margin (512 baud: ~4.4 s)
const unsigned int timeoutMs =
(unsigned int) (((frameSize + 11U) * 8U * 1000U) / POCSAG_Baud()) + 1000U;
for (unsigned int timeout = 0; timeout < timeoutMs / 5U; timeout++) {
SYSTEM_DelayMs(5);
if (BK4819_ReadRegister(BK4819_REG_0C) & 1U) {
BK4819_WriteRegister(BK4819_REG_02, 0);
@@ -566,12 +628,15 @@ static void MSG_POCSAGSendData(DataPacket *packet)
BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1_RED, false);
MSG_EnableRX(true);
gVfoConfigureMode = VFO_CONFIGURE;
dataPTR = dataPacket.data;
dataPTR = gMessageText;
}
void MSG_FSKSendData(DataPacket *dataPacketIn) {
if (gEeprom.FSKMode == MOD_POCSAG_1200) {
MSG_POCSAGSendData(dataPacketIn);
if (MODEM_IS_POCSAG(gEeprom.FSKMode)) { // e.g. UART command 0x06FF
char text[DataPacketDataSize + 1U];
memcpy(text, dataPacketIn->data, DataPacketDataSize);
text[DataPacketDataSize] = '\0';
MSG_POCSAGSend(dataPacketIn->dest, text);
return;
}
@@ -688,7 +753,7 @@ void MSG_FSKSendData(DataPacket *dataPacketIn) {
BK4819_ToggleGpioOut(BK4819_GPIO5_PIN1_RED, false);
MSG_EnableRX(true);
gVfoConfigureMode = VFO_CONFIGURE;
dataPTR = dataPacket.data;
dataPTR = gMessageText;
}
}
@@ -771,9 +836,13 @@ static void MSG_POCSAGStoreReceived(void)
POCSAG_Log("\r\n");
POCSAG_LogHex("PD ", pocsagRxBuffer, pocsagRxSize);
// monitor mode: accept pages for every RIC, otherwise only our own
const uint32_t ric = gEeprom.POCSAG_MONITOR ? POCSAG_ANY_RIC : gEeprom.FSKSRCAddress;
const bool decoded = POCSAG_TryDecode(ric, &sender, message, sizeof(message));
#if POCSAG_DEBUG_UART
if (POCSAG_TryDecode(POCSAG_ANY_RIC, &sender, message, sizeof(message))) {
POCSAG_LogNumber("PR ", sender);
if (decoded || POCSAG_TryDecode(POCSAG_ANY_RIC, &sender, message, sizeof(message))) {
POCSAG_LogNumber(decoded ? "PR " : "PO ", sender); // PO: page for another RIC
POCSAG_Log(" ");
POCSAG_Log(message);
POCSAG_Log("\r\n");
@@ -782,8 +851,7 @@ static void MSG_POCSAGStoreReceived(void)
}
#endif
if (!POCSAG_TryDecode(gEeprom.FSKSRCAddress, &sender, message, sizeof(message)) ||
message[0] == '\0')
if (!decoded || message[0] == '\0')
return;
if (pocsagDuplicateTimeout > 0 && sender == lastPocsagSender &&
@@ -794,14 +862,10 @@ static void MSG_POCSAGStoreReceived(void)
strcpy(lastPocsagMessage, message);
pocsagDuplicateTimeout = 20;
memset(&inBoundPacket, 0, sizeof(inBoundPacket));
inBoundPacket.dest = gEeprom.FSKSRCAddress;
inBoundPacket.src = sender;
inBoundPacket.flags = MESSAGE_FLAG_POCSAG;
memcpy(inBoundPacket.data, message, strlen(message) + 1U);
MESSAGES_SAVE();
MESSAGES_SAVE(sender, 0, 0, MESSAGE_FLAG_POCSAG, (const uint8_t *) message, strlen(message));
POCSAG_StartAlert();
gActiveMessageBank = MESSAGES_COUNT / 3U - 1U;
MESSAGES_ShowNewest();
gRequestDisplayScreen = DISPLAY_MESSAGES;
GUI_SelectNextDisplay(DISPLAY_MESSAGES);
UI_DisplayPopup("POCSAG received");
@@ -852,7 +916,7 @@ void FSK_HANDLE_IRQ(unsigned short irq) {
//UART_printf("\nMSG : S%i, F%i, E%i | %i", rx_sync, rx_fifo_almost_full, rx_finished, irq);
if (gEeprom.FSKMode == MOD_POCSAG_1200) {
if (MODEM_IS_POCSAG(gEeprom.FSKMode)) {
POCSAG_HandleIRQ(rx_sync, rx_fifo_almost_full, rx_finished);
return;
}