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

Dependents:   MCP41xxxApp MCP320xApp MCP41xxxApp

Committer:
Yann
Date:
Thu Sep 05 07:29:00 2013 +0000
Revision:
7:12a0d89aa72f
Parent:
6:ded0d8a6729c
Minor bug fiixed

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 6:ded0d8a6729c 29 /** This class provides simplified SPI access to a Microchip MCP42xxx/MCP41xxx Digital Potentiometer device. V0.0.0.2
Yann 6:ded0d8a6729c 30 * The SPI static settings are: 16bits, mode (0,0)
Yann 6:ded0d8a6729c 31 * This class manages ONLY ONE /CS pin. If more than one SPI devices are used, ALL /CS pins shall be done by the application itself
Yann 0:03314ad622d6 32 *
Yann 0:03314ad622d6 33 * Microchip MCP42xxx/MCP41xxx Serial EEPROM device reference: DS11195C
Yann 6:ded0d8a6729c 34 * Microchip MCP41xx/MCP42xx Serial EEPROM device reference: DS122060B
Yann 0:03314ad622d6 35 *
Yann 6:ded0d8a6729c 36 * Note that MCP41xxx has no SO pin, only a SOI input pin
Yann 6:ded0d8a6729c 37 * Note that MCP42xxx has a SO pin reserved for daisy-chain configuration
Yann 6:ded0d8a6729c 38 * Note that MCP41xx/MCP42xx increment/decrement operations are not supported due to the 16 bits SPI setting
Yann 0:03314ad622d6 39 * Note that for SPI details, please visit http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
Yann 0:03314ad622d6 40 *
Yann 6:ded0d8a6729c 41 * @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 42 * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com)
Yann 0:03314ad622d6 43 */
Yann 0:03314ad622d6 44 class CMCP4xxxx_SPI {
Yann 0:03314ad622d6 45 /** Reference counter used to guarentee unicity of the instance of SPI class
Yann 0:03314ad622d6 46 */
Yann 0:03314ad622d6 47 static unsigned char SPIModuleRefCounter;
Yann 0:03314ad622d6 48
Yann 2:7c27fb9785be 49 /** ChipSelect (pin 1) see DS11195C-page 12 Clause 3.4 Chip Select (CS)
Yann 2:7c27fb9785be 50 */
Yann 2:7c27fb9785be 51 DigitalOut *_cs;
Yann 2:7c27fb9785be 52
Yann 2:7c27fb9785be 53 /** Reset state indicator (pin 11), see DS11195C-page 21 Clause 5.5 Reset (RS) Pin Operation
Yann 0:03314ad622d6 54 */
Yann 0:03314ad622d6 55 DigitalOut *_reset;
Yann 2:7c27fb9785be 56
Yann 2:7c27fb9785be 57 /** Shutdown state indicator (pin 12) see DS11195C-page 21 5.6 Shutdown (SHDN) Pin Operation
Yann 0:03314ad622d6 58 */
Yann 0:03314ad622d6 59 DigitalOut *_shdn;
Yann 2:7c27fb9785be 60
Yann 0:03314ad622d6 61 /** An unique instance of SPI class
Yann 0:03314ad622d6 62 */
Yann 0:03314ad622d6 63 SPI *_spiInstance;
Yann 6:ded0d8a6729c 64 /** Increment step value used for inc/dec methods, when inc/dec commands are not supported (such as _4xxxx famillies)
Yann 6:ded0d8a6729c 65 */
Yann 6:ded0d8a6729c 66 unsigned char _incStep;
Yann 6:ded0d8a6729c 67 public:
Yann 7:12a0d89aa72f 68 /** MCP32xx family
Yann 6:ded0d8a6729c 69 */
Yann 7:12a0d89aa72f 70 enum Mcp4xxxFamily {
Yann 7:12a0d89aa72f 71 _41xxx = 0x00, //<! 41xxx family such as MCP41010 - See DS11195C
Yann 7:12a0d89aa72f 72 _42xxx = 0x01, //<! 42xxx family such as MCP42010 - See DS11195C
Yann 7:12a0d89aa72f 73 _41xx = 0x02, //<! 41xx family such as MCP4251 - Single Potentiometer/Rheostat
Yann 7:12a0d89aa72f 74 _42xx = 0x03 //<! 42x2 family such as MCP42x2 - Dual Potentiometer/Rheostat
Yann 6:ded0d8a6729c 75 };
Yann 7:12a0d89aa72f 76 Mcp4xxxFamily _family;
Yann 0:03314ad622d6 77 public:
Yann 0:03314ad622d6 78 /** Authorized commands
Yann 0:03314ad622d6 79 * See DS11195C-page 18
Yann 0:03314ad622d6 80 */
Yann 0:03314ad622d6 81 enum Commands {
Yann 6:ded0d8a6729c 82 WriteToDigiPot1, //<! Write to digital potentiometer #1
Yann 6:ded0d8a6729c 83 WriteToDigiPot2, //<! Write to digital potentiometer #2
Yann 6:ded0d8a6729c 84 WriteToBoth, //<! Write to both digital potentiometers
Yann 6:ded0d8a6729c 85 ShutdownDigiPot1, //<! Shutdown digital potentiometer #1
Yann 6:ded0d8a6729c 86 ShutdownDigiPot2, //<! Shutdown digital potentiometer #2
Yann 6:ded0d8a6729c 87 ShutdownBoth, //<! Shutdown both digital potentiometers
Yann 6:ded0d8a6729c 88 };
Yann 6:ded0d8a6729c 89 /** Register adresses
Yann 6:ded0d8a6729c 90 * See DS22060B-page 33 Clause 4.0 FUNCTIONAL OVERVIEW
Yann 6:ded0d8a6729c 91 */
Yann 6:ded0d8a6729c 92 enum Addresses {
Yann 6:ded0d8a6729c 93 TCon = 0x04,
Yann 6:ded0d8a6729c 94 Status = 0x05
Yann 0:03314ad622d6 95 };
Yann 0:03314ad622d6 96 public:
Yann 0:03314ad622d6 97 /** Constructor with Write Protect command pin wired.
Yann 0:03314ad622d6 98 *
Yann 0:03314ad622d6 99 * @param p_mosi: MBed pin for SDI
Yann 0:03314ad622d6 100 * @param p_miso: MBed pin for SDO. Note that this pin does not exist for MCP41xxx
Yann 0:03314ad622d6 101 * @param p_sclk: MBed pin for CLK
Yann 2:7c27fb9785be 102 * @param p_cs : MBed pin for Chip Select. If NC, assumes that application manage /CS, default value is NC, not connected
Yann 0:03314ad622d6 103 * @param p_reset: MBed pin to manage /RESET input. If NC, /RESET is not managed, default value is NC, not connected
Yann 0:03314ad622d6 104 * @param p_shdn: MBed pin to manage /SHDN input. If NC, /SHDN is not managed, default value is NC, not connected
Yann 0:03314ad622d6 105 * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz
Yann 0:03314ad622d6 106 */
Yann 7:12a0d89aa72f 107 CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs = NC, const PinName p_reset = NC, const PinName p_shdn = NC, const Mcp4xxxFamily family = _41xxx, const unsigned int p_frequency = 1000000);
Yann 0:03314ad622d6 108
Yann 0:03314ad622d6 109 /** Destructor
Yann 0:03314ad622d6 110 */
Yann 0:03314ad622d6 111 virtual ~CMCP4xxxx_SPI();
Yann 0:03314ad622d6 112
Yann 0:03314ad622d6 113 /** Used to return the unique instance of SPI instance
Yann 0:03314ad622d6 114 */
Yann 0:03314ad622d6 115 inline const SPI * operator * () { return (const SPI *)_spiInstance; };
Yann 0:03314ad622d6 116
Yann 6:ded0d8a6729c 117 /** Write a value to the specified register. Only for _41xx/_42xx famillies
Yann 6:ded0d8a6729c 118 * @param p_address The register to be written
Yann 6:ded0d8a6729c 119 * @param p_value The value to write
Yann 6:ded0d8a6729c 120 * @return 0x0000 on success, any value otherwise
Yann 6:ded0d8a6729c 121 */
Yann 6:ded0d8a6729c 122 unsigned short WriteRegister(const Addresses p_address, const unsigned char p_value);
Yann 6:ded0d8a6729c 123
Yann 6:ded0d8a6729c 124 /** Read the content of the specified register. Only for _41xx/_42xx famillies
Yann 6:ded0d8a6729c 125 * @param p_address The register to be written
Yann 6:ded0d8a6729c 126 * @return The register value
Yann 6:ded0d8a6729c 127 */
Yann 6:ded0d8a6729c 128 unsigned short ReadRegister(const Addresses p_address);
Yann 6:ded0d8a6729c 129
Yann 6:ded0d8a6729c 130 /** Increment the specified digital potentiometer
Yann 6:ded0d8a6729c 131 * @param p_command The digital potentiometer to increment. Default: WriteToDigiPot1
Yann 6:ded0d8a6729c 132 * @return 0x0000 on success, any value otherwise
Yann 6:ded0d8a6729c 133 */
Yann 7:12a0d89aa72f 134 unsigned short Increment(const Commands p_command = WriteToDigiPot1);
Yann 6:ded0d8a6729c 135
Yann 6:ded0d8a6729c 136 /** Decrement the specified digital potentiometer
Yann 6:ded0d8a6729c 137 * @param p_command The digital potentiometer to decrement. Default: WriteToDigiPot1
Yann 6:ded0d8a6729c 138 * @return 0x0000 on success, any value otherwise
Yann 6:ded0d8a6729c 139 */
Yann 6:ded0d8a6729c 140 unsigned short Decrement(const Commands p_command = WriteToDigiPot1);
Yann 6:ded0d8a6729c 141
Yann 1:cf3cee91eb87 142 /** Send a write a command (WriteToPot1, WriteToPot2 or WriteBoth)
Yann 0:03314ad622d6 143 *
Yann 6:ded0d8a6729c 144 * @param p_command The command to execute (See DS11195C-page 18)
Yann 0:03314ad622d6 145 * @param p_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation)
Yann 2:7c27fb9785be 146 * @return 0x0000 on success, 0Xffff otherwise
Yann 0:03314ad622d6 147 * Exemple:
Yann 0:03314ad622d6 148 * @code
Yann 1:cf3cee91eb87 149 * unsigned char potLevel;
Yann 0:03314ad622d6 150 * ...
Yann 1:cf3cee91eb87 151 * g_chipSelect.write(0);
Yann 1:cf3cee91eb87 152 * g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
Yann 1:cf3cee91eb87 153 * g_chipSelect.write(1);
Yann 0:03314ad622d6 154 * ...
Yann 0:03314ad622d6 155 * @endcode
Yann 0:03314ad622d6 156 */
Yann 0:03314ad622d6 157 unsigned short Write(const Commands p_command, const unsigned char p_value);
Yann 0:03314ad622d6 158
Yann 1:cf3cee91eb87 159 /** Send a shutdown a command (ShutdownPot1, ShutdownPot2 or ShutdownBoth)
Yann 1:cf3cee91eb87 160 *
Yann 6:ded0d8a6729c 161 * @param p_command The command to execute (See DS11195C-page 18)
Yann 6:ded0d8a6729c 162 * @param p_set Set to true to shutdown, false to set up. Only used for _41xx/_42xx famillies (See DS22060B-page 36 REGISTER 4-2: TCON BITS)
Yann 2:7c27fb9785be 163 * @return 0x0000 on success, 0Xffff otherwise
Yann 1:cf3cee91eb87 164 * Exemple:
Yann 1:cf3cee91eb87 165 * @code
Yann 1:cf3cee91eb87 166 * ...
Yann 1:cf3cee91eb87 167 * g_chipSelect.write(0);
Yann 6:ded0d8a6729c 168 * g_digitalPot.Shutdown(CMCP4xxxx_SPI::ShutdownPot1);
Yann 1:cf3cee91eb87 169 * g_chipSelect.write(1);
Yann 1:cf3cee91eb87 170 * ...
Yann 1:cf3cee91eb87 171 * @endcode
Yann 1:cf3cee91eb87 172 */
Yann 6:ded0d8a6729c 173 unsigned short Shutdown(const Commands p_command, const bool p_set = true);
Yann 1:cf3cee91eb87 174
Yann 0:03314ad622d6 175 /** Write a NOP command
Yann 6:ded0d8a6729c 176 */
Yann 7:12a0d89aa72f 177 //unsigned short Write();
Yann 0:03314ad622d6 178
Yann 7:12a0d89aa72f 179 /** Change the current family
Yann 7:12a0d89aa72f 180 * @param family The new type of ICs
Yann 6:ded0d8a6729c 181 * @return the previous type of ICs
Yann 6:ded0d8a6729c 182 */
Yann 7:12a0d89aa72f 183 Mcp4xxxFamily SetFamilly(const Mcp4xxxFamily family);
Yann 6:ded0d8a6729c 184
Yann 0:03314ad622d6 185 /** Reset the device
Yann 6:ded0d8a6729c 186 * @code
Yann 6:ded0d8a6729c 187 * unsigned char potLevel;
Yann 6:ded0d8a6729c 188 * ...
Yann 6:ded0d8a6729c 189 * g_digitalPot.Reset();
Yann 6:ded0d8a6729c 190 * ...
Yann 6:ded0d8a6729c 191 * @endcode
Yann 6:ded0d8a6729c 192 */
Yann 6:ded0d8a6729c 193 void Reset();
Yann 0:03314ad622d6 194
Yann 0:03314ad622d6 195 /** Shutdown the device
Yann 0:03314ad622d6 196 */
Yann 6:ded0d8a6729c 197 void Shutdown(const bool p_set);
Yann 0:03314ad622d6 198
Yann 0:03314ad622d6 199 private:
Yann 6:ded0d8a6729c 200 /** Internal reference identifier. Only used when /RS in is available
Yann 6:ded0d8a6729c 201 *
Yann 6:ded0d8a6729c 202 * @param p_set Set to true to shutdown, false to set up
Yann 0:03314ad622d6 203 */
Yann 0:03314ad622d6 204 std::string _internalId;
Yann 0:03314ad622d6 205
Yann 0:03314ad622d6 206 }; // End of class CMCP4xxxx_SPI
Yann 0:03314ad622d6 207
Yann 0:03314ad622d6 208 } // End of namespace MCP4xxxx_SPI
Yann 0:03314ad622d6 209
Yann 0:03314ad622d6 210 using namespace MCP4xxxx_SPI;
Yann 0:03314ad622d6 211
Yann 0:03314ad622d6 212 #endif // __MCP4xxxx_SPI_H__