Simplified access to the Microchip 1//8-Channels 12-Bit A/D Converters with SPI Serial Interface

Dependents:   MCP320xApp

Committer:
Yann
Date:
Fri Apr 05 13:38:09 2013 +0000
Revision:
0:17cee87e4563
Add support of MCP3204/8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:17cee87e4563 1 /* mbed simplified access to Microchip MCP320x 12 bits ADC devices (SPI)
Yann 0:17cee87e4563 2 * Copyright (c) 2013-2013 ygarcia, MIT License
Yann 0:17cee87e4563 3 *
Yann 0:17cee87e4563 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Yann 0:17cee87e4563 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Yann 0:17cee87e4563 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Yann 0:17cee87e4563 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Yann 0:17cee87e4563 8 * furnished to do so, subject to the following conditions:
Yann 0:17cee87e4563 9 *
Yann 0:17cee87e4563 10 * The above copyright notice and this permission notice shall be included in all copies or
Yann 0:17cee87e4563 11 * substantial portions of the Software.
Yann 0:17cee87e4563 12 *
Yann 0:17cee87e4563 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Yann 0:17cee87e4563 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Yann 0:17cee87e4563 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Yann 0:17cee87e4563 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Yann 0:17cee87e4563 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Yann 0:17cee87e4563 18 */
Yann 0:17cee87e4563 19 #if !defined(__MCP320x_SPI_H__)
Yann 0:17cee87e4563 20 #define __MCP320x_SPI_H__
Yann 0:17cee87e4563 21
Yann 0:17cee87e4563 22 #include <string>
Yann 0:17cee87e4563 23 #include <vector>
Yann 0:17cee87e4563 24
Yann 0:17cee87e4563 25 #include "Debug.h" // Include mbed header + debug primitives. See DebugLibrary
Yann 0:17cee87e4563 26
Yann 0:17cee87e4563 27 namespace MCP320x_SPI {
Yann 0:17cee87e4563 28
Yann 0:17cee87e4563 29 /** This class provides simplified SPI access to a Microchip MCP320x 12-Bit A/D Converter with SPI Serial Interface device. V0.0.0.1
Yann 0:17cee87e4563 30 *
Yann 0:17cee87e4563 31 * Microchip MCP42xxx/MCP41xxx Serial EEPROM device reference: DS11195C
Yann 0:17cee87e4563 32 *
Yann 0:17cee87e4563 33 * Note that MCP3201 has no SI pin, only a SO output pin
Yann 0:17cee87e4563 34 * Note that for SPI details, please visit http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
Yann 0:17cee87e4563 35 *
Yann 0:17cee87e4563 36 * @remark This class was validated with Tektronix TDS2014 oscilloscope in 3.3V
Yann 0:17cee87e4563 37 * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com)
Yann 0:17cee87e4563 38 */
Yann 0:17cee87e4563 39 class CMCP320x_SPI {
Yann 0:17cee87e4563 40 /** Reference counter used to guarentee unicity of the instance of SPI class
Yann 0:17cee87e4563 41 */
Yann 0:17cee87e4563 42 static unsigned char SPIModuleRefCounter;
Yann 0:17cee87e4563 43
Yann 0:17cee87e4563 44 /** ChipSelect (pin 1) see DS21290F-page 15 Clause 3.3 Chip Select/Shutdown (CS/SHDN)
Yann 0:17cee87e4563 45 */
Yann 0:17cee87e4563 46 DigitalOut *_cs;
Yann 0:17cee87e4563 47
Yann 0:17cee87e4563 48 /** An unique instance of SPI class
Yann 0:17cee87e4563 49 */
Yann 0:17cee87e4563 50 SPI *_spiInstance;
Yann 0:17cee87e4563 51
Yann 0:17cee87e4563 52 /** ADC sample structure
Yann 0:17cee87e4563 53 */
Yann 0:17cee87e4563 54 typedef union {
Yann 0:17cee87e4563 55 unsigned int value;
Yann 0:17cee87e4563 56 struct {
Yann 0:17cee87e4563 57 unsigned char bytes[2];
Yann 0:17cee87e4563 58 };
Yann 0:17cee87e4563 59 } ADCValue;
Yann 0:17cee87e4563 60 ADCValue _sample;
Yann 0:17cee87e4563 61 /** Number of channels according to the IC type
Yann 0:17cee87e4563 62 */
Yann 0:17cee87e4563 63 unsigned char _channelsNum;
Yann 0:17cee87e4563 64 /** Set to true for single-ended inputs configuration, false for pseudo-differential inputs
Yann 0:17cee87e4563 65 * @see DS21298E-page 19 Clause 5.0 SERIAL COMMUNICATIONS
Yann 0:17cee87e4563 66 */
Yann 0:17cee87e4563 67 unsigned char _settings;
Yann 0:17cee87e4563 68 public:
Yann 0:17cee87e4563 69 /** MCP320x familly
Yann 0:17cee87e4563 70 */
Yann 0:17cee87e4563 71 enum Mcp320xFamilly {
Yann 0:17cee87e4563 72 _3201 = 0x00, /** See DS21290F */
Yann 0:17cee87e4563 73 _3204 = 0x01, /** See DS21298E */
Yann 0:17cee87e4563 74 _3208 = 0x03 /** See DS21298E */
Yann 0:17cee87e4563 75 };
Yann 0:17cee87e4563 76 Mcp320xFamilly _familly;
Yann 0:17cee87e4563 77 /** MCP320x channels to read
Yann 0:17cee87e4563 78 */
Yann 0:17cee87e4563 79 enum Mcp320xChannels {
Yann 0:17cee87e4563 80 CH0 = 0x00, /** See DS21290F/DS21290F */
Yann 0:17cee87e4563 81 CH1 = 0x01, /** See DS21298E */
Yann 0:17cee87e4563 82 CH2 = 0x02, /** See DS21298E */
Yann 0:17cee87e4563 83 CH3 = 0x03, /** See DS21298E */
Yann 0:17cee87e4563 84 CH4 = 0x04, /** See DS21298E */
Yann 0:17cee87e4563 85 CH5 = 0x05, /** See DS21298E */
Yann 0:17cee87e4563 86 CH6 = 0x06, /** See DS21298E */
Yann 0:17cee87e4563 87 CH7 = 0x07 /** See DS21298E */
Yann 0:17cee87e4563 88 };
Yann 0:17cee87e4563 89 public:
Yann 0:17cee87e4563 90 /** Constructor with Write Protect command pin wired.
Yann 0:17cee87e4563 91 *
Yann 0:17cee87e4563 92 * @param p_mosi: MBed pin for SDI
Yann 0:17cee87e4563 93 * @param p_miso: MBed pin for SDO
Yann 0:17cee87e4563 94 * @param p_sclk: MBed pin for CLK
Yann 0:17cee87e4563 95 * @param p_cs : MBed pin for Chip Select. If NC, assumes that application manage /CS, default value is NC, not connected
Yann 0:17cee87e4563 96 * @param p_familly: MCP320x familly. Default: _3201
Yann 0:17cee87e4563 97 * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz
Yann 0:17cee87e4563 98 */
Yann 0:17cee87e4563 99 CMCP320x_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs = NC, const Mcp320xFamilly p_familly = _3201, const unsigned int p_frequency = 1000000);
Yann 0:17cee87e4563 100
Yann 0:17cee87e4563 101 /** Destructor
Yann 0:17cee87e4563 102 * If managed, the /CS pin is set to 1 before to release it
Yann 0:17cee87e4563 103 */
Yann 0:17cee87e4563 104 virtual ~CMCP320x_SPI();
Yann 0:17cee87e4563 105
Yann 0:17cee87e4563 106 /** Used to return the unique instance of SPI instance
Yann 0:17cee87e4563 107 */
Yann 0:17cee87e4563 108 inline const SPI * operator * () { return (const SPI *)_spiInstance; };
Yann 0:17cee87e4563 109
Yann 0:17cee87e4563 110 /**
Yann 0:17cee87e4563 111 * @desc Launch an analog to digital conversion on the specified channel
Yann 0:17cee87e4563 112 * @param p_channel The channel to convert
Yann 0:17cee87e4563 113 * @return The converted value
Yann 0:17cee87e4563 114 */
Yann 0:17cee87e4563 115 float Read(const Mcp320xChannels p_channels = CH1);
Yann 0:17cee87e4563 116
Yann 0:17cee87e4563 117 /**
Yann 0:17cee87e4563 118 * @desc Change current configuration (only for MCP3204/8)
Yann 0:17cee87e4563 119 * @param p_setConfig Set to true for single-ended inputs configuration, false for pseudo-differential inputs
Yann 0:17cee87e4563 120 * @see DS21298E-page 17 Clause 4.1 Analog Inputs
Yann 0:17cee87e4563 121 */
Yann 0:17cee87e4563 122 void SetConfig(const bool p_settings);
Yann 0:17cee87e4563 123
Yann 0:17cee87e4563 124 /** Shutdown the device
Yann 0:17cee87e4563 125 */
Yann 0:17cee87e4563 126 bool Shutdown(const bool p_shutdown);
Yann 0:17cee87e4563 127
Yann 0:17cee87e4563 128 private:
Yann 0:17cee87e4563 129 /** Internal reference identifier
Yann 0:17cee87e4563 130 */
Yann 0:17cee87e4563 131 std::string _internalId;
Yann 0:17cee87e4563 132
Yann 0:17cee87e4563 133 private:
Yann 0:17cee87e4563 134
Yann 0:17cee87e4563 135 /**
Yann 0:17cee87e4563 136 * @desc Launch an analog to digital conversion on the specified channel for MCP3201
Yann 0:17cee87e4563 137 * @see DS21290F-page 17 Clause 4.1 Analog Inputs
Yann 0:17cee87e4563 138 */
Yann 0:17cee87e4563 139 void Read_3201();
Yann 0:17cee87e4563 140
Yann 0:17cee87e4563 141 /**
Yann 0:17cee87e4563 142 * @desc Launch an analog to digital conversion on the specified channel for MCP3204/8
Yann 0:17cee87e4563 143 * @param p_setConfig Set to true for single-ended inputs configuration, false for pseudo-differential inputs
Yann 0:17cee87e4563 144 * @see DS21298E-page 17 Clause 4.1 Analog Inputs
Yann 0:17cee87e4563 145 */
Yann 0:17cee87e4563 146 void Read_320x(const Mcp320xChannels p_channels);
Yann 0:17cee87e4563 147
Yann 0:17cee87e4563 148 }; // End of class CMCP320x_SPI
Yann 0:17cee87e4563 149
Yann 0:17cee87e4563 150 } // End of namespace MCP320x_SPI
Yann 0:17cee87e4563 151
Yann 0:17cee87e4563 152 using namespace MCP320x_SPI;
Yann 0:17cee87e4563 153
Yann 0:17cee87e4563 154 #endif // __MCP320x_SPI_H__