Skyward boardcore
Loading...
Searching...
No Matches
SX1278Fsk.h
Go to the documentation of this file.
1/* Copyright (c) 2021 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
27
28#include <cstdint>
29
30#include "SX1278Common.h"
31
32namespace Boardcore
33{
34
39{
40public:
44 static constexpr size_t MTU = 255;
45
49 struct Config
50 {
78
92
108
109 int freq_rf = 434000000; //< RF Frequency in Hz.
110 int freq_dev = 50000; //< Frequency deviation in Hz.
111 int bitrate = 48000; //< Bitrate in b/s.
112 RxBw rx_bw = RxBw::HZ_125000; //< Rx filter bandwidth.
113 RxBw afc_bw = RxBw::HZ_125000; //< Afc filter bandwidth.
114 int ocp =
115 120; //< Over current protection limit in mA (0 for no limit).
116 int power = 13; //< Output power in dB (Between +2 and +17, or +20 for
117 // full power).
118 Shaping shaping = Shaping::GAUSSIAN_BT_1_0; //< Shaping applied to
119 // the modulation stream.
120 DcFree dc_free = DcFree::WHITENING; //< Dc free encoding scheme.
121 bool enable_crc = true; //< Enable hardware CRC calculation/checking
122 };
123
127 enum class Error
128 {
129 NONE, //< No error encountered.
130 BAD_VALUE, //< A requested value was outside the valid range.
131 BAD_VERSION, //< Chip isn't connected.
132 IRQ_TIMEOUT, //< Timeout on IRQ register.
133 };
134
138 explicit SX1278Fsk(SPIBus& bus, miosix::GpioPin cs, miosix::GpioPin dio0,
139 miosix::GpioPin dio1, miosix::GpioPin dio3,
141 std::unique_ptr<SX1278::ISX1278Frontend> frontend)
142 : SX1278Common(bus, cs, dio0, dio1, dio3, clock_divider,
143 std::move(frontend)),
144 crc_enabled(false)
145 {
146 }
147
151 [[nodiscard]] virtual Error init(const Config& config);
152
153 /*
154 * @brief Check if this device is connected.
155 */
156 bool checkVersion();
157
161 [[nodiscard]] virtual Error configure(const Config& config);
162
170 ssize_t receive(uint8_t* pkt, size_t max_len) override;
171
181 bool send(uint8_t* pkt, size_t len) override;
182
186 float getCurRssi();
187
191 float getLastRxRssi() override;
192
196 float getLastRxFei() override;
197
198private:
199 void enterFskMode();
200
201 void rateLimitTx();
202
203 IrqFlags getIrqFlags() override;
204 void resetIrqFlags(IrqFlags flags) override;
205
206 float getRssi();
207 float getFei();
208
209 void setMode(Mode mode) override;
210 void setMapping(SX1278::DioMapping mapping) override;
211
212 bool crc_enabled;
213 long long last_tx = 0;
214 float last_rx_rssi = 0.0f;
215 PrintLogger logger = Logging::getLogger("sx1278-fsk");
216};
217
218} // namespace Boardcore
static PrintLogger getLogger(const string &name)
Driver for STM32 low level SPI peripheral.
Definition SPIBus.h:61
RAII scoped bus lock guard.
Various SX1278 register/enums definitions.
Definition SX1278Fsk.h:39
virtual Error init(const Config &config)
Setup the device.
Definition SX1278Fsk.cpp:46
virtual Error configure(const Config &config)
Configure this device on the fly.
Definition SX1278Fsk.cpp:76
static constexpr size_t MTU
Maximum transmittable unit of this driver.
Definition SX1278Fsk.h:44
float getLastRxFei() override
Get the frequency error index in Hz, during last packet receive.
float getLastRxRssi() override
Get the RSSI in dBm, during last packet receive.
float getCurRssi()
Get the current perceived RSSI in dBm.
bool send(uint8_t *pkt, size_t len) override
Send a packet. The function must block until the packet is sent (successfully or not)
SX1278Fsk(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 SX1278Fsk.h:138
ssize_t receive(uint8_t *pkt, size_t max_len) override
Wait until a new packet is received.
ClockDivider
SPI Clock divider.
Definition SPIDefs.h:70
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 SX1278Fsk.h:50
RxBw
Channel filter bandwidth.
Definition SX1278Fsk.h:55
DcFree
Dc free encoding.
Definition SX1278Fsk.h:103
Shaping
Output modulation shaping.
Definition SX1278Fsk.h:83