most functionality to splashdwon, find neutral and start mission. short timeouts still in code for testing, will adjust to go directly to sit_idle after splashdown

Dependencies:   mbed MODSERIAL FATFileSystem

LTC1298/ltc1298.cpp

Committer:
danstrider
Date:
2017-10-23
Revision:
10:085ab7328054
Parent:
9:d5fcdcb3c89d
Child:
51:c5c40272ecc3

File content as of revision 10:085ab7328054:

#include "ltc1298.hpp"

SpiADC::SpiADC(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName led) :
    _spi(mosi, miso, sclk), // mosi, miso, sclk
    adcLed(led), // status led
    cs(csel) // chip select
{
}

void SpiADC::initialize() {
    //set up the spi bus and frequency
    _spi.format(13,0);
    _spi.frequency(1000000);
    
    //chip select high puts ltc1298 in standby
    cs = 1;
    
    //zero the initial ch0 and ch1 oversampled readings    
    ch0_filt = 0;
    ch1_filt = 0;

    //led on to say hello
    adcLed = 1;
}

// start an interupt driven trigger of the external ADC
void SpiADC::start() {
    interval.attach_us(this, &SpiADC::update, 100);  //this should be a 10 kHz sample rate
}

// stop the interupt driven trigger
void SpiADC::stop() {
    interval.detach();
}

void SpiADC::update() {
    //flash the LED
    adcLed = !adcLed;

    //chip select low starts data conversion
    cs = 0;
    
    //the next thing is the input data word
    //it is 4 bits and looks like this
    // | start | single/diff | odd/sign | MSB first/LSB first |
    // if you want single ended on channel 0 MSB first then input 0xD
    // if you want single ended on channel 1 MSB first then input 0xF

    // get channel 0
    unsigned int byte = _spi.write(0xD);
    //send a dummy byte to receive the data
    unsigned int byte1 = _spi.write(0x0);
    ch0_raw = byte1;
    ch0_filt  += (ch0_raw - ch0_filt)/CH0OVERSAMPLE;

    cs = 1;
    cs = 0;

    byte = _spi.write(0xF);
    //send a dummy byte to receive the data
    byte1 = _spi.write(0x0);
    ch1_raw = byte1;
    ch1_filt += (ch1_raw - ch1_filt)/CH1OVERSAMPLE;

    //switch chip select back to high
    cs = 1;
    return ;
}

int SpiADC::readCh0() {
    return ch0_filt;
}

int SpiADC::readCh1() {
    return ch1_filt;
}