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

Fork of MCP4261 by TeamElectronics

Committer:
stjo2809
Date:
Wed Jan 20 10:54:05 2016 +0000
Revision:
2:1e4469bdbed9
Parent:
1:935321af7311
tested

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 2:1e4469bdbed9 34 #define VW0_ADDR 0x00 // Volatile Wiper 0
stjo2809 2:1e4469bdbed9 35 #define VW1_ADDR 0x01 // Volatile Wiper 1
stjo2809 2:1e4469bdbed9 36 #define NVW0_ADDR 0x02 // Non Volatile Wiper 0
stjo2809 2:1e4469bdbed9 37 #define NVW1_ADDR 0x03 // 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 1:935321af7311 75 /** Create an instance of the MCP4261 connected via specfied SPI instance (use wthen SHDN is linked to Global reset).
stjo2809 1:935321af7311 76 *
stjo2809 1:935321af7311 77 * @param spi The mbed SPI instance (make in main routine)
stjo2809 1:935321af7311 78 * @param nWP The Hardware Write Protect Control pin.
stjo2809 1:935321af7311 79 * @param nCs The SPI chip select pin.
stjo2809 1:935321af7311 80 */
stjo2809 1:935321af7311 81 MCP4261(SPI& spi, PinName nCs, PinName nWP);
stjo2809 0:ff10d457fef2 82
stjo2809 0:ff10d457fef2 83
stjo2809 0:ff10d457fef2 84 /** Read an Address.
stjo2809 0:ff10d457fef2 85 *
stjo2809 0:ff10d457fef2 86 * @param address The selected register to read from.
stjo2809 0:ff10d457fef2 87 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 88 */
stjo2809 0:ff10d457fef2 89 int read(char address);
stjo2809 0:ff10d457fef2 90
stjo2809 0:ff10d457fef2 91 /** Write to Address.
stjo2809 0:ff10d457fef2 92 *
stjo2809 0:ff10d457fef2 93 * @param address The selected register to write to.
stjo2809 0:ff10d457fef2 94 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 95 */
stjo2809 0:ff10d457fef2 96 void write(char address, int data);
stjo2809 0:ff10d457fef2 97
stjo2809 0:ff10d457fef2 98 /** Increment wiper.
stjo2809 0:ff10d457fef2 99 *
stjo2809 0:ff10d457fef2 100 * @param number The selected wiper to increment.
stjo2809 0:ff10d457fef2 101 */
stjo2809 0:ff10d457fef2 102 void inc(bool number);
stjo2809 0:ff10d457fef2 103
stjo2809 0:ff10d457fef2 104 /** Decrement wiper.
stjo2809 0:ff10d457fef2 105 *
stjo2809 0:ff10d457fef2 106 * @param number The selected wiper to decrement.
stjo2809 0:ff10d457fef2 107 */
stjo2809 0:ff10d457fef2 108 void dec(bool number);
stjo2809 0:ff10d457fef2 109
stjo2809 0:ff10d457fef2 110 /** Read the Status register.
stjo2809 0:ff10d457fef2 111 *
stjo2809 0:ff10d457fef2 112 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 113 */
stjo2809 0:ff10d457fef2 114 int status();
stjo2809 0:ff10d457fef2 115
stjo2809 0:ff10d457fef2 116 /** Read the tcon register.
stjo2809 0:ff10d457fef2 117 *
stjo2809 0:ff10d457fef2 118 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 119 */
stjo2809 0:ff10d457fef2 120 int tcon();
stjo2809 0:ff10d457fef2 121
stjo2809 0:ff10d457fef2 122 /** write to tcon register.
stjo2809 0:ff10d457fef2 123 *
stjo2809 0:ff10d457fef2 124 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 125 */
stjo2809 0:ff10d457fef2 126 void tcon(int data);
stjo2809 0:ff10d457fef2 127
stjo2809 0:ff10d457fef2 128 /** Read the Volatile Wiper.
stjo2809 0:ff10d457fef2 129 *
stjo2809 0:ff10d457fef2 130 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 131 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 132 */
stjo2809 0:ff10d457fef2 133 int wiper(bool number);
stjo2809 0:ff10d457fef2 134
stjo2809 0:ff10d457fef2 135 /** write to Volatile Wiper.
stjo2809 0:ff10d457fef2 136 *
stjo2809 0:ff10d457fef2 137 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 138 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 139 */
stjo2809 0:ff10d457fef2 140 void wiper(bool number, int data);
stjo2809 0:ff10d457fef2 141
stjo2809 0:ff10d457fef2 142 /** Read the non-volatile wiper (Power On Reset start value).
stjo2809 0:ff10d457fef2 143 *
stjo2809 0:ff10d457fef2 144 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 145 * @return The 16 bits read.
stjo2809 0:ff10d457fef2 146 */
stjo2809 0:ff10d457fef2 147 int nvwiper(bool number);
stjo2809 0:ff10d457fef2 148
stjo2809 0:ff10d457fef2 149 /** write to non-volatile wiper (Power On Reset start value).
stjo2809 0:ff10d457fef2 150 *
stjo2809 0:ff10d457fef2 151 * @param number The wiper number = '0' or '1'
stjo2809 0:ff10d457fef2 152 * @param data The 16 bits to write to the register
stjo2809 0:ff10d457fef2 153 */
stjo2809 0:ff10d457fef2 154 void nvwiper(bool number, int data);
stjo2809 0:ff10d457fef2 155
stjo2809 0:ff10d457fef2 156 /** HARDWARE SHUTDOWN PIN (SHDN)
stjo2809 0:ff10d457fef2 157 *
stjo2809 0:ff10d457fef2 158 * @param act SHDN is Active = true and Inactive = false
stjo2809 0:ff10d457fef2 159 */
stjo2809 0:ff10d457fef2 160 void shdn(bool act);
stjo2809 0:ff10d457fef2 161
stjo2809 0:ff10d457fef2 162 /** HARDWARE WRITE PROTECT PIN (WP)
stjo2809 0:ff10d457fef2 163 *
stjo2809 0:ff10d457fef2 164 * @param act WP is Active = true and Inactive = false
stjo2809 0:ff10d457fef2 165 */
stjo2809 0:ff10d457fef2 166 void wp(bool act);
stjo2809 0:ff10d457fef2 167
stjo2809 0:ff10d457fef2 168
stjo2809 0:ff10d457fef2 169 private:
stjo2809 0:ff10d457fef2 170 SPI& _spi;
stjo2809 2:1e4469bdbed9 171 DigitalOut _nCs;
stjo2809 0:ff10d457fef2 172 DigitalOut _nWP;
stjo2809 0:ff10d457fef2 173 DigitalOut _nSHDN;
stjo2809 2:1e4469bdbed9 174
stjo2809 0:ff10d457fef2 175
stjo2809 0:ff10d457fef2 176 char _command_byte;
stjo2809 0:ff10d457fef2 177
stjo2809 0:ff10d457fef2 178 char _make_command_byte(int com, char address, int data);
stjo2809 0:ff10d457fef2 179 int _read(char address);
stjo2809 0:ff10d457fef2 180 void _write(char address, int data);
stjo2809 0:ff10d457fef2 181
stjo2809 0:ff10d457fef2 182 };
stjo2809 0:ff10d457fef2 183
stjo2809 0:ff10d457fef2 184 #endif