Files
pager/main/pager_tx.c
T
2026-11-12 19:43:23 +01:00

161 lines
5.4 KiB
C

/* 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
}