Solaris Software
Loading...
Searching...
No Matches
sx1262.c
Go to the documentation of this file.
1/**
2 * @file sx1262.c
3 * @brief sx1262 module telemetry + SPP service implementation.
4 */
5
7
8#include "spp/hal/spi/spi.h"
9#include "spp/hal/gpio/gpio.h"
10#include "spp/hal/time/time.h"
11
12#include <stdio.h>
13
14/* -----------------------------------------
15 STRUCTS
16--------------------------------------------*/
18 .p_spi = NULL,
19 .frame_number = 1,
20 .tx_state = SX1262_TX_STATE_IDLE,
21 .timer_ms = 0,
22};
23
24/* -----------------------------------------
25 VARIABLES
26--------------------------------------------*/
31
32
33volatile spp_bool_t g_txDoneFlag = false; // volatile to avoid optimizations
35
36/* -----------------------------------------
37 STATIC DECLARATIONS
38--------------------------------------------*/
42
43static SPP_RetVal_t SX1262_WaitBusy(void);
44static SPP_RetVal_t SX1262_SetStandby(void *);
47static SPP_RetVal_t SX1262_SetPaConfig(void *);
49static SPP_RetVal_t SX1262_SetTxParams(void *);
60
64static SPP_RetVal_t SX1262_Calibrate(void *);
67
69 .priority = 3U,
70 .p_nameConsumer = "sx1262",
71 .tiemoutMs = K_SX1262_TASK_TIMEOUT_MS,
73 .deliverToMailbox = SPP_SERVICES_SX1262_deliverToMailbox,
75};
76
77/* -----------------------------------------
78 PUBLIC FUNCTIONS
79--------------------------------------------*/
84
85
86/* -----------------------------------------
87 STATIC FUNCTIONS IMPLEMENTATION
88--------------------------------------------*/
89
90/**
91 * @brief Initializes the SX1262 radio for LoRa transmission.
92 *
93 * Initializes SPI and GPIO, resets the radio, configures the TCXO,
94 * regulator, RF frequency, modulation, transmission parameters
95 * and DIO1 interrupts.
96 *
97 * @return K_SPP_OK on success, error code otherwise.
98 */
100{
102
104 if (s_sx1262Ctx.p_spi == NULL)
105 {
106 printf("SX1262 INIT FAIL: getHandle\n");
107 return K_SPP_ERROR;
108 }
109
111
112 if (ret != K_SPP_OK)
113 {
114 printf("SX1262 INIT FAIL: deviceInit ret=%d\n", (int)ret);
115 return ret;
116 }
117
118 void *p_spi = s_sx1262Ctx.p_spi;
119
120 ret = SPP_HAL_GPIO_configInterrupt(SX1262_BUSY_PIN, 0, 0); // Gitanada pa poñer o BUSY como input
121 if (ret != K_SPP_OK)
122 {
123 printf("SX1262 INIT FAIL: BUSY pin ret=%d\n", (int)ret);
124 return ret;
125 }
126
127 // Hardware Reset
132
133 ret = SX1262_InitInterrupts();
134 if (ret != K_SPP_OK)
135 {
136 printf("SX1262 INIT FAIL: DIO1 init ret=%d\n", (int)ret);
137 return ret;
138 }
139
140 // Sequence of basic and radio configuration
141 ret = SX1262_SetStandby(p_spi);
142 if (ret != K_SPP_OK)
143 return ret;
144
145 ret = SX1262_SetRegulatorMode(p_spi);
146 if (ret != K_SPP_OK)
147 {
148 return ret;
149 }
150
151 ret = SX1262_SetDio3AsTcxoCtrl(p_spi);
152 if (ret != K_SPP_OK)
153 {
154 return ret;
155 }
156
157 ret = SX1262_ClearDeviceErrors(p_spi);
158 if (ret != K_SPP_OK)
159 {
160 return ret;
161 }
162
163 ret = SX1262_Calibrate(p_spi);
164 if (ret != K_SPP_OK)
165 {
166 return ret;
167 }
168
169 ret = SX1262_SetPacketType(p_spi);
170 if (ret != K_SPP_OK)
171 return ret;
172
173 ret = SX1262_SetRfFrequency(p_spi);
174 if (ret != K_SPP_OK)
175 return ret;
176
177 ret = SX1262_SetPaConfig(p_spi);
178 if (ret != K_SPP_OK)
179 return ret;
180
181 ret = SX1262_CalibrateImage(p_spi);
182 if (ret != K_SPP_OK)
183 return ret;
184
185 ret = SX1262_SetTxParams(p_spi);
186 if (ret != K_SPP_OK)
187 return ret;
188
189 ret = SX1262_SetBufferBaseAddress(p_spi);
190 if (ret != K_SPP_OK)
191 return ret;
192
193 // Modulation and Lora packet parameters
194 const spp_uint8_t lora_mod_params[4] = {
195 0x07, // Spreading Factor = 7
196 0x05, // BW = 250 kHz
197 0x01, // Code Rate = 4/5
198 0x00 // LDRO OFF (same as devkit)
199 };
200 ret = SX1262_SetModulationParams(p_spi, lora_mod_params[0], lora_mod_params[1], lora_mod_params[2],
201 lora_mod_params[3]);
202 if (ret != K_SPP_OK)
203 return ret;
204
205 // Sync Word Configuration to Private Network (0x1424)
206 spp_uint8_t syncWord[2] = {0x14, 0x24};
207 ret = SX1262_WriteRegister(p_spi, 0x0740, syncWord, 2);
208 if (ret != K_SPP_OK)
209 return ret;
210
211 // Interrupt configuration in DIO1 (TxDone + Timeout)
212 ret = SX1262_SetDioIrqParams(p_spi, 0x0201, 0x0201, 0x0000, 0x0000);
213
214 if (ret != K_SPP_OK)
215 {
216 return ret;
217 }
218
219 spp_uint16_t deviceErrors = 0U;
220
221 ret = SX1262_GetDeviceErrors(p_spi, &deviceErrors);
222 if (ret != K_SPP_OK)
223 {
224 printf("SX1262 INIT FAIL: GetDeviceErrors ret=%d\n", (int)ret);
225 return ret;
226 }
227
228 if (deviceErrors != 0U)
229 {
230 printf("SX1262 INIT FAIL: device errors=0x%04X\n", deviceErrors);
231 return K_SPP_ERROR;
232 }
233
234 printf("SX1262 INIT OK\n");
235
236 return ret;
237}
238
239/**
240 * @brief Copies a received SPP packet into the transmission mailbox.
241 *
242 * The mailbox is a circular buffer. If full, the oldest packet
243 * is overwritten.
244 *
245 * @param pqt Packet received from PUBSUB.
246 * @return K_SPP_OK.
247 */
249{
252
254 {
255 mailboxCount++; // we use mailboxCount to know how many packets we have to send later
256 }
257 else
258 {
260 }
261
262 return K_SPP_OK;
263}
264
265/**
266 * @brief Advances the radio transmission state machine.
267 *
268 * Processes one transmission stage per call: IDLE, WRITE_BUFFER,
269 * SET_PARAMS, SET_TX or WAIT_TX_DONE.
270 *
271 * Transmits 64-byte SPP packets individually. If more than
272 * K_SX1262_MAX_PACKETS_TO_TRANSMIT packets are queued when
273 * transmission starts, the oldest excess packets are discarded.
274 *
275 * Transmission completion is verified through DIO1 and IRQ status.
276 * Timeouts clear the transmission mailbox.
277 *
278 * @param p_data Unused.
279 * @return K_SPP_OK during normal operation, error code on
280 * transmission failure or timeout.
281 */
283{
284 (void)p_data;
285
286 if (s_sx1262Ctx.p_spi == NULL)
287 {
288 return K_SPP_ERROR;
289 }
290
292 spp_uint32_t current_time = SPP_HAL_TIME_getTimeMs();
294
295 switch (s_sx1262Ctx.tx_state)
296 {
298 if (mailboxCount > 0U)
299 {
301 {
303
305
307 }
308
309 s_sx1262Ctx.timer_ms = current_time;
311 }
312 break;
313
315 if (busy_status == 1U)
316 {
317 if ((current_time - s_sx1262Ctx.timer_ms) >= SX1262_MAX_BUSY_TIMEOUT_MS)
318 {
320 }
321
322 return K_SPP_OK;
323 }
324
327
328 if (ret != K_SPP_OK)
329 {
330 printf("SX1262 TX FAIL: WriteBuffer ret=%d\n", (int)ret);
331 return ret;
332 }
333
334 s_sx1262Ctx.timer_ms = current_time;
336 break;
337
339 if (busy_status == 1U)
340 {
341 if ((current_time - s_sx1262Ctx.timer_ms) >= SX1262_MAX_BUSY_TIMEOUT_MS)
342 {
344 }
345
346 return K_SPP_OK;
347 }
348
349 // Preamble = 8 symbols, variable packet length, CRC ON
351
352 if (ret != K_SPP_OK)
353 {
354 printf("SX1262 TX FAIL: SetPacketParams ret=%d\n", (int)ret);
355 return ret;
356 }
357
358 s_sx1262Ctx.timer_ms = current_time;
360 break;
361
363 if (busy_status == 1U)
364 {
365 if ((current_time - s_sx1262Ctx.timer_ms) >= SX1262_MAX_BUSY_TIMEOUT_MS)
366 {
368 }
369
370 return K_SPP_OK;
371 }
372
373 g_txDoneFlag = false;
374
375 ret = SX1262_SetTx(s_sx1262Ctx.p_spi, 0x00FA00U);
376
377 if (ret != K_SPP_OK)
378 {
379 printf("SX1262 TX FAIL: SetTx ret=%d\n", (int)ret);
380 return ret;
381 }
382
383 s_sx1262Ctx.timer_ms = current_time;
385 break;
386
388
389 if ((current_time - s_sx1262Ctx.timer_ms) >= SX1262_MAX_DONE_TIMEOUT_MS)
390 {
391 printf("SX1262 TX FAIL: completion timeout\n");
393 }
394
395 if (g_txDoneFlag == false)
396 {
397 return K_SPP_OK;
398 }
399
400 spp_uint16_t irqStatus = 0U;
401
402 ret = SX1262_GetIrqStatus(s_sx1262Ctx.p_spi, &irqStatus);
403
404 if (ret != K_SPP_OK)
405 {
406 printf("SX1262 TX FAIL: GetIrqStatus ret=%d\n", (int)ret);
407
408 return K_SPP_OK;
409 }
410
411 if ((irqStatus & SX1262_IRQ_TIMEOUT) != 0U)
412 {
413 printf("SX1262 TX FAIL: radio timeout\n");
415 }
416
417 if ((irqStatus & SX1262_IRQ_TX_DONE) == 0U)
418 {
419 printf("SX1262 TX FAIL: unexpected IRQ\n");
421 }
422
424
425 if (ret != K_SPP_OK)
426 {
427 printf("SX1262 TX FAIL: ClearIrqStatus ret=%d\n", (int)ret);
428 return K_SPP_OK;
429 }
430
431 g_txDoneFlag = false;
432
434
436
437 mailboxCount--;
438
440 break;
441
442 default:
443 return K_SPP_ERROR;
444 }
445
446 return K_SPP_OK;
447}
448
449
450/* -----------------------------------------
451 STATE CHANGES
452--------------------------------------------*/
453static SPP_RetVal_t SX1262_SetStandby(void *p_handle)
454{
455 spp_uint8_t buffer[2];
456 buffer[0] = SX1262_OPCODE_SET_STANDBY; // Opcode SetStandby
457 buffer[1] = 0x00; // STDBY_RC (without TXCO)
458
460 if (ret != K_SPP_OK)
461 return ret;
462 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 2);
463}
464
466{
467 spp_uint8_t buffer[2];
468
470 buffer[1] = SX1262_REGULATOR_DC_DC;
471
473 if (ret != K_SPP_OK)
474 {
475 return ret;
476 }
477
478 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, sizeof(buffer));
479}
480
482{
484
485 spp_uint8_t buffer[5];
486
488 buffer[1] = SX1262_TCXO_VOLTAGE_1V7;
489 buffer[2] = (spp_uint8_t)((delay >> 16) & 0xFFU);
490 buffer[3] = (spp_uint8_t)((delay >> 8) & 0xFFU);
491 buffer[4] = (spp_uint8_t)(delay & 0xFFU);
492
494 if (ret != K_SPP_OK)
495 {
496 return ret;
497 }
498
499 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, sizeof(buffer));
500}
501
503{
504 spp_uint8_t buffer[3] = {SX1262_OPCODE_CLEAR_DEVICE_ERRORS, 0x00U, 0x00U};
505
507 if (ret != K_SPP_OK)
508 {
509 return ret;
510 }
511
512 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, sizeof(buffer));
513}
514
515static SPP_RetVal_t SX1262_Calibrate(void *p_handle)
516{
517 spp_uint8_t buffer[2];
518
519 buffer[0] = SX1262_OPCODE_CALIBRATE;
520 buffer[1] = SX1262_CALIBRATE_ALL;
521
523 if (ret != K_SPP_OK)
524 {
525 return ret;
526 }
527
528 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, sizeof(buffer));
529}
530
531static SPP_RetVal_t SX1262_SetTx(void *p_handle, spp_uint32_t timeout)
532{
533 // SetTx requires 1 byte of Opcode and 3 bytes of parameters
534 spp_uint8_t buffer[4];
535
536 buffer[0] = SX1262_OPCODE_SET_TX;
537 buffer[1] = (spp_uint8_t)((timeout >> 16) & 0xFF);
538 buffer[2] = (spp_uint8_t)((timeout >> 8) & 0xFF);
539 buffer[3] = (spp_uint8_t)(timeout & 0xFF);
540
541 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 4);
542}
543
544static SPP_RetVal_t SX1262_GetDeviceErrors(void *p_handle, spp_uint16_t *p_errors)
545{
546 if (p_errors == NULL)
547 {
549 }
550
551 spp_uint8_t buffer[4] = {SX1262_OPCODE_GET_DEVICE_ERRORS, 0x00U, 0x00U, 0x00U};
552
554 if (ret != K_SPP_OK)
555 {
556 return ret;
557 }
558
559 ret = SPP_HAL_SPI_transmitRaw(p_handle, buffer, sizeof(buffer));
560 if (ret != K_SPP_OK)
561 {
562 return ret;
563 }
564
565 *p_errors = ((spp_uint16_t)buffer[2] << 8) | (spp_uint16_t)buffer[3];
566
567 return K_SPP_OK;
568}
569
570/* -----------------------------------------
571 BASIC PARAMETERS
572--------------------------------------------*/
573static SPP_RetVal_t SX1262_SetPacketType(void *p_handle)
574{
575 // LoRa
576 spp_uint8_t buffer[2];
578 buffer[1] = 0x01; // Lora Protocol
579
581 if (ret != K_SPP_OK)
582 return ret;
583 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 2);
584}
585
587{
588 spp_uint8_t buffer[5];
590 buffer[1] = 0x36; // 869,25 MHz
591 buffer[2] = 0x54;
592 buffer[3] = 0x00;
593 buffer[4] = 0x00;
594
596 if (ret != K_SPP_OK)
597 return ret;
598 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 5);
599}
600
601static SPP_RetVal_t SX1262_SetPaConfig(void *p_handle)
602{
603 spp_uint8_t buffer[5];
604 buffer[0] = SX1262_OPCODE_SET_PA_CONFIG;
605 buffer[1] = 0x04; // paDutyCycle to +22 dBm in SX1262
606 buffer[2] = 0x07; // hpMax to +14 dBm in SX1262
607 buffer[3] = 0x00; // deviceSel --> SX1262
608 buffer[4] = 0x01; // paLut
609
611 if (ret != K_SPP_OK)
612 return ret;
613 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 5);
614}
615
617{
618 spp_uint8_t buffer[3];
620 buffer[1] = 0xD7; // Calibration to 868 MHz (SX1262 is calibrated to 918Mhz by default)
621 buffer[2] = 0xDB;
622
624 if (ret != K_SPP_OK)
625 return ret;
626
627 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 3);
628}
629
630static SPP_RetVal_t SX1262_SetTxParams(void *p_handle)
631{
632 spp_uint8_t buffer[3];
633 buffer[0] = SX1262_OPCODE_SET_TX_PARAMS;
634 buffer[1] = 0x16; // power: +22 dBm (due to restrictions in SetPaConfig --> it will go down to +14 dBm)
635 buffer[2] = 0x04; // RampTime: 200 us (intermediate value)
636
638 if (ret != K_SPP_OK)
639 return ret;
640 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 3);
641}
642
643/* -----------------------------------------
644 MEMORY MANAGEMENT
645--------------------------------------------*/
647{
648 spp_uint8_t buffer[3];
650 buffer[1] = 0x00; // txBaseAddress
651 buffer[2] = 0x00; // rxBaseAddress
652
654 if (ret != K_SPP_OK)
655 return ret;
656 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 3);
657}
658
659/**
660 * @brief Writes a payload into the SX1262 internal TX buffer.
661 *
662 * Uses buffer offset 0 and prepends the SPI opcode and offset.
663 *
664 * @param p_handle SPI device handle.
665 * @param p_data Payload to transmit.
666 * @param length Payload size in bytes.
667 * @return Result of the SPI transaction.
668 */
669static SPP_RetVal_t SX1262_WriteBuffer(void *p_handle, spp_uint8_t *p_data, spp_uint8_t length)
670{
671 /* The maximum length of the buffer is 255 bytes
672 Adding 1 byte of Opcode and 1 byte of Offset, the array maximum length is 257 bytes */
673 spp_uint8_t buffer[257]; // TO-DO: check if this could be K_SPP_TOTAL_PACKET_SIZE + 2 instead 257
674
675 buffer[0] = SX1262_OPCODE_WRITE_BUFFER;
676 buffer[1] = 0x00; // the address where we start to write
677
678 for (spp_uint8_t i = 0; i < length; i++)
679 {
680 buffer[2 + i] = p_data[i];
681 }
682
683 // The total length of the SPI transaction is the length + 2 bytes of header
684 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, length + 2);
685}
686
687/* -----------------------------------------
688 LoRa Parameters
689--------------------------------------------*/
691 spp_uint8_t ldro)
692{
693 spp_uint8_t buffer[5];
695 buffer[1] = sf;
696 buffer[2] = bw;
697 buffer[3] = cr;
698 buffer[4] = ldro;
699
701 if (ret != K_SPP_OK)
702 return ret;
703 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 5);
704}
705
706static SPP_RetVal_t SX1262_SetPacketParams(void *p_handle, spp_uint16_t preambleLength, spp_uint8_t payloadLength)
707{
708 // LoRa SetPacketParams: opcode + 6 parameters
709 spp_uint8_t buffer[7] = {0};
710
712 buffer[1] = (spp_uint8_t)(preambleLength >> 8); // MSB
713 buffer[2] = (spp_uint8_t)(preambleLength & 0xFF); // LSB
714 buffer[3] = 0x00; // Variable packet length (explicit header)
715 buffer[4] = payloadLength; // Exact length of the packet in bytes
716 buffer[5] = 0x01; // CRC ON
717 buffer[6] = 0x00; // Standard IQ
718
719 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, sizeof(buffer));
720}
721
722/* -----------------------------------------
723 Interruption Management
724--------------------------------------------*/
725static SPP_RetVal_t SX1262_SetDioIrqParams(void *p_handle, spp_uint16_t irqMask, spp_uint16_t dio1Mask,
726 spp_uint16_t dio2Mask, spp_uint16_t dio3Mask)
727{
728 spp_uint8_t buffer[9];
730 buffer[1] = (spp_uint8_t)(irqMask >> 8);
731 buffer[2] = (spp_uint8_t)(irqMask & 0xFF);
732 buffer[3] = (spp_uint8_t)(dio1Mask >> 8);
733 buffer[4] = (spp_uint8_t)(dio1Mask & 0xFF);
734 buffer[5] = (spp_uint8_t)(dio2Mask >> 8);
735 buffer[6] = (spp_uint8_t)(dio2Mask & 0xFF);
736 buffer[7] = (spp_uint8_t)(dio3Mask >> 8);
737 buffer[8] = (spp_uint8_t)(dio3Mask & 0xFF);
738
740 if (ret != K_SPP_OK)
741 return ret;
742 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 9);
743}
744
745static SPP_RetVal_t SX1262_ClearIrqStatus(void *p_handle, spp_uint16_t clearMask)
746{
747 spp_uint8_t buffer[3];
749 buffer[1] = (spp_uint8_t)(clearMask >> 8);
750 buffer[2] = (spp_uint8_t)(clearMask & 0xFF);
751
753 if (ret != K_SPP_OK)
754 return ret;
755
756 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, 3);
757}
758
759/**
760 * @brief Reads the radio IRQ status register.
761 *
762 * Used to distinguish successful transmission completion
763 * from timeout or unexpected interrupts.
764 *
765 * @param p_handle SPI device handle.
766 * @param p_irqStatus Output pointer for the 16-bit IRQ flags.
767 * @return K_SPP_OK on success, error code otherwise.
768 */
769static SPP_RetVal_t SX1262_GetIrqStatus(void *p_handle, spp_uint16_t *p_irqStatus)
770{
771 if (p_irqStatus == NULL)
772 {
774 }
775
776 spp_uint8_t buffer[4] = {SX1262_OPCODE_GET_IRQ_STATUS, 0x00, 0x00, 0x00};
777
779 if (ret != K_SPP_OK)
780 {
781 return ret;
782 }
783
784 ret = SPP_HAL_SPI_transmitRaw(p_handle, buffer, sizeof(buffer));
785 if (ret != K_SPP_OK)
786 {
787 return ret;
788 }
789
790 *p_irqStatus = ((spp_uint16_t)buffer[2] << 8) | (spp_uint16_t)buffer[3];
791
792 return K_SPP_OK;
793}
794
795/**
796 * @brief Configures DIO1 as a rising-edge interrupt input.
797 *
798 * Registers the ISR context used to set g_txDoneFlag.
799 *
800 * @return K_SPP_OK on success, error code otherwise.
801 */
803{
805
806 // This instruction configures the pin as an input with interruption by rising edge
808 if (ret != K_SPP_OK)
809 {
810 return ret;
811 }
812
813 // This instruction registers the context to the ISR can modify g_txDoneFlag
815 if (ret != K_SPP_OK)
816 {
817 return ret;
818 }
819
820 return K_SPP_OK;
821}
822
823static SPP_RetVal_t SX1262_WriteRegister(void *p_handle, spp_uint16_t address, spp_uint8_t *p_data, spp_uint8_t length)
824{
825 // Maximum length: opcode + address + data
826 spp_uint8_t buffer[16];
827
828 buffer[0] = 0x0D; // Opcode WriteRegister
829 buffer[1] = (spp_uint8_t)(address >> 8); // MSB
830 buffer[2] = (spp_uint8_t)(address & 0xFF); // LSB
831
832 for (spp_uint8_t i = 0; i < length; i++)
833 {
834 buffer[3 + i] = p_data[i];
835 }
836
838 if (ret != K_SPP_OK)
839 return ret;
840
841 return SPP_HAL_SPI_transmitRaw(p_handle, buffer, length + 3);
842}
843
844/**
845 * @brief Waits until the SX1262 BUSY pin becomes low.
846 *
847 * Polls the GPIO until the radio is ready or the timeout
848 * is reached. This function blocks during the wait.
849 *
850 * @return K_SPP_OK when ready, K_SPP_ERROR on timeout.
851 */
853{
855
857 {
858 // Check if the maximum time of waiting was reached
860 {
861 return K_SPP_ERROR;
862 }
863 }
864
865 return K_SPP_OK;
866}
867
868/**
869 * @brief Resets the software transmission state after a timeout.
870 *
871 * Returns the TX FSM to IDLE, discards all queued packets
872 * and clears the TX completion flag.
873 *
874 * Does not perform a hardware reset of the radio.
875 *
876 * @return K_SPP_ERROR.
877 */
879{
880 // When an error happens --> reset of the transmision buffer
882 mailboxHead = 0;
883 mailboxTail = 0;
884 mailboxCount = 0;
885 g_txDoneFlag = false;
886 return K_SPP_ERROR;
887}
SPP_RetVal_t SPP_HAL_GPIO_write(spp_uint32_t pin, spp_uint8_t level)
Configures a GPIO as output and sets its logic level.
Definition gpio.c:64
SPP_RetVal_t SPP_HAL_GPIO_registerIsr(spp_uint32_t pin, void *p_isrCtx)
Register an ISR handler for a GPIO pin.
Definition gpio.c:37
SPP_RetVal_t SPP_HAL_GPIO_configInterrupt(spp_uint32_t pin, spp_uint32_t intrType, spp_uint32_t pull)
Configure a GPIO pin as an interrupt input.
Definition gpio.c:18
spp_uint16_t SPP_HAL_GPIO_read(spp_uint32_t pin)
Reads the logic level of a GPIO pin.
Definition gpio.c:57
SPP GPIO HAL API — dispatches through the registered HAL port.
SPP_Packet_t
Definition packet.h:79
#define K_SPP_TOTAL_PACKET_SIZE
Definition packet.h:29
SPP_RetVal_t
Unified return type for all SPP API functions.
Definition returnTypes.h:24
@ K_SPP_OK
Definition returnTypes.h:25
@ K_SPP_ERROR
Definition returnTypes.h:26
@ K_SPP_ERROR_NULL_POINTER
Definition returnTypes.h:30
SPP_RetVal_t SPP_HAL_SPI_transmitRaw(void *p_handle, spp_uint8_t *p_data, spp_uint8_t length)
Performs a raw full-duplex SPI transaction.
Definition spi.c:99
void * SPP_HAL_SPI_getHandle(spp_uint8_t deviceIdx)
Obtain the SPI device handle for the given device index.
Definition spi.c:39
SPP_RetVal_t SPP_HAL_SPI_deviceInit(void *p_handle)
Initialise a specific SPI device.
Definition spi.c:49
SPP SPI HAL API — dispatches through the registered HAL port.
Context passed to the GPIO ISR handler.
Definition gpio.h:27
Describes the contract of a consumer module. This module needs to have:
Definition service.h:40
spp_uint32_t timer_ms
Definition sx1262.h:71
spp_uint32_t frame_number
Definition sx1262.h:69
SX1262_TxState_t tx_state
Definition sx1262.h:70
void * p_spi
Definition sx1262.h:68
static SPP_RetVal_t SX1262_SetModulationParams(void *, spp_uint8_t, spp_uint8_t, spp_uint8_t, spp_uint8_t)
Definition sx1262.c:690
static SPP_RetVal_t SX1262_WriteRegister(void *, spp_uint16_t, spp_uint8_t *, spp_uint8_t)
Definition sx1262.c:823
static SPP_Packet_t mailboxData[K_SX1262_MAILBOX_SIZE]
Definition sx1262.c:27
static SPP_RetVal_t SX1262_ClearDeviceErrors(void *)
Definition sx1262.c:502
static SPP_RetVal_t SX1262_GetDeviceErrors(void *, spp_uint16_t *)
Definition sx1262.c:544
static SX1262_Ctx_t s_sx1262Ctx
Definition sx1262.c:17
static SPP_RetVal_t SX1262_Timeout_Sequence(void)
Resets the software transmission state after a timeout.
Definition sx1262.c:878
static SPP_RetVal_t SX1262_GetIrqStatus(void *, spp_uint16_t *)
Reads the radio IRQ status register.
Definition sx1262.c:769
static SPP_RetVal_t SX1262_InitInterrupts(void)
Configures DIO1 as a rising-edge interrupt input.
Definition sx1262.c:802
static SPP_RetVal_t SX1262_CalibrateImage(void *)
Definition sx1262.c:616
static SPP_RetVal_t SX1262_SetStandby(void *)
Definition sx1262.c:453
static SPP_RetVal_t SX1262_SetBufferBaseAddress(void *)
Definition sx1262.c:646
static spp_uint8_t mailboxTail
Definition sx1262.c:29
static SPP_RetVal_t SX1262_ClearIrqStatus(void *, spp_uint16_t)
Definition sx1262.c:745
SPP_GpioIsrCtx_t g_dio1Ctx
Definition sx1262.c:34
static SPP_RetVal_t SX1262_SetTx(void *, spp_uint32_t)
Definition sx1262.c:531
static SPP_RetVal_t SX1262_SetPacketType(void *)
Definition sx1262.c:573
static SPP_RetVal_t SX1262_SetDioIrqParams(void *, spp_uint16_t, spp_uint16_t, spp_uint16_t, spp_uint16_t)
Definition sx1262.c:725
static SPP_RetVal_t SX1262_Calibrate(void *)
Definition sx1262.c:515
static SPP_RetVal_t SPP_SERVICES_SX1262_consumeData(void *)
Advances the radio transmission state machine.
Definition sx1262.c:282
static SPP_RetVal_t SX1262_SetRfFrequency(void *)
Definition sx1262.c:586
static SPP_RetVal_t SX1262_WaitBusy(void)
Waits until the SX1262 BUSY pin becomes low.
Definition sx1262.c:852
static SPP_RetVal_t SX1262_SetDio3AsTcxoCtrl(void *)
Definition sx1262.c:481
volatile spp_bool_t g_txDoneFlag
Definition sx1262.c:33
static SPP_SERVICE_ConsumerContract_t sx1262Contract
Definition sx1262.c:68
static SPP_RetVal_t SX1262_SetPaConfig(void *)
Definition sx1262.c:601
static spp_uint8_t mailboxHead
Definition sx1262.c:28
static SPP_RetVal_t SX1262_SetPacketParams(void *, spp_uint16_t, spp_uint8_t)
Definition sx1262.c:706
static SPP_RetVal_t SPP_SERVICES_SX1262_init(void)
Initializes the SX1262 radio for LoRa transmission.
Definition sx1262.c:99
static SPP_RetVal_t SX1262_WriteBuffer(void *, spp_uint8_t *, spp_uint8_t)
Writes a payload into the SX1262 internal TX buffer.
Definition sx1262.c:669
static SPP_RetVal_t SX1262_SetTxParams(void *)
Definition sx1262.c:630
static spp_uint8_t mailboxCount
Definition sx1262.c:30
static SPP_RetVal_t SX1262_SetRegulatorMode(void *)
Definition sx1262.c:465
const SPP_SERVICE_ConsumerContract_t * SPP_SERVICES_SX1262_getConsumerContract(void)
Returns the SX1262 consumer contract for PUBSUB registration.
Definition sx1262.c:80
static SPP_RetVal_t SPP_SERVICES_SX1262_deliverToMailbox(const SPP_Packet_t)
Copies a received SPP packet into the transmission mailbox.
Definition sx1262.c:248
SX1262 LoRa telemetry consumer interface and configuration.
#define SX1262_IRQ_TIMEOUT
Definition sx1262.h:45
#define SX1262_OPCODE_SET_BUFFER_BASE_ADDRESS
Definition sx1262.h:25
#define SX126X_NRESET_PIN
Definition sx1262.h:39
#define SX1262_OPCODE_GET_IRQ_STATUS
Definition sx1262.h:43
#define SX1262_OPCODE_SET_REGULATOR_MODE
Definition sx1262.h:47
#define SX1262_MAX_DONE_TIMEOUT_MS
Definition sx1262.h:35
#define SX1262_OPCODE_SET_PA_CONFIG
Definition sx1262.h:23
#define SX1262_MAX_BUSY_TIMEOUT_MS
Definition sx1262.h:34
#define SX1262_OPCODE_SET_RF_FRECUENCY
Definition sx1262.h:22
#define SX1262_OPCODE_SET_STANDBY
Definition sx1262.h:19
#define K_SX1262_MAILBOX_SIZE
Definition sx1262.h:16
#define K_SX1262_SPI
Definition sx1262.h:40
#define SX1262_BUSY_PIN
Definition sx1262.h:37
#define SX1262_OPCODE_CALIBRATE_IMAGE
Definition sx1262.h:31
@ SX1262_TX_STATE_WAIT_TX_DONE
Definition sx1262.h:63
@ SX1262_TX_STATE_WRITE_BUFFER
Definition sx1262.h:60
@ SX1262_TX_STATE_IDLE
Definition sx1262.h:59
@ SX1262_TX_STATE_SET_PARAMS
Definition sx1262.h:61
@ SX1262_TX_STATE_SET_TX
Definition sx1262.h:62
#define SX1262_OPCODE_WRITE_BUFFER
Definition sx1262.h:26
#define SX1262_CALIBRATE_ALL
Definition sx1262.h:53
#define SX1262_TCXO_STARTUP_DELAY
Definition sx1262.h:51
#define SX1262_OPCODE_SET_TX_PARAMS
Definition sx1262.h:24
#define SX1262_OPCODE_SET_MODULATION_PARAMS
Definition sx1262.h:27
#define K_SX1262_TASK_TIMEOUT_MS
Definition sx1262.h:17
#define SX1262_OPCODE_CLEAR_IRQ_STATUS
Definition sx1262.h:30
#define SX1262_OPCODE_CALIBRATE
Definition sx1262.h:52
#define SX1262_TCXO_VOLTAGE_1V7
Definition sx1262.h:50
#define K_SX1262_MAX_PACKETS_TO_TRANSMIT
Definition sx1262.h:33
#define SX1262_IRQ_TX_DONE
Definition sx1262.h:44
#define SX1262_OPCODE_SET_TX
Definition sx1262.h:20
#define SX1262_OPCODE_SET_PACKET_TYPE
Definition sx1262.h:21
#define SX1262_OPCODE_SET_PACKET_PARAMS
Definition sx1262.h:28
#define SX1262_OPCODE_GET_DEVICE_ERRORS
Definition sx1262.h:55
#define SX1262_DIO1_PIN
Definition sx1262.h:38
#define SX1262_OPCODE_SET_DIO3_AS_TCXO_CTRL
Definition sx1262.h:49
#define SX1262_OPCODE_CLEAR_DEVICE_ERRORS
Definition sx1262.h:54
#define SX1262_OPCODE_SET_DIO_IRQ_PARAMS
Definition sx1262.h:29
#define SX1262_REGULATOR_DC_DC
Definition sx1262.h:48
spp_uint32_t SPP_HAL_TIME_getTimeMs(void)
Return the current hardware time in milliseconds.
Definition time.c:15
void SPP_HAL_TIME_delayMs(spp_uint32_t ms)
Block for the requested number of milliseconds.
Definition time.c:35
SPP HAL time API — dispatches through the registered HAL port.
uint16_t spp_uint16_t
Definition types.h:29
uint32_t spp_uint32_t
Definition types.h:31
uint8_t spp_uint8_t
Definition types.h:27
bool spp_bool_t
Definition types.h:36