App to configure a DDS (AD9854) using a K64F. USB an Ethernet were used as interface.

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

Revision:
1:072a0ab47d9c
Parent:
0:c9bdd57867af
Child:
2:f9e1bcb3409a
--- a/main.cpp	Thu Feb 05 19:04:48 2015 +0000
+++ b/main.cpp	Thu Feb 05 21:32:22 2015 +0000
@@ -7,39 +7,39 @@
 
 #include "mbed.h"
 #include "rtos.h"
+#include "EthernetInterface.h"
 #include "JroDDS.h"
 #include "JroIpdata.h"
-#include "JroEthernet.h"
 #include "SerialDriver.h"
 
+//Device commands
+#define CMD_RESET       0X01
+#define CMD_ENABLE      0x02
+#define CMD_CHANGE_IP   0x03
+
 #define BUFFER_SIZE     256
- 
-//ETHERNET
-#define ECHO_SERVER_PORT   2000
-//SERIAL
-#define SERIAL_BAUDRATE     1000000
-
-
-char* ip = "10.10.20.65";               // ip
-char* mask = "255.255.255.0";           // mask
-char* gateway = "10.10.20.1";           // gateway
-
-char* ip2 = "10.10.20.64"; 
-
 static char rx_buffer[BUFFER_SIZE];
 static char tx_buffer[BUFFER_SIZE]; 
 
 //SERIAL
+#define SERIAL_BAUDRATE     1000000
+const char* OK_MSG = "DDSv2: Successful";
+const char* KO_MSG = "DDSv2: Unsuccessful";
+
 SerialDriver screen(USBTX, USBRX);
 SerialDriver uart(D1, D0);
-//JroSerial jroUart(D1,D0, &screen);
 Thread *ser_thread_ptr;
 
 //ETHERNET
+#define ECHO_SERVER_PORT   2000
+const char* DEVICE_NAME = "DDSv2015";
+const char* IP = "10.10.20.63";               // IP
+const char* MASK = "255.255.255.0";           // MASK
+const char* GATEWAY = "10.10.20.1";           // GATEWAY
+
 IpData ipData(tx_buffer);
 EthernetInterface eth;
 TCPSocketServer server;
-JroEthernet jroEth(&eth);
 
 Thread *eth_thread_ptr;
 
@@ -60,29 +60,15 @@
 DigitalOut LedG(LED2);          
 DigitalOut LedB(LED3);  
 
-//PWM
-//PwmOut     LedR(LED1);
-
-// This thread forwards from uart to pc
-void forwardUart(void const * argument)
-{
-    // Sometimes You're the Nail
-    while(1)
-    {      
-        screen.putc(uart.getc());
-        LedG= !LedG;    
-    }
-}
-
-
 void waitSerialData_thread(void const *args){
     
     int n;
-    
-    Thread::signal_wait(0x1);
+    bool successful;
     
-    screen.putc(0x34);
-    screen.putc(0x30);
+    //Thread::signal_wait(0x1);
+    
+    //screen.putc(0x34);
+    //screen.putc(0x30);
     
     //uart.baud(SERIAL_BAUDRATE);
     
@@ -90,6 +76,7 @@
     
     while(1){
         //__screen.printf("\r\nWaiting serial data...\r\n");
+        successful = false;
         
         if (uart.isRxBufferEmpty()){
             Thread::wait(10);
@@ -97,30 +84,28 @@
             }
             
         Thread::wait(100);
-        n = uart.read(rx_buffer, 40, false);
-
+        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]);
                 
-        if (n != 40)
-        {   //Data is not complete
-            continue;
-        }
+        if (n == 40)   
+            if (dds_device.setAllDevice(rx_buffer) == 1)
+                successful = true;
         
