Koncentrator

Dependencies:   SX127x mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
MrSteel
Date:
Wed Jun 03 13:50:55 2015 +0000
Commit message:
Koncentrator

Changed in this revision

SX127x.lib Show annotated file Show diff for this revision Revisions of this file
application.h Show annotated file Show diff for this revision Revisions of this file
comm.cpp Show annotated file Show diff for this revision Revisions of this file
comm.h Show annotated file Show diff for this revision Revisions of this file
fun.cpp Show annotated file Show diff for this revision Revisions of this file
fun.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r dd55179403eb SX127x.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SX127x.lib	Wed Jun 03 13:50:55 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/dudmuck/code/SX127x/#7382c260c4b1
diff -r 000000000000 -r dd55179403eb application.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application.h	Wed Jun 03 13:50:55 2015 +0000
@@ -0,0 +1,81 @@
+#ifndef __APPLICATION_H__
+#define __APPLICATION_H__
+
+// Application
+#define APP                     "WIND Concentrator"
+#define SOURCE_ID               0
+#define DESTINATION_ID          0
+
+#define PC_INTERVAL             1
+#define FIBER_OPTIC_INTERVAL    1
+#define LORA_INTERVAL           1
+
+// Uart's
+#define PC_RX_BUFFER            500
+#define FIBER_OPTIC_RX_BUFFER   500
+#define LORA_RX_BUFFER          500
+
+// Uart's bauds
+#define PC_BAUD                 115200
+#define FIBER_OPTIC_BAUD        57600
+
+// Enable modules
+#define PC_ENABLE               1
+#define FIBER_OPTIC_ENABLE      0
+#define LORA_ENABLE             1
+
+// Protocol
+#define pSTX                    0x02
+#define pETX                    0x03
+#define pBEL                    0x07
+#define pACK                    0x06
+
+enum systemEnum{
+    Clear   = 0,
+    Set     = 0,
+    SetLed  = 0,
+    ClearLed= 1
+};
+
+enum communications {
+    lora_com,fiberOptic_com,pc_com
+};
+
+// System settings structure
+typedef struct {
+    unsigned char last_ppe_id[3];      // Last transmited PPE ID
+    unsigned char last_ppe_id_fo;
+    unsigned long TXCharCounter[3];    // Transmited chars -> PC,FIBER_OPTIC,LoRa 
+    unsigned long RXCharCounter[3];    // Received chars -> PC,FIBER_OPTIC,LoRa 
+    unsigned long errorCounter[3];  
+    unsigned char fiberOpticEnable;
+}systemStructure;
+
+extern systemStructure sys;
+
+extern Timer txFiberOpticTmr;
+extern Timer rxFiberOpticTmr;
+extern Timer pcTmr;
+
+enum counters{
+    PC,FO,LO,GS  
+};
+
+extern DigitalOut LEDG;          // Testni Led Green
+extern DigitalOut LEDR;          // Testni Led Red
+
+extern DigitalOut RX_LED;
+extern DigitalOut TX_LED;
+
+//extern DigitalOut FIBER_OPTIC_TX;
+extern DigitalOut FIBER_OPTIC_TX_PWR;
+extern DigitalOut FIBER_OPTIC_RX_PWR;
+
+// Turn On fiber optic
+extern DigitalIn OperationMode;
+
+extern RawSerial pc;            // USB serial port
+extern RawSerial fiberOptic;    // Fiber Optic serial port
+extern RawSerial lora;          // GSM serial port
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r dd55179403eb comm.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/comm.cpp	Wed Jun 03 13:50:55 2015 +0000
@@ -0,0 +1,244 @@
+#include "mbed.h"
+#include "stdlib.h"
+#include "rtos.h"
+#include "sx127x_lora.h"
+#include "comm.h"
+#include "fun.h"
+#include "application.h"
+
+queueStruct queue[5];
+uartBufferStructure pcBuffer;
+uartBufferStructure fiberOpticBuffer;
+uartBufferStructure loraBuffer;
+
+Mutex pc_mutex;                 // Mutex for RTOS system - USB serial port
+Mutex fiberOptic_mutex;         // Mutex for RTOS system - Fiber optic port
+Mutex lora_mutex;
+
+Timer txFiberOpticTmr;
+Timer rxFiberOpticTmr;
+Timer pcTmr;
+
+void pc_thread(void const *argument) {
+    pcBuffer.buf = (unsigned char *)malloc(sizeof(char)*PC_RX_BUFFER);
+    pc.baud(PC_BAUD);
+    pc.attach(&pc_handle); 
+    pc_mutex.lock();
+    pc.printf("\n#: Application %s \n#: Source ID: %d \n#: Destination ID: %d \n#: PC thread Started with BAUD(%d)", APP,SOURCE_ID,DESTINATION_ID,PC_BAUD);
+    pc_mutex.unlock();   
+    while(true){ 
+        wait_ms(1);
+    }
+}
+
+void lora_thread(void const *argument){
+    Timer txLoraTmr;
+
+    // Initialize lora
+    kom.init();
+    Lora.enable();
+    Lora.setSf(8);
+    Lora.setBw(250);
+    
+    kom.set_frf_MHz(868); 
+    Lora.start_rx(); 
+     
+    pc_mutex.lock();
+    pc.printf("\n#: LoRa thread started");
+    pc_mutex.unlock();
+    loraBuffer.buf = (unsigned char *)malloc(sizeof(char)*LORA_RX_BUFFER); 
+    
+    txLoraTmr.stop();    
+    txLoraTmr.reset();    
+    txLoraTmr.start();
+    
+    int step = 0;
+    while(true){ 
+        // PING rutine   
+        
+        if(txLoraTmr.read_ms()>=500 && step==0) {  
+            unsigned char tmpStr[] = {pSTX, sys.last_ppe_id[0], 0, SOURCE_ID, 0, 1, 0, 1, pBEL, pETX};            
+            //lora_mutex.lock();  
+            printStr(LORA_TARGET,tmpStr,sizeof(tmpStr));
+            //lora_mutex.unlock();                        
+            sys.last_ppe_id[0] = sys.last_ppe_id[0] + 1;   
+            txLoraTmr.reset();           
+            step++;
+            
+            pc_mutex.lock();
+            pc.printf("\n<:");            
+            pc_mutex.unlock();
+            TX_LED = SetLed;
+        }
+        if(txLoraTmr.read_ms()>=500 && step==1) { 
+            unsigned char tmpStr[] = {pSTX, sys.last_ppe_id[1], 0, SOURCE_ID, 0, 2, 0, 1, pBEL, pETX};           
+            //lora_mutex.lock();  
+            printStr(LORA_TARGET,tmpStr,sizeof(tmpStr));  
+            //lora_mutex.unlock();                                              
+            sys.last_ppe_id[1] = sys.last_ppe_id[1] + 1;              
+            txLoraTmr.reset();             
+            step++;
+            TX_LED = SetLed;          
+        } 
+        if(txLoraTmr.read_ms()>=500 && step==2) {      
+            unsigned char tmpStr[] = {pSTX, sys.last_ppe_id[2], 0, SOURCE_ID, 0, 3, 0, 1, pBEL, pETX};           
+            //lora_mutex.lock();  
+            printStr(LORA_TARGET,tmpStr,sizeof(tmpStr));   
+            //lora_mutex.unlock();                                             
+            sys.last_ppe_id[2] = sys.last_ppe_id[2] + 1;              
+            txLoraTmr.reset();             
+            step=0;
+            TX_LED = SetLed;             
+        }        
+        if(Lora.service() == SERVICE_READ_FIFO) {
+            unsigned int pyloadSize = kom.read_reg(REG_LR_RXNBBYTES);
+            float rssi = Lora.get_pkt_rssi();
+            unsigned int i;       
+            for(i=0;i<pyloadSize;i++){              
+                loraBuffer.buf[i] = kom.rx_buf[i];
+                loraBuffer.lenght = loraBuffer.lenght++;
+            }  
+            pc_mutex.lock();
+            pc.printf("[%d,%2.1f]",loraBuffer.buf[3],rssi);
+            pc_mutex.unlock();
+        }     
+        
+    }
+}
+
+void fiberOptic_thread(void const *argument){
+    OperationMode.mode(PullUp);   
+    fiberOpticBuffer.buf = (unsigned char *)malloc(sizeof(char)*FIBER_OPTIC_RX_BUFFER); 
+    fiberOptic.baud(FIBER_OPTIC_BAUD);
+    fiberOptic.attach(&fiberOptic_handle);  
+    if(!OperationMode.read()) { 
+        pc_mutex.lock();
+        pc.printf("\n#: Fiber Optic thread started with BAUD(%d)",FIBER_OPTIC_BAUD);
+        pc_mutex.unlock();
+        //FIBER_OPTIC_TX = 1;   
+        sys.fiberOpticEnable = true;
+        FIBER_OPTIC_TX_PWR = 0;
+        FIBER_OPTIC_RX_PWR = 0;
+        
+        txFiberOpticTmr.start();     
+          
+        while(true) {        
+            // PING rutine        
+            if(txFiberOpticTmr.read()>FIBER_OPTIC_INTERVAL) {
+                if(sys.last_ppe_id_fo==0)
+                    sys.last_ppe_id_fo = 1;
+                pc_mutex.lock();
+                pc.printf("\n>: PPE_ID_FO: %d, NODE: 1", sys.last_ppe_id_fo);
+                pc_mutex.unlock();     
+                fiberOptic_mutex.lock();
+                fiberOptic.printf("%c%c%c%c%c%c%c%c%c%c",pSTX,sys.last_ppe_id_fo,0,SOURCE_ID,0,1,0,1,pACK,pETX);
+                fiberOptic_mutex.unlock();           
+                sys.last_ppe_id_fo = sys.last_ppe_id_fo + 1;    
+                txFiberOpticTmr.reset();
+            }   
+            if(fiberOpticBuffer.flg) {
+                if(fiberOpticBuffer.lenght >= 5){
+                    pc_mutex.lock();
+                    pc.printf("\n>: Received shitty payload");
+                    pc_mutex.unlock(); 
+                    fiberOpticBuffer.flg = false;
+                    fiberOpticBuffer.lenght = false;
+                }
+                if(fiberOpticBuffer.buf[0]==pSTX && fiberOpticBuffer.buf[fiberOpticBuffer.lenght-1]==pETX ) {
+                    pc_mutex.lock();
+                    pc.printf("\n>: Received shitty payload");
+                    pc_mutex.unlock(); 
+                    fiberOpticBuffer.flg = false;
+                    fiberOpticBuffer.lenght = false;
+                }
+                
+            }
+        }
+    }
+    else {    
+        sys.fiberOpticEnable = false;    
+        pc_mutex.lock();
+        pc.printf("\n#: Fiber Optic thread disabled!\n");
+        pc_mutex.unlock();
+        //FIBER_OPTIC_TX = 0; 
+        FIBER_OPTIC_TX_PWR = 1;
+        FIBER_OPTIC_RX_PWR = 1;
+    }
+}
+
+void pc_handle(void) {
+    //LEDG = SetLed;
+    pc_mutex.lock();
+    unsigned char c = pc.getc();  
+    pc_mutex.unlock();
+    sys.RXCharCounter[pc_com] = sys.RXCharCounter[pc_com]+1;
+    if(pcBuffer.lenght < PC_RX_BUFFER) {
+        pcBuffer.buf[pcBuffer.lenght]   = c;
+        pcBuffer.lenght               = pcBuffer.lenght + 1;
+        pcBuffer.flg                  = true;
+    }
+}
+
+void fiberOptic_handle(void) {  
+    //LEDB = SetLed;
+    fiberOptic_mutex.lock();
+    unsigned char c = fiberOptic.getc();  
+    fiberOptic_mutex.unlock();    
+                    
+    if(fiberOpticBuffer.lenght < FIBER_OPTIC_RX_BUFFER) {
+        fiberOpticBuffer.buf[fiberOpticBuffer.lenght] = c;
+        fiberOpticBuffer.lenght               = fiberOpticBuffer.lenght + 1;
+        fiberOpticBuffer.flg                  = true;
+    }
+}
+
+void printBuffer(uartBufferStructure buffer) {
+    if(buffer.flg) {
+        unsigned int i;
+        pc_mutex.lock();
+        pc.printf("\n<: ");
+        pc_mutex.unlock(); 
+        for(i=0;i<buffer.lenght-1;i++){
+            pc_mutex.lock();
+            pc.printf("%02x ",buffer.buf[i]);
+            pc_mutex.unlock();
+        }
+    }
+       
+}
+
+
+
+void lora_rx_thread(void const *argument) {
+    /*
+    unsigned int i;
+    while(true) {        
+        lora_mutex.lock();        
+        if(Lora.service() == SERVICE_READ_FIFO) {
+        lora_mutex.unlock();  
+         
+            pc_mutex.lock();
+            pc.printf(".");
+            pc_mutex.unlock();  
+            unsigned int pyloadSize = kom.read_reg(REG_LR_RXNBBYTES);
+            float rssi = Lora.get_pkt_rssi();
+            
+            while(Lora.service() == SERVICE_READ_FIFO) { 
+                loraBuffer.buf[i] = kom.rx_buf[i];
+                loraBuffer.lenght = loraBuffer.lenght++;
+            }
+            
+                pc_mutex.lock();
+                pc.printf(".");
+                pc_mutex.unlock();  
+            while(Lora.service() == SERVICE_READ_FIFO) { 
+                loraBuffer.buf[i] = kom.rx_buf[i];
+                loraBuffer.lenght = loraBuffer.lenght++;
+                pc_mutex.lock();
+                pc.printf("Tukaj");
+                pc_mutex.unlock();  
+           // }
+        }
+    }
+    */
+}
\ No newline at end of file
diff -r 000000000000 -r dd55179403eb comm.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/comm.h	Wed Jun 03 13:50:55 2015 +0000
@@ -0,0 +1,45 @@
+#ifndef __COMM_H__
+#define __COMM_H__
+
+// Uart's targets for output payload
+#define PC_TARGET               0
+#define FIBER_OPTIC_TARGET      1
+#define LORA_TARGET             2
+//#define GSM_TARGET              3
+
+// Structures
+typedef struct {
+    unsigned char   flg;
+    unsigned int    lenght;
+    unsigned char   *buf;
+} uartBufferStructure;
+
+typedef struct {
+   unsigned char   flg; 
+    unsigned char  buf[50];
+} queueStruct;
+
+extern queueStruct queue[5];
+extern uartBufferStructure pcBuffer;
+extern uartBufferStructure fiberOpticBuffer;
+extern uartBufferStructure loraBuffer;
+
+extern Mutex pc_mutex; 
+extern Mutex fiberOptic_mutex;
+
+extern Mutex lora_mutex;
+
+extern SX127x kom;
+extern SX127x_lora Lora;
+
+void pc_thread(void const *argument);
+void pc_handle(void);      
+void fiberOptic_thread(void const *argument);
+void fiberOptic_handle(void); 
+void lora_thread(void const *argument);
+void printBuffer(uartBufferStructure buffer);
+
+void lora_rx_thread(void const *argument);
+
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r dd55179403eb fun.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fun.cpp	Wed Jun 03 13:50:55 2015 +0000
@@ -0,0 +1,64 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "sx127x_lora.h"
+#include "comm.h"
+#include "fun.h"
+#include "application.h"
+
+unsigned long readCurrentTimestamp( void ) {
+    time_t seconds = time(NULL);
+    return seconds;
+}
+
+void setCurrentTimeDate( unsigned long ts ) {
+    set_time(ts);
+}
+
+
+void printStr(unsigned int destination, unsigned char * str, unsigned int len) {
+    unsigned int i;
+    
+    #ifdef LORA_TARGET     
+    if(destination==LORA_TARGET) {
+        kom.write_reg(0x22,len);   
+    } 
+    #endif
+    
+    for(i=0;i<len;i++) {
+        switch(destination) {            
+            #ifdef PC_TARGET   
+            case PC_TARGET:
+                pc_mutex.lock();
+                pc.putc(str[i]);
+                pc_mutex.unlock();
+                break;    
+            #endif      
+            #ifdef FIBER_OPTIC_TARGET     
+            case FIBER_OPTIC_TARGET:
+                fiberOptic_mutex.lock();    
+                fiberOptic.putc(str[i]);
+                fiberOptic_mutex.unlock();            
+                break;   
+            #endif
+            #ifdef LORA_TARGET            
+            case LORA_TARGET:                 
+                kom.tx_buf[i] = str[i];          
+                break;
+            #endif
+            #ifdef GSM_TARGET
+            case GSM_TARGET:
+                //gsm_mutex.lock();                
+                //gsm_mutex.unlock();             
+                break;
+            #endif
+            default:
+                break;   
+        }
+    } 
+    #ifdef LORA_TARGET   
+    if(destination==LORA_TARGET)  
+        Lora.start_tx(len);        
+        while(Lora.service() != SERVICE_TX_DONE);   
+        Lora.start_rx();  
+    #endif
+}
\ No newline at end of file
diff -r 000000000000 -r dd55179403eb fun.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fun.h	Wed Jun 03 13:50:55 2015 +0000
@@ -0,0 +1,8 @@
+#ifndef __FUN_H__
+#define __FUN_H__
+
+unsigned long readCurrentTimestamp( void );
+void setCurrentTimeDate( unsigned long ts );
+void printStr(unsigned int destination, unsigned char * str, unsigned int len);
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r dd55179403eb main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jun 03 13:50:55 2015 +0000
@@ -0,0 +1,45 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "stdlib.h"
+#include "sx127x_lora.h"
+#include "comm.h"
+#include "fun.h"
+#include "application.h"
+
+// Lora TX/RX indication
+DigitalOut RX_LED(LED1);             // LED blink on receive - LoRa
+DigitalOut TX_LED(LED2);             // LED blink on transmit - LoRa
+
+// Serial ports
+RawSerial pc(USBTX,USBRX);          // USB serial port
+RawSerial fiberOptic(PTE0,PTE1);    // Fiber Optic serial port
+
+// Lora communication
+SX127x kom(PTD2, PTD3, PTD1, PTD0, PTD5, PTA13, PTC9);
+SX127x_lora Lora(kom);
+
+// Enable/Disable fiber optic mode
+DigitalIn OperationMode(PTE20);
+
+// Enable disable tx pin on fiber optic
+//DigitalOut FIBER_OPTIC_TX(PTE0);
+DigitalOut FIBER_OPTIC_TX_PWR(PTB9);
+DigitalOut FIBER_OPTIC_RX_PWR(PTB8);
+
+systemStructure sys;            // System structure
+
+int main(void) {
+     
+    Thread pcThread(pc_thread);
+    Thread loraThread(lora_thread);   
+    //Thread fiberOpticThread(fiberOptic_thread);
+    Thread loraRXThread(lora_rx_thread);
+    
+    while(true) {
+        if(loraBuffer.flg) {
+        } 
+        TX_LED = ClearLed;
+        RX_LED = ClearLed;  
+        wait_ms(1);
+    }
+}
diff -r 000000000000 -r dd55179403eb mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Jun 03 13:50:55 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#d3d0e710b443
diff -r 000000000000 -r dd55179403eb mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Jun 03 13:50:55 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf
\ No newline at end of file