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

Files at this revision

API Documentation at this revision

Comitter:
miguelcordero191
Date:
Tue Feb 24 20:09:49 2015 +0000
Parent:
2:f9e1bcb3409a
Child:
4:3883bfcdfb63
Commit message:
First release. JRO_DDS2_v1.0; ; Ethernet working; Serial working; LCD working; Storing on internal flash working; Changing Ip Address working

Changed in this revision

AD9854.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
FreescaleIAP.lib Show annotated file Show diff for this revision Revisions of this file
I2CLCD.lib Show annotated file Show diff for this revision Revisions of this file
jro.lib Show annotated file Show diff for this revision Revisions of this file
k64f_EthLink.lib 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.bld Show annotated file Show diff for this revision Revisions of this file
mbed.lib Show diff for this revision Revisions of this file
test_eth.cpp Show annotated file Show diff for this revision Revisions of this file
test_iap.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AD9854.lib	Tue Feb 24 20:09:49 2015 +0000
@@ -0,0 +1,1 @@
+AD9854#156a9e15919e
--- a/EthernetInterface.lib	Tue Feb 10 14:35:04 2015 +0000
+++ b/EthernetInterface.lib	Tue Feb 24 20:09:49 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/miguelcordero191/code/EthernetInterface/#3b0a475eb1ad
+http://developer.mbed.org/users/miguelcordero191/code/EthernetInterface/#303f4d8389a1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FreescaleIAP.lib	Tue Feb 24 20:09:49 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Sissors/code/FreescaleIAP/#186db0d96fcf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/I2CLCD.lib	Tue Feb 24 20:09:49 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/okini3939/code/I2CLCD/#0c291dbc7acc
--- a/jro.lib	Tue Feb 10 14:35:04 2015 +0000
+++ b/jro.lib	Tue Feb 24 20:09:49 2015 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/miguelcordero191/code/jro/#de495ce256b8
+http://developer.mbed.org/users/miguelcordero191/code/jro/#6500be930b36
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/k64f_EthLink.lib	Tue Feb 24 20:09:49 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/loopsva/code/k64f_EthLink/#fbd2336e1193
--- a/main.cpp	Tue Feb 10 14:35:04 2015 +0000
+++ b/main.cpp	Tue Feb 24 20:09:49 2015 +0000
@@ -8,42 +8,63 @@
 #include "mbed.h"
 #include "rtos.h"
 #include "EthernetInterface.h"
-#include "JroDDS.h"
-#include "JroIpdata.h"
 #include "SerialDriver.h"
+#include "I2CLCD.h"
 
-//Device commands
-#define CMD_RESET       0X01
-#define CMD_ENABLE      0x02
-#define CMD_CHANGE_IP   0x03
+#include "k64f_EthLink.h"
+#include "EthUtils.h"
+#include "JroIpdata.h"
+#include "JroSIR.h"
+#include "AD9854.h"
+
+#define mbed_reset        NVIC_SystemReset
 
 #define BUFFER_SIZE     256
 static char rx_buffer[BUFFER_SIZE];
 static char tx_buffer[BUFFER_SIZE]; 
 
+//THREADS
+Thread *ser_thread_ptr;
+Thread *eth_thread_ptr;
+Thread *lcd_thread_ptr;
+Mutex dds_mutex;
+Mutex eth_mutex;
+Mutex lcd_mutex;
+
 //SERIAL
 #define SERIAL_BAUDRATE     1000000
-const char* OK_MSG = "DDSv2: Successful";
-const char* KO_MSG = "DDSv2: Unsuccessful";
-const char* NI_MSG = "DDSv2: Not initialized";
+const char* UART_MSG_OK = "DDS Successful";
+const char* UART_MSG_KO = "DDS Unsuccessful";
+const char* UART_MSG_NC = "DDS Not connected";
+
+Serial screen(USBTX, USBRX);
+SerialDriver uart(D1, D0);
+
 
-SerialDriver screen(USBTX, USBRX);
-SerialDriver uart(D1, D0);
-Thread *ser_thread_ptr;
+//LCD
+I2C i2c_device(I2C_SDA, I2C_SCL);
+I2CLCD lcd(i2c_device, 0x78, LCD20x2);
+
+//const char LCD_WELCOME[]    =   "  JICAMARCA - DDS   ";
+const char LCD_IP_INI[]     =   "IP : checking...    ";
+const char LCD_IP_NC[]      =   "IP : Not connected  ";
+//const char LCD_IP_NI[]      =   "IP : Not initialized";
+
+const char LCD_DAC_INI[]    =   "DAC: checking...    ";
+const char LCD_DAC_NC[]     =   "DAC: Not detected   ";
+const char LCD_DAC_ENA[]    =   "     RF Enabled     ";
+const char LCD_DAC_DIS[]    =   "     RF Disabled    ";
 
 //ETHERNET
 #define ECHO_SERVER_PORT   2000
