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@11:071709f5f7d4, 2021-11-09 (annotated)
- Committer:
- nsheth
- Date:
- Tue Nov 09 10:26:59 2021 +0000
- Revision:
- 11:071709f5f7d4
- Parent:
- 10:49537b1dbbd7
Refactoring and formatting
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 | 10:49537b1dbbd7 | 37 | |
| nsheth | 2:4af4940055a7 | 38 | /** |
| nsheth | 9:f286301109fb | 39 | * @file ADMX2001.cpp |
| nsheth | 2:4af4940055a7 | 40 | * @brief This file contains admx200x APIs for sdpk1 host |
| nsheth | 2:4af4940055a7 | 41 | */ |
| nsheth | 2:4af4940055a7 | 42 | /*============= I N C L U D E S =============*/ |
| nsheth | 4:eb7a23c25751 | 43 | #include "ADMX2001.h" |
| nsheth | 4:eb7a23c25751 | 44 | #include "ADMX2001_commands.h" |
| nsheth | 2:4af4940055a7 | 45 | #include "message.h" |
| nsheth | 2:4af4940055a7 | 46 | #include <stdlib.h> |
| nsheth | 2:4af4940055a7 | 47 | #include <string.h> |
| nsheth | 2:4af4940055a7 | 48 | |
| nsheth | 2:4af4940055a7 | 49 | static uint32_t SwapEndian(uint32_t *pData); |
| nsheth | 2:4af4940055a7 | 50 | /** milli second delay**/ |
| nsheth | 2:4af4940055a7 | 51 | static void Admx200xDelay(uint32_t msec); |
| nsheth | 2:4af4940055a7 | 52 | |
| nsheth | 2:4af4940055a7 | 53 | /** |
| nsheth | 2:4af4940055a7 | 54 | * Swaps endian of a 32 bit number |
| nsheth | 2:4af4940055a7 | 55 | * @param data - 32 bit number |
| nsheth | 2:4af4940055a7 | 56 | * @return |
| nsheth | 2:4af4940055a7 | 57 | */ |
| nsheth | 2:4af4940055a7 | 58 | uint32_t SwapEndian(uint32_t *pData) |
| nsheth | 2:4af4940055a7 | 59 | { |
| nsheth | 2:4af4940055a7 | 60 | uint8_t *pDataU8 = (uint8_t *)pData; |
| nsheth | 2:4af4940055a7 | 61 | |
| nsheth | 2:4af4940055a7 | 62 | return (uint32_t)pDataU8[0] << 24 | (uint32_t)pDataU8[1] << 16 | |
| nsheth | 2:4af4940055a7 | 63 | (uint32_t)pDataU8[2] << 8 | (uint32_t)pDataU8[3]; |
| nsheth | 2:4af4940055a7 | 64 | } |
| nsheth | 2:4af4940055a7 | 65 | |
| nsheth | 2:4af4940055a7 | 66 | /** |
| nsheth | 2:4af4940055a7 | 67 | * @brief Initializes the ADMX200X. |
| nsheth | 2:4af4940055a7 | 68 | * |
| nsheth | 2:4af4940055a7 | 69 | * @param pDevice - The device structure. |
| nsheth | 2:4af4940055a7 | 70 | * @param pSpiDesc - Pointer to spi device |
| nsheth | 2:4af4940055a7 | 71 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 72 | */ |
| nsheth | 2:4af4940055a7 | 73 | int32_t Admx200xInit(Admx200xDev *pDevice, spi_desc *pSpiDesc) |
| nsheth | 2:4af4940055a7 | 74 | { |
| nsheth | 2:4af4940055a7 | 75 | int32_t ret = 0; |
| nsheth | 2:4af4940055a7 | 76 | |
| nsheth | 2:4af4940055a7 | 77 | pDevice->pSpiDesc = pSpiDesc; |
| nsheth | 2:4af4940055a7 | 78 | |
| nsheth | 2:4af4940055a7 | 79 | return ret; |
| nsheth | 2:4af4940055a7 | 80 | } |
| nsheth | 2:4af4940055a7 | 81 | |
| nsheth | 2:4af4940055a7 | 82 | /** |
| nsheth | 2:4af4940055a7 | 83 | * Wrapper fro delay |
| nsheth | 2:4af4940055a7 | 84 | * @param msecs - delay in milliseconds |
| nsheth | 2:4af4940055a7 | 85 | */ |
| nsheth | 2:4af4940055a7 | 86 | void Admx200xDelay(uint32_t msecs) |
| nsheth | 2:4af4940055a7 | 87 | { |
| nsheth | 2:4af4940055a7 | 88 | mdelay(msecs); |
| nsheth | 2:4af4940055a7 | 89 | } |
| nsheth | 2:4af4940055a7 | 90 | |
| nsheth | 2:4af4940055a7 | 91 | /** |
| nsheth | 2:4af4940055a7 | 92 | * @brief Create a command payload in buffer with inputs. |
| nsheth | 2:4af4940055a7 | 93 | * |
| nsheth | 2:4af4940055a7 | 94 | * @param pCmdId - Command ID. |
| nsheth | 2:4af4940055a7 | 95 | * @param pAddr - Sub commands addresses. |
| nsheth | 2:4af4940055a7 | 96 | * @param pData - Data to wildcat. |
| nsheth | 2:4af4940055a7 | 97 | * @param pBuffer - Data to wildcat. |
| nsheth | 2:4af4940055a7 | 98 | * |
| nsheth | 2:4af4940055a7 | 99 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 100 | */ |
| nsheth | 2:4af4940055a7 | 101 | int32_t Admx200xCreateCmdPayload(uint8_t *pCmdId, uint16_t *pAddr, |
| nsheth | 2:4af4940055a7 | 102 | uint32_t *pData, uint8_t *pBuffer) |
| nsheth | 2:4af4940055a7 | 103 | { |
| nsheth | 2:4af4940055a7 | 104 | int32_t ret = 0; |
| nsheth | 5:b9608e6cc24b | 105 | //DEBUG_MSG("cmd:%d,addr:%d, data: 0x%x\n\r", *pCmdId, *pAddr, *pData); |
| nsheth | 2:4af4940055a7 | 106 | pBuffer[0] = pCmdId[0]; |
| nsheth | 2:4af4940055a7 | 107 | pBuffer[1] = ((uint8_t *)pAddr)[1]; |
| nsheth | 2:4af4940055a7 | 108 | pBuffer[2] = ((uint8_t *)pAddr)[0]; |
| nsheth | 2:4af4940055a7 | 109 | pBuffer[3] = ((uint8_t *)pData)[3]; |
| nsheth | 2:4af4940055a7 | 110 | pBuffer[4] = ((uint8_t *)pData)[2]; |
| nsheth | 2:4af4940055a7 | 111 | pBuffer[5] = ((uint8_t *)pData)[1]; |
| nsheth | 2:4af4940055a7 | 112 | pBuffer[6] = ((uint8_t *)pData)[0]; |
| nsheth | 2:4af4940055a7 | 113 | |
| nsheth | 2:4af4940055a7 | 114 | return ret; |
| nsheth | 2:4af4940055a7 | 115 | } |
| nsheth | 2:4af4940055a7 | 116 | |
| nsheth | 2:4af4940055a7 | 117 | /** |
| nsheth | 2:4af4940055a7 | 118 | * @brief Parse the payload to get cmdID, addr and data. |
| nsheth | 2:4af4940055a7 | 119 | |
| nsheth | 2:4af4940055a7 | 120 | * @param[in] pBuffer - Data from wildcat. |
| nsheth | 2:4af4940055a7 | 121 | * @param[out] pCmdId - Pointer to store Command ID. |
| nsheth | 2:4af4940055a7 | 122 | * @param[out] pAddr - Pointer to store addresses. |
| nsheth | 2:4af4940055a7 | 123 | * @param[out] pData - Pointer to store Data |
| nsheth | 2:4af4940055a7 | 124 | * |
| nsheth | 2:4af4940055a7 | 125 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 126 | */ |
| nsheth | 2:4af4940055a7 | 127 | int32_t Admx200xParseCmdPayload(uint8_t *pBuffer, uint8_t *pCmdId, |
| nsheth | 2:4af4940055a7 | 128 | uint16_t *pAddr, uint32_t *pData) |
| nsheth | 2:4af4940055a7 | 129 | { |
| nsheth | 2:4af4940055a7 | 130 | int32_t ret = 0; |
| nsheth | 2:4af4940055a7 | 131 | |
| nsheth | 2:4af4940055a7 | 132 | *pCmdId = pBuffer[0]; |
| nsheth | 2:4af4940055a7 | 133 | *pAddr = ((uint16_t)pBuffer[1] << 8) | ((uint16_t)pBuffer[2]); |
| nsheth | 2:4af4940055a7 | 134 | |
| nsheth | 2:4af4940055a7 | 135 | *pData = SwapEndian((uint32_t *)&pBuffer[3]); |
| nsheth | 2:4af4940055a7 | 136 | |
| nsheth | 2:4af4940055a7 | 137 | return ret; |
| nsheth | 2:4af4940055a7 | 138 | } |
| nsheth | 2:4af4940055a7 | 139 | |
| nsheth | 2:4af4940055a7 | 140 | /** |
| nsheth | 2:4af4940055a7 | 141 | * @brief Waits till done bit is set in the status |
| nsheth | 2:4af4940055a7 | 142 | * |
| nsheth | 2:4af4940055a7 | 143 | * @param pDevice - The handler of the instance of the driver. |
| nsheth | 2:4af4940055a7 | 144 | * @param timeout - Count representing the number of polls to be done until the |
| nsheth | 2:4af4940055a7 | 145 | * function returns if no new data is available. |
| nsheth | 2:4af4940055a7 | 146 | * @param reqStatus - The status to be checked is passed via this argument. |
| nsheth | 2:4af4940055a7 | 147 | * @param pStatReg - Get the status word in pStatReg. |
| nsheth | 2:4af4940055a7 | 148 | * |
| nsheth | 2:4af4940055a7 | 149 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 150 | */ |
| nsheth | 2:4af4940055a7 | 151 | int32_t Admx200xWaitForStatus(Admx200xDev *pDevice, uint32_t timeout, |
| nsheth | 2:4af4940055a7 | 152 | uint32_t reqStatus, uint32_t *pStatReg) |
| nsheth | 2:4af4940055a7 | 153 | { |
| nsheth | 2:4af4940055a7 | 154 | int32_t status = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 155 | int8_t done = 0; |
| nsheth | 2:4af4940055a7 | 156 | uint8_t cmdId = CMD_STATUS_READ; |
| nsheth | 2:4af4940055a7 | 157 | |
| nsheth | 2:4af4940055a7 | 158 | if (pDevice != NULL) |
| nsheth | 2:4af4940055a7 | 159 | { |
| nsheth | 2:4af4940055a7 | 160 | while (!done && --timeout) |
| nsheth | 2:4af4940055a7 | 161 | { |
| nsheth | 2:4af4940055a7 | 162 | |
| nsheth | 2:4af4940055a7 | 163 | status = Admx200xReadData(pDevice, cmdId, pStatReg); |
| nsheth | 2:4af4940055a7 | 164 | |
| nsheth | 2:4af4940055a7 | 165 | if (status == 0) |
| nsheth | 2:4af4940055a7 | 166 | { |
| nsheth | 2:4af4940055a7 | 167 | /* Check the DONE bit in the Status Register */ |
| nsheth | 2:4af4940055a7 | 168 | if (((*pStatReg) & reqStatus) == reqStatus) |
| nsheth | 2:4af4940055a7 | 169 | { |
| nsheth | 2:4af4940055a7 | 170 | done = 1; |
| nsheth | 2:4af4940055a7 | 171 | if (*pStatReg & ADMX200X_STATUS_ERROR_BITM) |
| nsheth | 2:4af4940055a7 | 172 | { |
| nsheth | 2:4af4940055a7 | 173 | status = *pStatReg & ADMX200X_STATUS_CODE_BITM; |
| nsheth | 2:4af4940055a7 | 174 | if (status & ADMX_STATUS_LOG_ZERO_ERROR) |
| nsheth | 2:4af4940055a7 | 175 | { |
| nsheth | 2:4af4940055a7 | 176 | ERROR_MSG("sweep_start/sweep_end cannot be zero " |
| nsheth | 2:4af4940055a7 | 177 | "for logarithmic sweep"); |
| nsheth | 2:4af4940055a7 | 178 | status &= (~ADMX_STATUS_LOG_ZERO_ERROR); |
| nsheth | 2:4af4940055a7 | 179 | } |
| nsheth | 2:4af4940055a7 | 180 | if (status & ADMX_STATUS_LOG_SIGN_ERROR) |
| nsheth | 2:4af4940055a7 | 181 | { |
| nsheth | 2:4af4940055a7 | 182 | ERROR_MSG("sweep_start and sweep_end need to have " |
| nsheth | 2:4af4940055a7 | 183 | "the same sign " |
| nsheth | 2:4af4940055a7 | 184 | "for logarithmic sweep"); |
| nsheth | 2:4af4940055a7 | 185 | status &= (~ADMX_STATUS_LOG_SIGN_ERROR); |
| nsheth | 2:4af4940055a7 | 186 | } |
| nsheth | 2:4af4940055a7 | 187 | if (status & ADMX_STATUS_VOLT_ADC_ERROR) |
| nsheth | 2:4af4940055a7 | 188 | { |
| nsheth | 2:4af4940055a7 | 189 | ERROR_MSG("Voltage ADC Saturated\n"); |
| nsheth | 2:4af4940055a7 | 190 | status &= (~ADMX_STATUS_VOLT_ADC_ERROR); |
| nsheth | 2:4af4940055a7 | 191 | } |
| nsheth | 2:4af4940055a7 | 192 | if (status & ADMX_STATUS_CURR_ADC_ERROR) |
| nsheth | 2:4af4940055a7 | 193 | { |
| nsheth | 2:4af4940055a7 | 194 | ERROR_MSG("Current ADC Saturated\n"); |
| nsheth | 2:4af4940055a7 | 195 | status &= (~ADMX_STATUS_CURR_ADC_ERROR); |
| nsheth | 2:4af4940055a7 | 196 | } |
| nsheth | 2:4af4940055a7 | 197 | if (status & ADMX_STATUS_FIFO_ERROR) |
| nsheth | 2:4af4940055a7 | 198 | { |
| nsheth | 2:4af4940055a7 | 199 | ERROR_MSG( |
| nsheth | 2:4af4940055a7 | 200 | "FIFO either Overflowed or Underflowed\n"); |
| nsheth | 2:4af4940055a7 | 201 | status &= (~ADMX_STATUS_FIFO_ERROR); |
| nsheth | 2:4af4940055a7 | 202 | } |
| nsheth | 2:4af4940055a7 | 203 | if (status & ADMX_STATUS_COUNT_EXCEEDED) |
| nsheth | 2:4af4940055a7 | 204 | { |
| nsheth | 2:4af4940055a7 | 205 | ERROR_MSG("Sweep count exceeded maximum value"); |
| nsheth | 2:4af4940055a7 | 206 | status &= (~ADMX_STATUS_COUNT_EXCEEDED); |
| nsheth | 2:4af4940055a7 | 207 | } |
| nsheth | 2:4af4940055a7 | 208 | } |
| nsheth | 2:4af4940055a7 | 209 | } |
| nsheth | 2:4af4940055a7 | 210 | } |
| nsheth | 2:4af4940055a7 | 211 | } |
| nsheth | 2:4af4940055a7 | 212 | |
| nsheth | 2:4af4940055a7 | 213 | if (!timeout) |
| nsheth | 2:4af4940055a7 | 214 | { |
| nsheth | 2:4af4940055a7 | 215 | status = ADMX_STATUS_TIMEOUT; |
| nsheth | 2:4af4940055a7 | 216 | } |
| nsheth | 2:4af4940055a7 | 217 | } |
| nsheth | 2:4af4940055a7 | 218 | else |
| nsheth | 2:4af4940055a7 | 219 | { |
| nsheth | 2:4af4940055a7 | 220 | status = ADMX_STATUS_FAILED; |
| nsheth | 2:4af4940055a7 | 221 | } |
| nsheth | 2:4af4940055a7 | 222 | return status; |
| nsheth | 2:4af4940055a7 | 223 | } |
| nsheth | 2:4af4940055a7 | 224 | |
| nsheth | 2:4af4940055a7 | 225 | /** |
| nsheth | 2:4af4940055a7 | 226 | * @brief Sends teh command to ADMX200x and wait till done bit is set |
| nsheth | 2:4af4940055a7 | 227 | * @param pDevice - Device Handle |
| nsheth | 2:4af4940055a7 | 228 | * @param pCmdId - The command ID |
| nsheth | 2:4af4940055a7 | 229 | * @param pAddr - Address |
| nsheth | 2:4af4940055a7 | 230 | * @param pData - Data |
| nsheth | 2:4af4940055a7 | 231 | * @param pStatReg - Status register info |
| nsheth | 2:4af4940055a7 | 232 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 233 | */ |
| nsheth | 2:4af4940055a7 | 234 | int32_t Admx200xSendCmd(Admx200xDev *pDevice, uint8_t *pCmdId, uint16_t *pAddr, |
| nsheth | 2:4af4940055a7 | 235 | uint32_t *pData, uint32_t *pStatReg) |
| nsheth | 2:4af4940055a7 | 236 | { |
| nsheth | 2:4af4940055a7 | 237 | |
| nsheth | 2:4af4940055a7 | 238 | int32_t status = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 239 | uint8_t cmdPayload[ADMX200X_CMD_LENGTH]; |
| nsheth | 2:4af4940055a7 | 240 | |
| nsheth | 2:4af4940055a7 | 241 | Admx200xCreateCmdPayload(pCmdId, pAddr, pData, &cmdPayload[0]); |
| nsheth | 2:4af4940055a7 | 242 | |
| nsheth | 2:4af4940055a7 | 243 | status = spi_write_and_read(pDevice->pSpiDesc, &cmdPayload[0], |
| nsheth | 2:4af4940055a7 | 244 | ADMX200X_CMD_LENGTH); |
| nsheth | 2:4af4940055a7 | 245 | Admx200xDelay(100); |
| nsheth | 2:4af4940055a7 | 246 | // printf("CMD :%02x ADDR: %04x DATA: %08x\n\r",*pCmdId, *pAddr, *pData); |
| nsheth | 2:4af4940055a7 | 247 | if (!status) |
| nsheth | 2:4af4940055a7 | 248 | { |
| nsheth | 2:4af4940055a7 | 249 | /** Wait for status done = 1,Warn/error/command result = 0 from result |
| nsheth | 2:4af4940055a7 | 250 | */ |
| nsheth | 2:4af4940055a7 | 251 | status = Admx200xWaitForStatus(pDevice, SPI_TIMEOUT, |
| nsheth | 2:4af4940055a7 | 252 | ADMX200X_STATUS_DONE_BITM, pStatReg); |
| nsheth | 2:4af4940055a7 | 253 | |
| nsheth | 6:7d8f30b3bc57 | 254 | //printf("Stat Reg: %08x\n\r", *pStatReg); |
| nsheth | 2:4af4940055a7 | 255 | if (status == ADMX_STATUS_SUCCESS) |
| nsheth | 2:4af4940055a7 | 256 | { |
| nsheth | 2:4af4940055a7 | 257 | status = *pStatReg & ADMX200X_STATUS_CODE_BITM; |
| nsheth | 2:4af4940055a7 | 258 | } |
| nsheth | 2:4af4940055a7 | 259 | } |
| nsheth | 2:4af4940055a7 | 260 | if (status == ADMX_STATUS_TIMEOUT) |
| nsheth | 2:4af4940055a7 | 261 | { |
| nsheth | 2:4af4940055a7 | 262 | ERROR_MSG("SPI interface timed out : Not responding"); |
| nsheth | 2:4af4940055a7 | 263 | } |
| nsheth | 2:4af4940055a7 | 264 | return status; |
| nsheth | 2:4af4940055a7 | 265 | } |
| nsheth | 2:4af4940055a7 | 266 | |
| nsheth | 2:4af4940055a7 | 267 | /** |
| nsheth | 2:4af4940055a7 | 268 | * @brief Read result register from the WILDCAT |
| nsheth | 2:4af4940055a7 | 269 | * @param pDevice - Device Handle |
| nsheth | 2:4af4940055a7 | 270 | * @param cmdId - This determines where the data is read from - result |
| nsheth | 2:4af4940055a7 | 271 | * register, status register or Fifo |
| nsheth | 2:4af4940055a7 | 272 | * @param pResult - The data in the result register |
| nsheth | 2:4af4940055a7 | 273 | * @return Returns 0 for success or negative error code. |
| nsheth | 2:4af4940055a7 | 274 | */ |
| nsheth | 2:4af4940055a7 | 275 | int32_t Admx200xReadData(Admx200xDev *pDevice, uint8_t cmdId, uint32_t *pResult) |
| nsheth | 2:4af4940055a7 | 276 | { |
| nsheth | 2:4af4940055a7 | 277 | int32_t ret = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 278 | uint8_t cmdTemp; |
| nsheth | 2:4af4940055a7 | 279 | uint16_t addrTemp; |
| nsheth | 2:4af4940055a7 | 280 | uint16_t addr; |
| nsheth | 2:4af4940055a7 | 281 | uint32_t data; |
| nsheth | 2:4af4940055a7 | 282 | uint8_t cmdPayload[ADMX200X_CMD_LENGTH]; |
| nsheth | 2:4af4940055a7 | 283 | |
| nsheth | 2:4af4940055a7 | 284 | /*FIXME: Insert checks here. Not all command IDs are accepted here */ |
| nsheth | 2:4af4940055a7 | 285 | addr = 0; |
| nsheth | 2:4af4940055a7 | 286 | data = 0; |
| nsheth | 2:4af4940055a7 | 287 | |
| nsheth | 2:4af4940055a7 | 288 | Admx200xCreateCmdPayload(&cmdId, &addr, &data, &cmdPayload[0]); |
| nsheth | 2:4af4940055a7 | 289 | |
| nsheth | 2:4af4940055a7 | 290 | /* Send command and Read response from the slave */ |
| nsheth | 2:4af4940055a7 | 291 | ret = spi_write_and_read(pDevice->pSpiDesc, &cmdPayload[0], |
| nsheth | 2:4af4940055a7 | 292 | ADMX200X_CMD_LENGTH); |
| nsheth | 2:4af4940055a7 | 293 | Admx200xDelay(100); |
| nsheth | 2:4af4940055a7 | 294 | Admx200xParseCmdPayload(&cmdPayload[0], &cmdTemp, &addrTemp, pResult); |
| nsheth | 2:4af4940055a7 | 295 | |
| nsheth | 2:4af4940055a7 | 296 | return ret; |
| nsheth | 2:4af4940055a7 | 297 | } |
| nsheth | 2:4af4940055a7 | 298 | |
| nsheth | 2:4af4940055a7 | 299 | /** |
| nsheth | 2:4af4940055a7 | 300 | * @brief Read array of datatype doube from the fifo of the WILDCAT |
| nsheth | 2:4af4940055a7 | 301 | * @param pDevice - Device Handle |
| nsheth | 2:4af4940055a7 | 302 | * @param pFifo - Double array that stores the data |
| nsheth | 2:4af4940055a7 | 303 | * @param pCount - Stores the number of double values stored in the pFifo array |
| nsheth | 2:4af4940055a7 | 304 | */ |
| nsheth | 2:4af4940055a7 | 305 | int32_t Admx200xReadFifo(Admx200xDev *pDevice, double *pFifo, int32_t *pCount) |
| nsheth | 2:4af4940055a7 | 306 | { |
| nsheth | 2:4af4940055a7 | 307 | int32_t status = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 308 | int32_t i; |
| nsheth | 2:4af4940055a7 | 309 | uint32_t *pData = (uint32_t *)pFifo; |
| nsheth | 2:4af4940055a7 | 310 | uint8_t cmdId = CMD_STATUS_READ; |
| nsheth | 2:4af4940055a7 | 311 | uint32_t statusReg; |
| nsheth | 2:4af4940055a7 | 312 | int32_t numDoublesInFifo; |
| nsheth | 2:4af4940055a7 | 313 | |
| nsheth | 2:4af4940055a7 | 314 | status = Admx200xReadData(pDevice, cmdId, &statusReg); |
| nsheth | 2:4af4940055a7 | 315 | |
| nsheth | 2:4af4940055a7 | 316 | /* Fifo depth in doubles */ |
| nsheth | 2:4af4940055a7 | 317 | numDoublesInFifo = (statusReg & ADMX200X_STATUS_FIFO_DEPTH_BITM) >> 17; |
| nsheth | 2:4af4940055a7 | 318 | |
| nsheth | 2:4af4940055a7 | 319 | if (numDoublesInFifo < *pCount) |
| nsheth | 2:4af4940055a7 | 320 | { |
| nsheth | 2:4af4940055a7 | 321 | *pCount = numDoublesInFifo; |
| nsheth | 2:4af4940055a7 | 322 | } |
| nsheth | 2:4af4940055a7 | 323 | |
| nsheth | 2:4af4940055a7 | 324 | for (i = 0; i < *pCount; i++) |
| nsheth | 2:4af4940055a7 | 325 | { |
| nsheth | 2:4af4940055a7 | 326 | /* Read a double -- Split it into two reads for redability*/ |
| nsheth | 2:4af4940055a7 | 327 | Admx200xReadData(pDevice, CMD_FIFO_READ, &pData[2 * i]); |
| nsheth | 2:4af4940055a7 | 328 | Admx200xReadData(pDevice, CMD_FIFO_READ, &pData[2 * i + 1]); |
| nsheth | 2:4af4940055a7 | 329 | } |
| nsheth | 2:4af4940055a7 | 330 | return status; |
| nsheth | 2:4af4940055a7 | 331 | } |
| nsheth | 2:4af4940055a7 | 332 | |
| nsheth | 2:4af4940055a7 | 333 | /** |
| nsheth | 2:4af4940055a7 | 334 | * @brief Clears the Admx2001 SPI FIFO & errors |
| nsheth | 2:4af4940055a7 | 335 | * |
| nsheth | 2:4af4940055a7 | 336 | * @param pDevice - Device Handle |
| nsheth | 2:4af4940055a7 | 337 | * @return Returns 0 for success or error code. |
| nsheth | 2:4af4940055a7 | 338 | */ |
| nsheth | 2:4af4940055a7 | 339 | int32_t Admx200xClearSPI(Admx200xDev *pDevice) |
| nsheth | 2:4af4940055a7 | 340 | { |
| nsheth | 2:4af4940055a7 | 341 | int32_t status = ADMX_STATUS_SUCCESS; |
| nsheth | 2:4af4940055a7 | 342 | uint8_t cmdID; |
| nsheth | 2:4af4940055a7 | 343 | uint16_t addr = 0; |
| nsheth | 2:4af4940055a7 | 344 | uint32_t statReg = 0; |
| nsheth | 2:4af4940055a7 | 345 | uint32_t data = 0; |
| nsheth | 2:4af4940055a7 | 346 | |
| nsheth | 2:4af4940055a7 | 347 | cmdID = CMD_CLEAR_ERROR; |
| nsheth | 2:4af4940055a7 | 348 | status = Admx200xSendCmd(pDevice, &cmdID, &addr, &data, &statReg); |
| nsheth | 2:4af4940055a7 | 349 | |
| nsheth | 2:4af4940055a7 | 350 | return status; |
| nsheth | 2:4af4940055a7 | 351 | } |
| nsheth | 2:4af4940055a7 | 352 | |
| nsheth | 2:4af4940055a7 | 353 | /** |
| nsheth | 2:4af4940055a7 | 354 | * @} |
| nsheth | 2:4af4940055a7 | 355 | */ |