8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory

Fork of MCP4261 by TeamElectronics

Committer:
stjo2809
Date:
Sun Jun 28 10:24:49 2015 +0000
Revision:
1:935321af7311
Parent:
0:ff10d457fef2
Child:
2:1e4469bdbed9
v.1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stjo2809 0:ff10d457fef2 1 /* mbed MCP4261 Library, for driving the 7/8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory
stjo2809 0:ff10d457fef2 2 * Copyright (c) 2015, Created by Steen Joergensen (stjo2809)
stjo2809 0:ff10d457fef2 3 *
stjo2809 0:ff10d457fef2 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
stjo2809 0:ff10d457fef2 5 * of this software and associated documentation files (the "Software"), to deal
stjo2809 0:ff10d457fef2 6 * in the Software without restriction, including without limitation the rights
stjo2809 0:ff10d457fef2 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
stjo2809 0:ff10d457fef2 8 * copies of the Software, and to permit persons to whom the Software is
stjo2809 0:ff10d457fef2 9 * furnished to do so, subject to the following conditions:
stjo2809 0:ff10d457fef2 10 *
stjo2809 0:ff10d457fef2 11 * The above copyright notice and this permission notice shall be included in
stjo2809 0:ff10d457fef2 12 * all copies or substantial portions of the Software.
stjo2809 0:ff10d457fef2 13 *
stjo2809 0:ff10d457fef2 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
stjo2809 0:ff10d457fef2 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
stjo2809 0:ff10d457fef2 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
stjo2809 0:ff10d457fef2 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
stjo2809 0:ff10d457fef2 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
stjo2809 0:ff10d457fef2 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
stjo2809 0:ff10d457fef2 20 * THE SOFTWARE.
stjo2809 0:ff10d457fef2 21 */
stjo2809 0:ff10d457fef2 22
stjo2809 0:ff10d457fef2 23 #include "mbed.h"
stjo2809 0:ff10d457fef2 24
stjo2809 0:ff10d457fef2 25 #ifndef MBED_MCP4261_H
stjo2809 0:ff10d457fef2 26 #define MBED_MCP4261_H
stjo2809 0:ff10d457fef2 27
stjo2809 0:ff10d457fef2 28 //=============================================================================
stjo2809 0:ff10d457fef2 29 // All The Addresses
stjo2809 0:ff10d457fef2 30 //=============================================================================
stjo2809 0:ff10d457fef2 31
stjo2809 0:ff10d457fef2 32 #define TCON_ADDR 0x04 // Controls the state of each resistor network terminal connection.
stjo2809 0:ff10d457fef2 33 #define STATUS_ADDR 0x05 // Status (STATUS) Register, This register contains 5 status bits. WiperLock bits, Shutdown bit, Write Protect bit, EEPROM write cycle.
stjo2809 0:ff10d457fef2 34 #define VW0_ADDR 0x02 // Volatile Wiper 0
stjo2809 0:ff10d457fef2 35 #define VW1_ADDR 0x03 // Volatile Wiper 1
stjo2809 0:ff10d457fef2 36 #define NVW0_ADDR 0x04 // Non Volatile Wiper 0
stjo2809 0:ff10d457fef2 37 #define NVW1_ADDR 0x05 // Non Volatile Wiper 1
stjo2809 0:ff10d457fef2 38
stjo2809 0:ff10d457fef2 39 // DATA EEPROM locations has the address from 0x06 to 0x0F
stjo2809 0:ff10d457fef2 40
stjo2809 0:ff10d457fef2 41 //=============================================================================
stjo2809 0:ff10d457fef2 42 // Declaration of variables & custom #defines
stjo2809 0:ff10d457fef2 43 //=============================================================================
stjo2809 0:ff10d457fef2 44
stjo2809 0:ff10d457fef2 45 #define CB_WRITE 0x00 // Device commad bit for WRITE
stjo2809 0:ff10d457fef2 46 #define CB_INCR 0x01 // Device commad bit for INCREMENT
stjo2809 0:ff10d457fef2 47 #define CB_DECR 0x02 // Device commad bit for DECREMENT
stjo2809 0:ff10d457fef2 48 #define CB_READ 0x03 // Device commad bit for READ
stjo2809 0:ff10d457fef2 49
stjo2809 0:ff10d457fef2 50 //=============================================================================
stjo2809 0:ff10d457fef2 51 // Functions Declaration
stjo2809 0:ff10d457fef2 52 //=============================================================================
stjo2809 0:ff10d457fef2 53
stjo2809 0:ff10d457fef2 54 /** Interface to the 7/8-Bit Single/Dual SPI Digital POT with Non-Volatile Memory
stjo2809 0:ff10d457fef2 55 *
stjo2809 0:ff10d457fef2 56 * Using the driver:
stjo2809 0:ff10d457fef2 57 * - remenber to setup SPI in main routine or use pins instance.
stjo2809 0:ff10d457fef2 58 *
stjo2809 0:ff10d457fef2 59 * Defaults in this driver on start up:
stjo2809 0:ff10d457fef2 60 * - as default is HARDWARE WRITE PROTECT PIN "Off".
stjo2809 0:ff10d457fef2 61 * - as default is HARDWARE SHUTDOWN PIN "Off".
stjo2809 0:ff10d457fef2 62 *
stjo2809 0:ff10d457fef2 63 */
stjo2809 0:ff10d457fef2 64 class MCP4261 {
stjo2809 0:ff10d457fef2 65 public:
stjo2809 0:ff10d457fef2 66 /** Create an instance of the MCP4261 connected via specfied SPI instance.
stjo2809 0:ff10d457fef2 67 *
stjo2809 0:ff10d457fef2 68 * @param spi The mbed SPI instance (make in main routine)
stjo2809 0:ff10d457fef2 69 * @param nWP The Hardware Write Protect Control pin.
stjo2809 0:ff10d457fef2 70 * @param nSHDN The Shutdown pin.
stjo2809 0:ff10d457fef2 71 * @param nCs The SPI chip select pin.
stjo2809 0:ff10d457fef2 72 */
stjo2809 1:935321af7311 73 MCP4261(SPI& spi, PinName nCs, PinName nWP, PinName nSHDN);
stjo2809 0:ff10d457fef2 74
stjo2809 0:ff10d457fef2 75 /** Create an instance of the MCP4261 connected with SPI pins.
stjo2809 0:ff10d457fef2 76 *
stjo2809 0:ff10d457fef2 77 * @param nWP The Hardware Write Protect Control pin.
stjo2809 0:ff10d457fef2 78 * @param nSHDN The Shutdown pin.
stjo2809 0:ff10d457fef2 79 * @param mosi The SPI Master Output, Slave Input pin.
stjo2809 0:ff10d457fef2 80 * @param miso The SPI Master Input, Slave Output pin.
stjo2809 0:ff10d457fef2 81 * @param sck The SPI Serial Clock pin.
stjo2809 0:ff10d457fef2 82 * @param nCs The SPI chip select pin.
stjo2809 0:ff10d457fef2 83 */
stjo2809 1:935321af7311 84 MCP4261(PinName mosi, PinName miso,PinName sck, PinName nCs, PinName nWP, PinName nSHDN);
stjo2809 1:935321af7311 85
stjo2809 1:935321af7311 86 /** Create an instance of the MCP4261 connected via specfied SPI instance (use wthen SHDN is linked to Global reset).
stjo2809 1:935321af7311 87 *
stjo2809 1:935321af7311 88 * @param spi The mbed SPI instance (make in main routine)
stjo2809 1:935321af7311 89 * @param nWP The Hardware Write Protect Control pin.
stjo2809 1:935321af7311 90 * @param nCs The SPI chip select pin.
stjo2809 1:935321af7311 91 */
stjo2809 1:935321af7311 92 MCP4261(SPI& spi, PinName nCs, PinName nWP);
stjo2809 0:ff10d457fef2 93
stjo2809 0:ff10d457fef2 94
stjo2809 0:ff10d457fef2 95 /** Read an Address.
stjo2809 0:ff10d457fef2 96 *
stjo2809 0:ff10d457fef2 97 * @param address The selected register to read from.
stjo2809 0:ff10d457fef2 98 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 99 */
stjo2809 0:ff10d457fef2 100 int read(char address);
stjo2809 0:ff10d457fef2 101
stjo2809 0:ff10d457fef2 102 /** Write to Address.
stjo2809 0:ff10d457fef2 103 *
stjo2809 0:ff10d457fef2 104 * @param address The selected register to write to.
stjo2809 0:ff10d457fef2 105 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 106 */
stjo2809 0:ff10d457fef2 107 void write(char address, int data);
stjo2809 0:ff10d457fef2 108
stjo2809 0:ff10d457fef2 109 /** Increment wiper.
stjo2809 0:ff10d457fef2 110 *
stjo2809 0:ff10d457fef2 111 * @param number The selected wiper to increment.
stjo2809 0:ff10d457fef2 112 */
stjo2809 0:ff10d457fef2 113 void inc(bool number);
stjo2809 0:ff10d457fef2 114
stjo2809 0:ff10d457fef2 115 /** Decrement wiper.
stjo2809 0:ff10d457fef2 116 *
stjo2809 0:ff10d457fef2 117 * @param number The selected wiper to decrement.
stjo2809 0:ff10d457fef2 118 */
stjo2809 0:ff10d457fef2 119 void dec(bool number);
stjo2809 0:ff10d457fef2 120
stjo2809 0:ff10d457fef2 121 /** Read the Status register.
stjo2809 0:ff10d457fef2 122 *
stjo2809 0:ff10d457fef2 123 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 124 */
stjo2809 0:ff10d457fef2 125 int status();
stjo2809 0:ff10d457fef2 126
stjo2809 0:ff10d457fef2 127 /** Read the tcon register.
stjo2809 0:ff10d457fef2 128 *
stjo2809 0:ff10d457fef2 129 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 130 */
stjo2809 0:ff10d457fef2 131 int tcon();
stjo2809 0:ff10d457fef2 132
stjo2809 0:ff10d457fef2 133 /** write to tcon register.
stjo2809 0:ff10d457fef2 134 *
stjo2809 0:ff10d457fef2 135 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 136 */
stjo2809 0:ff10d457fef2 137 void tcon(int data);
stjo2809 0:ff10d457fef2 138
stjo2809 0:ff10d457fef2 139 /** Read the Volatile Wiper.
stjo2809 0:ff10d457fef2 140 *
stjo2809 0:ff10d457fef2 141 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 142 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 143 */
stjo2809 0:ff10d457fef2 144 int wiper(bool number);
stjo2809 0:ff10d457fef2 145
stjo2809 0:ff10d457fef2 146 /** write to Volatile Wiper.
stjo2809 0:ff10d457fef2 147 *
stjo2809 0:ff10d457fef2 148 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 149 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 150 */
stjo2809 0:ff10d457fef2 151 void wiper(bool number, int data);
stjo2809 0:ff10d457fef2 152
stjo2809 0:ff10d457fef2 153 /** Read the non-volatile wiper (Power On Reset start value).
stjo2809 0:ff10d457fef2 154 *
stjo2809 0:ff10d457fef2 155 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 156 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 157 */
stjo2809 0:ff10d457fef2 158 int nvwiper(bool number);
stjo2809 0:ff10d457fef2 159
stjo2809 0:ff10d457fef2 160 /** write to non-volatile wiper (Power On Reset start value).
stjo2809 0:ff10d457fef2 161 *
stjo2809 0:ff10d457fef2 162 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 163 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 164 */
stjo2809 0:ff10d457fef2 165 void nvwiper(bool number, int data);
stjo2809 0:ff10d457fef2 166
stjo2809 0:ff10d457fef2 167 /** HARDWARE SHUTDOWN PIN (SHDN)
stjo2809 0:ff10d457fef2 168 *
stjo2809 0:ff10d457fef2 169 * @param act SHDN is Active = true and Inactive = false
stjo2809 0:ff10d457fef2 170 */
stjo2809 0:ff10d457fef2 171 void shdn(bool act);
stjo2809 0:ff10d457fef2 172
stjo2809 0:ff10d457fef2 173 /** HARDWARE WRITE PROTECT PIN (WP)
stjo2809 0:ff10d457fef2 174 *
stjo2809 0:ff10d457fef2 175 * @param act WP is Active = true and Inactive = false
stjo2809 0:ff10d457fef2 176 */
stjo2809 0:ff10d457fef2 177 void wp(bool act);
stjo2809 0:ff10d457fef2 178
stjo2809 0:ff10d457fef2 179
stjo2809 0:ff10d457fef2 180 private:
stjo2809 0:ff10d457fef2 181 SPI& _spi;
stjo2809 0:ff10d457fef2 182 DigitalOut _nWP;
stjo2809 0:ff10d457fef2 183 DigitalOut _nSHDN;
stjo2809 0:ff10d457fef2 184 DigitalOut _nCs;
stjo2809 0:ff10d457fef2 185
stjo2809 0:ff10d457fef2 186 char _command_byte;
stjo2809 0:ff10d457fef2 187
stjo2809 0:ff10d457fef2 188 char _make_command_byte(int com, char address, int data);
stjo2809 0:ff10d457fef2 189 int _read(char address);
stjo2809 0:ff10d457fef2 190 void _write(char address, int data);
stjo2809 0:ff10d457fef2 191
stjo2809 0:ff10d457fef2 192 };
stjo2809 0:ff10d457fef2 193
stjo2809 0:ff10d457fef2 194 #endif