-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
+const char DEVICE_NAME[] = "jrodds";
 
 IpData ipData(tx_buffer);
+
+k64fEthLink eth_link;
 EthernetInterface eth;
 TCPSocketServer server;
 
-Thread *eth_thread_ptr;
-
 //DDS
 SPI spi_device(D11, D12, D13);
 
@@ -61,6 +82,52 @@
 DigitalOut LedG(LED2);          
 DigitalOut LedB(LED3);  
 
+DigitalIn  IPResetButton(PTB2, PullUp);
+
+int printAddr(char *_ip, char *_mask, char *_gateway){
+    lcd.printf(_ip, 0, 0);
+    lcd.printf(_mask, 0, 1);
+
+    screen.putc(0x0A);
+    screen.putc(0x0D);
+    
+    for (int i=0; i<20; i++){
+        screen.putc(_ip[i]);
+    }
+    screen.putc(0x0A);
+    screen.putc(0x0D);
+    
+    for (int i=0; i<20; i++){
+        screen.putc(_mask[i]);
+    }  
+     
+    screen.putc(0x0A);
+    screen.putc(0x0D);
+    
+    for (int i=0; i<20; i++){
+        screen.putc(_gateway[i]);
+    }
+
+    screen.putc(0x0A);
+    screen.putc(0x0D);
+    
+    return 1;
+}
+
+int wasIPResetPressed(){
+    
+    if (IPResetButton)
+        return 0;
+    
+    //Wait until button will be released
+    while(!IPResetButton){
+        Thread::wait(50);
+    }
+    
+    return 1;
+}
+
+
 void waitSerialData_thread(void const *args){
     
     int n;
@@ -77,7 +144,7 @@
         successful = false;
         
         if (uart.isRxBufferEmpty()){
-            Thread::wait(10);
+            Thread::wait(100);
             continue;
             }
             
@@ -89,14 +156,16 @@
             LedG = !LedG;
             Thread::wait(10);
         }
-        
+        lcd.printf("Serial command received", 0, 1);
         //******************** DDS NOT INITIALIZED *******************
         if (!dds_device.wasInitialized()){
-            for (int i=0; i<strlen(NI_MSG); i++){   
-                uart.putc(NI_MSG[i]);
+            for (int i=0; i<strlen(UART_MSG_NC); i++){   
+                uart.putc(UART_MSG_NC[i]);
             }
             continue;
         }
+        //Lock dds_device before execute any command
+        dds_mutex.lock();
         
         //********************* SUCCESSFUL DATA **********************
         if (n == 40)   
@@ -104,16 +173,14 @@
                 successful = true;
         
         //******************** REPLY UART*****************************
-        if (successful){        
-            for (int i=0; i<strlen(OK_MSG); i++){   
-                uart.putc(OK_MSG[i]);
-            }
-        }
-        else{
-            for (int i=0; i<strlen(KO_MSG); i++){   
-                uart.putc(KO_MSG[i]);
-            }
-        }
+        if (successful)      
+            //for (int i=0; i<strlen(UART_MSG_OK); i++)
+            uart.printf(UART_MSG_OK);
+        
+        else
+            uart.printf(UART_MSG_KO);
+        
+        dds_mutex.unlock();
         
     }
     
@@ -122,16 +189,31 @@
 void waitEthData_thread(void const *args){
     
     TCPSocketConnection client;
-    int status;
+    //int status;
     int n, totalSize=0;
+    char _ip[MAX_IP_LEN], _mask[MAX_IP_LEN], _gateway[MAX_IP_LEN];
     
-    //Thread::signal_wait(0x1);
+    //Lock ethernet resource until initial configuration will be loaded
+    eth_mutex.lock();
+    
+    readIpConfig(_ip, _mask, _gateway);
     
     LedB = 1;
     
+    if (eth.init(_ip, _mask, _gateway) != 0){
+        //mbed_reset();
+    }
+    
     eth.setName(DEVICE_NAME);
-    status = eth.init(IP, MASK, GATEWAY);
-    status = eth.connect();
+    eth.connect();
+    Thread::wait(200);
+    eth_mutex.unlock();
+    
+    //If ethernet connection fails then try again
+    while(eth_link.GetELink() != 0){
+        eth.connect();
+        Thread::wait(200);
+    }
     
     server.bind(ECHO_SERVER_PORT);
     server.listen(1);
@@ -164,10 +246,11 @@
         }
         
         if (ipData.decode(rx_buffer, totalSize) == 0){
+            client.send(ipData.getKOData(0x00), ipData.getKODataLen());
             client.close();
             continue;
         }
