Code: Select all
#include "SPI.h"
#define CS1A 40
#define CS1B 39
#define CS2A 15
#define CS2B 16
#define ADC1M 1
#define ADC1A 2
#define ADC2M 8
#define ADC2A 9
#define SPI1C 12
#define SPI1D 11
#define SPI2C 36
#define SPI2D 35
SPIClass spi_1(HSPI);
SPIClass spi_2(FSPI);
SPISettings settings(100000, MSBFIRST, SPI_MODE0);
void setup_spi()
{
/* Begin SPI */
spi_1.begin(SPI1C, -1, SPI1D, -1);
spi_2.begin(SPI2C, -1, SPI2D, -1);
/* Setting chip select pins as outputs */
pinMode(CS1A, OUTPUT);
pinMode(CS1B, OUTPUT);
pinMode(CS2A, OUTPUT);
pinMode(CS2B, OUTPUT);
/* Setting chip select pins high */
digitalWrite(CS1A, HIGH);
digitalWrite(CS1B, HIGH);
digitalWrite(CS2A, HIGH);
digitalWrite(CS2B, HIGH);
}
void setup()
{
Serial.begin(115200);
delay(100);
Serial.printf("%d, %d", HSPI, FSPI);
setup_spi();
}
void loop()
{
dac_write((1 << 13) - 1, &spi_1, CS1A);
dac_write((1 << 13) - 1, &spi_1, CS1B);
dac_write((1 << 13) - 1, &spi_2, CS2A);
dac_write((1 << 13) - 1, &spi_2, CS2B);
uint16_t value1 = analogRead(ADC1A);
uint16_t value2 = analogRead(ADC2A);
dac_write((1 << 14) - 1, &spi_1, CS1A);
dac_write((1 << 14) - 1, &spi_1, CS1B);
dac_write((1 << 14) - 1, &spi_2, CS2A);
dac_write((1 << 14) - 1, &spi_2, CS2B);
delay(1);
}
void dac_write(uint16_t data, SPIClass* interface, uint8_t cs_pin)
{
interface->beginTransaction(settings);
digitalWrite(cs_pin, LOW);
interface->transfer16(data << 2);
digitalWrite(cs_pin, HIGH);
interface->endTransaction();
}