-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathADS1256_Driver.cpp
187 lines (130 loc) · 3.74 KB
/
ADS1256_Driver.cpp
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
#include "ADS1256_Driver.h"
void ADS1256_Chip_Select(bool signal) {
bcm2835_gpio_write(ADS_CS_PIN, signal);
}
bool ADS1256_Data_Ready() {
return (bcm2835_gpio_lev(ADS_DRDY_PIN) == LOW);
}
int ADS1256_Poll_Data_Ready() {
bcm2835_delayMicroseconds(5);
for (int i = 0; i < DRDY_TIMEOUT; i++) {
if (ADS1256_Data_Ready()) {
return 1;
}
}
return 0;
}
void ADS1256_Send_Byte(uint8_t byte) {
bcm2835_delayMicroseconds(2);
bcm2835_spi_transfer(byte);
}
uint8_t ADS1256_Read_Byte() {
return bcm2835_spi_transfer(CMD_WAKEUP);
}
uint32_t ADS1256_Read_Word(bool is_RDATAC) {
uint32_t word;
ADS1256_Poll_Data_Ready();
ADS1256_Chip_Select(LOW);
if (!is_RDATAC) {
ADS1256_Send_Byte(CMD_RDATA);
bcm2835_delayMicroseconds(1000);
}
// Packing bytes into a 32 bit word, 24 bits are used
word = ((uint32_t)ADS1256_Read_Byte() << 16) & 0x00FF0000; // MSB
word |= ((uint32_t)ADS1256_Read_Byte() << 8); // MID BYTE
word |= ADS1256_Read_Byte(); // LSB
ADS1256_Chip_Select(HIGH);
// Check if the word is signed
if (word & 0x800000) {
// Extend the sign
word &= (0xFF000000);
}
return word;
}
void ADS1256_Reset() {
bcm2835_gpio_write(ADS_RST_PIN, 0);
bcm2835_delay(5);
bcm2835_gpio_write(ADS_RST_PIN, 1);
bcm2835_delay(5);
}
void ADS1256_Configure(ADS1256_GAIN gain, ADS1256_DRATE rate) {
ADS1256_Poll_Data_Ready();
uint8_t config[4] = { 0, 0, 0, 0 };
// Status Register | Auto Calibration, Analog Input Buffer Disabled
config[0] = 0x4;
// MUX Register | AIN 0 and AINCOM
config[1] = 0x8;
// ADCON Register | PGA Settings from gain variable
config[2] = gain;
// DRATE Register | Data rate from rate variable
config[3] = rate;
ADS1256_Chip_Select(LOW);
// Start writing from register 0 and continue writing 3 registers deep
ADS1256_Send_Byte(CMD_WREG);
ADS1256_Send_Byte(0x03);
for (int i = 0; i < 4; i++) {
ADS1256_Send_Byte(config[i]);
}
ADS1256_Chip_Select(HIGH);
bcm2835_delayMicroseconds(500);
}
void ADS1256_Send_Cmd(ADS1256_COMMANDS CMD) {
ADS1256_Chip_Select(LOW);
ADS1256_Send_Byte(CMD);
ADS1256_Chip_Select(HIGH);
}
void ADS1256_WREG(ADS1256_REGISTERS REG, uint8_t DATA) {
ADS1256_Chip_Select(LOW);
// Send Write Register command
ADS1256_Send_Byte(CMD_WREG | REG);
ADS1256_Send_Byte(0x00); // Single byte to write
// Data to be written to register
ADS1256_Send_Byte(DATA);
ADS1256_Chip_Select(HIGH);
}
uint8_t ADS1256_RREG(ADS1256_REGISTERS REG) {
uint8_t value = 0;
ADS1256_Poll_Data_Ready();
ADS1256_Chip_Select(LOW);
ADS1256_Send_Byte(CMD_RREG | REG); // 1-ST Control Byte: Read from REG
ADS1256_Send_Byte(0x00); // 2-ND Control Byte: Read from a single register
bcm2835_delayMicroseconds(30);
value = ADS1256_Read_Byte();
ADS1256_Chip_Select(HIGH);
return value;
}
void ADS1256_Enable_RDATAC() {
// Requires DRDY to be low to issue command
ADS1256_Poll_Data_Ready();
ADS1256_Send_Cmd(CMD_RDATAC);
bcm2835_delayMicroseconds(5);
}
void ADS1256_Set_Channel(uint8_t CHANNEL) {
ADS1256_WREG(REG_MUX, (CHANNEL << 4) | (1 << 3));
// Synchronize the new MUX register value
ADS1256_Send_Cmd(CMD_SYNC);
// Complete sync and wakeup
ADS1256_Send_Cmd(CMD_WAKEUP);
}
void ADS1256_Dump_Registers() {
// Iterate all registers
for (uint8_t i = REG_STATUS; i < REG_FSC2; i++) {
std::bitset<8> reg(ADS1256_RREG((ADS1256_REGISTERS)i));
std::cout << (ADS1256_REGISTERS)i << ": " << reg << std::endl;
}
}
int ADS1256_Get_Id() {
// Shift the MSB 4 bits, since they represent the chip ID
return (ADS1256_RREG(REG_STATUS) >> 4);
}
bool ADS1256_BUFFEN() {
// Bit 1 is the BUFFEN toggle bit
return (ADS1256_RREG(REG_STATUS) & 0x2);
}
bool ADS1256_ACAL() {
// Bit 2 is the ACAL toggle bit
return (ADS1256_RREG(REG_STATUS) & 0x4);
}
uint8_t ADS1256_GET_DRATE() {
return ADS1256_RREG(REG_DRATE);
}