SX1276Lib updated in order to be RTOS aware

Fork of SX1276Lib by Semtech

Revision:
27:8a37a9362714
Parent:
26:d09a8ef807e2
diff -r d09a8ef807e2 -r 8a37a9362714 sx1276/sx1276.cpp
--- 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