File: spisrc.cpp

package info (click to toggle)
simulavr 1.0.0%2Bgit20160221.e53413b-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,740 kB
  • sloc: cpp: 35,491; python: 6,995; ansic: 3,567; makefile: 1,075; sh: 653; asm: 414; tcl: 320; javascript: 32
file content (64 lines) | stat: -rw-r--r-- 1,736 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include "spisrc.h"
#include "avrerror.h"

using namespace std;

SpiSource::SpiSource(   const char* fileName,
                        Net&        ssNet,
                        Net&        sclkNet,
                        Net&        mosiNet
                        ) throw():
        _ss(),
        _sclk(),
        _mosi(),
        _spiFile(fileName)
        {
        _ss.outState = Pin::HIGH;
        ssNet.Add(&_ss);

        _sclk.outState = Pin::HIGH;
        sclkNet.Add(&_sclk);

        _mosi.outState = Pin::HIGH;
        mosiNet.Add(&_mosi);

        if(!_spiFile)
            avr_error("Cannot open SPI Source input file '%s'", fileName);
    }

static char*    readNextLine(std::ifstream& f,char* buffer,unsigned len,SystemClockOffset *timeToNextStepIn_ns){
    *timeToNextStepIn_ns    = 100000;   // Once every 100 microseconds
    for(unsigned i=0;i<2;++i){
        while(f.getline(buffer, len)){
            if(buffer[0] == '#') continue;
            return buffer;
            }
        *timeToNextStepIn_ns    = 1000000;  // Once every 100 microseconds
        f.clear();
        f.seekg (0, ios::beg);
        }
    return 0;
    }

int SpiSource::Step(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns){
    if(!_spiFile) return 0;

    char    lineBuffer[1024];

    if(!readNextLine(_spiFile,lineBuffer,sizeof(lineBuffer),timeToNextStepIn_ns)){
        _spiFile.close();
        return 0;
        }

    char*   p   = lineBuffer;
    unsigned long   ss      = strtoul(p, &p, 0);
    unsigned long   sclk    = strtoul(p, &p, 0);
    unsigned long   output  = strtoul(p, &p, 0);

    _ss = (ss)?'H':'L';
    _sclk   = (sclk)?'H':'L';
    _mosi   = (output)?'H':'L';
    return 0;
    }