Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices
Dependents: MCP41xxxApp MCP320xApp MCP41xxxApp
Revision 2:7c27fb9785be, committed 2013-01-29
- Comitter:
- Yann
- Date:
- Tue Jan 29 15:02:08 2013 +0000
- Parent:
- 1:cf3cee91eb87
- Child:
- 3:0acab5201dd8
- Commit message:
- Add /CS supported by library
Changed in this revision
MCP4xxxx_SPI.cpp | Show annotated file Show diff for this revision Revisions of this file |
MCP4xxxx_SPI.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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; }
--- a/MCP4xxxx_SPI.h Sun Jan 27 17:04:05 2013 +0000 +++ b/MCP4xxxx_SPI.h Tue Jan 29 15:02:08 2013 +0000 @@ -41,12 +41,18 @@ */ static unsigned char SPIModuleRefCounter; - /** Reset state indicator (pin 11); true to reset device, false otherwise (DS11195C-page 21 Clause 5.5 Reset (RS) Pin Operation) + /** ChipSelect (pin 1) see DS11195C-page 12 Clause 3.4 Chip Select (CS) + */ + DigitalOut *_cs; + + /** Reset state indicator (pin 11), see DS11195C-page 21 Clause 5.5 Reset (RS) Pin Operation */ DigitalOut *_reset; - /** Shutdown state indicator (pin 12); true to shutdown device, false otherwise (DS11195C-page 21 5.6 Shutdown (SHDN) Pin Operation) + + /** Shutdown state indicator (pin 12) see DS11195C-page 21 5.6 Shutdown (SHDN) Pin Operation */ DigitalOut *_shdn; + /** An unique instance of SPI class */ SPI *_spiInstance; @@ -68,11 +74,12 @@ * @param p_mosi: MBed pin for SDI * @param p_miso: MBed pin for SDO. Note that this pin does not exist for MCP41xxx * @param p_sclk: MBed pin for CLK + * @param p_cs : MBed pin for Chip Select. If NC, assumes that application manage /CS, default value is NC, not connected * @param p_reset: MBed pin to manage /RESET input. If NC, /RESET is not managed, default value is NC, not connected * @param p_shdn: MBed pin to manage /SHDN input. If NC, /SHDN is not managed, default value is NC, not connected * @param p_frequency: Frequency of the SPI interface (SCK), default value is 1MHz */ - CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_reset = NC, const PinName p_shdn = NC, const unsigned int p_frequency = 1000000); + CMCP4xxxx_SPI(const PinName p_mosi, const PinName p_miso, const PinName p_sclk, const PinName p_cs = NC, const PinName p_reset = NC, const PinName p_shdn = NC, const unsigned int p_frequency = 1000000); /** Destructor */ @@ -86,7 +93,7 @@ * * @param p_command The command to execute: Write or Shutdown (See DS11195C-page 18) * @param p_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation) - * @return 0x00 on success, 0Xffff otherwise + * @return 0x0000 on success, 0Xffff otherwise * Exemple: * @code * unsigned char potLevel; @@ -103,7 +110,7 @@ * * @param p_command The command to execute: Write or Shutdown (See DS11195C-page 18) * @param p_value The potentiometer selection bits (See DS11195C-page 14 Clause 4.1 Modes of Operation) - * @return 0x00 on success, 0Xffff otherwise + * @return 0x0000 on success, 0Xffff otherwise * Exemple: * @code * ...