Generic mbed Extensions used by STM Expansion Board Firmware Packages.

Dependents:   X_NUCLEO_IKS01A1 X_NUCLEO_6180XA1 1-DoorCloser 1-DoorCloser ... more

Fork of X_NUCLEO_COMMON by ST Expansion SW Team

Generic mbed Extensions used by STM Expansion Board Firmware Packages

DbgMCU

Helper class DbgMCU providing a default constructor which enables debugging on STM32 MCUs while using sleep modes.

DevI2C

Helper class DevI2C providing functions for multi-register I2C communication common for a series of I2C devices.

DevSPI

Helper class DevSPI providing functions for SPI communication common for a series of SPI devices.

Committer:
Wolfgang Betz
Date:
Fri Feb 19 09:33:04 2016 +0100
Revision:
15:a59f32439f9d
Parent:
14:7e8db11e12c7
Child:
16:216930edb6b7
Child:
17:99d103ca04dd
Correct 16-bit support

Who changed what in which revision?

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