Example of 6D orientation recognition for LSM6DSL in X-NUCLEO-IKS01A2
Dependencies: X_NUCLEO_IKS01A2 mbed
Fork of 6DOrientation_IKS01A2 by
6D Orientation Demo Application based on sensor expansion board X-NUCLEO-IKS01A2
Main function is to show how to use sensor expansion board to find out the 6D orientation and send data using UART to a connected PC or Desktop and display it on terminal applications like TeraTerm.
After connection has been established:
- the user can rotate the board to change the 6D orientation and then view the data using an hyper terminal.
- the user button can be used to display the current 6D orientation.
X_NUCLEO_IKS01A2/X_NUCLEO_COMMON/DevSPI/DevSPI.h@0:485458fca2bd, 2016-08-12 (annotated)
- Committer:
- cparata
- Date:
- Fri Aug 12 13:37:49 2016 +0000
- Revision:
- 0:485458fca2bd
First release of 6D orientation for LSM6DSL in IKS01A2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| cparata | 0:485458fca2bd | 1 | /** |
| cparata | 0:485458fca2bd | 2 | ****************************************************************************** |
| cparata | 0:485458fca2bd | 3 | * @file DevSPI.h |
| cparata | 0:485458fca2bd | 4 | * @author AST / Software Platforms and Cloud / EST |
| cparata | 0:485458fca2bd | 5 | * @version V1.2.1 |
| cparata | 0:485458fca2bd | 6 | * @date 19-February-2016 |
| cparata | 0:485458fca2bd | 7 | * @brief Header file for a special SPI class DevSPI which provides some |
| cparata | 0:485458fca2bd | 8 | * helper functions for on-board communication. |
| cparata | 0:485458fca2bd | 9 | ****************************************************************************** |
| cparata | 0:485458fca2bd | 10 | * @attention |
| cparata | 0:485458fca2bd | 11 | * |
| cparata | 0:485458fca2bd | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
| cparata | 0:485458fca2bd | 13 | * |
| cparata | 0:485458fca2bd | 14 | * Redistribution and use in source and binary forms, with or without modification, |
| cparata | 0:485458fca2bd | 15 | * are permitted provided that the following conditions are met: |
| cparata | 0:485458fca2bd | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
| cparata | 0:485458fca2bd | 17 | * this list of conditions and the following disclaimer. |
| cparata | 0:485458fca2bd | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| cparata | 0:485458fca2bd | 19 | * this list of conditions and the following disclaimer in the documentation |
| cparata | 0:485458fca2bd | 20 | * and/or other materials provided with the distribution. |
| cparata | 0:485458fca2bd | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| cparata | 0:485458fca2bd | 22 | * may be used to endorse or promote products derived from this software |
| cparata | 0:485458fca2bd | 23 | * without specific prior written permission. |
| cparata | 0:485458fca2bd | 24 | * |
| cparata | 0:485458fca2bd | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| cparata | 0:485458fca2bd | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| cparata | 0:485458fca2bd | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| cparata | 0:485458fca2bd | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| cparata | 0:485458fca2bd | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| cparata | 0:485458fca2bd | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| cparata | 0:485458fca2bd | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| cparata | 0:485458fca2bd | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| cparata | 0:485458fca2bd | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| cparata | 0:485458fca2bd | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| cparata | 0:485458fca2bd | 35 | * |
| cparata | 0:485458fca2bd | 36 | ****************************************************************************** |
| cparata | 0:485458fca2bd | 37 | */ |
| cparata | 0:485458fca2bd | 38 | |
| cparata | 0:485458fca2bd | 39 | /* Define to prevent from recursive inclusion --------------------------------*/ |
| cparata | 0:485458fca2bd | 40 | #ifndef __DEV_SPI_H |
| cparata | 0:485458fca2bd | 41 | #define __DEV_SPI_H |
| cparata | 0:485458fca2bd | 42 | |
| cparata | 0:485458fca2bd | 43 | /* Includes ------------------------------------------------------------------*/ |
| cparata | 0:485458fca2bd | 44 | #include "mbed.h" |
| cparata | 0:485458fca2bd | 45 | |
| cparata | 0:485458fca2bd | 46 | /* Macros --------------------------------------------------------------------*/ |
| cparata | 0:485458fca2bd | 47 | #if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) /* GCC */ || \ |
| cparata | 0:485458fca2bd | 48 | (defined(G_BYTE_ORDER) && (G_BYTE_ORDER == G_BIG_ENDIAN)) /* IAR */ || \ |
| cparata | 0:485458fca2bd | 49 | (defined(__BIG_ENDIAN)) /* ARM */ |
| cparata | 0:485458fca2bd | 50 | #define __DEV_SPI_BIG_ENDIAN |
| cparata | 0:485458fca2bd | 51 | #endif |
| cparata | 0:485458fca2bd | 52 | |
| cparata | 0:485458fca2bd | 53 | /* Classes -------------------------------------------------------------------*/ |
| cparata | 0:485458fca2bd | 54 | /** Helper class DevSPI providing functions for synchronous SPI communication |
| cparata | 0:485458fca2bd | 55 | * common for a series of SPI devices. |
| cparata | 0:485458fca2bd | 56 | */ |
| cparata | 0:485458fca2bd | 57 | class DevSPI : public SPI |
| cparata | 0:485458fca2bd | 58 | { |
| cparata | 0:485458fca2bd | 59 | public: |
| cparata | 0:485458fca2bd | 60 | /* |
| cparata | 0:485458fca2bd | 61 | * Create a DevSPI interface. |
| cparata | 0:485458fca2bd | 62 | * @param mosi pin name of the MOSI pin of the SPI device to be used for communication. |
| cparata | 0:485458fca2bd | 63 | * @param miso pin name of the MISO pin of the SPI device to be used for communication. |
| cparata | 0:485458fca2bd | 64 | * @param sclk pin name of the SCLK pin of the SPI device to be used for communication. |
| cparata | 0:485458fca2bd | 65 | */ |
| cparata | 0:485458fca2bd | 66 | DevSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) |
| cparata | 0:485458fca2bd | 67 | { |
| cparata | 0:485458fca2bd | 68 | /* Set default configuration. */ |
| cparata | 0:485458fca2bd | 69 | setup(8, 3, 1E6); |
| cparata | 0:485458fca2bd | 70 | } |
| cparata | 0:485458fca2bd | 71 | |
| cparata | 0:485458fca2bd | 72 | /* |
| cparata | 0:485458fca2bd | 73 | * Setup the spi. |
| cparata | 0:485458fca2bd | 74 | * Typically: |
| cparata | 0:485458fca2bd | 75 | * + 8 bit data; |
| cparata | 0:485458fca2bd | 76 | * + high steady state clock; |
| cparata | 0:485458fca2bd | 77 | * + second edge capture; |
| cparata | 0:485458fca2bd | 78 | * + 1MHz clock rate. |
| cparata | 0:485458fca2bd | 79 | * |
| cparata | 0:485458fca2bd | 80 | * @param bits Number of bits per SPI frame (4 - 16) |
| cparata | 0:485458fca2bd | 81 | * @param mode Clock polarity and phase mode (0 - 3) |
| cparata | 0:485458fca2bd | 82 | * @param frequency_hz SCLK frequency in hz (default = 1MHz) |
| cparata | 0:485458fca2bd | 83 | * |
| cparata | 0:485458fca2bd | 84 | * @code |
| cparata | 0:485458fca2bd | 85 | * mode | POL PHA |
| cparata | 0:485458fca2bd | 86 | * -----+-------- |
| cparata | 0:485458fca2bd | 87 | * 0 | 0 0 |
| cparata | 0:485458fca2bd | 88 | * 1 | 0 1 |
| cparata | 0:485458fca2bd | 89 | * 2 | 1 0 |
| cparata | 0:485458fca2bd | 90 | * 3 | 1 1 |
| cparata | 0:485458fca2bd | 91 | * @endcode |
| cparata | 0:485458fca2bd | 92 | */ |
| cparata | 0:485458fca2bd | 93 | void setup(int bits, int mode = 0, int frequency_hz = 1E6) |
| cparata | 0:485458fca2bd | 94 | { |
| cparata | 0:485458fca2bd | 95 | /* Set given configuration. */ |
| cparata | 0:485458fca2bd | 96 | format(bits, mode); |
| cparata | 0:485458fca2bd | 97 | frequency(frequency_hz); |
| cparata | 0:485458fca2bd | 98 | } |
| cparata | 0:485458fca2bd | 99 | |
| cparata | 0:485458fca2bd | 100 | /** |
| cparata | 0:485458fca2bd | 101 | * @brief Writes a buffer to the SPI peripheral device in 8-bit data mode |
| cparata | 0:485458fca2bd | 102 | * using synchronous SPI communication. |
| cparata | 0:485458fca2bd | 103 | * @param[in] pBuffer pointer to the buffer of data to send. |
| cparata | 0:485458fca2bd | 104 | * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication. |
| cparata | 0:485458fca2bd | 105 | * @param[in] NumBytesToWrite number of bytes to write. |
| cparata | 0:485458fca2bd | 106 | * @retval 0 if ok. |
| cparata | 0:485458fca2bd | 107 | * @retval -1 if data format error. |
| cparata | 0:485458fca2bd | 108 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
| cparata | 0:485458fca2bd | 109 | * before calling this function and to enable them again after. |
| cparata | 0:485458fca2bd | 110 | */ |
| cparata | 0:485458fca2bd | 111 | int spi_write(uint8_t* pBuffer, DigitalOut &ssel, uint16_t NumBytesToWrite) |
| cparata | 0:485458fca2bd | 112 | { |
| cparata | 0:485458fca2bd | 113 | /* Check data format */ |
| cparata | 0:485458fca2bd | 114 | if(_bits != 8) return -1; |
| cparata | 0:485458fca2bd | 115 | |
| cparata | 0:485458fca2bd | 116 | /* Select the chip. */ |
| cparata | 0:485458fca2bd | 117 | ssel = 0; |
| cparata | 0:485458fca2bd | 118 | |
| cparata | 0:485458fca2bd | 119 | /* Write data. */ |
| cparata | 0:485458fca2bd | 120 | for (int i = 0; i < NumBytesToWrite; i++) { |
| cparata | 0:485458fca2bd | 121 | write(pBuffer[i]); |
| cparata | 0:485458fca2bd | 122 | } |
| cparata | 0:485458fca2bd | 123 | |
| cparata | 0:485458fca2bd | 124 | /* Unselect the chip. */ |
| cparata | 0:485458fca2bd | 125 | ssel = 1; |
| cparata | 0:485458fca2bd | 126 | |
| cparata | 0:485458fca2bd | 127 | return 0; |
| cparata | 0:485458fca2bd | 128 | } |
| cparata | 0:485458fca2bd | 129 | |
| cparata | 0:485458fca2bd | 130 | /** |
| cparata | 0:485458fca2bd | 131 | * @brief Reads a buffer from the SPI peripheral device in 8-bit data mode |
| cparata | 0:485458fca2bd | 132 | * using synchronous SPI communication. |
| cparata | 0:485458fca2bd | 133 | * @param[out] pBuffer pointer to the buffer to read data into. |
| cparata | 0:485458fca2bd | 134 | * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication. |
| cparata | 0:485458fca2bd | 135 | * @param[in] NumBytesToRead number of bytes to read. |
| cparata | 0:485458fca2bd | 136 | * @retval 0 if ok. |
| cparata | 0:485458fca2bd | 137 | * @retval -1 if data format error. |
| cparata | 0:485458fca2bd | 138 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
| cparata | 0:485458fca2bd | 139 | * before calling this function and to enable them again after. |
| cparata | 0:485458fca2bd | 140 | */ |
| cparata | 0:485458fca2bd | 141 | int spi_read(uint8_t* pBuffer, DigitalOut &ssel, uint16_t NumBytesToRead) |
| cparata | 0:485458fca2bd | 142 | { |
| cparata | 0:485458fca2bd | 143 | /* Check data format */ |
| cparata | 0:485458fca2bd | 144 | if(_bits != 8) return -1; |
| cparata | 0:485458fca2bd | 145 | |
| cparata | 0:485458fca2bd | 146 | /* Select the chip. */ |
| cparata | 0:485458fca2bd | 147 | ssel = 0; |
| cparata | 0:485458fca2bd | 148 | |
| cparata | 0:485458fca2bd | 149 | /* Read data. */ |
| cparata | 0:485458fca2bd | 150 | for (int i = 0; i < NumBytesToRead; i++) { |
| cparata | 0:485458fca2bd | 151 | pBuffer[i] = write(0); |
| cparata | 0:485458fca2bd | 152 | } |
| cparata | 0:485458fca2bd | 153 | |
| cparata | 0:485458fca2bd | 154 | /* Unselect the chip. */ |
| cparata | 0:485458fca2bd | 155 | ssel = 1; |
| cparata | 0:485458fca2bd | 156 | |
| cparata | 0:485458fca2bd | 157 | return 0; |
| cparata | 0:485458fca2bd | 158 | } |
| cparata | 0:485458fca2bd | 159 | |
| cparata | 0:485458fca2bd | 160 | /** |
| cparata | 0:485458fca2bd | 161 | * @brief Reads and write a buffer from/to the SPI peripheral device at the same time |
| cparata | 0:485458fca2bd | 162 | * in 8-bit data mode using synchronous SPI communication. |
| cparata | 0:485458fca2bd | 163 | * @param[out] pBufferToRead pointer to the buffer to read data into. |
| cparata | 0:485458fca2bd | 164 | * @param[in] pBufferToWrite pointer to the buffer of data to send. |
| cparata | 0:485458fca2bd | 165 | * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication. |
| cparata | 0:485458fca2bd | 166 | * @param[in] NumBytes number of bytes to read and write. |
| cparata | 0:485458fca2bd | 167 | * @retval 0 if ok. |
| cparata | 0:485458fca2bd | 168 | * @retval -1 if data format error. |
| cparata | 0:485458fca2bd | 169 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
| cparata | 0:485458fca2bd | 170 | * before calling this function and to enable them again after. |
| cparata | 0:485458fca2bd | 171 | */ |
| cparata | 0:485458fca2bd | 172 | int spi_read_write(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, DigitalOut &ssel, uint16_t NumBytes) |
| cparata | 0:485458fca2bd | 173 | { |
| cparata | 0:485458fca2bd | 174 | /* Check data format */ |
| cparata | 0:485458fca2bd | 175 | if(_bits != 8) return -1; |
| cparata | 0:485458fca2bd | 176 | |
| cparata | 0:485458fca2bd | 177 | /* Select the chip. */ |
| cparata | 0:485458fca2bd | 178 | ssel = 0; |
| cparata | 0:485458fca2bd | 179 | |
| cparata | 0:485458fca2bd | 180 | /* Read and write data at the same time. */ |
| cparata | 0:485458fca2bd | 181 | for (int i = 0; i < NumBytes; i++) { |
| cparata | 0:485458fca2bd | 182 | pBufferToRead[i] = write(pBufferToWrite[i]); |
| cparata | 0:485458fca2bd | 183 | } |
| cparata | 0:485458fca2bd | 184 | |
| cparata | 0:485458fca2bd | 185 | /* Unselect the chip. */ |
| cparata | 0:485458fca2bd | 186 | ssel = 1; |
| cparata | 0:485458fca2bd | 187 | |
| cparata | 0:485458fca2bd | 188 | return 0; |
| cparata | 0:485458fca2bd | 189 | } |
| cparata | 0:485458fca2bd | 190 | |
| cparata | 0:485458fca2bd | 191 | /** |
| cparata | 0:485458fca2bd | 192 | * @brief Writes a buffer to the SPI peripheral device in 16-bit data mode |
| cparata | 0:485458fca2bd | 193 | * using synchronous SPI communication. |
| cparata | 0:485458fca2bd | 194 | * @param[in] pBuffer pointer to the buffer of data to send. |
| cparata | 0:485458fca2bd | 195 | * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication. |
| cparata | 0:485458fca2bd | 196 | * @param[in] NumValuesToWrite number of 16-bit values to write. |
| cparata | 0:485458fca2bd | 197 | * @retval 0 if ok. |
| cparata | 0:485458fca2bd | 198 | * @retval -1 if data format error. |
| cparata | 0:485458fca2bd | 199 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
| cparata | 0:485458fca2bd | 200 | * before calling this function and to enable them again after. |
| cparata | 0:485458fca2bd | 201 | * @note In order to guarantee this method to work correctly you have to |
| cparata | 0:485458fca2bd | 202 | * pass buffers which are correctly aligned. |
| cparata | 0:485458fca2bd | 203 | */ |
| cparata | 0:485458fca2bd | 204 | int spi_write(uint16_t* pBuffer, DigitalOut &ssel, uint16_t NumValuesToWrite) |
| cparata | 0:485458fca2bd | 205 | { |
| cparata | 0:485458fca2bd | 206 | /* Check data format */ |
| cparata | 0:485458fca2bd | 207 | if(_bits != 16) return -1; |
| cparata | 0:485458fca2bd | 208 | |
| cparata | 0:485458fca2bd | 209 | /* Select the chip. */ |
| cparata | 0:485458fca2bd | 210 | ssel = 0; |
| cparata | 0:485458fca2bd | 211 | |
| cparata | 0:485458fca2bd | 212 | /* Write data. */ |
| cparata | 0:485458fca2bd | 213 | for (int i = 0; i < NumValuesToWrite; i++) { |
| cparata | 0:485458fca2bd | 214 | write(htons(pBuffer[i])); |
| cparata | 0:485458fca2bd | 215 | } |
| cparata | 0:485458fca2bd | 216 | |
| cparata | 0:485458fca2bd | 217 | /* Unselect the chip. */ |
| cparata | 0:485458fca2bd | 218 | ssel = 1; |
| cparata | 0:485458fca2bd | 219 | |
| cparata | 0:485458fca2bd | 220 | return 0; |
| cparata | 0:485458fca2bd | 221 | } |
| cparata | 0:485458fca2bd | 222 | |
| cparata | 0:485458fca2bd | 223 | /** |
| cparata | 0:485458fca2bd | 224 | * @brief Reads a buffer from the SPI peripheral device in 16-bit data mode |
| cparata | 0:485458fca2bd | 225 | * using synchronous SPI communication. |
| cparata | 0:485458fca2bd | 226 | * @param[out] pBuffer pointer to the buffer to read data into. |
| cparata | 0:485458fca2bd | 227 | * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication. |
| cparata | 0:485458fca2bd | 228 | * @param[in] NumValuesToRead number of 16-bit values to read. |
| cparata | 0:485458fca2bd | 229 | * @retval 0 if ok. |
| cparata | 0:485458fca2bd | 230 | * @retval -1 if data format error. |
| cparata | 0:485458fca2bd | 231 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
| cparata | 0:485458fca2bd | 232 | * before calling this function and to enable them again after. |
| cparata | 0:485458fca2bd | 233 | * @note In order to guarantee this method to work correctly you have to |
| cparata | 0:485458fca2bd | 234 | * pass buffers which are correctly aligned. |
| cparata | 0:485458fca2bd | 235 | */ |
| cparata | 0:485458fca2bd | 236 | int spi_read(uint16_t* pBuffer, DigitalOut &ssel, uint16_t NumValuesToRead) |
| cparata | 0:485458fca2bd | 237 | { |
| cparata | 0:485458fca2bd | 238 | /* Check data format */ |
| cparata | 0:485458fca2bd | 239 | if(_bits != 16) return -1; |
| cparata | 0:485458fca2bd | 240 | |
| cparata | 0:485458fca2bd | 241 | /* Select the chip. */ |
| cparata | 0:485458fca2bd | 242 | ssel = 0; |
| cparata | 0:485458fca2bd | 243 | |
| cparata | 0:485458fca2bd | 244 | /* Read data. */ |
| cparata | 0:485458fca2bd | 245 | for (int i = 0; i < NumValuesToRead; i++) { |
| cparata | 0:485458fca2bd | 246 | pBuffer[i] = ntohs((uint16_t)write(0)); |
| cparata | 0:485458fca2bd | 247 | } |
| cparata | 0:485458fca2bd | 248 | |
| cparata | 0:485458fca2bd | 249 | /* Unselect the chip. */ |
| cparata | 0:485458fca2bd | 250 | ssel = 1; |
| cparata | 0:485458fca2bd | 251 | |
| cparata | 0:485458fca2bd | 252 | return 0; |
| cparata | 0:485458fca2bd | 253 | } |
| cparata | 0:485458fca2bd | 254 | |
| cparata | 0:485458fca2bd | 255 | /** |
| cparata | 0:485458fca2bd | 256 | * @brief Reads and write a buffer from/to the SPI peripheral device at the same time |
| cparata | 0:485458fca2bd | 257 | * in 16-bit data mode using synchronous SPI communication. |
| cparata | 0:485458fca2bd | 258 | * @param[out] pBufferToRead pointer to the buffer to read data into. |
| cparata | 0:485458fca2bd | 259 | * @param[in] pBufferToWrite pointer to the buffer of data to send. |
| cparata | 0:485458fca2bd | 260 | * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication. |
| cparata | 0:485458fca2bd | 261 | * @param[in] NumValues number of 16-bit values to read and write. |
| cparata | 0:485458fca2bd | 262 | * @retval 0 if ok. |
| cparata | 0:485458fca2bd | 263 | * @retval -1 if data format error. |
| cparata | 0:485458fca2bd | 264 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
| cparata | 0:485458fca2bd | 265 | * before calling this function and to enable them again after. |
| cparata | 0:485458fca2bd | 266 | * @note In order to guarantee this method to work correctly you have to |
| cparata | 0:485458fca2bd | 267 | * pass buffers which are correctly aligned. |
| cparata | 0:485458fca2bd | 268 | */ |
| cparata | 0:485458fca2bd | 269 | int spi_read_write(uint16_t* pBufferToRead, uint16_t* pBufferToWrite, DigitalOut &ssel, uint16_t NumValues) |
| cparata | 0:485458fca2bd | 270 | { |
| cparata | 0:485458fca2bd | 271 | /* Check data format */ |
| cparata | 0:485458fca2bd | 272 | if(_bits != 16) return -1; |
| cparata | 0:485458fca2bd | 273 | |
| cparata | 0:485458fca2bd | 274 | /* Select the chip. */ |
| cparata | 0:485458fca2bd | 275 | ssel = 0; |
| cparata | 0:485458fca2bd | 276 | |
| cparata | 0:485458fca2bd | 277 | /* Read and write data at the same time. */ |
| cparata | 0:485458fca2bd | 278 | for (int i = 0; i < NumValues; i++) { |
| cparata | 0:485458fca2bd | 279 | pBufferToRead[i] = ntohs((uint16_t)write(htons(pBufferToWrite[i]))); |
| cparata | 0:485458fca2bd | 280 | } |
| cparata | 0:485458fca2bd | 281 | |
| cparata | 0:485458fca2bd | 282 | /* Unselect the chip. */ |
| cparata | 0:485458fca2bd | 283 | ssel = 1; |
| cparata | 0:485458fca2bd | 284 | |
| cparata | 0:485458fca2bd | 285 | return 0; |
| cparata | 0:485458fca2bd | 286 | } |
| cparata | 0:485458fca2bd | 287 | |
| cparata | 0:485458fca2bd | 288 | protected: |
| cparata | 0:485458fca2bd | 289 | inline uint16_t htons(uint16_t x) { |
| cparata | 0:485458fca2bd | 290 | #ifndef __DEV_SPI_BIG_ENDIAN |
| cparata | 0:485458fca2bd | 291 | return (((x)<<8)|((x)>>8)); |
| cparata | 0:485458fca2bd | 292 | #else // __DEV_SPI_BIG_ENDIAN |
| cparata | 0:485458fca2bd | 293 | return (x); |
| cparata | 0:485458fca2bd | 294 | #endif // __DEV_SPI_BIG_ENDIAN |
| cparata | 0:485458fca2bd | 295 | } |
| cparata | 0:485458fca2bd | 296 | |
| cparata | 0:485458fca2bd | 297 | inline uint16_t ntohs(uint16_t x) { |
| cparata | 0:485458fca2bd | 298 | return htons(x); |
| cparata | 0:485458fca2bd | 299 | } |
| cparata | 0:485458fca2bd | 300 | }; |
| cparata | 0:485458fca2bd | 301 | |
| cparata | 0:485458fca2bd | 302 | #endif /* __DEV_SPI_H */ |
