| 1 | /***************************************************************************//** |
| 2 | * @file ad7791.c |
| 3 | * @brief Header file of AD7791 Driver for Microblaze processor. |
| 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 | #ifndef _ad7791_H_ |
| 42 | #define _ad7791_H_ |
| 43 | |
| 44 | /******************************************************************************/ |
| 45 | /******************************* Include Files ********************************/ |
| 46 | /******************************************************************************/ |
| 47 | #include "xil_types.h" |
| 48 | #include "xparameters.h" |
| 49 | |
| 50 | /******************************************************************************/ |
| 51 | /********************** Macros and Constants Definitions **********************/ |
| 52 | /******************************************************************************/ |
| 53 | #define AD7791_SLAVE_ID 1 |
| 54 | |
| 55 | /******************************************************************************/ |
| 56 | /******************************** AD7791 **************************************/ |
| 57 | /******************************************************************************/ |
| 58 | |
| 59 | /* AD7791 Registers */ |
| 60 | #define AD7791_REG_COMM 0 // Communications Register (WO, 8-bit) |
| 61 | #define AD7791_REG_STAT 0 // Status Register (RO, 8-bit) |
| 62 | #define AD7791_REG_MODE 1 // Mode Register (RW, 8-bit) |
| 63 | #define AD7791_REG_FILTER 2 // Mode Register (RW, 8-bit) |
| 64 | #define AD7791_REG_DATA 3 // Data Register (RO, 24-bit) |
| 65 | |
| 66 | /* AD7791_REG_COMM Bits */ |
| 67 | #define AD7791_COMM_WEN (1 << 7) // Write Enable - active low |
| 68 | #define AD7791_COMM_ADDR(x) (((x) & 0x3) << 4) // Register Address |
| 69 | #define AD7791_COMM_WRITE (0 << 3) // Write Operation |
| 70 | #define AD7791_COMM_READ (1 << 3) // Read Operation |
| 71 | #define AD7791_COMM_CREAD (1 << 2) // Continuous Read of Data Register |
| 72 | #define AD7791_COMM_CH(x) ((x) & 0x3) // Channel Selection |
| 73 | |
| 74 | /* AD7791_REG_STAT Bits */ |
| 75 | #define AD7791_STAT_RDY (1 << 7) // Ready bit for ADC |
| 76 | #define AD7791_STAT_ERR (1 << 6) // ADC Error Bit |
| 77 | #define AD7791_STAT_WL (1 << 2) // 1 if the device is AD7791 |
| 78 | #define AD7791_STAT_CH1 (1 << 1) // Channel 1 bit |
| 79 | #define AD7791_STAT_CH0 (1 << 0) // Channel 2 bit |
| 80 | |
| 81 | /* AD7791_REG_MODE */ |
| 82 | #define AD7791_MODE_SEL(x) (((x) & 0x3) << 6) // Mode Select |
| 83 | #define AD7791_MODE_BO (1 << 3) // Burnout Current Enable Bit |
| 84 | #define AD7791_MODE_UNIPOLAR (1 << 2) // Unipolar Operation Enable |
| 85 | #define AD7791_MODE_BIPOLAR (0 << 2) // Bipolar Operation Enable |
| 86 | #define AD7791_MODE_BUF (1 << 1) // Buffered Mode Enable |
| 87 | |
| 88 | /* AD7193_MODE_SEL(x) options */ |
| 89 | #define AD7791_MODE_CONT 0 // Continuous Conversion Mode |
| 90 | #define AD7791_MODE_SINGLE 2 // Single Conversion Mode |
| 91 | #define AD7791_MODE_PWRDN 3 // Power-Down Mode |
| 92 | |
| 93 | /* AD7791_REG_FILTER */ |
| 94 | #define AD7791_FILTER_CDIV(x) (((x) & 0x3) << 4) // Divide clock. |
| 95 | #define AD7791_FILTER_FS(x) ((x) & 0x7) // Output update rate. |
| 96 | |
| 97 | /* AD7791_FILTER_CDIV options */ |
| 98 | #define AD7791_FILTER_CDIV_NORMAL 0 // Normal Mode |
| 99 | #define AD7791_FILTER_CDIV_DIV2 1 // Clock Divided by 2 |
| 100 | #define AD7791_FILTER_CDIV_DIV4 2 // Clock Divided by 4 |
| 101 | #define AD7791_FILTER_CDIV_DIV8 3 // Clock Divided by 8 |
| 102 | |
| 103 | /******************************************************************************/ |
| 104 | /************************** Functions Declarations ****************************/ |
| 105 | /******************************************************************************/ |
| 106 | |
| 107 | /* Initializes SPI and initial values for AD7791 device. */ |
| 108 | char AD7791_Init(void); |
| 109 | |
| 110 | /* Reads data from AD7791. */ |
| 111 | unsigned long AD7791_Read(unsigned char regAddress, |
| 112 | unsigned char bytesNumber); |
| 113 | |
| 114 | /* Writes data to AD7791. */ |
| 115 | void AD7791_Write(unsigned char regAddress, |
| 116 | unsigned char bytesNumber, |
| 117 | unsigned char regData); |
| 118 | |
| 119 | /* Converts 24-bit raw data to millivolts. */ |
| 120 | float AD7791_ConvertToVolts(unsigned long rawData, |
| 121 | float vRef, |
| 122 | unsigned char polarity); |
| 123 | |
| 124 | /* Resets the serial interface with the AD7791. */ |
| 125 | void AD7791_Reset(void); |
| 126 | |
| 127 | #endif /* ad7791_H_ */ |