Including SPI 3-wires class
Fork of X_NUCLEO_COMMON_SPI3W by
DevSPI/DevSPI.h@11:165bd6bc00ea, 2016-02-11 (annotated)
- Committer:
- Davidroid
- Date:
- Thu Feb 11 12:40:33 2016 +0000
- Revision:
- 11:165bd6bc00ea
- Parent:
- 10:da29b5403ca1
- Child:
- 13:9a2c9b0bbe61
+ Added support for SPI at 16 bit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davide aliprandi <> | 9:1f35e8bf427b | 1 | /** |
davide aliprandi <> | 9:1f35e8bf427b | 2 | ****************************************************************************** |
davide aliprandi <> | 9:1f35e8bf427b | 3 | * @file DevSPI.h |
davide aliprandi <> | 9:1f35e8bf427b | 4 | * @author AST / Software Platforms and Cloud |
Davidroid | 11:165bd6bc00ea | 5 | * @version V1.0.1 |
Davidroid | 11:165bd6bc00ea | 6 | * @date February 11th, 2016 |
davide aliprandi <> | 9:1f35e8bf427b | 7 | * @brief Header file for a special SPI class DevSPI which provides some |
davide aliprandi <> | 9:1f35e8bf427b | 8 | * helper function for on-board communication. |
davide aliprandi <> | 9:1f35e8bf427b | 9 | ****************************************************************************** |
davide aliprandi <> | 9:1f35e8bf427b | 10 | * @attention |
davide aliprandi <> | 9:1f35e8bf427b | 11 | * |
davide aliprandi <> | 9:1f35e8bf427b | 12 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
davide aliprandi <> | 9:1f35e8bf427b | 13 | * |
davide aliprandi <> | 9:1f35e8bf427b | 14 | * Redistribution and use in source and binary forms, with or without modification, |
davide aliprandi <> | 9:1f35e8bf427b | 15 | * are permitted provided that the following conditions are met: |
davide aliprandi <> | 9:1f35e8bf427b | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
davide aliprandi <> | 9:1f35e8bf427b | 17 | * this list of conditions and the following disclaimer. |
davide aliprandi <> | 9:1f35e8bf427b | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
davide aliprandi <> | 9:1f35e8bf427b | 19 | * this list of conditions and the following disclaimer in the documentation |
davide aliprandi <> | 9:1f35e8bf427b | 20 | * and/or other materials provided with the distribution. |
davide aliprandi <> | 9:1f35e8bf427b | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
davide aliprandi <> | 9:1f35e8bf427b | 22 | * may be used to endorse or promote products derived from this software |
davide aliprandi <> | 9:1f35e8bf427b | 23 | * without specific prior written permission. |
davide aliprandi <> | 9:1f35e8bf427b | 24 | * |
davide aliprandi <> | 9:1f35e8bf427b | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
davide aliprandi <> | 9:1f35e8bf427b | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
davide aliprandi <> | 9:1f35e8bf427b | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
davide aliprandi <> | 9:1f35e8bf427b | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
davide aliprandi <> | 9:1f35e8bf427b | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
davide aliprandi <> | 9:1f35e8bf427b | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
davide aliprandi <> | 9:1f35e8bf427b | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
davide aliprandi <> | 9:1f35e8bf427b | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
davide aliprandi <> | 9:1f35e8bf427b | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
davide aliprandi <> | 9:1f35e8bf427b | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
davide aliprandi <> | 9:1f35e8bf427b | 35 | * |
davide aliprandi <> | 9:1f35e8bf427b | 36 | ****************************************************************************** |
davide aliprandi <> | 9:1f35e8bf427b | 37 | */ |
davide aliprandi <> | 9:1f35e8bf427b | 38 | |
davide aliprandi <> | 9:1f35e8bf427b | 39 | /* Define to prevent from recursive inclusion --------------------------------*/ |
davide aliprandi <> | 9:1f35e8bf427b | 40 | #ifndef __DEV_SPI_H |
davide aliprandi <> | 9:1f35e8bf427b | 41 | #define __DEV_SPI_H |
davide aliprandi <> | 9:1f35e8bf427b | 42 | |
davide aliprandi <> | 9:1f35e8bf427b | 43 | /* Includes ------------------------------------------------------------------*/ |
davide aliprandi <> | 9:1f35e8bf427b | 44 | #include "mbed.h" |
davide aliprandi <> | 9:1f35e8bf427b | 45 | |
davide aliprandi <> | 9:1f35e8bf427b | 46 | /* Classes -------------------------------------------------------------------*/ |
davide.aliprandi@st.com | 10:da29b5403ca1 | 47 | /** Helper class DevSPI providing functions for SPI communication common for a |
davide.aliprandi@st.com | 10:da29b5403ca1 | 48 | * series of SPI devices. |
davide aliprandi <> | 9:1f35e8bf427b | 49 | */ |
davide aliprandi <> | 9:1f35e8bf427b | 50 | class DevSPI : public SPI |
davide aliprandi <> | 9:1f35e8bf427b | 51 | { |
davide aliprandi <> | 9:1f35e8bf427b | 52 | public: |
davide aliprandi <> | 9:1f35e8bf427b | 53 | /* |
davide aliprandi <> | 9:1f35e8bf427b | 54 | * Create a DevSPI interface. |
davide aliprandi <> | 9:1f35e8bf427b | 55 | * @param mosi pin name of the MOSI pin of the SPI device to be used for communication. |
davide aliprandi <> | 9:1f35e8bf427b | 56 | * @param miso pin name of the MISO pin of the SPI device to be used for communication. |
davide aliprandi <> | 9:1f35e8bf427b | 57 | * @param sclk pin name of the SCLK pin of the SPI device to be used for communication. |
davide aliprandi <> | 9:1f35e8bf427b | 58 | */ |
davide aliprandi <> | 9:1f35e8bf427b | 59 | DevSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk) |
davide aliprandi <> | 9:1f35e8bf427b | 60 | { |
Davidroid | 11:165bd6bc00ea | 61 | /* Set default configuration. */ |
Davidroid | 11:165bd6bc00ea | 62 | setup(8, 3, 1E6); |
davide aliprandi <> | 9:1f35e8bf427b | 63 | } |
davide aliprandi <> | 9:1f35e8bf427b | 64 | |
davide aliprandi <> | 9:1f35e8bf427b | 65 | /* |
davide aliprandi <> | 9:1f35e8bf427b | 66 | * Setup the spi. |
davide aliprandi <> | 9:1f35e8bf427b | 67 | * Typically: |
davide aliprandi <> | 9:1f35e8bf427b | 68 | * + 8 bit data; |
davide aliprandi <> | 9:1f35e8bf427b | 69 | * + high steady state clock; |
davide aliprandi <> | 9:1f35e8bf427b | 70 | * + second edge capture; |
davide aliprandi <> | 9:1f35e8bf427b | 71 | * + 1MHz clock rate. |
davide aliprandi <> | 9:1f35e8bf427b | 72 | * |
davide aliprandi <> | 9:1f35e8bf427b | 73 | * @param bits Number of bits per SPI frame (4 - 16) |
davide aliprandi <> | 9:1f35e8bf427b | 74 | * @param mode Clock polarity and phase mode (0 - 3) |
davide aliprandi <> | 9:1f35e8bf427b | 75 | * @param frequency_hz SCLK frequency in hz (default = 1MHz) |
davide aliprandi <> | 9:1f35e8bf427b | 76 | * |
davide aliprandi <> | 9:1f35e8bf427b | 77 | * @code |
davide aliprandi <> | 9:1f35e8bf427b | 78 | * mode | POL PHA |
davide aliprandi <> | 9:1f35e8bf427b | 79 | * -----+-------- |
davide aliprandi <> | 9:1f35e8bf427b | 80 | * 0 | 0 0 |
davide aliprandi <> | 9:1f35e8bf427b | 81 | * 1 | 0 1 |
davide aliprandi <> | 9:1f35e8bf427b | 82 | * 2 | 1 0 |
davide aliprandi <> | 9:1f35e8bf427b | 83 | * 3 | 1 1 |
davide aliprandi <> | 9:1f35e8bf427b | 84 | * @endcode |
davide aliprandi <> | 9:1f35e8bf427b | 85 | */ |
davide aliprandi <> | 9:1f35e8bf427b | 86 | void setup(int bits, int mode = 0, int frequency_hz = 1E6) |
davide aliprandi <> | 9:1f35e8bf427b | 87 | { |
Davidroid | 11:165bd6bc00ea | 88 | /* Set given configuration. */ |
davide aliprandi <> | 9:1f35e8bf427b | 89 | format(bits, mode); |
davide aliprandi <> | 9:1f35e8bf427b | 90 | frequency(frequency_hz); |
davide aliprandi <> | 9:1f35e8bf427b | 91 | } |
davide aliprandi <> | 9:1f35e8bf427b | 92 | |
davide aliprandi <> | 9:1f35e8bf427b | 93 | /** |
davide aliprandi <> | 9:1f35e8bf427b | 94 | * @brief Writes a buffer to the SPI peripheral device. |
davide aliprandi <> | 9:1f35e8bf427b | 95 | * @param[in] pBuffer pointer to the buffer of data to send. |
davide aliprandi <> | 9:1f35e8bf427b | 96 | * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication. |
davide aliprandi <> | 9:1f35e8bf427b | 97 | * @param[in] NumBytesToWrite number of bytes to write. |
davide aliprandi <> | 9:1f35e8bf427b | 98 | * @retval 0 if ok. |
davide aliprandi <> | 9:1f35e8bf427b | 99 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
davide aliprandi <> | 9:1f35e8bf427b | 100 | * before calling this function and to enable them again after. |
davide aliprandi <> | 9:1f35e8bf427b | 101 | */ |
davide aliprandi <> | 9:1f35e8bf427b | 102 | int spi_write(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToWrite) |
davide aliprandi <> | 9:1f35e8bf427b | 103 | { |
davide aliprandi <> | 9:1f35e8bf427b | 104 | /* Select the chip. */ |
davide aliprandi <> | 9:1f35e8bf427b | 105 | ssel = 0; |
davide aliprandi <> | 9:1f35e8bf427b | 106 | |
davide aliprandi <> | 9:1f35e8bf427b | 107 | /* Write data. */ |
Davidroid | 11:165bd6bc00ea | 108 | if (_bits == 16) |
Davidroid | 11:165bd6bc00ea | 109 | for (int i = 0; i < NumBytesToWrite; i += 2) |
Davidroid | 11:165bd6bc00ea | 110 | write(((uint16_t *) pBuffer)[i]); |
Davidroid | 11:165bd6bc00ea | 111 | else if(_bits == 8) |
Davidroid | 11:165bd6bc00ea | 112 | for (int i = 0; i < NumBytesToWrite; i++) |
Davidroid | 11:165bd6bc00ea | 113 | write(pBuffer[i]); |
davide aliprandi <> | 9:1f35e8bf427b | 114 | |
davide aliprandi <> | 9:1f35e8bf427b | 115 | /* Unselect the chip. */ |
davide aliprandi <> | 9:1f35e8bf427b | 116 | ssel = 1; |
davide aliprandi <> | 9:1f35e8bf427b | 117 | |
davide aliprandi <> | 9:1f35e8bf427b | 118 | return 0; |
davide aliprandi <> | 9:1f35e8bf427b | 119 | } |
davide aliprandi <> | 9:1f35e8bf427b | 120 | |
davide aliprandi <> | 9:1f35e8bf427b | 121 | /** |
davide aliprandi <> | 9:1f35e8bf427b | 122 | * @brief Reads a buffer from the SPI peripheral device. |
davide aliprandi <> | 9:1f35e8bf427b | 123 | * @param[out] pBuffer pointer to the buffer to read data into. |
davide aliprandi <> | 9:1f35e8bf427b | 124 | * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication. |
davide aliprandi <> | 9:1f35e8bf427b | 125 | * @param[in] NumBytesToRead number of bytes to read. |
davide aliprandi <> | 9:1f35e8bf427b | 126 | * @retval 0 if ok. |
davide aliprandi <> | 9:1f35e8bf427b | 127 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
davide aliprandi <> | 9:1f35e8bf427b | 128 | * before calling this function and to enable them again after. |
davide aliprandi <> | 9:1f35e8bf427b | 129 | */ |
davide aliprandi <> | 9:1f35e8bf427b | 130 | int spi_read(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToRead) |
davide aliprandi <> | 9:1f35e8bf427b | 131 | { |
davide aliprandi <> | 9:1f35e8bf427b | 132 | /* Select the chip. */ |
davide aliprandi <> | 9:1f35e8bf427b | 133 | ssel = 0; |
davide aliprandi <> | 9:1f35e8bf427b | 134 | |
davide aliprandi <> | 9:1f35e8bf427b | 135 | /* Read data. */ |
Davidroid | 11:165bd6bc00ea | 136 | if (_bits == 16) |
Davidroid | 11:165bd6bc00ea | 137 | for (int i = 0; i < NumBytesToRead; i += 2) |
Davidroid | 11:165bd6bc00ea | 138 | ((uint16_t *) pBuffer)[i] = write(0x00); |
Davidroid | 11:165bd6bc00ea | 139 | else if(_bits == 8) |
Davidroid | 11:165bd6bc00ea | 140 | for (int i = 0; i < NumBytesToRead; i++) |
Davidroid | 11:165bd6bc00ea | 141 | pBuffer[i] = write(0x00); |
davide aliprandi <> | 9:1f35e8bf427b | 142 | |
davide aliprandi <> | 9:1f35e8bf427b | 143 | /* Unselect the chip. */ |
davide aliprandi <> | 9:1f35e8bf427b | 144 | ssel = 1; |
davide aliprandi <> | 9:1f35e8bf427b | 145 | |
davide aliprandi <> | 9:1f35e8bf427b | 146 | return 0; |
davide aliprandi <> | 9:1f35e8bf427b | 147 | } |
davide aliprandi <> | 9:1f35e8bf427b | 148 | |
davide aliprandi <> | 9:1f35e8bf427b | 149 | /** |
davide aliprandi <> | 9:1f35e8bf427b | 150 | * @brief Reads and write a buffer from/to the SPI peripheral device at the same time. |
davide aliprandi <> | 9:1f35e8bf427b | 151 | * @param[out] pBufferToRead pointer to the buffer to read data into. |
davide aliprandi <> | 9:1f35e8bf427b | 152 | * @param[in] pBufferToWrite pointer to the buffer of data to send. |
davide aliprandi <> | 9:1f35e8bf427b | 153 | * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication. |
davide aliprandi <> | 9:1f35e8bf427b | 154 | * @param[in] NumBytes number of bytes to read and write. |
davide aliprandi <> | 9:1f35e8bf427b | 155 | * @retval 0 if ok. |
davide aliprandi <> | 9:1f35e8bf427b | 156 | * @note When using the SPI in Interrupt-mode, remember to disable interrupts |
davide aliprandi <> | 9:1f35e8bf427b | 157 | * before calling this function and to enable them again after. |
davide aliprandi <> | 9:1f35e8bf427b | 158 | */ |
davide aliprandi <> | 9:1f35e8bf427b | 159 | int spi_read_write(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, DigitalOut ssel, uint16_t NumBytes) |
davide aliprandi <> | 9:1f35e8bf427b | 160 | { |
davide aliprandi <> | 9:1f35e8bf427b | 161 | /* Select the chip. */ |
davide aliprandi <> | 9:1f35e8bf427b | 162 | ssel = 0; |
davide aliprandi <> | 9:1f35e8bf427b | 163 | |
davide aliprandi <> | 9:1f35e8bf427b | 164 | /* Read and write data at the same time. */ |
Davidroid | 11:165bd6bc00ea | 165 | if (_bits == 16) |
Davidroid | 11:165bd6bc00ea | 166 | for (int i = 0; i < NumBytes; i += 2) |
Davidroid | 11:165bd6bc00ea | 167 | ((uint16_t *) pBufferToRead)[i] = write(((uint16_t *) pBufferToWrite)[i]); |
Davidroid | 11:165bd6bc00ea | 168 | else if(_bits == 8) |
Davidroid | 11:165bd6bc00ea | 169 | for (int i = 0; i < NumBytes; i++) |
Davidroid | 11:165bd6bc00ea | 170 | pBufferToRead[i] = write(pBufferToWrite[i]); |
davide aliprandi <> | 9:1f35e8bf427b | 171 | |
davide aliprandi <> | 9:1f35e8bf427b | 172 | /* Unselect the chip. */ |
davide aliprandi <> | 9:1f35e8bf427b | 173 | ssel = 1; |
davide aliprandi <> | 9:1f35e8bf427b | 174 | |
davide aliprandi <> | 9:1f35e8bf427b | 175 | return 0; |
davide aliprandi <> | 9:1f35e8bf427b | 176 | } |
davide aliprandi <> | 9:1f35e8bf427b | 177 | }; |
davide aliprandi <> | 9:1f35e8bf427b | 178 | |
davide aliprandi <> | 9:1f35e8bf427b | 179 | #endif /* __DEV_SPI_H */ |