Skyward boardcore
Loading...
Searching...
No Matches
SX1278Lora.h
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Author: Davide Mor
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
26
27#include "SX1278Common.h"
28#include "SX1278LoraTimings.h"
29
30namespace Boardcore
31{
32
34{
35public:
36 static constexpr size_t MTU = 255;
37
41 struct Config
42 {
59
66 enum class Cr
67 {
69 // correction overhead.
71 // correction overhead.
73 // correction overhead.
75 // correction overhead.
76 };
77
84 enum class Sf
85 {
86 // Spreading factor 6 is special, and not supported by this driver
87 // SF_6 = SX1278::Lora::RegModemConfig2::SF_6,
94 };
95
99
101 false; //< Optimize the transmission at high spreading factors.
102
103 int freq_rf = 434000000; //< RF frequency in Hz.
104 int ocp =
105 120; //< Over current protection limit in mA (0 for no limit).
106 int power =
107 13; //< Output power in dB (between +2 and +17 with pa_boost = on,
108 // and between +0 and +14 with pa_boost = off, +20 for +20dBm
109 // max power ignoring pa_boost).
110 bool enable_crc = true; //< Enable hardware CRC calculation/checking
111
116 {
117 using namespace SX1278::Lora;
118
122 static_cast<RegModemConfig1::Bw>(bandwidth)),
123 static_cast<RegModemConfig1::Cr>(coding_rate));
124 }
125 };
126
130 enum class Error
131 {
132 NONE, //< No error encountered.
133 BAD_VALUE, //< A requested value was outside the valid range.
134 BAD_VERSION, //< Chip isn't connected.
135 IRQ_TIMEOUT, //< Timeout on IRQ register.
136 };
137
141 explicit SX1278Lora(SPIBus& bus, miosix::GpioPin cs, miosix::GpioPin dio0,
142 miosix::GpioPin dio1, miosix::GpioPin dio3,
144 std::unique_ptr<SX1278::ISX1278Frontend> frontend)
145 : SX1278Common(bus, cs, dio0, dio1, dio3, clock_divider,
146 std::move(frontend)),
147 crc_enabled(false)
148 {
149 }
150
154 [[nodiscard]] virtual Error init(const Config& config);
155
156 /*
157 * @brief Check if this device is connected.
158 */
159 bool checkVersion();
160
164 [[nodiscard]] virtual Error configure(const Config& config);
165
173 ssize_t receive(uint8_t* pkt, size_t max_len) override;
174
184 bool send(uint8_t* pkt, size_t len) override;
185
189 float getLastRxRssi() override;
190
194 float getLastRxSnr() override;
195
196private:
197 void enterLoraMode();
198
199 void readFifo(uint8_t addr, uint8_t* dst, uint8_t size);
200 void writeFifo(uint8_t addr, uint8_t* src, uint8_t size);
201
202 IrqFlags getIrqFlags() override;
203 void resetIrqFlags(IrqFlags flags) override;
204
205 void setMode(Mode mode) override;
206 void setMapping(SX1278::DioMapping mapping) override;
207
208 bool crc_enabled;
209 PrintLogger logger = Logging::getLogger("sx1278-lora");
210};
211
212} // namespace Boardcore
static PrintLogger getLogger(const string &name)
Driver for STM32 low level SPI peripheral.
Definition SPIBus.h:61
RAII scoped bus lock guard.
float getLastRxSnr() override
Get the RSSI in dBm, during last packet receive.
bool send(uint8_t *pkt, size_t len) override
Send a packet. The function must block until the packet is sent (successfully or not)
ssize_t receive(uint8_t *pkt, size_t max_len) override
Wait until a new packet is received.
virtual Error init(const Config &config)
Setup the device.
SX1278Lora(SPIBus &bus, miosix::GpioPin cs, miosix::GpioPin dio0, miosix::GpioPin dio1, miosix::GpioPin dio3, SPI::ClockDivider clock_divider, std::unique_ptr< SX1278::ISX1278Frontend > frontend)
Construct a new SX1278.
Definition SX1278Lora.h:141
virtual Error configure(const Config &config)
Configure this device on the fly.
static constexpr size_t MTU
Definition SX1278Lora.h:36
float getLastRxRssi() override
Get the RSSI in dBm, during last packet receive.
ClockDivider
SPI Clock divider.
Definition SPIDefs.h:70
constexpr uint32_t bandwidthToInt(Bw bw)
Definition SX1278Defs.h:631
constexpr uint32_t effectiveBitrate(uint32_t spreading_factor, uint32_t bandwidth, uint32_t coding_rate)
Computes the actual usable bitrate in b/s.
This file includes all the types the logdecoder script will decode.
Definition WIZ5500.h:318
Represents an actual Dio mapping..
Definition SX1278Defs.h:93
Requested SX1278 configuration.
Definition SX1278Lora.h:42
Cr
Coding rate of the device.
Definition SX1278Lora.h:67
Bw
Bandwidth of the device.
Definition SX1278Lora.h:47
Sf
Spreading factor of the devide.
Definition SX1278Lora.h:85
uint32_t effectiveBitrate() const
Calculates effective and usable bitrate.
Definition SX1278Lora.h:115