Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ADMX2001.cpp@9:f286301109fb, 2021-10-27 (annotated)
- Committer:
- nsheth
- Date:
- Wed Oct 27 21:17:37 2021 +0000
- Revision:
- 9:f286301109fb
- Parent:
- 6:7d8f30b3bc57
- Child:
- 10:49537b1dbbd7
Minor refactoriing
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nsheth | 9:f286301109fb | 1 | /* Copyright (c) 2021 Analog Devices, Inc. All rights reserved. |
| nsheth | 9:f286301109fb | 2 | |
| nsheth | 9:f286301109fb | 3 | Redistribution and use in source and binary forms, with or without modification, |
| nsheth | 9:f286301109fb | 4 | are permitted provided that the following conditions are met: |
| nsheth | 9:f286301109fb | 5 | - Redistributions of source code must retain the above copyright notice, |
| nsheth | 9:f286301109fb | 6 | this list of conditions and the following disclaimer. |
| nsheth | 9:f286301109fb | 7 | - Redistributions in binary form must reproduce the above copyright notice, |
| nsheth | 9:f286301109fb | 8 | this list of conditions and the following disclaimer in the documentation |
| nsheth | 9:f286301109fb | 9 | and/or other materials provided with the distribution. |
| nsheth | 9:f286301109fb | 10 | - Modified versions of the software must be conspicuously marked as such. |
| nsheth | 9:f286301109fb | 11 | - This software is licensed solely and exclusively for use with processors/products |
| nsheth | 9:f286301109fb | 12 | manufactured by or for Analog Devices, Inc. |
| nsheth | 9:f286301109fb | 13 | - This software may not be combined or merged with other code in any manner |
| nsheth | 9:f286301109fb | 14 | that would cause the software to become subject to terms and conditions which |
| nsheth | 9:f286301109fb | 15 | differ from those listed here. |
| nsheth | 9:f286301109fb | 16 | - Neither the name of Analog Devices, Inc. nor the names of its contributors |
| nsheth | 9:f286301109fb | 17 | may be used to endorse or promote products derived from this software without |
| nsheth | 9:f286301109fb | 18 | specific prior written permission. |
| nsheth | 9:f286301109fb | 19 | - The use of this software may or may not infringe the patent rights of one or |
| nsheth | 9:f286301109fb | 20 | more patent holders. This license does not release you from the requirement |
| nsheth | 9:f286301109fb | 21 | that you obtain separate licenses from these patent holders to use this software. |
| nsheth | 9:f286301109fb | 22 | |
| nsheth | 9:f286301109fb | 23 | THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND |
| nsheth | 9:f286301109fb | 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
| nsheth | 9:f286301109fb | 25 | TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
| nsheth | 9:f286301109fb | 26 | NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| nsheth | 9:f286301109fb | 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES |
| nsheth | 9:f286301109fb | 28 | (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL |
| nsheth | 9:f286301109fb | 29 | PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| nsheth | 9:f286301109fb | 30 | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| nsheth | 9:f286301109fb | 31 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| nsheth | 9:f286301109fb | 32 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| nsheth | 9:f286301109fb | 33 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| nsheth | 9:f286301109fb | 34 | |
| nsheth | 9:f286301109fb | 35 | 2021-01-10-7CBSD SLA |
| nsheth | 9:f286301109fb | 36 | */ |
| nsheth | 2:4af4940055a7 | 37 | /** |
| nsheth | 9:f286301109fb | 38 | * @file ADMX2001.cpp |
| nsheth | 2:4af4940055a7 | 39 | * @brief This file contains admx200x APIs for sdpk1 host |
| nsheth | 2:4af4940055a7 | 40 | * @addtogroup SDPK1 |
| nsheth | 2:4af4940055a7 | 41 | * @{ |
| nsheth | 2:4af4940055a7 | 42 | */ |
| nsheth | 2:4af4940055a7 | 43 | /*============= I N C L U D E S =============*/ |
| nsheth | 4:eb7a23c25751 | 44 | #include "ADMX2001.h" |
| nsheth | 4:eb7a23c25751 | 45 | #include "ADMX2001_commands.h" |
| nsheth | 2:4af4940055a7 | 46 | #include "message.h" |
| nsheth | 2:4af4940055a7 | 47 | #include <stdlib.h> |
| nsheth | 2:4af4940055a7 | 48 | #include <string.h> |
| nsheth | 2:4af4940055a7 | 49 | |
| nsheth | 2:4af4940055a7 | 50 | static uint32_t SwapEndian(uint32_t *pData); |
| nsheth | 2:4af4940055a7 | 51 | /** milli second delay**/ |
| nsheth | 2:4af4940055a7 | 52 | static void Admx200xDelay(uint32_t msec); |
| nsheth | 2:4af4940055a7 | 53 | |
| nsheth | 2:4af4940055a7 | 54 | /** |
| nsheth | 2:4af4940055a7 | 55 | * Swaps endian of a 32 bit number |
| nsheth | 2:4af4940055a7 | 56 | * @param data - 32 bit number |
| nsheth | 2:4af4940055a7 | 57 | * @return |
| nsheth | 2:4af4940055a7 | 58 | */ |
| nsheth | 2:4af4940055a7 | 59 | uint32_t SwapEndian(uint32_t *pData) |
| nsheth | 2:4af4940055a7 | 60 | { |
| nsheth | 2:4af4940055a7 | 61 | uint8_t *pDataU8 = (uint8_t *)pData; |
| nsheth | 2:4af4940055a7 | 62 | |
| nsheth | 2:4af4940055a7 | 63 | return (uint32_t)pDataU8[0] << 24 | (uint32_t)pDataU8[1] << 16 | |
| nsheth | 2:4af4940055a7 | 64 | (uint32_t)pDataU8[2] << 8 | (uint32_t)pDataU8[3]; |
| nsheth | 2:4af4940055a7 | 65 | } |
| nsheth | 2:4af4940055a7 | 66 | |
| nsheth | 2:4af4940055a7 | 67 | /** |
| nsheth | 2:4af4940055a7 | 68 | * @brief Initializes the ADMX200X. |
| nsheth | 2:4af4940055a7 | 69 | * |
| nsheth | 2:4af4940055a7 | 70 | * @param pDevice - The device structure. |
| nsheth | 2:4af4940055a7 | 71 | * @param pSpiDesc - Pointer to spi device |
| nsheth | 2:4af4940055a7 | 72 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 73 | */ |
| nsheth | 2:4af4940055a7 | 74 | int32_t Admx200xInit(Admx200xDev *pDevice, spi_desc *pSpiDesc) |
| nsheth | 2:4af4940055a7 | 75 | { |
| nsheth | 2:4af4940055a7 | 76 | int32_t ret = 0; |
| nsheth | 2:4af4940055a7 | 77 | |
| nsheth | 2:4af4940055a7 | 78 | pDevice->pSpiDesc = pSpiDesc; |
| nsheth | 2:4af4940055a7 | 79 | |
| nsheth | 2:4af4940055a7 | 80 | return ret; |
| nsheth | 2:4af4940055a7 | 81 | } |
| nsheth | 2:4af4940055a7 | 82 | |
| nsheth | 2:4af4940055a7 | 83 | /** |
| nsheth | 2:4af4940055a7 | 84 | * Wrapper fro delay |
| nsheth | 2:4af4940055a7 | 85 | * @param msecs - delay in milliseconds |
| nsheth | 2:4af4940055a7 | 86 | */ |
| nsheth | 2:4af4940055a7 | 87 | void Admx200xDelay(uint32_t msecs) |
| nsheth | 2:4af4940055a7 | 88 | { |
| nsheth | 2:4af4940055a7 | 89 | mdelay(msecs); |
| nsheth | 2:4af4940055a7 | 90 | } |
| nsheth | 2:4af4940055a7 | 91 | |
| nsheth | 2:4af4940055a7 | 92 | /** |
| nsheth | 2:4af4940055a7 | 93 | * @brief Create a command payload in buffer with inputs. |
| nsheth | 2:4af4940055a7 | 94 | * |
| nsheth | 2:4af4940055a7 | 95 | * @param pCmdId - Command ID. |
| nsheth | 2:4af4940055a7 | 96 | * @param pAddr - Sub commands addresses. |
| nsheth | 2:4af4940055a7 | 97 | * @param pData - Data to wildcat. |
| nsheth | 2:4af4940055a7 | 98 | * @param pBuffer - Data to wildcat. |
| nsheth | 2:4af4940055a7 | 99 | * |
| nsheth | 2:4af4940055a7 | 100 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 101 | */ |
| nsheth | 2:4af4940055a7 | 102 | int32_t Admx200xCreateCmdPayload(uint8_t *pCmdId, uint16_t *pAddr, |
| nsheth | 2:4af4940055a7 | 103 | uint32_t *pData, uint8_t *pBuffer) |
| nsheth | 2:4af4940055a7 | 104 | { |
| nsheth | 2:4af4940055a7 | 105 | int32_t ret = 0; |
| nsheth | 5:b9608e6cc24b | 106 | //DEBUG_MSG("cmd:%d,addr:%d, data: 0x%x\n\r", *pCmdId, *pAddr, *pData); |
| nsheth | 2:4af4940055a7 | 107 | pBuffer[0] = pCmdId[0]; |
| nsheth | 2:4af4940055a7 | 108 | pBuffer[1] = ((uint8_t *)pAddr)[1]; |
| nsheth | 2:4af4940055a7 | 109 | pBuffer[2] = ((uint8_t *)pAddr)[0]; |
| nsheth | 2:4af4940055a7 | 110 | pBuffer[3] = ((uint8_t *)pData)[3]; |
| nsheth | 2:4af4940055a7 | 111 | pBuffer[4] = ((uint8_t *)pData)[2]; |
| nsheth | 2:4af4940055a7 | 112 | pBuffer[5] = ((uint8_t *)pData)[1]; |
| nsheth | 2:4af4940055a7 | 113 | pBuffer[6] = ((uint8_t *)pData)[0]; |
| nsheth | 2:4af4940055a7 | 114 | |
| nsheth | 2:4af4940055a7 | 115 | return ret; |
| nsheth | 2:4af4940055a7 | 116 | } |
| nsheth | 2:4af4940055a7 | 117 | |
| nsheth | 2:4af4940055a7 | 118 | /** |
| nsheth | 2:4af4940055a7 | 119 | * @brief Parse the payload to get cmdID, addr and data. |
| nsheth | 2:4af4940055a7 | 120 | |
| nsheth | 2:4af4940055a7 | 121 | * @param[in] pBuffer - Data from wildcat. |
| nsheth | 2:4af4940055a7 | 122 | * @param[out] pCmdId - Pointer to store Command ID. |
| nsheth | 2:4af4940055a7 | 123 | * @param[out] pAddr - Pointer to store addresses. |
| nsheth | 2:4af4940055a7 | 124 | * @param[out] pData - Pointer to store Data |
| nsheth | 2:4af4940055a7 | 125 | * |
| nsheth | 2:4af4940055a7 | 126 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 127 | */ |
| nsheth | 2:4af4940055a7 | 128 | int32_t Admx200xParseCmdPayload(uint8_t *pBuffer, uint8_t *pCmdId, |
| nsheth | 2:4af4940055a7 | 129 | uint16_t *pAddr, uint32_t *pData) |
| nsheth | 2:4af4940055a7 | 130 | { |
| nsheth | 2:4af4940055a7 | 131 | int32_t ret = 0; |
| nsheth | 2:4af4940055a7 | 132 | |
| nsheth | 2:4af4940055a7 | 133 | *pCmdId = pBuffer[0]; |
| nsheth | 2:4af4940055a7 | 134 | *pAddr = ((uint16_t)pBuffer[1] << 8) | ((uint16_t)pBuffer[2]); |
| nsheth | 2:4af4940055a7 | 135 | |
| nsheth | 2:4af4940055a7 | 136 | *pData = SwapEndian((uint32_t *)&pBuffer[3]); |
| nsheth | 2:4af4940055a7 | 137 | |
| nsheth | 2:4af4940055a7 | 138 | return ret; |
| nsheth | 2:4af4940055a7 | 139 | } |
| nsheth | 2:4af4940055a7 | 140 | |
| nsheth | 2:4af4940055a7 | 141 | /** |
| nsheth | 2:4af4940055a7 | 142 | * @brief Waits till done bit is set in the status |
| nsheth | 2:4af4940055a7 | 143 | * |
| nsheth | 2:4af4940055a7 | 144 | * @param pDevice - The handler of the instance of the driver. |
| nsheth | 2:4af4940055a7 | 145 | * @param timeout - Count representing the number of polls to be done until the |
| nsheth | 2:4af4940055a7 | 146 | * function returns if no new data is available. |
| nsheth | 2:4af4940055a7 | 147 | * @param reqStatus - The status to be checked is passed via this argument. |
| nsheth | 2:4af4940055a7 | 148 | * @param pStatReg - Get the status word in pStatReg. |
| nsheth | 2:4af4940055a7 | 149 | * |
| nsheth | 2:4af4940055a7 | 150 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 151 | */ |
| nsheth | 2:4af4940055a7 | 152 | int32_t Admx200xWaitForStatus(Admx200xDev *pDevice, uint32_t timeout, |
| nsheth | 2:4af4940055a7 | 153 | uint32_t reqStatus, uint32_t *pStatReg) |
| nsheth | 2:4af4940055a7 | 154 | { |
| nsheth | 2:4af4940055a7 | 155 | int32_t status = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 156 | int8_t done = 0; |
| nsheth | 2:4af4940055a7 | 157 | uint8_t cmdId = CMD_STATUS_READ; |
| nsheth | 2:4af4940055a7 | 158 | |
| nsheth | 2:4af4940055a7 | 159 | if (pDevice != NULL) |
| nsheth | 2:4af4940055a7 | 160 | { |
| nsheth | 2:4af4940055a7 | 161 | while (!done && --timeout) |
| nsheth | 2:4af4940055a7 | 162 | { |
| nsheth | 2:4af4940055a7 | 163 | |
| nsheth | 2:4af4940055a7 | 164 | status = Admx200xReadData(pDevice, cmdId, pStatReg); |
| nsheth | 2:4af4940055a7 | 165 | |
| nsheth | 2:4af4940055a7 | 166 | if (status == 0) |
| nsheth | 2:4af4940055a7 | 167 | { |
| nsheth | 2:4af4940055a7 | 168 | /* Check the DONE bit in the Status Register */ |
| nsheth | 2:4af4940055a7 | 169 | if (((*pStatReg) & reqStatus) == reqStatus) |
| nsheth | 2:4af4940055a7 | 170 | { |
| nsheth | 2:4af4940055a7 | 171 | done = 1; |
| nsheth | 2:4af4940055a7 | 172 | if (*pStatReg & ADMX200X_STATUS_ERROR_BITM) |
| nsheth | 2:4af4940055a7 | 173 | { |
| nsheth | 2:4af4940055a7 | 174 | status = *pStatReg & ADMX200X_STATUS_CODE_BITM; |
| nsheth | 2:4af4940055a7 | 175 | if (status & ADMX_STATUS_LOG_ZERO_ERROR) |
| nsheth | 2:4af4940055a7 | 176 | { |
| nsheth | 2:4af4940055a7 | 177 | ERROR_MSG("sweep_start/sweep_end cannot be zero " |
| nsheth | 2:4af4940055a7 | 178 | "for logarithmic sweep"); |
| nsheth | 2:4af4940055a7 | 179 | status &= (~ADMX_STATUS_LOG_ZERO_ERROR); |
| nsheth | 2:4af4940055a7 | 180 | } |
| nsheth | 2:4af4940055a7 | 181 | if (status & ADMX_STATUS_LOG_SIGN_ERROR) |
| nsheth | 2:4af4940055a7 | 182 | { |
| nsheth | 2:4af4940055a7 | 183 | ERROR_MSG("sweep_start and sweep_end need to have " |
| nsheth | 2:4af4940055a7 | 184 | "the same sign " |
| nsheth | 2:4af4940055a7 | 185 | "for logarithmic sweep"); |
| nsheth | 2:4af4940055a7 | 186 | status &= (~ADMX_STATUS_LOG_SIGN_ERROR); |
| nsheth | 2:4af4940055a7 | 187 | } |
| nsheth | 2:4af4940055a7 | 188 | if (status & ADMX_STATUS_VOLT_ADC_ERROR) |
| nsheth | 2:4af4940055a7 | 189 | { |
| nsheth | 2:4af4940055a7 | 190 | ERROR_MSG("Voltage ADC Saturated\n"); |
| nsheth | 2:4af4940055a7 | 191 | status &= (~ADMX_STATUS_VOLT_ADC_ERROR); |
| nsheth | 2:4af4940055a7 | 192 | } |
| nsheth | 2:4af4940055a7 | 193 | if (status & ADMX_STATUS_CURR_ADC_ERROR) |
| nsheth | 2:4af4940055a7 | 194 | { |
| nsheth | 2:4af4940055a7 | 195 | ERROR_MSG("Current ADC Saturated\n"); |
| nsheth | 2:4af4940055a7 | 196 | status &= (~ADMX_STATUS_CURR_ADC_ERROR); |
| nsheth | 2:4af4940055a7 | 197 | } |
| nsheth | 2:4af4940055a7 | 198 | if (status & ADMX_STATUS_FIFO_ERROR) |
| nsheth | 2:4af4940055a7 | 199 | { |
| nsheth | 2:4af4940055a7 | 200 | ERROR_MSG( |
| nsheth | 2:4af4940055a7 | 201 | "FIFO either Overflowed or Underflowed\n"); |
| nsheth | 2:4af4940055a7 | 202 | status &= (~ADMX_STATUS_FIFO_ERROR); |
| nsheth | 2:4af4940055a7 | 203 | } |
| nsheth | 2:4af4940055a7 | 204 | if (status & ADMX_STATUS_COUNT_EXCEEDED) |
| nsheth | 2:4af4940055a7 | 205 | { |
| nsheth | 2:4af4940055a7 | 206 | ERROR_MSG("Sweep count exceeded maximum value"); |
| nsheth | 2:4af4940055a7 | 207 | status &= (~ADMX_STATUS_COUNT_EXCEEDED); |
| nsheth | 2:4af4940055a7 | 208 | } |
| nsheth | 2:4af4940055a7 | 209 | } |
| nsheth | 2:4af4940055a7 | 210 | } |
| nsheth | 2:4af4940055a7 | 211 | } |
| nsheth | 2:4af4940055a7 | 212 | } |
| nsheth | 2:4af4940055a7 | 213 | |
| nsheth | 2:4af4940055a7 | 214 | if (!timeout) |
| nsheth | 2:4af4940055a7 | 215 | { |
| nsheth | 2:4af4940055a7 | 216 | status = ADMX_STATUS_TIMEOUT; |
| nsheth | 2:4af4940055a7 | 217 | } |
| nsheth | 2:4af4940055a7 | 218 | } |
| nsheth | 2:4af4940055a7 | 219 | else |
| nsheth | 2:4af4940055a7 | 220 | { |
| nsheth | 2:4af4940055a7 | 221 | status = ADMX_STATUS_FAILED; |
| nsheth | 2:4af4940055a7 | 222 | } |
| nsheth | 2:4af4940055a7 | 223 | return status; |
| nsheth | 2:4af4940055a7 | 224 | } |
| nsheth | 2:4af4940055a7 | 225 | |
| nsheth | 2:4af4940055a7 | 226 | /** |
| nsheth | 2:4af4940055a7 | 227 | * @brief Sends teh command to ADMX200x and wait till done bit is set |
| nsheth | 2:4af4940055a7 | 228 | * @param pDevice - Device Handle |
| nsheth | 2:4af4940055a7 | 229 | * @param pCmdId - The command ID |
| nsheth | 2:4af4940055a7 | 230 | * @param pAddr - Address |
| nsheth | 2:4af4940055a7 | 231 | * @param pData - Data |
| nsheth | 2:4af4940055a7 | 232 | * @param pStatReg - Status register info |
| nsheth | 2:4af4940055a7 | 233 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 234 | */ |
| nsheth | 2:4af4940055a7 | 235 | int32_t Admx200xSendCmd(Admx200xDev *pDevice, uint8_t *pCmdId, uint16_t *pAddr, |
| nsheth | 2:4af4940055a7 | 236 | uint32_t *pData, uint32_t *pStatReg) |
| nsheth | 2:4af4940055a7 | 237 | { |
| nsheth | 2:4af4940055a7 | 238 | |
| nsheth | 2:4af4940055a7 | 239 | int32_t status = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 240 | uint8_t cmdPayload[ADMX200X_CMD_LENGTH]; |
| nsheth | 2:4af4940055a7 | 241 | |
| nsheth | 2:4af4940055a7 | 242 | Admx200xCreateCmdPayload(pCmdId, pAddr, pData, &cmdPayload[0]); |
| nsheth | 2:4af4940055a7 | 243 | |
| nsheth | 2:4af4940055a7 | 244 | status = spi_write_and_read(pDevice->pSpiDesc, &cmdPayload[0], |
| nsheth | 2:4af4940055a7 | 245 | ADMX200X_CMD_LENGTH); |
| nsheth | 2:4af4940055a7 | 246 | Admx200xDelay(100); |
| nsheth | 2:4af4940055a7 | 247 | // printf("CMD :%02x ADDR: %04x DATA: %08x\n\r",*pCmdId, *pAddr, *pData); |
| nsheth | 2:4af4940055a7 | 248 | if (!status) |
| nsheth | 2:4af4940055a7 | 249 | { |
| nsheth | 2:4af4940055a7 | 250 | /** Wait for status done = 1,Warn/error/command result = 0 from result |
| nsheth | 2:4af4940055a7 | 251 | */ |
| nsheth | 2:4af4940055a7 | 252 | status = Admx200xWaitForStatus(pDevice, SPI_TIMEOUT, |
| nsheth | 2:4af4940055a7 | 253 | ADMX200X_STATUS_DONE_BITM, pStatReg); |
| nsheth | 2:4af4940055a7 | 254 | |
| nsheth | 6:7d8f30b3bc57 | 255 | //printf("Stat Reg: %08x\n\r", *pStatReg); |
| nsheth | 2:4af4940055a7 | 256 | if (status == ADMX_STATUS_SUCCESS) |
| nsheth | 2:4af4940055a7 | 257 | { |
| nsheth | 2:4af4940055a7 | 258 | status = *pStatReg & ADMX200X_STATUS_CODE_BITM; |
| nsheth | 2:4af4940055a7 | 259 | } |
| nsheth | 2:4af4940055a7 | 260 | } |
| nsheth | 2:4af4940055a7 | 261 | if (status == ADMX_STATUS_TIMEOUT) |
| nsheth | 2:4af4940055a7 | 262 | { |
| nsheth | 2:4af4940055a7 | 263 | ERROR_MSG("SPI interface timed out : Not responding"); |
| nsheth | 2:4af4940055a7 | 264 | } |
| nsheth | 2:4af4940055a7 | 265 | return status; |
| nsheth | 2:4af4940055a7 | 266 | } |
| nsheth | 2:4af4940055a7 | 267 | |
| nsheth | 2:4af4940055a7 | 268 | /** |
| nsheth | 2:4af4940055a7 | 269 | * @brief Read result register from the WILDCAT |
| nsheth | 2:4af4940055a7 | 270 | * @param pDevice - Device Handle |
| nsheth | 2:4af4940055a7 | 271 | * @param cmdId - This determines where the data is read from - result |
| nsheth | 2:4af4940055a7 | 272 | * register, status register or Fifo |
| nsheth | 2:4af4940055a7 | 273 | * @param pResult - The data in the result register |
| nsheth | 2:4af4940055a7 | 274 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 275 | */ |
| nsheth | 2:4af4940055a7 | 276 | int32_t Admx200xReadData(Admx200xDev *pDevice, uint8_t cmdId, uint32_t *pResult) |
| nsheth | 2:4af4940055a7 | 277 | { |
| nsheth | 2:4af4940055a7 | 278 | int32_t ret = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 279 | uint8_t cmdTemp; |
| nsheth | 2:4af4940055a7 | 280 | uint16_t addrTemp; |
| nsheth | 2:4af4940055a7 | 281 | uint16_t addr; |
| nsheth | 2:4af4940055a7 | 282 | uint32_t data; |
| nsheth | 2:4af4940055a7 | 283 | uint8_t cmdPayload[ADMX200X_CMD_LENGTH]; |
| nsheth | 2:4af4940055a7 | 284 | |
| nsheth | 2:4af4940055a7 | 285 | /*FIXME: Insert checks here. Not all command IDs are accepted here */ |
| nsheth | 2:4af4940055a7 | 286 | addr = 0; |
| nsheth | 2:4af4940055a7 | 287 | data = 0; |
| nsheth | 2:4af4940055a7 | 288 | |
| nsheth | 2:4af4940055a7 | 289 | Admx200xCreateCmdPayload(&cmdId, &addr, &data, &cmdPayload[0]); |
| nsheth | 2:4af4940055a7 | 290 | |
| nsheth | 2:4af4940055a7 | 291 | /* Send command and Read response from the slave */ |
| nsheth | 2:4af4940055a7 | 292 | ret = spi_write_and_read(pDevice->pSpiDesc, &cmdPayload[0], |
| nsheth | 2:4af4940055a7 | 293 | ADMX200X_CMD_LENGTH); |
| nsheth | 2:4af4940055a7 | 294 | Admx200xDelay(100); |
| nsheth | 2:4af4940055a7 | 295 | Admx200xParseCmdPayload(&cmdPayload[0], &cmdTemp, &addrTemp, pResult); |
| nsheth | 2:4af4940055a7 | 296 | |
| nsheth | 2:4af4940055a7 | 297 | return ret; |
| nsheth | 2:4af4940055a7 | 298 | } |
| nsheth | 2:4af4940055a7 | 299 | |
| nsheth | 2:4af4940055a7 | 300 | /** |
| nsheth | 2:4af4940055a7 | 301 | * @brief Read array of datatype doube from the fifo of the WILDCAT |
| nsheth | 2:4af4940055a7 | 302 | * @param pDevice - Device Handle |
| nsheth | 2:4af4940055a7 | 303 | * @param pFifo - Double array that stores the data |
| nsheth | 2:4af4940055a7 | 304 | * @param pCount - Stores the number of double values stored in the pFifo array |
| nsheth | 2:4af4940055a7 | 305 | */ |
| nsheth | 2:4af4940055a7 | 306 | int32_t Admx200xReadFifo(Admx200xDev *pDevice, double *pFifo, int32_t *pCount) |
| nsheth | 2:4af4940055a7 | 307 | { |
| nsheth | 2:4af4940055a7 | 308 | int32_t status = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 309 | int32_t i; |
| nsheth | 2:4af4940055a7 | 310 | uint32_t *pData = (uint32_t *)pFifo; |
| nsheth | 2:4af4940055a7 | 311 | uint8_t cmdId = CMD_STATUS_READ; |
| nsheth | 2:4af4940055a7 | 312 | uint32_t statusReg; |
| nsheth | 2:4af4940055a7 | 313 | int32_t numDoublesInFifo; |
| nsheth | 2:4af4940055a7 | 314 | |
| nsheth | 2:4af4940055a7 | 315 | status = Admx200xReadData(pDevice, cmdId, &statusReg); |
| nsheth | 2:4af4940055a7 | 316 | |
| nsheth | 2:4af4940055a7 | 317 | /* Fifo depth in doubles */ |
| nsheth | 2:4af4940055a7 | 318 | numDoublesInFifo = (statusReg & ADMX200X_STATUS_FIFO_DEPTH_BITM) >> 17; |
| nsheth | 2:4af4940055a7 | 319 | |
| nsheth | 2:4af4940055a7 | 320 | if (numDoublesInFifo < *pCount) |
| nsheth | 2:4af4940055a7 | 321 | { |
| nsheth | 2:4af4940055a7 | 322 | *pCount = numDoublesInFifo; |
| nsheth | 2:4af4940055a7 | 323 | } |
| nsheth | 2:4af4940055a7 | 324 | |
| nsheth | 2:4af4940055a7 | 325 | for (i = 0; i < *pCount; i++) |
| nsheth | 2:4af4940055a7 | 326 | { |
| nsheth | 2:4af4940055a7 | 327 | /* Read a double -- Split it into two reads for redability*/ |
| nsheth | 2:4af4940055a7 | 328 | Admx200xReadData(pDevice, CMD_FIFO_READ, &pData[2 * i]); |
| nsheth | 2:4af4940055a7 | 329 | Admx200xReadData(pDevice, CMD_FIFO_READ, &pData[2 * i + 1]); |
| nsheth | 2:4af4940055a7 | 330 | } |
| nsheth | 2:4af4940055a7 | 331 | return status; |
| nsheth | 2:4af4940055a7 | 332 | } |
| nsheth | 2:4af4940055a7 | 333 | |
| nsheth | 2:4af4940055a7 | 334 | /** |
| nsheth | 2:4af4940055a7 | 335 | * @brief Clears the Admx2001 SPI FIFO & errors |
| nsheth | 2:4af4940055a7 | 336 | * |
| nsheth | 2:4af4940055a7 | 337 | * @param pDevice - Device Handle |
| nsheth | 2:4af4940055a7 | 338 | * @return Returns 0 for success or error code. |
| nsheth | 2:4af4940055a7 | 339 | */ |
| nsheth | 2:4af4940055a7 | 340 | int32_t Admx200xClearSPI(Admx200xDev *pDevice) |
| nsheth | 2:4af4940055a7 | 341 | { |
| nsheth | 2:4af4940055a7 | 342 | int32_t status = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 343 | uint8_t cmdID; |
| nsheth | 2:4af4940055a7 | 344 | uint16_t addr = 0; |
| nsheth | 2:4af4940055a7 | 345 | uint32_t statReg = 0; |
| nsheth | 2:4af4940055a7 | 346 | uint32_t data = 0; |
| nsheth | 2:4af4940055a7 | 347 | |
| nsheth | 2:4af4940055a7 | 348 | cmdID = CMD_CLEAR_ERROR; |
| nsheth | 2:4af4940055a7 | 349 | status = Admx200xSendCmd(pDevice, &cmdID, &addr, &data, &statReg); |
| nsheth | 2:4af4940055a7 | 350 | |
| nsheth | 2:4af4940055a7 | 351 | return status; |
| nsheth | 2:4af4940055a7 | 352 | } |
| nsheth | 2:4af4940055a7 | 353 | |
| nsheth | 2:4af4940055a7 | 354 | /** |
| nsheth | 2:4af4940055a7 | 355 | * @} |
| nsheth | 2:4af4940055a7 | 356 | */ |
