added wait_us(31) in admw_spi.cpp to support hibernation mode

Committer:
ADIJake
Date:
Mon Apr 01 11:09:52 2019 +0000
Revision:
0:85855ecd3257
Initial Commit

Who changed what in which revision?

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