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:21:54 2016 +0100
Revision:
13:9a2c9b0bbe61
Parent:
12:fb6321e37154
Parent:
11:165bd6bc00ea
Child:
16:216930edb6b7
Merge branch 'master' of hg::http://wobetz@developer.mbed.org/teams/ST/code/X_NUCLEO_COMMON into common-split

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 13:9a2c9b0bbe61 5 * @version V1.0.1
Wolfgang Betz 13:9a2c9b0bbe61 6 * @date February 11th, 2016
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 13:9a2c9b0bbe61 61 /* Set default configuration. */
Wolfgang Betz 13:9a2c9b0bbe61 62 setup(8, 3, 1E6);
Wolfgang Betz 12:fb6321e37154 63 }
Wolfgang Betz 12:fb6321e37154 64
Wolfgang Betz 12:fb6321e37154 65 /*
Wolfgang Betz 12:fb6321e37154 66 * Setup the spi.
Wolfgang Betz 12:fb6321e37154 67 * Typically:
Wolfgang Betz 12:fb6321e37154 68 * + 8 bit data;
Wolfgang Betz 12:fb6321e37154 69 * + high steady state clock;
Wolfgang Betz 12:fb6321e37154 70 * + second edge capture;
Wolfgang Betz 12:fb6321e37154 71 * + 1MHz clock rate.
Wolfgang Betz 12:fb6321e37154 72 *
Wolfgang Betz 12:fb6321e37154 73 * @param bits Number of bits per SPI frame (4 - 16)
Wolfgang Betz 12:fb6321e37154 74 * @param mode Clock polarity and phase mode (0 - 3)
Wolfgang Betz 12:fb6321e37154 75 * @param frequency_hz SCLK frequency in hz (default = 1MHz)
Wolfgang Betz 12:fb6321e37154 76 *
Wolfgang Betz 12:fb6321e37154 77 * @code
Wolfgang Betz 12:fb6321e37154 78 * mode | POL PHA
Wolfgang Betz 12:fb6321e37154 79 * -----+--------
Wolfgang Betz 12:fb6321e37154 80 * 0 | 0 0
Wolfgang Betz 12:fb6321e37154 81 * 1 | 0 1
Wolfgang Betz 12:fb6321e37154 82 * 2 | 1 0
Wolfgang Betz 12:fb6321e37154 83 * 3 | 1 1
Wolfgang Betz 12:fb6321e37154 84 * @endcode
Wolfgang Betz 12:fb6321e37154 85 */
Wolfgang Betz 12:fb6321e37154 86 void setup(int bits, int mode = 0, int frequency_hz = 1E6)
Wolfgang Betz 12:fb6321e37154 87 {
Wolfgang Betz 13:9a2c9b0bbe61 88 /* Set given configuration. */
Wolfgang Betz 12:fb6321e37154 89 format(bits, mode);
Wolfgang Betz 12:fb6321e37154 90 frequency(frequency_hz);
Wolfgang Betz 12:fb6321e37154 91 }
Wolfgang Betz 12:fb6321e37154 92
Wolfgang Betz 12:fb6321e37154 93 /**
Wolfgang Betz 12:fb6321e37154 94 * @brief Writes a buffer to the SPI peripheral device.
Wolfgang Betz 12:fb6321e37154 95 * @param[in] pBuffer pointer to the buffer of data to send.
Wolfgang Betz 12:fb6321e37154 96 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
Wolfgang Betz 12:fb6321e37154 97 * @param[in] NumBytesToWrite number of bytes to write.
Wolfgang Betz 12:fb6321e37154 98 * @retval 0 if ok.
Wolfgang Betz 12:fb6321e37154 99 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
Wolfgang Betz 12:fb6321e37154 100 * before calling this function and to enable them again after.
Wolfgang Betz 12:fb6321e37154 101 */
Wolfgang Betz 12:fb6321e37154 102 int spi_write(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToWrite)
Wolfgang Betz 12:fb6321e37154 103 {
Wolfgang Betz 12:fb6321e37154 104 /* Select the chip. */
Wolfgang Betz 12:fb6321e37154 105 ssel = 0;
Wolfgang Betz 12:fb6321e37154 106
Wolfgang Betz 12:fb6321e37154 107 /* Write data. */
Wolfgang Betz 13:9a2c9b0bbe61 108 if (_bits == 16)
Wolfgang Betz 13:9a2c9b0bbe61 109 for (int i = 0; i < NumBytesToWrite; i += 2)
Wolfgang Betz 13:9a2c9b0bbe61 110 write(((uint16_t *) pBuffer)[i]);
Wolfgang Betz 13:9a2c9b0bbe61 111 else if(_bits == 8)
Wolfgang Betz 13:9a2c9b0bbe61 112 for (int i = 0; i < NumBytesToWrite; i++)
Wolfgang Betz 13:9a2c9b0bbe61 113 write(pBuffer[i]);
Wolfgang Betz 12:fb6321e37154 114
Wolfgang Betz 12:fb6321e37154 115 /* Unselect the chip. */
Wolfgang Betz 12:fb6321e37154 116 ssel = 1;
Wolfgang Betz 12:fb6321e37154 117
Wolfgang Betz 12:fb6321e37154 118 return 0;
Wolfgang Betz 12:fb6321e37154 119 }
Wolfgang Betz 12:fb6321e37154 120
Wolfgang Betz 12:fb6321e37154 121 /**
Wolfgang Betz 12:fb6321e37154 122 * @brief Reads a buffer from the SPI peripheral device.
Wolfgang Betz 12:fb6321e37154 123 * @param[out] pBuffer pointer to the buffer to read data into.
Wolfgang Betz 12:fb6321e37154 124 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
Wolfgang Betz 12:fb6321e37154 125 * @param[in] NumBytesToRead number of bytes to read.
Wolfgang Betz 12:fb6321e37154 126 * @retval 0 if ok.
Wolfgang Betz 12:fb6321e37154 127 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
Wolfgang Betz 12:fb6321e37154 128 * before calling this function and to enable them again after.
Wolfgang Betz 12:fb6321e37154 129 */
Wolfgang Betz 12:fb6321e37154 130 int spi_read(uint8_t* pBuffer, DigitalOut ssel, uint16_t NumBytesToRead)
Wolfgang Betz 12:fb6321e37154 131 {
Wolfgang Betz 12:fb6321e37154 132 /* Select the chip. */
Wolfgang Betz 12:fb6321e37154 133 ssel = 0;
Wolfgang Betz 12:fb6321e37154 134
Wolfgang Betz 12:fb6321e37154 135 /* Read data. */
Wolfgang Betz 13:9a2c9b0bbe61 136 if (_bits == 16)
Wolfgang Betz 13:9a2c9b0bbe61 137 for (int i = 0; i < NumBytesToRead; i += 2)
Wolfgang Betz 13:9a2c9b0bbe61 138 ((uint16_t *) pBuffer)[i] = write(0x00);
Wolfgang Betz 13:9a2c9b0bbe61 139 else if(_bits == 8)
Wolfgang Betz 13:9a2c9b0bbe61 140 for (int i = 0; i < NumBytesToRead; i++)
Wolfgang Betz 13:9a2c9b0bbe61 141 pBuffer[i] = write(0x00);
Wolfgang Betz 12:fb6321e37154 142
Wolfgang Betz 12:fb6321e37154 143 /* Unselect the chip. */
Wolfgang Betz 12:fb6321e37154 144 ssel = 1;
Wolfgang Betz 12:fb6321e37154 145
Wolfgang Betz 12:fb6321e37154 146 return 0;
Wolfgang Betz 12:fb6321e37154 147 }
Wolfgang Betz 12:fb6321e37154 148
Wolfgang Betz 12:fb6321e37154 149 /**
Wolfgang Betz 12:fb6321e37154 150 * @brief Reads and write a buffer from/to the SPI peripheral device at the same time.
Wolfgang Betz 12:fb6321e37154 151 * @param[out] pBufferToRead pointer to the buffer to read data into.
Wolfgang Betz 12:fb6321e37154 152 * @param[in] pBufferToWrite pointer to the buffer of data to send.
Wolfgang Betz 12:fb6321e37154 153 * @param[in] ssel GPIO of the SSEL pin of the SPI device to be used for communication.
Wolfgang Betz 12:fb6321e37154 154 * @param[in] NumBytes number of bytes to read and write.
Wolfgang Betz 12:fb6321e37154 155 * @retval 0 if ok.
Wolfgang Betz 12:fb6321e37154 156 * @note When using the SPI in Interrupt-mode, remember to disable interrupts
Wolfgang Betz 12:fb6321e37154 157 * before calling this function and to enable them again after.
Wolfgang Betz 12:fb6321e37154 158 */
Wolfgang Betz 12:fb6321e37154 159 int spi_read_write(uint8_t* pBufferToRead, uint8_t* pBufferToWrite, DigitalOut ssel, uint16_t NumBytes)
Wolfgang Betz 12:fb6321e37154 160 {
Wolfgang Betz 12:fb6321e37154 161 /* Select the chip. */
Wolfgang Betz 12:fb6321e37154 162 ssel = 0;
Wolfgang Betz 12:fb6321e37154 163
Wolfgang Betz 12:fb6321e37154 164 /* Read and write data at the same time. */
Wolfgang Betz 13:9a2c9b0bbe61 165 if (_bits == 16)
Wolfgang Betz 13:9a2c9b0bbe61 166 for (int i = 0; i < NumBytes; i += 2)
Wolfgang Betz 13:9a2c9b0bbe61 167 ((uint16_t *) pBufferToRead)[i] = write(((uint16_t *) pBufferToWrite)[i]);
Wolfgang Betz 13:9a2c9b0bbe61 168 else if(_bits == 8)
Wolfgang Betz 13:9a2c9b0bbe61 169 for (int i = 0; i < NumBytes; i++)
Wolfgang Betz 13:9a2c9b0bbe61 170 pBufferToRead[i] = write(pBufferToWrite[i]);
Wolfgang Betz 12:fb6321e37154 171
Wolfgang Betz 12:fb6321e37154 172 /* Unselect the chip. */
Wolfgang Betz 12:fb6321e37154 173 ssel = 1;
Wolfgang Betz 12:fb6321e37154 174
Wolfgang Betz 12:fb6321e37154 175 return 0;
Wolfgang Betz 12:fb6321e37154 176 }
Wolfgang Betz 12:fb6321e37154 177 };
Wolfgang Betz 12:fb6321e37154 178
Wolfgang Betz 12:fb6321e37154 179 #endif /* __DEV_SPI_H */