Maxim Integrated / MAX5171

Dependents:   MAX5171BOB_Tester MAX5171BOB_14bit_Remote_Sense_SPI_DAC MAX5171BOB_Serial_Tester

Files at this revision

API Documentation at this revision

Comitter:
whismanoid
Date:
Thu Mar 12 23:33:56 2020 +0000
Parent:
4:1984eef51fe3
Child:
6:f661c4a61207
Commit message:
get_spi_frequency get_spi_mode

Changed in this revision

MAX5171.cpp Show annotated file Show diff for this revision Revisions of this file
MAX5171.h Show annotated file Show diff for this revision Revisions of this file
--- a/MAX5171.cpp	Wed Oct 30 15:43:40 2019 -0700
+++ b/MAX5171.cpp	Thu Mar 12 23:33:56 2020 +0000
@@ -1,5 +1,5 @@
 // /*******************************************************************************
-// * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
+// * Copyright (C) 2020 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"),
@@ -43,7 +43,7 @@
 
 // Device Name = MAX5171
 // Device Description = Low-Power, Serial, 14-Bit, 1-Channel DACs with Force/Sense Voltage Output and SPI Interface
-// Device DeviceBriefDescription = 14-bit Force/Sense DAC
+// Device DeviceBriefDescription = 14-bit Force/Sense DAC
 // Device Manufacturer = Maxim Integrated
 // Device PartNumber = MAX5171AEEE+
 // Device RegValue_Width = DataWidth16bit_HL
@@ -137,13 +137,15 @@
     // SPI CS = ActiveLow
     // SPI FrameStart = CS
     m_SPI_cs_state = 1;
-    m_cs_pin = m_SPI_cs_state;
+    if (m_cs_pin.is_connected()) { // avoid mbed runtime error if pin is NC not connected
+        m_cs_pin = m_SPI_cs_state;
+    }
 
     // SPI CPOL = 0
     // SPI CPHA = 0
     // SPI MOSI and MISO Data are both stable on Rising edge of SCLK
     // SPI SCLK Idle Low
-    m_SPI_dataMode = 0; //SPI_MODE0; // CPOL=0,CPHA=0: Rising Edge stable; SCLK idle Low
+    m_SPI_dataMode = 0; //SPI_MODE0; // CPOL=0,CPHA=0: Rising Edge stable; SCLK idle Low
     m_spi.format(8,m_SPI_dataMode);         // int bits_must_be_8, int mode=0_3 CPOL=0,CPHA=0
 
     // SPI SCLKMaxMHz = 10
@@ -151,7 +153,7 @@
     //#define SPI_SCLK_Hz 48000000 // 48MHz
     //#define SPI_SCLK_Hz 24000000 // 24MHz
     //#define SPI_SCLK_Hz 12000000 // 12MHz
-    //#define SPI_SCLK_Hz 6000000 // 6MHz
+    //#define SPI_SCLK_Hz 6000000 // 6MHz
     //#define SPI_SCLK_Hz 4000000 // 4MHz
     //#define SPI_SCLK_Hz 2000000 // 2MHz
     //#define SPI_SCLK_Hz 1000000 // 1MHz
@@ -159,23 +161,23 @@
     m_spi.frequency(m_SPI_SCLK_Hz);
 
     //
-    // FB Analog Input to MAX5171 device
-    //
-    // RS Configuration Input to MAX5171 device
+    // FB Analog Input to MAX5171 device
+    //
+    // RS Configuration Input to MAX5171 device
     m_RS_pin = 1; // output logic high -- initial value in constructor
-    //
-    // PDLb Configuration Input to MAX5171 device
+    //
+    // PDLb Configuration Input to MAX5171 device
     m_PDLb_pin = 1; // output logic high -- initial value in constructor
-    //
-    // CLRb Configuration Input to MAX5171 device
+    //
+    // CLRb Configuration Input to MAX5171 device
     m_CLRb_pin = 1; // output logic high -- initial value in constructor
-    //
-    // SHDN Configuration Input to MAX5171 device
+    //
+    // SHDN Configuration Input to MAX5171 device
     m_SHDN_pin = 1; // output logic high -- initial value in constructor
-    //
-    // OUT Analog Output from device
-    //
-    // UPO General-Purpose Output from device
+    //
+    // OUT Analog Output from device
+    //
+    // UPO General-Purpose Output from device
 }
 
 MAX5171::~MAX5171()
@@ -183,7 +185,7 @@
     // do nothing
 }
 
