another update

This commit is contained in:
2026-11-12 19:43:23 +01:00
parent 46848926ba
commit 1cbcd628f3
10 changed files with 546 additions and 144 deletions

No files matched your search

+2 -2
View File
@@ -1,3 +1,3 @@
idf_component_register(SRCS "main.c"
idf_component_register(SRCS "main.c" "pager_presets.c" "pager_tx.c" "pocsag_bch.c"
INCLUDE_DIRS "."
REQUIRES esp_driver_gpio esp_driver_spi esp_driver_gptimer esp_adc nvs_flash led_strip)
REQUIRES esp_driver_gpio esp_driver_spi esp_driver_gptimer esp_adc esp_rom nvs_flash led_strip)
+190 -131
View File
@@ -38,55 +38,10 @@
#include "esp_adc/adc_cali_scheme.h"
#include "nvs.h"
#include "nvs_flash.h"
/* ---------------------------- Pins/config ---------------------------- */
#define SPI_HOST_USED SPI2_HOST
#define PIN_SPI_SCLK GPIO_NUM_4
#define PIN_SPI_MOSI GPIO_NUM_5
#define PIN_SPI_MISO GPIO_NUM_0
#define PIN_CC1101_CS GPIO_NUM_1
#define PIN_CC1101_GDO0 GPIO_NUM_3
#define PIN_OLED_CS GPIO_NUM_10
#define PIN_OLED_DC GPIO_NUM_11
#define PIN_OLED_RESET GPIO_NUM_12
#define PIN_BUTTON_BOOT GPIO_NUM_9
#define PIN_BUTTON_UP GPIO_NUM_6
#define PIN_BUTTON_DOWN GPIO_NUM_7
#define PIN_BUTTON_SELECT GPIO_NUM_14
#define PIN_BATTERY_ADC GPIO_NUM_2
#define PIN_STATUS_LED GPIO_NUM_13
#define PIN_NEOPIXEL GPIO_NUM_8
/* Change this one value to tune the receiver. Rebuild and flash afterwards. */
#define RF_FREQ_HZ 435000000UL
#define CC1101_XTAL_HZ 26000000UL
#define TUNE_MIN_HZ 433000000UL
#define TUNE_MAX_HZ 435500000UL
#define TUNE_STEP_HZ 25000UL
#define MY_RIC 1234567UL
#define OLED_IDLE_OFF_MS 60000U
#define OWN_PAGE_ALERT_MS 3000U
#define RX_DISPLAY_FLASHES 4U
#define RX_DISPLAY_FLASH_MS 120U
/* Enable after wiring BAT+ through a divider to GPIO2 and BAT- to GND. */
#define BATTERY_MONITOR_ENABLE 0
#define BATTERY_DIVIDER_TOP_OHMS 100000U
#define BATTERY_DIVIDER_BOTTOM_OHMS 100000U
#define SAMPLE_TIMER_HZ 1000000UL
#define SAMPLE_PERIOD_TICKS 26UL
#define SAMPLE_RATE_HZ (SAMPLE_TIMER_HZ / SAMPLE_PERIOD_TICKS)
#define SAMPLE_BUF_SIZE 16384U
#define SAMPLE_BUF_MASK (SAMPLE_BUF_SIZE - 1U)
#define SPI_TRANSFER_CHUNK 64U
#define HISTORY_COUNT 8
#define MESSAGE_SIZE 160
#include "pager_config.h"
#include "pager_presets.h"
#include "pager_tx.h"
#include "pocsag_bch.h"
static const char *TAG = "pager";
@@ -100,6 +55,13 @@ static unsigned display_baud;
static uint32_t radio_frequency_hz = RF_FREQ_HZ;
static bool oled_is_on = true;
static TickType_t ui_last_activity;
static uint32_t my_ric = MY_RIC;
static unsigned my_ric_preset_index;
static unsigned tx_ric_preset_index;
static unsigned tx_message_index;
static TickType_t tx_last_sent;
static bool tx_has_sent;
static const char *tx_status_text = "HOLD TO SEND";
static bool oled_inverted;
static unsigned rx_flash_toggles;
static TickType_t rx_flash_next_toggle;
@@ -504,6 +466,7 @@ static bool history_nvs_ready;
#define HISTORY_STORAGE_VERSION 1U
#define HISTORY_NVS_NAMESPACE "pager"
#define HISTORY_NVS_KEY "history"
#define OWN_RIC_NVS_KEY "own_ric"
typedef struct {
uint32_t version;
@@ -514,6 +477,18 @@ typedef struct {
static void ui_render(void);
static void ui_note_activity(void);
static void tx_send_selected(void);
static void ui_select_next_my_ric(void)
{
my_ric_preset_index = (my_ric_preset_index + 1U) % ric_preset_count;
my_ric = ric_presets[my_ric_preset_index].ric;
if (history_nvs_ready) {
ESP_ERROR_CHECK(nvs_set_u8(history_nvs, OWN_RIC_NVS_KEY,
(uint8_t)my_ric_preset_index));
ESP_ERROR_CHECK(nvs_commit(history_nvs));
}
}
static void history_storage_save(void)
{
@@ -549,6 +524,13 @@ static void history_storage_init(void)
&history_nvs));
history_nvs_ready = true;
uint8_t saved_ric_index;
if (nvs_get_u8(history_nvs, OWN_RIC_NVS_KEY, &saved_ric_index) == ESP_OK &&
saved_ric_index < ric_preset_count) {
my_ric_preset_index = saved_ric_index;
my_ric = ric_presets[my_ric_preset_index].ric;
}
history_storage_t storage;
size_t size = sizeof(storage);
err = nvs_get_blob(history_nvs, HISTORY_NVS_KEY, &storage, &size);
@@ -574,7 +556,7 @@ static void history_add(uint32_t ric, unsigned baud, unsigned function,
{
if (!text || !text[0])
return;
if (!monitor_mode && ric != MY_RIC)
if (!monitor_mode && ric != my_ric)
return;
pager_message_t *m = &history[history_head];
@@ -595,67 +577,8 @@ static void history_add(uint32_t ric, unsigned baud, unsigned function,
ui_render();
}
/* ------------------------------- BCH --------------------------------- */
#define POCSAG_SYNC 0x7CD215D8UL
#define POCSAG_IDLE 0x7A89C197UL
#define BCH_POLY 0x769U
static uint32_t syndrome_errors[1024];
static uint16_t bch_syndrome(uint32_t value)
{
for (int bit = 30; bit >= 10; bit--) {
if (value & (1UL << bit))
value ^= (uint32_t)BCH_POLY << (bit - 10);
}
return (uint16_t)(value & 0x3FF);
}
static void bch_init(void)
{
memset(syndrome_errors, 0, sizeof(syndrome_errors));
for (int i = 0; i < 31; i++) {
uint32_t e = 1UL << i;
syndrome_errors[bch_syndrome(e)] = e;
}
for (int i = 0; i < 31; i++) {
for (int j = i + 1; j < 31; j++) {
uint32_t e = (1UL << i) | (1UL << j);
syndrome_errors[bch_syndrome(e)] = e;
}
}
}
static bool bch_correct(uint32_t *word, unsigned *ncorrected)
{
uint32_t upper31 = *word >> 1;
uint16_t syn = bch_syndrome(upper31);
uint32_t error = syndrome_errors[syn];
if (syn && !error)
return false;
upper31 ^= error;
unsigned parity = 0;
for (int i = 1; i < 32; i++)
parity ^= (upper31 >> (i - 1)) & 1U;
unsigned parity_error = ((*word & 1U) != parity);
unsigned error_count = (unsigned)__builtin_popcount(error);
if (error_count + parity_error > 2U)
return false;
uint32_t rebuilt = (upper31 << 1) | parity;
*word = rebuilt;
*ncorrected = error_count + parity_error;
return true;
}
/* ---------------------------- POCSAG decode --------------------------- */
typedef enum {
@@ -706,7 +629,8 @@ enum {
UI_LIVE,
UI_HISTORY,
UI_MENU,
UI_TUNE
UI_TUNE,
UI_TX
};
static TickType_t page_alert_until;
@@ -716,6 +640,7 @@ static bool alert_output_state;
static bool alert_output_initialized;
static unsigned menu_return_screen;
static uint32_t tune_candidate_hz;
static unsigned menu_scroll;
typedef enum {
BUTTON_BOOT,
@@ -784,16 +709,16 @@ static void ui_draw_live(void)
oled_text(0, 0, monitor_mode ? "POCSAG MONITOR" : "MY PAGER");
#if BATTERY_MONITOR_ENABLE
if (battery_voltage_valid) {
snprintf(line, sizeof(line), "RIC %lu BAT %u.%02uV",
(unsigned long)MY_RIC,
(unsigned)(battery_voltage_mv / 1000U),
(unsigned)((battery_voltage_mv % 1000U) / 10U));
unsigned battery_percent = battery_voltage_mv <= 3300U ? 0U :
battery_voltage_mv >= 4200U ? 100U :
(unsigned)(((battery_voltage_mv - 3300U) * 100U) / 900U);
snprintf(line, sizeof(line), "RIC %lu BAT %u%%",
(unsigned long)my_ric, battery_percent);
} else {
snprintf(line, sizeof(line), "RIC %lu BAT --",
(unsigned long)MY_RIC);
snprintf(line, sizeof(line), "RIC %lu BAT --", (unsigned long)my_ric);
}
#else
snprintf(line, sizeof(line), "RIC %lu", (unsigned long)MY_RIC);
snprintf(line, sizeof(line), "RIC %lu", (unsigned long)my_ric);
#endif
oled_text(0, 9, line);
@@ -858,17 +783,25 @@ static void ui_draw_menu(void)
snprintf(frequency_item, sizeof(frequency_item), "FREQ %lu.%03lu MHZ",
(unsigned long)(radio_frequency_hz / 1000000UL),
(unsigned long)((radio_frequency_hz / 1000UL) % 1000UL));
char own_ric_item[24];
snprintf(own_ric_item, sizeof(own_ric_item), "MY RIC %lu",
(unsigned long)my_ric);
const char *items[] = {
monitor_mode ? "FILTER: ALL RIC" : "FILTER: MY RIC",
frequency_item,
own_ric_item,
"TX TEST PRESETS",
"LIVE DISPLAY",
"MESSAGE HISTORY",
"RETURN"
};
oled_text(0, 0, "PAGER MENU");
for (unsigned i = 0; i < 5; i++) {
int y = 8 + (int)i * 8;
if (i == menu_cursor) {
for (unsigned row = 0; row < 5U; row++) {
unsigned item = menu_scroll + row;
if (item >= 7U)
break;
int y = 8 + (int)row * 8;
if (item == menu_cursor) {
for (int row = 0; row < 7; row++) {
for (int col = 0; col < 5; col++) {
if (cursor_rows[row] & (1U << col))
@@ -876,7 +809,7 @@ static void ui_draw_menu(void)
}
}
}
oled_text(8, y, items[i]);
oled_text(8, y, items[item]);
}
oled_text(0, 49, "UP/DN MOVE; SEL OK");
oled_text(0, 57, "BOOT TAP NEXT / HOLD");
@@ -894,6 +827,23 @@ static void ui_draw_tune(void)
oled_text(0, 49, "TAP NEXT / HOLD SET");
}
static void ui_draw_tx(void)
{
char line[24];
const ric_preset_t *target = &ric_presets[tx_ric_preset_index];
const tx_message_preset_t *preset = &tx_message_presets[tx_message_index];
oled_text(0, 0, POCSAG_TX_ENABLE ? "TX TEST PRESETS" : "TX DISABLED BUILD");
snprintf(line, sizeof(line), "TO %s", target->label);
oled_text(0, 12, line);
snprintf(line, sizeof(line), "RIC %lu", (unsigned long)target->ric);
oled_text(0, 21, line);
snprintf(line, sizeof(line), "MSG %s", preset->label);
oled_text(0, 27, line);
oled_text(0, 36, preset->system_status ? "LIVE SYSTEM SNAPSHOT" : preset->text);
oled_text(0, 45, "UP/DN RIC; SEL MSG");
oled_text(0, 55, POCSAG_TX_ENABLE ? tx_status_text : "TX OFF IN BUILD CONFIG");
}
static void ui_render(void)
{
if (!oled_is_on)
@@ -905,6 +855,8 @@ static void ui_render(void)
ui_draw_history();
else if (ui_screen == UI_TUNE)
ui_draw_tune();
else if (ui_screen == UI_TX)
ui_draw_tx();
else
ui_draw_live();
if (hold_cue_visible) {
@@ -921,15 +873,33 @@ static void oled_draw_history(void)
ui_render();
}
static void ui_move_menu(int direction)
{
menu_cursor = direction < 0
? (menu_cursor + 6U) % 7U
: (menu_cursor + 1U) % 7U;
if (menu_cursor < menu_scroll)
menu_scroll = menu_cursor;
else if (menu_cursor >= menu_scroll + 5U)
menu_scroll = menu_cursor - 4U;
}
static void ui_next_tx_message(void)
{
tx_message_index = (tx_message_index + 1U) % tx_message_preset_count;
}
static void ui_short_press(void)
{
ui_note_activity();
if (ui_screen == UI_MENU) {
menu_cursor = (menu_cursor + 1U) % 5U;
ui_move_menu(1);
} else if (ui_screen == UI_TUNE) {
tune_candidate_hz += TUNE_STEP_HZ;
if (tune_candidate_hz > TUNE_MAX_HZ)
tune_candidate_hz = TUNE_MIN_HZ;
} else if (ui_screen == UI_TX) {
ui_next_tx_message();
} else if (ui_screen == UI_LIVE) {
if (live_page && message_active &&
text_page + 1U < text_page_count(message_len)) {
@@ -956,6 +926,11 @@ static void ui_short_press(void)
static void ui_long_press(void)
{
ui_note_activity();
if (ui_screen == UI_TX) {
tx_send_selected();
ui_render();
return;
}
if (ui_screen == UI_TUNE) {
cc1101_retune(tune_candidate_hz);
ui_screen = UI_MENU;
@@ -984,9 +959,17 @@ static void ui_long_press(void)
ui_screen = UI_TUNE;
break;
case 2:
ui_screen = UI_LIVE;
ui_select_next_my_ric();
break;
case 3:
tx_ric_preset_index = my_ric_preset_index;
tx_message_index = 0;
ui_screen = UI_TX;
break;
case 4:
ui_screen = UI_LIVE;
break;
case 5:
ui_screen = UI_HISTORY;
history_cursor = 0;
text_page = 0;
@@ -1009,7 +992,14 @@ static void ui_handle_button(ui_button_id_t button, bool long_press)
}
if (button == BUTTON_SELECT) {
if (ui_screen == UI_MENU || ui_screen == UI_TUNE) {
if (ui_screen == UI_TX) {
if (long_press)
tx_send_selected();
else
ui_next_tx_message();
ui_note_activity();
ui_render();
} else if (ui_screen == UI_MENU || ui_screen == UI_TUNE) {
ui_long_press();
} else if (ui_screen == UI_HISTORY) {
ui_screen = UI_LIVE;
@@ -1028,9 +1018,9 @@ static void ui_handle_button(ui_button_id_t button, bool long_press)
ui_note_activity();
if (ui_screen == UI_MENU) {
if (button == BUTTON_UP)
menu_cursor = (menu_cursor + 4U) % 5U;
ui_move_menu(-1);
else
menu_cursor = (menu_cursor + 1U) % 5U;
ui_move_menu(1);
} else if (ui_screen == UI_TUNE) {
if (button == BUTTON_UP) {
tune_candidate_hz = tune_candidate_hz <= TUNE_MIN_HZ + TUNE_STEP_HZ
@@ -1039,6 +1029,12 @@ static void ui_handle_button(ui_button_id_t button, bool long_press)
tune_candidate_hz = tune_candidate_hz >= TUNE_MAX_HZ
? TUNE_MIN_HZ : tune_candidate_hz + TUNE_STEP_HZ;
}
} else if (ui_screen == UI_TX) {
if (button == BUTTON_UP)
tx_ric_preset_index = (tx_ric_preset_index + ric_preset_count - 1U)
% ric_preset_count;
else
tx_ric_preset_index = (tx_ric_preset_index + 1U) % ric_preset_count;
} else if (history_count) {
if (ui_screen == UI_LIVE) {
ui_screen = UI_HISTORY;
@@ -1346,7 +1342,7 @@ static void decode_codeword(uint32_t raw, unsigned slot)
}
unsigned fixed = 0;
if (!bch_correct(&raw, &fixed)) {
if (!pocsag_bch_correct(&raw, &fixed)) {
ESP_LOGW(TAG, "BCH failure, raw=%08lX",
(unsigned long)raw);
message_abort();
@@ -1390,9 +1386,9 @@ static void decode_codeword(uint32_t raw, unsigned slot)
alpha_acc = 0;
alpha_nbits = 0;
numeric_nibble_count = 0;
live_page = monitor_mode || current_ric == MY_RIC;
live_page = monitor_mode || current_ric == my_ric;
text_page = 0;
if (current_ric == MY_RIC)
if (current_ric == my_ric)
ui_start_own_page_alert();
if (live_page) {
ui_screen = UI_LIVE;
@@ -1400,7 +1396,7 @@ static void decode_codeword(uint32_t raw, unsigned slot)
ui_start_display_flash();
}
if (monitor_mode || current_ric == MY_RIC) {
if (monitor_mode || current_ric == my_ric) {
ESP_LOGI(TAG, "Address RIC=%lu function=%u",
(unsigned long)current_ric, current_function);
}
@@ -1579,6 +1575,69 @@ static void start_sampler(void)
(unsigned long)SAMPLE_RATE_HZ);
}
/* Send is explicit from the TX screen; pause sampling so our own RF cannot enter RX decode. */
static void tx_send_selected(void)
{
#if !POCSAG_TX_ENABLE
tx_status_text = "ENABLE TX IN CONFIG";
ESP_LOGW(TAG, "TX is disabled; set POCSAG_TX_ENABLE in pager_config.h");
#else
TickType_t now = xTaskGetTickCount();
if (tx_has_sent && now - tx_last_sent < pdMS_TO_TICKS(TX_MIN_INTERVAL_MS)) {
tx_status_text = "WAIT 10 SECONDS";
return;
}
char system_message[TX_MAX_MESSAGE_CHARS + 1U];
const tx_message_preset_t *preset = &tx_message_presets[tx_message_index];
const char *selected_text = preset->text;
if (preset->system_status) {
#if BATTERY_MONITOR_ENABLE
if (battery_voltage_valid) {
unsigned battery_percent = battery_voltage_mv <= 3300U ? 0U :
battery_voltage_mv >= 4200U ? 100U :
(unsigned)(((battery_voltage_mv - 3300U) * 100U) / 900U);
snprintf(system_message, sizeof(system_message),
"SYS %luM %uB H%u BAT%u",
(unsigned long)(radio_frequency_hz / 1000000UL),
(unsigned)current_baud, history_count, battery_percent);
} else
#endif
{
snprintf(system_message, sizeof(system_message), "SYS %luM %uB H%u",
(unsigned long)(radio_frequency_hz / 1000000UL),
(unsigned)current_baud, history_count);
}
selected_text = system_message;
}
const ric_preset_t *target = &ric_presets[tx_ric_preset_index];
tx_status_text = "TRANSMITTING";
ui_render();
ESP_ERROR_CHECK(gptimer_stop(sample_timer));
esp_err_t err = pager_tx_send(cc_dev, PIN_CC1101_GDO0,
target->ric, selected_text);
sample_wr = 0;
sample_rd = 0;
sync_shift = 0;
sync_bits = 0;
batch_locked = false;
codeword_bits = 0;
codeword_shift = 0;
ESP_ERROR_CHECK(gptimer_start(sample_timer));
if (err == ESP_OK) {
tx_has_sent = true;
tx_last_sent = xTaskGetTickCount();
tx_status_text = "PAGE SENT";
ESP_LOGI(TAG, "Sent test preset to RIC %lu", (unsigned long)target->ric);
} else {
tx_status_text = "TX ERROR";
ESP_LOGE(TAG, "TX failed: %s", esp_err_to_name(err));
}
#endif
}
/* -------------------------------- main -------------------------------- */
void app_main(void)
@@ -1609,7 +1668,7 @@ void app_main(void)
ui_screen = UI_LIVE;
ui_render();
bch_init();
pocsag_bch_init();
cc1101_init();
ui_render();
+57
View File
@@ -0,0 +1,57 @@
#pragma once
#include "driver/gpio.h"
#include "driver/gptimer.h"
#include "driver/spi_master.h"
/* Board wiring: ESP32-H2 SuperMini, CC1101, SPI SSD1306, buttons, and LEDs. */
#define SPI_HOST_USED SPI2_HOST
#define PIN_SPI_SCLK GPIO_NUM_4
#define PIN_SPI_MOSI GPIO_NUM_5
#define PIN_SPI_MISO GPIO_NUM_0
#define PIN_CC1101_CS GPIO_NUM_1
#define PIN_CC1101_GDO0 GPIO_NUM_3
#define PIN_OLED_CS GPIO_NUM_10
#define PIN_OLED_DC GPIO_NUM_11
#define PIN_OLED_RESET GPIO_NUM_12
#define PIN_BUTTON_BOOT GPIO_NUM_9
#define PIN_BUTTON_UP GPIO_NUM_6
#define PIN_BUTTON_DOWN GPIO_NUM_7
#define PIN_BUTTON_SELECT GPIO_NUM_14
#define PIN_BATTERY_ADC GPIO_NUM_2
#define PIN_STATUS_LED GPIO_NUM_13
#define PIN_NEOPIXEL GPIO_NUM_8
/* RF defaults and menu tuning range. */
#define RF_FREQ_HZ 435000000UL
#define CC1101_XTAL_HZ 26000000UL
#define TUNE_MIN_HZ 433000000UL
#define TUNE_MAX_HZ 435500000UL
#define TUNE_STEP_HZ 25000UL
#define MY_RIC 1234567UL
/* Add the test RICs you control in pager_presets.c. */
#define POCSAG_TX_ENABLE 1
#define TX_MIN_INTERVAL_MS 10000U
#define TX_MAX_MESSAGE_CHARS 32U
#define TX_PATABLE_VALUE 0x03U
#define TX_BATCH_COUNT 3U
#define OLED_IDLE_OFF_MS 60000U
#define OWN_PAGE_ALERT_MS 3000U
#define RX_DISPLAY_FLASHES 4U
#define RX_DISPLAY_FLASH_MS 120U
/* Enable only with a battery divider wired BAT+ -> GPIO2 -> GND. */
#define BATTERY_MONITOR_ENABLE 0
#define BATTERY_DIVIDER_TOP_OHMS 100000U
#define BATTERY_DIVIDER_BOTTOM_OHMS 100000U
#define SAMPLE_TIMER_HZ 1000000UL
#define SAMPLE_PERIOD_TICKS 26UL
#define SAMPLE_RATE_HZ (SAMPLE_TIMER_HZ / SAMPLE_PERIOD_TICKS)
#define SAMPLE_BUF_SIZE 16384U
#define SAMPLE_BUF_MASK (SAMPLE_BUF_SIZE - 1U)
#define SPI_TRANSFER_CHUNK 64U
#define HISTORY_COUNT 8
#define MESSAGE_SIZE 160
+17
View File
@@ -0,0 +1,17 @@
#include "pager_config.h"
#include "pager_presets.h"
/* Keep the editable receiver/transmitter RIC allowlist in one place. */
const ric_preset_t ric_presets[] = {
{"MY PAGER", MY_RIC},
{"TEST RIC 1", 1234568UL},
};
const size_t ric_preset_count = sizeof(ric_presets) / sizeof(ric_presets[0]);
const tx_message_preset_t tx_message_presets[] = {
{"TEST", "POCSAG TEST", false},
{"HELLO", "HELLO FROM H2", false},
{"SYSTEM STATE", NULL, true},
};
const size_t tx_message_preset_count =
sizeof(tx_message_presets) / sizeof(tx_message_presets[0]);
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
typedef struct {
const char *label;
uint32_t ric;
} ric_preset_t;
typedef struct {
const char *label;
const char *text;
bool system_status;
} tx_message_preset_t;
extern const ric_preset_t ric_presets[];
extern const size_t ric_preset_count;
extern const tx_message_preset_t tx_message_presets[];
extern const size_t tx_message_preset_count;
+160
View File
@@ -0,0 +1,160 @@
/* CC1101 async POCSAG test transmitter; called only from the explicit TX menu. */
#include "pager_tx.h"
#include <string.h>
#include "esp_rom_sys.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "pager_config.h"
#define POCSAG_SYNC_WORD 0x7CD215D8UL
#define POCSAG_IDLE_WORD 0x7A89C197UL
#define POCSAG_BCH_POLY 0x769U
static esp_err_t tx_spi_write(spi_device_handle_t radio, uint8_t address,
uint8_t value)
{
uint8_t bytes[2] = {address, value};
spi_transaction_t transaction = {
.length = sizeof(bytes) * 8U,
.tx_buffer = bytes,
};
return spi_device_polling_transmit(radio, &transaction);
}
static esp_err_t tx_strobe(spi_device_handle_t radio, uint8_t command)
{
spi_transaction_t transaction = {
.length = 8,
.tx_buffer = &command,
};
return spi_device_polling_transmit(radio, &transaction);
}
static esp_err_t tx_write_pa_entry(spi_device_handle_t radio, uint8_t value)
{
uint8_t bytes[2] = {0x7E, value}; /* PATABLE burst-write command */
spi_transaction_t transaction = {
.length = sizeof(bytes) * 8U,
.tx_buffer = bytes,
};
return spi_device_polling_transmit(radio, &transaction);
}
#if POCSAG_TX_ENABLE
static uint16_t pocsag_bch_remainder(uint32_t value)
{
for (int bit = 30; bit >= 10; bit--) {
if (value & (1UL << bit))
value ^= POCSAG_BCH_POLY << (bit - 10);
}
return (uint16_t)(value & 0x3FFU);
}
static uint32_t pocsag_encode_word(bool is_message, uint32_t payload)
{
uint32_t header = ((is_message ? 1U : 0U) << 20) |
(payload & 0xFFFFFU);
uint32_t upper = header << 10;
upper |= pocsag_bch_remainder(upper);
unsigned parity = 0;
for (unsigned bit = 0; bit < 31U; bit++)
parity ^= (upper >> bit) & 1U;
return (upper << 1) | parity;
}
static void tx_bit(gpio_num_t pin, unsigned bit)
{
gpio_set_level(pin, bit ? 1 : 0);
esp_rom_delay_us(833);
}
static void tx_word(gpio_num_t pin, uint32_t word)
{
for (int bit = 31; bit >= 0; bit--)
tx_bit(pin, (word >> bit) & 1U);
}
static void tx_restore_rx(spi_device_handle_t radio, gpio_num_t data_gpio)
{
gpio_set_level(data_gpio, 0);
esp_rom_delay_us(1000);
ESP_ERROR_CHECK(tx_strobe(radio, 0x36)); /* SIDLE */
ESP_ERROR_CHECK(tx_spi_write(radio, 0x02, 0x0D)); /* async serial output */
ESP_ERROR_CHECK(gpio_set_direction(data_gpio, GPIO_MODE_INPUT));
ESP_ERROR_CHECK(tx_strobe(radio, 0x33)); /* SCAL */
vTaskDelay(pdMS_TO_TICKS(5));
ESP_ERROR_CHECK(tx_strobe(radio, 0x3A)); /* SFRX */
ESP_ERROR_CHECK(tx_strobe(radio, 0x34)); /* SRX */
}
#endif
esp_err_t pager_tx_send(spi_device_handle_t radio, gpio_num_t data_gpio,
uint32_t ric, const char *text)
{
#if !POCSAG_TX_ENABLE
(void)radio;
(void)data_gpio;
(void)ric;
(void)text;
return ESP_ERR_NOT_SUPPORTED;
#else
if (!radio || !text || !text[0] || ric > 0x1FFFFFU)
return ESP_ERR_INVALID_ARG;
size_t text_length = strnlen(text, TX_MAX_MESSAGE_CHARS + 1U);
if (text_length > TX_MAX_MESSAGE_CHARS)
return ESP_ERR_INVALID_SIZE;
uint32_t codewords[TX_BATCH_COUNT * 16U];
for (size_t i = 0; i < TX_BATCH_COUNT * 16U; i++)
codewords[i] = POCSAG_IDLE_WORD;
unsigned address_slot = (ric & 7U) * 2U;
uint32_t address_payload = ((ric >> 3) << 2) | 3U;
codewords[address_slot] = pocsag_encode_word(false, address_payload);
/* Append the POCSAG ETX character and pack 7-bit LSB-first characters. */
unsigned bit_count = (unsigned)(text_length + 1U) * 7U;
unsigned message_word_count = (bit_count + 19U) / 20U;
unsigned message_start = address_slot + 1U;
if (message_start + message_word_count > TX_BATCH_COUNT * 16U)
return ESP_ERR_INVALID_SIZE;
for (unsigned word_index = 0; word_index < message_word_count; word_index++) {
uint32_t payload = 0;
for (unsigned payload_bit = 0; payload_bit < 20U; payload_bit++) {
unsigned stream_bit = word_index * 20U + payload_bit;
unsigned character_index = stream_bit / 7U;
unsigned character_bit = stream_bit % 7U;
unsigned character = character_index < text_length
? (uint8_t)text[character_index]
: character_index == text_length ? 0x03U : 0U;
payload = (payload << 1) |
((character >> character_bit) & 1U);
}
codewords[message_start + word_index] = pocsag_encode_word(true, payload);
}
ESP_ERROR_CHECK(tx_strobe(radio, 0x36)); /* SIDLE */
ESP_ERROR_CHECK(tx_spi_write(radio, 0x02, 0x0E)); /* async serial input */
ESP_ERROR_CHECK(tx_write_pa_entry(radio, TX_PATABLE_VALUE));
ESP_ERROR_CHECK(gpio_set_direction(data_gpio, GPIO_MODE_OUTPUT));
gpio_set_level(data_gpio, 0);
ESP_ERROR_CHECK(tx_strobe(radio, 0x33)); /* SCAL */
vTaskDelay(pdMS_TO_TICKS(5));
ESP_ERROR_CHECK(tx_strobe(radio, 0x35)); /* STX */
for (unsigned bit = 0; bit < 576U; bit++)
tx_bit(data_gpio, (bit & 1U) ? 0U : 1U);
for (unsigned batch = 0; batch < TX_BATCH_COUNT; batch++) {
tx_word(data_gpio, POCSAG_SYNC_WORD);
for (unsigned slot = 0; slot < 16U; slot++)
tx_word(data_gpio, codewords[batch * 16U + slot]);
}
tx_restore_rx(radio, data_gpio);
return ESP_OK;
#endif
}
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "esp_err.h"
/* Send one bounded alpha test page; returns NOT_SUPPORTED if build TX is disabled. */
esp_err_t pager_tx_send(spi_device_handle_t radio, gpio_num_t data_gpio,
uint32_t ric, const char *text);
+55
View File
@@ -0,0 +1,55 @@
#include "pocsag_bch.h"
#include <string.h>
#define POCSAG_BCH_POLY 0x769U
static uint32_t syndrome_errors[1024];
static uint16_t bch_syndrome(uint32_t value)
{
for (int bit = 30; bit >= 10; bit--) {
if (value & (1UL << bit))
value ^= (uint32_t)POCSAG_BCH_POLY << (bit - 10);
}
return (uint16_t)(value & 0x3FFU);
}
/* The shortened BCH(31,21) syndrome table corrects one or two data-bit flips. */
void pocsag_bch_init(void)
{
memset(syndrome_errors, 0, sizeof(syndrome_errors));
for (int first = 0; first < 31; first++) {
uint32_t error = 1UL << first;
syndrome_errors[bch_syndrome(error)] = error;
}
for (int first = 0; first < 31; first++) {
for (int second = first + 1; second < 31; second++) {
uint32_t error = (1UL << first) | (1UL << second);
syndrome_errors[bch_syndrome(error)] = error;
}
}
}
bool pocsag_bch_correct(uint32_t *word, unsigned *corrected_bits)
{
uint32_t upper31 = *word >> 1;
uint16_t syndrome = bch_syndrome(upper31);
uint32_t error = syndrome_errors[syndrome];
if (syndrome && !error)
return false;
upper31 ^= error;
unsigned parity = 0;
for (int bit = 0; bit < 31; bit++)
parity ^= (upper31 >> bit) & 1U;
unsigned parity_error = ((*word & 1U) != parity);
unsigned data_errors = (unsigned)__builtin_popcount(error);
if (data_errors + parity_error > 2U)
return false;
*word = (upper31 << 1) | parity;
*corrected_bits = data_errors + parity_error;
return true;
}
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
/* Initialize the syndrome lookup table and correct up to two codeword errors. */
void pocsag_bch_init(void);
bool pocsag_bch_correct(uint32_t *word, unsigned *corrected_bits);