| 1 | /**************************************************************************//** |
| 2 | * @file ad7280a.c |
| 3 | * @brief Driver's header for the AD7280A Lithium Ion Battery Monitoring System |
| 4 | * @author Lucian Sin ([email protected]) |
| 5 | * |
| 6 | ******************************************************************************* |
| 7 | * Copyright 2014(c) Analog Devices, Inc. |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without modification, |
| 12 | * are permitted provided that the following conditions are met: |
| 13 | * - Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * - Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 20 | * contributors may be used to endorse or promote products derived |
| 21 | * from this software without specific prior written permission. |
| 22 | * - The use of this software may or may not infringe the patent rights |
| 23 | * of one or more patent holders. This license does not release you |
| 24 | * from the requirement that you obtain separate licenses from these |
| 25 | * patent holders to use this software. |
| 26 | * - Use of the software either in source or binary form, must be run |
| 27 | * on or directly connected to an Analog Devices Inc. component. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED |
| 30 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY |
| 31 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 32 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 34 | * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 35 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 36 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 38 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 39 | * |
| 40 | ******************************************************************************/ |
| 41 | |
| 42 | /******************************************************************************/ |
| 43 | /***************************** Include Files **********************************/ |
| 44 | /******************************************************************************/ |
| 45 | #ifndef _AD7280A_H_ |
| 46 | #define _AD7280A_H_ |
| 47 | |
| 48 | /*****************************************************************************/ |
| 49 | /***************************** Include Files *********************************/ |
| 50 | /*****************************************************************************/ |
| 51 | #include "xil_types.h" |
| 52 | #include "xparameters.h" |
| 53 | |
| 54 | /******************************************************************************/ |
| 55 | /********************** Macros and Constants Definitions **********************/ |
| 56 | /******************************************************************************/ |
| 57 | #define AD7280A_SLAVE_ID 1 |
| 58 | |
| 59 | /* GPIOs */ |
| 60 | #define AD7280A_PD_OUT GPIO5_PIN_OUT |
| 61 | #define AD7280A_PD_HIGH GPIO5_HIGH |
| 62 | #define AD7280A_PD_LOW GPIO5_LOW |
| 63 | #define AD7280A_CNVST_OUT GPIO7_PIN_OUT |
| 64 | #define AD7280A_CNVST_HIGH GPIO7_HIGH |
| 65 | #define AD7280A_CNVST_LOW GPIO7_LOW |
| 66 | |
| 67 | #define AD7280A_ALERT_IN GPIO6_PIN_IN |
| 68 | #define AD7280_ALERT (1 << 6) |
| 69 | |
| 70 | /* Acquisition time */ |
| 71 | #define AD7280A_ACQ_TIME_400ns 0 |
| 72 | #define AD7280A_ACQ_TIME_800ns 1 |
| 73 | #define AD7280A_ACQ_TIME_1200ns 2 |
| 74 | #define AD7280A_ACQ_TIME_1600ns 3 |
| 75 | |
| 76 | /* Conversion averaging */ |
| 77 | #define AD7280A_CONV_AVG_DIS 0 |
| 78 | #define AD7280A_CONV_AVG_2 1 |
| 79 | #define AD7280A_CONV_AVG_4 2 |
| 80 | #define AD7280A_CONV_AVG_8 3 |
| 81 | |
| 82 | /* Alert register bits */ |
| 83 | #define AD7280A_ALERT_REMOVE_VIN5 (1 << 2) |
| 84 | #define AD7280A_ALERT_REMOVE_VIN4_VIN5 (2 << 2) |
| 85 | #define AD7280A_ALERT_REMOVE_AUX5 (1 << 0) |
| 86 | #define AD7280A_ALERT_REMOVE_AUX4_AUX5 (2 << 0) |
| 87 | |
| 88 | /* Registers */ |
| 89 | #define AD7280A_CELL_VOLTAGE_1 0x0 /* D11 to D0, Read only */ |
| 90 | #define AD7280A_CELL_VOLTAGE_2 0x1 /* D11 to D0, Read only */ |
| 91 | #define AD7280A_CELL_VOLTAGE_3 0x2 /* D11 to D0, Read only */ |
| 92 | #define AD7280A_CELL_VOLTAGE_4 0x3 /* D11 to D0, Read only */ |
| 93 | #define AD7280A_CELL_VOLTAGE_5 0x4 /* D11 to D0, Read only */ |
| 94 | #define AD7280A_CELL_VOLTAGE_6 0x5 /* D11 to D0, Read only */ |
| 95 | #define AD7280A_AUX_ADC_1 0x6 /* D11 to D0, Read only */ |
| 96 | #define AD7280A_AUX_ADC_2 0x7 /* D11 to D0, Read only */ |
| 97 | #define AD7280A_AUX_ADC_3 0x8 /* D11 to D0, Read only */ |
| 98 | #define AD7280A_AUX_ADC_4 0x9 /* D11 to D0, Read only */ |
| 99 | #define AD7280A_AUX_ADC_5 0xA /* D11 to D0, Read only */ |
| 100 | #define AD7280A_AUX_ADC_6 0xB /* D11 to D0, Read only */ |
| 101 | #define AD7280A_SELF_TEST 0xC /* D11 to D0, Read only */ |
| 102 | #define AD7280A_CONTROL_HB 0xD /* D15 to D8, Read/write */ |
| 103 | #define AD7280A_CONTROL_LB 0xE /* D7 to D0, Read/write */ |
| 104 | #define AD7280A_CELL_OVERVOLTAGE 0xF /* D7 to D0, Read/write */ |
| 105 | #define AD7280A_CELL_UNDERVOLTAGE 0x10 /* D7 to D0, Read/write */ |
| 106 | #define AD7280A_AUX_ADC_OVERVOLTAGE 0x11 /* D7 to D0, Read/write */ |
| 107 | #define AD7280A_AUX_ADC_UNDERVOLTAGE 0x12 /* D7 to D0, Read/write */ |
| 108 | #define AD7280A_ALERT 0x13 /* D7 to D0, Read/write */ |
| 109 | #define AD7280A_CELL_BALANCE 0x14 /* D7 to D0, Read/write */ |
| 110 | #define AD7280A_CB1_TIMER 0x15 /* D7 to D0, Read/write */ |
| 111 | #define AD7280A_CB2_TIMER 0x16 /* D7 to D0, Read/write */ |
| 112 | #define AD7280A_CB3_TIMER 0x17 /* D7 to D0, Read/write */ |
| 113 | #define AD7280A_CB4_TIMER 0x18 /* D7 to D0, Read/write */ |
| 114 | #define AD7280A_CB5_TIMER 0x19 /* D7 to D0, Read/write */ |
| 115 | #define AD7280A_CB6_TIMER 0x1A /* D7 to D0, Read/write */ |
| 116 | #define AD7280A_PD_TIMER 0x1B /* D7 to D0, Read/write */ |
| 117 | #define AD7280A_READ 0x1C /* D7 to D0, Read/write */ |
| 118 | #define AD7280A_CNVST_N_CONTROL 0x1D /* D7 to D0, Read/write */ |
| 119 | |
| 120 | /* Bits and Masks */ |
| 121 | #define AD7280A_CTRL_HB_CONV_INPUT_ALL (0 << 6) |
| 122 | #define AD7280A_CTRL_HB_CONV_INPUT_6CELL_AUX1_3_4 (1 << 6) |
| 123 | #define AD7280A_CTRL_HB_CONV_INPUT_6CELL (2 << 6) |
| 124 | #define AD7280A_CTRL_HB_CONV_INPUT_SELF_TEST (3 << 6) |
| 125 | #define AD7280A_CTRL_HB_CONV_RES_READ_ALL (0 << 4) |
| 126 | #define AD7280A_CTRL_HB_CONV_RES_READ_6CELL_AUX1_3_4 (1 << 4) |
| 127 | #define AD7280A_CTRL_HB_CONV_RES_READ_6CELL (2 << 4) |
| 128 | #define AD7280A_CTRL_HB_CONV_RES_READ_NO (3 << 4) |
| 129 | #define AD7280A_CTRL_HB_CONV_START_CNVST (0 << 3) |
| 130 | #define AD7280A_CTRL_HB_CONV_START_CS (1 << 3) |
| 131 | #define AD7280A_CTRL_HB_CONV_AVG_DIS (0 << 1) |
| 132 | #define AD7280A_CTRL_HB_CONV_AVG_2 (1 << 1) |
| 133 | #define AD7280A_CTRL_HB_CONV_AVG_4 (2 << 1) |
| 134 | #define AD7280A_CTRL_HB_CONV_AVG_8 (3 << 1) |
| 135 | #define AD7280A_CTRL_HB_CONV_AVG(x) ((x) << 1) |
| 136 | #define AD7280A_CTRL_HB_PWRDN_SW (1 << 0) |
| 137 | |
| 138 | #define AD7280A_CTRL_LB_SWRST (1 << 7) |
| 139 | #define AD7280A_CTRL_LB_ACQ_TIME_400ns (0 << 5) |
| 140 | #define AD7280A_CTRL_LB_ACQ_TIME_800ns (1 << 5) |
| 141 | #define AD7280A_CTRL_LB_ACQ_TIME_1200ns (2 << 5) |
| 142 | #define AD7280A_CTRL_LB_ACQ_TIME_1600ns (3 << 5) |
| 143 | #define AD7280A_CTRL_LB_ACQ_TIME(x) ((x) << 5) |
| 144 | #define AD7280A_CTRL_LB_MUST_SET (1 << 4) |
| 145 | #define AD7280A_CTRL_LB_THERMISTOR_EN (1 << 3) |
| 146 | #define AD7280A_CTRL_LB_LOCK_DEV_ADDR (1 << 2) |
| 147 | #define AD7280A_CTRL_LB_INC_DEV_ADDR (1 << 1) |
| 148 | #define AD7280A_CTRL_LB_DAISY_CHAIN_RB_EN (1 << 0) |
| 149 | |
| 150 | #define AD7280A_ALERT_GEN_STATIC_HIGH (1 << 6) |
| 151 | #define AD7280A_ALERT_RELAY_SIG_CHAIN_DOWN (3 << 6) |
| 152 | |
| 153 | #define AD7280A_ALL_CELLS (0xAD << 16) |
| 154 | |
| 155 | #define AD7280A_DEVADDR_MASTER 0 |
| 156 | #define AD7280A_DEVADDR_ALL 0x1F |
| 157 | |
| 158 | /* Value to be sent when readings are performed */ |
| 159 | #define AD7280A_READ_TXVAL 0xF800030A |
| 160 | |
| 161 | #define NUMBITS_READ 22 // Number of bits for CRC when reading |
| 162 | #define NUMBITS_WRITE 21 // Number of bits for CRC when writing |
| 163 | |
| 164 | /*****************************************************************************/ |
| 165 | /************************ Functions Declarations *****************************/ |
| 166 | /*****************************************************************************/ |
| 167 | /* Initializes the communication with the device. */ |
| 168 | char AD7280A_Init(void); |
| 169 | |
| 170 | /* Reads/transmits 32 data bits from/to AD7280A. */ |
| 171 | unsigned long AD7280A_Transfer_32bits(unsigned long data); |
| 172 | |
| 173 | /* Computes the CRC value for a write transmission, and prepares the complete |
| 174 | write codeword */ |
| 175 | unsigned long AD7280A_CRC_Write(unsigned long message); |
| 176 | |
| 177 | /* Checks the received message if the received CRC and computed CRC are |
| 178 | the same. */ |
| 179 | long AD7280A_CRC_Read(unsigned long message); |
| 180 | |
| 181 | /* Performs a read from all registers on 2 devices. */ |
| 182 | char AD7280A_Convert_Read_All(void); |
| 183 | |
| 184 | /* Converts acquired data to float values. */ |
| 185 | char AD7280A_Convert_Data_All(void); |
| 186 | |
| 187 | /* Reads the register content of one selected register. */ |
| 188 | short AD7280A_Read_Register(unsigned char devAddr, |
| 189 | unsigned char readReg); |
| 190 | |
| 191 | /* Reads the conversion of one channel. */ |
| 192 | short AD7280A_Read_Conversion(unsigned char devAddr, |
| 193 | unsigned char readReg); |
| 194 | |
| 195 | /* Converts the input data to a voltage value. */ |
| 196 | float AD7280A_Convert_Data(unsigned char type, unsigned short data); |
| 197 | |
| 198 | /* Writes the content of one selected register from the selected device. */ |
| 199 | void AD7280A_Write_Register(unsigned char devAddr, |
| 200 | unsigned char readReg, |
| 201 | unsigned char regVal); |
| 202 | |
| 203 | /* Performs the self test for two devices(one master and one slave). */ |
| 204 | void AD7280A_Selftest_All(float *selfTestRegA, float *selfTestRegB); |
| 205 | |
| 206 | /* Reads the value of Alert Pin from the device. */ |
| 207 | unsigned char AD7280A_Alert_Pin(void); |
| 208 | |
| 209 | #endif /*_AD7280A_H_*/ |