From 6812cfbcdbf5b2e8c416380e70447b5411adc37c Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 25 Apr 2023 03:08:16 +0200 Subject: [PATCH] fix phase shift overflow, implement frame serialization --- create_sinus_tables.py | 6 ++- include/afsk_sinus.hpp | 6 ++- src/main.cpp | 102 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 101 insertions(+), 13 deletions(-) diff --git a/create_sinus_tables.py b/create_sinus_tables.py index de2a6e2..aecddeb 100755 --- a/create_sinus_tables.py +++ b/create_sinus_tables.py @@ -52,10 +52,14 @@ void sendZero(uint32_t *us, uint8_t *phaseShift) {{ while ((t = micros() - *us) < INV_BAUD) {{ OUT_PORT = pgm_read_byte(&SINUS2200[ (PS2200[*phaseShift] + t) % N_SINUS_2200 ]); }} - (*phaseShift)++; + *phaseShift = (*phaseShift + 1) % N_SHIFTS; *us += INV_BAUD; }} +void setZero() {{ + OUT_PORT = pgm_read_byte(&SINUS2200[0]); +}} + ''') diff --git a/include/afsk_sinus.hpp b/include/afsk_sinus.hpp index b39f490..5b21486 100644 --- a/include/afsk_sinus.hpp +++ b/include/afsk_sinus.hpp @@ -27,7 +27,11 @@ void sendZero(uint32_t *us, uint8_t *phaseShift) { while ((t = micros() - *us) < INV_BAUD) { OUT_PORT = pgm_read_byte(&SINUS2200[ (PS2200[*phaseShift] + t) % N_SINUS_2200 ]); } - (*phaseShift)++; + *phaseShift = (*phaseShift + 1) % N_SHIFTS; *us += INV_BAUD; } +void setZero() { + OUT_PORT = pgm_read_byte(&SINUS2200[0]); +} + diff --git a/src/main.cpp b/src/main.cpp index 84e3ab1..47af543 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,73 @@ #include +#include #include "afsk_sinus.hpp" +#define NCOMMENT 0 +#define CALLSIGN "HB3YBQ" + + +uint8_t txdelay[32]; + +struct ax25 { + uint8_t sflag; + uint8_t daddr[7]; + uint8_t saddr[7]; + uint8_t ctrl; + uint8_t proto; + struct { + uint8_t dtid; + uint8_t type; + char time[7]; + char lat[8]; + uint8_t sym_table_id; + char lon[9]; + uint8_t sym_code; + char comment[NCOMMENT]; + } __attribute__((packed)) aprs; + uint16_t fcs; + uint8_t eflag; +} __attribute__((packed)) frame; + +enum AprsSymbol : uint8_t { + NONE, + AMBULANCE, + BUS, + FIRE_TRUCK, + BICYCLE, + YACHT, + HELICOPTER, + SMALL_AIRCRAFT, + SHIP, + CAR, + MOTORCYCLE, + BALLOON, + JEEP, + RECREATIONAL_VEHICLE, + TRUCK, + VAN +}; + extern void sendOne(uint32_t *us, uint8_t *phaseShift); extern void sendZero(uint32_t *us, uint8_t *phaseShift); +extern void setZero(); uint32_t m = 0; uint8_t phaseShift = 0; +bool nrzi = true; -void sendBell202(uint8_t byt) { +void sendBell202(uint8_t byt, bool sync) { + if (sync) { + phaseShift = 0; + m = micros(); + } for (uint8_t i = 0; i < 8; ++i) { - if ((byt >> i) & 1) { + if (!((byt >> i) & 1)) { + nrzi = !nrzi; + } + if (nrzi) { sendOne(&m, &phaseShift); } else { sendZero(&m, &phaseShift); @@ -20,23 +75,48 @@ void sendBell202(uint8_t byt) { } } -void sendBell202buf(uint8_t *buf, size_t len) { - m = micros(); +void sendBell202buf(uint8_t *buf, size_t len, bool sync) { for (size_t i = 0; i < len; ++i) { - sendBell202(buf[i]); + sendBell202(buf[i], sync); + sync = false; } } +void initFrame() { + memset(txdelay, 0x7e, sizeof(txdelay)); + memset(&frame, 0, sizeof(frame)); + frame.sflag = 0x7e; + memcpy(&frame.daddr, "GPS\x00\x00\x00\x00", 7); + memcpy(&frame.saddr, CALLSIGN, 6); + frame.ctrl = 0x03; + frame.proto = 0xf0; + frame.aprs.dtid = 1; + memcpy(&frame.aprs.time, "151430h", 7); + memcpy(&frame.aprs.lat, "4730.00N", 8); + frame.aprs.sym_table_id = '/'; + memcpy(&frame.aprs.lon, "00730.00E", 9); + frame.aprs.sym_code = BICYCLE; + frame.fcs = 0xffff; + for (size_t i = 0; i < sizeof(frame) - 3; ++i) { + frame.fcs = _crc_xmodem_update(frame.fcs, ((uint8_t*) &frame)[i]); + } + frame.eflag = 0x7e; +} + void setup() { OUT_DDR = 0xff; - Serial.begin(115200); + setZero(); + initFrame(); + nrzi = true; } -uint8_t bytes[] = { - 0xff, 0x55, 0xff, 0x00, 0xff, 0x00 -}; - void loop() { - sendBell202buf(bytes, 6); + sendBell202buf(txdelay, sizeof(txdelay), true); + //sendBell202buf((uint8_t*) &frame, sizeof(frame) - 1, false); + //sendBell202buf((uint8_t*) &frame, sizeof(frame) - 1, false); + sendBell202buf((uint8_t*) &frame, sizeof(frame), false); + setZero(); + nrzi = true; + delay(10000); }