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

Dependents:   MCP320xApp

Committer:
Yann
Date:
Thu Sep 05 07:22:02 2013 +0000
Revision:
2:a273b5fcad50
Parent:
1:f0a81abeaef5
Minor bug fixed; Method name changed: Incrememt -> Increment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:17cee87e4563 1 /* mbed simplified access to Microchip 12 bits ADC devices (SPI)
Yann 0:17cee87e4563 2 * Copyright (c) 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
Yann 0:17cee87e4563 20 #include "MCP320x_SPI.h"
Yann 0:17cee87e4563 21
Yann 0:17cee87e4563 22 namespace MCP320x_SPI {
Yann 0:17cee87e4563 23
Yann 0:17cee87e4563 24 unsigned char CMCP320x_SPI::SPIModuleRefCounter = 0;
Yann 0:17cee87e4563 25
Yann 0:17cee87e4563 26 CMCP320x_SPI::CMCP320x_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs, const Mcp320xFamilly p_familly, const unsigned int p_frequency) : _internalId("") {
Yann 0:17cee87e4563 27 DEBUG_ENTER("CMCP320x_SPI")
Yann 0:17cee87e4563 28
Yann 0:17cee87e4563 29 CMCP320x_SPI::SPIModuleRefCounter += 1;
Yann 0:17cee87e4563 30 if (CMCP320x_SPI::SPIModuleRefCounter > 1) {
Yann 0:17cee87e4563 31 //FIXME Check that SPI settings are identical. Otherwise it should failed
Yann 0:17cee87e4563 32 return;
Yann 0:17cee87e4563 33 }
Yann 0:17cee87e4563 34
Yann 0:17cee87e4563 35 _familly = p_familly;
Yann 0:17cee87e4563 36 switch (_familly) {
Yann 0:17cee87e4563 37 case _3201:
Yann 2:a273b5fcad50 38 _channelsNum = 1;
Yann 0:17cee87e4563 39 break;
Yann 0:17cee87e4563 40 case _3204:
Yann 0:17cee87e4563 41 _channelsNum = 4;
Yann 0:17cee87e4563 42 break;
Yann 0:17cee87e4563 43 default: // _3208
Yann 0:17cee87e4563 44 _channelsNum = 8;
Yann 0:17cee87e4563 45 } // End of 'switch' statement
Yann 0:17cee87e4563 46 _settings = 0x02; // SGL/DIFF bit set to 1 = See DS21298E-page 19 TABLE 5-1: CONFIGURATION BITS FOR THE MCP3204/TABLE 5-2: CONFIGURATION BITS FOR THE MCP3208
Yann 0:17cee87e4563 47 DEBUG("CMCP320x_SPI: familly:%d - #channels:%d", _familly, _channelsNum)
Yann 0:17cee87e4563 48 _spiInstance = new SPI(p_mosi, p_miso, p_sclk);
Yann 0:17cee87e4563 49 _spiInstance->frequency(p_frequency); // Set the frequency of the SPI interface
Yann 0:17cee87e4563 50 _spiInstance->format(8, 3);
Yann 0:17cee87e4563 51 DEBUG_ENTER("CMCP320x_SPI: refCounter=%d", CMCP320x_SPI::SPIModuleRefCounter)
Yann 0:17cee87e4563 52
Yann 0:17cee87e4563 53 if (p_cs != NC) {
Yann 0:17cee87e4563 54 DEBUG("CMCP320x_SPI: /CS managed");
Yann 0:17cee87e4563 55 _cs = new DigitalOut(p_cs);
Yann 0:17cee87e4563 56 _cs->write(1); // Disable chip
Yann 0:17cee87e4563 57 } else {
Yann 0:17cee87e4563 58 DEBUG("CMCP320x_SPI: /CS not managed");
Yann 0:17cee87e4563 59 _cs = NULL; // Not used
Yann 0:17cee87e4563 60 }
Yann 0:17cee87e4563 61
Yann 0:17cee87e4563 62
Yann 0:17cee87e4563 63 DEBUG_LEAVE("CMCP320x_SPI")
Yann 0:17cee87e4563 64 }
Yann 0:17cee87e4563 65
Yann 0:17cee87e4563 66 CMCP320x_SPI::~CMCP320x_SPI() {
Yann 0:17cee87e4563 67 DEBUG_ENTER("~CMCP320x_SPI")
Yann 0:17cee87e4563 68
Yann 0:17cee87e4563 69 // Release I2C instance
Yann 0:17cee87e4563 70 DEBUG_ENTER("~CMCP320x_SPI: refCounter=%d", CMCP320x_SPI::SPIModuleRefCounter)
Yann 0:17cee87e4563 71 CMCP320x_SPI::SPIModuleRefCounter -= 1;
Yann 0:17cee87e4563 72 if (CMCP320x_SPI::SPIModuleRefCounter == 0) {
Yann 0:17cee87e4563 73 delete _spiInstance;
Yann 0:17cee87e4563 74 _spiInstance = NULL;
Yann 0:17cee87e4563 75 }
Yann 0:17cee87e4563 76 // Release _reset if required
Yann 0:17cee87e4563 77 if (_cs != NULL) {
Yann 0:17cee87e4563 78 _cs->write(1);
Yann 0:17cee87e4563 79 delete _cs;
Yann 0:17cee87e4563 80 }
Yann 0:17cee87e4563 81
Yann 0:17cee87e4563 82 DEBUG_LEAVE("~CMCP320x_SPI")
Yann 0:17cee87e4563 83 }
Yann 0:17cee87e4563 84
Yann 0:17cee87e4563 85 float CMCP320x_SPI::Read(const Mcp320xChannels p_channels) {
Yann 0:17cee87e4563 86 DEBUG_ENTER("CMCP320x_SPI::Read: %d", (unsigned char)p_channels)
Yann 0:17cee87e4563 87
Yann 0:17cee87e4563 88 // Read a sample
Yann 0:17cee87e4563 89 _sample.value = 0x00;
Yann 0:17cee87e4563 90 switch (_familly) {
Yann 0:17cee87e4563 91 case _3204:
Yann 0:17cee87e4563 92 // No break;
Yann 0:17cee87e4563 93 case _3208:
Yann 0:17cee87e4563 94 Read_320x(p_channels);
Yann 0:17cee87e4563 95 break;
Yann 0:17cee87e4563 96 default: // _3201
Yann 0:17cee87e4563 97 Read_3201();
Yann 0:17cee87e4563 98 break;
Yann 0:17cee87e4563 99 } // End of 'switch' statement
Yann 0:17cee87e4563 100 DEBUG("CMCP320x_SPI::Read: 0x%02x - 0x%02x", _sample.bytes[0], _sample.bytes[1])
Yann 0:17cee87e4563 101 // Convert it
Yann 0:17cee87e4563 102 float temp;
Yann 0:17cee87e4563 103 _sample.value >>= 1; // Adjust composite integer for 12 valid bits
Yann 0:17cee87e4563 104 _sample.value &= 0x0FFF; // Mask out upper nibble of integer
Yann 0:17cee87e4563 105 temp = (_sample.value * 0.001225585);
Yann 0:17cee87e4563 106
Yann 0:17cee87e4563 107 DEBUG_LEAVE("CMCP320x_SPI::Read: %f", temp)
Yann 0:17cee87e4563 108 return temp;
Yann 0:17cee87e4563 109 }
Yann 0:17cee87e4563 110
Yann 0:17cee87e4563 111 void CMCP320x_SPI::SetConfig(const bool p_settings) {
Yann 0:17cee87e4563 112 DEBUG_LEAVE("CMCP320x_SPI::SetConfig: %x", (unsigned char)p_settings)
Yann 0:17cee87e4563 113
Yann 0:17cee87e4563 114 if (_settings) {
Yann 0:17cee87e4563 115 _settings = 0x02;
Yann 0:17cee87e4563 116 } else {
Yann 0:17cee87e4563 117 _settings = 0x00;
Yann 0:17cee87e4563 118 }
Yann 0:17cee87e4563 119 }
Yann 0:17cee87e4563 120
Yann 0:17cee87e4563 121 bool CMCP320x_SPI::Shutdown(const bool p_shutdown) {
Yann 0:17cee87e4563 122 // Sanity check
Yann 0:17cee87e4563 123 if (_cs == NULL) {
Yann 0:17cee87e4563 124 return false;
Yann 0:17cee87e4563 125 }
Yann 0:17cee87e4563 126
Yann 0:17cee87e4563 127 _cs->write(p_shutdown == false ? 0 : 1);
Yann 0:17cee87e4563 128
Yann 0:17cee87e4563 129 return true;
Yann 0:17cee87e4563 130 }
Yann 0:17cee87e4563 131
Yann 0:17cee87e4563 132 void CMCP320x_SPI::Read_3201() {
Yann 0:17cee87e4563 133 if (_cs != NULL) {
Yann 0:17cee87e4563 134 _cs->write(0);
Yann 0:17cee87e4563 135 wait_us(1);
Yann 0:17cee87e4563 136 }
Yann 0:17cee87e4563 137 _sample.bytes[1] = _spiInstance->write(0);
Yann 0:17cee87e4563 138 _sample.bytes[0] = _spiInstance->write(0);
Yann 0:17cee87e4563 139 if (_cs != NULL) {
Yann 0:17cee87e4563 140 _cs->write(1);
Yann 0:17cee87e4563 141 }
Yann 0:17cee87e4563 142 }
Yann 0:17cee87e4563 143
Yann 0:17cee87e4563 144 void CMCP320x_SPI::Read_320x(const Mcp320xChannels p_channels) {
Yann 0:17cee87e4563 145 DEBUG_ENTER("CMCP320x_SPI::Read_320x: %d", (unsigned char)p_channels)
Yann 0:17cee87e4563 146
Yann 0:17cee87e4563 147 unsigned char _channels = (unsigned char)p_channels % _channelsNum;
Yann 0:17cee87e4563 148 // Set start bit
Yann 0:17cee87e4563 149 unsigned char mask = 0x04 | _settings; // Start bit set to 1 - See DS21298E-page 19 Clause 5.0 SERIAL COMMUNICATIONS
Yann 0:17cee87e4563 150 // Set channel address
Yann 0:17cee87e4563 151 unsigned char cmd0;
Yann 0:17cee87e4563 152 unsigned char cmd1;
Yann 0:17cee87e4563 153 if (_familly == _3204) {
Yann 0:17cee87e4563 154 cmd0 = mask;
Yann 0:17cee87e4563 155 cmd1 = _channels << 6; // MCP3204 has 4 channels in single-ended mode
Yann 0:17cee87e4563 156 } else { // MCP3208
Yann 2:a273b5fcad50 157 cmd0 = mask | ((_channels & 0x04) >> 2); // Extract D2 bit - See DS21298E-page 19 Clause 5.0 SERIAL COMMUNICATIONS
Yann 2:a273b5fcad50 158 cmd1 = _channels << 6; // MCP3204 has 8 channels in single-ended mode
Yann 0:17cee87e4563 159 }
Yann 2:a273b5fcad50 160 DEBUG("CMCP320x_SPI::Read_320x: cmd0:%02x - cmd1:%02x", cmd0, cmd1)
Yann 0:17cee87e4563 161 if (_cs != NULL) {
Yann 0:17cee87e4563 162 _cs->write(0);
Yann 0:17cee87e4563 163 wait_us(1);
Yann 0:17cee87e4563 164 }
Yann 0:17cee87e4563 165 _spiInstance->write(cmd0); // Don't care of the result - See DS21298E-page 21 Clause 6.1 Using the MCP3204/3208 with Microcontroller (MCU) SPI Ports
Yann 0:17cee87e4563 166 _sample.bytes[1] = _spiInstance->write(cmd1); // DS21298E-page 21 See FIGURE 6-1: SPI Communication using 8-bit segments (Mode 0,0: SCLK idles low)
Yann 0:17cee87e4563 167 _sample.bytes[0] = _spiInstance->write(0);
Yann 0:17cee87e4563 168 if (_cs != NULL) {
Yann 0:17cee87e4563 169 _cs->write(1);
Yann 0:17cee87e4563 170 }
Yann 0:17cee87e4563 171
Yann 0:17cee87e4563 172 DEBUG_LEAVE("CMCP320x_SPI::Read_320x")
Yann 0:17cee87e4563 173 }
Yann 0:17cee87e4563 174
Yann 0:17cee87e4563 175 } // End of namespace MCP320x_SPI