SX1276Lib updated in order to be RTOS aware

Fork of SX1276Lib by Semtech

Files at this revision

API Documentation at this revision

Comitter:
Lorenzo Maiorfi
Date:
Fri Mar 02 15:39:25 2018 +0100
Parent:
26:d09a8ef807e2
Child:
28:bff3fce265f8
Commit message:
RTOS support

Changed in this revision

sx1276/sx1276-hal.cpp Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276.cpp Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276.h Show annotated file Show diff for this revision Revisions of this file
--- a/sx1276/sx1276-hal.cpp	Mon Apr 24 09:26:23 2017 +0000
+++ b/sx1276/sx1276-hal.cpp	Fri Mar 02 15:39:25 2018 +0100
@@ -48,7 +48,7 @@
 }
 
 SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events )
-                        #if defined ( TARGET_NUCLEO_L152RE )
+                        #if defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_L476RG) || defined (TARGET_NUCLEO_F401RE) || defined (TARGET_NUCLEO_L073RZ)
                         :   SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, A3, D9 ), // For NUCLEO L152RE dio4 is on port A3
                             AntSwitch( A4 ),
                             Fake( D8 )
--- a/sx1276/sx1276.cpp	Mon Apr 24 09:26:23 2017 +0000
+++ b/sx1276/sx1276.cpp	Fri Mar 02 15:39:25 2018 +0100
@@ -58,12 +58,12 @@
 
     this->dioIrq = new DioIrqHandler[6];
 
-    this->dioIrq[0] = &SX1276::OnDio0Irq;
-    this->dioIrq[1] = &SX1276::OnDio1Irq;
-    this->dioIrq[2] = &SX1276::OnDio2Irq;
-    this->dioIrq[3] = &SX1276::OnDio3Irq;
-    this->dioIrq[4] = &SX1276::OnDio4Irq;
-    this->dioIrq[5] = NULL;
+    this->dioIrq[0] = &SX1276::enqueueOnDio0Irq; /*&SX1276::OnDio0Irq;*/
+    this->dioIrq[1] = &SX1276::enqueueOnDio1Irq; /*&SX1276::OnDio1Irq;*/
+    this->dioIrq[2] = &SX1276::enqueueOnDio2Irq; /*&SX1276::OnDio2Irq;*/
+    this->dioIrq[3] = &SX1276::enqueueOnDio3Irq; /*&SX1276::OnDio3Irq;*/
+    this->dioIrq[4] = &SX1276::enqueueOnDio4Irq; /*&SX1276::OnDio4Irq;*/
+    this->dioIrq[5] = &SX1276::enqueueOnDio5Irq; /*NULL;*/
 
     this->settings.State = RF_IDLE;
 }
@@ -77,6 +77,10 @@
 void SX1276::Init( RadioEvents_t *events )
 {
     this->RadioEvents = events;
+
+    // <RTOS>
+    //_thread_events_queue->start(callback(_eq_events, &EventQueue::dispatch_forever));
+    // </RTOS>
 }
 
 RadioState SX1276::GetStatus( void )
@@ -825,7 +829,7 @@
     this->settings.State = RF_RX_RUNNING;
     if( timeout != 0 )
     {
-        rxTimeoutTimer.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ), timeout * 1e3 );
+        rxTimeoutTimer.attach_us(mbed::callback( this, &SX1276::enqueueOnTimeoutIrq /*&SX1276::OnTimeoutIrq*/ ), timeout * 1e3 );
     }
 
     if( this->settings.Modem == MODEM_FSK )
@@ -834,7 +838,7 @@
 
         if( rxContinuous == false )
         {
-            rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ),
+            rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::enqueueOnTimeoutIrq /*&SX1276::OnTimeoutIrq*/ ),
                                          this->settings.Fsk.RxSingleTimeout * 1e3 );
         }
     }
@@ -909,7 +913,7 @@
     }
 
     this->settings.State = RF_TX_RUNNING;
-    txTimeoutTimer.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ), timeout * 1e3 );
+    txTimeoutTimer.attach_us( mbed::callback( this, &SX1276::enqueueOnTimeoutIrq /*&SX1276::OnTimeoutIrq*/ ), timeout * 1e3 );
     SetOpMode( RF_OPMODE_TRANSMITTER );
 }
 
