-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltc241x.c
214 lines (189 loc) · 5.51 KB
/
ltc241x.c
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
/**
* ltc241x.c
* 2022 October 21
*
* Description:
* This is the SPI API source file. The default SPI config is defined here.
* This only contains necessary functions for LTC2414/LTC2418 ADC communications.
* Noted that when the chip is LTC2414. Just read the first 8 channels.
* Another 8 channels will return a garbage value.
*
* Copyright 2022 Xiaosheng An
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*
**/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ltc241x.h"
#include "spi.h"
// Replace the sleep function as you like (sleep_ms(int ms))
#include "sleep.h"
static const char *addresses[16] = {
"10000", "11000", "10001", "11001", "10010", "11010", "10011", "11011", "10100", "11100", "10101", "11101", "10110", "11110", "10111", "11111"
};
uint8_t fromBinary(const char *s) {
return (uint8_t) strtoul(s, NULL, 2);
}
/**
* Init the SPI bus for LTC241X ADC IC.
*
* `config` is the empty LTC241X_config_t variable that would be set by the two
* `internal` determines whether the internal oscillator is used or not. If not, pull-down the SCK line.
* `differential` determines whether the channel input is differential.
**/
int LTC241X_init(LTC241X_config_t *config, const char* device, bool internal, bool differential)
{
config->internal_osc = internal;
if (internal)
{
config->frequency = INTERNAL_LTC241X_SCK_FREQ;
config->conversion_time = INTERNAL_MAX_CONVERSION_TIME_MS;
}
else
{
config->frequency = CALC_LTC241X_SCK_FREQ(EXT_OSC_FREQ);
config->conversion_time = CALC_MAX_CONVERSION_TIME_MS(EXT_OSC_FREQ);
}
config->differential = differential;
memset(config->calibration, 0, sizeof(config->calibration));
*(config->spi) = SPI_get_default_config();
strcpy(config->spi->device, device);
config->spi->lsb = false;
config->spi->block_size = 4;
config->spi->frequency = config->frequency;
SPI_init(config->spi);
}
/**
* Calibrate all the 16 channels with the offset.
*
* `loop` determins how many times a single channel is read and averaged.
**/
int LTC241X_calibrate(LTC241X_config_t *config, int32_t loop)
{
int i, j;
int reading, total = 0;
#if LTC241x == LTC2414
for (i = 0; i < 8; i++)
#elif LTC241x == LTC2418
for (i = 0; i < 16; i++)
#endif
{
total = 0;
for (j = 0; j < loop; j++)
{
LTC241X_readSingle(config, i, &reading, 3);
sleep_ms(config->conversion_time * 2);
total += reading;
}
config->calibration[i] = total / loop;
}
#ifdef _SPI_VERBOSE_
printf("\nCalibration done! Calibration data as following:\n");
#if LTC241x == LTC2414
for (i = 0; i < 8; i++)
#elif LTC241x == LTC2418
for (i = 0; i < 16; i++)
#endif
{
printf("Calibration %d: %d\n", i, config->calibration[i]);
}
printf("\n");
#endif
}
/**
* This is to decode the input to the LTC2418 according to the requested channel and pre-defined configs
*
**/
uint8_t LTC241X_encodeDataIn(const LTC241X_config_t *config, int channel)
{
char *data;
uint8_t ret;
data = malloc(8 * sizeof(char));
strcpy(data, "101");
if (!config->differential) // Single-ended
{
strcat(data, addresses[channel]);
}
else // Differential
{
// TODO: to be implemented...
}
ret = fromBinary(data);
free(data);
return ret;
}
/**
* Check 32-bit parity
* Credit: https://graphics.stanford.edu/~seander/bithacks.html##ParityMultiply
*
* Return 0 if even number of 1.
* Return 1 if odd number of 1.
**/
unsigned int check_parity(unsigned int v)
{
v ^= v >> 1;
v ^= v >> 2;
v = (v & 0x11111111U) * 0x11111111U;
return (v >> 28) & 1;
}
/**
* Perform a single read on a specific channel from 0 to 15. The output data is loaded into `output` argument.
* The `attempts` parameter determines how many times of recursion maximum shall be called before returning error code.
*
* Return -2 if the channel address does not match.
* Return -1 if Even parity is not met.
* Return 0 if OK.
**/
int LTC241X_readSingle(LTC241X_config_t *config, int channel, int32_t *output, int attempts)
{
uint8_t *tx_buf;// {LTC241X_encodeDataIn(config, channel), 0, 0, 0};
uint8_t *rx_buf;
uint32_t result;
int32_t sign = -1, uvalue_out;
uint8_t address;
tx_buf = malloc(4 * sizeof(uint8_t));
rx_buf = malloc(4 * sizeof(uint8_t));
memset(tx_buf, LTC241X_encodeDataIn(config, channel), sizeof(uint8_t));
memset(tx_buf + 1, 0, 3 * sizeof(uint8_t));
memset(rx_buf, 0, 4 * sizeof(uint8_t));
SPI_transfer(config->spi, tx_buf, rx_buf);
result = ((uint32_t)rx_buf[0] << 24) | ((uint32_t)rx_buf[1] << 16) | ((uint32_t)rx_buf[2] << 8) | ((uint32_t)rx_buf[3] << 0);
if (check_parity(result) == 1)
{
#ifdef _SPI_VERBOSE_
printf("Pariity attempt %d\n", attempts);
#endif
if (attempts < 0) {return -1;}
else {
sleep_ms(config->conversion_time * 2);
return LTC241X_readSingle(config, channel, output, attempts - 1);
}
}
if (result & (1 << 29)) {sign = 1;}
address = (uint8_t)((result >> 1) & 31); // 0b11111
if (address != fromBinary(addresses[channel]))
{
#ifdef _SPI_VERBOSE_
printf("Address attempt %d\n", attempts);
#endif
if (attempts < 0) {return -2;}
else
{
sleep_ms(config->conversion_time * 2);
return LTC241X_readSingle(config, channel, output, attempts - 1);
}
}
uvalue_out = (int32_t)((result >> 6) & 8388607); // 0b11111111111111111111111 -> 23-bit
*output = sign * uvalue_out - config->calibration[channel];
#ifdef _SPI_VERBOSE_
printf("raw: %x\n", result);
printf("output: %d\n", *output);
#endif
return 0;
}