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-10
Revision:
2:f9e1bcb3409a
Parent:
1:072a0ab47d9c
Child:
3:f84802422619

File content as of revision 2:f9e1bcb3409a:

/*
 * 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";
const char* NI_MSG = "DDSv2: Not initialized";

SerialDriver screen(USBTX, USBRX);
SerialDriver uart(D1, D0);
Thread *ser_thread_ptr;

//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

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);
    
    LedG = 1;
    
    uart.baud(SERIAL_BAUDRATE);
    
    while(1){
        LedG = 0;
        successful = false;
        
        if (uart.isRxBufferEmpty()){
            Thread::wait(10);
            continue;
            }
            
        Thread::wait(10);
        n = uart.read(rx_buffer, 255, false);
        
        //******************** 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]);
            }
        }
        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);
    
    LedB = 1;
    
    eth.setName(DEVICE_NAME);
    status = eth.init(IP, MASK, GATEWAY);
    status = eth.connect();
    
    server.bind(ECHO_SERVER_PORT);
    server.listen(1);
    
    LedB = 0;

    while(1)
    {
        LedB = 0;
        n = 0;
        totalSize = 0;
        
        server.accept(client);
        
        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(10);
        }
        
        LedB = 1;
    
        if (totalSize < 1){
            client.close();
            continue;
        }
        
        if (ipData.decode(rx_buffer, totalSize) == 0){
            client.close();
            continue;
        }
        
        //******************** 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());
        ipData.encode(ipData.getCmd(), dds_device.getCmdAnswer(), dds_device.getCmdAnswerLen());
        
        client.send(ipData.getTxData(), ipData.getTxDataLen());
        client.close();
        
    }
}

int main() 
{
    screen.baud(9600);

    screen.putc(0x0A);
    screen.putc(0x0D);
    screen.putc(0x33);
    screen.putc(0x33);
    
    ser_thread_ptr = new Thread(&waitSerialData_thread);
    eth_thread_ptr = new Thread(&waitEthData_thread);
    
    LedR = 1;
    
    screen.putc(0x33);
    screen.putc(0x32);
    
    while(true){
        if (dds_device.init())
            break;
        LedR = !LedR;
        Thread::wait(250);
    }
    
    LedR = 0;
    
    screen.putc(0x33);
    screen.putc(0x31);
    
    LedR = 1;
    
    dds_device.defaultSettings();
    LedR = 0;

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

}

#endif