@@ -960,7 +964,7 @@
     Write( REG_DIOMAPPING2, RF_DIOMAPPING2_DIO4_10 | RF_DIOMAPPING2_DIO5_10 );
 
     this->settings.State = RF_TX_RUNNING;
-    txTimeoutTimer.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ), timeout );
+    txTimeoutTimer.attach_us( mbed::callback( this, &SX1276::enqueueOnTimeoutIrq /*&SX1276::OnTimeoutIrq*/ ), timeout );
     SetOpMode( RF_OPMODE_TRANSMITTER );
 }
 
@@ -1097,7 +1101,7 @@
             {
                 // Continuous mode restart Rx chain
                 Write( REG_RXCONFIG, Read( REG_RXCONFIG ) | RF_RXCONFIG_RESTARTRXWITHOUTPLLLOCK );
-                rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ),
+                rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::enqueueOnTimeoutIrq /*&SX1276::OnTimeoutIrq*/ ),
                                              this->settings.Fsk.RxSingleTimeout * 1e3 );
             }
             else
@@ -1108,7 +1112,8 @@
         }
         if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxTimeout != NULL ) )
         {
-            this->RadioEvents->RxTimeout( );
+            /*this->RadioEvents->RxTimeout( );*/
+            enqueueRadioEvent_RxTimeout();
         }
         break;
     case RF_TX_RUNNING:
@@ -1140,7 +1145,8 @@
         this->settings.State = RF_IDLE;
         if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->TxTimeout != NULL ) )
         {
-            this->RadioEvents->TxTimeout( );
+            /*this->RadioEvents->TxTimeout( );*/
+            enqueueRadioEvent_TxTimeout();
         }
         break;
     default:
@@ -1182,13 +1188,14 @@
                         {
                             // Continuous mode restart Rx chain
                             Write( REG_RXCONFIG, Read( REG_RXCONFIG ) | RF_RXCONFIG_RESTARTRXWITHOUTPLLLOCK );
-                            rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ),
+                            rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::enqueueOnTimeoutIrq /*&SX1276::OnTimeoutIrq*/ ),
                                                          this->settings.Fsk.RxSingleTimeout * 1e3 );
                         }
 
                         if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxError != NULL ) )
                         {
-                            this->RadioEvents->RxError( );
+                            /*this->RadioEvents->RxError( );*/
+                            enqueueRadioEvent_RxError();
                         }
                         this->settings.FskPacketHandler.PreambleDetected = false;
                         this->settings.FskPacketHandler.SyncWordDetected = false;
@@ -1229,13 +1236,14 @@
                 {
                     // Continuous mode restart Rx chain
                     Write( REG_RXCONFIG, Read( REG_RXCONFIG ) | RF_RXCONFIG_RESTARTRXWITHOUTPLLLOCK );
-                    rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ),
+                    rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::enqueueOnTimeoutIrq /*&SX1276::OnTimeoutIrq*/ ),
                                                  this->settings.Fsk.RxSingleTimeout * 1e3 );
                 }
 
                 if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxDone != NULL ) )
                 {
-                    this->RadioEvents->RxDone( rxtxBuffer, this->settings.FskPacketHandler.Size, this->settings.FskPacketHandler.RssiValue, 0 );
+                    /*this->RadioEvents->RxDone( rxtxBuffer, this->settings.FskPacketHandler.Size, this->settings.FskPacketHandler.RssiValue, 0 );*/
+                    enqueueRadioEvent_RxDone(rxtxBuffer, this->settings.FskPacketHandler.Size, this->settings.FskPacketHandler.RssiValue, 0);
                 }
                 this->settings.FskPacketHandler.PreambleDetected = false;
                 this->settings.FskPacketHandler.SyncWordDetected = false;
@@ -1263,7 +1271,8 @@
 
                         if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxError != NULL ) )
                         {
-                            this->RadioEvents->RxError( );
+                            /*this->RadioEvents->RxError( );*/
+                            enqueueRadioEvent_RxError();
                         }
                         break;
                     }
