| 1 | /**************************************************************************//** |
| 2 | * @file ad5662.c |
| 3 | * @brief Implementation of ad5662 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 "ad5662.h" |
| 46 | |
| 47 | /******************************************************************************/ |
| 48 | /************************ Variables Definitions *******************************/ |
| 49 | /******************************************************************************/ |
| 50 | unsigned short fullScale = 0xFFFF; |
| 51 | |
| 52 | /******************************************************************************/ |
| 53 | /*************************** Functions Definitions ****************************/ |
| 54 | /******************************************************************************/ |
| 55 | |
| 56 | /**************************************************************************//** |
| 57 | * @brief Initializes the communication with the device. |
| 58 | * |
| 59 | * @param None. |
| 60 | * |
| 61 | * @return status - Result of the initialization procedure. |
| 62 | * Example: 0 - if initialization was successful; |
| 63 | * -1 - if initialization was unsuccessful. |
| 64 | ******************************************************************************/ |
| 65 | char ad5662_Init(void) |
| 66 | { |
| 67 | |
| 68 | char status = -1; |
| 69 | |
| 70 | /* Setup SPI Interface */ |
| 71 | status = SPI_Init(0, 1000000, 1, 1); |
| 72 | |
| 73 | return status; |
| 74 | } |
| 75 | |
| 76 | /***************************************************************************//** |
| 77 | * @brief Writes 24 bits to device through SPI. |
| 78 | * |
| 79 | * @param data - Data which will be transmitted. |
| 80 | * |
| 81 | * @return status - Result of the write operation. |
| 82 | * Example: 0 - if operation was successful; |
| 83 | * -1 - if operation was unsuccessful. |
| 84 | *******************************************************************************/ |
| 85 | char ad5662_Write24Bits(unsigned long data) |
| 86 | { |
| 87 | unsigned char bytesWritten = 0; |
| 88 | char status = 0; |
| 89 | unsigned char dataBuf[3] = {(data & 0x00FF0000) >> 16, |
| 90 | (data & 0x0000FF00) >> 8, |
| 91 | (data & 0x000000FF) >> 0}; |
| 92 | |
| 93 | bytesWritten = SPI_Write(ad5662_SLAVE_ID, dataBuf, 3); |
| 94 | if (bytesWritten != 3) |
| 95 | { |
| 96 | return status = -1; |
| 97 | } |
| 98 | |
| 99 | return status; |
| 100 | } |
| 101 | |
| 102 | /***************************************************************************//** |
| 103 | * @brief Sets the output DAC voltage to a desired value. |
| 104 | * |
| 105 | * @param vOut - the desired output voltage of the DAC. |
| 106 | * vRef - reference voltage of the DAC. |
| 107 | * |
| 108 | * @return None. |
| 109 | *******************************************************************************/ |
| 110 | void ad5662_SetVoltage(float vOut, float vRef) |
| 111 | { |
| 112 | unsigned long dacCode = 0; |
| 113 | |
| 114 | dacCode = (unsigned long)((float)(vOut / vRef) * fullScale); |
| 115 | dacCode |= AD5662_LOAD; |
| 116 | ad5662_Write24Bits(dacCode); |
| 117 | } |