From 9e6de5e983af7285a32267f69d590dccf354af35 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Fri, 18 Aug 2023 16:03:16 +0200 Subject: Write test code for EN_RX and EN_PA --- tracker-stm32/README.md | 11 +++ tracker-stm32/platformio.ini | 11 ++- tracker-stm32/src/main.cpp | 170 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 166 insertions(+), 26 deletions(-) (limited to 'tracker-stm32') diff --git a/tracker-stm32/README.md b/tracker-stm32/README.md index a2bda13..e4518ab 100644 --- a/tracker-stm32/README.md +++ b/tracker-stm32/README.md @@ -31,3 +31,14 @@ Compile: Program NUCLEO board: pio run -t upload + +Program STM32: + + ./openocd.sh + + arm-none-eabi-gdb .pio/build/hercules/firmware.elf + (gdb) target remote :3333 + (gdb) monitor arm semihosting enable + (gdb) load + (gdb) cont + diff --git a/tracker-stm32/platformio.ini b/tracker-stm32/platformio.ini index a50655e..32ece90 100644 --- a/tracker-stm32/platformio.ini +++ b/tracker-stm32/platformio.ini @@ -9,4 +9,13 @@ lib_deps = jgromes/RadioLib@^6.0.0 mikalhart/TinyGPSPlus@1.0.3 arduino-libraries/SD@^1.2.4 - koendv/STM32duino-Semihosting@1.0.5 +; koendv/STM32duino-Semihosting@1.0.5 + +; USB VID/PID is Openmoko Neo1973 serial +build_flags = + -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC + -D USBCON + -D USBD_VID=0x1d50 + -D USBD_PID=0x5120 + -D USB_MANUFACTURER="HB9EGM" + -D USB_PRODUCT="\"HERCULES\"" diff --git a/tracker-stm32/src/main.cpp b/tracker-stm32/src/main.cpp index e452712..7c2a273 100644 --- a/tracker-stm32/src/main.cpp +++ b/tracker-stm32/src/main.cpp @@ -2,7 +2,11 @@ // This file is a bit of a mess, because first it contained aprs434 compressed // code running on STM32H7, and now it contains traditional APRS-over-LoRa for STM32F1 #include + +#if defined(SEMIHOSTING) #include +#endif + #include #include #include @@ -28,22 +32,15 @@ constexpr int ENCODER_SW_8 = PB15; constexpr int BAT_MEAS = PA0; -// Date Type Codes defined in aprs434.github.io -constexpr int DATA_TYPE_CODE_GEOLOCATION = 0; -constexpr int DATA_TYPE_CODE_STATUS_REPORT = 1; - // TODO: read these settings from the SD Card -constexpr char CALLSIGN[] = "HB9EGM"; -constexpr int SSID = 7; constexpr char CALLSIGN_SSID_PATH[] = "HB9EGM-7>APZEGM"; -constexpr int PATH_CODE = 2; // metropolitan mobile constexpr char SYMBOL_TABLE_IDENTIFIER = '/'; constexpr char SYMBOL_CODE_BICYCLE = 'b'; constexpr char SYMBOL_CODE_FOOT = '['; constexpr long REPORT_TX_INTERVAL = 181000; constexpr long TEXT_TX_INTERVAL = 47000; -constexpr char TEXT_REPORT[] = "mpb.li/git/lora-aprs-hb9egm"; // Max length=28 +constexpr char TEXT_REPORT[] = "mpb.li/git/lora-aprs-hb9egm"; // Max length=28 for compressed #if 0 @@ -68,7 +65,9 @@ constexpr size_t MAX_REPORT_LEN = 48; size_t report_len = 0; uint8_t report[MAX_REPORT_LEN]; +#if defined(SEMIHOSTING) SemihostingStream sh; +#endif static char letterize(int x) { return (char) x + 65; @@ -76,7 +75,21 @@ static char letterize(int x) { void setup() { Wire.begin(); + SerialUSB.begin(); pinMode(LED_STATUSn, OUTPUT); + pinMode(LED_TXn, OUTPUT); + digitalWrite(LED_TXn, HIGH); + + digitalWrite(EN_PA, LOW); + pinMode(EN_PA, OUTPUT); + digitalWrite(EN_RX, LOW); + pinMode(EN_RX, OUTPUT); + + pinMode(ENCODER_SW_1, INPUT_PULLUP); + pinMode(ENCODER_SW_2, INPUT_PULLUP); + pinMode(ENCODER_SW_4, INPUT_PULLUP); + pinMode(ENCODER_SW_8, INPUT_PULLUP); + pinMode(BTN1n, INPUT); #if 0 pinMode(SD_CS, OUTPUT); @@ -87,64 +100,82 @@ void setup() { } #endif - sh.print(F("START")); serialGNSS.begin(9600); +#if defined(SEMIHOSTING) sh.print(F("[RFM] Init ")); +#endif int state = radio.begin(433.775); if (state == RADIOLIB_ERR_NONE) { +#if defined(SEMIHOSTING) sh.println(F("success!")); +#endif } else { +#if defined(SEMIHOSTING) sh.print(F("failed, code ")); sh.println(state); +#endif while (true); } if (radio.setBandwidth(125.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) { +#if defined(SEMIHOSTING) sh.println(F("Selected bandwidth is invalid for this module!")); +#endif while (true); } if (radio.setSpreadingFactor(12) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) { +#if defined(SEMIHOSTING) sh.println(F("Selected spreading factor is invalid for this module!")); +#endif while (true); } if (radio.setCodingRate(5) == RADIOLIB_ERR_INVALID_CODING_RATE) { +#if defined(SEMIHOSTING) sh.println(F("Selected coding rate is invalid for this module!")); +#endif while (true); } // NOTE: value 0x34 is reserved for LoRaWAN networks and should not be used if (radio.setSyncWord(0x14) != RADIOLIB_ERR_NONE) { +#if defined(SEMIHOSTING) sh.println(F("Unable to set sync word!")); +#endif while (true); } if (radio.setOutputPower(20) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) { +#if defined(SEMIHOSTING) sh.println(F("Selected output power is invalid for this module!")); +#endif while (true); } if (radio.setPreambleLength(8) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) { +#if defined(SEMIHOSTING) sh.println(F("Selected preamble length is invalid for this module!")); +#endif while (true); } // radio.setRfSwitchPins(4, 5); + + digitalWrite(LED_STATUSn, HIGH); } static void handle_radio_error(int state) { +#if defined(SEMIHOSTING) if (state == RADIOLIB_ERR_NONE) { sh.print(F(" RFM OK Datarate: ")); sh.print(radio.getDataRate()); sh.println(F(" bps")); - } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) { sh.println(F(" too long!")); - } else if (state == RADIOLIB_ERR_TX_TIMEOUT) { sh.println(F(" timeout!")); @@ -154,6 +185,7 @@ static void handle_radio_error(int state) sh.print(F(" failed, code ")); sh.println(state); } +#endif } void append_position_report() @@ -192,6 +224,28 @@ void append_position_report() report[report_len++] = 0x47; } +static void init_report() +{ + report_len = 0; + + report[report_len++] = '<'; + report[report_len++] = 0xFF; + report[report_len++] = 0x01; + + const char* p = CALLSIGN_SSID_PATH; + while (*p) { + report[report_len++] = *(p++); + } +} + +static int read_encoder() +{ + return 15 - (digitalRead(ENCODER_SW_1) + + 2 * digitalRead(ENCODER_SW_2) + + 4 * digitalRead(ENCODER_SW_4) + + 8 * digitalRead(ENCODER_SW_8)); +} + void loop() { while (serialGNSS.available() > 0) { @@ -202,6 +256,56 @@ void loop() if (now - lastGnssPoll > 1000) { lastGnssPoll = now; + const int encoder_value = read_encoder(); + const bool btn1 = digitalRead(BTN1n) == 0; + digitalWrite(LED_TXn, not btn1); + + if (encoder_value == 1) { + digitalWrite(EN_RX, HIGH); + digitalWrite(EN_PA, LOW); + } + else if (encoder_value == 2) { + digitalWrite(EN_RX, LOW); + digitalWrite(EN_PA, HIGH); + } + else { + digitalWrite(EN_RX, LOW); + digitalWrite(EN_PA, LOW); + } + + if (SerialUSB) { + const long latitude = gps.location.lat(); + SerialUSB.print(F("Lat ")); + SerialUSB.print(latitude); + + const long longitude = gps.location.lng(); + SerialUSB.print(F(" Lon ")); + SerialUSB.print(longitude); + + const byte SIV = gps.satellites.value(); + SerialUSB.print(F(" SIV: ")); + SerialUSB.print(SIV); + + SerialUSB.print(F(" Enc: ")); + SerialUSB.print(encoder_value); + + SerialUSB.print(F(" BTN: ")); + SerialUSB.println((int)btn1); + } + } +} + +void oldloop() +{ + while (serialGNSS.available() > 0) { + gps.encode(serialGNSS.read()); + } + + const auto now = millis(); + if (now - lastGnssPoll > 1000) { + lastGnssPoll = now; + +#if 0 const long latitude = gps.location.lat(); sh.print(F("Lat ")); sh.print(latitude); @@ -213,23 +317,14 @@ void loop() const byte SIV = gps.satellites.value(); sh.print(F(" SIV: ")); sh.println(SIV); +#endif + if (now - lastPositionReport > REPORT_TX_INTERVAL) { lastPositionReport = now; - digitalWrite(LED_STATUSn, LOW); - - report_len = 0; - - report[report_len++] = '<'; - report[report_len++] = 0xFF; - report[report_len++] = 0x01; - - const char* p = CALLSIGN_SSID_PATH; - while (*p) { - report[report_len++] = *p; - p++; - } + digitalWrite(LED_TXn, LOW); + init_report(); report[report_len++] = ':'; report[report_len++] = '!'; report[report_len++] = SYMBOL_TABLE_IDENTIFIER; @@ -237,12 +332,37 @@ void loop() int state = radio.transmit(report, report_len); handle_radio_error(state); - digitalWrite(LED_STATUSn, HIGH); + digitalWrite(LED_TXn, HIGH); + } + } + + if (now - lastTextReport > TEXT_TX_INTERVAL) { + lastTextReport = now; + digitalWrite(LED_TXn, LOW); + + init_report(); + report[report_len++] = ':'; + report[report_len++] = '>'; + const char* p = TEXT_REPORT; + while (*p) { + report[report_len++] = *(p++); } + + int state = radio.transmit(report, report_len); + handle_radio_error(state); + + digitalWrite(LED_TXn, HIGH); } } #if 0 +// Date Type Codes defined in aprs434.github.io +constexpr int DATA_TYPE_CODE_GEOLOCATION = 0; +constexpr int DATA_TYPE_CODE_STATUS_REPORT = 1; +constexpr char CALLSIGN[] = "HB9EGM"; +constexpr int SSID = 7; +constexpr int PATH_CODE = 2; // metropolitan mobile + void loop() { while (serialGNSS.available() > 0) { -- cgit v1.2.3