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

Fork of MCP4261 by TeamElectronics

Committer:
stjo2809
Date:
Mon Mar 23 07:52:40 2015 +0000
Revision:
0:ff10d457fef2
Child:
1:935321af7311
revision 0.1

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 0:ff10d457fef2 73 MCP4261(SPI& spi, PinName nWP, PinName nSHDN, PinName nCs);
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 0:ff10d457fef2 84 MCP4261(PinName nWP, PinName nSHDN, PinName mosi, PinName miso,PinName sck, PinName nCs);
stjo2809 0:ff10d457fef2 85
stjo2809 0:ff10d457fef2 86
stjo2809 0:ff10d457fef2 87 /** Read an Address.
stjo2809 0:ff10d457fef2 88 *
stjo2809 0:ff10d457fef2 89 * @param address The selected register to read from.
stjo2809 0:ff10d457fef2 90 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 91 */
stjo2809 0:ff10d457fef2 92 int read(char address);
stjo2809 0:ff10d457fef2 93
stjo2809 0:ff10d457fef2 94 /** Write to Address.
stjo2809 0:ff10d457fef2 95 *
stjo2809 0:ff10d457fef2 96 * @param address The selected register to write to.
stjo2809 0:ff10d457fef2 97 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 98 */
stjo2809 0:ff10d457fef2 99 void write(char address, int data);
stjo2809 0:ff10d457fef2 100
stjo2809 0:ff10d457fef2 101 /** Increment wiper.
stjo2809 0:ff10d457fef2 102 *
stjo2809 0:ff10d457fef2 103 * @param number The selected wiper to increment.
stjo2809 0:ff10d457fef2 104 */
stjo2809 0:ff10d457fef2 105 void inc(bool number);
stjo2809 0:ff10d457fef2 106
stjo2809 0:ff10d457fef2 107 /** Decrement wiper.
stjo2809 0:ff10d457fef2 108 *
stjo2809 0:ff10d457fef2 109 * @param number The selected wiper to decrement.
stjo2809 0:ff10d457fef2 110 */
stjo2809 0:ff10d457fef2 111 void dec(bool number);
stjo2809 0:ff10d457fef2 112
stjo2809 0:ff10d457fef2 113 /** Read the Status register.
stjo2809 0:ff10d457fef2 114 *
stjo2809 0:ff10d457fef2 115 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 116 */
stjo2809 0:ff10d457fef2 117 int status();
stjo2809 0:ff10d457fef2 118
stjo2809 0:ff10d457fef2 119 /** Read the tcon register.
stjo2809 0:ff10d457fef2 120 *
stjo2809 0:ff10d457fef2 121 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 122 */
stjo2809 0:ff10d457fef2 123 int tcon();
stjo2809 0:ff10d457fef2 124
stjo2809 0:ff10d457fef2 125 /** write to tcon register.
stjo2809 0:ff10d457fef2 126 *
stjo2809 0:ff10d457fef2 127 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 128 */
stjo2809 0:ff10d457fef2 129 void tcon(int data);
stjo2809 0:ff10d457fef2 130
stjo2809 0:ff10d457fef2 131 /** Read the Volatile Wiper.
stjo2809 0:ff10d457fef2 132 *
stjo2809 0:ff10d457fef2 133 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 134 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 135 */
stjo2809 0:ff10d457fef2 136 int wiper(bool number);
stjo2809 0:ff10d457fef2 137
stjo2809 0:ff10d457fef2 138 /** write to Volatile Wiper.
stjo2809 0:ff10d457fef2 139 *
stjo2809 0:ff10d457fef2 140 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 141 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 142 */
stjo2809 0:ff10d457fef2 143 void wiper(bool number, int data);
stjo2809 0:ff10d457fef2 144
stjo2809 0:ff10d457fef2 145 /** Read the non-volatile wiper (Power On Reset start value).
stjo2809 0:ff10d457fef2 146 *
stjo2809 0:ff10d457fef2 147 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 148 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 149 */
stjo2809 0:ff10d457fef2 150 int nvwiper(bool number);
stjo2809 0:ff10d457fef2 151
stjo2809 0:ff10d457fef2 152 /** write to non-volatile wiper (Power On Reset start value).
stjo2809 0:ff10d457fef2 153 *
stjo2809 0:ff10d457fef2 154 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 155 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 156 */
stjo2809 0:ff10d457fef2 157 void nvwiper(bool number, int data);
stjo2809 0:ff10d457fef2 158
stjo2809 0:ff10d457fef2 159 /** HARDWARE SHUTDOWN PIN (SHDN)
stjo2809 0:ff10d457fef2 160 *
stjo2809 0:ff10d457fef2 161 * @param act SHDN is Active = true and Inactive = false
stjo2809 0:ff10d457fef2 162 */
stjo2809 0:ff10d457fef2 163 void shdn(bool act);
stjo2809 0:ff10d457fef2 164
stjo2809 0:ff10d457fef2 165 /** HARDWARE WRITE PROTECT PIN (WP)
stjo2809 0:ff10d457fef2 166 *
stjo2809 0:ff10d457fef2 167 * @param act WP is Active = true and Inactive = false
stjo2809 0:ff10d457fef2 168 */
stjo2809 0:ff10d457fef2 169 void wp(bool act);
stjo2809 0:ff10d457fef2 170
stjo2809 0:ff10d457fef2 171
stjo2809 0:ff10d457fef2 172 private:
stjo2809 0:ff10d457fef2 173 SPI& _spi;
stjo2809 0:ff10d457fef2 174 DigitalOut _nWP;
stjo2809 0:ff10d457fef2 175 DigitalOut _nSHDN;
stjo2809 0:ff10d457fef2 176 DigitalOut _nCs;
stjo2809 0:ff10d457fef2 177
stjo2809 0:ff10d457fef2 178 char _command_byte;
stjo2809 0:ff10d457fef2 179
stjo2809 0:ff10d457fef2 180 char _make_command_byte(int com, char address, int data);
stjo2809 0:ff10d457fef2 181 int _read(char address);
stjo2809 0:ff10d457fef2 182 void _write(char address, int data);
stjo2809 0:ff10d457fef2 183
stjo2809 0:ff10d457fef2 184 };
stjo2809 0:ff10d457fef2 185
stjo2809 0:ff10d457fef2 186 #endif