Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices
Dependents: MCP41xxxApp MCP320xApp MCP41xxxApp
MCP4xxxx_SPI.cpp@6:ded0d8a6729c, 2013-06-06 (annotated)
- Committer:
- Yann
- Date:
- Thu Jun 06 07:59:16 2013 +0000
- Revision:
- 6:ded0d8a6729c
- Parent:
- 5:4f6133144e7e
- Child:
- 7:12a0d89aa72f
Validate support of _42xx familly
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yann | 6:ded0d8a6729c | 1 | /* mbed simplified access to Microchip 24LCxx Serial EEPROM devices (SPI) |
Yann | 1:cf3cee91eb87 | 2 | * Copyright (c) 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 | |
Yann | 0:03314ad622d6 | 20 | #include "MCP4xxxx_SPI.h" |
Yann | 0:03314ad622d6 | 21 | |
Yann | 0:03314ad622d6 | 22 | namespace MCP4xxxx_SPI { |
Yann | 0:03314ad622d6 | 23 | |
Yann | 0:03314ad622d6 | 24 | unsigned char CMCP4xxxx_SPI::SPIModuleRefCounter = 0; |
Yann | 0:03314ad622d6 | 25 | |
Yann | 6:ded0d8a6729c | 26 | CMCP4xxxx_SPI::CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs, const PinName p_reset, const PinName p_shdn, const Mcp4xxxFamilly p_familly, const unsigned int p_frequency) : _internalId("") { |
Yann | 0:03314ad622d6 | 27 | DEBUG_ENTER("CMCP4xxxx_SPI") |
Yann | 0:03314ad622d6 | 28 | |
Yann | 6:ded0d8a6729c | 29 | if (CMCP4xxxx_SPI::SPIModuleRefCounter != 0) { |
Yann | 6:ded0d8a6729c | 30 | error("CMCP4xxxx_SPI: Wrong params"); |
Yann | 0:03314ad622d6 | 31 | } |
Yann | 0:03314ad622d6 | 32 | |
Yann | 0:03314ad622d6 | 33 | _spiInstance = new SPI(p_mosi, p_miso, p_sclk); |
Yann | 0:03314ad622d6 | 34 | _spiInstance->frequency(p_frequency); // Set the frequency of the SPI interface |
Yann | 0:03314ad622d6 | 35 | _spiInstance->format(16, 0); // See http://mbed.org/users/mbed_official/code/mbed/docs/0954ebd79f59//classmbed_1_1SPI.html |
Yann | 6:ded0d8a6729c | 36 | CMCP4xxxx_SPI::SPIModuleRefCounter += 1; |
Yann | 6:ded0d8a6729c | 37 | DEBUG_ENTER("CMCP4xxxx_SPI: refCounter=%d", CMCP4xxxx_SPI::SPIModuleRefCounter) |
Yann | 0:03314ad622d6 | 38 | |
Yann | 2:7c27fb9785be | 39 | if (p_cs != NC) { |
Yann | 2:7c27fb9785be | 40 | DEBUG("CMCP4xxxx_SPI: /CS managed"); |
Yann | 2:7c27fb9785be | 41 | _cs = new DigitalOut(p_cs); |
Yann | 2:7c27fb9785be | 42 | _cs->write(1); // Disable chip |
Yann | 2:7c27fb9785be | 43 | } else { |
Yann | 2:7c27fb9785be | 44 | DEBUG("CMCP4xxxx_SPI: /CS not managed"); |
Yann | 2:7c27fb9785be | 45 | _cs = NULL; // Not used |
Yann | 2:7c27fb9785be | 46 | } |
Yann | 2:7c27fb9785be | 47 | |
Yann | 0:03314ad622d6 | 48 | if (p_reset != NC) { |
Yann | 0:03314ad622d6 | 49 | DEBUG("CMCP4xxxx_SPI: /RESET managed"); |
Yann | 0:03314ad622d6 | 50 | _reset = new DigitalOut(p_reset); |
Yann | 0:03314ad622d6 | 51 | _reset->write(1); // Disable reset |
Yann | 0:03314ad622d6 | 52 | } else { |
Yann | 0:03314ad622d6 | 53 | DEBUG("CMCP4xxxx_SPI: /RESET not managed"); |
Yann | 0:03314ad622d6 | 54 | _reset = NULL; // Not used |
Yann | 0:03314ad622d6 | 55 | } |
Yann | 0:03314ad622d6 | 56 | |
Yann | 0:03314ad622d6 | 57 | if (p_shdn != NC) { |
Yann | 0:03314ad622d6 | 58 | DEBUG("CMCP4xxxx_SPI: /SHDN managed"); |
Yann | 0:03314ad622d6 | 59 | _shdn = new DigitalOut(p_shdn); |
Yann | 0:03314ad622d6 | 60 | _shdn->write(1); // Disable shutdown |
Yann | 0:03314ad622d6 | 61 | } else { |
Yann | 0:03314ad622d6 | 62 | DEBUG("CMCP4xxxx_SPI: /SHDN not managed"); |
Yann | 0:03314ad622d6 | 63 | _shdn = NULL; // Not used |
Yann | 0:03314ad622d6 | 64 | } |
Yann | 6:ded0d8a6729c | 65 | |
Yann | 6:ded0d8a6729c | 66 | _familly = p_familly; |
Yann | 6:ded0d8a6729c | 67 | SetFamilly(p_familly); |
Yann | 6:ded0d8a6729c | 68 | |
Yann | 6:ded0d8a6729c | 69 | _incStep = 0x80; // Set to middle step |
Yann | 6:ded0d8a6729c | 70 | |
Yann | 0:03314ad622d6 | 71 | DEBUG_LEAVE("CMCP4xxxx_SPI") |
Yann | 0:03314ad622d6 | 72 | } |
Yann | 0:03314ad622d6 | 73 | |
Yann | 0:03314ad622d6 | 74 | CMCP4xxxx_SPI::~CMCP4xxxx_SPI() { |
Yann | 0:03314ad622d6 | 75 | DEBUG_ENTER("~CMCP4xxxx_SPI") |
Yann | 0:03314ad622d6 | 76 | |
Yann | 0:03314ad622d6 | 77 | // Release I2C instance |
Yann | 0:03314ad622d6 | 78 | DEBUG_ENTER("~CMCP4xxxx_SPI: refCounter=%d", CMCP4xxxx_SPI::SPIModuleRefCounter) |
Yann | 0:03314ad622d6 | 79 | CMCP4xxxx_SPI::SPIModuleRefCounter -= 1; |
Yann | 0:03314ad622d6 | 80 | if (CMCP4xxxx_SPI::SPIModuleRefCounter == 0) { |
Yann | 0:03314ad622d6 | 81 | delete _spiInstance; |
Yann | 0:03314ad622d6 | 82 | _spiInstance = NULL; |
Yann | 0:03314ad622d6 | 83 | } |
Yann | 0:03314ad622d6 | 84 | // Release _reset if required |
Yann | 2:7c27fb9785be | 85 | if (_cs != NULL) { |
Yann | 6:ded0d8a6729c | 86 | _cs->write(0); |
Yann | 2:7c27fb9785be | 87 | delete _cs; |
Yann | 2:7c27fb9785be | 88 | } |
Yann | 2:7c27fb9785be | 89 | // Release _reset if required |
Yann | 0:03314ad622d6 | 90 | if (_reset != NULL) { |
Yann | 0:03314ad622d6 | 91 | _reset->write(0); |
Yann | 0:03314ad622d6 | 92 | delete _reset; |
Yann | 0:03314ad622d6 | 93 | } |
Yann | 2:7c27fb9785be | 94 | // Release _shdn if required |
Yann | 0:03314ad622d6 | 95 | if (_shdn != NULL) { |
Yann | 0:03314ad622d6 | 96 | _shdn->write(0); |
Yann | 0:03314ad622d6 | 97 | delete _shdn; |
Yann | 0:03314ad622d6 | 98 | } |
Yann | 0:03314ad622d6 | 99 | |
Yann | 0:03314ad622d6 | 100 | DEBUG_LEAVE("~CMCP4xxxx_SPI") |
Yann | 0:03314ad622d6 | 101 | } |
Yann | 0:03314ad622d6 | 102 | |
Yann | 6:ded0d8a6729c | 103 | unsigned short CMCP4xxxx_SPI::WriteRegister(const Addresses p_address, const unsigned char p_value) { |
Yann | 6:ded0d8a6729c | 104 | DEBUG_ENTER("CMCP4xxxx_SPI::WriteRegister: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_address, p_value, (unsigned char)_familly) |
Yann | 0:03314ad622d6 | 105 | |
Yann | 1:cf3cee91eb87 | 106 | // Sanity check |
Yann | 6:ded0d8a6729c | 107 | if (p_address == CMCP4xxxx_SPI::Status) { |
Yann | 6:ded0d8a6729c | 108 | // Wrong parameters |
Yann | 6:ded0d8a6729c | 109 | return (unsigned short) -1; |
Yann | 6:ded0d8a6729c | 110 | } |
Yann | 6:ded0d8a6729c | 111 | |
Yann | 6:ded0d8a6729c | 112 | unsigned short command = ((p_address & 0x0f) << 4 | 0x00/*TODO Use 'define' for Write command*/) << 8; |
Yann | 6:ded0d8a6729c | 113 | command |= p_value; |
Yann | 6:ded0d8a6729c | 114 | |
Yann | 6:ded0d8a6729c | 115 | DEBUG("CMCP4xxxx_SPI::WriteRegister: Send command: 0x%04x", command) |
Yann | 6:ded0d8a6729c | 116 | if (_cs != NULL) { |
Yann | 6:ded0d8a6729c | 117 | _cs->write(0); |
Yann | 6:ded0d8a6729c | 118 | } |
Yann | 6:ded0d8a6729c | 119 | unsigned short result = _spiInstance->write(command); |
Yann | 6:ded0d8a6729c | 120 | DEBUG("CMCP4xxxx_SPI::WriteRegister: returned value=%d", result) |
Yann | 6:ded0d8a6729c | 121 | if (_cs != NULL) { |
Yann | 6:ded0d8a6729c | 122 | _cs->write(1); |
Yann | 6:ded0d8a6729c | 123 | } |
Yann | 6:ded0d8a6729c | 124 | |
Yann | 6:ded0d8a6729c | 125 | DEBUG_LEAVE("CMCP4xxxx_SPI::WriteRegister: %d", result & 0x00ff) |
Yann | 6:ded0d8a6729c | 126 | return result & 0x00ff; |
Yann | 6:ded0d8a6729c | 127 | } |
Yann | 6:ded0d8a6729c | 128 | |
Yann | 6:ded0d8a6729c | 129 | unsigned short CMCP4xxxx_SPI::ReadRegister(const Addresses p_address) { |
Yann | 6:ded0d8a6729c | 130 | DEBUG_ENTER("CMCP4xxxx_SPI::ReadRegister: 0x%02x - 0x%02x", (unsigned char)p_address, (unsigned char)_familly) |
Yann | 6:ded0d8a6729c | 131 | |
Yann | 6:ded0d8a6729c | 132 | unsigned short command = ((p_address & 0x0f) << 4 | (0x03/*TODO Use 'define' for Read command*/ << 2)) << 8; |
Yann | 6:ded0d8a6729c | 133 | |
Yann | 6:ded0d8a6729c | 134 | DEBUG("CMCP4xxxx_SPI::ReadRegister: Send command: 0x%04x", command) |
Yann | 6:ded0d8a6729c | 135 | if (_cs != NULL) { |
Yann | 6:ded0d8a6729c | 136 | _cs->write(0); |
Yann | 6:ded0d8a6729c | 137 | } |
Yann | 6:ded0d8a6729c | 138 | unsigned short result = _spiInstance->write(command); |
Yann | 6:ded0d8a6729c | 139 | DEBUG("CMCP4xxxx_SPI::ReadRegister: full result=0x%04x", result) |
Yann | 6:ded0d8a6729c | 140 | if (_cs != NULL) { |
Yann | 6:ded0d8a6729c | 141 | _cs->write(1); |
Yann | 6:ded0d8a6729c | 142 | } |
Yann | 6:ded0d8a6729c | 143 | |
Yann | 6:ded0d8a6729c | 144 | DEBUG_LEAVE("CMCP4xxxx_SPI::ReadRegister: 0x%02x", result & 0x00ff) |
Yann | 6:ded0d8a6729c | 145 | return result & 0x00ff; |
Yann | 6:ded0d8a6729c | 146 | } |
Yann | 6:ded0d8a6729c | 147 | |
Yann | 6:ded0d8a6729c | 148 | unsigned short CMCP4xxxx_SPI::Incrememt(const Commands p_command) { |
Yann | 6:ded0d8a6729c | 149 | // Sanity check |
Yann | 6:ded0d8a6729c | 150 | if (_incStep != 0xff) { |
Yann | 6:ded0d8a6729c | 151 | _incStep += 1; // TODO Change increment 1 by a parametrized increment step |
Yann | 6:ded0d8a6729c | 152 | } |
Yann | 6:ded0d8a6729c | 153 | return Write(p_command, _incStep); |
Yann | 6:ded0d8a6729c | 154 | } |
Yann | 6:ded0d8a6729c | 155 | |
Yann | 6:ded0d8a6729c | 156 | unsigned short CMCP4xxxx_SPI::Decrement(const Commands p_command) { |
Yann | 6:ded0d8a6729c | 157 | // Sanity check |
Yann | 6:ded0d8a6729c | 158 | if (_incStep != 0x00) { |
Yann | 6:ded0d8a6729c | 159 | _incStep -= 1; // TODO Change increment 1 by a parametrized increment step |
Yann | 6:ded0d8a6729c | 160 | } |
Yann | 6:ded0d8a6729c | 161 | return Write(p_command, _incStep); |
Yann | 6:ded0d8a6729c | 162 | } |
Yann | 6:ded0d8a6729c | 163 | |
Yann | 6:ded0d8a6729c | 164 | |
Yann | 6:ded0d8a6729c | 165 | unsigned short CMCP4xxxx_SPI::Write(const Commands p_command, const unsigned char p_value) { |
Yann | 6:ded0d8a6729c | 166 | DEBUG_ENTER("CMCP4xxxx_SPI::Write: 0x%02x - 0x%02x - 0x%02x", (unsigned char)p_command, p_value, (unsigned char)_familly) |
Yann | 6:ded0d8a6729c | 167 | |
Yann | 6:ded0d8a6729c | 168 | // Sanity check |
Yann | 6:ded0d8a6729c | 169 | if ((p_command != WriteToDigiPot1) && (p_command != WriteToDigiPot2) && (p_command != WriteToBoth)) { |
Yann | 1:cf3cee91eb87 | 170 | // Wrong parameters |
Yann | 1:cf3cee91eb87 | 171 | return (unsigned short) -1; |
Yann | 1:cf3cee91eb87 | 172 | } |
Yann | 1:cf3cee91eb87 | 173 | |
Yann | 0:03314ad622d6 | 174 | unsigned short command = 0; |
Yann | 6:ded0d8a6729c | 175 | if ((_familly == CMCP4xxxx_SPI::_41xxx) || (_familly == CMCP4xxxx_SPI::_42xxx)) { // See DS11195C |
Yann | 6:ded0d8a6729c | 176 | switch (p_command) { |
Yann | 6:ded0d8a6729c | 177 | case WriteToDigiPot1: |
Yann | 6:ded0d8a6729c | 178 | command = (0x11 << 8 | p_value); |
Yann | 6:ded0d8a6729c | 179 | break; |
Yann | 6:ded0d8a6729c | 180 | case WriteToDigiPot2: |
Yann | 6:ded0d8a6729c | 181 | command = (0x12 << 8 | p_value); |
Yann | 6:ded0d8a6729c | 182 | break; |
Yann | 6:ded0d8a6729c | 183 | default: |
Yann | 6:ded0d8a6729c | 184 | command = (0x13 << 8 | p_value); |
Yann | 6:ded0d8a6729c | 185 | } // End of 'switch' statement |
Yann | 6:ded0d8a6729c | 186 | } else { // See DS22060B |
Yann | 6:ded0d8a6729c | 187 | switch (p_command) { |
Yann | 6:ded0d8a6729c | 188 | case WriteToDigiPot2: // Wiper1: adress=0x01, WriteCmd=00 |
Yann | 6:ded0d8a6729c | 189 | command = (0x01 << 5 | p_value); |
Yann | 6:ded0d8a6729c | 190 | break; |
Yann | 6:ded0d8a6729c | 191 | default: |
Yann | 6:ded0d8a6729c | 192 | command = p_value; // Wiper1: adress=0x00, WriteCmd=00 |
Yann | 6:ded0d8a6729c | 193 | } // End of 'switch' statement |
Yann | 6:ded0d8a6729c | 194 | } |
Yann | 1:cf3cee91eb87 | 195 | |
Yann | 6:ded0d8a6729c | 196 | DEBUG("CMCP4xxxx_SPI::Write: Send command: 0x%04x", (unsigned char)command, (unsigned char)_familly) |
Yann | 2:7c27fb9785be | 197 | if (_cs != NULL) { |
Yann | 2:7c27fb9785be | 198 | _cs->write(0); |
Yann | 2:7c27fb9785be | 199 | } |
Yann | 1:cf3cee91eb87 | 200 | unsigned short result = _spiInstance->write(command); |
Yann | 2:7c27fb9785be | 201 | if (_cs != NULL) { |
Yann | 2:7c27fb9785be | 202 | _cs->write(1); |
Yann | 2:7c27fb9785be | 203 | } |
Yann | 1:cf3cee91eb87 | 204 | |
Yann | 1:cf3cee91eb87 | 205 | DEBUG_LEAVE("CMCP4xxxx_SPI::Write: %d", result) |
Yann | 1:cf3cee91eb87 | 206 | return result; |
Yann | 1:cf3cee91eb87 | 207 | } |
Yann | 1:cf3cee91eb87 | 208 | |
Yann | 6:ded0d8a6729c | 209 | unsigned short CMCP4xxxx_SPI::Shutdown(const Commands p_command, const bool p_set) { |
Yann | 6:ded0d8a6729c | 210 | DEBUG_ENTER("CMCP4xxxx_SPI::Shutdown: 0x%02x - 0x%02x", (unsigned char)p_command, (unsigned char)_familly) |
Yann | 1:cf3cee91eb87 | 211 | |
Yann | 1:cf3cee91eb87 | 212 | // Sanity check |
Yann | 6:ded0d8a6729c | 213 | if ((p_command != ShutdownDigiPot1) && (p_command != ShutdownDigiPot2) && (p_command != ShutdownBoth)) { |
Yann | 1:cf3cee91eb87 | 214 | // Wrong parameters |
Yann | 1:cf3cee91eb87 | 215 | return (unsigned short) -1; |
Yann | 1:cf3cee91eb87 | 216 | } |
Yann | 1:cf3cee91eb87 | 217 | |
Yann | 1:cf3cee91eb87 | 218 | unsigned short command = 0; |
Yann | 6:ded0d8a6729c | 219 | if ((_familly == CMCP4xxxx_SPI::_41xxx) || (_familly == CMCP4xxxx_SPI::_42xxx)) { // See DS11195C |
Yann | 6:ded0d8a6729c | 220 | switch (p_command) { |
Yann | 6:ded0d8a6729c | 221 | case ShutdownDigiPot1: |
Yann | 6:ded0d8a6729c | 222 | command = (0x21 << 8); |
Yann | 6:ded0d8a6729c | 223 | break; |
Yann | 6:ded0d8a6729c | 224 | case ShutdownDigiPot2: |
Yann | 6:ded0d8a6729c | 225 | command = (0x21 << 8); |
Yann | 6:ded0d8a6729c | 226 | break; |
Yann | 6:ded0d8a6729c | 227 | default: //<! Shutdown both digital potentiometers |
Yann | 6:ded0d8a6729c | 228 | command = (0x23 << 8); |
Yann | 6:ded0d8a6729c | 229 | } // End of 'switch' statement |
Yann | 6:ded0d8a6729c | 230 | } else { // See DS22060B |
Yann | 6:ded0d8a6729c | 231 | unsigned short tcon = ReadRegister(TCon); // Read TCon register |
Yann | 6:ded0d8a6729c | 232 | if (p_set == true) { |
Yann | 6:ded0d8a6729c | 233 | switch (p_command) { |
Yann | 6:ded0d8a6729c | 234 | case ShutdownDigiPot1: |
Yann | 6:ded0d8a6729c | 235 | command = 0x4000 | (tcon & 0xf8); |
Yann | 6:ded0d8a6729c | 236 | break; |
Yann | 6:ded0d8a6729c | 237 | case ShutdownDigiPot2: |
Yann | 6:ded0d8a6729c | 238 | command = 0x4000 | (tcon & 0x8f); |
Yann | 6:ded0d8a6729c | 239 | break; |
Yann | 6:ded0d8a6729c | 240 | default: //<! Shutdown both digital potentiometers |
Yann | 6:ded0d8a6729c | 241 | command = 0x4000; |
Yann | 6:ded0d8a6729c | 242 | } // End of 'switch' statement |
Yann | 6:ded0d8a6729c | 243 | } else { |
Yann | 6:ded0d8a6729c | 244 | switch (p_command) { |
Yann | 6:ded0d8a6729c | 245 | case ShutdownDigiPot1: |
Yann | 6:ded0d8a6729c | 246 | command = 0x4000 | (tcon | 0x000f); |
Yann | 6:ded0d8a6729c | 247 | break; |
Yann | 6:ded0d8a6729c | 248 | case ShutdownDigiPot2: |
Yann | 6:ded0d8a6729c | 249 | command = 0x4000 | (tcon | 0x00f0); |
Yann | 6:ded0d8a6729c | 250 | break; |
Yann | 6:ded0d8a6729c | 251 | default: //<! Shutdown both digital potentiometers |
Yann | 6:ded0d8a6729c | 252 | command = 0x40ff; |
Yann | 6:ded0d8a6729c | 253 | } // End of 'switch' statement |
Yann | 6:ded0d8a6729c | 254 | } |
Yann | 6:ded0d8a6729c | 255 | } |
Yann | 0:03314ad622d6 | 256 | |
Yann | 6:ded0d8a6729c | 257 | DEBUG("CMCP4xxxx_SPI::Shutdown: Send command: 0x%04x", command) |
Yann | 2:7c27fb9785be | 258 | if (_cs != NULL) { |
Yann | 2:7c27fb9785be | 259 | _cs->write(0); |
Yann | 2:7c27fb9785be | 260 | } |
Yann | 0:03314ad622d6 | 261 | unsigned short result = _spiInstance->write(command); |
Yann | 2:7c27fb9785be | 262 | if (_cs != NULL) { |
Yann | 2:7c27fb9785be | 263 | _cs->write(1); |
Yann | 2:7c27fb9785be | 264 | } |
Yann | 0:03314ad622d6 | 265 | |
Yann | 6:ded0d8a6729c | 266 | DEBUG_LEAVE("CMCP4xxxx_SPI::Shutdown: %d", result) |
Yann | 0:03314ad622d6 | 267 | return result; |
Yann | 0:03314ad622d6 | 268 | } |
Yann | 0:03314ad622d6 | 269 | unsigned short CMCP4xxxx_SPI::Write() { |
Yann | 0:03314ad622d6 | 270 | return _spiInstance->write(0); |
Yann | 0:03314ad622d6 | 271 | } |
Yann | 6:ded0d8a6729c | 272 | |
Yann | 6:ded0d8a6729c | 273 | CMCP4xxxx_SPI::Mcp4xxxFamilly CMCP4xxxx_SPI::SetFamilly(const CMCP4xxxx_SPI::Mcp4xxxFamilly p_familly) { |
Yann | 6:ded0d8a6729c | 274 | DEBUG_ENTER("CMCP4xxxx_SPI::SetFamilly: 0x%02x", (unsigned char)p_familly) |
Yann | 6:ded0d8a6729c | 275 | |
Yann | 6:ded0d8a6729c | 276 | Mcp4xxxFamilly old = _familly; |
Yann | 6:ded0d8a6729c | 277 | _familly = p_familly; |
Yann | 6:ded0d8a6729c | 278 | if ((_familly != CMCP4xxxx_SPI::_41xxx) && (_familly != CMCP4xxxx_SPI::_42xxx)) { |
Yann | 6:ded0d8a6729c | 279 | // Setup TCON register |
Yann | 6:ded0d8a6729c | 280 | DEBUG("CMCP4xxxx_SPI::SetFamilly: Setup TCON register") |
Yann | 6:ded0d8a6729c | 281 | WriteRegister(CMCP4xxxx_SPI::TCon, 0x7f); // See DS22060B-page 36 REGISTER 4-2: TCON BITS |
Yann | 6:ded0d8a6729c | 282 | // TODO Use 'define' for command |
Yann | 0:03314ad622d6 | 283 | } |
Yann | 0:03314ad622d6 | 284 | |
Yann | 6:ded0d8a6729c | 285 | DEBUG_LEAVE("CMCP4xxxx_SPI::SetFamilly: 0x%02x", (unsigned char)old) |
Yann | 6:ded0d8a6729c | 286 | return old; |
Yann | 6:ded0d8a6729c | 287 | } |
Yann | 6:ded0d8a6729c | 288 | |
Yann | 6:ded0d8a6729c | 289 | void CMCP4xxxx_SPI::Reset() { |
Yann | 6:ded0d8a6729c | 290 | _incStep = 0x80; // Set to middle step |
Yann | 6:ded0d8a6729c | 291 | // Sanity check |
Yann | 6:ded0d8a6729c | 292 | if (_reset != NULL) { |
Yann | 6:ded0d8a6729c | 293 | _reset->write(0); // Set level low to activate reset |
Yann | 6:ded0d8a6729c | 294 | wait_us(1); // Wait for 1us |
Yann | 6:ded0d8a6729c | 295 | _reset->write(1); // Set level low to de-activate reset |
Yann | 6:ded0d8a6729c | 296 | } |
Yann | 0:03314ad622d6 | 297 | } |
Yann | 0:03314ad622d6 | 298 | |
Yann | 6:ded0d8a6729c | 299 | void CMCP4xxxx_SPI::Shutdown(const bool p_set) { |
Yann | 6:ded0d8a6729c | 300 | if (_shdn != NULL) { |
Yann | 6:ded0d8a6729c | 301 | _shdn->write(p_set == false ? 0 : 1); |
Yann | 0:03314ad622d6 | 302 | } |
Yann | 0:03314ad622d6 | 303 | } |
Yann | 0:03314ad622d6 | 304 | |
Yann | 0:03314ad622d6 | 305 | } // End of namespace MCP4xxxx_SPI |