| 1 | /**************************************************************************//** |
| 2 | * @file ad5660.c |
| 3 | * @brief Implementation of ad5660 Driver for Microblaze processor. |
| 4 | * @author Lucian Sin ([email protected]) |
| 5 | * |
| 6 | ******************************************************************************* |
| 7 | * Copyright 2013(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 | #include "ad5660.h" |
| 46 | |
| 47 | /******************************************************************************/ |
| 48 | /************************ Variables Definitions *******************************/ |
| 49 | /******************************************************************************/ |
| 50 | struct ad56x0_chip_info { |
| 51 | unsigned char bitsNumber; |
| 52 | float vRef; |
| 53 | unsigned short fullScale; |
| 54 | }; |
| 55 | |
| 56 | static const struct ad56x0_chip_info ad56x0_chip_info[] = { |
| 57 | [ID_AD5620_2_3] = { |
| 58 | .bitsNumber = 12, |
| 59 | .vRef = 2.5, |
| 60 | .fullScale = 0xFFF, |
| 61 | }, |
| 62 | [ID_AD5620_1] = { |
| 63 | .bitsNumber = 12, |
| 64 | .vRef = 1.25, |
| 65 | .fullScale = 0xFFF, |
| 66 | }, |
| 67 | [ID_AD5640_2_3] = { |
| 68 | .bitsNumber = 14, |
| 69 | .vRef = 2.5, |
| 70 | .fullScale = 0x3FFF, |
| 71 | }, |
| 72 | [ID_AD5640_1] = { |
| 73 | .bitsNumber = 14, |
| 74 | .vRef = 1.25, |
| 75 | .fullScale = 0x3FFF, |
| 76 | }, |
| 77 | [ID_AD5660_2_3] = { |
| 78 | .bitsNumber = 16, |
| 79 | .vRef = 2.5, |
| 80 | .fullScale = 0xFFFF, |
| 81 | }, |
| 82 | [ID_AD5660_1] = { |
| 83 | .bitsNumber = 16, |
| 84 | .vRef = 1.25, |
| 85 | .fullScale = 0xFFFF, |
| 86 | }, |
| 87 | }; |
| 88 | |
| 89 | AD56x0_type act_device; |
| 90 | |
| 91 | /******************************************************************************/ |
| 92 | /*************************** Functions Definitions ****************************/ |
| 93 | /******************************************************************************/ |
| 94 | |
| 95 | /**************************************************************************//** |
| 96 | * @brief Initializes the communication with the device. |
| 97 | * |
| 98 | * @param None. |
| 99 | * |
| 100 | * @return status - Result of the initialization procedure. |
| 101 | * Example: 0 - if initialization was successful; |
| 102 | * -1 - if initialization was unsuccessful. |
| 103 | ******************************************************************************/ |
| 104 | char ad5660_Init(AD56x0_type device) |
| 105 | { |
| 106 | char status = -1; |
| 107 | |
| 108 | act_device = device; |
| 109 | /* Setup SPI Interface */ |
| 110 | status = SPI_Init(0, 1000000, 1, 1); |
| 111 | |
| 112 | return status; |
| 113 | } |
| 114 | |
| 115 | /***************************************************************************//** |
| 116 | * @brief Writes 12/14/16 bits of data to input register through SPI. |
| 117 | * |
| 118 | * @param data - Data which will be transmitted. |
| 119 | * |
| 120 | * @return status - Result of the write operation. |
| 121 | * Example: 0 - if operation was successful; |
| 122 | * -1 - if operation was unsuccessful. |
| 123 | *******************************************************************************/ |
| 124 | char ad5660_WriteRegister(unsigned long data) |
| 125 | { |
| 126 | unsigned char bytesWritten = 0; |
| 127 | char status = 0; |
| 128 | unsigned char dataBuf[3] = {0, 0, 0}; |
| 129 | |
| 130 | if(ad56x0_chip_info[act_device].bitsNumber == 16) |
| 131 | { |
| 132 | dataBuf[0] = (data & 0x00FF0000) >> 16; |
| 133 | dataBuf[1] = (data & 0x0000FF00) >> 8; |
| 134 | dataBuf[2] = (data & 0x000000FF) >> 0; |
| 135 | bytesWritten = SPI_Write(ad5660_SLAVE_ID, dataBuf, 3); |
| 136 | if (bytesWritten != 3) |
| 137 | { |
| 138 | status = -1; |
| 139 | } |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | if(ad56x0_chip_info[act_device].bitsNumber == 14) |
| 144 | { |
| 145 | dataBuf[0] = (data & 0xFF00) >> 8; |
| 146 | dataBuf[1] = (data & 0x00FF) >> 0; |
| 147 | bytesWritten = SPI_Write(ad5660_SLAVE_ID, dataBuf, 2); |
| 148 | if (bytesWritten != 2) |
| 149 | { |
| 150 | status = -1; |
| 151 | } |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | if(ad56x0_chip_info[act_device].bitsNumber == 12) |
| 156 | { |
| 157 | dataBuf[0] = (data & 0x3FC0) >> 6; |
| 158 | dataBuf[1] = (data & 0x003F) << 2; |
| 159 | bytesWritten = SPI_Write(ad5660_SLAVE_ID, dataBuf, 2); |
| 160 | if (bytesWritten != 2) |
| 161 | { |
| 162 | status = -1; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return status; |
| 169 | } |
| 170 | |
| 171 | /***************************************************************************//** |
| 172 | * @brief Sets the output DAC voltage to a desired value. |
| 173 | * |
| 174 | * @param vOut - the desired output voltage of the DAC. |
| 175 | * |
| 176 | * @return None. |
| 177 | *******************************************************************************/ |
| 178 | void ad5660_SetVoltage(float vOut) |
| 179 | { |
| 180 | unsigned long dacCode = 0; |
| 181 | |
| 182 | dacCode = (unsigned long)((float)(vOut / ad56x0_chip_info[act_device].vRef) * |
| 183 | ad56x0_chip_info[act_device].fullScale); |
| 184 | dacCode |= ad5660_LOAD; |
| 185 | ad5660_WriteRegister(dacCode); |
| 186 | } |