Yann Garcia / MCP320x_SPI

Dependents:   MCP320xApp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP320x_SPI.h Source File

MCP320x_SPI.h

00001 /* mbed simplified access to Microchip MCP320x 12 bits ADC devices (SPI)
00002  * Copyright (c) 2013-2013 ygarcia, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or 
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 #if !defined(__MCP320x_SPI_H__)
00020 #define __MCP320x_SPI_H__
00021 
00022 #include <string>
00023 #include <vector>
00024 
00025 #include "Debug.h" // Include mbed header + debug primitives. See DebugLibrary
00026 
00027 namespace MCP320x_SPI {
00028 
00029     /** This class provides simplified SPI access to a Microchip MCP320x 12-Bit A/D Converter with SPI Serial Interface device. V0.0.0.1
00030      *
00031      * Microchip MCP42xxx/MCP41xxx Serial EEPROM device reference: DS11195C
00032      *
00033      * Note that MCP3201 has no SI pin, only a SO output pin
00034      * Note that for SPI details, please visit http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
00035      *
00036      * @remark This class was validated with Tektronix TDS2014 oscilloscope in 3.3V
00037      * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com)
00038      */
00039     class CMCP320x_SPI { 
00040         /** Reference counter used to guarentee unicity of the instance of SPI class
00041          */
00042         static unsigned char SPIModuleRefCounter;
00043         
00044         /** ChipSelect (pin 1) see DS21290F-page 15 Clause 3.3 Chip Select/Shutdown (CS/SHDN)
00045          */
00046         DigitalOut *_cs;
00047         
00048         /** An unique instance of SPI class
00049          */
00050         SPI *_spiInstance;
00051         
00052         /** ADC sample structure
00053          */
00054         typedef union {
00055             unsigned int value;
00056             struct {
00057                 unsigned char bytes[2];
00058             };
00059         } ADCValue;
00060         ADCValue _sample;
00061         /** Number of channels according to the IC type 
00062          */
00063         unsigned char _channelsNum;
00064         /** Set to true for single-ended inputs configuration, false for pseudo-differential inputs
00065          * @see DS21298E-page 19 Clause 5.0 SERIAL COMMUNICATIONS
00066          */
00067         unsigned char _settings;
00068    public:
00069         /** MCP320x familly
00070          */
00071         enum Mcp320xFamilly {
00072             _3201 = 0x00, /** See DS21290F */
00073             _3204 = 0x01, /** See DS21298E */
00074             _3208 = 0x03  /** See DS21298E */
00075         };
00076         Mcp320xFamilly _familly;
00077         /** MCP320x channels to read
00078          */
00079         enum Mcp320xChannels {
00080             CH0 = 0x00, /** See DS21290F/DS21290F */
00081             CH1 = 0x01, /** See DS21298E */
00082             CH2 = 0x02, /** See DS21298E */
00083             CH3 = 0x03, /** See DS21298E */
00084             CH4 = 0x04, /** See DS21298E */
00085             CH5 = 0x05, /** See DS21298E */
00086             CH6 = 0x06, /** See DS21298E */
00087             CH7 = 0x07  /** See DS21298E */
00088         };
00089    public:
00090         /** Constructor with Write Protect command pin wired.
00091          *
00092          * @param p_mosi: MBed pin for SDI
00093          * @param p_miso: MBed pin for SDO
00094          * @param p_sclk: MBed pin for CLK
00095          * @param p_cs  : MBed pin for Chip Select. If NC, assumes that application manage /CS, default value is NC, not connected
00096          * @param p_familly: MCP320x familly. Default: _3201
00097          * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz
00098          */
00099         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);
00100     
00101         /** Destructor
00102          * If managed, the /CS pin is set to 1 before to release it
00103          */
00104         virtual ~CMCP320x_SPI();
00105 
00106         /** Used to return the unique instance of SPI instance
00107          */
00108         inline const SPI * operator * () { return (const SPI *)_spiInstance; };
00109 
00110         /** 
00111          * @desc Launch an analog to digital conversion on the specified channel
00112          * @param p_channel The channel to convert
00113          * @return The converted value
00114          */
00115         float Read(const Mcp320xChannels p_channels = CH1);
00116         
00117         /** 
00118          * @desc Change current configuration (only for MCP3204/8)
00119          * @param p_setConfig Set to true for single-ended inputs configuration, false for pseudo-differential inputs
00120          * @see DS21298E-page 17 Clause 4.1 Analog Inputs
00121          */
00122         void SetConfig(const bool p_settings);
00123     
00124          /** Shutdown the device
00125          */
00126         bool Shutdown(const bool p_shutdown);
00127     
00128    private:
00129         /** Internal reference identifier
00130          */
00131         std::string _internalId;
00132         
00133     private:
00134     
00135         /** 
00136          * @desc Launch an analog to digital conversion on the specified channel for MCP3201
00137          * @see DS21290F-page 17 Clause 4.1 Analog Inputs
00138          */
00139         void Read_3201();
00140         
00141         /** 
00142          * @desc Launch an analog to digital conversion on the specified channel for MCP3204/8
00143          * @param p_setConfig Set to true for single-ended inputs configuration, false for pseudo-differential inputs
00144          * @see DS21298E-page 17 Clause 4.1 Analog Inputs
00145          */
00146         void Read_320x(const Mcp320xChannels p_channels);    
00147         
00148     }; // End of class CMCP320x_SPI
00149 
00150 } // End of namespace MCP320x_SPI
00151 
00152 using namespace MCP320x_SPI;
00153 
00154 #endif // __MCP320x_SPI_H__