| 1 | /* mbed Microcontroller Library - SPIHalfDuplex |
| 2 | * Copyright (c) 2010-2011 ARM Limited. All rights reserved. |
| 3 | */ |
| 4 | |
| 5 | #ifndef MBED_SPIHALFDUPLEX_H |
| 6 | #define MBED_SPIHALFDUPLEX_H |
| 7 | |
| 8 | #include "device.h" |
| 9 | |
| 10 | #if DEVICE_SPI |
| 11 | |
| 12 | #include "SPI.h" |
| 13 | |
| 14 | namespace mbed { |
| 15 | |
| 16 | /* Class: SPIHalfDuplex |
| 17 | * A SPI half-duplex master, used for communicating with SPI slave devices |
| 18 | * over a shared data line. |
| 19 | * |
| 20 | * The default format is set to 8-bits for both master and slave, and a |
| 21 | * clock frequency of 1MHz |
| 22 | * |
| 23 | * Most SPI devies will also require Chip Select and Reset signals. These |
| 24 | * can be controlled using <DigitalOut> pins. |
| 25 | * |
| 26 | * Although this is for a shared data line, both MISO and MOSI are defined, |
| 27 | * and should be tied together externally to the mbed. This class handles |
| 28 | * the tri-stating of the MOSI pin. |
| 29 | * |
| 30 | * Example: |
| 31 | * > // Send a byte to a SPI half-duplex slave, and record the response |
| 32 | * > |
| 33 | * > #include "mbed.h" |
| 34 | * > |
| 35 | * > SPIHalfDuplex device(p5, p6, p7) // mosi, miso, sclk |
| 36 | * > |
| 37 | * > int main() { |
| 38 | * > int respone = device.write(0xAA); |
| 39 | * > } |
| 40 | */ |
| 41 | |
| 42 | class SPIHalfDuplex : public SPI { |
| 43 | |
| 44 | public: |
| 45 | |
| 46 | /* Constructor: SPIHalfDuplex |
| 47 | * Create a SPI half-duplex master connected to the specified pins |
| 48 | * |
| 49 | * Variables: |
| 50 | * mosi - SPI Master Out, Slave In pin |
| 51 | * miso - SPI Master In, Slave Out pin |
| 52 | * sclk - SPI Clock pin |
| 53 | * name - (optional) A string to identify the object |
| 54 | * |
| 55 | * Pin Options: |
| 56 | * (5, 6, 7) or (11, 12, 13) |
| 57 | * |
| 58 | * mosi or miso can be specfied as NC if not used |
| 59 | */ |
| 60 | SPIHalfDuplex(PinName mosi, PinName miso, PinName sclk, |
| 61 | const char *name = NULL); |
| 62 | |
| 63 | #if 0 // Inherited from SPI - documentation only |
| 64 | /* Function: format |
| 65 | * Configure the data transmission format |
| 66 | * |
| 67 | * Variables: |
| 68 | * bits - Number of bits per SPI frame (4 - 16) |
| 69 | * mode - Clock polarity and phase mode (0 - 3) |
| 70 | * |
| 71 | * > mode | POL PHA |
| 72 | * > -----+-------- |
| 73 | * > 0 | 0 0 |
| 74 | * > 1 | 0 1 |
| 75 | * > 2 | 1 0 |
| 76 | * > 3 | 1 1 |
| 77 | */ |
| 78 | void format(int bits, int mode = 0); |
| 79 | |
| 80 | /* Function: frequency |
| 81 | * Set the spi bus clock frequency |
| 82 | * |
| 83 | * Variables: |
| 84 | * hz - SCLK frequency in hz (default = 1MHz) |
| 85 | */ |
| 86 | void frequency(int hz = 1000000); |
| 87 | #endif |
| 88 | |
| 89 | /* Function: write |
| 90 | * Write to the SPI Slave and return the response |
| 91 | * |
| 92 | * Variables: |
| 93 | * value - Data to be sent to the SPI slave |
| 94 | * returns - Response from the SPI slave |
| 95 | */ |
| 96 | virtual int write(int value); |
| 97 | |
| 98 | /* Function: slave_format |
| 99 | * Set the number of databits expected from the slave, from 4-16 |
| 100 | * |
| 101 | * Variables: |
| 102 | * sbits - Number of expected bits in the slave response |
| 103 | */ |
| 104 | void slave_format(int sbits); |
| 105 | |
| 106 | protected: |
| 107 | PinName _mosi; |
| 108 | PinName _miso; |
| 109 | int _sbits; |
| 110 | |
| 111 | }; // End of class |
| 112 | |
| 113 | } // End of namespace mbed |
| 114 | |
| 115 | #endif |
| 116 | |
| 117 | #endif |