ADISense1000 Version 2.1 code base

Fork of AdiSense1000_V21 by Sean Wilson

Committer:
Dan O'Donovan
Date:
Fri Jun 22 09:29:24 2018 +0100
Branch:
v2.0
Revision:
31:de49744b57a6
Parent:
27:567abf893938
Adding host library and example code for v2.1 release to the v2.0 branch

Who changed what in which revision?

UserRevisionLine numberNew 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
Dan O'Donovan 27:567abf893938 37 /*!
Dan O'Donovan 27:567abf893938 38 ******************************************************************************
Dan O'Donovan 27:567abf893938 39 * @file:
Dan O'Donovan 27:567abf893938 40 * @brief: ADISENSE OS-dependent wrapper layer for SPI interface
Dan O'Donovan 27:567abf893938 41 *-----------------------------------------------------------------------------
Dan O'Donovan 27:567abf893938 42 */
Dan O'Donovan 27:567abf893938 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
danodonovan 26:12d0204be712 73 SpiContext::SpiContext(PinName mosiPin,
danodonovan 26:12d0204be712 74 PinName misoPin,
danodonovan 26:12d0204be712 75 PinName sckPin,
danodonovan 26:12d0204be712 76 PinName csPin,
danodonovan 26:12d0204be712 77 unsigned maxSpeed)
danodonovan 26:12d0204be712 78 : _spi(mosiPin, misoPin, sckPin),
danodonovan 26:12d0204be712 79 _cs(csPin, 1)
danodonovan 26:12d0204be712 80 {
danodonovan 26:12d0204be712 81
danodonovan 26:12d0204be712 82 _spi.format(ADI_SENSE_SPI_FRAME_SIZE, ADI_SENSE_SPI_MODE);
danodonovan 26:12d0204be712 83 _spi.frequency(maxSpeed);
danodonovan 26:12d0204be712 84 }
danodonovan 26:12d0204be712 85
danodonovan 26:12d0204be712 86 int SpiContext::transfer(
danodonovan 26:12d0204be712 87 void *pTxData,
danodonovan 26:12d0204be712 88 void *pRxData,
danodonovan 26:12d0204be712 89 unsigned nLength,
danodonovan 26:12d0204be712 90 bool bCsHold)
danodonovan 26:12d0204be712 91 {
danodonovan 26:12d0204be712 92 int rc = 0;
danodonovan 26:12d0204be712 93
danodonovan 26:12d0204be712 94 _cs = 0;
danodonovan 26:12d0204be712 95
Dan O'Donovan 31:de49744b57a6 96 rc = _spi.write((char*)(pTxData), pTxData ? nLength : 0,
Dan O'Donovan 31:de49744b57a6 97 (char*)(pRxData), pRxData ? nLength : 0);
danodonovan 26:12d0204be712 98
danodonovan 26:12d0204be712 99 if ((rc < 0) || !bCsHold)
danodonovan 26:12d0204be712 100 _cs = 1;
danodonovan 26:12d0204be712 101
danodonovan 26:12d0204be712 102 return rc;
danodonovan 26:12d0204be712 103 }
danodonovan 26:12d0204be712 104
danodonovan 26:12d0204be712 105 #ifdef __cplusplus
danodonovan 26:12d0204be712 106 extern "C" {
danodonovan 26:12d0204be712 107 #endif
danodonovan 26:12d0204be712 108
danodonovan 26:12d0204be712 109 /*
danodonovan 26:12d0204be712 110 * Open the SPI interface and allocate resources
danodonovan 26:12d0204be712 111 */
danodonovan 26:12d0204be712 112 ADI_SENSE_RESULT adi_sense_SpiOpen(
danodonovan 26:12d0204be712 113 ADI_SENSE_PLATFORM_SPI_CONFIG *pConfig,
danodonovan 26:12d0204be712 114 ADI_SENSE_SPI_HANDLE *phDevice)
danodonovan 26:12d0204be712 115 {
danodonovan 26:12d0204be712 116 SpiContext *pCtx = new SpiContext((PinName)pConfig->mosiPin,
danodonovan 26:12d0204be712 117 (PinName)pConfig->misoPin,
danodonovan 26:12d0204be712 118 (PinName)pConfig->sckPin,
danodonovan 26:12d0204be712 119 (PinName)pConfig->csPin,
danodonovan 26:12d0204be712 120 pConfig->maxSpeedHz);
danodonovan 26:12d0204be712 121 if (!pCtx)
danodonovan 26:12d0204be712 122 {
danodonovan 26:12d0204be712 123 ADI_SENSE_LOG_ERROR("Failed to allocate memory for SPI interface");
danodonovan 26:12d0204be712 124 return ADI_SENSE_NO_MEM;
danodonovan 26:12d0204be712 125 }
danodonovan 26:12d0204be712 126
danodonovan 26:12d0204be712 127 *phDevice = reinterpret_cast<ADI_SENSE_SPI_HANDLE>(pCtx);
danodonovan 26:12d0204be712 128 return ADI_SENSE_SUCCESS;
danodonovan 26:12d0204be712 129 }
danodonovan 26:12d0204be712 130
danodonovan 26:12d0204be712 131 /*
danodonovan 26:12d0204be712 132 * Execute a bi-directional data transfer on the SPI interface
danodonovan 26:12d0204be712 133 */
danodonovan 26:12d0204be712 134 ADI_SENSE_RESULT
danodonovan 26:12d0204be712 135 adi_sense_SpiTransfer(
danodonovan 26:12d0204be712 136 ADI_SENSE_SPI_HANDLE hDevice,
danodonovan 26:12d0204be712 137 void *pTxData,
danodonovan 26:12d0204be712 138 void *pRxData,
danodonovan 26:12d0204be712 139 unsigned nLength,
danodonovan 26:12d0204be712 140 bool bCsHold)
danodonovan 26:12d0204be712 141 {
danodonovan 26:12d0204be712 142 SpiContext *pCtx = reinterpret_cast<SpiContext *>(hDevice);
danodonovan 26:12d0204be712 143
danodonovan 26:12d0204be712 144 if (pCtx->transfer(pTxData, pRxData, nLength, bCsHold) < 0)
danodonovan 26:12d0204be712 145 {
danodonovan 26:12d0204be712 146 ADI_SENSE_LOG_ERROR("Failed to complete SPI transfer");
danodonovan 26:12d0204be712 147 return ADI_SENSE_FAILURE;
danodonovan 26:12d0204be712 148 }
danodonovan 26:12d0204be712 149
danodonovan 26:12d0204be712 150 return ADI_SENSE_SUCCESS;
danodonovan 26:12d0204be712 151 }
danodonovan 26:12d0204be712 152
danodonovan 26:12d0204be712 153 /*
danodonovan 26:12d0204be712 154 * Close the SPI interface and free resources
danodonovan 26:12d0204be712 155 */
danodonovan 26:12d0204be712 156 void adi_sense_SpiClose(
danodonovan 26:12d0204be712 157 ADI_SENSE_SPI_HANDLE hDevice)
danodonovan 26:12d0204be712 158 {
danodonovan 26:12d0204be712 159 SpiContext *pCtx = reinterpret_cast<SpiContext *>(hDevice);
danodonovan 26:12d0204be712 160
danodonovan 26:12d0204be712 161 delete pCtx;
danodonovan 26:12d0204be712 162 }
danodonovan 26:12d0204be712 163
danodonovan 26:12d0204be712 164 #ifdef __cplusplus
danodonovan 26:12d0204be712 165 }
danodonovan 26:12d0204be712 166 #endif
danodonovan 26:12d0204be712 167
danodonovan 26:12d0204be712 168 /*!
danodonovan 26:12d0204be712 169 * @}
danodonovan 26:12d0204be712 170 */