Koncentrator
Dependencies: SX127x mbed-rtos mbed
comm.cpp
00001 #include "mbed.h" 00002 #include "stdlib.h" 00003 #include "rtos.h" 00004 #include "sx127x_lora.h" 00005 #include "comm.h" 00006 #include "fun.h" 00007 #include "application.h" 00008 00009 queueStruct queue[5]; 00010 uartBufferStructure pcBuffer; 00011 uartBufferStructure fiberOpticBuffer; 00012 uartBufferStructure loraBuffer; 00013 00014 Mutex pc_mutex; // Mutex for RTOS system - USB serial port 00015 Mutex fiberOptic_mutex; // Mutex for RTOS system - Fiber optic port 00016 Mutex lora_mutex; 00017 00018 Timer txFiberOpticTmr; 00019 Timer rxFiberOpticTmr; 00020 Timer pcTmr; 00021 00022 void pc_thread(void const *argument) { 00023 pcBuffer.buf = (unsigned char *)malloc(sizeof(char)*PC_RX_BUFFER); 00024 pc.baud(PC_BAUD); 00025 pc.attach(&pc_handle); 00026 pc_mutex.lock(); 00027 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); 00028 pc_mutex.unlock(); 00029 while(true){ 00030 wait_ms(1); 00031 } 00032 } 00033 00034 void lora_thread(void const *argument){ 00035 Timer txLoraTmr; 00036 00037 // Initialize lora 00038 kom.init(); 00039 Lora.enable(); 00040 Lora.setSf(8); 00041 Lora.setBw(250); 00042 00043 kom.set_frf_MHz(868); 00044 Lora.start_rx(); 00045 00046 pc_mutex.lock(); 00047 pc.printf("\n#: LoRa thread started"); 00048 pc_mutex.unlock(); 00049 loraBuffer.buf = (unsigned char *)malloc(sizeof(char)*LORA_RX_BUFFER); 00050 00051 txLoraTmr.stop(); 00052 txLoraTmr.reset(); 00053 txLoraTmr.start(); 00054 00055 int step = 0; 00056 while(true){ 00057 // PING rutine 00058 00059 if(txLoraTmr.read_ms()>=500 && step==0) { 00060 unsigned char tmpStr[] = {pSTX, sys.last_ppe_id[0], 0, SOURCE_ID, 0, 1, 0, 1, pBEL, pETX}; 00061 //lora_mutex.lock(); 00062 printStr(LORA_TARGET,tmpStr,sizeof(tmpStr)); 00063 //lora_mutex.unlock(); 00064 sys.last_ppe_id[0] = sys.last_ppe_id[0] + 1; 00065 txLoraTmr.reset(); 00066 step++; 00067 00068 pc_mutex.lock(); 00069 pc.printf("\n<:"); 00070 pc_mutex.unlock(); 00071 TX_LED = SetLed; 00072 } 00073 if(txLoraTmr.read_ms()>=500 && step==1) { 00074 unsigned char tmpStr[] = {pSTX, sys.last_ppe_id[1], 0, SOURCE_ID, 0, 2, 0, 1, pBEL, pETX}; 00075 //lora_mutex.lock(); 00076 printStr(LORA_TARGET,tmpStr,sizeof(tmpStr)); 00077 //lora_mutex.unlock(); 00078 sys.last_ppe_id[1] = sys.last_ppe_id[1] + 1; 00079 txLoraTmr.reset(); 00080 step++; 00081 TX_LED = SetLed; 00082 } 00083 if(txLoraTmr.read_ms()>=500 && step==2) { 00084 unsigned char tmpStr[] = {pSTX, sys.last_ppe_id[2], 0, SOURCE_ID, 0, 3, 0, 1, pBEL, pETX}; 00085 //lora_mutex.lock(); 00086 printStr(LORA_TARGET,tmpStr,sizeof(tmpStr)); 00087 //lora_mutex.unlock(); 00088 sys.last_ppe_id[2] = sys.last_ppe_id[2] + 1; 00089 txLoraTmr.reset(); 00090 step=0; 00091 TX_LED = SetLed; 00092 } 00093 if(Lora.service() == SERVICE_READ_FIFO) { 00094 unsigned int pyloadSize = kom.read_reg(REG_LR_RXNBBYTES); 00095 float rssi = Lora.get_pkt_rssi(); 00096 unsigned int i; 00097 for(i=0;i<pyloadSize;i++){ 00098 loraBuffer.buf[i] = kom.rx_buf[i]; 00099 loraBuffer.lenght = loraBuffer.lenght++; 00100 } 00101 pc_mutex.lock(); 00102 pc.printf("[%d,%2.1f]",loraBuffer.buf[3],rssi); 00103 pc_mutex.unlock(); 00104 } 00105 00106 } 00107 } 00108 00109 void fiberOptic_thread(void const *argument){ 00110 OperationMode.mode(PullUp); 00111 fiberOpticBuffer.buf = (unsigned char *)malloc(sizeof(char)*FIBER_OPTIC_RX_BUFFER); 00112 fiberOptic.baud(FIBER_OPTIC_BAUD); 00113 fiberOptic.attach(&fiberOptic_handle); 00114 if(!OperationMode.read()) { 00115 pc_mutex.lock(); 00116 pc.printf("\n#: Fiber Optic thread started with BAUD(%d)",FIBER_OPTIC_BAUD); 00117 pc_mutex.unlock(); 00118 //FIBER_OPTIC_TX = 1; 00119 sys.fiberOpticEnable = true; 00120 FIBER_OPTIC_TX_PWR = 0; 00121 FIBER_OPTIC_RX_PWR = 0; 00122 00123 txFiberOpticTmr.start(); 00124 00125 while(true) { 00126 // PING rutine 00127 if(txFiberOpticTmr.read()>FIBER_OPTIC_INTERVAL) { 00128 if(sys.last_ppe_id_fo==0) 00129 sys.last_ppe_id_fo = 1; 00130 pc_mutex.lock(); 00131 pc.printf("\n>: PPE_ID_FO: %d, NODE: 1", sys.last_ppe_id_fo); 00132 pc_mutex.unlock(); 00133 fiberOptic_mutex.lock(); 00134 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); 00135 fiberOptic_mutex.unlock(); 00136 sys.last_ppe_id_fo = sys.last_ppe_id_fo + 1; 00137 txFiberOpticTmr.reset(); 00138 } 00139 if(fiberOpticBuffer.flg) { 00140 if(fiberOpticBuffer.lenght >= 5){ 00141 pc_mutex.lock(); 00142 pc.printf("\n>: Received shitty payload"); 00143 pc_mutex.unlock(); 00144 fiberOpticBuffer.flg = false; 00145 fiberOpticBuffer.lenght = false; 00146 } 00147 if(fiberOpticBuffer.buf[0]==pSTX && fiberOpticBuffer.buf[fiberOpticBuffer.lenght-1]==pETX ) { 00148 pc_mutex.lock(); 00149 pc.printf("\n>: Received shitty payload"); 00150 pc_mutex.unlock(); 00151 fiberOpticBuffer.flg = false; 00152 fiberOpticBuffer.lenght = false; 00153 } 00154 00155 } 00156 } 00157 } 00158 else { 00159 sys.fiberOpticEnable = false; 00160 pc_mutex.lock(); 00161 pc.printf("\n#: Fiber Optic thread disabled!\n"); 00162 pc_mutex.unlock(); 00163 //FIBER_OPTIC_TX = 0; 00164 FIBER_OPTIC_TX_PWR = 1; 00165 FIBER_OPTIC_RX_PWR = 1; 00166 } 00167 } 00168 00169 void pc_handle(void) { 00170 //LEDG = SetLed; 00171 pc_mutex.lock(); 00172 unsigned char c = pc.getc(); 00173 pc_mutex.unlock(); 00174 sys.RXCharCounter[pc_com] = sys.RXCharCounter[pc_com]+1; 00175 if(pcBuffer.lenght < PC_RX_BUFFER) { 00176 pcBuffer.buf[pcBuffer.lenght] = c; 00177 pcBuffer.lenght = pcBuffer.lenght + 1; 00178 pcBuffer.flg = true; 00179 } 00180 } 00181 00182 void fiberOptic_handle(void) { 00183 //LEDB = SetLed; 00184 fiberOptic_mutex.lock(); 00185 unsigned char c = fiberOptic.getc(); 00186 fiberOptic_mutex.unlock(); 00187 00188 if(fiberOpticBuffer.lenght < FIBER_OPTIC_RX_BUFFER) { 00189 fiberOpticBuffer.buf[fiberOpticBuffer.lenght] = c; 00190 fiberOpticBuffer.lenght = fiberOpticBuffer.lenght + 1; 00191 fiberOpticBuffer.flg = true; 00192 } 00193 } 00194 00195 void printBuffer(uartBufferStructure buffer) { 00196 if(buffer.flg) { 00197 unsigned int i; 00198 pc_mutex.lock(); 00199 pc.printf("\n<: "); 00200 pc_mutex.unlock(); 00201 for(i=0;i<buffer.lenght-1;i++){ 00202 pc_mutex.lock(); 00203 pc.printf("%02x ",buffer.buf[i]); 00204 pc_mutex.unlock(); 00205 } 00206 } 00207 00208 } 00209 00210 00211 00212 void lora_rx_thread(void const *argument) { 00213 /* 00214 unsigned int i; 00215 while(true) { 00216 lora_mutex.lock(); 00217 if(Lora.service() == SERVICE_READ_FIFO) { 00218 lora_mutex.unlock(); 00219 00220 pc_mutex.lock(); 00221 pc.printf("."); 00222 pc_mutex.unlock(); 00223 unsigned int pyloadSize = kom.read_reg(REG_LR_RXNBBYTES); 00224 float rssi = Lora.get_pkt_rssi(); 00225 00226 while(Lora.service() == SERVICE_READ_FIFO) { 00227 loraBuffer.buf[i] = kom.rx_buf[i]; 00228 loraBuffer.lenght = loraBuffer.lenght++; 00229 } 00230 00231 pc_mutex.lock(); 00232 pc.printf("."); 00233 pc_mutex.unlock(); 00234 while(Lora.service() == SERVICE_READ_FIFO) { 00235 loraBuffer.buf[i] = kom.rx_buf[i]; 00236 loraBuffer.lenght = loraBuffer.lenght++; 00237 pc_mutex.lock(); 00238 pc.printf("Tukaj"); 00239 pc_mutex.unlock(); 00240 // } 00241 } 00242 } 00243 */ 00244 }
Generated on Tue Jul 12 2022 18:10:08 by 1.7.2