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

main.cpp

Committer:
miguelcordero191
Date:
2015-02-05
Revision:
1:072a0ab47d9c
Parent:
0:c9bdd57867af
Child:
2:f9e1bcb3409a

File content as of revision 1:072a0ab47d9c:

/*
 * Author: MIGUEL URCO
 * Date: 10/11/2014
 * Notes: Checks the Ethernet cable connection
*/
#if 1

#include "mbed.h"
#include "rtos.h"
#include "EthernetInterface.h"
#include "JroDDS.h"
#include "JroIpdata.h"
#include "SerialDriver.h"

//Device commands
#define CMD_RESET       0X01
#define CMD_ENABLE      0x02
#define CMD_CHANGE_IP   0x03

#define BUFFER_SIZE     256
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);
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;

Thread *eth_thread_ptr;

//DDS
SPI spi_device(D11, D12, D13);

DigitalOut    dds_mreset(D4);
DigitalOut    dds_outramp(D5);
DigitalOut    dds_sp_mode(D6);
DigitalOut    dds_cs(D7);
DigitalOut    dds_io_reset(D9);
DigitalInOut  dds_updclk(D10);

DDS dds_device(&spi_device, &dds_mreset, &dds_outramp, &dds_sp_mode, &dds_cs, &dds_io_reset, &dds_updclk);

//LEDS
DigitalOut LedR(LED1);          
DigitalOut LedG(LED2);          
DigitalOut LedB(LED3);  

void waitSerialData_thread(void const *args){
    
    int n;
    bool successful;
    
    //Thread::signal_wait(0x1);
    
    //screen.putc(0x34);
    //screen.putc(0x30);
    
    //uart.baud(SERIAL_BAUDRATE);
    
    //jroUart.Init(SERIAL_BAUDRATE);
    
    while(1){
        //__screen.printf("\r\nWaiting serial data...\r\n");
        successful = false;
        
        if (uart.isRxBufferEmpty()){
            Thread::wait(10);
            continue;
            }
            
        Thread::wait(100);
        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)   
            if (dds_device.setAllDevice(rx_buffer) == 1)
                successful = true;
        
        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]);
            }
        }
        
    }
    
}

void waitEthData_thread(void const *args){
    
    TCPSocketConnection client;
    int status;
    int n, totalSize=0;
    
    //Thread::signal_wait(0x1);
    
    LedR = 0;
    
    //screen.putc(0x35);
    //screen.putc(0x30);
    eth.setName(DEVICE_NAME);
    status = eth.init(IP, MASK, GATEWAY);
    status = eth.connect();
    
    server.bind(ECHO_SERVER_PORT);
    server.listen(1);
    
    LedR = 1;

    //screen.putc(0x35);
    //screen.putc(0x31);
    
    //__screen.printf("\r\nEth thread initialized...\r\n");

    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());
        
        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 (totalSize < 1)
            continue;
        
        if (ipData.decode(rx_buffer, totalSize) == 0)
            continue;
        
        if (ipData.getCmd() == CMD_CHANGE_IP){
            //changing ip and reseting device
        }
        
        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());
        client.close();

        //screen.putc(0x35);
        //screen.putc(0x32);
        
    }
}

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);
    
    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);
    
    screen.putc(0x33);
    screen.putc(0x31);
    //__screen.printf("\r\nInitializing DDS...\r\n");
    
    while(true){
        if (dds_device.init())
            break;
            
        Thread::wait(250);
    }
    
    LedG = 1;
    
    screen.putc(0x33);
    screen.putc(0x32);
    
    LedB = 0;
    
    //__screen.printf("\r\nSetting defaults values to DDS...\r\n");
    dds_device.defaultSettings();
    LedB = 1;

    screen.putc(0x33);
    screen.putc(0x33);
    
    Thread::wait(5000);
    //int c=0;
    while(true){
        Thread::wait(1000);
        //screen.putc(0x2D + c);
        //c = (c+1) % 3;
    }

}

#endif