Maxim Integrated MAX5171 14-bit Force/Sense DAC

Dependents:   MAX5171BOB_Tester MAX5171BOB_14bit_Remote_Sense_SPI_DAC MAX5171BOB_Serial_Tester

Committer:
whismanoid
Date:
Fri Jun 21 08:17:41 2019 +0000
Revision:
3:79839617d532
Parent:
1:e73245718b2a
Child:
4:1984eef51fe3
VRef initialization in MAX5171 class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
whismanoid 1:e73245718b2a 1 // /*******************************************************************************
whismanoid 1:e73245718b2a 2 // * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
whismanoid 1:e73245718b2a 3 // *
whismanoid 1:e73245718b2a 4 // * Permission is hereby granted, free of charge, to any person obtaining a
whismanoid 1:e73245718b2a 5 // * copy of this software and associated documentation files (the "Software"),
whismanoid 1:e73245718b2a 6 // * to deal in the Software without restriction, including without limitation
whismanoid 1:e73245718b2a 7 // * the rights to use, copy, modify, merge, publish, distribute, sublicense,
whismanoid 1:e73245718b2a 8 // * and/or sell copies of the Software, and to permit persons to whom the
whismanoid 1:e73245718b2a 9 // * Software is furnished to do so, subject to the following conditions:
whismanoid 1:e73245718b2a 10 // *
whismanoid 1:e73245718b2a 11 // * The above copyright notice and this permission notice shall be included
whismanoid 1:e73245718b2a 12 // * in all copies or substantial portions of the Software.
whismanoid 1:e73245718b2a 13 // *
whismanoid 1:e73245718b2a 14 // * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
whismanoid 1:e73245718b2a 15 // * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
whismanoid 1:e73245718b2a 16 // * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
whismanoid 1:e73245718b2a 17 // * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
whismanoid 1:e73245718b2a 18 // * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
whismanoid 1:e73245718b2a 19 // * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
whismanoid 1:e73245718b2a 20 // * OTHER DEALINGS IN THE SOFTWARE.
whismanoid 1:e73245718b2a 21 // *
whismanoid 1:e73245718b2a 22 // * Except as contained in this notice, the name of Maxim Integrated
whismanoid 1:e73245718b2a 23 // * Products, Inc. shall not be used except as stated in the Maxim Integrated
whismanoid 1:e73245718b2a 24 // * Products, Inc. Branding Policy.
whismanoid 1:e73245718b2a 25 // *
whismanoid 1:e73245718b2a 26 // * The mere transfer of this software does not imply any licenses
whismanoid 1:e73245718b2a 27 // * of trade secrets, proprietary technology, copyrights, patents,
whismanoid 1:e73245718b2a 28 // * trademarks, maskwork rights, or any other form of intellectual
whismanoid 1:e73245718b2a 29 // * property whatsoever. Maxim Integrated Products, Inc. retains all
whismanoid 1:e73245718b2a 30 // * ownership rights.
whismanoid 1:e73245718b2a 31 // *******************************************************************************
whismanoid 1:e73245718b2a 32 // */
whismanoid 1:e73245718b2a 33 // *********************************************************************
whismanoid 1:e73245718b2a 34 // @file MAX5171.cpp
whismanoid 1:e73245718b2a 35 // *********************************************************************
whismanoid 1:e73245718b2a 36 // Device Driver file
whismanoid 1:e73245718b2a 37 // DO NOT EDIT; except areas designated "CUSTOMIZE". Automatically generated file.
whismanoid 1:e73245718b2a 38 // generated by XMLSystemOfDevicesToMBED.py
whismanoid 1:e73245718b2a 39 // System Name = ExampleSystem
whismanoid 1:e73245718b2a 40 // System Description = Device driver example
whismanoid 1:e73245718b2a 41
whismanoid 1:e73245718b2a 42 #include "MAX5171.h"
whismanoid 1:e73245718b2a 43
whismanoid 1:e73245718b2a 44 // Device Name = MAX5171
whismanoid 1:e73245718b2a 45 // Device Description = Low-Power, Serial, 14-Bit, 1-Channel DACs with Force/Sense Voltage Output and SPI Interface
whismanoid 1:e73245718b2a 46 // Device Manufacturer = Maxim Integrated
whismanoid 1:e73245718b2a 47 // Device PartNumber = MAX5171AEEE+
whismanoid 1:e73245718b2a 48 // Device RegValue_Width = DataWidth16bit_HL
whismanoid 1:e73245718b2a 49 //
whismanoid 1:e73245718b2a 50 // DAC NumChannels = 1
whismanoid 1:e73245718b2a 51 // DAC ResolutionBits = 14
whismanoid 1:e73245718b2a 52 //
whismanoid 1:e73245718b2a 53 // SPI CS = ActiveLow
whismanoid 1:e73245718b2a 54 // SPI FrameStart = CS
whismanoid 1:e73245718b2a 55 // SPI CPOL = 0
whismanoid 1:e73245718b2a 56 // SPI CPHA = 0
whismanoid 1:e73245718b2a 57 // SPI MOSI and MISO Data are both stable on Rising edge of SCLK
whismanoid 1:e73245718b2a 58 // SPI SCLK Idle Low
whismanoid 1:e73245718b2a 59 // SPI SCLKMaxMHz = 10
whismanoid 1:e73245718b2a 60 // SPI SCLKMinMHz = 0
whismanoid 1:e73245718b2a 61 //
whismanoid 1:e73245718b2a 62 // InputPin Name = FB
whismanoid 1:e73245718b2a 63 // InputPin Description = Feedback Input
whismanoid 1:e73245718b2a 64 // InputPin Function = Analog
whismanoid 1:e73245718b2a 65 //
whismanoid 1:e73245718b2a 66 // InputPin Name = RS
whismanoid 1:e73245718b2a 67 // InputPin Description = Reset Mode Select (digital input). Connect to VDD to select midscale reset output value. Connect to DGND
whismanoid 1:e73245718b2a 68 // to select 0 reset output value.
whismanoid 1:e73245718b2a 69 // InputPin Function = Configuration
whismanoid 1:e73245718b2a 70 //
whismanoid 1:e73245718b2a 71 // InputPin Name = PDL#
whismanoid 1:e73245718b2a 72 // InputPin Description = Power-Down Lockout (digital input). Connect to VDD to allow shutdown. Connect to DGND to disable shutdown.
whismanoid 1:e73245718b2a 73 // InputPin Function = Configuration
whismanoid 1:e73245718b2a 74 //
whismanoid 1:e73245718b2a 75 // InputPin Name = CLR#
whismanoid 1:e73245718b2a 76 // InputPin Description = Clear DAC (digital input). Clears the DAC to its predetermined output state as set by RS.
whismanoid 1:e73245718b2a 77 // InputPin Function = Configuration
whismanoid 1:e73245718b2a 78 //
whismanoid 1:e73245718b2a 79 // InputPin Name = SHDN
whismanoid 1:e73245718b2a 80 // InputPin Description = Shutdown (digital input). Pulling SHDN high when PDL = VDD places the chip in shutdown mode with a
whismanoid 1:e73245718b2a 81 // maximum shutdown current 0f 10uA.
whismanoid 1:e73245718b2a 82 // InputPin Function = Configuration
whismanoid 1:e73245718b2a 83 //
whismanoid 1:e73245718b2a 84 // OutputPin Name = OUT
whismanoid 1:e73245718b2a 85 // OutputPin Description = Analog Voltage Output. High impedance in shutdown. Output voltage is limited to VDD.
whismanoid 1:e73245718b2a 86 // OutputPin Function = Analog
whismanoid 1:e73245718b2a 87 //
whismanoid 1:e73245718b2a 88 // OutputPin Name = UPO
whismanoid 1:e73245718b2a 89 // OutputPin Description = User-Programmable Output. State is set by serial input.
whismanoid 1:e73245718b2a 90 // OutputPin Function = General-Purpose
whismanoid 1:e73245718b2a 91 //
whismanoid 1:e73245718b2a 92 // SupplyPin Name = VDD
whismanoid 1:e73245718b2a 93 // SupplyPin Description = Positive Supply. Bypass to AGND with a 4.7uF capacitor in parallel with a 0.1uF capacitor.
whismanoid 1:e73245718b2a 94 // SupplyPin VinMax = 4.50
whismanoid 1:e73245718b2a 95 // SupplyPin VinMin = 5.50
whismanoid 1:e73245718b2a 96 // SupplyPin Function = Analog
whismanoid 1:e73245718b2a 97 //
whismanoid 1:e73245718b2a 98 // SupplyPin Name = REF
whismanoid 1:e73245718b2a 99 // SupplyPin Description = Reference Input. Maximum VREF is VDD - 1.4V. Connect an external +2.5V reference.
whismanoid 1:e73245718b2a 100 // SupplyPin VinMax = VDD-1.4
whismanoid 1:e73245718b2a 101 // SupplyPin VinMin = 0
whismanoid 1:e73245718b2a 102 // SupplyPin Function = Analog
whismanoid 1:e73245718b2a 103 //
whismanoid 1:e73245718b2a 104 // SupplyPin Name = DGND
whismanoid 1:e73245718b2a 105 // SupplyPin Description = Digital Ground.
whismanoid 1:e73245718b2a 106 // SupplyPin VinMax = 0
whismanoid 1:e73245718b2a 107 // SupplyPin VinMin = 0
whismanoid 1:e73245718b2a 108 // SupplyPin Function = Analog
whismanoid 1:e73245718b2a 109 //
whismanoid 1:e73245718b2a 110 // SupplyPin Name = AGND
whismanoid 1:e73245718b2a 111 // SupplyPin Description = Analog Ground.
whismanoid 1:e73245718b2a 112 // SupplyPin VinMax = 0
whismanoid 1:e73245718b2a 113 // SupplyPin VinMin = 0
whismanoid 1:e73245718b2a 114 // SupplyPin Function = Analog
whismanoid 1:e73245718b2a 115 //
whismanoid 1:e73245718b2a 116
whismanoid 1:e73245718b2a 117 // CODE GENERATOR: class constructor definition
whismanoid 1:e73245718b2a 118 MAX5171::MAX5171(SPI &spi, DigitalOut &cs_pin, // SPI interface
whismanoid 1:e73245718b2a 119 // CODE GENERATOR: class constructor definition gpio InputPin pins
whismanoid 1:e73245718b2a 120 // AnalogOut &FB_pin, // Analog Input to MAX5171 device
whismanoid 1:e73245718b2a 121 DigitalOut &RS_pin, // Digital Configuration Input to MAX5171 device
whismanoid 1:e73245718b2a 122 DigitalOut &PDLb_pin, // Digital Configuration Input to MAX5171 device
whismanoid 1:e73245718b2a 123 DigitalOut &CLRb_pin, // Digital Configuration Input to MAX5171 device
whismanoid 1:e73245718b2a 124 DigitalOut &SHDN_pin, // Digital Configuration Input to MAX5171 device
whismanoid 1:e73245718b2a 125 // CODE GENERATOR: class constructor definition gpio OutputPin pins
whismanoid 1:e73245718b2a 126 // AnalogIn &OUT_pin, // Analog Output from MAX5171 device
whismanoid 1:e73245718b2a 127 DigitalIn &UPO_pin, // Digital General-Purpose Output from MAX5171 device
whismanoid 1:e73245718b2a 128 // CODE GENERATOR: class constructor definition ic_variant
whismanoid 1:e73245718b2a 129 MAX5171_ic_t ic_variant)
whismanoid 3:79839617d532 130 // CODE GENERATOR: class constructor initializer list
whismanoid 1:e73245718b2a 131 : m_spi(spi), m_cs_pin(cs_pin), // SPI interface
whismanoid 3:79839617d532 132 // CODE GENERATOR: class constructor initializer list gpio InputPin pins
whismanoid 3:79839617d532 133 // m_FB_pin(FB_pin), // Analog Input to MAX5171 device
whismanoid 3:79839617d532 134 m_RS_pin(RS_pin), // Digital Configuration Input to MAX5171 device
whismanoid 3:79839617d532 135 m_PDLb_pin(PDLb_pin), // Digital Configuration Input to MAX5171 device
whismanoid 3:79839617d532 136 m_CLRb_pin(CLRb_pin), // Digital Configuration Input to MAX5171 device
whismanoid 3:79839617d532 137 m_SHDN_pin(SHDN_pin), // Digital Configuration Input to MAX5171 device
whismanoid 3:79839617d532 138 // CODE GENERATOR: class constructor initializer list gpio OutputPin pins
whismanoid 3:79839617d532 139 // m_OUT_pin(OUT_pin), // Analog Output from MAX5171 device
whismanoid 3:79839617d532 140 m_UPO_pin(UPO_pin), // Digital General-Purpose Output from MAX5171 device
whismanoid 3:79839617d532 141 // CODE GENERATOR: class constructor initializer list ic_variant
whismanoid 3:79839617d532 142 m_ic_variant(ic_variant)
whismanoid 1:e73245718b2a 143 {
whismanoid 1:e73245718b2a 144 // CODE GENERATOR: class constructor definition SPI interface initialization
whismanoid 1:e73245718b2a 145 //
whismanoid 1:e73245718b2a 146 // SPI CS = ActiveLow
whismanoid 1:e73245718b2a 147 // SPI FrameStart = CS
whismanoid 1:e73245718b2a 148 m_SPI_cs_state = 1;
whismanoid 1:e73245718b2a 149 m_cs_pin = m_SPI_cs_state;
whismanoid 1:e73245718b2a 150
whismanoid 1:e73245718b2a 151 // SPI CPOL = 0
whismanoid 1:e73245718b2a 152 // SPI CPHA = 0
whismanoid 1:e73245718b2a 153 // SPI MOSI and MISO Data are both stable on Rising edge of SCLK
whismanoid 1:e73245718b2a 154 // SPI SCLK Idle Low
whismanoid 1:e73245718b2a 155 m_SPI_dataMode = 0; //SPI_MODE0 // CPOL=0,CPHA=0: Rising Edge stable; SCLK idle Low
whismanoid 1:e73245718b2a 156 m_spi.format(8,m_SPI_dataMode); // int bits_must_be_8, int mode=0_3 CPOL=0,CPHA=0
whismanoid 1:e73245718b2a 157
whismanoid 1:e73245718b2a 158 // SPI SCLKMaxMHz = 10
whismanoid 1:e73245718b2a 159 // SPI SCLKMinMHz = 0
whismanoid 1:e73245718b2a 160 //#define SPI_SCLK_Hz 48000000 // 48MHz
whismanoid 1:e73245718b2a 161 //#define SPI_SCLK_Hz 24000000 // 24MHz
whismanoid 1:e73245718b2a 162 //#define SPI_SCLK_Hz 12000000 // 12MHz
whismanoid 1:e73245718b2a 163 //#define SPI_SCLK_Hz 4000000 // 4MHz
whismanoid 1:e73245718b2a 164 //#define SPI_SCLK_Hz 2000000 // 2MHz
whismanoid 1:e73245718b2a 165 //#define SPI_SCLK_Hz 1000000 // 1MHz
whismanoid 1:e73245718b2a 166 m_SPI_SCLK_Hz = 10000000; // 10MHz; MAX5171 limit is 10MHz
whismanoid 1:e73245718b2a 167 m_spi.frequency(m_SPI_SCLK_Hz);
whismanoid 1:e73245718b2a 168
whismanoid 1:e73245718b2a 169 // TODO1: CODE GENERATOR: class constructor definition gpio InputPin (Input to device) initialization
whismanoid 1:e73245718b2a 170 //
whismanoid 1:e73245718b2a 171 m_RS_pin = 1; // output logic high -- initial value in constructor
whismanoid 1:e73245718b2a 172 m_PDLb_pin = 1; // output logic high -- initial value in constructor
whismanoid 1:e73245718b2a 173 m_CLRb_pin = 1; // output logic high -- initial value in constructor
whismanoid 1:e73245718b2a 174 m_SHDN_pin = 1; // output logic high -- initial value in constructor
whismanoid 1:e73245718b2a 175 }
whismanoid 1:e73245718b2a 176
whismanoid 1:e73245718b2a 177 // CODE GENERATOR: class destructor definition
whismanoid 1:e73245718b2a 178 MAX5171::~MAX5171()
whismanoid 1:e73245718b2a 179 {
whismanoid 1:e73245718b2a 180 // do nothing
whismanoid 1:e73245718b2a 181 }
whismanoid 1:e73245718b2a 182
whismanoid 1:e73245718b2a 183 // CODE GENERATOR: spi_frequency setter definition
whismanoid 1:e73245718b2a 184 // set SPI SCLK frequency
whismanoid 1:e73245718b2a 185 void MAX5171::spi_frequency(int spi_sclk_Hz)
whismanoid 1:e73245718b2a 186 {
whismanoid 1:e73245718b2a 187 m_SPI_SCLK_Hz = spi_sclk_Hz;
whismanoid 1:e73245718b2a 188 m_spi.frequency(m_SPI_SCLK_Hz);
whismanoid 1:e73245718b2a 189 }
whismanoid 1:e73245718b2a 190
whismanoid 1:e73245718b2a 191 // CODE GENERATOR: omit global g_MAX5171_device
whismanoid 1:e73245718b2a 192 // CODE GENERATOR: extern function declarations
whismanoid 1:e73245718b2a 193 // CODE GENERATOR: extern function requirement MAX5171::SPIoutputCS
whismanoid 1:e73245718b2a 194 // Assert SPI Chip Select
whismanoid 1:e73245718b2a 195 // SPI chip-select for MAX5171
whismanoid 1:e73245718b2a 196 //
whismanoid 1:e73245718b2a 197 void MAX5171::SPIoutputCS(int isLogicHigh)
whismanoid 1:e73245718b2a 198 {
whismanoid 1:e73245718b2a 199 // CODE GENERATOR: extern function definition for function SPIoutputCS
whismanoid 1:e73245718b2a 200 // CODE GENERATOR: extern function definition for standard SPI interface function SPIoutputCS(int isLogicHigh)
whismanoid 1:e73245718b2a 201 m_SPI_cs_state = isLogicHigh;
whismanoid 1:e73245718b2a 202 m_cs_pin = m_SPI_cs_state;
whismanoid 1:e73245718b2a 203 }
whismanoid 1:e73245718b2a 204
whismanoid 1:e73245718b2a 205 // CODE GENERATOR: extern function requirement MAX5171::SPIwrite16bits
whismanoid 1:e73245718b2a 206 // SPI write 16 bits
whismanoid 1:e73245718b2a 207 // SPI interface to MAX5171 shift 16 bits mosiData into MAX5171 DIN
whismanoid 1:e73245718b2a 208 //
whismanoid 1:e73245718b2a 209 void MAX5171::SPIwrite16bits(int16_t mosiData16)
whismanoid 1:e73245718b2a 210 {
whismanoid 1:e73245718b2a 211 // CODE GENERATOR: extern function definition for function SPIwrite16bits
whismanoid 1:e73245718b2a 212 // TODO1: CODE GENERATOR: extern function definition for standard SPI interface function SPIwrite16bits(int16_t mosiData16)
whismanoid 1:e73245718b2a 213 size_t byteCount = 2;
whismanoid 1:e73245718b2a 214 static char mosiData[2];
whismanoid 1:e73245718b2a 215 static char misoData[2];
whismanoid 1:e73245718b2a 216 mosiData[0] = (char)((mosiData16 >> 8) & 0xFF); // MSByte
whismanoid 1:e73245718b2a 217 mosiData[1] = (char)((mosiData16 >> 0) & 0xFF); // LSByte
whismanoid 1:e73245718b2a 218 //
whismanoid 1:e73245718b2a 219 // Arduino: begin critical section: noInterrupts() masks all interrupt sources; end critical section with interrupts()
whismanoid 1:e73245718b2a 220 //~ noInterrupts();
whismanoid 1:e73245718b2a 221 //
whismanoid 1:e73245718b2a 222 //~ digitalWrite(Scope_Trigger_Pin, LOW); // diagnostic Scope_Trigger_Pin
whismanoid 1:e73245718b2a 223 //
whismanoid 1:e73245718b2a 224 unsigned int numBytesTransferred = m_spi.write(mosiData, byteCount, misoData, byteCount);
whismanoid 1:e73245718b2a 225 //~ m_spi.transfer(mosiData8_FF0000);
whismanoid 1:e73245718b2a 226 //~ m_spi.transfer(mosiData16_00FF00);
whismanoid 1:e73245718b2a 227 //~ m_spi.transfer(mosiData16_0000FF);
whismanoid 1:e73245718b2a 228 //
whismanoid 1:e73245718b2a 229 //~ digitalWrite(Scope_Trigger_Pin, HIGH); // diagnostic Scope_Trigger_Pin
whismanoid 1:e73245718b2a 230 //
whismanoid 1:e73245718b2a 231 // Arduino: begin critical section: noInterrupts() masks all interrupt sources; end critical section with interrupts()
whismanoid 1:e73245718b2a 232 //~ interrupts();
whismanoid 1:e73245718b2a 233 //
whismanoid 1:e73245718b2a 234 // VERIFY: SPIwrite24bits print diagnostic information
whismanoid 1:e73245718b2a 235 //cmdLine.serial().printf(" MOSI->"));
whismanoid 1:e73245718b2a 236 //cmdLine.serial().printf(" 0x"));
whismanoid 1:e73245718b2a 237 //Serial.print( (mosiData8_FF0000 & 0xFF), HEX);
whismanoid 1:e73245718b2a 238 //cmdLine.serial().printf(" 0x"));
whismanoid 1:e73245718b2a 239 //Serial.print( (mosiData16_00FF00 & 0xFF), HEX);
whismanoid 1:e73245718b2a 240 //cmdLine.serial().printf(" 0x"));
whismanoid 1:e73245718b2a 241 //Serial.print( (mosiData16_0000FF & 0xFF), HEX);
whismanoid 1:e73245718b2a 242 // hex dump mosiData[0..byteCount-1]
whismanoid 1:e73245718b2a 243 #if 0 // HAS_MICROUSBSERIAL
whismanoid 1:e73245718b2a 244 cmdLine_microUSBserial.serial().printf("\r\nSPI");
whismanoid 1:e73245718b2a 245 if (byteCount > 7) {
whismanoid 1:e73245718b2a 246 cmdLine_microUSBserial.serial().printf(" byteCount:%d", byteCount);
whismanoid 1:e73245718b2a 247 }
whismanoid 1:e73245718b2a 248 cmdLine_microUSBserial.serial().printf(" MOSI->");
whismanoid 3:79839617d532 249 for (unsigned int byteIndex = 0; byteIndex < byteCount; byteIndex++)
whismanoid 3:79839617d532 250 {
whismanoid 1:e73245718b2a 251 cmdLine_microUSBserial.serial().printf(" 0x%2.2X", mosiData[byteIndex]);
whismanoid 1:e73245718b2a 252 }
whismanoid 1:e73245718b2a 253 // hex dump misoData[0..byteCount-1]
whismanoid 1:e73245718b2a 254 cmdLine_microUSBserial.serial().printf(" MISO<-");
whismanoid 3:79839617d532 255 for (unsigned int byteIndex = 0; byteIndex < numBytesTransferred; byteIndex++)
whismanoid 3:79839617d532 256 {
whismanoid 1:e73245718b2a 257 cmdLine_microUSBserial.serial().printf(" 0x%2.2X", misoData[byteIndex]);
whismanoid 1:e73245718b2a 258 }
whismanoid 1:e73245718b2a 259 cmdLine_microUSBserial.serial().printf(" ");
whismanoid 1:e73245718b2a 260 #endif
whismanoid 1:e73245718b2a 261 #if 0 // HAS_DAPLINK_SERIAL
whismanoid 1:e73245718b2a 262 cmdLine_DAPLINKserial.serial().printf("\r\nSPI");
whismanoid 1:e73245718b2a 263 if (byteCount > 7) {
whismanoid 1:e73245718b2a 264 cmdLine_DAPLINKserial.serial().printf(" byteCount:%d", byteCount);
whismanoid 1:e73245718b2a 265 }
whismanoid 1:e73245718b2a 266 cmdLine_DAPLINKserial.serial().printf(" MOSI->");
whismanoid 3:79839617d532 267 for (unsigned int byteIndex = 0; byteIndex < byteCount; byteIndex++)
whismanoid 3:79839617d532 268 {
whismanoid 1:e73245718b2a 269 cmdLine_DAPLINKserial.serial().printf(" 0x%2.2X", mosiData[byteIndex]);
whismanoid 1:e73245718b2a 270 }
whismanoid 1:e73245718b2a 271 // hex dump misoData[0..byteCount-1]
whismanoid 1:e73245718b2a 272 cmdLine_DAPLINKserial.serial().printf(" MISO<-");
whismanoid 3:79839617d532 273 for (unsigned int byteIndex = 0; byteIndex < numBytesTransferred; byteIndex++)
whismanoid 3:79839617d532 274 {
whismanoid 1:e73245718b2a 275 cmdLine_DAPLINKserial.serial().printf(" 0x%2.2X", misoData[byteIndex]);
whismanoid 1:e73245718b2a 276 }
whismanoid 1:e73245718b2a 277 cmdLine_DAPLINKserial.serial().printf(" ");
whismanoid 1:e73245718b2a 278 #endif
whismanoid 1:e73245718b2a 279 // VERIFY: DIAGNOSTIC: print MAX5715 device register write
whismanoid 1:e73245718b2a 280 // TODO: MAX5715_print_register_verbose(mosiData8_FF0000, mosiData16_00FFFF);
whismanoid 1:e73245718b2a 281 // TODO: print_verbose_SPI_diagnostic(mosiData16_FF00, mosiData16_00FF, misoData16_FF00, misoData16_00FF);
whismanoid 1:e73245718b2a 282 //
whismanoid 1:e73245718b2a 283 // int misoData16 = (misoData16_FF00 << 8) | misoData16_00FF;
whismanoid 1:e73245718b2a 284 // return misoData16;
whismanoid 1:e73245718b2a 285 }
whismanoid 0:80a9add62408 286
whismanoid 1:e73245718b2a 287 // CODE GENERATOR: class member function definitions
whismanoid 1:e73245718b2a 288 //----------------------------------------
whismanoid 1:e73245718b2a 289 // Initialize device
whismanoid 1:e73245718b2a 290 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 291 uint8_t MAX5171::Init(void)
whismanoid 1:e73245718b2a 292 {
whismanoid 3:79839617d532 293
whismanoid 3:79839617d532 294 //----------------------------------------
whismanoid 3:79839617d532 295 // Nominal Full-Scale Voltage Reference
whismanoid 3:79839617d532 296 VRef = 2.500;
whismanoid 3:79839617d532 297
whismanoid 1:e73245718b2a 298 //----------------------------------------
whismanoid 1:e73245718b2a 299 // success
whismanoid 1:e73245718b2a 300 return 1;
whismanoid 1:e73245718b2a 301 }
whismanoid 1:e73245718b2a 302
whismanoid 1:e73245718b2a 303 //----------------------------------------
whismanoid 1:e73245718b2a 304 // Return the DAC register value corresponding to physical voltage.
whismanoid 1:e73245718b2a 305 // Does not perform any offset or gain correction.
whismanoid 1:e73245718b2a 306 //
whismanoid 1:e73245718b2a 307 // @pre VRef = Voltage of REF input, in Volts
whismanoid 1:e73245718b2a 308 // @param[in] voltage = physical voltage in Volts
whismanoid 1:e73245718b2a 309 // @return raw 14-bit MAX5171 code (right justified).
whismanoid 1:e73245718b2a 310 uint16_t MAX5171::DACCodeOfVoltage(double voltageV)
whismanoid 1:e73245718b2a 311 {
whismanoid 3:79839617d532 312
whismanoid 1:e73245718b2a 313 //----------------------------------------
whismanoid 1:e73245718b2a 314 // Linear map min and max endpoints
whismanoid 1:e73245718b2a 315 const double MaxScaleVoltage = VRef; // voltage of maximum code 0x3fff
whismanoid 1:e73245718b2a 316 const double MinScaleVoltage = 0.0; // voltage of minimum code 0x000
whismanoid 1:e73245718b2a 317 const uint16_t FULL_SCALE_CODE_14BIT = 0x3fff;
whismanoid 1:e73245718b2a 318 const uint16_t MaxCode = FULL_SCALE_CODE_14BIT;
whismanoid 1:e73245718b2a 319 const uint16_t MinCode = 0x000;
whismanoid 1:e73245718b2a 320 double codeFraction = (voltageV - MinScaleVoltage) / (MaxScaleVoltage - MinScaleVoltage);
whismanoid 1:e73245718b2a 321 double dacRegValueIdeal = ((codeFraction * (double)(MaxCode - MinCode + 1)) + MinCode + 0.5);
whismanoid 1:e73245718b2a 322 uint16_t dacRegValue = (uint16_t)dacRegValueIdeal;
whismanoid 3:79839617d532 323 if (dacRegValueIdeal > MaxCode)
whismanoid 3:79839617d532 324 {
whismanoid 1:e73245718b2a 325 dacRegValue = MaxCode;
whismanoid 3:79839617d532 326 } else if (dacRegValueIdeal < MinCode)
whismanoid 3:79839617d532 327 {
whismanoid 1:e73245718b2a 328 dacRegValue = MinCode;
whismanoid 1:e73245718b2a 329 }
whismanoid 1:e73245718b2a 330 return dacRegValue;
whismanoid 1:e73245718b2a 331 }
whismanoid 1:e73245718b2a 332
whismanoid 1:e73245718b2a 333 //----------------------------------------
whismanoid 1:e73245718b2a 334 // Return the physical voltage corresponding to DAC register.
whismanoid 1:e73245718b2a 335 // Does not perform any offset or gain correction.
whismanoid 1:e73245718b2a 336 //
whismanoid 1:e73245718b2a 337 // @pre VRef = Voltage of REF input, in Volts
whismanoid 1:e73245718b2a 338 // @param[in] value_u14: raw 14-bit MAX5171 code (right justified).
whismanoid 1:e73245718b2a 339 // @return physical voltage corresponding to MAX5171 code.
whismanoid 1:e73245718b2a 340 double MAX5171::VoltageOfCode(uint16_t value_u14)
whismanoid 1:e73245718b2a 341 {
whismanoid 3:79839617d532 342
whismanoid 1:e73245718b2a 343 //----------------------------------------
whismanoid 1:e73245718b2a 344 // Linear map min and max endpoints
whismanoid 1:e73245718b2a 345 double MaxScaleVoltage = VRef; // voltage of maximum code 0x3fff
whismanoid 1:e73245718b2a 346 double MinScaleVoltage = 0.0; // voltage of minimum code 0x000
whismanoid 1:e73245718b2a 347 const uint16_t FULL_SCALE_CODE_14BIT = 0x3fff;
whismanoid 1:e73245718b2a 348 const uint16_t MaxCode = FULL_SCALE_CODE_14BIT;
whismanoid 1:e73245718b2a 349 const uint16_t MinCode = 0x000;
whismanoid 1:e73245718b2a 350 double codeFraction = ((double)value_u14 - MinCode) / (MaxCode - MinCode + 1);
whismanoid 1:e73245718b2a 351 return MinScaleVoltage + ((MaxScaleVoltage - MinScaleVoltage) * codeFraction);
whismanoid 1:e73245718b2a 352 }
whismanoid 1:e73245718b2a 353
whismanoid 1:e73245718b2a 354 //----------------------------------------
whismanoid 1:e73245718b2a 355 // CMD_00dd_dddd_dddd_dddd_CODE
whismanoid 1:e73245718b2a 356 //
whismanoid 1:e73245718b2a 357 // Load input register; DAC registers are unchanged.
whismanoid 1:e73245718b2a 358 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 359 uint8_t MAX5171::CODE(uint16_t dacCodeLsbs)
whismanoid 1:e73245718b2a 360 {
whismanoid 3:79839617d532 361
whismanoid 1:e73245718b2a 362 //----------------------------------------
whismanoid 1:e73245718b2a 363 // Define command code
whismanoid 1:e73245718b2a 364 uint16_t mosiData16 = CMD_00dd_dddd_dddd_dddd_CODE
whismanoid 3:79839617d532 365 | ((0x3FFF & dacCodeLsbs) << 0); // left-align dddd_dddd_dddd
whismanoid 3:79839617d532 366
whismanoid 1:e73245718b2a 367 //----------------------------------------
whismanoid 1:e73245718b2a 368 // SPI write 16-bit mosiData16
whismanoid 1:e73245718b2a 369 SPIoutputCS(0);
whismanoid 1:e73245718b2a 370 SPIwrite16bits(mosiData16);
whismanoid 1:e73245718b2a 371 SPIoutputCS(1);
whismanoid 3:79839617d532 372
whismanoid 1:e73245718b2a 373 //----------------------------------------
whismanoid 1:e73245718b2a 374 // shadow of write-only register CMD_00dd_dddd_dddd_dddd_CODE
whismanoid 1:e73245718b2a 375 DACCode = dacCodeLsbs;
whismanoid 3:79839617d532 376
whismanoid 1:e73245718b2a 377 //----------------------------------------
whismanoid 1:e73245718b2a 378 // success
whismanoid 1:e73245718b2a 379 return 1;
whismanoid 1:e73245718b2a 380 }
whismanoid 1:e73245718b2a 381
whismanoid 1:e73245718b2a 382 //----------------------------------------
whismanoid 1:e73245718b2a 383 // CMD_01dd_dddd_dddd_dddd_CODE_LOAD
whismanoid 1:e73245718b2a 384 //
whismanoid 1:e73245718b2a 385 // Load input register; DAC registers are updated (start up DAC with new data).
whismanoid 1:e73245718b2a 386 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 387 uint8_t MAX5171::CODE_LOAD(uint16_t dacCodeLsbs)
whismanoid 1:e73245718b2a 388 {
whismanoid 3:79839617d532 389
whismanoid 1:e73245718b2a 390 //----------------------------------------
whismanoid 1:e73245718b2a 391 // Define command code
whismanoid 1:e73245718b2a 392 uint16_t mosiData16 = CMD_01dd_dddd_dddd_dddd_CODE_LOAD
whismanoid 3:79839617d532 393 | ((0x3FFF & dacCodeLsbs) << 0); // left-align dddd_dddd_dddd
whismanoid 3:79839617d532 394
whismanoid 1:e73245718b2a 395 //----------------------------------------
whismanoid 1:e73245718b2a 396 // SPI write 16-bit mosiData16
whismanoid 1:e73245718b2a 397 SPIoutputCS(0);
whismanoid 1:e73245718b2a 398 SPIwrite16bits(mosiData16);
whismanoid 1:e73245718b2a 399 SPIoutputCS(1);
whismanoid 3:79839617d532 400
whismanoid 1:e73245718b2a 401 //----------------------------------------
whismanoid 1:e73245718b2a 402 // shadow of write-only register CMD_00dd_dddd_dddd_dddd_CODE
whismanoid 1:e73245718b2a 403 DACCode = dacCodeLsbs;
whismanoid 3:79839617d532 404
whismanoid 1:e73245718b2a 405 //----------------------------------------
whismanoid 1:e73245718b2a 406 // success
whismanoid 1:e73245718b2a 407 return 1;
whismanoid 1:e73245718b2a 408 }
whismanoid 1:e73245718b2a 409
whismanoid 1:e73245718b2a 410 //----------------------------------------
whismanoid 1:e73245718b2a 411 // CMD_10xx_xxxx_xxxx_xxxx_LOAD
whismanoid 1:e73245718b2a 412 //
whismanoid 1:e73245718b2a 413 // Update DAC register from input register (start up DAC with data previously
whismanoid 1:e73245718b2a 414 // stored in the input registers).
whismanoid 1:e73245718b2a 415 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 416 uint8_t MAX5171::LOAD(void)
whismanoid 1:e73245718b2a 417 {
whismanoid 3:79839617d532 418
whismanoid 1:e73245718b2a 419 //----------------------------------------
whismanoid 1:e73245718b2a 420 // Define command code
whismanoid 1:e73245718b2a 421 uint16_t mosiData16 = CMD_10xx_xxxx_xxxx_xxxx_LOAD;
whismanoid 3:79839617d532 422
whismanoid 1:e73245718b2a 423 //----------------------------------------
whismanoid 1:e73245718b2a 424 // SPI write 16-bit mosiData16
whismanoid 1:e73245718b2a 425 SPIoutputCS(0);
whismanoid 1:e73245718b2a 426 SPIwrite16bits(mosiData16);
whismanoid 1:e73245718b2a 427 SPIoutputCS(1);
whismanoid 3:79839617d532 428
whismanoid 1:e73245718b2a 429 //----------------------------------------
whismanoid 1:e73245718b2a 430 // success
whismanoid 1:e73245718b2a 431 return 1;
whismanoid 1:e73245718b2a 432 }
whismanoid 1:e73245718b2a 433
whismanoid 1:e73245718b2a 434 //----------------------------------------
whismanoid 1:e73245718b2a 435 // CMD_1100_xxxx_xxxx_xxxx_NOP
whismanoid 1:e73245718b2a 436 //
whismanoid 1:e73245718b2a 437 // No operation (NOP).
whismanoid 1:e73245718b2a 438 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 439 uint8_t MAX5171::NOP(void)
whismanoid 1:e73245718b2a 440 {
whismanoid 3:79839617d532 441
whismanoid 1:e73245718b2a 442 //----------------------------------------
whismanoid 1:e73245718b2a 443 // Define command code
whismanoid 1:e73245718b2a 444 uint16_t mosiData16 = CMD_1100_xxxx_xxxx_xxxx_NOP;
whismanoid 3:79839617d532 445
whismanoid 1:e73245718b2a 446 //----------------------------------------
whismanoid 1:e73245718b2a 447 // SPI write 16-bit mosiData16
whismanoid 1:e73245718b2a 448 SPIoutputCS(0);
whismanoid 1:e73245718b2a 449 SPIwrite16bits(mosiData16);
whismanoid 1:e73245718b2a 450 SPIoutputCS(1);
whismanoid 3:79839617d532 451
whismanoid 1:e73245718b2a 452 //----------------------------------------
whismanoid 1:e73245718b2a 453 // success
whismanoid 1:e73245718b2a 454 return 1;
whismanoid 1:e73245718b2a 455 }
whismanoid 1:e73245718b2a 456
whismanoid 1:e73245718b2a 457 //----------------------------------------
whismanoid 1:e73245718b2a 458 // CMD_1101_xxxx_xxxx_xxxx_SHUTDOWN
whismanoid 1:e73245718b2a 459 //
whismanoid 1:e73245718b2a 460 // Shut down DAC (provided PDL# = 1).
whismanoid 1:e73245718b2a 461 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 462 uint8_t MAX5171::SHUTDOWN(void)
whismanoid 1:e73245718b2a 463 {
whismanoid 3:79839617d532 464
whismanoid 1:e73245718b2a 465 //----------------------------------------
whismanoid 1:e73245718b2a 466 // Define command code
whismanoid 1:e73245718b2a 467 uint16_t mosiData16 = CMD_1101_xxxx_xxxx_xxxx_SHUTDOWN;
whismanoid 3:79839617d532 468
whismanoid 1:e73245718b2a 469 //----------------------------------------
whismanoid 1:e73245718b2a 470 // SPI write 16-bit mosiData16
whismanoid 1:e73245718b2a 471 SPIoutputCS(0);
whismanoid 1:e73245718b2a 472 SPIwrite16bits(mosiData16);
whismanoid 1:e73245718b2a 473 SPIoutputCS(1);
whismanoid 3:79839617d532 474
whismanoid 1:e73245718b2a 475 //----------------------------------------
whismanoid 1:e73245718b2a 476 // success
whismanoid 1:e73245718b2a 477 return 1;
whismanoid 1:e73245718b2a 478 }
whismanoid 1:e73245718b2a 479
whismanoid 1:e73245718b2a 480 //----------------------------------------
whismanoid 1:e73245718b2a 481 // CMD_1110_0xxx_xxxx_xxxx_UPO_LOW
whismanoid 1:e73245718b2a 482 //
whismanoid 1:e73245718b2a 483 // UPO goes low (default).
whismanoid 1:e73245718b2a 484 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 485 uint8_t MAX5171::UPO_LOW(void)
whismanoid 1:e73245718b2a 486 {
whismanoid 3:79839617d532 487
whismanoid 1:e73245718b2a 488 //----------------------------------------
whismanoid 1:e73245718b2a 489 // Define command code
whismanoid 1:e73245718b2a 490 uint16_t mosiData16 = CMD_1110_0xxx_xxxx_xxxx_UPO_LOW;
whismanoid 3:79839617d532 491
whismanoid 1:e73245718b2a 492 //----------------------------------------
whismanoid 1:e73245718b2a 493 // SPI write 16-bit mosiData16
whismanoid 1:e73245718b2a 494 SPIoutputCS(0);
whismanoid 1:e73245718b2a 495 SPIwrite16bits(mosiData16);
whismanoid 1:e73245718b2a 496 SPIoutputCS(1);
whismanoid 3:79839617d532 497
whismanoid 1:e73245718b2a 498 //----------------------------------------
whismanoid 1:e73245718b2a 499 // success
whismanoid 1:e73245718b2a 500 return 1;
whismanoid 1:e73245718b2a 501 }
whismanoid 1:e73245718b2a 502
whismanoid 1:e73245718b2a 503 //----------------------------------------
whismanoid 1:e73245718b2a 504 // CMD_1110_1xxx_xxxx_xxxx_UPO_HIGH
whismanoid 1:e73245718b2a 505 //
whismanoid 1:e73245718b2a 506 // UPO goes high.
whismanoid 1:e73245718b2a 507 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 508 uint8_t MAX5171::UPO_HIGH(void)
whismanoid 1:e73245718b2a 509 {
whismanoid 3:79839617d532 510
whismanoid 1:e73245718b2a 511 //----------------------------------------
whismanoid 1:e73245718b2a 512 // Define command code
whismanoid 1:e73245718b2a 513 uint16_t mosiData16 = CMD_1110_1xxx_xxxx_xxxx_UPO_HIGH;
whismanoid 3:79839617d532 514
whismanoid 1:e73245718b2a 515 //----------------------------------------
whismanoid 1:e73245718b2a 516 // SPI write 16-bit mosiData16
whismanoid 1:e73245718b2a 517 SPIoutputCS(0);
whismanoid 1:e73245718b2a 518 SPIwrite16bits(mosiData16);
whismanoid 1:e73245718b2a 519 SPIoutputCS(1);
whismanoid 3:79839617d532 520
whismanoid 1:e73245718b2a 521 //----------------------------------------
whismanoid 1:e73245718b2a 522 // success
whismanoid 1:e73245718b2a 523 return 1;
whismanoid 1:e73245718b2a 524 }
whismanoid 1:e73245718b2a 525
whismanoid 1:e73245718b2a 526 //----------------------------------------
whismanoid 1:e73245718b2a 527 // CMD_1111_0xxx_xxxx_xxxx_MODE1_DOUT_SCLK_RISING_EDGE
whismanoid 1:e73245718b2a 528 //
whismanoid 1:e73245718b2a 529 // Mode 1, DOUT clocked out on SCLK’s rising edge.
whismanoid 1:e73245718b2a 530 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 531 uint8_t MAX5171::MODE1_DOUT_SCLK_RISING_EDGE(void)
whismanoid 1:e73245718b2a 532 {
whismanoid 3:79839617d532 533
whismanoid 1:e73245718b2a 534 //----------------------------------------
whismanoid 1:e73245718b2a 535 // Define command code
whismanoid 1:e73245718b2a 536 uint16_t mosiData16 = CMD_1111_0xxx_xxxx_xxxx_MODE1_DOUT_SCLK_RISING_EDGE;
whismanoid 3:79839617d532 537
whismanoid 1:e73245718b2a 538 //----------------------------------------
whismanoid 1:e73245718b2a 539 // SPI write 16-bit mosiData16
whismanoid 1:e73245718b2a 540 SPIoutputCS(0);
whismanoid 1:e73245718b2a 541 SPIwrite16bits(mosiData16);
whismanoid 1:e73245718b2a 542 SPIoutputCS(1);
whismanoid 3:79839617d532 543
whismanoid 1:e73245718b2a 544 //----------------------------------------
whismanoid 1:e73245718b2a 545 // success
whismanoid 1:e73245718b2a 546 return 1;
whismanoid 1:e73245718b2a 547 }
whismanoid 1:e73245718b2a 548
whismanoid 1:e73245718b2a 549 //----------------------------------------
whismanoid 1:e73245718b2a 550 // CMD_1111_1xxx_xxxx_xxxx_MODE0_DOUT_SCLK_FALLING_EDGE
whismanoid 1:e73245718b2a 551 //
whismanoid 1:e73245718b2a 552 // Mode 0, DOUT clocked out on SCLK’s falling edge (default).
whismanoid 1:e73245718b2a 553 // @return 1 on success; 0 on failure
whismanoid 1:e73245718b2a 554 uint8_t MAX5171::MODE0_DOUT_SCLK_FALLING_EDGE(void)
whismanoid 1:e73245718b2a 555 {
whismanoid 3:79839617d532 556
whismanoid 1:e73245718b2a 557 //----------------------------------------
whismanoid 1:e73245718b2a 558 // Define command code
whismanoid 1:e73245718b2a 559 uint16_t mosiData16 = CMD_1111_1xxx_xxxx_xxxx_MODE0_DOUT_SCLK_FALLING_EDGE;
whismanoid 3:79839617d532 560
whismanoid 1:e73245718b2a 561 //----------------------------------------
whismanoid 1:e73245718b2a 562 // SPI write 16-bit mosiData16
whismanoid 1:e73245718b2a 563 SPIoutputCS(0);
whismanoid 1:e73245718b2a 564 SPIwrite16bits(mosiData16);
whismanoid 1:e73245718b2a 565 SPIoutputCS(1);
whismanoid 3:79839617d532 566
whismanoid 1:e73245718b2a 567 //----------------------------------------
whismanoid 1:e73245718b2a 568 // success
whismanoid 1:e73245718b2a 569 return 1;
whismanoid 1:e73245718b2a 570 }
whismanoid 1:e73245718b2a 571
whismanoid 1:e73245718b2a 572
whismanoid 1:e73245718b2a 573 // End of file