![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Minor changes to support ADMW FWv1.17.75
src/mbed/admw_spi.cpp@62:a5b88f433406, 2020-04-08 (annotated)
- Committer:
- Vkadaba
- Date:
- Wed Apr 08 10:05:08 2020 +0000
- Revision:
- 62:a5b88f433406
- Parent:
- 60:363fffe5bd22
Minor changes to support FW v1.17.75
Who changed what in which revision?
User | Revision | Line number | New 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 | 60:363fffe5bd22 | 111 | wait_us(31); |
Vkadaba | 13:97cb32670539 | 112 | *(pCtx->_cs) = 0; |
Vkadaba | 48:5731f1aa2c5a | 113 | |
Vkadaba | 13:97cb32670539 | 114 | rc = pCtx->_spi->write((char*)(pTxData), pTxData ? nLength : 0, |
Vkadaba | 32:52445bef314d | 115 | (char*)(pRxData), pRxData ? nLength : 0); |
Vkadaba | 32:52445bef314d | 116 | |
Vkadaba | 13:97cb32670539 | 117 | if ((rc < 0) || !bCsHold) |
Vkadaba | 62:a5b88f433406 | 118 | { |
Vkadaba | 62:a5b88f433406 | 119 | *(pCtx->_cs) = 1; |
Vkadaba | 62:a5b88f433406 | 120 | } |
Vkadaba | 62:a5b88f433406 | 121 | *(pCtx->_wakeup) = 0; |
Vkadaba | 48:5731f1aa2c5a | 122 | |
Vkadaba | 32:52445bef314d | 123 | if (rc < 0) |
Vkadaba | 32:52445bef314d | 124 | { |
Vkadaba | 13:97cb32670539 | 125 | ADMW_LOG_ERROR("Failed to complete SPI transfer"); |
Vkadaba | 13:97cb32670539 | 126 | return ADMW_FAILURE; |
Vkadaba | 13:97cb32670539 | 127 | } |
Vkadaba | 32:52445bef314d | 128 | |
Vkadaba | 13:97cb32670539 | 129 | pCtx->_spi->format(ADMW_SPI_FRAME_SIZE, ADMW_SPI_MODE); |
Vkadaba | 32:52445bef314d | 130 | |
Vkadaba | 32:52445bef314d | 131 | return ADMW_SUCCESS; |
Vkadaba | 32:52445bef314d | 132 | } |
Vkadaba | 23:bb685f35b08b | 133 | |
Vkadaba | 32:52445bef314d | 134 | /* |
Vkadaba | 32:52445bef314d | 135 | * Execute a bi-directional data transfer on the SPI interface |
Vkadaba | 32:52445bef314d | 136 | */ |
Vkadaba | 5:0728bde67bdb | 137 | ADMW_RESULT |
Vkadaba | 5:0728bde67bdb | 138 | admw_SpiTransfer( |
Vkadaba | 5:0728bde67bdb | 139 | ADMW_SPI_HANDLE hDevice, |
ADIJake | 0:85855ecd3257 | 140 | void *pTxData, |
ADIJake | 0:85855ecd3257 | 141 | void *pRxData, |
ADIJake | 0:85855ecd3257 | 142 | unsigned nLength, |
ADIJake | 0:85855ecd3257 | 143 | bool bCsHold) |
ADIJake | 0:85855ecd3257 | 144 | { |
ADIJake | 0:85855ecd3257 | 145 | int rc = 0; |
Vkadaba | 32:52445bef314d | 146 | |
Vkadaba | 5:0728bde67bdb | 147 | SpiContext_t *pCtx = (SpiContext_t*)hDevice; |
Vkadaba | 48:5731f1aa2c5a | 148 | |
Vkadaba | 48:5731f1aa2c5a | 149 | *(pCtx->_wakeup) = 1; |
Vkadaba | 60:363fffe5bd22 | 150 | wait_us(31); |
Vkadaba | 5:0728bde67bdb | 151 | *(pCtx->_cs) = 0; |
Vkadaba | 60:363fffe5bd22 | 152 | *(pCtx->_wakeup) = 0; |
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 | 60:363fffe5bd22 | 158 | |
Vkadaba | 32:52445bef314d | 159 | if (rc < 0) |
Vkadaba | 32:52445bef314d | 160 | { |
Vkadaba | 5:0728bde67bdb | 161 | ADMW_LOG_ERROR("Failed to complete SPI transfer"); |
Vkadaba | 5:0728bde67bdb | 162 | return ADMW_FAILURE; |
ADIJake | 0:85855ecd3257 | 163 | } |
Vkadaba | 32:52445bef314d | 164 | |
Vkadaba | 5:0728bde67bdb | 165 | return ADMW_SUCCESS; |
ADIJake | 0:85855ecd3257 | 166 | } |
ADIJake | 0:85855ecd3257 | 167 | |
ADIJake | 0:85855ecd3257 | 168 | /* |
ADIJake | 0:85855ecd3257 | 169 | * Close the SPI interface and free resources |
ADIJake | 0:85855ecd3257 | 170 | */ |
Vkadaba | 5:0728bde67bdb | 171 | void admw_SpiClose( |
Vkadaba | 5:0728bde67bdb | 172 | ADMW_SPI_HANDLE hDevice) |
ADIJake | 0:85855ecd3257 | 173 | { |
Vkadaba | 5:0728bde67bdb | 174 | SpiContext_t *pCtx = (SpiContext_t *)hDevice; |
ADIJake | 0:85855ecd3257 | 175 | |
Vkadaba | 5:0728bde67bdb | 176 | delete pCtx->_spi; |
Vkadaba | 5:0728bde67bdb | 177 | delete pCtx->_cs; |
Vkadaba | 5:0728bde67bdb | 178 | delete pCtx->_wakeup; |
Vkadaba | 32:52445bef314d | 179 | |
Vkadaba | 5:0728bde67bdb | 180 | free(pCtx); |
ADIJake | 0:85855ecd3257 | 181 | } |
ADIJake | 0:85855ecd3257 | 182 | |
ADIJake | 0:85855ecd3257 | 183 | #ifdef __cplusplus |
ADIJake | 0:85855ecd3257 | 184 | } |
ADIJake | 0:85855ecd3257 | 185 | #endif |
ADIJake | 0:85855ecd3257 | 186 | |
ADIJake | 0:85855ecd3257 | 187 | /*! |
ADIJake | 0:85855ecd3257 | 188 | * @} |
Vkadaba | 60:363fffe5bd22 | 189 | */ |