Firmware for the controller of the JRO Radar Controller.

Dependencies:   CR2 EthernetInterface FreescaleIAP I2CLCD SerialDriver jro k64f_EthLink mbed-rtos mbed

Fork of JRO_DDSv2 by Miguel Urco

Revision:
2:f9e1bcb3409a
Parent:
1:072a0ab47d9c
Child:
3:f84802422619
--- a/main.cpp	Thu Feb 05 21:32:22 2015 +0000
+++ b/main.cpp	Tue Feb 10 14:35:04 2015 +0000
@@ -25,6 +25,7 @@
 #define SERIAL_BAUDRATE     1000000
 const char* OK_MSG = "DDSv2: Successful";
 const char* KO_MSG = "DDSv2: Unsuccessful";
+const char* NI_MSG = "DDSv2: Not initialized";
 
 SerialDriver screen(USBTX, USBRX);
 SerialDriver uart(D1, D0);
@@ -32,7 +33,7 @@
 
 //ETHERNET
 #define ECHO_SERVER_PORT   2000
-const char* DEVICE_NAME = "DDSv2015";
+const char* DEVICE_NAME = "jicamarcadds";
 const char* IP = "10.10.20.63";               // IP
 const char* MASK = "255.255.255.0";           // MASK
 const char* GATEWAY = "10.10.20.1";           // GATEWAY
@@ -67,15 +68,12 @@
     
     //Thread::signal_wait(0x1);
     
-    //screen.putc(0x34);
-    //screen.putc(0x30);
+    LedG = 1;
     
