| 1 | /**************************************************************************//** |
| 2 | * @file ad5751.c |
| 3 | * @brief Implementation of ad5751 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 "ad5751.h" |
| 46 | |
| 47 | const unsigned char RANGE_CONFIG[15] = {0x05, // 0-5V |
| 48 | 0x06, // 0-10V |
| 49 | 0x09, // 0-6V (20% overrange) |
| 50 | 0x0A, // 0-12V (20% overrange) |
| 51 | 0x0E, // 0-40V |
| 52 | 0x0F, // 0-44V |
| 53 | 0x00, // 4-20mA (External 15 k current sense resistor) |
| 54 | 0x10, // 4-20mA (Internal current sense resistor) |
| 55 | 0x01, // 0-20mA (External 15 k current sense resistor) |
| 56 | 0x11, // 0-20mA (Internal current sense resistor) |
| 57 | 0x02, // 0-24mA (External 15 k current sense resistor) |
| 58 | 0x12, // 0-24mA (Internal current sense resistor) |
| 59 | 0x1D, // 3.92-20.4mA (Internal current sense resistor) |
| 60 | 0x1E, // 0-20.4mA (Internal current sense resistor) |
| 61 | 0x1F};// 0-24.5mA (Internal current sense resistor) |
| 62 | |
| 63 | /******************************************************************************/ |
| 64 | /*************************** Functions Definitions ****************************/ |
| 65 | /******************************************************************************/ |
| 66 | |
| 67 | /**************************************************************************//** |
| 68 | * @brief Initializes the communication with the device. |
| 69 | * |
| 70 | * @param None. |
| 71 | * |
| 72 | * @return status - Result of the initialization procedure. |
| 73 | * Example: 0 - if initialization was successful; |
| 74 | * -1 - if initialization was unsuccessful. |
| 75 | ******************************************************************************/ |
| 76 | char ad5751_Init(void) |
| 77 | { |
| 78 | char status = -1; |
| 79 | |
| 80 | /* Setup SPI Interface */ |
| 81 | status = SPI_Init(0, 1000000, 1, 1); |
| 82 | ad5751_CLR_OUT; |
| 83 | ad5751_CLR_LOW; |
| 84 | ad5751_FAULT_IN; |
| 85 | |
| 86 | return status; |
| 87 | } |
| 88 | |
| 89 | /***************************************************************************//** |
| 90 | * @brief Writes 16 bits to device through SPI. |
| 91 | * |
| 92 | * @param data - Data which will be transmitted. |
| 93 | * |
| 94 | * @return status - Result of the write operation. |
| 95 | * Example: 0 - if operation was successful; |
| 96 | * -1 - if operation was unsuccessful. |
| 97 | *******************************************************************************/ |
| 98 | char ad5751_Write16Bits(unsigned short data) |
| 99 | { |
| 100 | unsigned char bytesWritten = 0; |
| 101 | char status = 0; |
| 102 | unsigned char dataBuf[2] = {(data & 0xFF00) >> 8, |
| 103 | (data & 0x00FF) >> 0}; |
| 104 | |
| 105 | bytesWritten = SPI_Write(ad5751_SLAVE_ID, dataBuf, 2); |
| 106 | if (bytesWritten != 2) |
| 107 | { |
| 108 | status = -1; |
| 109 | } |
| 110 | |
| 111 | return status; |
| 112 | } |
| 113 | |
| 114 | /***************************************************************************//** |
| 115 | * @brief Reads 16 bits from the device through SPI. |
| 116 | * |
| 117 | * @param data - Data which will be transmitted (MOSI pin). |
| 118 | * |
| 119 | * @return bytesRead - received data, in case of success. |
| 120 | *******************************************************************************/ |
| 121 | unsigned short ad5751_Read16Bits(unsigned short data) |
| 122 | { |
| 123 | unsigned char bytesRead = 0; |
| 124 | unsigned short receivedData; |
| 125 | unsigned char dataBuf[2] = {(data & 0xFF00) >> 8, |
| 126 | (data & 0x00FF) >> 0}; |
| 127 | |
| 128 | bytesRead = SPI_Read(ad5751_SLAVE_ID, dataBuf, 2); |
| 129 | if(bytesRead != 2) |
| 130 | { |
| 131 | return -1; |
| 132 | } |
| 133 | receivedData = ((unsigned short)dataBuf[0] << 8) + ((unsigned short)dataBuf[1] << 0); |
| 134 | |
| 135 | return receivedData; |
| 136 | } |