Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices
Dependents: MCP41xxxApp MCP320xApp MCP41xxxApp
Diff: MCP4xxxx_SPI.cpp
- Revision:
- 2:7c27fb9785be
- Parent:
- 1:cf3cee91eb87
- Child:
- 4:bbfc8e352ff5
--- a/MCP4xxxx_SPI.cpp Sun Jan 27 17:04:05 2013 +0000 +++ b/MCP4xxxx_SPI.cpp Tue Jan 29 15:02:08 2013 +0000 @@ -23,7 +23,7 @@ unsigned char CMCP4xxxx_SPI::SPIModuleRefCounter = 0; - CMCP4xxxx_SPI::CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_reset, const PinName p_shdn, const unsigned int p_frequency) : _internalId("") { + 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 unsigned int p_frequency) : _internalId("") { DEBUG_ENTER("CMCP4xxxx_SPI") if (CMCP4xxxx_SPI::SPIModuleRefCounter != 0) { @@ -36,6 +36,15 @@ CMCP4xxxx_SPI::SPIModuleRefCounter += 1; DEBUG_ENTER("CMCP4xxxx_SPI: refCounter=%d", CMCP4xxxx_SPI::SPIModuleRefCounter) + if (p_cs != NC) { + DEBUG("CMCP4xxxx_SPI: /CS managed"); + _cs = new DigitalOut(p_cs); + _cs->write(1); // Disable chip + } else { + DEBUG("CMCP4xxxx_SPI: /CS not managed"); + _cs = NULL; // Not used + } + if (p_reset != NC) { DEBUG("CMCP4xxxx_SPI: /RESET managed"); _reset = new DigitalOut(p_reset); @@ -68,11 +77,16 @@ _spiInstance = NULL; } // Release _reset if required + if (_cs != NULL) { + _cs->write(0); + delete _cs; + } + // Release _reset if required if (_reset != NULL) { _reset->write(0); delete _reset; } - // Release _shdn if required + // Release _shdn if required if (_shdn != NULL) { _shdn->write(0); delete _shdn; @@ -103,7 +117,13 @@ } // End of 'switch' statement DEBUG("CMCP4xxxx_SPI: Send command: 0x%04x", command) + if (_cs != NULL) { + _cs->write(0); + } unsigned short result = _spiInstance->write(command); + if (_cs != NULL) { + _cs->write(1); + } DEBUG_LEAVE("CMCP4xxxx_SPI::Write: %d", result) return result; @@ -131,7 +151,13 @@ } // End of 'switch' statement DEBUG("CMCP4xxxx_SPI: Send command: 0x%04x", command) + if (_cs != NULL) { + _cs->write(0); + } unsigned short result = _spiInstance->write(command); + if (_cs != NULL) { + _cs->write(1); + } DEBUG_LEAVE("CMCP4xxxx_SPI::Write: %d", result) return result; @@ -148,7 +174,7 @@ _reset->write(0); // Set level low to activate reset wait_us(1); // Wait for 1us - _reset->write(1); // Set level low to de-activate reset + _reset->write(1); // Set level low to de-activate reset return true; }