Skip to content

Commit 3a4d480

Browse files
committed
Implement DAC for enabled boards
Fixes #9
1 parent a15e621 commit 3a4d480

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

cores/arduino/wiring_analog.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@
2727
static int write_resolution = 8;
2828
static int read_resolution = 10;
2929

30+
#if DEVICE_ANALOGOUT
31+
#include "mbed/drivers/AnalogOut.h"
32+
mbed::AnalogOut* dac = NULL;
33+
void analogWriteDAC(PinName pin, int val) {
34+
if (dac == NULL) {
35+
dac = new mbed::AnalogOut(pin);
36+
}
37+
float percent = (float)val/(float)(1 << write_resolution);
38+
if (percent > 1.0f) {
39+
percent = 1.0f;
40+
}
41+
dac->write(percent);
42+
}
43+
#endif
44+
3045
void analogWrite(PinName pin, int val)
3146
{
3247
pin_size_t idx = PinNameToIndex(pin);
@@ -45,6 +60,12 @@ void analogWrite(pin_size_t pin, int val)
4560
if (pin >= PINS_COUNT) {
4661
return;
4762
}
63+
#ifdef DAC
64+
if (pin == DAC) {
65+
analogWriteDAC(digitalPinToPinName(pin), val);
66+
return;
67+
}
68+
#endif
4869
float percent = (float)val/(float)(1 << write_resolution);
4970
mbed::PwmOut* pwm = digitalPinToPwm(pin);
5071
if (pwm == NULL) {

variants/PORTENTA_H7_M4/pins_arduino.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ static const uint8_t A5 = PIN_A5;
3939
static const uint8_t A6 = PIN_A6;
4040
#define ADC_RESOLUTION 12
4141

42+
//DACs
43+
#define DAC (A6)
44+
4245
// Serial
4346
#define PIN_SERIAL_RX (13ul)
4447
#define PIN_SERIAL_TX (14ul)
@@ -97,6 +100,6 @@ void _ontouch1200bps_();
97100
#define SPI_MOSI (digitalPinToPinName(PIN_SPI_MOSI))
98101
#define SPI_SCK (digitalPinToPinName(PIN_SPI_SCK))
99102

100-
#define digitalPinToPort(P) (digitalPinToPinName(P)/32)
103+
#define digitalPinToPort(P) (digitalPinToPinName(P)/16)
101104

102105
#endif //__PINS_ARDUINO__

variants/PORTENTA_H7_M7/pins_arduino.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ static const uint8_t A5 = PIN_A5;
3939
static const uint8_t A6 = PIN_A6;
4040
#define ADC_RESOLUTION 12
4141

42+
//DACs
43+
#define DAC (A6)
44+
4245
// Serial
4346
#define PIN_SERIAL_RX (13ul)
4447
#define PIN_SERIAL_TX (14ul)
@@ -91,7 +94,6 @@ void _ontouch1200bps_();
9194
#define I2C_SDA1 I2C_SDA_INTERNAL
9295
#define I2C_SCL1 I2C_SCL_INTERNAL
9396

94-
9597
#define SPI_HOWMANY 1
9698

9799
#define SPI_MISO (digitalPinToPinName(PIN_SPI_MISO))

0 commit comments

Comments
 (0)