libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
mcp3208.h
Go to the documentation of this file.
1 #ifndef LIBPROPELLER_MCP3208_H_
2 #define LIBPROPELLER_MCP3208_H_
3 
4 #include <propeller.h>
5 
6 extern char _load_start_mcp3208_cog[];
7 
27 class MCP3208 {
28 public:
29 
30 
43  void Start(const int dataPin, const int clockPin,
44  const int selectPin, const int mode = 0xFF,
45  const int dacAPin = -1, const int dacBPin = -1) {
46 
47  if (dacAPin == -1 && dacBPin == -1) {
48  //No DACs
49  Startx(dataPin, clockPin, selectPin, mode, 0);
50  } else if (dacAPin != -1 && dacBPin == -1) {
51  //One DAC
52  Startx(dataPin, clockPin, selectPin, mode, (dacAPin & 0x1f) | 0x80);
53  } else {
54  //Two DACs
55  Startx(dataPin, clockPin, selectPin, mode,
56  (((dacBPin & 0x1f) | 0x80) << 8) + ((dacAPin & 0x1f) | 0x80));
57  }
58  }
59 
62  void Stop(void) {
63  if (Cog > 0) {
64  cogstop(Cog - 1);
65  Cog = 0;
66  }
67  }
68 
74  int In(const int Channel) {
75 
76  // Get pull the 16 bit word out of the 32 byte word.
77  int result = Ins[Channel / 2];
78 
79  // If index is odd then pull the upper word, if even pull the lower word.
80  result = Channel & 0x1 ? result >> 16 : result;
81 
82  // Make sure that we get just what we are interested in.
83  return result & 0xFFFF;
84  }
85 
93  int Average(const int Channel, const int N) {
94  //TODO(SRLM): What is C and Count?
95  int sampleTotal = 0;
96  int C = Count;
97  for (int i = 0; i < N; i++) {
98  while (C == Count) {
99  }
100  sampleTotal += In(Channel);
101  C++;
102 
103  }
104 
105  return sampleTotal / N;
106  }
107 
115  void Out(const short aOutput, const short bOutput = -1) {
116  Dacx = aOutput << 16;
117  Dacy = bOutput << 16;
118  }
119 private:
120  int Cog;
121 
122  //Warning: do not rearrange these variables. The assembly relies on the order.
123  int volatile Ins[4];
124  int volatile Count;
125  int volatile Dacx, Dacy;
126 
127  void Startx(const int dataPin, const int clockPin,
128  const int selectPin, const int mode, const int Dacmode) {
129 
130  volatile void * asm_reference = NULL;
131  __asm__ volatile ("mov %[asm_reference], #MCP3208_Entry\n\t"
132  : [asm_reference] "+r" (asm_reference));
133 
134  Stop();
135 
136  Ins[0] = dataPin;
137  Ins[1] = clockPin;
138  Ins[2] = selectPin;
139  Ins[3] = mode;
140 
141  Count = Dacmode;
142 
143  Cog = cognew(_load_start_mcp3208_cog, Ins) + 1;
144  }
145 };
146 
147 #endif // LIBPROPELLER_MCP3208_H_