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@35:cc01f0766fc2, 2018-08-17 (annotated)
- Committer:
- Ross_o_halloran
- Date:
- Fri Aug 17 15:46:16 2018 +0000
- Revision:
- 35:cc01f0766fc2
- Parent:
- 28:4eb837cd71df
Sean
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevin1990 | 15:78f3f517417f | 1 | /* |
kevin1990 | 28:4eb837cd71df | 2 | CONFIDENTIAL AND PROPRIETARY INFORMATION |
kevin1990 | 28:4eb837cd71df | 3 | |
kevin1990 | 28:4eb837cd71df | 4 | Copyright (c) 2018 Emutex Ltd. All rights reserved. |
kevin1990 | 28:4eb837cd71df | 5 | This software and documentation contain confidential and |
kevin1990 | 28:4eb837cd71df | 6 | proprietary information that is the property of |
kevin1990 | 28:4eb837cd71df | 7 | Emutex Ltd. The software and documentation are |
kevin1990 | 28:4eb837cd71df | 8 | furnished under a license agreement and may be used |
kevin1990 | 28:4eb837cd71df | 9 | or copied only in accordance with the terms of the license |
kevin1990 | 28:4eb837cd71df | 10 | agreement. No part of the software and documentation |
kevin1990 | 28:4eb837cd71df | 11 | may be reproduced, transmitted, or translated, in any |
kevin1990 | 28:4eb837cd71df | 12 | form or by any means, electronic, mechanical, manual, |
kevin1990 | 28:4eb837cd71df | 13 | optical, or otherwise, without prior written permission |
kevin1990 | 28:4eb837cd71df | 14 | of Emutex Ltd., or as expressly provided by the license agreement. |
kevin1990 | 28:4eb837cd71df | 15 | Reverse engineering is prohibited, and reproduction, |
kevin1990 | 28:4eb837cd71df | 16 | disclosure or use without specific written authorization |
kevin1990 | 28:4eb837cd71df | 17 | of Emutex Ltd. is strictly forbidden. |
kevin1990 | 15:78f3f517417f | 18 | * |
danodonovan | 23:2adb6216b001 | 19 | Copyright 2017 (c) Analog Devices, Inc. |
kevin1990 | 15:78f3f517417f | 20 | |
kevin1990 | 15:78f3f517417f | 21 | All rights reserved. |
kevin1990 | 15:78f3f517417f | 22 | |
danodonovan | 23:2adb6216b001 | 23 | Redistribution and use in source and binary forms, with or without |
danodonovan | 23:2adb6216b001 | 24 | modification, are permitted provided that the following conditions are met: |
danodonovan | 23:2adb6216b001 | 25 | - Redistributions of source code must retain the above copyright |
danodonovan | 23:2adb6216b001 | 26 | notice, this list of conditions and the following disclaimer. |
danodonovan | 23:2adb6216b001 | 27 | - Redistributions in binary form must reproduce the above copyright |
danodonovan | 23:2adb6216b001 | 28 | notice, this list of conditions and the following disclaimer in |
danodonovan | 23:2adb6216b001 | 29 | the documentation and/or other materials provided with the |
danodonovan | 23:2adb6216b001 | 30 | distribution. |
kevin1990 | 15:78f3f517417f | 31 | - Neither the name of Analog Devices, Inc. nor the names of its |
kevin1990 | 15:78f3f517417f | 32 | contributors may be used to endorse or promote products derived |
kevin1990 | 15:78f3f517417f | 33 | from this software without specific prior written permission. |
danodonovan | 23:2adb6216b001 | 34 | - The use of this software may or may not infringe the patent rights |
danodonovan | 23:2adb6216b001 | 35 | of one or more patent holders. This license does not release you |
danodonovan | 23:2adb6216b001 | 36 | from the requirement that you obtain separate licenses from these |
danodonovan | 23:2adb6216b001 | 37 | patent holders to use this software. |
danodonovan | 23:2adb6216b001 | 38 | - Use of the software either in source or binary form, must be run |
danodonovan | 23:2adb6216b001 | 39 | on or directly connected to an Analog Devices Inc. component. |
kevin1990 | 15:78f3f517417f | 40 | |
danodonovan | 23:2adb6216b001 | 41 | THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
danodonovan | 23:2adb6216b001 | 42 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
danodonovan | 23:2adb6216b001 | 43 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
danodonovan | 23:2adb6216b001 | 44 | IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
danodonovan | 23:2adb6216b001 | 45 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
danodonovan | 23:2adb6216b001 | 46 | LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
danodonovan | 23:2adb6216b001 | 47 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
danodonovan | 23:2adb6216b001 | 48 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
danodonovan | 23:2adb6216b001 | 49 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
danodonovan | 23:2adb6216b001 | 50 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kevin1990 | 15:78f3f517417f | 51 | * |
kevin1990 | 15:78f3f517417f | 52 | *****************************************************************************/ |
kevin1990 | 28:4eb837cd71df | 53 | /*! |
kevin1990 | 28:4eb837cd71df | 54 | ****************************************************************************** |
kevin1990 | 28:4eb837cd71df | 55 | * @file: |
kevin1990 | 28:4eb837cd71df | 56 | *----------------------------------------------------------------------------- |
kevin1990 | 28:4eb837cd71df | 57 | * |
kevin1990 | 28:4eb837cd71df | 58 | */ |
kevin1990 | 15:78f3f517417f | 59 | #include "mbed.h" |
kevin1990 | 15:78f3f517417f | 60 | #include "inc/adi_sense_api.h" |
kevin1990 | 15:78f3f517417f | 61 | #include "inc/adi_sense_1000/adi_sense_1000_api.h" |
kevin1990 | 15:78f3f517417f | 62 | #include "inc/adi_sense_log.h" |
kevin1990 | 15:78f3f517417f | 63 | #include "common/utils.h" |
kevin1990 | 15:78f3f517417f | 64 | |
Ross_o_halloran | 35:cc01f0766fc2 | 65 | extern ADI_SENSE_CONFIG babyseat_conf; |
Ross_o_halloran | 35:cc01f0766fc2 | 66 | extern ADI_SENSE_1000_LUT_DESCRIPTOR *sample_lut_desc_list[]; |
Ross_o_halloran | 35:cc01f0766fc2 | 67 | extern ADI_SENSE_1000_LUT_TABLE_DATA *sample_lut_data_list[]; |
Ross_o_halloran | 35:cc01f0766fc2 | 68 | extern unsigned sample_lut_num_tables; |
Ross_o_halloran | 35:cc01f0766fc2 | 69 | |
kevin1990 | 15:78f3f517417f | 70 | |
kevin1990 | 15:78f3f517417f | 71 | /* Change the following pointer to select any of the configurations above */ |
Ross_o_halloran | 35:cc01f0766fc2 | 72 | static ADI_SENSE_CONFIG *pSelectedConfig = &babyseat_conf; |
kevin1990 | 15:78f3f517417f | 73 | |
kevin1990 | 15:78f3f517417f | 74 | static ADI_SENSE_CONNECTION connectionInfo = { |
kevin1990 | 15:78f3f517417f | 75 | .type = ADI_SENSE_CONNECTION_TYPE_SPI, |
kevin1990 | 15:78f3f517417f | 76 | .spi = { |
kevin1990 | 15:78f3f517417f | 77 | .mosiPin = SPI_MOSI, |
kevin1990 | 15:78f3f517417f | 78 | .misoPin = SPI_MISO, |
kevin1990 | 15:78f3f517417f | 79 | .sckPin = SPI_SCK, |
kevin1990 | 15:78f3f517417f | 80 | .csPin = D10, |
kevin1990 | 15:78f3f517417f | 81 | .maxSpeedHz = 2000000, |
kevin1990 | 15:78f3f517417f | 82 | }, |
kevin1990 | 15:78f3f517417f | 83 | .gpio = { |
kevin1990 | 15:78f3f517417f | 84 | .resetPin = D6, |
kevin1990 | 15:78f3f517417f | 85 | .errorPin = D3, |
kevin1990 | 15:78f3f517417f | 86 | .alertPin = D4, |
kevin1990 | 15:78f3f517417f | 87 | .datareadyPin = D5, |
kevin1990 | 15:78f3f517417f | 88 | }, |
kevin1990 | 28:4eb837cd71df | 89 | .log = { |
kevin1990 | 28:4eb837cd71df | 90 | .txPin = PA_11, |
kevin1990 | 28:4eb837cd71df | 91 | .rxPin = PA_12, |
kevin1990 | 28:4eb837cd71df | 92 | .baudRate = 115200, |
kevin1990 | 28:4eb837cd71df | 93 | .disableLogs = false, |
kevin1990 | 28:4eb837cd71df | 94 | }, |
kevin1990 | 15:78f3f517417f | 95 | }; |
kevin1990 | 15:78f3f517417f | 96 | |
kevin1990 | 15:78f3f517417f | 97 | int main() |
kevin1990 | 15:78f3f517417f | 98 | { |
kevin1990 | 15:78f3f517417f | 99 | ADI_SENSE_RESULT res; |
kevin1990 | 28:4eb837cd71df | 100 | ADI_SENSE_STATUS status; |
kevin1990 | 15:78f3f517417f | 101 | ADI_SENSE_DEVICE_HANDLE hDevice; |
kevin1990 | 15:78f3f517417f | 102 | ADI_SENSE_MEASUREMENT_MODE eMeasurementMode = ADI_SENSE_MEASUREMENT_MODE_NORMAL; |
kevin1990 | 15:78f3f517417f | 103 | bool_t bDeviceReady; |
kevin1990 | 15:78f3f517417f | 104 | |
kevin1990 | 15:78f3f517417f | 105 | /* |
kevin1990 | 15:78f3f517417f | 106 | * Open an ADI Sense device instance. |
kevin1990 | 15:78f3f517417f | 107 | */ |
kevin1990 | 15:78f3f517417f | 108 | res = adi_sense_Open(0, &connectionInfo, &hDevice); |
kevin1990 | 15:78f3f517417f | 109 | if (res != ADI_SENSE_SUCCESS) |
kevin1990 | 15:78f3f517417f | 110 | { |
kevin1990 | 15:78f3f517417f | 111 | ADI_SENSE_LOG_ERROR("Failed to open device instance"); |
kevin1990 | 15:78f3f517417f | 112 | return res; |
kevin1990 | 15:78f3f517417f | 113 | } |
kevin1990 | 15:78f3f517417f | 114 | |
kevin1990 | 15:78f3f517417f | 115 | /* |
kevin1990 | 15:78f3f517417f | 116 | * Reset the given ADI Sense device.... |
kevin1990 | 15:78f3f517417f | 117 | */ |
kevin1990 | 15:78f3f517417f | 118 | ADI_SENSE_LOG_INFO("Resetting ADI Sense device, please wait..."); |
kevin1990 | 15:78f3f517417f | 119 | res = adi_sense_Reset(hDevice); |
kevin1990 | 15:78f3f517417f | 120 | if (res != ADI_SENSE_SUCCESS) |
kevin1990 | 15:78f3f517417f | 121 | { |
kevin1990 | 15:78f3f517417f | 122 | ADI_SENSE_LOG_ERROR("Failed to reset device"); |
kevin1990 | 15:78f3f517417f | 123 | return res; |
kevin1990 | 15:78f3f517417f | 124 | } |
kevin1990 | 15:78f3f517417f | 125 | /* |
kevin1990 | 15:78f3f517417f | 126 | * ...and wait until the device is ready. |
kevin1990 | 15:78f3f517417f | 127 | */ |
kevin1990 | 15:78f3f517417f | 128 | do { |
kevin1990 | 15:78f3f517417f | 129 | wait_ms(100); |
kevin1990 | 15:78f3f517417f | 130 | res = adi_sense_GetDeviceReadyState(hDevice, &bDeviceReady); |
kevin1990 | 15:78f3f517417f | 131 | if (res != ADI_SENSE_SUCCESS) |
kevin1990 | 15:78f3f517417f | 132 | { |
kevin1990 | 15:78f3f517417f | 133 | ADI_SENSE_LOG_ERROR("Failed to get device ready-state"); |
kevin1990 | 15:78f3f517417f | 134 | return res; |
kevin1990 | 15:78f3f517417f | 135 | } |
kevin1990 | 15:78f3f517417f | 136 | } while (! bDeviceReady); |
kevin1990 | 15:78f3f517417f | 137 | ADI_SENSE_LOG_INFO("ADI Sense device ready"); |
kevin1990 | 15:78f3f517417f | 138 | |
Ross_o_halloran | 35:cc01f0766fc2 | 139 | /* |
Ross_o_halloran | 35:cc01f0766fc2 | 140 | * Assemble the list of user-defined Look-Up Tables from sample_lut_data.c |
Ross_o_halloran | 35:cc01f0766fc2 | 141 | * into the single contiguous data format required by the device. |
Ross_o_halloran | 35:cc01f0766fc2 | 142 | */ |
Ross_o_halloran | 35:cc01f0766fc2 | 143 | unsigned lutBufferSize = ADI_SENSE_LUT_MAX_SIZE; |
Ross_o_halloran | 35:cc01f0766fc2 | 144 | ADI_SENSE_1000_LUT *pLutBuffer = (ADI_SENSE_1000_LUT *) ::operator new (lutBufferSize); |
Ross_o_halloran | 35:cc01f0766fc2 | 145 | if (pLutBuffer == NULL) |
Ross_o_halloran | 35:cc01f0766fc2 | 146 | { |
Ross_o_halloran | 35:cc01f0766fc2 | 147 | ADI_SENSE_LOG_ERROR("Failed to allocate memory for user-defined LUT data buffer"); |
Ross_o_halloran | 35:cc01f0766fc2 | 148 | return ADI_SENSE_NO_MEM; |
Ross_o_halloran | 35:cc01f0766fc2 | 149 | } |
Ross_o_halloran | 35:cc01f0766fc2 | 150 | |
Ross_o_halloran | 35:cc01f0766fc2 | 151 | ADI_SENSE_LOG_INFO("Assembling LUT data"); |
Ross_o_halloran | 35:cc01f0766fc2 | 152 | res = adi_sense_1000_AssembleLutData(pLutBuffer, lutBufferSize, |
Ross_o_halloran | 35:cc01f0766fc2 | 153 | sample_lut_num_tables, |
Ross_o_halloran | 35:cc01f0766fc2 | 154 | sample_lut_desc_list, |
Ross_o_halloran | 35:cc01f0766fc2 | 155 | sample_lut_data_list); |
Ross_o_halloran | 35:cc01f0766fc2 | 156 | if (res != ADI_SENSE_SUCCESS) |
Ross_o_halloran | 35:cc01f0766fc2 | 157 | { |
Ross_o_halloran | 35:cc01f0766fc2 | 158 | ADI_SENSE_LOG_ERROR("Failed to assemble user-defined LUT data"); |
Ross_o_halloran | 35:cc01f0766fc2 | 159 | return res; |
Ross_o_halloran | 35:cc01f0766fc2 | 160 | } |
Ross_o_halloran | 35:cc01f0766fc2 | 161 | |
Ross_o_halloran | 35:cc01f0766fc2 | 162 | /* |
Ross_o_halloran | 35:cc01f0766fc2 | 163 | * Write assembled user-defined Look-Up Table data structure to the device |
Ross_o_halloran | 35:cc01f0766fc2 | 164 | * User-defined LUT data is not applied until adi_sense_ApplyConfigUpdates() is called. |
Ross_o_halloran | 35:cc01f0766fc2 | 165 | */ |
Ross_o_halloran | 35:cc01f0766fc2 | 166 | ADI_SENSE_LOG_INFO("Setting LUT data"); |
Ross_o_halloran | 35:cc01f0766fc2 | 167 | res = adi_sense_1000_SetLutData(hDevice, pLutBuffer); |
Ross_o_halloran | 35:cc01f0766fc2 | 168 | if (res != ADI_SENSE_SUCCESS) |
Ross_o_halloran | 35:cc01f0766fc2 | 169 | { |
Ross_o_halloran | 35:cc01f0766fc2 | 170 | ADI_SENSE_LOG_ERROR("Failed to set user-defined LUT data"); |
Ross_o_halloran | 35:cc01f0766fc2 | 171 | return res; |
Ross_o_halloran | 35:cc01f0766fc2 | 172 | } |
Ross_o_halloran | 35:cc01f0766fc2 | 173 | |
Ross_o_halloran | 35:cc01f0766fc2 | 174 | delete pLutBuffer; |
kevin1990 | 15:78f3f517417f | 175 | /* |
kevin1990 | 15:78f3f517417f | 176 | * Write configuration settings to the device registers. |
kevin1990 | 15:78f3f517417f | 177 | * Settings are not applied until adi_sense_ApplyConfigUpdates() is called. |
kevin1990 | 15:78f3f517417f | 178 | */ |
kevin1990 | 15:78f3f517417f | 179 | ADI_SENSE_LOG_INFO("Setting device configuration"); |
kevin1990 | 15:78f3f517417f | 180 | res = adi_sense_SetConfig(hDevice, pSelectedConfig); |
kevin1990 | 15:78f3f517417f | 181 | if (res != ADI_SENSE_SUCCESS) |
kevin1990 | 15:78f3f517417f | 182 | { |
kevin1990 | 15:78f3f517417f | 183 | ADI_SENSE_LOG_ERROR("Failed to set device configuration"); |
kevin1990 | 15:78f3f517417f | 184 | return res; |
kevin1990 | 15:78f3f517417f | 185 | } |
kevin1990 | 15:78f3f517417f | 186 | res = adi_sense_ApplyConfigUpdates(hDevice); |
kevin1990 | 15:78f3f517417f | 187 | if (res != ADI_SENSE_SUCCESS) |
kevin1990 | 15:78f3f517417f | 188 | { |
kevin1990 | 15:78f3f517417f | 189 | ADI_SENSE_LOG_ERROR("Failed to apply device configuration"); |
kevin1990 | 15:78f3f517417f | 190 | return res; |
kevin1990 | 15:78f3f517417f | 191 | } |
kevin1990 | 28:4eb837cd71df | 192 | /* |
kevin1990 | 28:4eb837cd71df | 193 | * Check device status after updating the configuration |
kevin1990 | 28:4eb837cd71df | 194 | */ |
kevin1990 | 28:4eb837cd71df | 195 | res = adi_sense_GetStatus(hDevice, &status); |
kevin1990 | 28:4eb837cd71df | 196 | if (res != ADI_SENSE_SUCCESS) |
kevin1990 | 28:4eb837cd71df | 197 | { |
kevin1990 | 28:4eb837cd71df | 198 | ADI_SENSE_LOG_ERROR("Failed to retrieve device status"); |
kevin1990 | 28:4eb837cd71df | 199 | return res; |
kevin1990 | 28:4eb837cd71df | 200 | } |
kevin1990 | 28:4eb837cd71df | 201 | if (status.deviceStatus & |
kevin1990 | 28:4eb837cd71df | 202 | (ADI_SENSE_DEVICE_STATUS_ERROR | ADI_SENSE_DEVICE_STATUS_ALERT)) |
kevin1990 | 28:4eb837cd71df | 203 | { |
kevin1990 | 28:4eb837cd71df | 204 | utils_printStatus(&status); |
kevin1990 | 28:4eb837cd71df | 205 | } |
kevin1990 | 15:78f3f517417f | 206 | |
kevin1990 | 15:78f3f517417f | 207 | /* |
kevin1990 | 15:78f3f517417f | 208 | * Kick off the measurement cycle here |
kevin1990 | 15:78f3f517417f | 209 | */ |
kevin1990 | 15:78f3f517417f | 210 | ADI_SENSE_LOG_INFO("Configuration completed, starting measurement cycles"); |
kevin1990 | 15:78f3f517417f | 211 | utils_runMeasurement(hDevice, eMeasurementMode); |
kevin1990 | 15:78f3f517417f | 212 | |
kevin1990 | 15:78f3f517417f | 213 | /* |
kevin1990 | 15:78f3f517417f | 214 | * Clean up and exit |
kevin1990 | 15:78f3f517417f | 215 | */ |
kevin1990 | 15:78f3f517417f | 216 | res = adi_sense_Close(hDevice); |
kevin1990 | 15:78f3f517417f | 217 | if (res != ADI_SENSE_SUCCESS) |
kevin1990 | 15:78f3f517417f | 218 | { |
kevin1990 | 15:78f3f517417f | 219 | ADI_SENSE_LOG_ERROR("Failed to close device instance"); |
kevin1990 | 15:78f3f517417f | 220 | return res; |
kevin1990 | 15:78f3f517417f | 221 | } |
kevin1990 | 15:78f3f517417f | 222 | |
kevin1990 | 15:78f3f517417f | 223 | return 0; |
kevin1990 | 15:78f3f517417f | 224 | } |