Skip to content

Commit bceb17f

Browse files
committed
0.1.0 AD7390
1 parent 1c5c183 commit bceb17f

File tree

17 files changed

+958
-0
lines changed

17 files changed

+958
-0
lines changed

libraries/AD7390/.arduino-ci.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
platforms:
2+
rpipico:
3+
board: rp2040:rp2040:rpipico
4+
package: rp2040:rp2040
5+
gcc:
6+
features:
7+
defines:
8+
- ARDUINO_ARCH_RP2040
9+
warnings:
10+
flags:
11+
12+
packages:
13+
rp2040:rp2040:
14+
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
15+
16+
compile:
17+
# Choosing to run compilation tests on 2 different Arduino platforms
18+
platforms:
19+
- uno
20+
# - due
21+
# - zero
22+
# - leonardo
23+
- m4
24+
- esp32
25+
- esp8266
26+
# - mega2560
27+
- rpipico
28+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
github: RobTillaart
4+
custom: "https://www.paypal.me/robtillaart"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Arduino-lint
2+
3+
on: [push, pull_request]
4+
jobs:
5+
lint:
6+
runs-on: ubuntu-latest
7+
timeout-minutes: 5
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Arduino CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
runTest:
7+
runs-on: ubuntu-latest
8+
timeout-minutes: 20
9+
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 5
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: json-syntax-check
16+
uses: limitusus/json-syntax-check@v2
17+
with:
18+
pattern: "\\.json$"

libraries/AD7390/AD7390.cpp

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
//
2+
// FILE: AD7390.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// DATE: 2025-06-14
6+
// PURPOSE: Arduino library for AD7390/AD7391 12/10 bit SPI DAC.
7+
// URL: https://github.com/RobTillaart/AD7390
8+
9+
10+
#include "AD7390.h"
11+
12+
// SOFTWARE SPI
13+
AD7390::AD7390(uint8_t select, uint8_t clear, uint8_t dataOut, uint8_t clock)
14+
{
15+
_select = select;
16+
_clear = clear;
17+
_dataOut = dataOut;
18+
_clock = clock;
19+
_hwSPI = false;
20+
_mySPI = NULL;
21+
_maxValue = 4095;
22+
}
23+
24+
// HARDWARE SPI
25+
AD7390::AD7390(uint8_t select, uint8_t clear, __SPI_CLASS__ * mySPI)
26+
{
27+
_select = select;
28+
_clear = clear;
29+
_dataOut = 255;
30+
_clock = 255;
31+
_hwSPI = true;
32+
_mySPI = mySPI;
33+
_maxValue = 4095;
34+
}
35+
36+
// initializes the pins and starts SPI in case of hardware SPI
37+
void AD7390::begin(uint16_t value)
38+
{
39+
pinMode(_select, OUTPUT);
40+
digitalWrite(_select, LOW);
41+
pinMode(_clear, OUTPUT);
42+
digitalWrite(_clear, HIGH);
43+
44+
setSPIspeed(16000000);
45+
46+
if(_hwSPI)
47+
{
48+
// _mySPI->end();
49+
// _mySPI->begin();
50+
// delay(1);
51+
}
52+
else
53+
{
54+
pinMode(_dataOut, OUTPUT);
55+
pinMode(_clock, OUTPUT);
56+
digitalWrite(_dataOut, LOW);
57+
digitalWrite(_clock, LOW);
58+
}
59+
60+
setValue(value);
61+
}
62+
63+
64+
/////////////////////////////////////////////////////////////////////////////
65+
//
66+
// SET VALUE
67+
//
68+
bool AD7390::setValue(uint16_t value)
69+
{
70+
if (_value > _maxValue) return false;
71+
_value = value;
72+
updateDevice(value);
73+
return true;
74+
}
75+
76+
uint16_t AD7390::getValue()
77+
{
78+
return _value;
79+
}
80+
81+
uint16_t AD7390::getMaxValue()
82+
{
83+
return _maxValue;
84+
}
85+
86+
bool AD7390::setPercentage(float percentage)
87+
{
88+
if ((percentage < 0) || (percentage > 100.0)) return false;
89+
return setValue(round(percentage * (_maxValue / 100.0)));
90+
}
91+
92+
float AD7390::getPercentage()
93+
{
94+
uint16_t v = _value;
95+
if (v == 0) return 0.0;
96+
return (100.0 / _maxValue) * v;
97+
}
98+
99+
void AD7390::clear()
100+
{
101+
digitalWrite(_clear, LOW);
102+
delayMicroseconds(1);
103+
digitalWrite(_clear, HIGH);
104+
_value = 0;
105+
}
106+
107+
108+
/////////////////////////////////////////////////////////////////////////////
109+
//
110+
// SPI
111+
//
112+
void AD7390::setSPIspeed(uint32_t speed)
113+
{
114+
_SPIspeed = speed;
115+
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE3);
116+
}
117+
118+
uint32_t AD7390::getSPIspeed()
119+
{
120+
return _SPIspeed;
121+
}
122+
123+
bool AD7390::usesHWSPI()
124+
{
125+
return _hwSPI;
126+
}
127+
128+
129+
/////////////////////////////////////////////////////////////////////////////
130+
//
131+
// PROTECTED
132+
//
133+
void AD7390::updateDevice(uint16_t value)
134+
{
135+
digitalWrite(_select, HIGH);
136+
if (_hwSPI)
137+
{
138+
_mySPI->beginTransaction(_spi_settings);
139+
_mySPI->transfer16(value);
140+
_mySPI->endTransaction();
141+
}
142+
else // Software SPI
143+
{
144+
swSPI_transfer(value);
145+
}
146+
digitalWrite(_select, LOW);
147+
}
148+
149+
void AD7390::swSPI_transfer(uint16_t value)
150+
{
151+
uint8_t clk = _clock;
152+
uint8_t dao = _dataOut;
153+
for (uint16_t mask = 0x8000; mask; mask >>= 1) // 0x0800 performance?
154+
{
155+
digitalWrite(dao,(value & mask));
156+
digitalWrite(clk, HIGH);
157+
digitalWrite(clk, LOW);
158+
}
159+
}
160+
161+
162+
/////////////////////////////////////////////////////////////////////////////
163+
//
164+
// DERIVED CLASS
165+
//
166+
AD7391::AD7391(uint8_t select, uint8_t clear, __SPI_CLASS__ * mySPI)
167+
: AD7390(select, clear, mySPI)
168+
{
169+
_maxValue = 1023;
170+
}
171+
172+
173+
AD7391::AD7391(uint8_t select, uint8_t clear, uint8_t dataOut, uint8_t clock)
174+
: AD7390(select, clear, dataOut, clock)
175+
{
176+
_maxValue = 1023;
177+
}
178+
179+
180+
// -- END OF FILE --
181+