-        
+        lcd.printf("Eth command received", 0, 1);
         //******************** DDS NOT INITIALIZED *******************
         if (!dds_device.wasInitialized()){
             client.send(ipData.getNIData(ipData.getCmd()), ipData.getNIDataLen());
@@ -176,22 +259,128 @@
         }
         
         //******************** REPLY REQ *****************************
+        
+        //********* IP CONFIG ****
         if (ipData.getCmd() == CMD_CHANGE_IP){
-            //changing ip and reseting device
-            //status = eth.setNewAddr(IP2, MASK, GATEWAY);
+            
+            if ( splitIpConf(ipData.getPayload(), _ip, _mask, _gateway) ){
+                //changing ip and reseting device
+                client.send(ipData.getOKData(ipData.getCmd()), ipData.getOKDataLen());
+                Thread::wait(200);
+                client.close();
+                
+                saveIpConfig(_ip, _mask, _gateway);
+                eth.setNewAddr(_ip, _mask, _gateway);
+                Thread::wait(500);
+                //mbed_reset();
+                continue;
+            }
+            else{
+                client.send(ipData.getKOData(ipData.getCmd()), ipData.getKODataLen());
+                Thread::wait(200);
+                client.close();
+                continue;
+            }
         }
         
+        //Lock dds_device before execute any command
+        dds_mutex.lock();
+        
+        // ********** OTHER COMMANDS
         dds_device.setCommand(ipData.getCmd(), ipData.getPayload(), ipData.getPayloadLen());
         ipData.encode(ipData.getCmd(), dds_device.getCmdAnswer(), dds_device.getCmdAnswerLen());
         
         client.send(ipData.getTxData(), ipData.getTxDataLen());
+        Thread::wait(10);
         client.close();
         
+        dds_mutex.unlock();
     }
 }
 
+void lcdView_thread(void const *args){
+    
+    char c=0;
+    char lcd_status[2]="\xF7";
+    
+    lcd.printf(LCD_DAC_INI, 0, 0);
+    lcd.printf(LCD_IP_INI, 0, 1);
+    Thread::wait(2000);
+    
+    while(1){
+        /*
+        if (lcd_mutex.trylock()==false){
+            Thread::wait(1000);
+            continue;
+        }
+        */
+        
+        if (dds_device.wasInitialized()){
+            
+            //RF ENABLED OR DISABLED
+            if (c==1){
+                if(dds_device.isRFEnabled()){
+                    lcd.printf(LCD_DAC_ENA, 0, 0);
+                }
+                else{
+                    lcd.printf(LCD_DAC_DIS, 0, 0);
+                }
+                lcd_status[0] = 0x01;
+                lcd.printf(lcd_status, 19, 0);
+            
+            }
+            //FREQUENCY
+            if (c==3){
+                char tmp[21];
+                sprintf(tmp, "Fout = %7.5f*Clk", dds_device.getFreqFactor1());
+                lcd.printf(tmp, 0, 0);
+            }
+            if (c==5){
+                char tmp[21];
+                sprintf(tmp, "Modulation = %s", dds_device.getModeStr() );
+                lcd.printf(tmp, 0, 0);
+            }
+        }
+        else{
+            lcd.printf(LCD_DAC_NC, 0, 0);
+            lcd_status[0] = lcd_status[0] ^ 0xFF;
+            lcd.printf(lcd_status, 19, 0);
+        }
+        
+        //Verify if ethernet resource is free
+        if (eth_mutex.trylock()){
+            if (eth_link.GetELink() == 0){
+                if (c==0){
+                    lcd.printf("IP :                ", 0, 1);
+                    lcd.printf(eth.getIPAddress(), 5, 1);
+                }
+                if (c==2){
+                    lcd.printf("MSK:                ", 0, 1);
+                    lcd.printf(eth.getNetworkMask(), 5, 1);
+                }      
+                if (c==4){
+                    lcd.printf("GW :                ", 0, 1);
+                    lcd.printf(eth.getGateway(), 5, 1);
+                }     
+            }
+            else{
+                lcd.printf(LCD_IP_NC, 0, 1);
+            }
+            eth_mutex.unlock();
+        }
+        else{
+            lcd.printf(LCD_IP_INI, 0, 1);
+        }
+        c++;
+        if (c>5) c=0;
+        //lcd_mutex.unlock();
+        Thread::wait(1000);
+    }
+    
+}
+
 int main() 
