Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices

Dependents:   MCP41xxxApp MCP320xApp MCP41xxxApp

Committer:
Yann
Date:
Sun Jan 27 17:04:05 2013 +0000
Revision:
1:cf3cee91eb87
Parent:
0:03314ad622d6
Child:
2:7c27fb9785be
Validate library with hardware (MCP4100)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:03314ad622d6 1 /* mbed simplified access to Microchip MCP42xxx/MCP41xxx Digital Potentiometer devices (SPI)
Yann 0:03314ad622d6 2 * Copyright (c) 2013-2013 ygarcia, MIT License
Yann 0:03314ad622d6 3 *
Yann 0:03314ad622d6 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Yann 0:03314ad622d6 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Yann 0:03314ad622d6 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Yann 0:03314ad622d6 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Yann 0:03314ad622d6 8 * furnished to do so, subject to the following conditions:
Yann 0:03314ad622d6 9 *
Yann 0:03314ad622d6 10 * The above copyright notice and this permission notice shall be included in all copies or
Yann 0:03314ad622d6 11 * substantial portions of the Software.
Yann 0:03314ad622d6 12 *
Yann 0:03314ad622d6 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Yann 0:03314ad622d6 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Yann 0:03314ad622d6 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Yann 0:03314ad622d6 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Yann 0:03314ad622d6 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Yann 0:03314ad622d6 18 */
Yann 0:03314ad622d6 19 #if !defined(__MCP4xxxx_SPI_H__)
Yann 0:03314ad622d6 20 #define __MCP4xxxx_SPI_H__
Yann 0:03314ad622d6 21
Yann 0:03314ad622d6 22 #include <string>
Yann 0:03314ad622d6 23 #include <vector>
Yann 0:03314ad622d6 24
Yann 0:03314ad622d6 25 #include "Debug.h" // Include mbed header + debug primitives. See DebugLibrary
Yann 0:03314ad622d6 26
Yann 0:03314ad622d6 27 namespace MCP4xxxx_SPI {
Yann 0:03314ad622d6 28
Yann 0:03314ad622d6 29 /** This class provides simplified SPI access to a Microchip MCP42xxx/MCP41xxx Digital Potentiometer device. V0.0.0.1
Yann 0:03314ad622d6 30 * This class DOES NOT manage /CS pin. It shall be done by the application itself
Yann 0:03314ad622d6 31 *
Yann 0:03314ad622d6 32 * Microchip MCP42xxx/MCP41xxx Serial EEPROM device reference: DS11195C
Yann 0:03314ad622d6 33 *
Yann 0:03314ad622d6 34 * Note that for SPI details, please visit http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
Yann 0:03314ad622d6 35 *
Yann 0:03314ad622d6 36 * @remark This class was validated with Tektronix TDS2014 oscilloscope in 3.3V and in mixte power mode 3.3V for mbed and 5V for the Microchip 24LCxx Serial EEPROM device
Yann 0:03314ad622d6 37 * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com)
Yann 0:03314ad622d6 38 */
Yann 0:03314ad622d6 39 class CMCP4xxxx_SPI {
Yann 0:03314ad622d6 40 /** Reference counter used to guarentee unicity of the instance of SPI class
Yann 0:03314ad622d6 41 */
Yann 0:03314ad622d6 42 static unsigned char SPIModuleRefCounter;
Yann 0:03314ad622d6 43
Yann 0:03314ad622d6 44 /** Reset state indicator (pin 11); true to reset device, false otherwise (DS11195C-page 21 Clause 5.5 Reset (RS) Pin Operation)
Yann 0:03314ad622d6 45 */
Yann 0:03314ad622d6 46 DigitalOut *_reset;
Yann 0:03314ad622d6 47 /** Shutdown state indicator (pin 12); true to shutdown device, false otherwise (DS11195C-page 21 5.6 Shutdown (SHDN) Pin Operation)
Yann 0:03314ad622d6 48 */
Yann 0:03314ad622d6 49 DigitalOut *_shdn;
Yann 0:03314ad622d6 50 /** An unique instance of SPI class
Yann 0:03314ad622d6 51 */
Yann 0:03314ad622d6 52 SPI *_spiInstance;
Yann 0:03314ad622d6 53 public:
Yann 0:03314ad622d6 54 /** Authorized commands
Yann 0:03314ad622d6 55 * See DS11195C-page 18
Yann 0:03314ad622d6 56 */
Yann 0:03314ad622d6 57 enum Commands {
Yann 0:03314ad622d6 58 WriteToPot1, //<! Write to digital potentiometer #1
Yann 0:03314ad622d6 59 WriteToPot2, //<! Write to digital potentiometer #2
Yann 1:cf3cee91eb87 60 WriteToBoth, //<! Write to both digital potentiometers
Yann 0:03314ad622d6 61 ShutdownPot1, //<! Shutdown digital potentiometer #1
Yann 0:03314ad622d6 62 ShutdownPot2, //<! Shutdown digital potentiometer #2
Yann 1:cf3cee91eb87 63 ShutdownBoth, //<! Shutdown both digital potentiometers
Yann 0:03314ad622d6 64 };
Yann 0:03314ad622d6 65 public:
Yann 0:03314ad622d6 66 /** Constructor with Write Protect command pin wired.
Yann 0:03314ad622d6 67 *
Yann 0:03314ad622d6 68 * @param p_mosi: MBed pin for SDI
Yann 0:03314ad622d6 69 * @param p_miso: MBed pin for SDO. Note that this pin does not exist for MCP41xxx
Yann 0:03314ad622d6 70 * @param p_sclk: MBed pin for CLK
Yann 0:03314ad622d6 71 * @param p_reset: MBed pin to manage /RESET input. If NC, /RESET is not managed, default value is NC, not connected
Yann 0:03314ad622d6 72 * @param p_shdn: MBed pin to manage /SHDN input. If NC, /SHDN is not managed, default value is NC, not connected
Yann 0:03314ad622d6 73 * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz
Yann 0:03314ad622d6 74 */
Yann 0:03314ad622d6 75 CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_reset = NC, const PinName p_shdn = NC, const unsigned int p_frequency = 1000000);
Yann 0:03314ad622d6 76
Yann 0:03314ad622d6 77 /** Destructor
Yann 0:03314ad622d6 78 */
Yann 0:03314ad622d6 79 virtual ~CMCP4xxxx_SPI();
Yann 0:03314ad622d6 80
Yann 0:03314ad622d6 81 /** Used to return the unique instance of SPI instance
Yann 0:03314ad622d6 82 */
Yann 0:03314ad622d6 83 inline const SPI * operator * () { return (const SPI *)_spiInstance; };
Yann 0:03314ad622d6 84
Yann 1:cf3cee91eb87 85 /** Send a write a command (WriteToPot1, WriteToPot2 or WriteBoth)
Yann 0:03314ad622d6 86 *
Yann 0:03314ad622d6 87 * @param p_command The command to execute: Write or Shutdown (See DS11195C-page 18)
Yann 0:03314ad622d6 88 * @param p_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation)
Yann 1:cf3cee91eb87 89 * @return 0x00 on success, 0Xffff otherwise
Yann 0:03314ad622d6 90 * Exemple:
Yann 0:03314ad622d6 91 * @code
Yann 1:cf3cee91eb87 92 * unsigned char potLevel;
Yann 0:03314ad622d6 93 * ...
Yann 1:cf3cee91eb87 94 * g_chipSelect.write(0);
Yann 1:cf3cee91eb87 95 * g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
Yann 1:cf3cee91eb87 96 * g_chipSelect.write(1);
Yann 0:03314ad622d6 97 * ...
Yann 0:03314ad622d6 98 * @endcode
Yann 0:03314ad622d6 99 */
Yann 0:03314ad622d6 100 unsigned short Write(const Commands p_command, const unsigned char p_value);
Yann 0:03314ad622d6 101
Yann 1:cf3cee91eb87 102 /** Send a shutdown a command (ShutdownPot1, ShutdownPot2 or ShutdownBoth)
Yann 1:cf3cee91eb87 103 *
Yann 1:cf3cee91eb87 104 * @param p_command The command to execute: Write or Shutdown (See DS11195C-page 18)
Yann 1:cf3cee91eb87 105 * @param p_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation)
Yann 1:cf3cee91eb87 106 * @return 0x00 on success, 0Xffff otherwise
Yann 1:cf3cee91eb87 107 * Exemple:
Yann 1:cf3cee91eb87 108 * @code
Yann 1:cf3cee91eb87 109 * ...
Yann 1:cf3cee91eb87 110 * g_chipSelect.write(0);
Yann 1:cf3cee91eb87 111 * g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1);
Yann 1:cf3cee91eb87 112 * g_chipSelect.write(1);
Yann 1:cf3cee91eb87 113 * ...
Yann 1:cf3cee91eb87 114 * @endcode
Yann 1:cf3cee91eb87 115 */
Yann 1:cf3cee91eb87 116 unsigned short Write(const Commands p_command);
Yann 1:cf3cee91eb87 117
Yann 0:03314ad622d6 118 /** Write a NOP command
Yann 0:03314ad622d6 119 */
Yann 0:03314ad622d6 120 unsigned short Write();
Yann 0:03314ad622d6 121
Yann 0:03314ad622d6 122 /** Reset the device
Yann 1:cf3cee91eb87 123 * @code
Yann 1:cf3cee91eb87 124 * unsigned char potLevel;
Yann 1:cf3cee91eb87 125 * ...
Yann 1:cf3cee91eb87 126 * g_chipSelect.write(0);
Yann 1:cf3cee91eb87 127 * g_digitalPot.Reset();
Yann 1:cf3cee91eb87 128 * g_chipSelect.write(1);
Yann 1:cf3cee91eb87 129 * ...
Yann 1:cf3cee91eb87 130 * @endcode
Yann 0:03314ad622d6 131 */
Yann 0:03314ad622d6 132 bool Reset();
Yann 0:03314ad622d6 133
Yann 0:03314ad622d6 134 /** Shutdown the device
Yann 0:03314ad622d6 135 */
Yann 0:03314ad622d6 136 bool Shutdown(const bool p_set);
Yann 0:03314ad622d6 137
Yann 0:03314ad622d6 138 private:
Yann 0:03314ad622d6 139 /** Internal reference identifier
Yann 0:03314ad622d6 140 */
Yann 0:03314ad622d6 141 std::string _internalId;
Yann 0:03314ad622d6 142
Yann 0:03314ad622d6 143 }; // End of class CMCP4xxxx_SPI
Yann 0:03314ad622d6 144
Yann 0:03314ad622d6 145 } // End of namespace MCP4xxxx_SPI
Yann 0:03314ad622d6 146
Yann 0:03314ad622d6 147 using namespace MCP4xxxx_SPI;
Yann 0:03314ad622d6 148
Yann 0:03314ad622d6 149 #endif // __MCP4xxxx_SPI_H__