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

Committer:
Vkadaba
Date:
Fri Mar 06 05:32:12 2020 +0000
Revision:
57:c6275de14bc5
Parent:
48:5731f1aa2c5a
Child:
60:363fffe5bd22
SUpporting the latest (v1.14.66) device FW

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ADIJake 0:85855ecd3257 1 /******************************************************************************
Vkadaba 48:5731f1aa2c5a 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:
Vkadaba 5:0728bde67bdb 40 * @brief: ADMW 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
Vkadaba 5:0728bde67bdb 46 #include "inc/admw_spi.h"
Vkadaba 5:0728bde67bdb 47 #include "inc/admw_log.h"
ADIJake 0:85855ecd3257 48
Vkadaba 5:0728bde67bdb 49 #define ADMW_SPI_MODE 0 /* CPOL=0, CPHA=0 */
Vkadaba 5:0728bde67bdb 50 #define ADMW_SPI_FRAME_SIZE 8 /* 8-bit frame size */
Vkadaba 5:0728bde67bdb 51
Vkadaba 5:0728bde67bdb 52
Vkadaba 5:0728bde67bdb 53 #ifdef __cplusplus
Vkadaba 5:0728bde67bdb 54 extern "C" {
Vkadaba 5:0728bde67bdb 55 #endif
ADIJake 0:85855ecd3257 56
Vkadaba 5:0728bde67bdb 57
Vkadaba 5:0728bde67bdb 58 // Struct to contain anything needed to identify the SPI device
Vkadaba 5:0728bde67bdb 59 // This is returned as a ADMW_SPI_HANDLE*, and is required to use
Vkadaba 5:0728bde67bdb 60 // the other SPI functions
Vkadaba 5:0728bde67bdb 61 typedef struct {
Vkadaba 5:0728bde67bdb 62 SPI *_spi;
Vkadaba 5:0728bde67bdb 63 DigitalOut *_cs;
Vkadaba 5:0728bde67bdb 64 DigitalOut *_wakeup;
Vkadaba 5:0728bde67bdb 65 } SpiContext_t;
Vkadaba 5:0728bde67bdb 66
ADIJake 0:85855ecd3257 67
Vkadaba 5:0728bde67bdb 68 /*
Vkadaba 5:0728bde67bdb 69 * Open the SPI interface and allocate resources
Vkadaba 5:0728bde67bdb 70 */
Vkadaba 5:0728bde67bdb 71 ADMW_RESULT admw_SpiOpen(
Vkadaba 5:0728bde67bdb 72 ADMW_PLATFORM_SPI_CONFIG *pConfig,
Vkadaba 5:0728bde67bdb 73 ADMW_SPI_HANDLE *phDevice)
ADIJake 0:85855ecd3257 74 {
Vkadaba 5:0728bde67bdb 75 SpiContext_t *pCtx = (SpiContext_t*)malloc(sizeof(*pCtx));
Vkadaba 5:0728bde67bdb 76 if(!pCtx) {
Vkadaba 5:0728bde67bdb 77 ADMW_LOG_ERROR("Failed to allocate memory for SPI device");
Vkadaba 5:0728bde67bdb 78 return ADMW_NO_MEM;
Vkadaba 5:0728bde67bdb 79 }
Vkadaba 32:52445bef314d 80
Vkadaba 5:0728bde67bdb 81 pCtx->_spi = new SPI((PinName)pConfig->mosiPin,
Vkadaba 32:52445bef314d 82 (PinName)pConfig->misoPin,
Vkadaba 32:52445bef314d 83 (PinName)pConfig->sckPin);
Vkadaba 5:0728bde67bdb 84 pCtx->_cs = new DigitalOut((PinName)pConfig->csPin, 1);
Vkadaba 32:52445bef314d 85
Vkadaba 5:0728bde67bdb 86 pCtx->_spi->format(ADMW_SPI_FRAME_SIZE, ADMW_SPI_MODE);
Vkadaba 5:0728bde67bdb 87 pCtx->_spi->frequency(pConfig->maxSpeedHz);
Vkadaba 32:52445bef314d 88
Vkadaba 5:0728bde67bdb 89 pCtx->_wakeup = new DigitalOut((PinName)pConfig->wakeupPin, 1);
Vkadaba 32:52445bef314d 90
Vkadaba 5:0728bde67bdb 91 *phDevice = (ADMW_SPI_HANDLE)pCtx;
Vkadaba 32:52445bef314d 92
Vkadaba 5:0728bde67bdb 93 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 94 }
ADIJake 0:85855ecd3257 95
Vkadaba 23:bb685f35b08b 96 ADMW_RESULT
Vkadaba 13:97cb32670539 97 admw_SpiReceive(
Vkadaba 13:97cb32670539 98 ADMW_SPI_HANDLE hDevice,
Vkadaba 13:97cb32670539 99 void *pTxData,
Vkadaba 13:97cb32670539 100 void *pRxData,
Vkadaba 13:97cb32670539 101 unsigned nLength,
Vkadaba 13:97cb32670539 102 bool bCsHold)
Vkadaba 13:97cb32670539 103 {
Vkadaba 23:bb685f35b08b 104 SpiContext_t *pCtx = (SpiContext_t*)hDevice;
Vkadaba 32:52445bef314d 105
Vkadaba 23:bb685f35b08b 106 pCtx->_spi->format(ADMW_SPI_FRAME_SIZE, 1);
Vkadaba 32:52445bef314d 107
Vkadaba 23:bb685f35b08b 108 int rc = 0;
Vkadaba 48:5731f1aa2c5a 109
Vkadaba 48:5731f1aa2c5a 110 *(pCtx->_wakeup) = 1;
Vkadaba 32:52445bef314d 111
Vkadaba 13:97cb32670539 112 *(pCtx->_cs) = 0;
Vkadaba 32:52445bef314d 113
Vkadaba 48:5731f1aa2c5a 114
Vkadaba 48:5731f1aa2c5a 115
Vkadaba 13:97cb32670539 116 rc = pCtx->_spi->write((char*)(pTxData), pTxData ? nLength : 0,
Vkadaba 32:52445bef314d 117 (char*)(pRxData), pRxData ? nLength : 0);
Vkadaba 32:52445bef314d 118
Vkadaba 13:97cb32670539 119 if ((rc < 0) || !bCsHold)
Vkadaba 13:97cb32670539 120 *(pCtx->_cs) = 1;
Vkadaba 32:52445bef314d 121
Vkadaba 48:5731f1aa2c5a 122 *(pCtx->_wakeup) = 0;
Vkadaba 48:5731f1aa2c5a 123
Vkadaba 32:52445bef314d 124 if (rc < 0)
Vkadaba 32:52445bef314d 125 {
Vkadaba 13:97cb32670539 126 ADMW_LOG_ERROR("Failed to complete SPI transfer");
Vkadaba 13:97cb32670539 127 return ADMW_FAILURE;
Vkadaba 13:97cb32670539 128 }
Vkadaba 32:52445bef314d 129
Vkadaba 13:97cb32670539 130 pCtx->_spi->format(ADMW_SPI_FRAME_SIZE, ADMW_SPI_MODE);
Vkadaba 32:52445bef314d 131
Vkadaba 32:52445bef314d 132 return ADMW_SUCCESS;
Vkadaba 32:52445bef314d 133 }
Vkadaba 23:bb685f35b08b 134
Vkadaba 32:52445bef314d 135 /*
Vkadaba 32:52445bef314d 136 * Execute a bi-directional data transfer on the SPI interface
Vkadaba 32:52445bef314d 137 */
Vkadaba 5:0728bde67bdb 138 ADMW_RESULT
Vkadaba 5:0728bde67bdb 139 admw_SpiTransfer(
Vkadaba 5:0728bde67bdb 140 ADMW_SPI_HANDLE hDevice,
ADIJake 0:85855ecd3257 141 void *pTxData,
ADIJake 0:85855ecd3257 142 void *pRxData,
ADIJake 0:85855ecd3257 143 unsigned nLength,
ADIJake 0:85855ecd3257 144 bool bCsHold)
ADIJake 0:85855ecd3257 145 {
ADIJake 0:85855ecd3257 146 int rc = 0;
Vkadaba 32:52445bef314d 147
Vkadaba 5:0728bde67bdb 148 SpiContext_t *pCtx = (SpiContext_t*)hDevice;
Vkadaba 48:5731f1aa2c5a 149
Vkadaba 48:5731f1aa2c5a 150 *(pCtx->_wakeup) = 1;
Vkadaba 5:0728bde67bdb 151 *(pCtx->_cs) = 0;
Vkadaba 32:52445bef314d 152
Vkadaba 5:0728bde67bdb 153 rc = pCtx->_spi->write((char*)(pTxData), pTxData ? nLength : 0,
Vkadaba 32:52445bef314d 154 (char*)(pRxData), pRxData ? nLength : 0);
Vkadaba 32:52445bef314d 155
ADIJake 0:85855ecd3257 156 if ((rc < 0) || !bCsHold)
Vkadaba 5:0728bde67bdb 157 *(pCtx->_cs) = 1;
Vkadaba 48:5731f1aa2c5a 158
Vkadaba 48:5731f1aa2c5a 159 *(pCtx->_wakeup) = 0;
Vkadaba 48:5731f1aa2c5a 160
Vkadaba 32:52445bef314d 161 if (rc < 0)
Vkadaba 32:52445bef314d 162 {
Vkadaba 5:0728bde67bdb 163 ADMW_LOG_ERROR("Failed to complete SPI transfer");
Vkadaba 5:0728bde67bdb 164 return ADMW_FAILURE;
ADIJake 0:85855ecd3257 165 }
Vkadaba 32:52445bef314d 166
Vkadaba 5:0728bde67bdb 167 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 168 }
ADIJake 0:85855ecd3257 169
ADIJake 0:85855ecd3257 170 /*
ADIJake 0:85855ecd3257 171 * Close the SPI interface and free resources
ADIJake 0:85855ecd3257 172 */
Vkadaba 5:0728bde67bdb 173 void admw_SpiClose(
Vkadaba 5:0728bde67bdb 174 ADMW_SPI_HANDLE hDevice)
ADIJake 0:85855ecd3257 175 {
Vkadaba 5:0728bde67bdb 176 SpiContext_t *pCtx = (SpiContext_t *)hDevice;
ADIJake 0:85855ecd3257 177
Vkadaba 5:0728bde67bdb 178 delete pCtx->_spi;
Vkadaba 5:0728bde67bdb 179 delete pCtx->_cs;
Vkadaba 5:0728bde67bdb 180 delete pCtx->_wakeup;
Vkadaba 32:52445bef314d 181
Vkadaba 5:0728bde67bdb 182 free(pCtx);
ADIJake 0:85855ecd3257 183 }
ADIJake 0:85855ecd3257 184
ADIJake 0:85855ecd3257 185 #ifdef __cplusplus
ADIJake 0:85855ecd3257 186 }
ADIJake 0:85855ecd3257 187 #endif
ADIJake 0:85855ecd3257 188
ADIJake 0:85855ecd3257 189 /*!
ADIJake 0:85855ecd3257 190 * @}
Vkadaba 48:5731f1aa2c5a 191 */