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.
Fork of AdiSense1000 by
main.cpp@7:4dbae381f693, 2017-10-20 (annotated)
- Committer:
- kevin1990
- Date:
- Fri Oct 20 15:58:01 2017 +0000
- Revision:
- 7:4dbae381f693
- Parent:
- 5:dbb2b71a59ed
v0.3 release (New Host api)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevin1990 | 5:dbb2b71a59ed | 1 | /* |
kevin1990 | 3:3796776e2c27 | 2 | ****************************************************************************** |
kevin1990 | 5:dbb2b71a59ed | 3 | * file: main.cpp |
kevin1990 | 3:3796776e2c27 | 4 | *----------------------------------------------------------------------------- |
kevin1990 | 7:4dbae381f693 | 5 | */ |
kevin1990 | 7:4dbae381f693 | 6 | |
kevin1990 | 7:4dbae381f693 | 7 | /* |
kevin1990 | 3:3796776e2c27 | 8 | Copyright (c) 2017 Emutex Ltd. / Analog Devices, Inc. |
kevin1990 | 3:3796776e2c27 | 9 | |
kevin1990 | 3:3796776e2c27 | 10 | All rights reserved. |
kevin1990 | 3:3796776e2c27 | 11 | |
kevin1990 | 3:3796776e2c27 | 12 | Redistribution and use in source and binary forms, with or without modification, |
kevin1990 | 3:3796776e2c27 | 13 | are permitted provided that the following conditions are met: |
kevin1990 | 3:3796776e2c27 | 14 | - Redistributions of source code must retain the above copyright notice, |
kevin1990 | 3:3796776e2c27 | 15 | this list of conditions and the following disclaimer. |
kevin1990 | 3:3796776e2c27 | 16 | - Redistributions in binary form must reproduce the above copyright notice, |
kevin1990 | 3:3796776e2c27 | 17 | this list of conditions and the following disclaimer in the documentation |
kevin1990 | 3:3796776e2c27 | 18 | and/or other materials provided with the distribution. |
kevin1990 | 3:3796776e2c27 | 19 | - Modified versions of the software must be conspicuously marked as such. |
kevin1990 | 3:3796776e2c27 | 20 | - This software is licensed solely and exclusively for use with processors |
kevin1990 | 3:3796776e2c27 | 21 | manufactured by or for Analog Devices, Inc. |
kevin1990 | 3:3796776e2c27 | 22 | - This software may not be combined or merged with other code in any manner |
kevin1990 | 3:3796776e2c27 | 23 | that would cause the software to become subject to terms and conditions |
kevin1990 | 3:3796776e2c27 | 24 | which differ from those listed here. |
kevin1990 | 3:3796776e2c27 | 25 | - Neither the name of Analog Devices, Inc. nor the names of its |
kevin1990 | 3:3796776e2c27 | 26 | contributors may be used to endorse or promote products derived |
kevin1990 | 3:3796776e2c27 | 27 | from this software without specific prior written permission. |
kevin1990 | 3:3796776e2c27 | 28 | - The use of this software may or may not infringe the patent rights of one |
kevin1990 | 3:3796776e2c27 | 29 | or more patent holders. This license does not release you from the |
kevin1990 | 3:3796776e2c27 | 30 | requirement that you obtain separate licenses from these patent holders |
kevin1990 | 3:3796776e2c27 | 31 | to use this software. |
kevin1990 | 3:3796776e2c27 | 32 | |
kevin1990 | 3:3796776e2c27 | 33 | THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY |
kevin1990 | 3:3796776e2c27 | 34 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
kevin1990 | 3:3796776e2c27 | 35 | TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
kevin1990 | 3:3796776e2c27 | 36 | NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
kevin1990 | 3:3796776e2c27 | 37 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES |
kevin1990 | 3:3796776e2c27 | 38 | (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL |
kevin1990 | 3:3796776e2c27 | 39 | PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
kevin1990 | 3:3796776e2c27 | 40 | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
kevin1990 | 3:3796776e2c27 | 41 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
kevin1990 | 3:3796776e2c27 | 42 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
kevin1990 | 3:3796776e2c27 | 43 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kevin1990 | 7:4dbae381f693 | 44 | */ |
kevin1990 | 3:3796776e2c27 | 45 | |
kevin1990 | 7:4dbae381f693 | 46 | #include "mbed.h" |
kevin1990 | 7:4dbae381f693 | 47 | #include "inc/adi_sense_api.h" |
kevin1990 | 7:4dbae381f693 | 48 | #include "inc/adi_sense_log.h" |
kevin1990 | 3:3796776e2c27 | 49 | |
kevin1990 | 7:4dbae381f693 | 50 | /* Define an arbitrary number of measurement cycles to execute */ |
kevin1990 | 7:4dbae381f693 | 51 | #define MAX_MEASUREMENT_CYCLES 10 |
kevin1990 | 3:3796776e2c27 | 52 | |
kevin1990 | 7:4dbae381f693 | 53 | #define ADI_SENSE_DEVICE_INDEX (0) |
kevin1990 | 5:dbb2b71a59ed | 54 | |
kevin1990 | 7:4dbae381f693 | 55 | #define ASSERT(x) \ |
kevin1990 | 7:4dbae381f693 | 56 | do { \ |
kevin1990 | 7:4dbae381f693 | 57 | if (!(x)) \ |
kevin1990 | 7:4dbae381f693 | 58 | { \ |
kevin1990 | 7:4dbae381f693 | 59 | ADI_SENSE_LOG_ERROR("Assertion failed on line %d of %s", \ |
kevin1990 | 7:4dbae381f693 | 60 | __LINE__, __FILE__); \ |
kevin1990 | 7:4dbae381f693 | 61 | exit(1); \ |
kevin1990 | 7:4dbae381f693 | 62 | } \ |
kevin1990 | 7:4dbae381f693 | 63 | } while(0) |
kevin1990 | 3:3796776e2c27 | 64 | |
kevin1990 | 7:4dbae381f693 | 65 | extern ADI_SENSE_CONFIG adi_sense_config; |
kevin1990 | 7:4dbae381f693 | 66 | extern ADI_SENSE_DSP_LUT_RAW adi_sense_dsp_lut; |
kevin1990 | 5:dbb2b71a59ed | 67 | |
kevin1990 | 7:4dbae381f693 | 68 | static ADI_SENSE_CONNECTION connectionInfo = { |
kevin1990 | 7:4dbae381f693 | 69 | .type = ADI_SENSE_CONNECTION_TYPE_SPI, |
kevin1990 | 7:4dbae381f693 | 70 | .spi = { |
kevin1990 | 7:4dbae381f693 | 71 | .mosiPin = SPI_MOSI, |
kevin1990 | 7:4dbae381f693 | 72 | .misoPin = SPI_MISO, |
kevin1990 | 7:4dbae381f693 | 73 | .sckPin = SPI_SCK, |
kevin1990 | 7:4dbae381f693 | 74 | .csPin = D10, |
kevin1990 | 7:4dbae381f693 | 75 | .maxSpeedHz = 2000000, |
kevin1990 | 7:4dbae381f693 | 76 | }, |
kevin1990 | 7:4dbae381f693 | 77 | .gpio = { |
kevin1990 | 7:4dbae381f693 | 78 | .resetPin = D6, |
kevin1990 | 7:4dbae381f693 | 79 | .errorPin = D3, |
kevin1990 | 7:4dbae381f693 | 80 | .alertPin = D4, |
kevin1990 | 7:4dbae381f693 | 81 | .datareadyPin = D5, |
kevin1990 | 7:4dbae381f693 | 82 | }, |
kevin1990 | 7:4dbae381f693 | 83 | }; |
kevin1990 | 3:3796776e2c27 | 84 | |
kevin1990 | 3:3796776e2c27 | 85 | |
kevin1990 | 7:4dbae381f693 | 86 | static void printSamples(ADI_SENSE_DATA_SAMPLE *pSampleBuffer, unsigned count) |
kevin1990 | 3:3796776e2c27 | 87 | { |
kevin1990 | 7:4dbae381f693 | 88 | for (unsigned i = 0; i < count; i++) |
kevin1990 | 7:4dbae381f693 | 89 | { |
kevin1990 | 7:4dbae381f693 | 90 | ADI_SENSE_LOG_DEBUG("sample %3u/%u: channel=%2u, value=%10f, raw=%10u, flags: %s %s", |
kevin1990 | 7:4dbae381f693 | 91 | i+1, count, |
kevin1990 | 7:4dbae381f693 | 92 | pSampleBuffer[i].channelId, |
kevin1990 | 7:4dbae381f693 | 93 | pSampleBuffer[i].processedValue, |
kevin1990 | 7:4dbae381f693 | 94 | pSampleBuffer[i].rawValue, |
kevin1990 | 7:4dbae381f693 | 95 | pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ERROR ? "ERROR" : "", |
kevin1990 | 7:4dbae381f693 | 96 | pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ALERT ? "ALERT" : ""); |
kevin1990 | 7:4dbae381f693 | 97 | } |
kevin1990 | 3:3796776e2c27 | 98 | } |
kevin1990 | 3:3796776e2c27 | 99 | |
kevin1990 | 7:4dbae381f693 | 100 | static void datareadyCallback( |
kevin1990 | 7:4dbae381f693 | 101 | ADI_SENSE_GPIO_PIN ePinId, |
kevin1990 | 7:4dbae381f693 | 102 | void *pArg) |
kevin1990 | 5:dbb2b71a59ed | 103 | { |
kevin1990 | 7:4dbae381f693 | 104 | volatile bool_t *pbDataready = (volatile bool_t *) pArg; |
kevin1990 | 3:3796776e2c27 | 105 | |
kevin1990 | 7:4dbae381f693 | 106 | *pbDataready = true; |
kevin1990 | 5:dbb2b71a59ed | 107 | } |
kevin1990 | 5:dbb2b71a59ed | 108 | |
kevin1990 | 7:4dbae381f693 | 109 | int main() |
kevin1990 | 5:dbb2b71a59ed | 110 | { |
kevin1990 | 7:4dbae381f693 | 111 | ADI_SENSE_DEVICE_HANDLE hDevice; |
kevin1990 | 7:4dbae381f693 | 112 | ADI_SENSE_PRODUCT_ID productId; |
kevin1990 | 7:4dbae381f693 | 113 | ADI_SENSE_DATA_SAMPLE *pSampleBuffer; |
kevin1990 | 7:4dbae381f693 | 114 | ADI_SENSE_OPERATING_MODE eOperatingMode; |
kevin1990 | 7:4dbae381f693 | 115 | ADI_SENSE_DATA_PUBLISH_MODE eDataPublishMode; |
kevin1990 | 7:4dbae381f693 | 116 | uint32_t nSamplesPerDataready; |
kevin1990 | 7:4dbae381f693 | 117 | uint32_t nSamplesPerCycle; |
kevin1990 | 7:4dbae381f693 | 118 | bool_t bDeviceReady; |
kevin1990 | 7:4dbae381f693 | 119 | |
kevin1990 | 7:4dbae381f693 | 120 | /* |
kevin1990 | 7:4dbae381f693 | 121 | * Open an ADI Sense device instance. |
kevin1990 | 7:4dbae381f693 | 122 | */ |
kevin1990 | 7:4dbae381f693 | 123 | ASSERT(adi_sense_Open(0, &connectionInfo, &hDevice) == ADI_SENSE_SUCCESS); |
kevin1990 | 5:dbb2b71a59ed | 124 | |
kevin1990 | 7:4dbae381f693 | 125 | /* |
kevin1990 | 7:4dbae381f693 | 126 | * Reset the given ADI Sense device, and wait until the device is ready. |
kevin1990 | 7:4dbae381f693 | 127 | */ |
kevin1990 | 7:4dbae381f693 | 128 | ADI_SENSE_LOG_INFO("Resetting ADI Sense device, please wait..."); |
kevin1990 | 7:4dbae381f693 | 129 | ASSERT(adi_sense_Reset(hDevice) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 130 | |
kevin1990 | 7:4dbae381f693 | 131 | /* |
kevin1990 | 7:4dbae381f693 | 132 | * Wait until the device is ready. |
kevin1990 | 7:4dbae381f693 | 133 | */ |
kevin1990 | 7:4dbae381f693 | 134 | do { |
kevin1990 | 7:4dbae381f693 | 135 | wait_ms(100); |
kevin1990 | 7:4dbae381f693 | 136 | ASSERT(adi_sense_GetDeviceReadyState(hDevice, &bDeviceReady) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 137 | } while (! bDeviceReady); |
kevin1990 | 7:4dbae381f693 | 138 | ADI_SENSE_LOG_INFO("ADI Sense device ready"); |
kevin1990 | 5:dbb2b71a59ed | 139 | |
kevin1990 | 7:4dbae381f693 | 140 | /* |
kevin1990 | 7:4dbae381f693 | 141 | * Read the product ID from the device registers. |
kevin1990 | 7:4dbae381f693 | 142 | */ |
kevin1990 | 7:4dbae381f693 | 143 | ASSERT(adi_sense_GetProductID(hDevice, &productId) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 144 | ADI_SENSE_LOG_INFO("Product ID: 0x%04X", productId); |
kevin1990 | 7:4dbae381f693 | 145 | |
kevin1990 | 7:4dbae381f693 | 146 | /* |
kevin1990 | 7:4dbae381f693 | 147 | * Write configuration settings to the device registers. |
kevin1990 | 7:4dbae381f693 | 148 | * Settings are not applied until adi_sense_ApplyConfigUpdates() is called. |
kevin1990 | 7:4dbae381f693 | 149 | */ |
kevin1990 | 7:4dbae381f693 | 150 | ASSERT(adi_sense_SetDeviceConfig(hDevice, &adi_sense_config) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 151 | ASSERT(adi_sense_SetDspData(hDevice, &adi_sense_dsp_lut) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 152 | ASSERT(adi_sense_ApplyConfigUpdates(hDevice) == ADI_SENSE_SUCCESS); |
kevin1990 | 5:dbb2b71a59ed | 153 | |
kevin1990 | 7:4dbae381f693 | 154 | /* |
kevin1990 | 7:4dbae381f693 | 155 | * Allocate a buffer to store the samples retreived on each DATAREADY pulse |
kevin1990 | 7:4dbae381f693 | 156 | */ |
kevin1990 | 7:4dbae381f693 | 157 | ASSERT(adi_sense_GetDataPublishingInfo(hDevice, |
kevin1990 | 7:4dbae381f693 | 158 | &eOperatingMode, |
kevin1990 | 7:4dbae381f693 | 159 | &eDataPublishMode, |
kevin1990 | 7:4dbae381f693 | 160 | &nSamplesPerDataready, |
kevin1990 | 7:4dbae381f693 | 161 | &nSamplesPerCycle) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 162 | pSampleBuffer = new ADI_SENSE_DATA_SAMPLE[nSamplesPerDataready]; |
kevin1990 | 7:4dbae381f693 | 163 | ASSERT(pSampleBuffer != NULL); |
kevin1990 | 5:dbb2b71a59ed | 164 | |
kevin1990 | 7:4dbae381f693 | 165 | ADI_SENSE_LOG_INFO("Configuration loaded and applied, starting measurement cycles"); |
kevin1990 | 7:4dbae381f693 | 166 | |
kevin1990 | 7:4dbae381f693 | 167 | if (eOperatingMode == ADI_SENSE_OPERATING_MODE_SINGLECYCLE) |
kevin1990 | 7:4dbae381f693 | 168 | { |
kevin1990 | 7:4dbae381f693 | 169 | uint32_t nTotalCycles = 0; |
kevin1990 | 5:dbb2b71a59ed | 170 | |
kevin1990 | 7:4dbae381f693 | 171 | /* For demonstration reasons only, we're using interrupt-triggered DATAREADY |
kevin1990 | 7:4dbae381f693 | 172 | * notifications in this mode */ |
kevin1990 | 7:4dbae381f693 | 173 | volatile bool_t bDataReady = false; |
kevin1990 | 7:4dbae381f693 | 174 | ASSERT(adi_sense_RegisterGpioCallback(hDevice, ADI_SENSE_GPIO_PIN_DATAREADY, |
kevin1990 | 7:4dbae381f693 | 175 | datareadyCallback, |
kevin1990 | 7:4dbae381f693 | 176 | (void *)&bDataReady) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 177 | |
kevin1990 | 7:4dbae381f693 | 178 | do |
kevin1990 | 7:4dbae381f693 | 179 | { |
kevin1990 | 7:4dbae381f693 | 180 | ASSERT(adi_sense_StartMeasurement(hDevice) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 181 | |
kevin1990 | 7:4dbae381f693 | 182 | for (unsigned i = 0; i < nSamplesPerCycle / nSamplesPerDataready; i++) |
kevin1990 | 7:4dbae381f693 | 183 | { |
kevin1990 | 7:4dbae381f693 | 184 | uint32_t nReturned; |
kevin1990 | 5:dbb2b71a59ed | 185 | |
kevin1990 | 7:4dbae381f693 | 186 | /* |
kevin1990 | 7:4dbae381f693 | 187 | * Interrupt method: wait for DATAREADY interrupt callback |
kevin1990 | 7:4dbae381f693 | 188 | * |
kevin1990 | 7:4dbae381f693 | 189 | * Note that we could potentially enter a sleep mode here if this |
kevin1990 | 7:4dbae381f693 | 190 | * GPIO interrupt is capable of waking the CPU... |
kevin1990 | 7:4dbae381f693 | 191 | */ |
kevin1990 | 7:4dbae381f693 | 192 | while (! bDataReady) |
kevin1990 | 7:4dbae381f693 | 193 | ; |
kevin1990 | 7:4dbae381f693 | 194 | /* Reset bDataReady flag to detect the next callback */ |
kevin1990 | 7:4dbae381f693 | 195 | bDataReady = false; |
kevin1990 | 7:4dbae381f693 | 196 | |
kevin1990 | 7:4dbae381f693 | 197 | ASSERT(adi_sense_GetData(hDevice, pSampleBuffer, nSamplesPerDataready, &nReturned) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 198 | ASSERT(nReturned == nSamplesPerDataready); |
kevin1990 | 7:4dbae381f693 | 199 | |
kevin1990 | 7:4dbae381f693 | 200 | printSamples(pSampleBuffer, nReturned); |
kevin1990 | 7:4dbae381f693 | 201 | } |
kevin1990 | 7:4dbae381f693 | 202 | nTotalCycles++; |
kevin1990 | 5:dbb2b71a59ed | 203 | |
kevin1990 | 7:4dbae381f693 | 204 | } while (nTotalCycles < MAX_MEASUREMENT_CYCLES); |
kevin1990 | 7:4dbae381f693 | 205 | } |
kevin1990 | 7:4dbae381f693 | 206 | else |
kevin1990 | 7:4dbae381f693 | 207 | { |
kevin1990 | 7:4dbae381f693 | 208 | uint32_t nTotalSamples = 0; |
kevin1990 | 7:4dbae381f693 | 209 | bool_t bDataReady = false; |
kevin1990 | 5:dbb2b71a59ed | 210 | |
kevin1990 | 7:4dbae381f693 | 211 | ASSERT(adi_sense_StartMeasurement(hDevice) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 212 | |
kevin1990 | 7:4dbae381f693 | 213 | do |
kevin1990 | 7:4dbae381f693 | 214 | { |
kevin1990 | 7:4dbae381f693 | 215 | uint32_t nReturned; |
kevin1990 | 5:dbb2b71a59ed | 216 | |
kevin1990 | 7:4dbae381f693 | 217 | /* Polling method: continuously check DATAREADY until it is asserted */ |
kevin1990 | 7:4dbae381f693 | 218 | do { |
kevin1990 | 7:4dbae381f693 | 219 | ASSERT(adi_sense_GetGpioState(hDevice, ADI_SENSE_GPIO_PIN_DATAREADY, &bDataReady) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 220 | } while (! bDataReady); |
kevin1990 | 7:4dbae381f693 | 221 | |
kevin1990 | 7:4dbae381f693 | 222 | ASSERT(adi_sense_GetData(hDevice, pSampleBuffer, nSamplesPerDataready, &nReturned) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 223 | ASSERT(nReturned == nSamplesPerDataready); |
kevin1990 | 7:4dbae381f693 | 224 | nTotalSamples += nReturned; |
kevin1990 | 5:dbb2b71a59ed | 225 | |
kevin1990 | 7:4dbae381f693 | 226 | printSamples(pSampleBuffer, nReturned); |
kevin1990 | 7:4dbae381f693 | 227 | } while ((nTotalSamples / nSamplesPerCycle) < MAX_MEASUREMENT_CYCLES); |
kevin1990 | 5:dbb2b71a59ed | 228 | |
kevin1990 | 7:4dbae381f693 | 229 | ASSERT(adi_sense_StopMeasurement(hDevice) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 230 | } |
kevin1990 | 7:4dbae381f693 | 231 | delete [] pSampleBuffer; |
kevin1990 | 7:4dbae381f693 | 232 | |
kevin1990 | 7:4dbae381f693 | 233 | ASSERT(adi_sense_Close(hDevice) == ADI_SENSE_SUCCESS); |
kevin1990 | 7:4dbae381f693 | 234 | |
kevin1990 | 7:4dbae381f693 | 235 | return 0; |
kevin1990 | 3:3796776e2c27 | 236 | } |
kevin1990 | 2:625a45555a85 | 237 |