![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Minor changes to support ADMW FWv1.17.75
src/mbed/admw_spi.cpp@23:bb685f35b08b, 2019-09-26 (annotated)
- Committer:
- Vkadaba
- Date:
- Thu Sep 26 06:15:39 2019 +0000
- Revision:
- 23:bb685f35b08b
- Parent:
- 13:97cb32670539
- Child:
- 32:52445bef314d
Fixing code alignment
Who changed what in which revision?
User | Revision | Line number | New 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: |
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 | 23:bb685f35b08b | 80 | |
Vkadaba | 5:0728bde67bdb | 81 | pCtx->_spi = new SPI((PinName)pConfig->mosiPin, |
Vkadaba | 23:bb685f35b08b | 82 | (PinName)pConfig->misoPin, |
Vkadaba | 23:bb685f35b08b | 83 | (PinName)pConfig->sckPin); |
Vkadaba | 5:0728bde67bdb | 84 | pCtx->_cs = new DigitalOut((PinName)pConfig->csPin, 1); |
Vkadaba | 23:bb685f35b08b | 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 | 23:bb685f35b08b | 88 | |
Vkadaba | 5:0728bde67bdb | 89 | pCtx->_wakeup = new DigitalOut((PinName)pConfig->wakeupPin, 1); |
Vkadaba | 23:bb685f35b08b | 90 | |
Vkadaba | 5:0728bde67bdb | 91 | *phDevice = (ADMW_SPI_HANDLE)pCtx; |
Vkadaba | 23:bb685f35b08b | 92 | |
Vkadaba | 5:0728bde67bdb | 93 | return ADMW_SUCCESS; |
ADIJake | 0:85855ecd3257 | 94 | } |
ADIJake | 0:85855ecd3257 | 95 | |
Vkadaba | 5:0728bde67bdb | 96 | /* |
Vkadaba | 5:0728bde67bdb | 97 | * Execute a bi-directional data transfer on the SPI interface |
Vkadaba | 5:0728bde67bdb | 98 | */ |
Vkadaba | 23:bb685f35b08b | 99 | ADMW_RESULT |
Vkadaba | 13:97cb32670539 | 100 | admw_SpiReceive( |
Vkadaba | 13:97cb32670539 | 101 | ADMW_SPI_HANDLE hDevice, |
Vkadaba | 13:97cb32670539 | 102 | void *pTxData, |
Vkadaba | 13:97cb32670539 | 103 | void *pRxData, |
Vkadaba | 13:97cb32670539 | 104 | unsigned nLength, |
Vkadaba | 13:97cb32670539 | 105 | bool bCsHold) |
Vkadaba | 13:97cb32670539 | 106 | { |
Vkadaba | 23:bb685f35b08b | 107 | SpiContext_t *pCtx = (SpiContext_t*)hDevice; |
Vkadaba | 23:bb685f35b08b | 108 | |
Vkadaba | 23:bb685f35b08b | 109 | pCtx->_spi->format(ADMW_SPI_FRAME_SIZE, 1); |
Vkadaba | 23:bb685f35b08b | 110 | |
Vkadaba | 23:bb685f35b08b | 111 | int rc = 0; |
Vkadaba | 23:bb685f35b08b | 112 | |
Vkadaba | 13:97cb32670539 | 113 | *(pCtx->_wakeup) = 0; |
Vkadaba | 13:97cb32670539 | 114 | |
Vkadaba | 13:97cb32670539 | 115 | wait_us(60); |
Vkadaba | 13:97cb32670539 | 116 | |
Vkadaba | 13:97cb32670539 | 117 | *(pCtx->_cs) = 0; |
Vkadaba | 13:97cb32670539 | 118 | |
Vkadaba | 13:97cb32670539 | 119 | rc = pCtx->_spi->write((char*)(pTxData), pTxData ? nLength : 0, |
Vkadaba | 23:bb685f35b08b | 120 | (char*)(pRxData), pRxData ? nLength : 0); |
Vkadaba | 23:bb685f35b08b | 121 | |
Vkadaba | 13:97cb32670539 | 122 | if ((rc < 0) || !bCsHold) |
Vkadaba | 13:97cb32670539 | 123 | *(pCtx->_cs) = 1; |
Vkadaba | 13:97cb32670539 | 124 | |
Vkadaba | 13:97cb32670539 | 125 | *(pCtx->_wakeup) = 1; |
Vkadaba | 13:97cb32670539 | 126 | |
Vkadaba | 23:bb685f35b08b | 127 | if (rc < 0) { |
Vkadaba | 13:97cb32670539 | 128 | ADMW_LOG_ERROR("Failed to complete SPI transfer"); |
Vkadaba | 13:97cb32670539 | 129 | return ADMW_FAILURE; |
Vkadaba | 13:97cb32670539 | 130 | } |
Vkadaba | 13:97cb32670539 | 131 | |
Vkadaba | 13:97cb32670539 | 132 | pCtx->_spi->format(ADMW_SPI_FRAME_SIZE, ADMW_SPI_MODE); |
Vkadaba | 23:bb685f35b08b | 133 | |
Vkadaba | 13:97cb32670539 | 134 | return ADMW_SUCCESS; |
Vkadaba | 23:bb685f35b08b | 135 | |
Vkadaba | 13:97cb32670539 | 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 | 23:bb685f35b08b | 146 | |
Vkadaba | 5:0728bde67bdb | 147 | SpiContext_t *pCtx = (SpiContext_t*)hDevice; |
Vkadaba | 23:bb685f35b08b | 148 | |
Vkadaba | 5:0728bde67bdb | 149 | *(pCtx->_wakeup) = 0; |
ADIJake | 0:85855ecd3257 | 150 | |
Vkadaba | 5:0728bde67bdb | 151 | wait_us(60); |
ADIJake | 0:85855ecd3257 | 152 | |
Vkadaba | 5:0728bde67bdb | 153 | *(pCtx->_cs) = 0; |
Vkadaba | 5:0728bde67bdb | 154 | |
Vkadaba | 5:0728bde67bdb | 155 | rc = pCtx->_spi->write((char*)(pTxData), pTxData ? nLength : 0, |
Vkadaba | 23:bb685f35b08b | 156 | (char*)(pRxData), pRxData ? nLength : 0); |
Vkadaba | 23:bb685f35b08b | 157 | |
ADIJake | 0:85855ecd3257 | 158 | if ((rc < 0) || !bCsHold) |
Vkadaba | 5:0728bde67bdb | 159 | *(pCtx->_cs) = 1; |
ADIJake | 0:85855ecd3257 | 160 | |
Vkadaba | 5:0728bde67bdb | 161 | *(pCtx->_wakeup) = 1; |
ADIJake | 0:85855ecd3257 | 162 | |
Vkadaba | 23:bb685f35b08b | 163 | if (rc < 0) { |
Vkadaba | 5:0728bde67bdb | 164 | ADMW_LOG_ERROR("Failed to complete SPI transfer"); |
Vkadaba | 5:0728bde67bdb | 165 | return ADMW_FAILURE; |
ADIJake | 0:85855ecd3257 | 166 | } |
ADIJake | 0:85855ecd3257 | 167 | |
Vkadaba | 5:0728bde67bdb | 168 | return ADMW_SUCCESS; |
ADIJake | 0:85855ecd3257 | 169 | } |
ADIJake | 0:85855ecd3257 | 170 | |
ADIJake | 0:85855ecd3257 | 171 | /* |
ADIJake | 0:85855ecd3257 | 172 | * Close the SPI interface and free resources |
ADIJake | 0:85855ecd3257 | 173 | */ |
Vkadaba | 5:0728bde67bdb | 174 | void admw_SpiClose( |
Vkadaba | 5:0728bde67bdb | 175 | ADMW_SPI_HANDLE hDevice) |
ADIJake | 0:85855ecd3257 | 176 | { |
Vkadaba | 5:0728bde67bdb | 177 | SpiContext_t *pCtx = (SpiContext_t *)hDevice; |
ADIJake | 0:85855ecd3257 | 178 | |
Vkadaba | 5:0728bde67bdb | 179 | delete pCtx->_spi; |
Vkadaba | 5:0728bde67bdb | 180 | delete pCtx->_cs; |
Vkadaba | 5:0728bde67bdb | 181 | delete pCtx->_wakeup; |
Vkadaba | 23:bb685f35b08b | 182 | |
Vkadaba | 5:0728bde67bdb | 183 | free(pCtx); |
ADIJake | 0:85855ecd3257 | 184 | } |
ADIJake | 0:85855ecd3257 | 185 | |
ADIJake | 0:85855ecd3257 | 186 | #ifdef __cplusplus |
ADIJake | 0:85855ecd3257 | 187 | } |
ADIJake | 0:85855ecd3257 | 188 | #endif |
ADIJake | 0:85855ecd3257 | 189 | |
ADIJake | 0:85855ecd3257 | 190 | /*! |
ADIJake | 0:85855ecd3257 | 191 | * @} |
ADIJake | 0:85855ecd3257 | 192 | */ |