libraries/AD7390/AD7390.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#pragma once
2+
//
3+
// FILE: AD7390.h
4+
// AUTHOR: Rob Tillaart
5+
// VERSION: 0.1.0
6+
// DATE: 2025-06-14
7+
// PURPOSE: Arduino library for AD7390/AD7391 12/10 bit SPI DAC.
8+
// URL: https://github.com/RobTillaart/AD7390
9+
10+
#include "Arduino.h"
11+
#include "SPI.h"
12+
13+
14+
#define AD7390_LIB_VERSION (F("0.1.0"))
15+
16+
17+
#ifndef __SPI_CLASS__
18+
// MBED must be tested before RP2040
19+
#if defined(ARDUINO_ARCH_MBED)
20+
#define __SPI_CLASS__ SPIClass
21+
#elif defined(ARDUINO_ARCH_RP2040)
22+
#define __SPI_CLASS__ SPIClassRP2040
23+
#else
24+
#define __SPI_CLASS__ SPIClass
25+
#endif
26+
#endif
27+
28+
29+
class AD7390
30+
{
31+
public:
32+
// HARDWARE SPI
33+
// select = LD, clear = CLR
34+
AD7390(uint8_t select, uint8_t clear, __SPI_CLASS__ * mySPI = &SPI);
35+
// SOFTWARE SPI
36+
// select = LD, clear = CLR, dataOut = SDI, clock = CLK
37+
AD7390(uint8_t select, uint8_t clear, uint8_t dataOut, uint8_t clock);
38+
39+
void begin(uint16_t value);
40+
41+
bool setValue(uint16_t value);
42+
uint16_t getValue();
43+
uint16_t getMaxValue();
44+
bool setPercentage(float percentage);
45+
float getPercentage();
46+
47+
void clear(); // set DAC to zero.
48+
49+
// REFVOLTAGE possible interface addition
50+
// bool setRefVoltage(float volts);
51+
// bool setVoltage(float volts);
52+
// float getVoltage();
53+
54+
// speed in Hz
55+
void setSPIspeed(uint32_t speed);
56+
uint32_t getSPIspeed();
57+
58+
// Debugging
59+
bool usesHWSPI();
60+
61+
62+
protected:
63+
uint8_t _dataOut;
64+
uint8_t _clock;
65+
uint8_t _select;
66+
uint8_t _clear;
67+
68+
bool _hwSPI;
69+
uint32_t _SPIspeed = 16000000;
70+
71+
uint16_t _value;
72+
uint16_t _maxValue;
73+
74+
void updateDevice(uint16_t value);
75+
void swSPI_transfer(uint16_t value);
76+
77+
__SPI_CLASS__ * _mySPI;
78+
SPISettings _spi_settings;
79+
};
80+
81+
82+
/////////////////////////////////////////////////////////////////////////////
83+
//
84+
// DERIVED CLASSES
85+
//
86+
class AD7391 : public AD7390
87+
{
88+
public:
89+
AD7391(uint8_t select, uint8_t clear, __SPI_CLASS__ * mySPI = &SPI);
90+
AD7391(uint8_t select, uint8_t clear, uint8_t dataOut, uint8_t clock);
91+
};
92+
93+
94+
// -- END OF FILE --

libraries/AD7390/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Change Log AD7390
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
9+
## [0.1.0] - 2025-06-14
10+
- initial version
11+

libraries/AD7390/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025-2025 Rob Tillaart
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)