Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

Gas detector, stimulus and sampling.

// Built with BFTC Rev. 1.0.0, gio 15 gennaio 2015 08:33:41

/*
    Copyright 2014 Digital Technology Art SRL
    This file is part of Blackfin Toolchain (BFTC) project.

    BFTC is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BFTC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Name-Program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Valid only for channel 0

#include "main.h"
#include "diskio.h"

// Output defines
#define BLANK   0
#define VIDEO   1
#define DISK    2

#define micros() *pTIMER0_COUNTER

#define ST st = *pTIMER0_COUNTER
#define ET et = *pTIMER0_COUNTER

#define MAXCOUNT 0xFFFFFFFF

// Output swing 0-5V
#define BIT 12              // Possible selection 12 or 16

#if(BIT == 12)
    #define DAC_ 0x10       // x AD5624, 12 bit DAC
    #define By_mV(mv) (4095L * ((unsigned) mv)) / 5000L
#endif

#if(BIT == 16)
    #define DAC_ 0x01       // x AD5664, 16 bit DAC
    #define By_mV(mv) (65535L * ((unsigned) mv)) / 5000L
#endif

#define k           16              // !
#define k_2         k / 2           // !

// Instrument constants
#define C   0.000002    // Capacitance
#define RL  10000.0      // Resistance load (ohm)
#define U   (RL * C)    // Time constant
#define Ris 65536.0     // A/D resolution
#define t   0.000005    // ?? 0.00002

typedef unsigned short int U16;
typedef unsigned int U32;
typedef float real;
    
FATFS FatFs;            // Work area (file system object) for logical drive

U16 ad[4];   // 16bit ADCs output
U16 adTmp0, adTmp1, adTmp2, adTmp3, loopCounter = 0;
unsigned adClk = 44;

U16 lowVoltage[4]  = { By_mV(0), By_mV(0), By_mV(0), By_mV(0) };                // mV Heater swing
U16 highVoltage[4] = { By_mV(5000), By_mV(5000), By_mV(5000), By_mV(5000) };    // mV ""
U32 numSamples = 1000, absCounter = 0, cycleDelay = 50000;
char output = VIDEO;
char channel = 0;

void diskCB(BYTE drv, DWORD sector, BYTE count)
{
    if((sector % 1024) == 0)
        printf("Drive: %d, Sector: %8d, Count: %3d\n", drv, sector, count);
}

int ADsample()
{
    if(adClk)
    {
        adClk--;
        PPF_TGL = AD_CLK;
        if(adClk <= 31)
        {
            // On rising edge sample data
            if(adClk & 1)
            {
                if(PPF & AD_IN0) adTmp0 |= 1;
                if(PPF & AD_IN1) adTmp1 |= 1;
                if(PPF & AD_IN2) adTmp2 |= 1;
                if(PPF & AD_IN3) adTmp3 |= 1;
                
            }
            else
            {   // On falling edge shift data but not the last!
                if(adClk)
                {
                    adTmp0 <<= 1;
                    adTmp1 <<= 1;
                    adTmp2 <<= 1;
                    adTmp3 <<= 1;
                }
            }
        }
        return 0;
    }
    PPF_SET = AD_CS;
    adClk = 44;
    ad[0] = adTmp0;
    ad[1] = adTmp1;
    ad[2] = adTmp2;
    ad[3] = adTmp3;
    adTmp0 = adTmp1 = adTmp2 = adTmp3 = 0;
    return 1;
}

void DAC(unsigned channel, unsigned value, bool update)
{
    unsigned    n, mk = 0x800000;
#if(BIT == 12)
    value <<= 4;
#endif
    // Prepare the output data
    value |= (channel << 16);
    if(update)
        value |= 0x100000;  // Load & update
    PPG_CLR = DA_CS;        // Set DAC CS active
    for(n = 0; n < 24; n++)
    {
        PPG_SET = DA_CLK;
        if(value & mk)  PPG_SET = DA_DATA;
        else PPG_CLR = DA_DATA;
        PPG_CLR = DA_CLK;
        mk >>= 1;
    }
    PPG_SET = DA_CS;        // Set DAC CS disabled
}

U16 analogRead(int chan)
{
    bool        done;
    unsigned    st, et, tx;
    PPF_CLR = AD_CS;
    // Get A/D data for all channels
    while(1)
    {
        ST;
        done = ADsample();
        if(done) return ad[chan];
        // 250 ns delay, this is the semi-period for a SPI clock of 2MHz
        do
        {
            ET;
            if(st > et) tx = MAXCOUNT - st + et;
            else tx = et - st;
        }while(tx < 25);    // 25
    }
}

int Menu()
{
    printf("CH [%d]\n", channel);
    printf("----- M E N U ---------\n");
    printf("1 - Single channel\n");
    printf("2 - Dual   channel\n");
    printf("4 - Quad   channel\n");
    printf("C - Channel\n");
    printf("F - SD Format\n");
    printf("N - Number of samples\n");
    printf("O - Output\n");
    printf("V - Voltage swing\n");
    printf("Y - Cycle delay\n");
    printf("\n> ");
    return (GetChar_Uart0());
}

#define FOR4X for(c = 0; c < 4; c++)
    
void SampleQuadChannel()
{
    unsigned    begin, tl[4], th[4], n = 0, i, ii, iii, c, state[4], et[8];
    real        F, Vcond[4], Vs;
    FRESULT     fr;    // FatFs return code
    DRESULT     dr;
    FIL         fil;   // File object
    char        buff[128];
    
    if(output == DISK)
    {
        sprintf(buff, "DQ_%04d.txt", absCounter++);
        dr = disk_initialize(0);
        if(dr == RES_OK)
        {
            // Register work area to the default drive
            fr = f_mount(&FatFs, "", 0);
            if(fr == RES_OK)
            {
                fr = f_open(&fil, buff, FA_CREATE_ALWAYS | FA_WRITE);
                if(fr) 
                {
                    printf("Can't open [%s]\n", buff);
                    return;
                }
            }
            else
            {
                printf("%s\n", disk_errString(dr));
                disk_ioctl(0, CTRL_POWER_OFF, 0);
                return;
            }
        }
    }
    
    printf("Started ...\n");
    FOR4X
        DAC(c, highVoltage[c], true);

    for(i = 0; i < numSamples; i++)
    {
        n++;
        
        FOR4X
        {
            Vcond[c] = 1;
            state[c] = 0;
        }
        begin = micros();
        do
        {
            analogRead(0);  // Read all channel
            //printf("%u %u\n", ad[chanA], ad[chanB]);
            FOR4X
            switch(state[c])
            {
                case 0: // Charging
                        ii = c << 1;
                        Vs = Vcond[c];
                        F = t / ((Ris / ((real) ad[c]) - 1) * U);
                        Vcond[c] = 5.0 * F + Vs * (1.0 - F);
                        if(Vcond[c] > 4.0)
                        {
                            et[ii] = micros();
                            if(begin > et[ii]) th[c] = MAXCOUNT - begin + et[ii];
                            else th[c] = et[ii] - begin;
                            th[c] /= 100;
                            state[c] = 1;
                        }
                        break;
                case 1: // Discharge
                        iii = ii + 1;
                        Vs = Vcond[c];
                        F = t / ((Ris / ((real) ad[c]) - 1) * U);
                        Vcond[c] = 0.001 * F + Vs * (1.0 - F);
                        if(Vcond[c] < 1.0)
                        {
                            et[iii] = micros();
                            if(et[ii] > et[iii]) tl[c] = MAXCOUNT - et[ii] + et[iii];
                            else tl[c] = et[iii] - et[ii];
                            tl[c] /= 100;
                            state[c] = 2;
                        }
                        break;
                case 2: // Idle state
                        break;
            }
        }while(state[0] < 2 || state[1] < 2 || state[2] < 2 || state[3] < 2);
        sprintf(buff, "%u, %u, %u, %u, %u, %u, %u, %u, %u\n", i, th[0], tl[0], th[1], tl[1], th[2], tl[2], th[3], tl[3]);
        switch(output)
        {
            case VIDEO: printf(buff);
                        break;
            case DISK:  f_puts(buff, &fil);
                        SendChar_Uart0('.');
                        break;
        }
        if(n == k_2)
        {
            FOR4X
                DAC(c, lowVoltage[c], true);
        }
        if(n == k)
        {
            FOR4X
                DAC(c, highVoltage[c], true);
            n = 0;
        }
    }
    
    if(output == DISK)
    {
        f_close(&fil);
        fr = f_mount(0, "", 0);
        disk_ioctl(0, CTRL_POWER_OFF, 0);
    }
    printf("End ...\n");
    // Turn off Vout
    FOR4X
        DAC(c, 0, true);
}

void SampleDualChannel(int chanA, int chanB)
{
    unsigned    t1, t2, t3, t4, t5, tl[2], th[2], n = 0, i, stateA, stateB;
    real        F, Vcond[2], Vs;
    U16         Vheater[2];
    FRESULT     fr;    // FatFs return code
    DRESULT     dr;
    FIL         fil;   // File object
    char        buff[128];
    bool        update;
    
    if(output == DISK)
    {
        sprintf(buff, "D%d%d_%04d.txt", chanA, chanB, absCounter++);
        dr = disk_initialize(0);
        if(dr == RES_OK)
        {
            // Register work area to the default drive
            fr = f_mount(&FatFs, "", 0);
            if(fr == RES_OK)
            {
                fr = f_open(&fil, buff, FA_CREATE_ALWAYS | FA_WRITE);
                if(fr) 
                {
                    printf("Can't open [%s]\n", buff);
                    return;
                }
            }
            else
            {
                printf("%s\n", disk_errString(dr));
                disk_ioctl(0, CTRL_POWER_OFF, 0);
                return;
            }
        }
    }
    
    printf("Started ...\n");
    update = true;
    Vheater[0] = highVoltage[chanA];
    Vheater[1] = highVoltage[chanB];
    for(i = 0; i < numSamples; i++)
    {
        if(update)
        {
            DAC(chanA, Vheater[0], false);
            DAC(chanB, Vheater[1], true);
            update = false;
        }
        n++;
        
        Vcond[0] = 1;
        Vcond[1] = 1;
        stateA = stateB = 0;
        t1 = micros();
        do
        {
            analogRead(0);  // Read all channel
            //printf("%u %u\n", ad[chanA], ad[chanB]);
            switch(stateA)
            {
                case 0: // Charging
                        Vs = Vcond[0];
                        F = t / ((Ris / ((real) ad[chanA]) - 1) * U);
                        Vcond[0] = 5.0 * F + Vs * (1.0 - F);
                        if(Vcond[0] > 4.0)
                        {
                            t2 = micros();
                            if(t1 > t2) th[0] = MAXCOUNT - t1 + t2;
                            else th[0] = t2 - t1;
                            th[0] /= 100;
                            stateA = 1;
                        }
                        break;
                case 1: // Discharge
                        Vs = Vcond[0];
                        F = t / ((Ris / ((real) ad[chanA]) - 1) * U);
                        Vcond[0] = 0.001 * F + Vs * (1.0 - F);
                        if(Vcond[0] < 1.0)
                        {
                            t3 = micros();
                            if(t2 > t3) tl[0] = MAXCOUNT - t2 + t3;
                            else tl[0] = t3 - t2;
                            tl[0] /= 100;
                            stateA = 2;
                        }
                        break;
                case 2: // Idle state
                        break;
            }
            switch(stateB)
            {
                case 0: // Charging
                        Vs = Vcond[1];
                        F = t / ((Ris / ((real) ad[chanB]) - 1) * U);
                        Vcond[1] = 5.0 * F + Vs * (1.0 - F);
                        if(Vcond[1] > 4.0)
                        {
                            t4 = micros();
                            if(t1 > t4) th[1] = MAXCOUNT - t1 + t4;
                            else th[1] = t4 - t1;
                            th[1] /= 100;
                            stateB = 1;
                        }
                        break;
                case 1: // Discharge
                        Vs = Vcond[1];
                        F = t / ((Ris / ((real) ad[chanB]) - 1) * U);
                        Vcond[1] = 0.001 * F + Vs * (1.0 - F);
                        if(Vcond[1] < 1.0)
                        {
                            t5 = micros();
                            if(t4 > t5) tl[1] = MAXCOUNT - t4 + t5;
                            else tl[1] = t5 - t4;
                            tl[1] /= 100;
                            stateB = 2;
                        }
                        break;
                case 2: // Idle state
                        break;
            }
        }while(stateA < 2 || stateB < 2);
        sprintf(buff, "%u, %u, %u, %u, %u\n", i, th[0], tl[0], th[1], tl[1]);
        switch(output)
        {
            case VIDEO: printf(buff);
                        break;
            case DISK:  f_puts(buff, &fil);
                        SendChar_Uart0('.');
                        break;
        }
        if(n == k_2)
        {
            Vheater[0] = lowVoltage[chanA];
            Vheater[1] = lowVoltage[chanB];
            update = true;
        }
        if(n == k)
        {
            Vheater[0] = highVoltage[chanA];
            Vheater[1] = highVoltage[chanB];
            n = 0;
            update = true;
        }
    }
    
    if(output == DISK)
    {
        f_close(&fil);
        fr = f_mount(0, "", 0);
        disk_ioctl(0, CTRL_POWER_OFF, 0);
    }
    printf("End ...\n");
    // Turn off Vout
    DAC(chanA, 0, true);
    DAC(chanB, 0, true);
}

void SampleSingleChannel(int chan)
{
    unsigned    t1, t2, t3, tl, th, n = 0, i;
    real        F, Vcond, Vs;
    U16         Vheater, Vp;
    FRESULT     fr;    // FatFs return code
    DRESULT     dr;
    FIL         fil;   // File object
    char        buff[128];
    
    if(output == DISK)
    {
        sprintf(buff, "D_%d_%04d.txt", chan, absCounter++);
        dr = disk_initialize(0);
        if(dr == RES_OK)
        {
            // Register work area to the default drive
            fr = f_mount(&FatFs, "", 0);
            if(fr == RES_OK)
            {
                fr = f_open(&fil, buff, FA_CREATE_ALWAYS | FA_WRITE);
                if(fr) 
                {
                    printf("Can't open [%s]\n", buff);
                    return;
                }
            }
            else
            {
                printf("%s\n", disk_errString(dr));
                disk_ioctl(0, CTRL_POWER_OFF, 0);
                return;
            }
        }
    }
    
    printf("Started ...\n");
    Vp = 0;
    Vheater = highVoltage[chan];
    for(i = 0; i < numSamples; i++)
    {
        if(Vp != Vheater) // Don't update if the previous value is the same
        {
            DAC(chan, Vheater, true);
            Vp = Vheater;
        }
        n++;
        t1 = micros();
        Vcond = 1;
        while(Vcond <= 4.0)
        {
            Vs = Vcond;
            F = t / ((Ris / ((real) analogRead(chan)) - 1) * U);
            Vcond = 5 * F + Vs * (1.0 - F);
            //printf("%f %u\n", Vcond, ad[chan]);
        }
        t2 = micros();
        if(t1 > t2) th = MAXCOUNT - t1 + t2;
        else th = t2 - t1;
        th /= 100;

        while(Vcond >= 1.0)
        {
            Vs = Vcond;
            F = t / ((Ris / ((real) analogRead(chan)) - 1) * U);
            Vcond = 0.001 * F + Vs * (1.0 - F);
            //printf("%f %u\n", Vcond, ad[chan]);
        }
        t3 = micros();
        if(t2 > t3) tl = MAXCOUNT - t2 + t3;
        else tl = t3 - t2;
        tl /= 100;
        
        // Display data
        sprintf(buff, "%u, %u, %u\n", i, th, tl);
        switch(output)
        {
            case VIDEO: printf(buff);
                        break;
            case DISK:  f_puts(buff, &fil);
                        SendChar_Uart0('.');
                        break;
        }
        if(n == k_2)
            Vheater = lowVoltage[chan];
        if(n == k)
        {
            Vheater = highVoltage[chan];
            n = 0;
        }
        DelayUs(cycleDelay);
    }
    
    if(output == DISK)
    {
        f_close(&fil);
        fr = f_mount(0, "", 0);
        disk_ioctl(0, CTRL_POWER_OFF, 0);
    }
    
    printf("End ...\n");
    // Turn off Vout
    DAC(chan, 0, true);
}

int GetInt(const char * label, int low, int high)
{
    int     ret;
    printf("\n%s ", label);
    _scanf("%d", &ret);
    if(ret < low) ret = low;
    if(ret > high) ret = high;
    printf("\n");
    return ret;
}

void DiskFormat()
{
    DRESULT dr;
    FRESULT fr; // FatFs return code
    dr = disk_initialize(0);
    if(dr == RES_OK)
    {
        // Register work area to the default drive
        fr = f_mount(&FatFs, "", 0);
        if(fr == RES_OK)
        {
            printf("FAT on SD ready\n");
            disk_setCallback(diskCB);
            fr = f_mkfs("", 0, 4096);
            disk_setCallback(0);
            printf("SD Format done!\n");
            
            fr = f_mount(0, "", 0);
            disk_ioctl(0, CTRL_POWER_OFF, 0);
            printf("FAT unmounted, SD power OFF\n");
        }
        else
        {
            printf("%s\n", disk_errString(dr));
            disk_ioctl(0, CTRL_POWER_OFF, 0);
        }
    }
}

int main(void)
{
    int         com;
    
    Set_PLL(16, 4);             // CORE: 25MHz * 16 = 400MHz, SCLK: 400MHz / 4 = 100MHz
    Set_Port();                 // Set the port according to project set
    Set_Uart0(115200);  // printf is redirected to UART0
    Set_TimerCounter(TIMER_0);
    Set_TimerEnable(TIMER_0, true);
    
    // Turn off Vout
    DAC(0, 0, false);
    DAC(1, 0, false);
    DAC(2, 0, false);
    DAC(3, 0, true);
    
        while(1)                        // Main loop
        {
        com = Menu();
        printf("\n");
        switch(com)
        {
            case '1':   SampleSingleChannel(channel);
                        break;
            case '2':   SampleDualChannel(GetInt("1st Channel [0:3] =", 0, 3), GetInt("2nd Channel [0:3] =", 0, 3));
                        break;
            case '4':   SampleQuadChannel();
                        break;
            case 'C':   channel = GetInt("Channel [0:3] =", 0, 3);
                        break;
            case 'F':   DiskFormat();
                        break;
            case 'N':   numSamples = GetInt("Samples [100:1000000] =", 100, 1000000);
                        break;
            case 'O':   output = GetInt("Output selection [0:BLANK, 1:VIDEO, 2:DISK] =", 0, 2);
                        break;
            case 'V':   lowVoltage[channel] = By_mV(GetInt("Low voltage [0:5000] =", 0, 5000));
                        highVoltage[channel] = By_mV(GetInt("High voltage [0:5000] =", 0, 5000));
                        break;
            case 'Y':   cycleDelay = GetInt("Cycle delay [1:500] mS =", 1, 500) * 1000;
                        break;
            default:    printf("\nUnknown command\n");
        }
        }
        return 0;
}

Generated on Tue Apr 7 20:07:44 2015 for BF592A Library by doxygen 1.3.1