00001 #include <iostream>
00002 #include "spisrc.h"
00003 #include "avrerror.h"
00004
00005 using namespace std;
00006
00007 SpiSource::SpiSource( const char* fileName,
00008 Net& ssNet,
00009 Net& sclkNet,
00010 Net& mosiNet
00011 ) throw():
00012 _ss(),
00013 _sclk(),
00014 _mosi(),
00015 _spiFile(fileName)
00016 {
00017 _ss.outState = Pin::HIGH;
00018 ssNet.Add(&_ss);
00019
00020 _sclk.outState = Pin::HIGH;
00021 sclkNet.Add(&_sclk);
00022
00023 _mosi.outState = Pin::HIGH;
00024 mosiNet.Add(&_mosi);
00025
00026 if(!_spiFile)
00027 avr_error("Cannot open SPI Source input file '%s'", fileName);
00028 }
00029
00030 static char* readNextLine(std::ifstream& f,char* buffer,unsigned len,SystemClockOffset *timeToNextStepIn_ns){
00031 *timeToNextStepIn_ns = 100000;
00032 for(unsigned i=0;i<2;++i){
00033 while(f.getline(buffer, len)){
00034 if(buffer[0] == '#') continue;
00035 return buffer;
00036 }
00037 *timeToNextStepIn_ns = 1000000;
00038 f.clear();
00039 f.seekg (0, ios::beg);
00040 }
00041 return 0;
00042 }
00043
00044 int SpiSource::Step(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns){
00045 if(!_spiFile) return 0;
00046
00047 char lineBuffer[1024];
00048
00049 if(!readNextLine(_spiFile,lineBuffer,sizeof(lineBuffer),timeToNextStepIn_ns)){
00050 _spiFile.close();
00051 return 0;
00052 }
00053
00054 char* p = lineBuffer;
00055 unsigned long ss = strtoul(p, &p, 0);
00056 unsigned long sclk = strtoul(p, &p, 0);
00057 unsigned long output = strtoul(p, &p, 0);
00058
00059 _ss = (ss)?'H':'L';
00060 _sclk = (sclk)?'H':'L';
00061 _mosi = (output)?'H':'L';
00062 return 0;
00063 }
00064