Rohan Gurav
/
Sean_AdiSense1000_V21
ADISense1000 Version 2.1 code base
Fork of AdiSense1000_V21 by
src/mbed/adi_sense_spi.cpp@28:4eb837cd71df, 2018-05-17 (annotated)
- Committer:
- kevin1990
- Date:
- Thu May 17 10:34:45 2018 +0100
- Revision:
- 28:4eb837cd71df
- Parent:
- 26:12d0204be712
- Child:
- 31:de49744b57a6
Adding host library and example code for v1.3 release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
danodonovan | 26:12d0204be712 | 1 | /****************************************************************************** |
danodonovan | 26:12d0204be712 | 2 | Copyright 2017 (c) Analog Devices, Inc. |
danodonovan | 26:12d0204be712 | 3 | |
danodonovan | 26:12d0204be712 | 4 | All rights reserved. |
danodonovan | 26:12d0204be712 | 5 | |
danodonovan | 26:12d0204be712 | 6 | Redistribution and use in source and binary forms, with or without |
danodonovan | 26:12d0204be712 | 7 | modification, are permitted provided that the following conditions are met: |
danodonovan | 26:12d0204be712 | 8 | - Redistributions of source code must retain the above copyright |
danodonovan | 26:12d0204be712 | 9 | notice, this list of conditions and the following disclaimer. |
danodonovan | 26:12d0204be712 | 10 | - Redistributions in binary form must reproduce the above copyright |
danodonovan | 26:12d0204be712 | 11 | notice, this list of conditions and the following disclaimer in |
danodonovan | 26:12d0204be712 | 12 | the documentation and/or other materials provided with the |
danodonovan | 26:12d0204be712 | 13 | distribution. |
danodonovan | 26:12d0204be712 | 14 | - Neither the name of Analog Devices, Inc. nor the names of its |
danodonovan | 26:12d0204be712 | 15 | contributors may be used to endorse or promote products derived |
danodonovan | 26:12d0204be712 | 16 | from this software without specific prior written permission. |
danodonovan | 26:12d0204be712 | 17 | - The use of this software may or may not infringe the patent rights |
danodonovan | 26:12d0204be712 | 18 | of one or more patent holders. This license does not release you |
danodonovan | 26:12d0204be712 | 19 | from the requirement that you obtain separate licenses from these |
danodonovan | 26:12d0204be712 | 20 | patent holders to use this software. |
danodonovan | 26:12d0204be712 | 21 | - Use of the software either in source or binary form, must be run |
danodonovan | 26:12d0204be712 | 22 | on or directly connected to an Analog Devices Inc. component. |
danodonovan | 26:12d0204be712 | 23 | |
danodonovan | 26:12d0204be712 | 24 | THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
danodonovan | 26:12d0204be712 | 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
danodonovan | 26:12d0204be712 | 26 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
danodonovan | 26:12d0204be712 | 27 | IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
danodonovan | 26:12d0204be712 | 28 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
danodonovan | 26:12d0204be712 | 29 | LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
danodonovan | 26:12d0204be712 | 30 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
danodonovan | 26:12d0204be712 | 31 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
danodonovan | 26:12d0204be712 | 32 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
danodonovan | 26:12d0204be712 | 33 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
danodonovan | 26:12d0204be712 | 34 | * |
danodonovan | 26:12d0204be712 | 35 | *****************************************************************************/ |
danodonovan | 26:12d0204be712 | 36 | |
kevin1990 | 28:4eb837cd71df | 37 | /*! |
kevin1990 | 28:4eb837cd71df | 38 | ****************************************************************************** |
kevin1990 | 28:4eb837cd71df | 39 | * @file: |
kevin1990 | 28:4eb837cd71df | 40 | * @brief: ADISENSE OS-dependent wrapper layer for SPI interface |
kevin1990 | 28:4eb837cd71df | 41 | *----------------------------------------------------------------------------- |
kevin1990 | 28:4eb837cd71df | 42 | */ |
kevin1990 | 28:4eb837cd71df | 43 | |
danodonovan | 26:12d0204be712 | 44 | #include <mbed.h> |
danodonovan | 26:12d0204be712 | 45 | |
danodonovan | 26:12d0204be712 | 46 | #include "inc/adi_sense_spi.h" |
danodonovan | 26:12d0204be712 | 47 | #include "inc/adi_sense_log.h" |
danodonovan | 26:12d0204be712 | 48 | |
danodonovan | 26:12d0204be712 | 49 | #define ADI_SENSE_SPI_MODE 0 /* CPOL=0, CPHA=0 */ |
danodonovan | 26:12d0204be712 | 50 | #define ADI_SENSE_SPI_FRAME_SIZE 8 /* 8-bit frame size */ |
danodonovan | 26:12d0204be712 | 51 | |
danodonovan | 26:12d0204be712 | 52 | class SpiContext |
danodonovan | 26:12d0204be712 | 53 | { |
danodonovan | 26:12d0204be712 | 54 | public: |
danodonovan | 26:12d0204be712 | 55 | SpiContext( |
danodonovan | 26:12d0204be712 | 56 | PinName mosiPin, |
danodonovan | 26:12d0204be712 | 57 | PinName misoPin, |
danodonovan | 26:12d0204be712 | 58 | PinName sckPin, |
danodonovan | 26:12d0204be712 | 59 | PinName csPin, |
danodonovan | 26:12d0204be712 | 60 | unsigned maxSpeed); |
danodonovan | 26:12d0204be712 | 61 | |
danodonovan | 26:12d0204be712 | 62 | int transfer( |
danodonovan | 26:12d0204be712 | 63 | void *pTxData, |
danodonovan | 26:12d0204be712 | 64 | void *pRxData, |
danodonovan | 26:12d0204be712 | 65 | unsigned nLength, |
danodonovan | 26:12d0204be712 | 66 | bool bCsHold); |
danodonovan | 26:12d0204be712 | 67 | |
danodonovan | 26:12d0204be712 | 68 | private: |
danodonovan | 26:12d0204be712 | 69 | SPI _spi; |
danodonovan | 26:12d0204be712 | 70 | DigitalOut _cs; |
danodonovan | 26:12d0204be712 | 71 | |
danodonovan | 26:12d0204be712 | 72 | event_callback_t _callback; |
danodonovan | 26:12d0204be712 | 73 | volatile int _cbEvent; |
danodonovan | 26:12d0204be712 | 74 | volatile bool _cbFired; |
danodonovan | 26:12d0204be712 | 75 | |
danodonovan | 26:12d0204be712 | 76 | void _cbHandler( |
danodonovan | 26:12d0204be712 | 77 | int event); |
danodonovan | 26:12d0204be712 | 78 | |
danodonovan | 26:12d0204be712 | 79 | int _waitForCallback( |
danodonovan | 26:12d0204be712 | 80 | void); |
danodonovan | 26:12d0204be712 | 81 | }; |
danodonovan | 26:12d0204be712 | 82 | |
danodonovan | 26:12d0204be712 | 83 | SpiContext::SpiContext(PinName mosiPin, |
danodonovan | 26:12d0204be712 | 84 | PinName misoPin, |
danodonovan | 26:12d0204be712 | 85 | PinName sckPin, |
danodonovan | 26:12d0204be712 | 86 | PinName csPin, |
danodonovan | 26:12d0204be712 | 87 | unsigned maxSpeed) |
danodonovan | 26:12d0204be712 | 88 | : _spi(mosiPin, misoPin, sckPin), |
danodonovan | 26:12d0204be712 | 89 | _cs(csPin, 1) |
danodonovan | 26:12d0204be712 | 90 | { |
danodonovan | 26:12d0204be712 | 91 | _cbEvent = 0; |
danodonovan | 26:12d0204be712 | 92 | _cbFired = false; |
danodonovan | 26:12d0204be712 | 93 | _callback = callback(this, &SpiContext::_cbHandler); |
danodonovan | 26:12d0204be712 | 94 | |
danodonovan | 26:12d0204be712 | 95 | _spi.format(ADI_SENSE_SPI_FRAME_SIZE, ADI_SENSE_SPI_MODE); |
danodonovan | 26:12d0204be712 | 96 | _spi.frequency(maxSpeed); |
danodonovan | 26:12d0204be712 | 97 | } |
danodonovan | 26:12d0204be712 | 98 | |
danodonovan | 26:12d0204be712 | 99 | void SpiContext::_cbHandler(int event) |
danodonovan | 26:12d0204be712 | 100 | { |
danodonovan | 26:12d0204be712 | 101 | _cbEvent = event; |
danodonovan | 26:12d0204be712 | 102 | _cbFired = true; |
danodonovan | 26:12d0204be712 | 103 | } |
danodonovan | 26:12d0204be712 | 104 | |
danodonovan | 26:12d0204be712 | 105 | int SpiContext::_waitForCallback(void) |
danodonovan | 26:12d0204be712 | 106 | { |
danodonovan | 26:12d0204be712 | 107 | int rc = 0; |
danodonovan | 26:12d0204be712 | 108 | |
danodonovan | 26:12d0204be712 | 109 | while ((_cbFired != true) && (_cbEvent != SPI_EVENT_COMPLETE)) |
danodonovan | 26:12d0204be712 | 110 | { ; } |
danodonovan | 26:12d0204be712 | 111 | |
danodonovan | 26:12d0204be712 | 112 | _cbFired = false; |
danodonovan | 26:12d0204be712 | 113 | _cbEvent = 0; |
danodonovan | 26:12d0204be712 | 114 | |
danodonovan | 26:12d0204be712 | 115 | return rc; |
danodonovan | 26:12d0204be712 | 116 | } |
danodonovan | 26:12d0204be712 | 117 | |
danodonovan | 26:12d0204be712 | 118 | int SpiContext::transfer( |
danodonovan | 26:12d0204be712 | 119 | void *pTxData, |
danodonovan | 26:12d0204be712 | 120 | void *pRxData, |
danodonovan | 26:12d0204be712 | 121 | unsigned nLength, |
danodonovan | 26:12d0204be712 | 122 | bool bCsHold) |
danodonovan | 26:12d0204be712 | 123 | { |
danodonovan | 26:12d0204be712 | 124 | int rc = 0; |
danodonovan | 26:12d0204be712 | 125 | |
danodonovan | 26:12d0204be712 | 126 | _cs = 0; |
danodonovan | 26:12d0204be712 | 127 | |
danodonovan | 26:12d0204be712 | 128 | rc = _spi.transfer((uint8_t *)pTxData, nLength, |
danodonovan | 26:12d0204be712 | 129 | (uint8_t *)pRxData, nLength, |
danodonovan | 26:12d0204be712 | 130 | _callback, SPI_EVENT_COMPLETE); |
danodonovan | 26:12d0204be712 | 131 | if (rc < 0) |
danodonovan | 26:12d0204be712 | 132 | { |
danodonovan | 26:12d0204be712 | 133 | _cs = 1; |
danodonovan | 26:12d0204be712 | 134 | return rc; |
kevin1990 | 28:4eb837cd71df | 135 | } |
danodonovan | 26:12d0204be712 | 136 | |
danodonovan | 26:12d0204be712 | 137 | rc = _waitForCallback(); |
danodonovan | 26:12d0204be712 | 138 | if ((rc < 0) || !bCsHold) |
danodonovan | 26:12d0204be712 | 139 | _cs = 1; |
danodonovan | 26:12d0204be712 | 140 | |
danodonovan | 26:12d0204be712 | 141 | return rc; |
danodonovan | 26:12d0204be712 | 142 | } |
danodonovan | 26:12d0204be712 | 143 | |
danodonovan | 26:12d0204be712 | 144 | #ifdef __cplusplus |
danodonovan | 26:12d0204be712 | 145 | extern "C" { |
danodonovan | 26:12d0204be712 | 146 | #endif |
danodonovan | 26:12d0204be712 | 147 | |
danodonovan | 26:12d0204be712 | 148 | /* |
danodonovan | 26:12d0204be712 | 149 | * Open the SPI interface and allocate resources |
danodonovan | 26:12d0204be712 | 150 | */ |
danodonovan | 26:12d0204be712 | 151 | ADI_SENSE_RESULT adi_sense_SpiOpen( |
danodonovan | 26:12d0204be712 | 152 | ADI_SENSE_PLATFORM_SPI_CONFIG *pConfig, |
danodonovan | 26:12d0204be712 | 153 | ADI_SENSE_SPI_HANDLE *phDevice) |
danodonovan | 26:12d0204be712 | 154 | { |
danodonovan | 26:12d0204be712 | 155 | SpiContext *pCtx = new SpiContext((PinName)pConfig->mosiPin, |
danodonovan | 26:12d0204be712 | 156 | (PinName)pConfig->misoPin, |
danodonovan | 26:12d0204be712 | 157 | (PinName)pConfig->sckPin, |
danodonovan | 26:12d0204be712 | 158 | (PinName)pConfig->csPin, |
danodonovan | 26:12d0204be712 | 159 | pConfig->maxSpeedHz); |
danodonovan | 26:12d0204be712 | 160 | if (!pCtx) |
danodonovan | 26:12d0204be712 | 161 | { |
danodonovan | 26:12d0204be712 | 162 | ADI_SENSE_LOG_ERROR("Failed to allocate memory for SPI interface"); |
danodonovan | 26:12d0204be712 | 163 | return ADI_SENSE_NO_MEM; |
danodonovan | 26:12d0204be712 | 164 | } |
danodonovan | 26:12d0204be712 | 165 | |
danodonovan | 26:12d0204be712 | 166 | *phDevice = reinterpret_cast<ADI_SENSE_SPI_HANDLE>(pCtx); |
danodonovan | 26:12d0204be712 | 167 | return ADI_SENSE_SUCCESS; |
danodonovan | 26:12d0204be712 | 168 | } |
danodonovan | 26:12d0204be712 | 169 | |
danodonovan | 26:12d0204be712 | 170 | /* |
danodonovan | 26:12d0204be712 | 171 | * Execute a bi-directional data transfer on the SPI interface |
danodonovan | 26:12d0204be712 | 172 | */ |
danodonovan | 26:12d0204be712 | 173 | ADI_SENSE_RESULT |
danodonovan | 26:12d0204be712 | 174 | adi_sense_SpiTransfer( |
danodonovan | 26:12d0204be712 | 175 | ADI_SENSE_SPI_HANDLE hDevice, |
danodonovan | 26:12d0204be712 | 176 | void *pTxData, |
danodonovan | 26:12d0204be712 | 177 | void *pRxData, |
danodonovan | 26:12d0204be712 | 178 | unsigned nLength, |
danodonovan | 26:12d0204be712 | 179 | bool bCsHold) |
danodonovan | 26:12d0204be712 | 180 | { |
danodonovan | 26:12d0204be712 | 181 | SpiContext *pCtx = reinterpret_cast<SpiContext *>(hDevice); |
danodonovan | 26:12d0204be712 | 182 | |
danodonovan | 26:12d0204be712 | 183 | if (pCtx->transfer(pTxData, pRxData, nLength, bCsHold) < 0) |
danodonovan | 26:12d0204be712 | 184 | { |
danodonovan | 26:12d0204be712 | 185 | ADI_SENSE_LOG_ERROR("Failed to complete SPI transfer"); |
danodonovan | 26:12d0204be712 | 186 | return ADI_SENSE_FAILURE; |
danodonovan | 26:12d0204be712 | 187 | } |
danodonovan | 26:12d0204be712 | 188 | |
danodonovan | 26:12d0204be712 | 189 | return ADI_SENSE_SUCCESS; |
danodonovan | 26:12d0204be712 | 190 | } |
danodonovan | 26:12d0204be712 | 191 | |
danodonovan | 26:12d0204be712 | 192 | /* |
danodonovan | 26:12d0204be712 | 193 | * Close the SPI interface and free resources |
danodonovan | 26:12d0204be712 | 194 | */ |
danodonovan | 26:12d0204be712 | 195 | void adi_sense_SpiClose( |
danodonovan | 26:12d0204be712 | 196 | ADI_SENSE_SPI_HANDLE hDevice) |
danodonovan | 26:12d0204be712 | 197 | { |
danodonovan | 26:12d0204be712 | 198 | SpiContext *pCtx = reinterpret_cast<SpiContext *>(hDevice); |
danodonovan | 26:12d0204be712 | 199 | |
danodonovan | 26:12d0204be712 | 200 | delete pCtx; |
danodonovan | 26:12d0204be712 | 201 | } |
danodonovan | 26:12d0204be712 | 202 | |
danodonovan | 26:12d0204be712 | 203 | #ifdef __cplusplus |
danodonovan | 26:12d0204be712 | 204 | } |
danodonovan | 26:12d0204be712 | 205 | #endif |
danodonovan | 26:12d0204be712 | 206 | |
danodonovan | 26:12d0204be712 | 207 | /*! |
danodonovan | 26:12d0204be712 | 208 | * @} |
danodonovan | 26:12d0204be712 | 209 | */ |