-        if (dds_device.setAllDevice(rx_buffer, &screen) == 1){
-            screen.putc(0x34);
-            screen.putc(0x34);
-            //jroSerial.writeData("Done");
+        if (successful){        
+            for (int i=0; i<strlen(OK_MSG); i++){   
+                uart.putc(OK_MSG[i]);
+            }
         }
-        else
-        {
-            //jroSerial.writeData("Error");
-            screen.putc(0x34);
-            screen.putc(0x35);
+        else{
+            for (int i=0; i<strlen(KO_MSG); i++){   
+                uart.putc(KO_MSG[i]);
+            }
         }
         
     }
@@ -129,39 +114,57 @@
 
 void waitEthData_thread(void const *args){
     
-    int n;
+    TCPSocketConnection client;
+    int status;
+    int n, totalSize=0;
     
-    Thread::signal_wait(0x1);
+    //Thread::signal_wait(0x1);
     
     LedR = 0;
     
-    screen.putc(0x35);
-    screen.putc(0x30);
+    //screen.putc(0x35);
+    //screen.putc(0x30);
+    eth.setName(DEVICE_NAME);
+    status = eth.init(IP, MASK, GATEWAY);
+    status = eth.connect();
     
-    jroEth.init(ip, mask, gateway);
-    jroEth.openTCPServer(&server, ECHO_SERVER_PORT);
+    server.bind(ECHO_SERVER_PORT);
+    server.listen(1);
     
     LedR = 1;
 
-    screen.putc(0x35);
-    screen.putc(0x31);
-        
+    //screen.putc(0x35);
+    //screen.putc(0x31);
     
     //__screen.printf("\r\nEth thread initialized...\r\n");
 
-    while(1) {
+    while(1)
+    {
         LedB = 0;
+        n = 0;
+        totalSize = 0;
         //screen.printf("\r\n ********************** \r\n"); 
         //__screen.printf("\r\nWait for new connection...\r\n");
-        n = jroEth.read(rx_buffer);
+        server.accept(client);
+        
+        client.set_blocking(false, 500); // Timeout after (1.5)s
+        
+        //__screen.printf("Connection from: %s\r\n", client.get_address());
         
-        screen.putc(0x35);
-        screen.putc(0x32);
+        while (true) {
+            n = client.receive(rx_buffer, sizeof(rx_buffer));
+            if (n <= 0) break;
+            totalSize += n;
+            Thread::wait(100);
+        }
+        
+        //screen.putc(0x35);
+        //screen.putc(0x32);
     
-        if (n < 1)
+        if (totalSize < 1)
             continue;
         
-        if (ipData.decode(rx_buffer, n) == 0)
+        if (ipData.decode(rx_buffer, totalSize) == 0)
             continue;
         
         if (ipData.getCmd() == CMD_CHANGE_IP){
@@ -171,8 +174,11 @@
         dds_device.setCommand(ipData.getCmd(), ipData.getPayload(), ipData.getPayloadLen());
         ipData.encode(ipData.getCmd(), dds_device.getCmdAnswer(), dds_device.getCmdAnswerLen());
         
-        jroEth.sendResponse(ipData.getTxData(), ipData.getTxDataLen());
-        jroEth.closeClient();
+        client.send(ipData.getTxData(), ipData.getTxDataLen());
+        client.close();
+
+        //screen.putc(0x35);
+        //screen.putc(0x32);
         
     }
 }
@@ -181,14 +187,16 @@
 {
     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);
     
-    //ser_thread_ptr = new Thread(&waitSerialData_thread);
+    ser_thread_ptr = new Thread(&waitSerialData_thread);
     eth_thread_ptr = new Thread(&waitEthData_thread);
     
     //__screen.printf("\r\n ******************************************* \r\n"); 
@@ -205,8 +213,8 @@
     //__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);
+    //eth_thread_ptr->signal_set(0x1);
+    //Thread::wait(1500);
     
     screen.putc(0x33);
     screen.putc(0x31);
@@ -227,15 +235,18 @@
     LedB = 0;
     
     //__screen.printf("\r\nSetting defaults values to DDS...\r\n");
-    dds_device.defaultSettings(&screen);
+    dds_device.defaultSettings();
     LedB = 1;
 
     screen.putc(0x33);
     screen.putc(0x33);
     
-    while(1) {
-       Thread::wait(250);
-        
+    Thread::wait(5000);
+    //int c=0;
+    while(true){
+        Thread::wait(1000);
+        //screen.putc(0x2D + c);
+        //c = (c+1) % 3;
     }
 
 }