| 1 | /**************************************************************************//** |
| 2 | * @file ad7685.c |
| 3 | * @brief Implementation of ad7685 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 "ad7685.h" |
| 46 | |
| 47 | /******************************************************************************/ |
| 48 | /*************************** Functions Definitions ****************************/ |
| 49 | /******************************************************************************/ |
| 50 | |
| 51 | /**************************************************************************//** |
| 52 | * @brief Initializes the communication with the device. |
| 53 | * |
| 54 | * @param None. |
| 55 | * |
| 56 | * @return status - Result of the initialization procedure. |
| 57 | * Example: 0 - if initialization was successful; |
| 58 | * -1 - if initialization was unsuccessful. |
| 59 | ******************************************************************************/ |
| 60 | char ad7685_Init(void) |
| 61 | { |
| 62 | char status = -1; |
| 63 | |
| 64 | /* Setup SPI Interface */ |
| 65 | status = SPI_Init(0, 1000000, 1, 0); |
| 66 | |
| 67 | return status; |
| 68 | } |
| 69 | |
| 70 | /**************************************************************************//** |
| 71 | * @brief Makes a conversion for a number of devices daisy-chained (only works |
| 72 | * for Chain Mode, no busy indicator). |
| 73 | * |
| 74 | * @param partsNo - number of devices daisy-chained. |
| 75 | * *dataDac - pointer to the output values of each device. |
| 76 | * |
| 77 | * @return rxData - Received data in case of success. |
| 78 | ******************************************************************************/ |
| 79 | char ad7685_GetConversion(char partsNo, unsigned short *dataDac) |
| 80 | { |
| 81 | unsigned char bytesRead = 0; |
| 82 | unsigned char bytesNo = 0; |
| 83 | unsigned char data[] = {0,}; |
| 84 | unsigned char count = 0; |
| 85 | |
| 86 | bytesNo = partsNo * 2; |
| 87 | bytesRead = SPI_Read(ad7685_SLAVE_ID, data, bytesNo); |
| 88 | if (bytesRead != bytesNo) |
| 89 | { |
| 90 | return -1; |
| 91 | } |
| 92 | for (count = 0; count < bytesNo; count++) |
| 93 | { |
| 94 | dataDac[count] = (data[2 * count] << 8) + (data[2 * count + 1] << 0); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /***************************************************************************//** |
| 101 | * @brief Converts a 16-bit raw sample to volts. |
| 102 | * |
| 103 | * @param rawSample - 16-bit data sample. |
| 104 | * @param vRef - The value of the voltage reference used by the device. |
| 105 | * |
| 106 | * @return voltage - The result of the conversion expressed as volts. |
| 107 | *******************************************************************************/ |
| 108 | float ad7685_ConvertToVolts(unsigned short rawSample, float vRef) |
| 109 | { |
| 110 | float voltage = 0; |
| 111 | |
| 112 | voltage = vRef * (float)rawSample / (1ul << 16); |
| 113 | |
| 114 | return voltage; |
| 115 | } |