Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices
Dependents: MCP41xxxApp MCP320xApp MCP41xxxApp
MCP4xxxx_SPI.h@0:03314ad622d6, 2013-01-25 (annotated)
- Committer:
- Yann
- Date:
- Fri Jan 25 16:10:58 2013 +0000
- Revision:
- 0:03314ad622d6
- Child:
- 1:cf3cee91eb87
Create library and test application to support Microchip Digital Potentiometers MCP42xxx/MCP41xxx
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:03314ad622d6 | 60 | WriteToPotBoth, //<! Write to both digital potentiometers |
Yann | 0:03314ad622d6 | 61 | ShutdownPot1, //<! Shutdown digital potentiometer #1 |
Yann | 0:03314ad622d6 | 62 | ShutdownPot2, //<! Shutdown digital potentiometer #2 |
Yann | 0:03314ad622d6 | 63 | ShutdownPotBoth, //<! 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 | 0:03314ad622d6 | 85 | /** Write a command |
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 | 0:03314ad622d6 | 89 | * @return true on success, false otherwise |
Yann | 0:03314ad622d6 | 90 | * Exemple: |
Yann | 0:03314ad622d6 | 91 | * @code |
Yann | 0:03314ad622d6 | 92 | * unsigned char value = 0xaa; |
Yann | 0:03314ad622d6 | 93 | * ... |
Yann | 0:03314ad622d6 | 94 | * myEEPROM.Write(memoryAddress, value); |
Yann | 0:03314ad622d6 | 95 | * ... |
Yann | 0:03314ad622d6 | 96 | * @endcode |
Yann | 0:03314ad622d6 | 97 | */ |
Yann | 0:03314ad622d6 | 98 | unsigned short Write(const Commands p_command, const unsigned char p_value); |
Yann | 0:03314ad622d6 | 99 | |
Yann | 0:03314ad622d6 | 100 | /** Write a NOP command |
Yann | 0:03314ad622d6 | 101 | */ |
Yann | 0:03314ad622d6 | 102 | unsigned short Write(); |
Yann | 0:03314ad622d6 | 103 | |
Yann | 0:03314ad622d6 | 104 | /** Reset the device |
Yann | 0:03314ad622d6 | 105 | */ |
Yann | 0:03314ad622d6 | 106 | bool Reset(); |
Yann | 0:03314ad622d6 | 107 | |
Yann | 0:03314ad622d6 | 108 | /** Shutdown the device |
Yann | 0:03314ad622d6 | 109 | */ |
Yann | 0:03314ad622d6 | 110 | bool Shutdown(const bool p_set); |
Yann | 0:03314ad622d6 | 111 | |
Yann | 0:03314ad622d6 | 112 | private: |
Yann | 0:03314ad622d6 | 113 | /** Internal reference identifier |
Yann | 0:03314ad622d6 | 114 | */ |
Yann | 0:03314ad622d6 | 115 | std::string _internalId; |
Yann | 0:03314ad622d6 | 116 | |
Yann | 0:03314ad622d6 | 117 | }; // End of class CMCP4xxxx_SPI |
Yann | 0:03314ad622d6 | 118 | |
Yann | 0:03314ad622d6 | 119 | } // End of namespace MCP4xxxx_SPI |
Yann | 0:03314ad622d6 | 120 | |
Yann | 0:03314ad622d6 | 121 | using namespace MCP4xxxx_SPI; |
Yann | 0:03314ad622d6 | 122 | |
Yann | 0:03314ad622d6 | 123 | #endif // __MCP4xxxx_SPI_H__ |