SPI Half-Duplex

Committer:
mbed_unsupported
Date:
Thu Dec 06 12:47:45 2012 +0000
Revision:
0:e9b41fc7d351
SPI Half-Duplex

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_unsupported 0:e9b41fc7d351 1 /* mbed Microcontroller Library
mbed_unsupported 0:e9b41fc7d351 2 * Copyright (c) 2006-2012 ARM Limited
mbed_unsupported 0:e9b41fc7d351 3 *
mbed_unsupported 0:e9b41fc7d351 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
mbed_unsupported 0:e9b41fc7d351 5 * of this software and associated documentation files (the "Software"), to deal
mbed_unsupported 0:e9b41fc7d351 6 * in the Software without restriction, including without limitation the rights
mbed_unsupported 0:e9b41fc7d351 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mbed_unsupported 0:e9b41fc7d351 8 * copies of the Software, and to permit persons to whom the Software is
mbed_unsupported 0:e9b41fc7d351 9 * furnished to do so, subject to the following conditions:
mbed_unsupported 0:e9b41fc7d351 10 *
mbed_unsupported 0:e9b41fc7d351 11 * The above copyright notice and this permission notice shall be included in
mbed_unsupported 0:e9b41fc7d351 12 * all copies or substantial portions of the Software.
mbed_unsupported 0:e9b41fc7d351 13 *
mbed_unsupported 0:e9b41fc7d351 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mbed_unsupported 0:e9b41fc7d351 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mbed_unsupported 0:e9b41fc7d351 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mbed_unsupported 0:e9b41fc7d351 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mbed_unsupported 0:e9b41fc7d351 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_unsupported 0:e9b41fc7d351 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
mbed_unsupported 0:e9b41fc7d351 20 * SOFTWARE.
mbed_unsupported 0:e9b41fc7d351 21 *
mbed_unsupported 0:e9b41fc7d351 22 * NOTE: This is an unsupported legacy untested library.
mbed_unsupported 0:e9b41fc7d351 23 */
mbed_unsupported 0:e9b41fc7d351 24 #ifndef MBED_SPIHALFDUPLEX_H
mbed_unsupported 0:e9b41fc7d351 25 #define MBED_SPIHALFDUPLEX_H
mbed_unsupported 0:e9b41fc7d351 26
mbed_unsupported 0:e9b41fc7d351 27 #include "device.h"
mbed_unsupported 0:e9b41fc7d351 28
mbed_unsupported 0:e9b41fc7d351 29 #if DEVICE_SPI
mbed_unsupported 0:e9b41fc7d351 30
mbed_unsupported 0:e9b41fc7d351 31 #include "SPI.h"
mbed_unsupported 0:e9b41fc7d351 32
mbed_unsupported 0:e9b41fc7d351 33 namespace mbed {
mbed_unsupported 0:e9b41fc7d351 34
mbed_unsupported 0:e9b41fc7d351 35 /* Class: SPIHalfDuplex
mbed_unsupported 0:e9b41fc7d351 36 * A SPI half-duplex master, used for communicating with SPI slave devices
mbed_unsupported 0:e9b41fc7d351 37 * over a shared data line.
mbed_unsupported 0:e9b41fc7d351 38 *
mbed_unsupported 0:e9b41fc7d351 39 * The default format is set to 8-bits for both master and slave, and a
mbed_unsupported 0:e9b41fc7d351 40 * clock frequency of 1MHz
mbed_unsupported 0:e9b41fc7d351 41 *
mbed_unsupported 0:e9b41fc7d351 42 * Most SPI devies will also require Chip Select and Reset signals. These
mbed_unsupported 0:e9b41fc7d351 43 * can be controlled using <DigitalOut> pins.
mbed_unsupported 0:e9b41fc7d351 44 *
mbed_unsupported 0:e9b41fc7d351 45 * Although this is for a shared data line, both MISO and MOSI are defined,
mbed_unsupported 0:e9b41fc7d351 46 * and should be tied together externally to the mbed. This class handles
mbed_unsupported 0:e9b41fc7d351 47 * the tri-stating of the MOSI pin.
mbed_unsupported 0:e9b41fc7d351 48 *
mbed_unsupported 0:e9b41fc7d351 49 * Example:
mbed_unsupported 0:e9b41fc7d351 50 * > // Send a byte to a SPI half-duplex slave, and record the response
mbed_unsupported 0:e9b41fc7d351 51 * >
mbed_unsupported 0:e9b41fc7d351 52 * > #include "mbed.h"
mbed_unsupported 0:e9b41fc7d351 53 * >
mbed_unsupported 0:e9b41fc7d351 54 * > SPIHalfDuplex device(p5, p6, p7) // mosi, miso, sclk
mbed_unsupported 0:e9b41fc7d351 55 * >
mbed_unsupported 0:e9b41fc7d351 56 * > int main() {
mbed_unsupported 0:e9b41fc7d351 57 * > int respone = device.write(0xAA);
mbed_unsupported 0:e9b41fc7d351 58 * > }
mbed_unsupported 0:e9b41fc7d351 59 */
mbed_unsupported 0:e9b41fc7d351 60
mbed_unsupported 0:e9b41fc7d351 61 class SPIHalfDuplex : public SPI {
mbed_unsupported 0:e9b41fc7d351 62
mbed_unsupported 0:e9b41fc7d351 63 public:
mbed_unsupported 0:e9b41fc7d351 64
mbed_unsupported 0:e9b41fc7d351 65 /* Constructor: SPIHalfDuplex
mbed_unsupported 0:e9b41fc7d351 66 * Create a SPI half-duplex master connected to the specified pins
mbed_unsupported 0:e9b41fc7d351 67 *
mbed_unsupported 0:e9b41fc7d351 68 * Variables:
mbed_unsupported 0:e9b41fc7d351 69 * mosi - SPI Master Out, Slave In pin
mbed_unsupported 0:e9b41fc7d351 70 * miso - SPI Master In, Slave Out pin
mbed_unsupported 0:e9b41fc7d351 71 * sclk - SPI Clock pin
mbed_unsupported 0:e9b41fc7d351 72 * name - (optional) A string to identify the object
mbed_unsupported 0:e9b41fc7d351 73 *
mbed_unsupported 0:e9b41fc7d351 74 * Pin Options:
mbed_unsupported 0:e9b41fc7d351 75 * (5, 6, 7) or (11, 12, 13)
mbed_unsupported 0:e9b41fc7d351 76 *
mbed_unsupported 0:e9b41fc7d351 77 * mosi or miso can be specfied as NC if not used
mbed_unsupported 0:e9b41fc7d351 78 */
mbed_unsupported 0:e9b41fc7d351 79 SPIHalfDuplex(PinName mosi, PinName miso, PinName sclk,
mbed_unsupported 0:e9b41fc7d351 80 const char *name = NULL);
mbed_unsupported 0:e9b41fc7d351 81
mbed_unsupported 0:e9b41fc7d351 82 #if 0 // Inherited from SPI - documentation only
mbed_unsupported 0:e9b41fc7d351 83 /* Function: format
mbed_unsupported 0:e9b41fc7d351 84 * Configure the data transmission format
mbed_unsupported 0:e9b41fc7d351 85 *
mbed_unsupported 0:e9b41fc7d351 86 * Variables:
mbed_unsupported 0:e9b41fc7d351 87 * bits - Number of bits per SPI frame (4 - 16)
mbed_unsupported 0:e9b41fc7d351 88 * mode - Clock polarity and phase mode (0 - 3)
mbed_unsupported 0:e9b41fc7d351 89 *
mbed_unsupported 0:e9b41fc7d351 90 * > mode | POL PHA
mbed_unsupported 0:e9b41fc7d351 91 * > -----+--------
mbed_unsupported 0:e9b41fc7d351 92 * > 0 | 0 0
mbed_unsupported 0:e9b41fc7d351 93 * > 1 | 0 1
mbed_unsupported 0:e9b41fc7d351 94 * > 2 | 1 0
mbed_unsupported 0:e9b41fc7d351 95 * > 3 | 1 1
mbed_unsupported 0:e9b41fc7d351 96 */
mbed_unsupported 0:e9b41fc7d351 97 void format(int bits, int mode = 0);
mbed_unsupported 0:e9b41fc7d351 98
mbed_unsupported 0:e9b41fc7d351 99 /* Function: frequency
mbed_unsupported 0:e9b41fc7d351 100 * Set the spi bus clock frequency
mbed_unsupported 0:e9b41fc7d351 101 *
mbed_unsupported 0:e9b41fc7d351 102 * Variables:
mbed_unsupported 0:e9b41fc7d351 103 * hz - SCLK frequency in hz (default = 1MHz)
mbed_unsupported 0:e9b41fc7d351 104 */
mbed_unsupported 0:e9b41fc7d351 105 void frequency(int hz = 1000000);
mbed_unsupported 0:e9b41fc7d351 106 #endif
mbed_unsupported 0:e9b41fc7d351 107
mbed_unsupported 0:e9b41fc7d351 108 /* Function: write
mbed_unsupported 0:e9b41fc7d351 109 * Write to the SPI Slave and return the response
mbed_unsupported 0:e9b41fc7d351 110 *
mbed_unsupported 0:e9b41fc7d351 111 * Variables:
mbed_unsupported 0:e9b41fc7d351 112 * value - Data to be sent to the SPI slave
mbed_unsupported 0:e9b41fc7d351 113 * returns - Response from the SPI slave
mbed_unsupported 0:e9b41fc7d351 114 */
mbed_unsupported 0:e9b41fc7d351 115 virtual int write(int value);
mbed_unsupported 0:e9b41fc7d351 116
mbed_unsupported 0:e9b41fc7d351 117 /* Function: slave_format
mbed_unsupported 0:e9b41fc7d351 118 * Set the number of databits expected from the slave, from 4-16
mbed_unsupported 0:e9b41fc7d351 119 *
mbed_unsupported 0:e9b41fc7d351 120 * Variables:
mbed_unsupported 0:e9b41fc7d351 121 * sbits - Number of expected bits in the slave response
mbed_unsupported 0:e9b41fc7d351 122 */
mbed_unsupported 0:e9b41fc7d351 123 void slave_format(int sbits);
mbed_unsupported 0:e9b41fc7d351 124
mbed_unsupported 0:e9b41fc7d351 125 protected:
mbed_unsupported 0:e9b41fc7d351 126 PinName _mosi;
mbed_unsupported 0:e9b41fc7d351 127 PinName _miso;
mbed_unsupported 0:e9b41fc7d351 128 int _sbits;
mbed_unsupported 0:e9b41fc7d351 129
mbed_unsupported 0:e9b41fc7d351 130 }; // End of class
mbed_unsupported 0:e9b41fc7d351 131
mbed_unsupported 0:e9b41fc7d351 132 } // End of namespace mbed
mbed_unsupported 0:e9b41fc7d351 133
mbed_unsupported 0:e9b41fc7d351 134 #endif
mbed_unsupported 0:e9b41fc7d351 135
mbed_unsupported 0:e9b41fc7d351 136 #endif