Including SPI 3-wires class

Fork of X_NUCLEO_COMMON_SPI3W by Licio Mapelli

Committer:
Wolfgang Betz
Date:
Mon Oct 19 10:24:47 2015 +0200
Revision:
12:fb6321e37154
Child:
13:9a2c9b0bbe61
Child:
14:7e8db11e12c7
Merge commit '918da5c9e4b8b146ee33c27656c0ff1c5bdb33a1' into betzw_wb

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 12:fb6321e37154 4 * @author AST / Software Platforms and Cloud
Wolfgang Betz 12:fb6321e37154 5 * @version V0.0.1
Wolfgang Betz 12:fb6321e37154 6 * @date September 24th, 2015
Wolfgang Betz 12:fb6321e37154 7 * @brief Header file for a special SPI class DevSPI which provides some
Wolfgang Betz 12:fb6321e37154 8 * helper function for on-board communication.
Wolfgang Betz 12:fb6321e37154 9 ******************************************************************************
Wolfgang Betz 12:fb6321e37154 10 * @attention
Wolfgang Betz 12:fb6321e37154 11 *
Wolfgang Betz 12:fb6321e37154 12 * <h2><center>&copy; COPYRIGHT(c) 2014 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 12:fb6321e37154 46 /* Classes -------------------------------------------------------------------*/
Wolfgang Betz 12:fb6321e37154 47 /** Helper class DevSPI providing functions for SPI communication common for a
Wolfgang Betz 12:fb6321e37154 48 * series of SPI devices.
Wolfgang Betz 12:fb6321e37154 49 */
Wolfgang Betz 12:fb6321e37154 50 class DevSPI : public SPI
Wolfgang Betz 12:fb6321e37154 51 {
Wolfgang Betz 12:fb6321e37154 52 public:
Wolfgang Betz 12:fb6321e37154 53 /*
Wolfgang Betz 12:fb6321e37154 54 * Create a DevSPI interface.
Wolfgang Betz 12:fb6321e37154 55 * @param mosi pin name of the MOSI pin of the SPI device to be used for communication.
Wolfgang Betz 12:fb6321e37154 56 * @param miso pin name of the MISO pin of the SPI device to be used for communication.
Wolfgang Betz 12:fb6321e37154 57 * @param sclk pin name of the SCLK pin of the SPI device to be used for communication.
Wolfgang Betz 12:fb6321e37154 58 */
Wolfgang Betz 12:fb6321e37154 59 DevSPI(PinName mosi, PinName miso, PinName sclk) : SPI(mosi, miso, sclk)
Wolfgang Betz 12:fb6321e37154 60 {
Wolfgang Betz 12:fb6321e37154 61 bits = 9;
Wolfgang Betz 12:fb6321e37154 62 mode = 0;
Wolfgang Betz 12:fb6321e37154 63 frequency_hz = 1E6;
Wolfgang Betz 12:fb6321e37154 64 setup_done = false;
Wolfgang Betz 12:fb6321e37154 65 }
Wolfgang Betz 12:fb6321e37154 66
Wolfgang Betz 12:fb6321e37154 67 /*
Wolfgang Betz 12:fb6321e37154 68 * Setup the spi.
Wolfgang Betz 12:fb6321e37154 69 * Typically:
Wolfgang Betz 12:fb6321e37154 70 * + 8 bit data;
Wolfgang Betz 12:fb6321e37154 71 * + high steady state clock;
Wolfgang Betz 12:fb6321e37154 72 * + second edge capture;
Wolfgang Betz 12:fb6321e37154 73 * + 1MHz clock rate.
Wolfgang Betz 12:fb6321e37154 74 *
Wolfgang Betz 12:fb6321e37154 75 * @param bits Number of bits per SPI frame (4 - 16)
Wolfgang Betz 12:fb6321e37154 76 * @param mode Clock polarity and phase mode (0 - 3)
Wolfgang Betz 12:fb6321e37154 77 * @param frequency_hz SCLK frequency in hz (default = 1MHz)
Wolfgang Betz 12:fb6321e37154 78 *
Wolfgang Betz 12:fb6321e37154 79 * @code
Wolfgang Betz 12:fb6321e37154 80 * mode | POL PHA
Wolfgang Betz 12:fb6321e37154 81 * -----+--------
Wolfgang Betz 12:fb6321e37154 82 * 0 | 0 0
Wolfgang Betz 12:fb6321e37154 83 * 1 | 0 1
Wolfgang Betz 12:fb6321e37154 84 * 2 | 1 0
Wolfgang Betz 12:fb6321e37154 85 * 3 | 1 1
Wolfgang Betz 12:fb6321e37154 86 * @endcode
Wolfgang Betz 12:fb6321e37154 87 */
Wolfgang Betz 12:fb6321e37154 88 void setup(int bits, int mode = 0, int frequency_hz = 1E6)
Wolfgang Betz 12:fb6321e37154 89 {
Wolfgang Betz 12:fb6321e37154 90 format(bits, mode);
Wolfgang Betz 12:fb6321e37154 91 frequency(frequency_hz);
Wolfgang Betz 12:fb6321e37154 92 setup_done = true;
Wolfgang Betz 12:fb6321e37154 93 }
Wolfgang Betz 12:fb6321e37154 94
Wolfgang Betz 12:fb6321e37154 95 /**
Wolfgang Betz 12:fb6321e37154 96 * @brief Writes a buffer to the SPI peripheral device.
Wolfgang Betz 12:fb6321e37154 97 * @param[in] pBuffer pointer to the buffer of data to send.
Wolfgang Betz 12:fb6321e37154 98 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
Wolfgang Betz 12:fb6321e37154 99 * @param[in] NumBytesToWrite number of bytes to write.
Wolfgang Betz 12:fb6321e37154 100 * @retval 0 if ok.
Wolfgang Betz 12:fb6321e37154 101 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
Wolfgang Betz 12:fb6321e37154 102 * before calling this function and to enable them again after.
Wolfgang Betz 12:fb6321e37154 103 */
Wolfgang Betz 12:fb6321e37154 104 int spi_write(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToWrite)
Wolfgang Betz 12:fb6321e37154 105 {
Wolfgang Betz 12:fb6321e37154 106 /* Setup. */
Wolfgang Betz 12:fb6321e37154 107 if (!setup_done)
Wolfgang Betz 12:fb6321e37154 108 {
Wolfgang Betz 12:fb6321e37154 109 ssel = 1;
Wolfgang Betz 12:fb6321e37154 110 setup(8, 3, 1E6);
Wolfgang Betz 12:fb6321e37154 111 setup_done = true;
Wolfgang Betz 12:fb6321e37154 112 }
Wolfgang Betz 12:fb6321e37154 113
Wolfgang Betz 12:fb6321e37154 114 /* Select the chip. */
Wolfgang Betz 12:fb6321e37154 115 ssel = 0;
Wolfgang Betz 12:fb6321e37154 116
Wolfgang Betz 12:fb6321e37154 117 /* Write data. */
Wolfgang Betz 12:fb6321e37154 118 for (int i = 0; i < NumBytesToWrite; i++)
Wolfgang Betz 12:fb6321e37154 119 write(pBuffer[i]);
Wolfgang Betz 12:fb6321e37154 120
Wolfgang Betz 12:fb6321e37154 121 /* Unselect the chip. */
Wolfgang Betz 12:fb6321e37154 122 ssel = 1;
Wolfgang Betz 12:fb6321e37154 123
Wolfgang Betz 12:fb6321e37154 124 return 0;
Wolfgang Betz 12:fb6321e37154 125 }
Wolfgang Betz 12:fb6321e37154 126
Wolfgang Betz 12:fb6321e37154 127 /**
Wolfgang Betz 12:fb6321e37154 128 * @brief Reads a buffer from the SPI peripheral device.
Wolfgang Betz 12:fb6321e37154 129 * @param[out] pBuffer pointer to the buffer to read data into.
Wolfgang Betz 12:fb6321e37154 130 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
Wolfgang Betz 12:fb6321e37154 131 * @param[in] NumBytesToRead number of bytes to read.
Wolfgang Betz 12:fb6321e37154 132 * @retval 0 if ok.
Wolfgang Betz 12:fb6321e37154 133 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
Wolfgang Betz 12:fb6321e37154 134 * before calling this function and to enable them again after.
Wolfgang Betz 12:fb6321e37154 135 */
Wolfgang Betz 12:fb6321e37154 136 int spi_read(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToRead)
Wolfgang Betz 12:fb6321e37154 137 {
Wolfgang Betz 12:fb6321e37154 138 /* Setup. */
Wolfgang Betz 12:fb6321e37154 139 if (!setup_done)
Wolfgang Betz 12:fb6321e37154 140 {
Wolfgang Betz 12:fb6321e37154 141 ssel = 1;
Wolfgang Betz 12:fb6321e37154 142 setup(8, 3, 1E6);
Wolfgang Betz 12:fb6321e37154 143 setup_done = true;
Wolfgang Betz 12:fb6321e37154 144 }
Wolfgang Betz 12:fb6321e37154 145
Wolfgang Betz 12:fb6321e37154 146 /* Select the chip. */
Wolfgang Betz 12:fb6321e37154 147 ssel = 0;
Wolfgang Betz 12:fb6321e37154 148
Wolfgang Betz 12:fb6321e37154 149 /* Read data. */
Wolfgang Betz 12:fb6321e37154 150 for (int i = 0; i < NumBytesToRead; i++)
Wolfgang Betz 12:fb6321e37154 151 pBuffer[i] = write(0x00);
Wolfgang Betz 12:fb6321e37154 152
Wolfgang Betz 12:fb6321e37154 153 /* Unselect the chip. */
Wolfgang Betz 12:fb6321e37154 154 ssel = 1;
Wolfgang Betz 12:fb6321e37154 155
Wolfgang Betz 12:fb6321e37154 156 return 0;
Wolfgang Betz 12:fb6321e37154 157 }
Wolfgang Betz 12:fb6321e37154 158
Wolfgang Betz 12:fb6321e37154 159 /**
Wolfgang Betz 12:fb6321e37154 160 * @brief Reads and write a buffer from/to the SPI peripheral device at the same time.
Wolfgang Betz 12:fb6321e37154 161 * @param[out] pBufferToRead pointer to the buffer to read data into.
Wolfgang Betz 12:fb6321e37154 162 * @param[in] pBufferToWrite pointer to the buffer of data to send.
Wolfgang Betz 12:fb6321e37154 163 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
Wolfgang Betz 12:fb6321e37154 164 * @param[in] NumBytes number of bytes to read and write.
Wolfgang Betz 12:fb6321e37154 165 * @retval 0 if ok.
Wolfgang Betz 12:fb6321e37154 166 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
Wolfgang Betz 12:fb6321e37154 167 * before calling this function and to enable them again after.
Wolfgang Betz 12:fb6321e37154 168 */
Wolfgang Betz 12:fb6321e37154 169 int spi_read_write(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, DigitalOut ssel, uint16_t NumBytes)
Wolfgang Betz 12:fb6321e37154 170 {
Wolfgang Betz 12:fb6321e37154 171 /* Setup. */
Wolfgang Betz 12:fb6321e37154 172 if (!setup_done)
Wolfgang Betz 12:fb6321e37154 173 {
Wolfgang Betz 12:fb6321e37154 174 ssel = 1;
Wolfgang Betz 12:fb6321e37154 175 setup(8, 3, 1E6);
Wolfgang Betz 12:fb6321e37154 176 setup_done = true;
Wolfgang Betz 12:fb6321e37154 177 }
Wolfgang Betz 12:fb6321e37154 178
Wolfgang Betz 12:fb6321e37154 179 /* Select the chip. */
Wolfgang Betz 12:fb6321e37154 180 ssel = 0;
Wolfgang Betz 12:fb6321e37154 181
Wolfgang Betz 12:fb6321e37154 182 /* Read and write data at the same time. */
Wolfgang Betz 12:fb6321e37154 183 for (int i = 0; i < NumBytes; i++)
Wolfgang Betz 12:fb6321e37154 184 pBufferToRead[i] = write(pBufferToWrite[i]);
Wolfgang Betz 12:fb6321e37154 185
Wolfgang Betz 12:fb6321e37154 186 /* Unselect the chip. */
Wolfgang Betz 12:fb6321e37154 187 ssel = 1;
Wolfgang Betz 12:fb6321e37154 188
Wolfgang Betz 12:fb6321e37154 189 return 0;
Wolfgang Betz 12:fb6321e37154 190 }
Wolfgang Betz 12:fb6321e37154 191
Wolfgang Betz 12:fb6321e37154 192 protected:
Wolfgang Betz 12:fb6321e37154 193 bool setup_done;
Wolfgang Betz 12:fb6321e37154 194 int bits;
Wolfgang Betz 12:fb6321e37154 195 int mode;
Wolfgang Betz 12:fb6321e37154 196 int frequency_hz;
Wolfgang Betz 12:fb6321e37154 197 };
Wolfgang Betz 12:fb6321e37154 198
Wolfgang Betz 12:fb6321e37154 199 #endif /* __DEV_SPI_H */