summaryrefslogtreecommitdiffstats
path: root/src/progs/direct/base/direct_18.cpp
blob: d512d3220c99792b5f4ddaf7e93aae5dc937b0a1 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/***************************************************************************
 *   Copyright (C) 2003-2005 Alain Gibaud <alain.gibaud@free.fr>           *
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "direct_18.h"

// Commands specific to 18F:
// xhh = send a 8 bits constant (hh must be a 2 digits hex constant)
// Xhhhh =  send a 16 bits constant (hhhh must be a 4 digits hex constant)
bool Direct::P18::pulse(const char *&cmd, BitValue value, BitValue &res)
{
  switch (*cmd) {
    case 'x': {
      uint n;
      sscanf(++cmd, "%02X", &n);
      ++cmd;
      log(Log::DebugLevel::Max, "SEND 1 byte constant : " + toHexLabel(n, 2));
      send_bits(n, 8);
      break;
    }
    case 'X': {
      uint n;
      sscanf(++cmd,"%04X", &n);
      cmd += 3;
      log(Log::DebugLevel::Max, "SEND 2 bytes constant : " + toHexLabel(n, 4));
      send_bits(n, 16);
      break;
    }
    default: return Pic8DeviceSpecific::pulse(cmd, value, res);
  }
  return true;
}

void Direct::P18::send_word(BitValue d)
{
    hardware().setWrite();
    for (uint x = 0; x<16; x++) {
        hardware().setPin(Clock, High);
        if ( d.bit(0) ) hardware().setPin(DataOut, High);
        else hardware().setPin(DataOut, Low);
        Port::usleep(_clockDelay+5);
        hardware().setPin(Clock, Low);
        Port::usleep(_clockDelay+3);
        d >>= 1;                    // Move the data over 1 bit
    }
    hardware().setPin(DataOut, High);
    hardware().setRead();
}

BitValue Direct::P18::get_word()
{
    hardware().setRead();
    BitValue ind = 0;
    send_cmd(9);
    hardware().setPin(DataOut, High);
    for (uint x = 0; x<16; x++) {
        hardware().setPin(Clock, High);
        Port::usleep(_clockDelay+5);
        if ( x>7 && hardware().readBit() ) ind |= (1 << x-8);
        hardware().setPin(Clock, Low);
        Port::usleep(_clockDelay+3);
    }
    send_cmd(9);
    hardware().setPin(DataOut, High);
    for (uint x = 0; x<16; x++) {
        hardware().setPin(Clock, High);
        Port::usleep(_clockDelay+5);
        if ( x>7 && hardware().readBit() ) ind |= (1 << x);
        hardware().setPin(Clock, Low);
        Port::usleep(_clockDelay+3);
    }
    return ind;
}

BitValue Direct::P18::get_byte()
{
    hardware().setRead();
    BitValue ind = 0;
    send_cmd(2);
    hardware().setPin(DataOut, High);
    for (uint x = 0; x<16; x++) {
        hardware().setPin(Clock, High);
        Port::usleep(_clockDelay+5);
        if ( x>7 && hardware().readBit() ) ind |= (1 << x-8);
        hardware().setPin(Clock, Low);
        Port::usleep(_clockDelay+3);
    }
    return ind;
}

void Direct::P18::send_bits(BitValue d, uint nbb)
{
    hardware().setWrite();
    for (int x = nbb; x; --x) {
        hardware().setPin(Clock, High);
        if ( d.bit(0) ) hardware().setPin(DataOut, High);
        else hardware().setPin(DataOut, Low);
        Port::usleep(_clockDelay+5);
        hardware().setPin(Clock, Low);
        Port::usleep(_clockDelay+3);
        d >>= 1;  // Move the data over 1 bit
    }
    hardware().setPin(DataOut, High);
    hardware().setRead();
}