STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <SPI.h>
3
4#include "STAR_ADS126X.h"
5
6// Change the following line to automatically use the correct pins for the board
7// being tested (PT_Board, LC_Board, RTD_Board, or TC_Board)
8#define PINS_ACTIVE_LAYOUT sense_board_pins::PT_Board
9
10// These lines MUST be after the #define PINS_ACTIVE_LAYOUT or they will
11// overwrite it with the default value!
12#include "connector_adc_map.h"
13#include "sense_board_pins.h"
14
15// Pin on each connector to test (always 1)
16#define TEST_PIN 1
17
18using namespace sense_board_pins;
19
21
22// Buffer configuration
23// Magic (4 bytes) + 83 samples × 3 channels × (4 bytes timestamp + 4 bytes
24// reading) = 1996 bytes
25#define BUFFER_SIZE 1996
26#define MAGIC_SIZE 4
27#define SAMPLES_PER_BUFFER 83
28#define NUM_CHANNELS 3
29
30static uint8_t txBuffer[BUFFER_SIZE];
31static const uint8_t MAGIC[MAGIC_SIZE] = {'A', 'D', '2', '6'};
32
33void setup() {
34 Serial.begin(115200);
35
36 // Setup SPI
37 SPI.begin(Pins.ADC_SCLK, Pins.ADC_MISO, Pins.ADC_MOSI, Pins.ADC_CS_1);
38
39 // Due to the ADC output having valid data on FALLING CLK edges
40 SPI.setDataMode(SPI_MODE1);
41 pinMode(Pins.ADC_DRDY_1, INPUT);
42
43 // Setup ADC
44 ads126x.begin(Pins.ADC_CS_1);
45
46 // Stop it while we config it, as suggested by datasheet
47 ads126x.stopADC1();
48
49 // Bypass the PGA, so it does not affect measurements
50 ads126x.bypassPGA();
51
52 // Set the filter. You can change this to try different filters
53 ads126x.setFilter(ADS126X_SINC4);
54
55 // Set the datarate. You can change this, but the options depends on the
56 // filter
58
59 // Start ADC now that configuration is done
60 ads126x.startADC1();
61
62 // Initialize buffer with magic bytes
63 memcpy(txBuffer, MAGIC, MAGIC_SIZE);
64}
65
66void loop() {
67 // Pointer to data section (after magic bytes)
68 uint8_t* dataPtr = txBuffer + MAGIC_SIZE;
69
70 // Collect 83 samples to fill the buffer
71 for (int sample = 0; sample < SAMPLES_PER_BUFFER; sample++) {
72 // Loop through connectors 1, 2, 3
73 for (int connector = 1; connector <= NUM_CHANNELS; connector++) {
74 // Set the mux for this connector
75 ads126x.setInputMux(getAdcChannel(connector, TEST_PIN),
77
78 // Wait for DRDY pin (allow settle time after mux change)
79 while (digitalRead(Pins.ADC_DRDY_1) != LOW) {
81 }
82
84
85 // Capture timestamp right before reading
86 uint32_t timestamp = micros();
87
88 // Get the reading
89 const auto reading = ads126x.readADC1();
90 int32_t readingValue = reading.value;
91
92 // Store timestamp (little-endian format)
93 *dataPtr++ = (timestamp >> 0) & 0xFF;
94 *dataPtr++ = (timestamp >> 8) & 0xFF;
95 *dataPtr++ = (timestamp >> 16) & 0xFF;
96 *dataPtr++ = (timestamp >> 24) & 0xFF;
97
98 // Store reading (little-endian format)
99 *dataPtr++ = (readingValue >> 0) & 0xFF;
100 *dataPtr++ = (readingValue >> 8) & 0xFF;
101 *dataPtr++ = (readingValue >> 16) & 0xFF;
102 *dataPtr++ = (readingValue >> 24) & 0xFF;
103 }
104 }
105
106 // Send the complete buffer
108}
static SerialStub Serial
Definition Arduino.h:128
#define INPUT
Definition Arduino.h:21
int digitalRead(int)
Definition Arduino.h:50
void pinMode(int, int)
Definition Arduino.h:46
unsigned long micros()
Definition Arduino.h:37
#define SPI_MODE1
Definition Arduino.h:131
#define LOW
Definition Arduino.h:20
void delayMicroseconds(unsigned int)
Definition Arduino.h:58
#define ADS126X_AINCOM
#define ADS126X_RATE_1200
#define ADS126X_SINC4
static ADS126X ads126x
Definition main.cpp:20
#define TEST_PIN
Definition main.cpp:23
#define SAMPLES_PER_BUFFER
Definition main.cpp:27
void setup()
Definition main.cpp:33
#define BUFFER_SIZE
Definition main.cpp:25
static uint8_t txBuffer[BUFFER_SIZE]
Definition main.cpp:30
#define MAGIC_SIZE
Definition main.cpp:26
#define NUM_CHANNELS
Definition main.cpp:28
static const uint8_t MAGIC[MAGIC_SIZE]
Definition main.cpp:31
void loop()
Definition main.cpp:66
constexpr int getAdcChannel(uint8_t connector, uint8_t pin)
constexpr const Layout & Pins