-/// set SPI SCLK frequency
+/// set SPI SCLK frequency
 void MAX5171::spi_frequency(int spi_sclk_Hz)
 {
     m_SPI_SCLK_Hz = spi_sclk_Hz;
@@ -193,10 +195,12 @@
 // Assert SPI Chip Select
 // SPI chip-select for MAX5171
 //
-void MAX5171::SPIoutputCS(int isLogicHigh)
+inline void MAX5171::SPIoutputCS(int isLogicHigh)
 {
     m_SPI_cs_state = isLogicHigh;
-    m_cs_pin = m_SPI_cs_state;
+    if (m_cs_pin.is_connected()) { // avoid mbed runtime error if pin is NC not connected
+        m_cs_pin = m_SPI_cs_state;
+    }
 }
 
 // SPI write 16 bits
@@ -224,11 +228,11 @@
     //
     // Arduino: begin critical section: noInterrupts() masks all interrupt sources; end critical section with interrupts()
     //~ interrupts();
-    // Optional Diagnostic function to print SPI transactions
-    if (onSPIprint)
-    {
-        onSPIprint(byteCount, (uint8_t*)mosiData, (uint8_t*)misoData);
-    }
+    // Optional Diagnostic function to print SPI transactions
+    if (onSPIprint)
+    {
+        onSPIprint(byteCount, (uint8_t*)mosiData, (uint8_t*)misoData);
+    }
     //
     // VERIFY: SPIwrite24bits print diagnostic information
     //cmdLine.serial().printf(" MOSI->"));
@@ -284,7 +288,7 @@
 }
 
 //----------------------------------------
-// Menu item '!'
+// Menu item '!'
 // Initialize device
 // @return 1 on success; 0 on failure
 uint8_t MAX5171::Init(void)
@@ -525,7 +529,7 @@
 //----------------------------------------
 // CMD_1111_0xxx_xxxx_xxxx_MODE1_DOUT_SCLK_RISING_EDGE
 //