@@ -1318,7 +1327,8 @@
 
                     if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxDone != NULL ) )
                     {
-                        this->RadioEvents->RxDone( rxtxBuffer, this->settings.LoRaPacketHandler.Size, this->settings.LoRaPacketHandler.RssiValue, this->settings.LoRaPacketHandler.SnrValue );
+                        /*this->RadioEvents->RxDone( rxtxBuffer, this->settings.LoRaPacketHandler.Size, this->settings.LoRaPacketHandler.RssiValue, this->settings.LoRaPacketHandler.SnrValue );*/
+                        enqueueRadioEvent_RxDone(rxtxBuffer, this->settings.LoRaPacketHandler.Size, this->settings.LoRaPacketHandler.RssiValue, this->settings.LoRaPacketHandler.SnrValue);
                     }
                 }
                 break;
@@ -1340,7 +1350,8 @@
                 this->settings.State = RF_IDLE;
                 if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->TxDone != NULL ) )
                 {
-                    this->RadioEvents->TxDone( );
+                    /*this->RadioEvents->TxDone( );*/
+                    enqueueRadioEvent_TxDone();
                 }
                 break;
             }
@@ -1392,7 +1403,8 @@
                 this->settings.State = RF_IDLE;
                 if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxTimeout != NULL ) )
                 {
-                    this->RadioEvents->RxTimeout( );
+                    /*this->RadioEvents->RxTimeout( );*/
+                    enqueueRadioEvent_RxTimeout();
                 }
                 break;
             default:
@@ -1463,7 +1475,8 @@
 
                     if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->FhssChangeChannel != NULL ) )
                     {
-                        this->RadioEvents->FhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) );
+                        /*this->RadioEvents->FhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) );*/
+                        enqueueRadioEvent_FhssChangeChannel(( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) );
                     }
                 }
                 break;
@@ -1484,7 +1497,8 @@
 
                     if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->FhssChangeChannel != NULL ) )
                     {
-                        this->RadioEvents->FhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) );
+                        /*this->RadioEvents->FhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) );*/
+                        enqueueRadioEvent_FhssChangeChannel(( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ));
                     }
                 }
                 break;
@@ -1510,7 +1524,8 @@
             Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_CADDETECTED | RFLR_IRQFLAGS_CADDONE );
             if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->CadDone != NULL ) )
             {
-                this->RadioEvents->CadDone( true );
+                /*this->RadioEvents->CadDone( true );*/
+                enqueueRadioEvent_CadDone(true);
             }
         }
         else
@@ -1519,7 +1534,8 @@
             Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_CADDONE );
             if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->CadDone != NULL ) )
             {
-                this->RadioEvents->CadDone( false );
+                /*this->RadioEvents->CadDone( false );*/
+                enqueueRadioEvent_CadDone(false);
             }
         }
         break;
@@ -1559,3 +1575,112 @@
         break;
     }
 }