-{
+{   
     screen.baud(9600);
 
     screen.putc(0x0A);
@@ -201,6 +390,7 @@
     
     ser_thread_ptr = new Thread(&waitSerialData_thread);
     eth_thread_ptr = new Thread(&waitEthData_thread);
+    lcd_thread_ptr = new Thread(&lcdView_thread);
     
     LedR = 1;
     
@@ -210,32 +400,49 @@
     while(true){
         if (dds_device.init())
             break;
+            
         LedR = !LedR;
         Thread::wait(250);
+        
+        if (wasIPResetPressed()){
+            eraseIpConfig();
+            mbed_reset();
+        }
     }
     
     LedR = 0;
-    
+    /*
     screen.putc(0x33);
     screen.putc(0x31);
-    
+    */
     LedR = 1;
     
     dds_device.defaultSettings();
     LedR = 0;
-
+    /*
     screen.putc(0x33);
     screen.putc(0x30);
-    
-    Thread::wait(5000);
+    */
+    Thread::wait(1000);
     
     //int c=0;
     while(true){
-        Thread::wait(1000);
-        //screen.putc(0x2D + c);
-        //c = (c+1) % 3;
+        
+        if (dds_device.isRFEnabled()){
+            LedR = 0;
+        }
+        else{
+            LedR = 1;
+        }
+        
+        if (wasIPResetPressed()){
+            eraseIpConfig();
+            mbed_reset();
+        }
+        
+        Thread::wait(200);
     }
-
+    
 }
 
 #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Feb 24 20:09:49 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac
\ No newline at end of file
--- a/mbed.lib	Tue Feb 10 14:35:04 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/#4fc01daae5a5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test_eth.cpp	Tue Feb 24 20:09:49 2015 +0000
@@ -0,0 +1,39 @@
+#if 0
+
+#include "mbed.h"
+#include "rtos.h"
+#include "EthernetInterface.h"
+
+RawSerial pc(USBTX, USBRX);
+EthernetInterface eth;
+
+#define IP_INI          "10.10.50.85";               // IP
+#define MASK_INI        "255.255.255.0";           // MASK
+#define GATEWAY_INI     "10.10.50.1";           // GATEWAY
+const char* IP = IP_INI;               // IP
+const char* MASK = MASK_INI;           // MASK
+const char* GATEWAY = GATEWAY_INI;           // GATEWAY
+    
+int main(){
+    pc.printf("Initializing network interface...\r\n");
+    if (0 == eth.init(IP, MASK, GATEWAY)) {
+        // It should always take this path
+        do {
+            pc.printf("Connecting to network...\r\n");
+            if (0 == eth.connect()) {
+                pc.printf("Ethernet connected as %s\r\n", eth.getIPAddress());
+                Thread::wait(5000);
+                pc.printf("lost connection.\r\n");
+                eth.disconnect();
+            }
+            else {
+                pc.printf("  ... failed to connect.\r\n");
+            }
+        } while (1);
+    }
+    else {
+        pc.printf("  ... failed to initialize, rebooting...\r\n");
+    }
+}
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test_iap.cpp	Tue Feb 24 20:09:49 2015 +0000
@@ -0,0 +1,27 @@
+#include "mbed.h"
+#include "FreescaleIAP.h"
+ 
+ #if 0
+int main() {
+    int address = flash_size() - SECTOR_SIZE;           //Write in last sector
+    
+    int *data = (int*)address;
+    char *data_str = (char*)address;
+    printf("Starting\r\n"); 
+    erase_sector(address);
+    int numbers[10] = {0, 1, 10, 100, 1000, 10000, 1000000, 10000000, 100000000, 1000000000};
+    char *ip = "a12b3c4d5";
+    //program_flash(address, (char*)&numbers, 40);        //10 integers of 4 bytes each: 40 bytes length
+    program_flash(address, ip, strlen(ip));                     //15 chars of 4 bytes each: 60 bytes length
+    printf("Resulting flash: \r\n");
+    //for (int i = 0; i<10; i++)
+        //printf("%d\r\n", data[i]);
+    //
+    printf("%s\r\n", data_str);
+    printf("Done\r\n\n");
+        
+ 
+    while (true) {
+    }
+}
+#endif
\ No newline at end of file