-    //uart.baud(SERIAL_BAUDRATE);
-    
-    //jroUart.Init(SERIAL_BAUDRATE);
+    uart.baud(SERIAL_BAUDRATE);
     
     while(1){
-        //__screen.printf("\r\nWaiting serial data...\r\n");
+        LedG = 0;
         successful = false;
         
         if (uart.isRxBufferEmpty()){
@@ -83,20 +81,29 @@
             continue;
             }
             
-        Thread::wait(100);
+        Thread::wait(10);
         n = uart.read(rx_buffer, 255, false);
-        /*
-        screen.putc(0x6E);
-        screen.putc(0x3D);
-        screen.putc(n);
-        */
-        //for (int i=0; i<n; i++)
-        //    screen.putc(rx_buffer[i]);
-                
+        
+        //******************** BLINK LED *****************************
+        for (int i=0; i<n; i++){
+            LedG = !LedG;
+            Thread::wait(10);
+        }
+        
+        //******************** DDS NOT INITIALIZED *******************
+        if (!dds_device.wasInitialized()){
+            for (int i=0; i<strlen(NI_MSG); i++){   
+                uart.putc(NI_MSG[i]);
+            }
+            continue;
+        }
+        
+        //********************* SUCCESSFUL DATA **********************
         if (n == 40)   
             if (dds_device.setAllDevice(rx_buffer) == 1)
                 successful = true;
         
+        //******************** REPLY UART*****************************
         if (successful){        
             for (int i=0; i<strlen(OK_MSG); i++){   
                 uart.putc(OK_MSG[i]);
@@ -120,10 +127,8 @@
     
     //Thread::signal_wait(0x1);
     
-    LedR = 0;
+    LedB = 1;
     
-    //screen.putc(0x35);
-    //screen.putc(0x30);
     eth.setName(DEVICE_NAME);
     status = eth.init(IP, MASK, GATEWAY);
     status = eth.connect();
@@ -131,44 +136,49 @@
     server.bind(ECHO_SERVER_PORT);
     server.listen(1);
     
-    LedR = 1;
-
-    //screen.putc(0x35);
-    //screen.putc(0x31);
-    
-    //__screen.printf("\r\nEth thread initialized...\r\n");
+    LedB = 0;
 
     while(1)
     {
         LedB = 0;
         n = 0;
         totalSize = 0;
-        //screen.printf("\r\n ********************** \r\n"); 
-        //__screen.printf("\r\nWait for new connection...\r\n");
+        
         server.accept(client);
         
-        client.set_blocking(false, 500); // Timeout after (1.5)s
-        
-        //__screen.printf("Connection from: %s\r\n", client.get_address());
+        client.set_blocking(false, 500); // Timeout after (0.5)s
         
         while (true) {
+            LedR = !LedR;
             n = client.receive(rx_buffer, sizeof(rx_buffer));
             if (n <= 0) break;
             totalSize += n;
-            Thread::wait(100);
+            Thread::wait(10);
+        }
+        
+        LedB = 1;
+    
+        if (totalSize < 1){
+            client.close();
+            continue;
         }
         
-        //screen.putc(0x35);
-        //screen.putc(0x32);
-    
-        if (totalSize < 1)
+        if (ipData.decode(rx_buffer, totalSize) == 0){
+            client.close();
             continue;
+        }
         
-        if (ipData.decode(rx_buffer, totalSize) == 0)
+        //******************** DDS NOT INITIALIZED *******************
+        if (!dds_device.wasInitialized()){
+            client.send(ipData.getNIData(ipData.getCmd()), ipData.getNIDataLen());
+            client.close();
             continue;
+        }
         
+        //******************** REPLY REQ *****************************
         if (ipData.getCmd() == CMD_CHANGE_IP){
             //changing ip and reseting device
+            //status = eth.setNewAddr(IP2, MASK, GATEWAY);
         }
         
         dds_device.setCommand(ipData.getCmd(), ipData.getPayload(), ipData.getPayloadLen());
@@ -176,9 +186,6 @@
         
         client.send(ipData.getTxData(), ipData.getTxDataLen());
         client.close();
-
-        //screen.putc(0x35);
-        //screen.putc(0x32);
         
     }
 }
@@ -186,62 +193,42 @@
 int main() 
 {
     screen.baud(9600);
-    uart.baud(SERIAL_BAUDRATE);
 
     screen.putc(0x0A);
     screen.putc(0x0D);
     screen.putc(0x33);
-    screen.putc(0x30);
-    
-    
-    //ser_thread_ptr = new Thread(&forwardUart);
+    screen.putc(0x33);
     
     ser_thread_ptr = new Thread(&waitSerialData_thread);
     eth_thread_ptr = new Thread(&waitEthData_thread);
     
-    //__screen.printf("\r\n ******************************************* \r\n"); 
-    //__screen.printf("\r\n ******************************************* \r\n"); 
-    
-    //__screen.printf("\nInitializing program...\r\n");
-    //screen.putc(0x33);
-    //screen.putc(0x30);
-    
-    //LedR.period_ms(100);
-    
-    LedG = 0;
-    
-    //__screen.printf("\r\nHabilitando threads...\r\n");
-    //ser_thread_ptr->signal_set(0x1);
-    //Thread::wait(300);
-    //eth_thread_ptr->signal_set(0x1);
-    //Thread::wait(1500);
+    LedR = 1;
     
     screen.putc(0x33);
-    screen.putc(0x31);
-    //__screen.printf("\r\nInitializing DDS...\r\n");
+    screen.putc(0x32);
     
     while(true){
         if (dds_device.init())
             break;
-            
+        LedR = !LedR;
         Thread::wait(250);
     }
     
-    LedG = 1;
+    LedR = 0;
     
     screen.putc(0x33);
-    screen.putc(0x32);
-    
-    LedB = 0;
+    screen.putc(0x31);
     
-    //__screen.printf("\r\nSetting defaults values to DDS...\r\n");
+    LedR = 1;
+    
     dds_device.defaultSettings();
-    LedB = 1;
+    LedR = 0;
 
     screen.putc(0x33);
-    screen.putc(0x33);
+    screen.putc(0x30);
     
     Thread::wait(5000);
+    
     //int c=0;
     while(true){
         Thread::wait(1000);