+
+// Inizio nuova parte per RTOS
+
+void SX1276::enqueueOnTimeoutIrq()
+{
+    _eq_events->call(this, &SX1276::OnTimeoutIrq);
+}
+
+void SX1276::radioEvent_RxTimeout()
+{
+    this->RadioEvents->RxTimeout( );
+}
+
+void SX1276::enqueueRadioEvent_RxTimeout()
+{
+     _eq_events->call(this, &SX1276::radioEvent_RxTimeout);
+}
+
+void SX1276::radioEvent_TxTimeout()
+{
+    this->RadioEvents->TxTimeout( );
+}
+
+void SX1276::enqueueRadioEvent_TxTimeout()
+{
+     _eq_events->call(this, &SX1276::radioEvent_TxTimeout);
+}
+
+void SX1276::radioEvent_RxError()
+{
+    this->RadioEvents->RxError( );
+}
+
+void SX1276::enqueueRadioEvent_RxError()
+{
+     _eq_events->call(this, &SX1276::radioEvent_RxError);
+}
+
+void SX1276::radioEvent_RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
+{
+    this->RadioEvents->RxDone(payload, size, rssi, snr);
+}
+
+void SX1276::enqueueRadioEvent_RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
+{
+     _eq_events->call(this, &SX1276::radioEvent_RxDone, payload, size, rssi, snr);
+}
+
+void SX1276::radioEvent_TxDone()
+{
+    this->RadioEvents->TxDone();
+}
+
+void SX1276::enqueueRadioEvent_TxDone()
+{
+     _eq_events->call(this, &SX1276::radioEvent_TxDone);
+}
+
+void SX1276::radioEvent_FhssChangeChannel(uint8_t currentChannel)
+{
+    this->RadioEvents->FhssChangeChannel(currentChannel);
+}
+
+void SX1276::enqueueRadioEvent_FhssChangeChannel(uint8_t currentChannel)
+{
+    _eq_events->call(this, &SX1276::radioEvent_FhssChangeChannel, currentChannel);
+}
+
+void SX1276::radioEvent_CadDone(bool channelActivityDetected)
+{
+    this->RadioEvents->CadDone(channelActivityDetected);
+}
+
+void SX1276::enqueueRadioEvent_CadDone(bool channelActivityDetected)
+{
+    _eq_events->call(this, &SX1276::radioEvent_CadDone, channelActivityDetected);
+}
+
+void SX1276::enqueueOnDio0Irq()
+{
+    _eq_events->call(this, &SX1276::OnDio0Irq);
+}
+
+void SX1276::enqueueOnDio1Irq()
+{
+    _eq_events->call(this, &SX1276::OnDio1Irq);
+}
+
+void SX1276::enqueueOnDio2Irq()
+{
+    _eq_events->call(this, &SX1276::OnDio2Irq);
+}
+
+void SX1276::enqueueOnDio3Irq()
+{
+    _eq_events->call(this, &SX1276::OnDio3Irq);
+}
+
+void SX1276::enqueueOnDio4Irq()
+{
+    _eq_events->call(this, &SX1276::OnDio4Irq);
+}
+
+void SX1276::enqueueOnDio5Irq()
+{
+    /* NULL */
+}
+
+// Fine nuova parte per RTOS
\ No newline at end of file
--- a/sx1276/sx1276.h	Mon Apr 24 09:26:23 2017 +0000
+++ b/sx1276/sx1276.h	Fri Mar 02 15:39:25 2018 +0100
@@ -100,6 +100,12 @@
     RadioSettings_t settings;
 
     static const FskBandwidth_t FskBandwidths[];
+
+    // <RTOS>
+    Thread* _thread_events_queue;
+    EventQueue* _eq_events;
+    // </RTOS>
+
 protected:
 
     /*!
@@ -115,6 +121,11 @@
             PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5 );
     SX1276( RadioEvents_t *events );
     virtual ~SX1276( );
+
+    // <RTOS>
+    void assign_events_queue_thread(Thread* thread) {_thread_events_queue=thread;}
+    void assign_events_queue(EventQueue* event_queue) {_eq_events=event_queue;}
+    // </RTOS>
     
     //-------------------------------------------------------------------------
     //                        Redefined Radio functions
@@ -501,6 +512,36 @@
      * \retval regValue Bandwidth register value.
      */
     static uint8_t GetFskBandwidthRegValue( uint32_t bandwidth );
+
+     // Inizio nuova parte per RTOS
+
+    virtual void enqueueOnTimeoutIrq();
+
+    virtual void enqueueRadioEvent_RxTimeout();
+    virtual void enqueueRadioEvent_TxTimeout();
+    virtual void enqueueRadioEvent_RxError();
+    virtual void enqueueRadioEvent_RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
+    virtual void enqueueRadioEvent_TxDone();
+    virtual void enqueueRadioEvent_FhssChangeChannel(uint8_t currentChannel);
+    virtual void enqueueRadioEvent_CadDone(bool channelActivityDetected);
+    virtual void enqueueOnDio0Irq();
+    virtual void enqueueOnDio1Irq();
+    virtual void enqueueOnDio2Irq();
+    virtual void enqueueOnDio3Irq();
+    virtual void enqueueOnDio4Irq();
+    virtual void enqueueOnDio5Irq();
+
+private:
+
+    void radioEvent_RxTimeout();
+    void radioEvent_TxTimeout();
+    void radioEvent_RxError();
+    void radioEvent_RxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
+    void radioEvent_TxDone();
+    void radioEvent_FhssChangeChannel(uint8_t currentChannel);
+    void radioEvent_CadDone(bool channelActivityDetected);
+
+    // Fine nuova parte per RTOS
 };
 
 #endif // __SX1276_H__