-// Mode 1, DOUT clocked out on SCLK's rising edge.
+// Mode 1, DOUT clocked out on SCLK's rising edge.
 // @return 1 on success; 0 on failure
 uint8_t MAX5171::MODE1_DOUT_SCLK_RISING_EDGE(void)
 {
--- a/MAX5171.h	Wed Oct 30 15:43:40 2019 -0700
+++ b/MAX5171.h	Thu Mar 12 23:33:56 2020 +0000
@@ -1,5 +1,5 @@
 // /*******************************************************************************
-// * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
+// * Copyright (C) 2020 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"),
@@ -40,7 +40,7 @@
 // System Description = Device driver example
 // Device Name = MAX5171
 // Device Description = Low-Power, Serial, 14-Bit, 1-Channel DACs with Force/Sense Voltage Output and SPI Interface
-// Device DeviceBriefDescription = 14-bit Force/Sense DAC
+// Device DeviceBriefDescription = 14-bit Force/Sense DAC
 // Device Manufacturer = Maxim Integrated
 // Device PartNumber = MAX5171AEEE+
 // Device RegValue_Width = DataWidth16bit_HL
@@ -63,41 +63,41 @@
 #ifndef __MAX5171_H__
 #define __MAX5171_H__
 
-// standard include for target platform -- Platform_Include_Boilerplate
+// standard include for target platform -- Platform_Include_Boilerplate
 #include "mbed.h"
-// Platforms:
-//   - MAX32625MBED
-//      - supports mbed-os-5.11, requires USBDevice library
-//      - add https://developer.mbed.org/teams/MaximIntegrated/code/USBDevice/
-//      - remove max32630fthr library (if present)
-//      - remove MAX32620FTHR library (if present)
-//   - MAX32600MBED
-//      - remove max32630fthr library (if present)
-//      - remove MAX32620FTHR library (if present)
-//      - Windows 10 note:  Don't connect HDK until you are ready to load new firmware into the board.
-//   - NUCLEO_F446RE
-//      - remove USBDevice library
-//      - remove max32630fthr library (if present)
-//      - remove MAX32620FTHR library (if present)
-//   - NUCLEO_F401RE
-//      - remove USBDevice library
-//      - remove max32630fthr library (if present)
-//      - remove MAX32620FTHR library (if present)
-//   - MAX32630FTHR
-//      - #include "max32630fthr.h"
-//      - add http://os.mbed.org/teams/MaximIntegrated/code/max32630fthr/
-//      - remove MAX32620FTHR library (if present)
-//   - MAX32620FTHR
-//      - #include "MAX32620FTHR.h"
-//      - remove max32630fthr library (if present)
-//      - add https://os.mbed.com/teams/MaximIntegrated/code/MAX32620FTHR/
-//      - not tested yet
-//   - MAX32625PICO
-//      - remove max32630fthr library (if present)
-//      - remove MAX32620FTHR library (if present)
-//      - not tested yet
-//
-// end Platform_Include_Boilerplate
+// Platforms:
+//   - MAX32625MBED
+//      - supports mbed-os-5.11, requires USBDevice library
+//      - add https://developer.mbed.org/teams/MaximIntegrated/code/USBDevice/
+//      - remove max32630fthr library (if present)
+//      - remove MAX32620FTHR library (if present)
+//   - MAX32600MBED
+//      - remove max32630fthr library (if present)
+//      - remove MAX32620FTHR library (if present)
+//      - Windows 10 note:  Don't connect HDK until you are ready to load new firmware into the board.
+//   - NUCLEO_F446RE
+//      - remove USBDevice library
+//      - remove max32630fthr library (if present)
+//      - remove MAX32620FTHR library (if present)
+//   - NUCLEO_F401RE
+//      - remove USBDevice library
+//      - remove max32630fthr library (if present)
+//      - remove MAX32620FTHR library (if present)
+//   - MAX32630FTHR
+//      - #include "max32630fthr.h"
+//      - add http://os.mbed.org/teams/MaximIntegrated/code/max32630fthr/
+//      - remove MAX32620FTHR library (if present)
+//   - MAX32620FTHR
+//      - #include "MAX32620FTHR.h"
+//      - remove max32630fthr library (if present)
+//      - add https://os.mbed.com/teams/MaximIntegrated/code/MAX32620FTHR/
+//      - not tested yet
+//   - MAX32625PICO
+//      - remove max32630fthr library (if present)
+//      - remove MAX32620FTHR library (if present)
+//      - not tested yet
+//
+// end Platform_Include_Boilerplate
 
 /**
  * @brief MAX5171 Low-Power, Serial, 14-Bit, 1-Channel DACs with Force/Sense Voltage Output and SPI Interface
@@ -110,41 +110,41 @@
  *
  * @code
  * // example code includes
- * // standard include for target platform -- Platform_Include_Boilerplate
+ * // standard include for target platform -- Platform_Include_Boilerplate
  * #include "mbed.h"
- * // Platforms:
- * //   - MAX32625MBED
+ * // Platforms:
+ * //   - MAX32625MBED
  * //      - supports mbed-os-5.11, requires USBDevice library
- * //      - add https://developer.mbed.org/teams/MaximIntegrated/code/USBDevice/
- * //      - remove max32630fthr library (if present)
- * //      - remove MAX32620FTHR library (if present)
- * //   - MAX32600MBED
- * //      - remove max32630fthr library (if present)
- * //      - remove MAX32620FTHR library (if present)
- * //      - Windows 10 note:  Don't connect HDK until you are ready to load new firmware into the board.
- * //   - NUCLEO_F446RE
- * //      - remove USBDevice library
- * //      - remove max32630fthr library (if present)
- * //      - remove MAX32620FTHR library (if present)
- * //   - NUCLEO_F401RE
- * //      - remove USBDevice library
- * //      - remove max32630fthr library (if present)
- * //      - remove MAX32620FTHR library (if present)
- * //   - MAX32630FTHR
- * //      - #include "max32630fthr.h"
- * //      - add http://os.mbed.org/teams/MaximIntegrated/code/max32630fthr/
- * //      - remove MAX32620FTHR library (if present)
- * //   - MAX32620FTHR
- * //      - #include "MAX32620FTHR.h"
- * //      - remove max32630fthr library (if present)
- * //      - add https://os.mbed.com/teams/MaximIntegrated/code/MAX32620FTHR/
- * //      - not tested yet
- * //   - MAX32625PICO
- * //      - remove max32630fthr library (if present)
- * //      - remove MAX32620FTHR library (if present)
- * //      - not tested yet
- * //
- * // end Platform_Include_Boilerplate
+ * //      - add https://developer.mbed.org/teams/MaximIntegrated/code/USBDevice/
+ * //      - remove max32630fthr library (if present)
+ * //      - remove MAX32620FTHR library (if present)
+ * //   - MAX32600MBED
+ * //      - remove max32630fthr library (if present)
+ * //      - remove MAX32620FTHR library (if present)
+ * //      - Windows 10 note:  Don't connect HDK until you are ready to load new firmware into the board.
+ * //   - NUCLEO_F446RE
+ * //      - remove USBDevice library
+ * //      - remove max32630fthr library (if present)
+ * //      - remove MAX32620FTHR library (if present)
+ * //   - NUCLEO_F401RE
+ * //      - remove USBDevice library
+ * //      - remove max32630fthr library (if present)
+ * //      - remove MAX32620FTHR library (if present)
+ * //   - MAX32630FTHR
+ * //      - #include "max32630fthr.h"
+ * //      - add http://os.mbed.org/teams/MaximIntegrated/code/max32630fthr/
+ * //      - remove MAX32620FTHR library (if present)
+ * //   - MAX32620FTHR
+ * //      - #include "MAX32620FTHR.h"
+ * //      - remove max32630fthr library (if present)
+ * //      - add https://os.mbed.com/teams/MaximIntegrated/code/MAX32620FTHR/
+ * //      - not tested yet
+ * //   - MAX32625PICO
+ * //      - remove max32630fthr library (if present)
+ * //      - remove MAX32620FTHR library (if present)
+ * //      - not tested yet
+ * //
+ * // end Platform_Include_Boilerplate
  * #include "MAX5171.h"
  *
  * // example code board support
@@ -201,29 +201,29 @@
  * // example code main function
  * int main()
  * {
+ *     g_MAX5171_device.Init();
+ *
  *     while (1)
  *     {
- *         g_MAX5171_device.Init();
- *
- *         uint16_t code;
- *         //
- *         // example #1
- *         code = 0x3332; // 80.0% of full scale REF(2.50V) = 2.00V
+ *         uint16_t code;
+ *         //
+ *         // example #1
+ *         code = 0x3332; // 80.0% of full scale REF(2.50V) = 2.00V
  *         g_MAX5171_device.CODE_LOAD(code);
  *         //
- *         // example #2
- *         code = 0x1fff; // 50.0% of full scale REF(2.50V) = 1.25V
+ *         // example #2
+ *         code = 0x1fff; // 50.0% of full scale REF(2.50V) = 1.25V
  *         g_MAX5171_device.CODE_LOAD(code);
  *         //
- *         // example #3
- *         code = 0x1999; // 40.0% of full scale REF(2.50V) = 1.00V
+ *         // example #3
+ *         code = 0x1999; // 40.0% of full scale REF(2.50V) = 1.00V
  *         g_MAX5171_device.CODE_LOAD(code);
  *         //
- *         // example #4
- *         code = 0x3fff; // 100.0% of full scale REF(2.50V) = 2.50V
+ *         // example #4
+ *         code = 0x3fff; // 100.0% of full scale REF(2.50V) = 2.50V
  *         g_MAX5171_device.CODE_LOAD(code);
  *
- *         // wait(3.0);
+ *         // wait(3.0);
  *     }
  * }
  * @endcode
@@ -301,19 +301,25 @@
      **************************************************************/
     ~MAX5171();
 
-    /// Function pointer void f(size_t byteCount, uint8_t mosiData[], uint8_t misoData[])
-    Callback<void(size_t, uint8_t*, uint8_t*)> onSPIprint; //!< optional @ref onSPIprint SPI diagnostic function
-
-    /// set SPI SCLK frequency
+    /// Function pointer void f(size_t byteCount, uint8_t mosiData[], uint8_t misoData[])
+    Callback<void(size_t, uint8_t*, uint8_t*)> onSPIprint; //!< optional @ref onSPIprint SPI diagnostic function
+
+    /// set SPI SCLK frequency
     void spi_frequency(int spi_sclk_Hz);
 
+    /// get SPI SCLK frequency
+    int get_spi_frequency() const { return m_SPI_SCLK_Hz; }
+
+    /// get SPI mode
+    int get_spi_dataMode() const { return m_SPI_dataMode; }
+
 //----------------------------------------
 public:
 
     /// reference voltage, in Volts
     double VRef;
 
-    /// shadow of dacCodeLsbs; write-only dacCodeLsbs field CMD_00dd_dddd_dddd_dddd_CODE or CMD_01dd_dddd_dddd_dddd_CODE_LOAD
+    /// shadow of dacCodeLsbs; write-only dacCodeLsbs field CMD_00dd_dddd_dddd_dddd_CODE or CMD_01dd_dddd_dddd_dddd_CODE_LOAD
     int16_t DACCode;
 
 
@@ -382,7 +388,7 @@
 public:
 
     //----------------------------------------
-    /// Menu item '!'
+    /// Menu item '!'
     /// Initialize device
     /// @return 1 on success; 0 on failure
     uint8_t Init(void);
@@ -458,14 +464,14 @@
     //----------------------------------------
     /// CMD_1111_0xxx_xxxx_xxxx_MODE1_DOUT_SCLK_RISING_EDGE
     ///
-    /// Mode 1, DOUT clocked out on SCLK's rising edge.
+    /// Mode 1, DOUT clocked out on SCLK's rising edge.
     /// @return 1 on success; 0 on failure
     uint8_t MODE1_DOUT_SCLK_RISING_EDGE(void);
 
     //----------------------------------------
     /// CMD_1111_1xxx_xxxx_xxxx_MODE0_DOUT_SCLK_FALLING_EDGE
     ///
-    /// Mode 0, DOUT clocked out on SCLK's falling edge (default).
+    /// Mode 0, DOUT clocked out on SCLK's falling edge (default).
     /// @return 1 on success; 0 on failure
     uint8_t MODE0_DOUT_SCLK_FALLING_EDGE(void);