1717// ANSI
1818#include <string.h>
1919
20+ #define ESP_INTR_FLAG_DEFAULT 0
21+
2022#define HZ_PER_KHZ 1000
2123#define KHZ_PER_MHZ 1000
2224#define HZ_PER_MHZ (HZ_PER_KHZ * KHZ_PER_MHZ)
@@ -347,22 +349,78 @@ void RtcStopAlarm( void )
347349
348350static RadioOperatingModes_t OperatingMode ;
349351
352+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
353+ void SX126xWaitOnBusy ( void )
354+ {
355+ while (gpio_get_level (lora_spi .busy ) == 1 );
356+ }
357+ #endif
358+
350359/*!
351360 * \brief HW Reset of the radio
352361 */
353362void SX126xReset ( void )
354363{
364+ gpio_config_t cfg_reset_output = {
365+ (1ULL <<lora_spi .reset ),
366+ GPIO_MODE_OUTPUT ,
367+ GPIO_PULLUP_ENABLE ,
368+ GPIO_PULLDOWN_DISABLE ,
369+ GPIO_INTR_DISABLE
370+ };
371+ ESP_ERROR_CHECK (gpio_config (& cfg_reset_output ));
372+
373+ ESP_ERROR_CHECK (gpio_set_level (lora_spi .reset , 1 ));
374+
375+ // Hold low for at least 100us.
376+ // SX1261/2 Datasheet, Rev 1.1 Section 8.1 Reset
377+ DelayMs (1 );
378+
379+ gpio_config_t cfg_reset_input = {
380+ (1ULL <<lora_spi .reset ),
381+ GPIO_MODE_DISABLE ,
382+ GPIO_PULLUP_ENABLE ,
383+ GPIO_PULLDOWN_DISABLE ,
384+ GPIO_INTR_DISABLE
385+ };
386+ ESP_ERROR_CHECK (gpio_config (& cfg_reset_input ));
387+
388+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
389+ // Wait for chip to be ready.
390+ SX126xWaitOnBusy ();
391+ #endif
355392
393+ SX126xSetOperatingMode (MODE_STDBY_RC );
356394}
357395
358396/*!
359397 * \brief Wakes up the radio
360398 */
361399void SX126xWakeup ( void )
362400{
401+ CRITICAL_SECTION_BEGIN ( );
363402
403+ ESP_ERROR_CHECK (gpio_set_level (lora_spi .cs , 0 ));
404+ ESP_ERROR_CHECK (gpio_set_level (lora_spi .cs , 1 ));
405+
406+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
407+ // Wait for chip to be ready.
408+ SX126xWaitOnBusy ();
409+ #endif
410+
411+ // Update operating mode context variable
412+ SX126xSetOperatingMode (MODE_STDBY_RC );
413+
414+ CRITICAL_SECTION_END ( );
364415}
365416
417+ /*!
418+ * \brief Initializes the RF Switch I/Os pins interface
419+ */
420+ void SX126xAntSwOn ( void )
421+ {
422+ // No antenna switch available on this board design.
423+ }
366424
367425/*!
368426 * \brief De-initializes the RF Switch I/Os pins interface
@@ -371,7 +429,7 @@ void SX126xWakeup( void )
371429 */
372430void SX126xAntSwOff ( void )
373431{
374-
432+ // No antenna switch available on this board design.
375433}
376434
377435/*!
@@ -424,8 +482,17 @@ void SX126xSetOperatingMode( RadioOperatingModes_t mode )
424482 */
425483void SX126xIoIrqInit ( DioIrqHandler dioIrq )
426484{
427- //GpioSetInterrupt( &SX126x.DIO1, IRQ_RISING_EDGE, IRQ_HIGH_PRIORITY, dioIrq );
428-
485+ gpio_config_t io_conf = {
486+ (1ULL <<lora_spi .irq_dio1 ),
487+ GPIO_MODE_INPUT ,
488+ GPIO_PULLUP_DISABLE ,
489+ GPIO_PULLDOWN_DISABLE ,
490+ GPIO_INTR_POSEDGE
491+ };
492+ ESP_ERROR_CHECK (gpio_config (& io_conf ));
493+ ESP_ERROR_CHECK (gpio_set_intr_type (lora_spi .irq_dio1 , GPIO_INTR_POSEDGE ));
494+ ESP_ERROR_CHECK (gpio_install_isr_service (ESP_INTR_FLAG_DEFAULT ));
495+ ESP_ERROR_CHECK (gpio_isr_handler_add (lora_spi .irq_dio1 , dioIrq , (void * ) lora_spi .irq_dio1 ));
429496}
430497
431498/*!
@@ -435,23 +502,23 @@ void SX126xIoIrqInit( DioIrqHandler dioIrq )
435502 */
436503uint32_t SX126xGetDio1PinState ( void )
437504{
438- return 0 ;
505+ return gpio_get_level ( lora_spi . irq_dio1 ) ;
439506}
440507
441508/*!
442509 * \brief Initializes RF switch control pins.
443510 */
444511void SX126xIoRfSwitchInit ( void )
445512{
446-
513+ SX126xSetDio2AsRfSwitchCtrl (true);
447514}
448515
449516/*!
450517 * \brief Initializes the TCXO power pin.
451518 */
452519void SX126xIoTcxoInit ( void )
453520{
454-
521+ // No TCXO component available on this board design.
455522}
456523
457524/*!
@@ -461,7 +528,7 @@ void SX126xIoTcxoInit( void )
461528 */
462529void SX126xSetRfTxPower ( int8_t power )
463530{
464-
531+ SX126xSetTxParams ( power , RADIO_RAMP_40_US );
465532}
466533
467534// SPI stuff
@@ -474,7 +541,7 @@ void SX126xSetRfTxPower( int8_t power )
474541 */
475542void SX126xWriteRegister ( uint16_t address , uint8_t value )
476543{
477-
544+ SX126xWriteRegisters ( address , & value , 1 );
478545}
479546
480547/*!
@@ -486,15 +553,47 @@ void SX126xWriteRegister( uint16_t address, uint8_t value )
486553 */
487554uint8_t SX126xReadRegister ( uint16_t address )
488555{
489- return 0 ;
556+ uint8_t data ;
557+ SX126xReadRegisters (address , & data , 1 );
558+ return data ;
490559}
491560
492561void SX126xReadRegisters ( uint16_t address , uint8_t * buffer , uint16_t size )
493562{
494-
563+ spi_transaction_t trans = {};
564+ trans .cmd = RADIO_READ_REGISTER ;
565+ trans .addr = address ;
566+ trans .rx_buffer = buffer ;
567+ trans .length = size * 8 ; // size is in bits, not bytes
568+ // https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/spi_master.html#_CPPv4N17spi_transaction_t6lengthE
569+
570+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
571+ SX126xCheckDeviceReady ();
572+ #endif
573+
574+ ESP_ERROR_CHECK (spi_device_transmit (lora_spi .handle , & trans ));
575+
576+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
577+ SX126xWaitOnBusy ();
578+ #endif
495579}
496580void SX126xWriteRegisters ( uint16_t address , uint8_t * buffer , uint16_t size )
497581{
582+ spi_transaction_t trans = {};
583+ trans .cmd = RADIO_WRITE_REGISTER ;
584+ trans .addr = address ;
585+ trans .tx_buffer = buffer ;
586+ trans .length = size * 8 ; // size is in bits, not bytes
587+
588+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
589+ SX126xCheckDeviceReady ();
590+ #endif
591+
592+ ESP_ERROR_CHECK (spi_device_transmit (lora_spi .handle , & trans ));
593+
594+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
595+ SX126xWaitOnBusy ();
596+ #endif
498597}
499598
500599/*!
@@ -506,7 +605,20 @@ void SX126xWriteRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
506605 */
507606void SX126xWriteCommand ( RadioCommands_t opcode , uint8_t * buffer , uint16_t size )
508607{
608+ spi_transaction_t trans = {};
609+ trans .cmd = opcode ;
610+ trans .tx_buffer = buffer ;
611+ trans .length = size * 8 ; // size is in bits, not bytes
612+
613+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
614+ SX126xCheckDeviceReady ();
615+ #endif
509616
617+ ESP_ERROR_CHECK (spi_device_transmit (lora_spi .handle , & trans ));
618+
619+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
620+ SX126xWaitOnBusy ();
621+ #endif
510622}
511623
512624/*!
@@ -520,14 +632,59 @@ void SX126xWriteCommand( RadioCommands_t opcode, uint8_t *buffer, uint16_t size
520632 */
521633uint8_t SX126xReadCommand ( RadioCommands_t opcode , uint8_t * buffer , uint16_t size )
522634{
635+ spi_transaction_t trans = {};
636+ trans .cmd = opcode ;
637+ trans .rx_buffer = buffer ;
638+ trans .length = size * 8 ; // size is in bits, not bytes
639+
640+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
641+ SX126xCheckDeviceReady ();
642+ #endif
643+
644+ ESP_ERROR_CHECK (spi_device_transmit (lora_spi .handle , & trans ));
645+
646+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
647+ SX126xWaitOnBusy ();
648+ #endif
649+
523650 return 0 ;
524651}
525652
526653void SX126xReadBuffer ( uint8_t offset , uint8_t * buffer , uint8_t size )
527654{
655+ spi_transaction_t trans = {};
656+ trans .cmd = RADIO_READ_BUFFER ;
657+ trans .addr = offset ;
658+ trans .rx_buffer = buffer ;
659+ trans .length = size * 8 ; // size is in bits, not bytes
660+
661+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
662+ SX126xCheckDeviceReady ();
663+ #endif
664+
665+ ESP_ERROR_CHECK (spi_device_transmit (lora_spi .handle , & trans ));
666+
667+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
668+ SX126xWaitOnBusy ();
669+ #endif
528670}
529671
530672void SX126xWriteBuffer ( uint8_t offset , uint8_t * buffer , uint8_t size )
531673{
674+ spi_transaction_t trans = {};
675+ trans .cmd = RADIO_WRITE_BUFFER ;
676+ trans .addr = offset ;
677+ trans .tx_buffer = buffer ;
678+ trans .length = size * 8 ; // size is in bits, not bytes
679+
680+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
681+ SX126xCheckDeviceReady ();
682+ #endif
683+
684+ ESP_ERROR_CHECK (spi_device_transmit (lora_spi .handle , & trans ));
685+
686+ #if defined(SX1261MBXBAS ) || defined(SX1262MBXCAS ) || defined(SX1262MBXDAS )
687+ SX126xWaitOnBusy ();
688+ #endif
532689}
533690
0 commit comments