Maxim Integrated 7-bit Sink/Source Current DAC. DS4424, DS4422 input/output current Digital-to-Analog Converter Driver/library code.
Dependents: DS4424_Hello_Current_DAC_on_MAX32630FTHR
Revision 5:fc75fced724f, committed 2019-01-23
- Comitter:
- phonemacro
- Date:
- Wed Jan 23 00:43:09 2019 +0000
- Parent:
- 4:3824afaf0d61
- Commit message:
- Add methods for processed data (picoAmps). Reverse the parameter order in the protected method read_register.
Changed in this revision
| DS4424.cpp | Show annotated file Show diff for this revision Revisions of this file |
| DS4424.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/DS4424.cpp Sun Oct 07 22:13:14 2018 +0000
+++ b/DS4424.cpp Wed Jan 23 00:43:09 2019 +0000
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
+ * Copyright (C) 2018-2019 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@@ -32,6 +32,10 @@
*/
#include "DS4424.h"
+/* #include "USBSerial.h" */
+#define DS4424_U8_MAX ((uint8_t)~0U)
+#define DS4424_S8_MAX ((int8_t)(DS4424_U8_MAX>>1))
+#define DS4424_S8_MIN ((int8_t)(-DS4424_S8_MAX - 1))
/*
* DS4424 DAC control register 8 bits
@@ -84,7 +88,7 @@
union ds4424_raw_data raw;
if (chan_addr >= REG_OUT0 && chan_addr <= m_max_ch_reg_addr)
- ret = read_register(chan_addr, value);
+ ret = read_register(value, chan_addr);
if (ret != 0)
return DS4424_ERROR;
@@ -98,6 +102,23 @@
}
+
+/******************************************************************************/
+int DS4424::read_hw_raw(uint8_t &result, ChannelRegAddr_e chan_addr)
+{
+ uint8_t value;
+ int ret = DS4424_ERROR;
+
+ if (chan_addr >= REG_OUT0 && chan_addr <= m_max_ch_reg_addr)
+ ret = read_register(value, chan_addr);
+ if (ret != 0)
+ return DS4424_ERROR;
+
+ result = value;
+ return DS4424_NO_ERROR;
+}
+
+
/******************************************************************************/
int DS4424::write_raw(int32_t value, ChannelRegAddr_e chan_addr)
{
@@ -126,9 +147,90 @@
}
}
+
+
+/******************************************************************************/
+int DS4424::write_hw_raw(uint8_t value, ChannelRegAddr_e chan_addr)
+{
+ int ret = DS4424_ERROR;
+ if (chan_addr >= REG_OUT0 && chan_addr <= m_max_ch_reg_addr) {
+ ret = write_register(chan_addr, value);
+ if (ret != 0)
+ return DS4424_ERROR;
+ return DS4424_NO_ERROR;
+ } else {
+ return DS4424_ERROR;
+ }
+}
+
+
/******************************************************************************/
-int DS4424::read_register(ChannelRegAddr_e reg, uint8_t &value)
+int DS4424::convert_picoAmps_to_hw_raw(uint8_t *val_out,
+ int32_t picoAmps, uint32_t rfs_resistor)
+{
+ uint32_t val, tmp_scale, round_up;
+ union ds4424_raw_data raw;
+
+ /* val can be 0 to 200,000,000 (200 picoAmps) */
+ val = picoAmps;
+ raw.source_bit = DS4424_SOURCE_I;
+ if (picoAmps < 0) {
+ raw.source_bit = DS4424_SINK_I;
+ val = -picoAmps;
+ }
+ if (val > DS4424_MAX_PICOAMPS ||
+ rfs_resistor < DS4424_MIN_RFS ||
+ rfs_resistor > DS4424_MAX_RFS)
+ return DS4424_ERROR;
+ rfs_resistor /= 100;
+ val = val / 1000;
+
+ tmp_scale = DS4424_IFS_SCALE / 10;
+ round_up = tmp_scale / 2;
+ val = ((val * rfs_resistor) + round_up) /
+ tmp_scale;
+
+ val = val / 100;
+ if (val > DS4424_S8_MAX) {
+ printf("%s : Requested current %d\r\n",
+ __func__, val);
+ printf("exceeds maximum. DAC set to maximum %d\n\r\n",
+ DS4424_S8_MAX);
+ val = DS4424_S8_MAX;
+ }
+ raw.dx = val;
+ *val_out = raw.bits;
+ return DS4424_NO_ERROR;
+
+}
+
+
+/******************************************************************************/
+int DS4424::convert_raw_to_picoAmps(int32_t *val_out,
+ int8_t raw_in, uint32_t rfs_resistor)
+{
+ uint32_t round_up;
+ union ds4424_raw_data raw;
+ if (rfs_resistor < DS4424_MIN_RFS || rfs_resistor > DS4424_MAX_RFS) {
+ return DS4424_ERROR;
+ }
+ rfs_resistor /= 100;
+
+ raw.bits = raw_in;
+ *val_out = DS4424_IFS_SCALE * raw.dx * 10;
+ round_up = rfs_resistor / 2;
+ *val_out = (*val_out + round_up) / rfs_resistor;
+
+ if (raw.source_bit == DS4424_SINK_I)
+ *val_out = -*val_out; /* this routine use negatives if sinking */
+ *val_out = *val_out * 1000; /* picoAmps */
+ return DS4424_NO_ERROR;
+}
+
+
+/******************************************************************************/
+int DS4424::read_register(uint8_t &value, ChannelRegAddr_e reg)
{
int32_t ret;
--- a/DS4424.h Sun Oct 07 22:13:14 2018 +0000
+++ b/DS4424.h Wed Jan 23 00:43:09 2019 +0000
@@ -1,6 +1,6 @@
/*******************************************************************************
* @file DS4424.h
-* Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
+* Copyright (C) 2018-2019 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@@ -46,9 +46,14 @@
#define DS4424_SOURCE_I 1
#define DS4424_SINK_I 0
+#define DS4424_MAX_RFS 160000
+#define DS4424_MIN_RFS 40000
+#define DS4424_IFS_SCALE 61000 /* 61000*100= 6100000 = 100,000,000 * .976/16 */
+#define DS4424_MAX_PICOAMPS 200000000
+
/**
* @brief Four/two Channel I2C Sink/Source Current DAC
- * @version 1.0000.3
+ * @version 1.0000.301
*
* @details The DS4424/DS422 contains four/two I2C
* programmable current DACs. This driver enables
@@ -152,30 +157,72 @@
~DS4424();
/**
- * @brief Read raw value of a channel. Negative values are sink; Positive values are source.
+ * @brief Read raw value of a channel using two's complement.
* @param result - Value that is read from the register
+ * Negative values are sink; Positive values are source.
* @param chan_addr - Channel 0 to 3
* @return 0 on success, negative number on failure
*/
int read_raw(int32_t &result, ChannelRegAddr_e chan_addr);
/**
- * @brief Write raw value to a channel.
- * @param value - Value that is stored to the register
+ * @brief Read raw value of a channel, using the hardware format
+ * @param *result - Value that is read from the register.
+ * MSb is 0 for sink; MSb is 1 for source.
+ * @param chan_addr - Channel 0 to 3
+ * @return 0 on success, negative number on failure
+ */
+ int read_hw_raw(uint8_t &result, ChannelRegAddr_e chan_addr);
+
+ /**
+ * @brief Write two's complement raw value to a channel.
+ * @param value - Two's complement value that is stored to the register.
+ * Sink values are negative.
* @param chan_addr - Channel 0 to 3
* @return 0 on success, negative number on failure
*/
int write_raw(int32_t value, ChannelRegAddr_e chan_addr);
+
+ /**
+ * @brief Write raw value to a channel, using the hardware format
+ * @param value - Value that is stored to the register
+ * MSb is 0 for sink; MSb is 1 for source.
+ * @param chan_addr - Channel 0 to 3
+ * @return 0 on success, negative number on failure
+ */
+ int write_hw_raw(uint8_t value, ChannelRegAddr_e chan_addr);
+
+ /**
+ * @brief Convert picoAmps to raw values
+ * @param *val_out - 8-bit raw value
+ * @param picoAmps - signed value in picoAmps
+ * Negative values are sink; Positive values are source.
+ * @param rfs_resistor - external resistor values in Ohms
+ * @return 0 on success, negative number on failure
+ */
+ int convert_picoAmps_to_hw_raw(uint8_t *val_out,
+ int32_t picoAmps, uint32_t rfs_resistor);
+
+ /**
+ * @brief Convert raw values to picoAmps
+ * @param *val_out - value in picoAmps
+ * @param raw_in - 8 bit raw value, two's complement
+ * Negative values are sink; Positive values are source.
+ * @param rfs_resistor - external resistor values in Ohms
+ * @return 0 on success, negative number on failure
+ */
+ int convert_raw_to_picoAmps(int32_t *val_out,
+ int8_t raw_in, uint32_t rfs_resistor);
protected:
/**
* @brief Read register of device at slave address
+ * @param[out] value - Read data on success
* @param reg - Register address
- * @param[out] value - Read data on success
* @return 0 on success, negative number on failure
*/
- int read_register(ChannelRegAddr_e reg, uint8_t &value);
+ int read_register(uint8_t &value, ChannelRegAddr_e reg);
/**
* @brief Write register of device at slave address