| 1 | /***************************************************************************//** |
| 2 | * @file AD5628.c |
| 3 | * @brief Implementation of AD5628 Driver. |
| 4 | * @author DNechita ([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2012(c) Analog Devices, Inc. |
| 7 | * |
| 8 | * All rights reserved. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions are met: |
| 12 | * - Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * - Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * - Neither the name of Analog Devices, Inc. nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * - The use of this software may or may not infringe the patent rights |
| 22 | * of one or more patent holders. This license does not release you |
| 23 | * from the requirement that you obtain separate licenses from these |
| 24 | * patent holders to use this software. |
| 25 | * - Use of the software either in source or binary form, must be run |
| 26 | * on or directly connected to an Analog Devices Inc. component. |
| 27 | * |
| 28 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
| 29 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| 30 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 31 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | * |
| 39 | ******************************************************************************** |
| 40 | * SVN Revision: $WCREV$ |
| 41 | *******************************************************************************/ |
| 42 | |
| 43 | /******************************************************************************/ |
| 44 | /***************************** Include Files **********************************/ |
| 45 | /******************************************************************************/ |
| 46 | #include "AD5628.h" // AD5628 definitions. |
| 47 | |
| 48 | /***************************************************************************//** |
| 49 | * @brief Resets the device and performs several initializations. |
| 50 | * |
| 51 | * @return Result of the initialization procedure. |
| 52 | * Example: -1- SPI peripheral was not initialized. |
| 53 | * 0- SPI peripheral is initialized. |
| 54 | *******************************************************************************/ |
| 55 | char AD5628_Init(void) |
| 56 | { |
| 57 | char status = -1; |
| 58 | |
| 59 | /* Initializes communication. */ |
| 60 | status = SPI_Init(0, 1000000, 1, 1); |
| 61 | /* Behaves as a power-on reset. */ |
| 62 | AD5628_Reset(); |
| 63 | /* Turns on the on-board reference. */ |
| 64 | AD5628_SetInputRegister(AD5628_CMD(AD5628_CMD_SET_INT_REF)| |
| 65 | AD5628_INT_REF_ON); |
| 66 | /* Clear code is set to 0x0000. */ |
| 67 | AD5628_SetInputRegister(AD5628_CMD(AD5628_CMD_LOAD_CLEAR_CODE)| |
| 68 | AD5628_CODE_0X0000); |
| 69 | |
| 70 | return status; |
| 71 | } |
| 72 | |
| 73 | /***************************************************************************//** |
| 74 | * @brief Sets the device in a specific power mode. |
| 75 | * |
| 76 | * @param pwrMode - power mode of the device. |
| 77 | * Example: AD5628_PWRDN_NONE |
| 78 | * AD5628_PWRDN_1K |
| 79 | * AD5628_PWRDN_100K |
| 80 | * AD5628_PWRDN_3STATE |
| 81 | * |
| 82 | * @param channel - The channel or channels that are being configured. |
| 83 | * Example: AD5628_ADDR_DAC_A |
| 84 | * AD5628_ADDR_DAC_B |
| 85 | * ... |
| 86 | * AD5628_ADDR_DAC_ALL |
| 87 | * |
| 88 | * @return none. |
| 89 | *******************************************************************************/ |
| 90 | void AD5628_PowerMode(unsigned char pwrMode, unsigned char channel) |
| 91 | { |
| 92 | unsigned char selectedChannel = 0; |
| 93 | |
| 94 | if(channel == AD5628_ADDR_DAC_ALL) |
| 95 | { |
| 96 | selectedChannel = 0xFF; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | selectedChannel = (1 << channel); |
| 101 | } |
| 102 | /* Selects a power mode for the selected channel. */ |
| 103 | AD5628_SetInputRegister(AD5628_CMD(AD5628_CMD_POWERDOWN) | |
| 104 | AD5628_POWER_MODE(pwrMode) | |
| 105 | selectedChannel); |
| 106 | } |
| 107 | |
| 108 | /***************************************************************************//** |
| 109 | * @brief Resets the device. |
| 110 | * |
| 111 | * @return none. |
| 112 | *******************************************************************************/ |
| 113 | void AD5628_Reset(void) |
| 114 | { |
| 115 | AD5628_SetInputRegister(AD5628_CMD(AD5628_CMD_RESET)); |
| 116 | } |
| 117 | |
| 118 | /***************************************************************************//** |
| 119 | * @brief Writes a 32-bit data-word to the Input Register of the device. |
| 120 | * |
| 121 | * @param registerValue - Value of the register. |
| 122 | * |
| 123 | * @return none. |
| 124 | *******************************************************************************/ |
| 125 | void AD5628_SetInputRegister(unsigned long registerValue) |
| 126 | { |
| 127 | unsigned char registerWord[4] = {0, 0, 0, 0}; |
| 128 | |
| 129 | registerWord[0] = (unsigned char)((registerValue & 0xFF000000) >> 24); |
| 130 | registerWord[1] = (unsigned char)((registerValue & 0x00FF0000) >> 16); |
| 131 | registerWord[2] = (unsigned char)((registerValue & 0x0000FF00) >> 8); |
| 132 | registerWord[3] = (unsigned char)((registerValue & 0x000000FF) >> 0); |
| 133 | SPI_Write(AD5628_SLAVE_ID, registerWord, 4); |
| 134 | } |