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:
0:c9bdd57867af
Child:
1:072a0ab47d9c

File content as of revision 0:c9bdd57867af:

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

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

#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
SerialDriver screen(USBTX, USBRX);
SerialDriver uart(D1, D0);
//JroSerial jroUart(D1,D0, &screen);
Thread *ser_thread_ptr;

//ETHERNET
IpData ipData(tx_buffer);
EthernetInterface eth;
TCPSocketServer server;
JroEthernet jroEth(&eth);

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);  

//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);
    
    screen.putc(0x34);
    screen.putc(0x30);
    
    //uart.baud(SERIAL_BAUDRATE);
    
    //jroUart.Init(SERIAL_BAUDRATE);
    
    while(1){
        //__screen.printf("\r\nWaiting serial data...\r\n");
        
        if (uart.isRxBufferEmpty()){
            Thread::wait(10);
            continue;
            }
            
        Thread::wait(100);
        n = uart.read(rx_buffer, 40, 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 (dds_device.setAllDevice(rx_buffer, &screen) == 1){
            screen.putc(0x34);
            screen.putc(0x34);
            //jroSerial.writeData("Done");
        }
        else
        {
            //jroSerial.writeData("Error");
            screen.putc(0x34);
            screen.putc(0x35);
        }
        
    }
    
}

void waitEthData_thread(void const *args){
    
    int n;
    
    Thread::signal_wait(0x1);
    
    LedR = 0;
    
    screen.putc(0x35);
    screen.putc(0x30);
    
    jroEth.init(ip, mask, gateway);
    jroEth.openTCPServer(&server, ECHO_SERVER_PORT);
    
    LedR = 1;

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

    while(1) {
        LedB = 0;
        //screen.printf("\r\n ********************** \r\n"); 
        //__screen.printf("\r\nWait for new connection...\r\n");
        n = jroEth.read(rx_buffer);
        
        screen.putc(0x35);
        screen.putc(0x32);
    
        if (n < 1)
            continue;
        
        if (ipData.decode(rx_buffer, n) == 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());
        
        jroEth.sendResponse(ipData.getTxData(), ipData.getTxDataLen());
        jroEth.closeClient();
        
    }
}

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(&screen);
    LedB = 1;

    screen.putc(0x33);
    screen.putc(0x33);
    
    while(1) {
       Thread::wait(250);
        
    }

}

#endif