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

Dependents:   MCP41xxxApp MCP320xApp MCP41xxxApp

Committer:
Yann
Date:
Fri Apr 05 13:36:29 2013 +0000
Revision:
5:4f6133144e7e
Parent:
3:0acab5201dd8
Child:
6:ded0d8a6729c
Add support of MCP3204/8

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 *
Yann 0:03314ad622d6 31 * Microchip MCP42xxx/MCP41xxx Serial EEPROM device reference: DS11195C
Yann 0:03314ad622d6 32 *
Yann 0:03314ad622d6 33 * Note that for SPI details, please visit http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
Yann 0:03314ad622d6 34 *
Yann 3:0acab5201dd8 35 * @remark This class was validated with Tektronix TDS2014 oscilloscope in 3.3V
Yann 0:03314ad622d6 36 * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com)
Yann 0:03314ad622d6 37 */
Yann 0:03314ad622d6 38 class CMCP4xxxx_SPI {
Yann 0:03314ad622d6 39 /** Reference counter used to guarentee unicity of the instance of SPI class
Yann 0:03314ad622d6 40 */
Yann 0:03314ad622d6 41 static unsigned char SPIModuleRefCounter;
Yann 0:03314ad622d6 42
Yann 2:7c27fb9785be 43 /** ChipSelect (pin 1) see DS11195C-page 12 Clause 3.4 Chip Select (CS)
Yann 2:7c27fb9785be 44 */
Yann 2:7c27fb9785be 45 DigitalOut *_cs;
Yann 2:7c27fb9785be 46
Yann 2:7c27fb9785be 47 /** Reset state indicator (pin 11), see DS11195C-page 21 Clause 5.5 Reset (RS) Pin Operation
Yann 0:03314ad622d6 48 */
Yann 0:03314ad622d6 49 DigitalOut *_reset;
Yann 2:7c27fb9785be 50
Yann 2:7c27fb9785be 51 /** Shutdown state indicator (pin 12) see DS11195C-page 21 5.6 Shutdown (SHDN) Pin Operation
Yann 0:03314ad622d6 52 */
Yann 0:03314ad622d6 53 DigitalOut *_shdn;
Yann 2:7c27fb9785be 54
Yann 0:03314ad622d6 55 /** An unique instance of SPI class
Yann 0:03314ad622d6 56 */
Yann 0:03314ad622d6 57 SPI *_spiInstance;
Yann 0:03314ad622d6 58 public:
Yann 0:03314ad622d6 59 /** Authorized commands
Yann 0:03314ad622d6 60 * See DS11195C-page 18
Yann 0:03314ad622d6 61 */
Yann 0:03314ad622d6 62 enum Commands {
Yann 0:03314ad622d6 63 WriteToPot1, //<! Write to digital potentiometer #1
Yann 0:03314ad622d6 64 WriteToPot2, //<! Write to digital potentiometer #2
Yann 1:cf3cee91eb87 65 WriteToBoth, //<! Write to both digital potentiometers
Yann 0:03314ad622d6 66 ShutdownPot1, //<! Shutdown digital potentiometer #1
Yann 0:03314ad622d6 67 ShutdownPot2, //<! Shutdown digital potentiometer #2
Yann 1:cf3cee91eb87 68 ShutdownBoth, //<! Shutdown both digital potentiometers
Yann 0:03314ad622d6 69 };
Yann 0:03314ad622d6 70 public:
Yann 0:03314ad622d6 71 /** Constructor with Write Protect command pin wired.
Yann 0:03314ad622d6 72 *
Yann 0:03314ad622d6 73 * @param p_mosi: MBed pin for SDI
Yann 0:03314ad622d6 74 * @param p_miso: MBed pin for SDO. Note that this pin does not exist for MCP41xxx
Yann 0:03314ad622d6 75 * @param p_sclk: MBed pin for CLK
Yann 2:7c27fb9785be 76 * @param p_cs : MBed pin for Chip Select. If NC, assumes that application manage /CS, default value is NC, not connected
Yann 0:03314ad622d6 77 * @param p_reset: MBed pin to manage /RESET input. If NC, /RESET is not managed, default value is NC, not connected
Yann 0:03314ad622d6 78 * @param p_shdn: MBed pin to manage /SHDN input. If NC, /SHDN is not managed, default value is NC, not connected
Yann 0:03314ad622d6 79 * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz
Yann 0:03314ad622d6 80 */
Yann 2:7c27fb9785be 81 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 unsigned int p_frequency = 1000000);
Yann 0:03314ad622d6 82
Yann 0:03314ad622d6 83 /** Destructor
Yann 5:4f6133144e7e 84 * /CS pin is set to 1 before to release it
Yann 0:03314ad622d6 85 */
Yann 0:03314ad622d6 86 virtual ~CMCP4xxxx_SPI();
Yann 0:03314ad622d6 87
Yann 0:03314ad622d6 88 /** Used to return the unique instance of SPI instance
Yann 0:03314ad622d6 89 */
Yann 0:03314ad622d6 90 inline const SPI * operator * () { return (const SPI *)_spiInstance; };
Yann 0:03314ad622d6 91
Yann 1:cf3cee91eb87 92 /** Send a write a command (WriteToPot1, WriteToPot2 or WriteBoth)
Yann 0:03314ad622d6 93 *
Yann 0:03314ad622d6 94 * @param p_command The command to execute: Write or Shutdown (See DS11195C-page 18)
Yann 0:03314ad622d6 95 * @param p_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation)
Yann 2:7c27fb9785be 96 * @return 0x0000 on success, 0Xffff otherwise
Yann 0:03314ad622d6 97 * Exemple:
Yann 0:03314ad622d6 98 * @code
Yann 1:cf3cee91eb87 99 * unsigned char potLevel;
Yann 0:03314ad622d6 100 * ...
Yann 1:cf3cee91eb87 101 * g_chipSelect.write(0);
Yann 1:cf3cee91eb87 102 * g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
Yann 1:cf3cee91eb87 103 * g_chipSelect.write(1);
Yann 0:03314ad622d6 104 * ...
Yann 0:03314ad622d6 105 * @endcode
Yann 0:03314ad622d6 106 */
Yann 0:03314ad622d6 107 unsigned short Write(const Commands p_command, const unsigned char p_value);
Yann 0:03314ad622d6 108
Yann 1:cf3cee91eb87 109 /** Send a shutdown a command (ShutdownPot1, ShutdownPot2 or ShutdownBoth)
Yann 1:cf3cee91eb87 110 *
Yann 1:cf3cee91eb87 111 * @param p_command The command to execute: Write or Shutdown (See DS11195C-page 18)
Yann 1:cf3cee91eb87 112 * @param p_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation)
Yann 2:7c27fb9785be 113 * @return 0x0000 on success, 0Xffff otherwise
Yann 1:cf3cee91eb87 114 * Exemple:
Yann 1:cf3cee91eb87 115 * @code
Yann 1:cf3cee91eb87 116 * ...
Yann 1:cf3cee91eb87 117 * g_chipSelect.write(0);
Yann 1:cf3cee91eb87 118 * g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1);
Yann 1:cf3cee91eb87 119 * g_chipSelect.write(1);
Yann 1:cf3cee91eb87 120 * ...
Yann 1:cf3cee91eb87 121 * @endcode
Yann 1:cf3cee91eb87 122 */
Yann 1:cf3cee91eb87 123 unsigned short Write(const Commands p_command);
Yann 1:cf3cee91eb87 124
Yann 0:03314ad622d6 125 /** Write a NOP command
Yann 0:03314ad622d6 126 */
Yann 0:03314ad622d6 127 unsigned short Write();
Yann 0:03314ad622d6 128
Yann 0:03314ad622d6 129 /** Reset the device
Yann 1:cf3cee91eb87 130 * @code
Yann 1:cf3cee91eb87 131 * unsigned char potLevel;
Yann 1:cf3cee91eb87 132 * ...
Yann 1:cf3cee91eb87 133 * g_chipSelect.write(0);
Yann 1:cf3cee91eb87 134 * g_digitalPot.Reset();
Yann 1:cf3cee91eb87 135 * g_chipSelect.write(1);
Yann 1:cf3cee91eb87 136 * ...
Yann 1:cf3cee91eb87 137 * @endcode
Yann 0:03314ad622d6 138 */
Yann 0:03314ad622d6 139 bool Reset();
Yann 0:03314ad622d6 140
Yann 0:03314ad622d6 141 /** Shutdown the device
Yann 0:03314ad622d6 142 */
Yann 0:03314ad622d6 143 bool Shutdown(const bool p_set);
Yann 0:03314ad622d6 144
Yann 0:03314ad622d6 145 private:
Yann 0:03314ad622d6 146 /** Internal reference identifier
Yann 0:03314ad622d6 147 */
Yann 0:03314ad622d6 148 std::string _internalId;
Yann 0:03314ad622d6 149
Yann 0:03314ad622d6 150 }; // End of class CMCP4xxxx_SPI
Yann 0:03314ad622d6 151
Yann 0:03314ad622d6 152 } // End of namespace MCP4xxxx_SPI
Yann 0:03314ad622d6 153
Yann 0:03314ad622d6 154 using namespace MCP4xxxx_SPI;
Yann 0:03314ad622d6 155
Yann 0:03314ad622d6 156 #endif // __MCP4xxxx_SPI_H__