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