A LoRa APRS node with KISS interface based on a Raspberry Pi Pico
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

158 lines
3.9 KiB

/*********************************************************************************
*
* lora_aprs_node_pico is a LoRa APRS KISS modem with additional PE1RXF telemetry
* capabilities. It runs on a Raspberry Pi Pico.
*
* (C)2023 M.T. Konstapel https://meezenest.nl/mees
*
* This file is part of lora_aprs_node_pico.
*
* lora_aprs_node_pico is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* lora_aprs_node_pico is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with lora_aprs_node_pico. If not, see <https://www.gnu.org/licenses/>.
*
**********************************************************************************/
#ifndef LORA_H
#define LORA_H
// // #include <Arduino.h>
// #include <SPI.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/gpio.h"
#include "hardware/spi.h"
#include "string.h"
#include "Print.h"
#define PIN_MISO 16
#define PIN_CS 8
#define PIN_SCK 18
#define PIN_MOSI 19
#define SPI_PORT spi0
#define READ_BIT 0x80
#define LORA_DEFAULT_SPI spi0
#define LORA_DEFAULT_SPI_FREQUENCY 8E6
#define LORA_DEFAULT_SS_PIN 8
#define LORA_DEFAULT_RESET_PIN 11
#define LORA_DEFAULT_DIO0_PIN 10
#define LORA_DEFAULT_DIO1_PIN 9
#endif
#define PA_OUTPUT_RFO_PIN 0
#define PA_OUTPUT_PA_BOOST_PIN 1
static void __empty();
//class LoRaClass : public Stream {
class LoRaClass : public Print {
public:
LoRaClass();
int begin(long frequency);
void end();
int beginPacket(int implicitHeader = false);
int endPacket(bool async = false);
int parsePacket(int size = 0);
int packetRssi();
float packetSnr();
long packetFrequencyError();
int rssi();
// from Print
virtual size_t write(uint8_t byte);
virtual size_t write(const uint8_t *buffer, size_t size);
// from Stream
virtual int available();
virtual int read();
virtual int peek();
virtual void flush();
void onReceive(void(*callback)(int));
void onTxDone(void(*callback)());
void receive(int size = 0);
void idle();
void sleep();
// size_t print(const char* c);
void setTxPower(int level, int outputPin = PA_OUTPUT_PA_BOOST_PIN);
void setFrequency(long frequency);
void setSpreadingFactor(int sf);
void setSignalBandwidth(long sbw);
void setCodingRate4(int denominator);
void setPreambleLength(long length);
void setSyncWord(int sw);
void enableCrc();
void disableCrc();
void enableInvertIQ();
void disableInvertIQ();
void setOCP(uint8_t mA); // Over Current Protection control
void setGain(uint8_t gain); // Set LNA gain
// deprecated
void crc() { enableCrc(); }
void noCrc() { disableCrc(); }
uint8_t random();
void setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN);
void setSPI(spi_inst_t& spi);
void setSPIFrequency(uint32_t frequency);
void dumpRegisters();
private:
void explicitHeaderMode();
void implicitHeaderMode();
void handleDio0Rise();
bool isTransmitting();
int getSpreadingFactor();
long getSignalBandwidth();
void setLdoFlag();
uint8_t readRegister(uint8_t address);
void writeRegister(uint8_t address, uint8_t value);
uint8_t singleTransfer(uint8_t address, uint8_t value);
static void onDio0Rise(uint, uint32_t);
private:
// SPISettings _spiSettings;
spi_inst_t* _spi;
int _ss;
int _reset;
int _dio0;
long _frequency;
int _packetIndex;
int _implicitHeaderMode;
void (*_onReceive)(int);
void (*_onTxDone)();
};
extern LoRaClass LoRa;
// #endif