| 1 | /***************************************************************************//** |
| 2 | * @file ADXRS453.c |
| 3 | * @brief Implementation of ADXRS453 Driver. |
| 4 | * @author ATofan ([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 "system_config.h" |
| 47 | #include "ADXRS453.h" |
| 48 | |
| 49 | volatile int rxData = 0; |
| 50 | |
| 51 | extern u32 (*SPI_Init)(u32, char, char, char); |
| 52 | extern u32 (*SPI_TransferData)(u32, char, char*, char, char*, char); |
| 53 | |
| 54 | /****************************************************************************** |
| 55 | * @brief Send and Read Data over SPI to/from the ADXRS453 |
| 56 | * |
| 57 | * @param txData - Data to be sent. |
| 58 | * |
| 59 | * @return retValue - Data read from the ADXRS453. |
| 60 | ******************************************************************************/ |
| 61 | int ADXRS453_SendData(int txData) |
| 62 | { |
| 63 | char txBuffer[4] = {0x00, 0x00, 0x00, 0x00}; |
| 64 | char rxBuffer[4] = {0x00, 0x00, 0x00, 0x00}; |
| 65 | int cfgValue = txData; |
| 66 | int retValue = 0; |
| 67 | |
| 68 | txBuffer[0] = ((cfgValue & 0xFF000000) >> 24); |
| 69 | txBuffer[1] = ((cfgValue & 0x00FF0000) >> 16); |
| 70 | txBuffer[2] = ((cfgValue & 0x0000FF00) >> 8); |
| 71 | txBuffer[3] = ((cfgValue & 0x000000FF) >> 0); |
| 72 | |
| 73 | SPI_TransferData(SPI_BASEADDR, 4, txBuffer, 4, rxBuffer, 1); |
| 74 | |
| 75 | retValue = ((rxBuffer[0] << 24) | (rxBuffer[1] << 16) | (rxBuffer[2] << 8) | (rxBuffer[3] << 0)); |
| 76 | |
| 77 | return(retValue); |
| 78 | } |
| 79 | |
| 80 | /****************************************************************************** |
| 81 | * @brief Initialize the ADXRS453. |
| 82 | * |
| 83 | * @param None. |
| 84 | * |
| 85 | * @return None. |
| 86 | ******************************************************************************/ |
| 87 | void ADXRS453_Init(void) |
| 88 | { |
| 89 | ADXRS453_SendData(0x20000003); |
| 90 | delay_ms(50); |
| 91 | ADXRS453_SendData(0x20000000); |
| 92 | delay_ms(50); |
| 93 | ADXRS453_SendData(0x20000000); |
| 94 | ADXRS453_SendData(0x20000000); |
| 95 | } |
| 96 | |
| 97 | /****************************************************************************** |
| 98 | * @brief Read degrees/second register from the ADXRS453 |
| 99 | * |
| 100 | * @param None. |
| 101 | * |
| 102 | * @return rxDeg - Value of register. |
| 103 | ******************************************************************************/ |
| 104 | int ADXRS453_ReadDegrees(void) |
| 105 | { |
| 106 | int rxDeg = 0; |
| 107 | rxDeg = ADXRS453_SendData(0x20000000); |
| 108 | |
| 109 | if(((rxDeg & 0xC000000) == 0x4000000)&&((rxDeg & 0x00000004) != 0x00000004)) |
| 110 | { |
| 111 | return(rxDeg); |
| 112 | } |
| 113 | |
| 114 | return(rxDeg); |
| 115 | } |
| 116 | |
| 117 | /****************************************************************************** |
| 118 | * @brief Print the Degrees/Second. |
| 119 | * |
| 120 | * @param x - Degrees/Second |
| 121 | ******************************************************************************/ |
| 122 | void PrintDegreesPos(int x) |
| 123 | { |
| 124 | char c[] = {' ',' ',' ','0','.','0','0','0','0',0}; |
| 125 | int nr; |
| 126 | int i = 8; |
| 127 | |
| 128 | // If the number is in negative degree range |
| 129 | // set '-' as the beginning character in string |
| 130 | // and set the corresponding value |
| 131 | if (x > 32767 * 125) |
| 132 | { |
| 133 | nr = (0x10000 * 125) - x; |
| 134 | c[0] = '-'; |
| 135 | } |
| 136 | |
| 137 | // If the number is in positive degree range |
| 138 | // set '+' as the beginning character in string |
| 139 | // and set the corresponding value |
| 140 | else |
| 141 | { |
| 142 | nr = x; |
| 143 | c[0] = '+'; |
| 144 | } |
| 145 | |
| 146 | // Extract mod 10 values from the number |
| 147 | // and place them in the corresponding |
| 148 | // position in the string, with ASCII conversion |
| 149 | while(nr > 0) |
| 150 | { |
| 151 | c[i] = (nr % 10) + 48; |
| 152 | nr = nr / 10; |
| 153 | i = i - 1; |
| 154 | // Skip the 5th position (it is pre-loaded with '.' |
| 155 | if(i == 4) |
| 156 | { |
| 157 | i = i-1; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Print the corresponding value on UART |
| 162 | xil_printf("Degrees / Second = %s\n\r",c); |
| 163 | } |
| 164 | |
| 165 | /****************************************************************************** |
| 166 | * @brief Demo program for the ADXRS453 |
| 167 | * |
| 168 | * @param None. |
| 169 | * |
| 170 | * @return None. |
| 171 | ******************************************************************************/ |
| 172 | void DemoProgram(void) |
| 173 | { |
| 174 | xil_printf("PmodGYRO2 Demo Program\n\r"); |
| 175 | |
| 176 | while(1) |
| 177 | { |
| 178 | rxData = ADXRS453_ReadDegrees(); |
| 179 | PrintDegreesPos(((rxData & 0x3FFFC00) >> 10)*125); |
| 180 | } |
| 181 | } |