simulavr  1.1.0
spisrc.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "spisrc.h"
3 #include "avrerror.h"
4 
5 using namespace std;
6 
7 SpiSource::SpiSource( const char* fileName,
8  Net& ssNet,
9  Net& sclkNet,
10  Net& mosiNet
11  ) throw():
12  _ss(),
13  _sclk(),
14  _mosi(),
15  _spiFile(fileName)
16  {
17  _ss.outState = Pin::HIGH;
18  ssNet.Add(&_ss);
19 
20  _sclk.outState = Pin::HIGH;
21  sclkNet.Add(&_sclk);
22 
23  _mosi.outState = Pin::HIGH;
24  mosiNet.Add(&_mosi);
25 
26  if(!_spiFile)
27  avr_error("Cannot open SPI Source input file '%s'", fileName);
28  }
29 
30 static char* readNextLine(std::ifstream& f,char* buffer,unsigned len,SystemClockOffset *timeToNextStepIn_ns){
31  *timeToNextStepIn_ns = 100000; // Once every 100 microseconds
32  for(unsigned i=0;i<2;++i){
33  while(f.getline(buffer, len)){
34  if(buffer[0] == '#') continue;
35  return buffer;
36  }
37  *timeToNextStepIn_ns = 1000000; // Once every 100 microseconds
38  f.clear();
39  f.seekg (0, ios::beg);
40  }
41  return 0;
42  }
43 
44 int SpiSource::Step(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns){
45  if(!_spiFile) return 0;
46 
47  char lineBuffer[1024];
48 
49  if(!readNextLine(_spiFile,lineBuffer,sizeof(lineBuffer),timeToNextStepIn_ns)){
50  _spiFile.close();
51  return 0;
52  }
53 
54  char* p = lineBuffer;
55  unsigned long ss = strtoul(p, &p, 0);
56  unsigned long sclk = strtoul(p, &p, 0);
57  unsigned long output = strtoul(p, &p, 0);
58 
59  _ss = (ss)?'H':'L';
60  _sclk = (sclk)?'H':'L';
61  _mosi = (output)?'H':'L';
62  return 0;
63  }
64 
STL namespace.
#define avr_error(...)
Definition: avrerror.h:135
long long SystemClockOffset
static char * readNextLine(std::ifstream &f, char *buffer, unsigned len, SystemClockOffset *timeToNextStepIn_ns)
Definition: spisrc.cpp:30
int Step(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns=0)
Return nonzero if a breakpoint was hit.
Definition: spisrc.cpp:44
Definition: pin.h:117
SpiSource(const char *fileName, Net &ssNet, Net &sclkNet, Net &mosiNet)
Definition: spisrc.cpp:7
Connect Pins to each other and transfers a output change from a pin to input values for all pins...
Definition: net.h:34