Skyward boardcore
Loading...
Searching...
No Matches
SX1278Defs.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 <cstdint>
28
29namespace Boardcore
30{
31
32namespace SX1278
33{
34
38constexpr int FXOSC = 32000000;
39
43constexpr float FSTEP = 61.03515625;
44
46{
47 SPIBusConfig bus_config = {};
48 bus_config.clockDivider = clock_divider;
49 bus_config.mode = SPI::Mode::MODE_0;
50 bus_config.bitOrder = SPI::Order::MSB_FIRST;
51 bus_config.byteOrder = SPI::Order::MSB_FIRST;
52 bus_config.writeBit = SPI::WriteBit::INVERTED;
53 bus_config.csHoldTimeUs = 3;
54
55 return bus_config;
56}
57
58constexpr int MIN_FREQ_DEV = 0;
59constexpr int MAX_FREQ_DEV = 0x3fff * FSTEP;
60
61constexpr int MIN_FREQ_RF = 0;
62constexpr int MAX_FREQ_RF = 0xffffff * FSTEP;
63
67enum class Dio
68{
69 DIO0 = 0,
70 DIO1 = 1,
71 DIO2 = 2,
72 DIO3 = 3,
73 DIO4 = 4,
74 DIO5 = 5
75};
76
77namespace RegDioMapping
78{
79
80inline constexpr uint16_t make(int dio0, int dio1, int dio2, int dio3, int dio4,
81 int dio5, bool map_preamble_detect)
82{
83 return (((dio0 & 0b11) << 14) | ((dio1 & 0b11) << 12) |
84 ((dio2 & 0b11) << 10) | ((dio3 & 0b11) << 8) |
85 ((dio4 & 0b11) << 6) | ((dio5 & 0b11) << 4) |
86 (map_preamble_detect ? 1 : 0));
87}
88
92struct Mapping
93{
94 constexpr Mapping() : raw(0) {}
95 constexpr Mapping(int dio0, int dio1, int dio2, int dio3, int dio4,
96 int dio5, bool map_preamble_detect = false)
97 : raw(make(dio0, dio1, dio2, dio3, dio4, dio5, map_preamble_detect))
98 {
99 }
100
101 int getMapping(Dio dio) const
102 {
103 switch (dio)
104 {
105 case Dio::DIO0:
106 return (raw >> 14) & 0b11;
107 case Dio::DIO1:
108 return (raw >> 12) & 0b11;
109 case Dio::DIO2:
110 return (raw >> 10) & 0b11;
111 case Dio::DIO3:
112 return (raw >> 8) & 0b11;
113 case Dio::DIO4:
114 return (raw >> 6) & 0b11;
115 case Dio::DIO5:
116 return (raw >> 4) & 0b11;
117 default:
118 return 0;
119 }
120 }
121
122 bool operator==(const Mapping& other) const { return raw == other.raw; }
123
124 bool operator!=(const Mapping& other) const { return raw != other.raw; }
125
126 uint16_t raw;
127};
128
129} // namespace RegDioMapping
130
134namespace Fsk
135{
136
140constexpr int FIFO_LEN = 64;
141
142namespace RegOpMode
143{
144
150
152{
153 MODE_SLEEP = 0b000,
154 MODE_STDBY = 0b001,
155 MODE_FSTX = 0b010,
156 MODE_TX = 0b011,
157 MODE_FSRX = 0b100,
158 MODE_RX = 0b101,
159};
160
161inline constexpr uint8_t make(Mode mode, bool low_frequency_mode_on,
162 ModulationType modulation_type)
163{
164 return mode | (low_frequency_mode_on ? (1 << 3) : 0) |
165 (modulation_type << 5);
166}
167
168} // namespace RegOpMode
169
170namespace RegPaConfig
171{
172
173inline constexpr uint8_t make(uint8_t output_power, uint8_t max_power,
174 bool pa_select)
175{
176 return (output_power & 0b1111) | ((max_power & 0b111) << 4) |
177 (pa_select ? 1 << 7 : 0);
178}
179
180} // namespace RegPaConfig
181
182namespace RegPaRamp
183{
191
193{
195 PA_RAMP_MS_2 = 0b0001,
196 PA_RAMP_MS_1 = 0b0010,
210};
211
212inline constexpr uint8_t make(PaRamp pa_ramp,
213 ModulationShaping modulation_shaping)
214{
215 return pa_ramp | (modulation_shaping << 5);
216}
217
218} // namespace RegPaRamp
219
220namespace RegOcp
221{
222
223inline constexpr uint8_t make(uint8_t ocp_trim, bool ocp_on)
224{
225 return (ocp_trim & 0b11111) | (ocp_on ? 1 << 5 : 0);
226}
227
228} // namespace RegOcp
229
230namespace RegRxConfig
231{
232
233inline constexpr uint8_t make(bool rx_trigger_rssi_interrupt,
234 bool rx_trigger_preable_detect, bool agc_auto_on,
235 bool afc_auto_on, bool restart_rx_with_pll_lock,
236 bool restart_rx_without_pll_lock,
237 bool restart_rx_on_collision)
238{
239 return (rx_trigger_rssi_interrupt ? 0b001 : 0) |
240 (rx_trigger_preable_detect ? 0b110 : 0) |
241 (agc_auto_on ? 1 << 3 : 0) | (afc_auto_on ? 1 << 4 : 0) |
242 (restart_rx_with_pll_lock ? 1 << 5 : 0) |
243 (restart_rx_without_pll_lock ? 1 << 6 : 0) |
244 (restart_rx_on_collision ? 1 << 7 : 0);
245}
246
247} // namespace RegRxConfig
248
249namespace RegRxBw
250{
251
253{
254 HZ_2600 = 0b10111,
255 HZ_3100 = 0b01111,
256 HZ_3900 = 0b00111,
257 HZ_5200 = 0b10110,
258 HZ_6300 = 0b01110,
259 HZ_7800 = 0b00110,
260 HZ_10400 = 0b10101,
261 HZ_12500 = 0b01101,
262 HZ_15600 = 0b00101,
263 HZ_20800 = 0b10100,
264 HZ_25000 = 0b01100,
265 HZ_31300 = 0b00100,
266 HZ_41700 = 0b10011,
267 HZ_50000 = 0b01011,
268 HZ_62500 = 0b00011,
269 HZ_83300 = 0b10010,
270 HZ_100000 = 0b01010,
271 HZ_125000 = 0b00010,
272 HZ_166700 = 0b10001,
273 HZ_200000 = 0b01001,
274 HZ_250000 = 0b00001,
275};
276
277inline constexpr uint8_t make(RxBw rx_bw) { return rx_bw; }
278
279} // namespace RegRxBw
280
281namespace RegAfcBw
282{
283
285
286inline constexpr uint8_t make(RxBwAfc rx_bw_afc) { return rx_bw_afc; }
287
288} // namespace RegAfcBw
289
290namespace RegPreambleDetector
291{
292
299
300inline constexpr uint8_t make(int tol, Size size, bool on)
301{
302 return (tol & 0b11111) | (size << 5) | (on ? 1 << 7 : 0);
303}
304
305} // namespace RegPreambleDetector
306
307namespace RegSyncConfig
308{
315
321
322inline constexpr uint8_t make(int size, bool on,
323 PreamblePolarity preamble_polarity,
324 AutoRestartRxMode auto_restart_rx_mode)
325{
326 return ((size - 1) & 0b111) | (on ? 1 << 4 : 0) | (preamble_polarity << 5) |
327 (auto_restart_rx_mode << 6);
328}
329
330} // namespace RegSyncConfig
331
332namespace RegPacketConfig1
333{
339
346
353
359
360inline constexpr uint8_t make(CrcWhiteningType crc_whitening_type,
361 AddressFiltering address_filtering,
362 bool crc_auto_clear_off, bool crc_on,
363 DcFree dc_free, PacketFormat packet_format)
364{
365 return crc_whitening_type | (address_filtering << 1) |
366 (crc_auto_clear_off ? 1 << 3 : 0) | (crc_on ? 1 << 4 : 0) |
367 (dc_free << 5) | (packet_format << 7);
368}
369
370} // namespace RegPacketConfig1
371
372namespace RegPacketConfig2
373{
379
380inline constexpr uint8_t make(bool beacon_on, bool io_home_power_frame,
381 bool io_home_on, DataMode data_mode)
382{
383 return (beacon_on ? 1 << 3 : 0) | (io_home_power_frame ? 1 << 4 : 0) |
384 (io_home_on ? 1 << 5 : 0) | (data_mode << 6);
385}
386
387} // namespace RegPacketConfig2
388
389namespace RegFifoThresh
390{
396
397inline constexpr uint8_t make(int fifo_threshold,
398 TxStartCondition tx_start_condition)
399{
400 return (fifo_threshold & 0b111111) | (tx_start_condition << 7);
401}
402
403} // namespace RegFifoThresh
404
405namespace RegIrqFlags
406{
407
409{
410 MODE_READY = 1 << 15,
411 RX_READY = 1 << 14,
412 TX_READY = 1 << 13,
413 PILL_LOCK = 1 << 12,
414 RSSI = 1 << 11,
415 TIMEOUT = 1 << 10,
418 FIFO_FULL = 1 << 7,
419 FIFO_EMPTY = 1 << 6,
420 FIFO_LEVEL = 1 << 5,
421 FIFO_OVERRUN = 1 << 4,
422 PACKET_SENT = 1 << 3,
424 CRC_OK = 1 << 1,
425 LOW_BAT = 1 << 0,
426};
427
428} // namespace RegIrqFlags
429
430namespace RegPaDac
431{
437
438inline constexpr uint8_t make(PaDac pa_dac) { return pa_dac | (0x10 << 3); }
439
440} // namespace RegPaDac
441
443{
444 REG_FIFO = 0x00,
445
446 // Registers for common settings
455
456 // Registers for the transmitter
459 REG_OCP = 0x0b,
460
461 // Registers for the receiver
462 REG_LNA = 0x0c,
468 REG_RX_BW = 0x12,
473 // Reserved 17 to 19
484
485 // RC Oscillator registers
486 REG_OSC = 0x24,
487
488 // Packet handling registers
506
507 // Sequencer registers
513
514 // Service registers
516 REG_TEMP = 0x3c,
518
519 // Status registers
522
523 // IO Control registers
526
527 // Version register
529
530 // Additional registers
532 REG_TCXO = 0x4b,
541};
542
543} // namespace Fsk
544
548namespace Lora
549{
550
551namespace RegOpMode
552{
553
555{
556 MODE_SLEEP = 0b000,
557 MODE_STDBY = 0b001,
558 MODE_FSTX = 0b010,
559 MODE_TX = 0b011,
560 MODE_FSRX = 0b100,
563 MODE_CAD = 0b111
565
566inline constexpr uint8_t make(Mode mode, bool low_frequency_mode_on,
567 bool access_shared_reg)
568{
569 return mode | (low_frequency_mode_on ? (1 << 3) : 0) |
570 (access_shared_reg ? (1 << 6) : 0) | (1 << 7);
571}
572
573} // namespace RegOpMode
574
575namespace RegPaConfig
576{
577
578inline constexpr uint8_t make(uint8_t output_power, uint8_t max_power,
579 bool pa_select)
580{
581 return (output_power & 0b1111) | ((max_power & 0b111) << 4) |
582 (pa_select ? 1 << 7 : 0);
583}
584
585} // namespace RegPaConfig
586
587namespace RegOcp
588{
589
590inline constexpr uint8_t make(uint8_t ocp_trim, bool ocp_on)
591{
592 return (ocp_trim & 0b11111) | (ocp_on ? 1 << 5 : 0);
593}
594
595} // namespace RegOcp
596
597namespace RegIrqFlags
598{
599
601{
602 RX_TIMEOUT = 1 << 7,
603 RX_DONE = 1 << 6,
605 VALID_HEADER = 1 << 4,
606 TX_DONE = 1 << 3,
607 CAD_DONE = 1 << 2,
609 CAD_DETECTED = 1 << 0,
610};
611
612}
613
614namespace RegModemConfig1
615{
616
617enum Bw
618{
619 BW_HZ_7800 = 0b0000,
620 BW_HZ_10400 = 0b0001,
621 BW_HZ_15600 = 0b0010,
622 BW_HZ_20800 = 0b0011,
623 BW_HZ_31250 = 0b0100,
624 BW_HZ_41700 = 0b0101,
625 BW_HZ_62500 = 0b0110,
626 BW_HZ_125000 = 0b0111,
627 BW_HZ_250000 = 0b1000,
628 BW_HZ_500000 = 0b1001,
629};
630
631inline constexpr uint32_t bandwidthToInt(Bw bw)
632{
633 switch (bw)
634 {
636 return 7800;
638 return 10400;
640 return 15600;
642 return 20800;
644 return 31250;
646 return 41700;
648 return 62500;
650 return 125000;
652 return 250000;
654 return 500000;
655 }
656
657 // Gcc complains...
658 return 0;
659}
660
661enum Cr
662{
663 CR_1 = 0b001,
664 CR_2 = 0b010,
665 CR_3 = 0b011,
666 CR_4 = 0b100
668
669inline constexpr uint8_t make(bool implicit_mode_on, Cr coding_rate, Bw bw)
670{
671 return (implicit_mode_on ? 1 : 0) | (coding_rate << 1) | (bw << 4);
672}
673
674} // namespace RegModemConfig1
675
676namespace RegModemConfig2
677{
678
679enum Sf
680{
681 SF_6 = 6,
682 SF_7 = 7,
683 SF_8 = 8,
684 SF_9 = 9,
685 SF_10 = 10,
686 SF_11 = 11,
687 SF_12 = 12,
688};
689
690inline constexpr uint8_t make(bool rx_payload_crc_on, bool tx_continuous_mode,
691 Sf spreading_factor)
692{
693 return (rx_payload_crc_on ? 1 << 2 : 0) |
694 (tx_continuous_mode ? 1 << 3 : 0) | (spreading_factor << 4);
695}
696
697} // namespace RegModemConfig2
698
699namespace RegModemConfig3
700{
701
702inline constexpr uint8_t make(bool agc_auto_on, bool low_data_rate_optimize)
703{
704 return (agc_auto_on ? 1 << 2 : 0) | (low_data_rate_optimize ? 1 << 3 : 0);
705}
706
707} // namespace RegModemConfig3
708
709namespace RegDetectOptimize
710{
711
712inline constexpr uint8_t make(uint8_t detection_optimize, bool automatic_if_on)
713{
714 return (detection_optimize & 0b11) | (automatic_if_on ? 1 << 7 : 0);
715}
716
717} // namespace RegDetectOptimize
718
719namespace RegPaDac
720{
721
722inline constexpr uint8_t make(bool pa_boost)
723{
724 return (pa_boost ? 0x07 : 0x04) | (0x10 << 3);
725}
726
727} // namespace RegPaDac
728
730{
731 REG_FIFO = 0x00,
732
733 // Registers for common settings
738
739 // Registers for RF blocks
742 REG_OCP = 0x0b,
743 REG_LNA = 0x0c,
744
745 // Lora page registers
786
787 // IO Control registers
790
791 // Version register
793
794 // Additional registers
795 REG_TCXO = 0x4b,
803};
804
805} // namespace Lora
806
807} // namespace SX1278
808
809} // namespace Boardcore
@ MODE_0
CPOL = 0, CPHA = 0 -> Clock low when idle, sample on first edge.
ClockDivider
SPI Clock divider.
Definition SPIDefs.h:70
@ INVERTED
Inverted write bit settings (1 for write, 0 for reads)
constexpr uint8_t make(RxBwAfc rx_bw_afc)
Definition SX1278Defs.h:286
constexpr uint8_t make(int fifo_threshold, TxStartCondition tx_start_condition)
Definition SX1278Defs.h:397
constexpr uint8_t make(uint8_t ocp_trim, bool ocp_on)
Definition SX1278Defs.h:223
constexpr uint8_t make(Mode mode, bool low_frequency_mode_on, ModulationType modulation_type)
Definition SX1278Defs.h:161
constexpr uint8_t make(uint8_t output_power, uint8_t max_power, bool pa_select)
Definition SX1278Defs.h:173
constexpr uint8_t make(PaDac pa_dac)
Definition SX1278Defs.h:438
constexpr uint8_t make(PaRamp pa_ramp, ModulationShaping modulation_shaping)
Definition SX1278Defs.h:212
constexpr uint8_t make(CrcWhiteningType crc_whitening_type, AddressFiltering address_filtering, bool crc_auto_clear_off, bool crc_on, DcFree dc_free, PacketFormat packet_format)
Definition SX1278Defs.h:360
constexpr uint8_t make(bool beacon_on, bool io_home_power_frame, bool io_home_on, DataMode data_mode)
Definition SX1278Defs.h:380
constexpr uint8_t make(int tol, Size size, bool on)
Definition SX1278Defs.h:300
constexpr uint8_t make(RxBw rx_bw)
Definition SX1278Defs.h:277
constexpr uint8_t make(bool rx_trigger_rssi_interrupt, bool rx_trigger_preable_detect, bool agc_auto_on, bool afc_auto_on, bool restart_rx_with_pll_lock, bool restart_rx_without_pll_lock, bool restart_rx_on_collision)
Definition SX1278Defs.h:233
constexpr uint8_t make(int size, bool on, PreamblePolarity preamble_polarity, AutoRestartRxMode auto_restart_rx_mode)
Definition SX1278Defs.h:322
constexpr int FIFO_LEN
Length of the internal FIFO.
Definition SX1278Defs.h:140
constexpr uint8_t make(uint8_t detection_optimize, bool automatic_if_on)
Definition SX1278Defs.h:712
constexpr uint8_t make(bool implicit_mode_on, Cr coding_rate, Bw bw)
Definition SX1278Defs.h:669
constexpr uint32_t bandwidthToInt(Bw bw)
Definition SX1278Defs.h:631
constexpr uint8_t make(bool rx_payload_crc_on, bool tx_continuous_mode, Sf spreading_factor)
Definition SX1278Defs.h:690
constexpr uint8_t make(bool agc_auto_on, bool low_data_rate_optimize)
Definition SX1278Defs.h:702
constexpr uint8_t make(uint8_t ocp_trim, bool ocp_on)
Definition SX1278Defs.h:590
constexpr uint8_t make(Mode mode, bool low_frequency_mode_on, bool access_shared_reg)
Definition SX1278Defs.h:566
constexpr uint8_t make(uint8_t output_power, uint8_t max_power, bool pa_select)
Definition SX1278Defs.h:578
constexpr uint8_t make(bool pa_boost)
Definition SX1278Defs.h:722
constexpr uint16_t make(int dio0, int dio1, int dio2, int dio3, int dio4, int dio5, bool map_preamble_detect)
Definition SX1278Defs.h:80
constexpr int MIN_FREQ_DEV
Definition SX1278Defs.h:58
constexpr float FSTEP
Frequency step (Hz) used in some calculations.
Definition SX1278Defs.h:43
Dio
Represents a DIO..
Definition SX1278Defs.h:68
SPIBusConfig getSpiBusConfig(SPI::ClockDivider clock_divider)
Definition SX1278Defs.h:45
constexpr int MAX_FREQ_DEV
Definition SX1278Defs.h:59
constexpr int MIN_FREQ_RF
Definition SX1278Defs.h:61
constexpr int MAX_FREQ_RF
Definition SX1278Defs.h:62
constexpr int FXOSC
Main oscillator frequency (Hz)
Definition SX1278Defs.h:38
This file includes all the types the logdecoder script will decode.
SPI Bus configuration for a specific slave.
SPI::ClockDivider clockDivider
< Peripheral clock division
Represents an actual Dio mapping..
Definition SX1278Defs.h:93
bool operator==(const Mapping &other) const
Definition SX1278Defs.h:122
bool operator!=(const Mapping &other) const
Definition SX1278Defs.h:124
constexpr Mapping(int dio0, int dio1, int dio2, int dio3, int dio4, int dio5, bool map_preamble_detect=false)
Definition SX1278Defs.h:95