-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAFE4400.cpp
More file actions
263 lines (203 loc) · 6.7 KB
/
AFE4400.cpp
File metadata and controls
263 lines (203 loc) · 6.7 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Arduino Library for Texas Instruments AFE4400 - Pulse-Oximetry Analog Front End
go to http://www.ti.com/product/afe4400 for datasheet
See https://github.com/mogar/AFE4400 for more info
The MIT License (MIT)
Copyright (c) 2014 Morgan Redfield
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Arduino.h>
#include <AFE4400.h>
#include <SPI.h>
AFE4400::AFE4400() {
debugSerial = 0;
nAFE_pwdn = 3;
AFE_ADC_rdy = 2;
nAFE_rst = 4;
}
void AFE4400::sw_reset() {
// write to control0 reg bit 3 1
// soft reset
SPIWriteBit(CONTROL0, 3, true);
// wait for bit 3 of control0 to be 0
delay(10);
}
//
// begin()
// Initial SPI setup, soft reset of device
//
void AFE4400::begin(int ss) {
chipSelectPin = ss;
// set up control pins
pinMode(nAFE_pwdn, OUTPUT);
pinMode(AFE_ADC_rdy, OUTPUT);
pinMode(nAFE_rst, OUTPUT);
pinMode(chipSelectPin, OUTPUT);
// enable control pins
digitalWrite(nAFE_pwdn, HIGH);
digitalWrite(nAFE_rst, HIGH);
// set up SPI
SPI.begin();
SPI.setDataMode(SPI_MODE0); //CPHA = CPOL = 0 MODE = 0
delay(1000);
sw_reset();
if (debugSerial) {Serial.println("Soft Reset\n");}
}
void AFE4400::enableDebugSerial() {
debugSerial = 1;
}
void AFE4400::disableDebugSerial() {
debugSerial = 0;
}
void AFE4400::setLEDCurrent(uint32_t led1_current, uint32_t led2_current) {
// read the reg
uint32_t current_val = SPIReadReg(LEDCNTRL);
// set led 1 value
led1_current = led1_current & 0xFF;
current_val = current_val & ~(0xFF << 8);
current_val = current_val | (led1_current << 8);
// set led 2 value
led2_current = led2_current & 0xFF;
current_val = current_val & ~(0xFF);
current_val = current_val | (led2_current);
// write reg
SPIWriteReg(LEDCNTRL, current_val);
}
void AFE4400::setDefaultTiming() {
// set pulse repetition frequency to 500Hz, duty cycle to 25%
// setup the timer module to default values
writeTimingData(LED2STC, 6000);
writeTimingData(LED2ENDC, 7999);
writeTimingData(LED2LEDSTC, 6000);
writeTimingData(LED2LEDENDC, 7998);
writeTimingData(ALED2STC, 0);
writeTimingData(ALED2ENDC, 1998);
writeTimingData(LED1STC, 2000);
writeTimingData(LED1ENDC, 3998);
writeTimingData(LED1LEDSTC, 2000);
writeTimingData(LED1LEDENDC, 3999);
writeTimingData(ALED1STC, 4000);
writeTimingData(ALED1ENDC, 5998);
writeTimingData(LED2CONVST, 2);
writeTimingData(LED2CONVEND, 1999);
writeTimingData(ALED2CONVST, 2002);
writeTimingData(ALED2CONVEND, 3999);
writeTimingData(LED1CONVST, 4002);
writeTimingData(LED1CONVEND, 5999);
writeTimingData(ALED1CONVST, 6002);
writeTimingData(ALED1CONVEND, 6002);
writeTimingData(ALED1CONVEND, 7999);
writeTimingData(ADCRSTSTCT0, 0);
writeTimingData(ADCRSTENDCT0, 2);
writeTimingData(ADCRSTSTCT1, 2000);
writeTimingData(ADCRSTENDCT1, 2002);
writeTimingData(ADCRSTSTCT2, 4000);
writeTimingData(ADCRSTENDCT2, 4002);
writeTimingData(ADCRSTSTCT3, 6000);
writeTimingData(ADCRSTENDCT3, 6002);
writeTimingData(PRPCOUNT, 7999);
}
//
// beginMeasure()
// turn on Measurement mode - required after reset
// be sure to set LED current before calling
// be sure to set timing parameters before calling (e.g. with setDefaultTiming)
//
void AFE4400::beginMeasure() {
// setup the LED driver (bit 11 of CONTROL2)
SPIWriteBit(CONTROL2, 11, false); // set it for H-Bridge mode
// turn on LED current (bit 17 of LEDCNTRL)
SPIWriteBit(LEDCNTRL, 17, true);
// enable the timer module (bit D8 of CONTROL1)
SPIWriteBit(CONTROL1, 8, true);
if (debugSerial) {Serial.print("Now beginning pulse and oximeter readings");}
// TODO: setup a timer or an interrupt to do SPI periodically
}
int AFE4400::readPulseData(){
return pulse;
}
int AFE4400::readOxData(){
return oximetry;
}
// Basic SPI routines to simplify code
// read and write one register
void AFE4400::SPIWriteBit(byte regAddress, uint8_t bit, bool bit_high) {
// read the reg
uint32_t current_val = SPIReadReg(regAddress);
// check to see if we need to change the bit
if (bit_high & !(current_val & 1 << bit)) {
// set bit correct in reg
current_val = current_val | (bit_high << bit);
// write reg
SPIWriteReg(regAddress, current_val);
} else if (!bit_high & (current_val & 1 << bit)) {
// set bit correct in reg
current_val = current_val & ~(bit_high << bit);
// write reg
SPIWriteReg(regAddress, current_val);
}
}
uint32_t AFE4400::SPIReadReg(byte regAddress){
byte temp_byte = 0;
uint32_t reg_value = 0;
// enable reading from registers
SPIEnableRead();
digitalWrite(chipSelectPin, LOW);
// set address
SPI.transfer(regAddress);
// get first byte
temp_byte = SPI.transfer(0x00);
reg_value = temp_byte << 16;
// get second byte
temp_byte = SPI.transfer(0x00);
reg_value = temp_byte << 8;
// get last byte
temp_byte = SPI.transfer(0x00);
digitalWrite(chipSelectPin, HIGH);
// disable reading from registers
SPIDisableRead();
return reg_value;
}
void AFE4400::SPIEnableRead() {
SPIWriteReg(CONTROL0, 1);
delay(1);
}
void AFE4400::SPIDisableRead() {
SPIWriteReg(CONTROL0, 0);
delay(1);
}
void AFE4400::SPIWriteReg(byte regAddress, uint32_t regValue){
byte temp_byte = 0;
digitalWrite(chipSelectPin, LOW);
SPI.transfer(regAddress); // write instruction
temp_byte = regValue >> 16;
SPI.transfer(regValue);
temp_byte = regValue >> 8;
SPI.transfer(regValue);
temp_byte = regValue;
SPI.transfer(regValue);
digitalWrite(chipSelectPin, HIGH);
}
void AFE4400::writeTimingData(byte regAddress, uint16_t timing_value) {
if (regAddress < LED2STC || regAddress > PRPCOUNT) {
if (debugSerial) {Serial.print("Error: timing address out of range"); }
return;
}
uint32_t reg_val = timing_value;
// write the value
SPIWriteReg(regAddress, reg_val);
}