Including SPI 3-wires class
Fork of X_NUCLEO_COMMON_SPI3W by
DevSPI3W/SPI3W.cpp@25:e4f6488865d1, 2017-08-31 (annotated)
- Committer:
- mapellil
- Date:
- Thu Aug 31 11:24:35 2017 +0200
- Revision:
- 25:e4f6488865d1
- Parent:
- 24:95fd537e9725
Modifications to support mbed-os pull req https://github.com/ARMmbed/mbed-os/pull/4975
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mapellil | 22:62b662a23496 | 1 | /** |
mapellil | 22:62b662a23496 | 2 | ****************************************************************************** |
mapellil | 22:62b662a23496 | 3 | * @file SPI3W.cpp |
mapellil | 22:62b662a23496 | 4 | * @author CLab |
mapellil | 22:62b662a23496 | 5 | * @version V1.0.0 |
mapellil | 22:62b662a23496 | 6 | * @date 5 August 2016 |
mapellil | 22:62b662a23496 | 7 | * @brief Implementation of an SPI 3 wires driver. |
mapellil | 22:62b662a23496 | 8 | ****************************************************************************** |
mapellil | 22:62b662a23496 | 9 | * @attention |
mapellil | 22:62b662a23496 | 10 | * |
mapellil | 22:62b662a23496 | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
mapellil | 22:62b662a23496 | 12 | * |
mapellil | 22:62b662a23496 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
mapellil | 22:62b662a23496 | 14 | * are permitted provided that the following conditions are met: |
mapellil | 22:62b662a23496 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
mapellil | 22:62b662a23496 | 16 | * this list of conditions and the following disclaimer. |
mapellil | 22:62b662a23496 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mapellil | 22:62b662a23496 | 18 | * this list of conditions and the following disclaimer in the documentation |
mapellil | 22:62b662a23496 | 19 | * and/or other materials provided with the distribution. |
mapellil | 22:62b662a23496 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mapellil | 22:62b662a23496 | 21 | * may be used to endorse or promote products derived from this software |
mapellil | 22:62b662a23496 | 22 | * without specific prior written permission. |
mapellil | 22:62b662a23496 | 23 | * |
mapellil | 22:62b662a23496 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mapellil | 22:62b662a23496 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mapellil | 22:62b662a23496 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mapellil | 22:62b662a23496 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mapellil | 22:62b662a23496 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mapellil | 22:62b662a23496 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mapellil | 22:62b662a23496 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mapellil | 22:62b662a23496 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mapellil | 22:62b662a23496 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mapellil | 22:62b662a23496 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mapellil | 22:62b662a23496 | 34 | * |
mapellil | 22:62b662a23496 | 35 | ****************************************************************************** |
mapellil | 22:62b662a23496 | 36 | */ |
mapellil | 22:62b662a23496 | 37 | |
mapellil | 22:62b662a23496 | 38 | #include "mbed.h" |
mapellil | 22:62b662a23496 | 39 | #include "SPI3W.h" |
mapellil | 22:62b662a23496 | 40 | //#include "SensorTile.h" |
mapellil | 22:62b662a23496 | 41 | |
mapellil | 22:62b662a23496 | 42 | |
mapellil | 22:62b662a23496 | 43 | /* Class Implementation ------------------------------------------------------*/ |
mapellil | 22:62b662a23496 | 44 | |
mapellil | 22:62b662a23496 | 45 | /** Constructor |
mapellil | 22:62b662a23496 | 46 | * @param spi3w object of an helper class which handles the spi peripheral |
mapellil | 22:62b662a23496 | 47 | * @param |
mapellil | 22:62b662a23496 | 48 | */ |
mapellil | 22:62b662a23496 | 49 | |
mapellil | 22:62b662a23496 | 50 | SPI3W::SPI3W (PinName spi_sda, PinName spi_sdi, PinName spi_clk) : SPI(spi_sda, spi_sdi, spi_clk, NC) |
mapellil | 22:62b662a23496 | 51 | { |
mapellil | 22:62b662a23496 | 52 | } |
mapellil | 22:62b662a23496 | 53 | |
mapellil | 22:62b662a23496 | 54 | |
mapellil | 22:62b662a23496 | 55 | uint8_t SPI3W::Sensor_IO_SPI_CS_Enable(DigitalOut * _cs_pin) |
mapellil | 22:62b662a23496 | 56 | { |
mapellil | 22:62b662a23496 | 57 | *_cs_pin = 0; |
mapellil | 22:62b662a23496 | 58 | return 0; |
mapellil | 22:62b662a23496 | 59 | } |
mapellil | 22:62b662a23496 | 60 | |
mapellil | 22:62b662a23496 | 61 | uint8_t SPI3W::Sensor_IO_SPI_CS_Disable(DigitalOut * _cs_pin) |
mapellil | 22:62b662a23496 | 62 | { |
mapellil | 22:62b662a23496 | 63 | *_cs_pin = 1; |
mapellil | 22:62b662a23496 | 64 | return 0; |
mapellil | 22:62b662a23496 | 65 | } |
mapellil | 22:62b662a23496 | 66 | |
mapellil | 22:62b662a23496 | 67 | |
mapellil | 22:62b662a23496 | 68 | /** |
mapellil | 22:62b662a23496 | 69 | * @brief Writes a buffer to the sensor |
mapellil | 22:62b662a23496 | 70 | * @param handle instance handle |
mapellil | 22:62b662a23496 | 71 | * @param WriteAddr specifies the internal sensor address register to be written to |
mapellil | 22:62b662a23496 | 72 | * @param pBuffer pointer to data buffer |
mapellil | 22:62b662a23496 | 73 | * @param nBytesToWrite number of bytes to be written |
mapellil | 22:62b662a23496 | 74 | * @retval 0 in case of success |
mapellil | 22:62b662a23496 | 75 | * @retval 1 in case of failure |
mapellil | 22:62b662a23496 | 76 | */ |
mapellil | 22:62b662a23496 | 77 | uint8_t SPI3W::Sensor_IO_SPI_Write( DigitalOut * _cs_pin, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) |
mapellil | 25:e4f6488865d1 | 78 | { |
mapellil | 22:62b662a23496 | 79 | // Select the correct device |
mapellil | 22:62b662a23496 | 80 | Sensor_IO_SPI_CS_Enable(_cs_pin); |
mapellil | 22:62b662a23496 | 81 | |
mapellil | 25:e4f6488865d1 | 82 | write(WriteAddr); |
mapellil | 25:e4f6488865d1 | 83 | write((char *)pBuffer, (int) nBytesToWrite, NULL, 0); |
mapellil | 25:e4f6488865d1 | 84 | |
mapellil | 22:62b662a23496 | 85 | // Deselect the device |
mapellil | 22:62b662a23496 | 86 | Sensor_IO_SPI_CS_Disable(_cs_pin); |
mapellil | 22:62b662a23496 | 87 | |
mapellil | 22:62b662a23496 | 88 | return 0; |
mapellil | 22:62b662a23496 | 89 | } |
mapellil | 22:62b662a23496 | 90 | |
mapellil | 22:62b662a23496 | 91 | /** |
mapellil | 22:62b662a23496 | 92 | * @brief Writes a buffer to the sensor |
mapellil | 22:62b662a23496 | 93 | * @param handle instance handle |
mapellil | 22:62b662a23496 | 94 | * @param WriteAddr specifies the internal sensor address register to be written to |
mapellil | 22:62b662a23496 | 95 | * @param pBuffer pointer to data buffer |
mapellil | 22:62b662a23496 | 96 | * @param nBytesToWrite number of bytes to be written |
mapellil | 22:62b662a23496 | 97 | * @retval 0 in case of success |
mapellil | 22:62b662a23496 | 98 | * @retval 1 in case of failure |
mapellil | 22:62b662a23496 | 99 | */ |
mapellil | 22:62b662a23496 | 100 | uint8_t SPI3W::Sensor_IO_Write( DigitalOut * _cs_pin, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) |
mapellil | 22:62b662a23496 | 101 | { |
mapellil | 22:62b662a23496 | 102 | return Sensor_IO_SPI_Write( _cs_pin, WriteAddr, pBuffer, nBytesToWrite ); |
mapellil | 22:62b662a23496 | 103 | } |
mapellil | 22:62b662a23496 | 104 | |
mapellil | 22:62b662a23496 | 105 | /** |
mapellil | 22:62b662a23496 | 106 | * @brief Reads a from the sensor to buffer |
mapellil | 22:62b662a23496 | 107 | * @param handle instance handle |
mapellil | 22:62b662a23496 | 108 | * @param ReadAddr specifies the internal sensor address register to be read from |
mapellil | 22:62b662a23496 | 109 | * @param pBuffer pointer to data buffer |
mapellil | 22:62b662a23496 | 110 | * @param nBytesToRead number of bytes to be read |
mapellil | 22:62b662a23496 | 111 | * @retval 0 in case of success |
mapellil | 22:62b662a23496 | 112 | * @retval 1 in case of failure |
mapellil | 22:62b662a23496 | 113 | */ |
mapellil | 22:62b662a23496 | 114 | uint8_t SPI3W::Sensor_IO_SPI_Read( DigitalOut * _cs_pin, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) |
mapellil | 23:a42a4217ff73 | 115 | { |
mapellil | 22:62b662a23496 | 116 | /* Select the correct device */ |
mapellil | 22:62b662a23496 | 117 | Sensor_IO_SPI_CS_Enable(_cs_pin); |
mapellil | 23:a42a4217ff73 | 118 | |
mapellil | 25:e4f6488865d1 | 119 | /* Write RD Reg Address with RD bit*/ |
mapellil | 25:e4f6488865d1 | 120 | uint8_t TxByte = ReadAddr | 0x80; |
mapellil | 25:e4f6488865d1 | 121 | write((char *)&TxByte, 1, (char *)pBuffer, (int) nBytesToRead); |
mapellil | 22:62b662a23496 | 122 | |
mapellil | 22:62b662a23496 | 123 | /* Deselect the device */ |
mapellil | 22:62b662a23496 | 124 | Sensor_IO_SPI_CS_Disable(_cs_pin); |
mapellil | 22:62b662a23496 | 125 | |
mapellil | 22:62b662a23496 | 126 | return 0; |
mapellil | 22:62b662a23496 | 127 | } |
mapellil | 22:62b662a23496 | 128 | |
mapellil | 22:62b662a23496 | 129 | |
mapellil | 22:62b662a23496 | 130 | /** |
mapellil | 22:62b662a23496 | 131 | * @brief Reads from the sensor to a buffer |
mapellil | 22:62b662a23496 | 132 | * @param handle instance handle |
mapellil | 22:62b662a23496 | 133 | * @param ReadAddr specifies the internal sensor address register to be read from |
mapellil | 22:62b662a23496 | 134 | * @param pBuffer pointer to data buffer |
mapellil | 22:62b662a23496 | 135 | * @param nBytesToRead number of bytes to be read |
mapellil | 22:62b662a23496 | 136 | * @retval 0 in case of success |
mapellil | 22:62b662a23496 | 137 | * @retval 1 in case of failure |
mapellil | 22:62b662a23496 | 138 | */ |
mapellil | 22:62b662a23496 | 139 | uint8_t SPI3W::Sensor_IO_Read( DigitalOut * _cs_pin, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) |
mapellil | 22:62b662a23496 | 140 | { |
mapellil | 22:62b662a23496 | 141 | return Sensor_IO_SPI_Read( _cs_pin, ReadAddr, pBuffer, nBytesToRead ); |
mapellil | 22:62b662a23496 | 142 | } |
mapellil | 22:62b662a23496 | 143 | |
mapellil | 22:62b662a23496 | 144 | |
mapellil | 22:62b662a23496 | 145 | uint8_t SPI3W::spi3w_read(uint8_t * pBuffer, DigitalOut * _cs_pin, uint8_t RegisterAddr, uint16_t NumByteToRead) |
mapellil | 22:62b662a23496 | 146 | { |
mapellil | 22:62b662a23496 | 147 | return Sensor_IO_Read( _cs_pin, RegisterAddr, pBuffer, NumByteToRead ); |
mapellil | 22:62b662a23496 | 148 | } |
mapellil | 22:62b662a23496 | 149 | |
mapellil | 22:62b662a23496 | 150 | uint8_t SPI3W::spi3w_write(uint8_t * pBuffer, DigitalOut * _cs_pin, uint8_t RegisterAddr, uint16_t NumByteToWrite) |
mapellil | 22:62b662a23496 | 151 | { |
mapellil | 22:62b662a23496 | 152 | return Sensor_IO_Write( _cs_pin, RegisterAddr, pBuffer, NumByteToWrite ); |
mapellil | 22:62b662a23496 | 153 | } |
mapellil | 22:62b662a23496 | 154 |