mbed-os
Fork of mbed-os by
features/unsupported/tests/libs/SPIHalfDuplex/SPIHalfDuplex.h@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library - SPIHalfDuplex |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2010-2011 ARM Limited. All rights reserved. |
elessair | 0:f269e3021894 | 3 | */ |
elessair | 0:f269e3021894 | 4 | #ifndef MBED_SPIHALFDUPLEX_H |
elessair | 0:f269e3021894 | 5 | #define MBED_SPIHALFDUPLEX_H |
elessair | 0:f269e3021894 | 6 | |
elessair | 0:f269e3021894 | 7 | #include "platform.h" |
elessair | 0:f269e3021894 | 8 | |
elessair | 0:f269e3021894 | 9 | #if DEVICE_SPI |
elessair | 0:f269e3021894 | 10 | |
elessair | 0:f269e3021894 | 11 | #include "SPI.h" |
elessair | 0:f269e3021894 | 12 | |
elessair | 0:f269e3021894 | 13 | namespace mbed { |
elessair | 0:f269e3021894 | 14 | |
elessair | 0:f269e3021894 | 15 | /** A SPI half-duplex master, used for communicating with SPI slave devices |
elessair | 0:f269e3021894 | 16 | * over a shared data line. |
elessair | 0:f269e3021894 | 17 | * |
elessair | 0:f269e3021894 | 18 | * The default format is set to 8-bits for both master and slave, and a |
elessair | 0:f269e3021894 | 19 | * clock frequency of 1MHz |
elessair | 0:f269e3021894 | 20 | * |
elessair | 0:f269e3021894 | 21 | * Most SPI devies will also require Chip Select and Reset signals. These |
elessair | 0:f269e3021894 | 22 | * can be controlled using <DigitalOut> pins. |
elessair | 0:f269e3021894 | 23 | * |
elessair | 0:f269e3021894 | 24 | * Although this is for a shared data line, both MISO and MOSI are defined, |
elessair | 0:f269e3021894 | 25 | * and should be tied together externally to the mbed. This class handles |
elessair | 0:f269e3021894 | 26 | * the tri-stating of the MOSI pin. |
elessair | 0:f269e3021894 | 27 | * |
elessair | 0:f269e3021894 | 28 | * Example: |
elessair | 0:f269e3021894 | 29 | * @code |
elessair | 0:f269e3021894 | 30 | * // Send a byte to a SPI half-duplex slave, and record the response |
elessair | 0:f269e3021894 | 31 | * |
elessair | 0:f269e3021894 | 32 | * #include "mbed.h" |
elessair | 0:f269e3021894 | 33 | * |
elessair | 0:f269e3021894 | 34 | * SPIHalfDuplex device(p5, p6, p7) // mosi, miso, sclk |
elessair | 0:f269e3021894 | 35 | * |
elessair | 0:f269e3021894 | 36 | * int main() { |
elessair | 0:f269e3021894 | 37 | * int respone = device.write(0xAA); |
elessair | 0:f269e3021894 | 38 | * } |
elessair | 0:f269e3021894 | 39 | * @endcode |
elessair | 0:f269e3021894 | 40 | */ |
elessair | 0:f269e3021894 | 41 | |
elessair | 0:f269e3021894 | 42 | class SPIHalfDuplex : public SPI { |
elessair | 0:f269e3021894 | 43 | |
elessair | 0:f269e3021894 | 44 | public: |
elessair | 0:f269e3021894 | 45 | |
elessair | 0:f269e3021894 | 46 | /** Create a SPI half-duplex master connected to the specified pins |
elessair | 0:f269e3021894 | 47 | * |
elessair | 0:f269e3021894 | 48 | * Pin Options: |
elessair | 0:f269e3021894 | 49 | * (5, 6, 7) or (11, 12, 13) |
elessair | 0:f269e3021894 | 50 | * |
elessair | 0:f269e3021894 | 51 | * mosi or miso can be specfied as NC if not used |
elessair | 0:f269e3021894 | 52 | * |
elessair | 0:f269e3021894 | 53 | * @param mosi SPI Master Out, Slave In pin |
elessair | 0:f269e3021894 | 54 | * @param miso SPI Master In, Slave Out pin |
elessair | 0:f269e3021894 | 55 | * @param sclk SPI Clock pin |
elessair | 0:f269e3021894 | 56 | * @param name (optional) A string to identify the object |
elessair | 0:f269e3021894 | 57 | */ |
elessair | 0:f269e3021894 | 58 | SPIHalfDuplex(PinName mosi, PinName miso, PinName sclk); |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | /** Write to the SPI Slave and return the response |
elessair | 0:f269e3021894 | 61 | * |
elessair | 0:f269e3021894 | 62 | * @param value Data to be sent to the SPI slave |
elessair | 0:f269e3021894 | 63 | * |
elessair | 0:f269e3021894 | 64 | * @returns |
elessair | 0:f269e3021894 | 65 | * Response from the SPI slave |
elessair | 0:f269e3021894 | 66 | */ |
elessair | 0:f269e3021894 | 67 | virtual int write(int value); |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | /** Set the number of databits expected from the slave, from 4-16 |
elessair | 0:f269e3021894 | 70 | * |
elessair | 0:f269e3021894 | 71 | * @param sbits Number of expected bits in the slave response |
elessair | 0:f269e3021894 | 72 | */ |
elessair | 0:f269e3021894 | 73 | void slave_format(int sbits); |
elessair | 0:f269e3021894 | 74 | |
elessair | 0:f269e3021894 | 75 | protected: |
elessair | 0:f269e3021894 | 76 | PinName _mosi; |
elessair | 0:f269e3021894 | 77 | PinName _miso; |
elessair | 0:f269e3021894 | 78 | int _sbits; |
elessair | 0:f269e3021894 | 79 | }; // End of class |
elessair | 0:f269e3021894 | 80 | |
elessair | 0:f269e3021894 | 81 | } // End of namespace mbed |
elessair | 0:f269e3021894 | 82 | |
elessair | 0:f269e3021894 | 83 | #endif |
elessair | 0:f269e3021894 | 84 | |
elessair | 0:f269e3021894 | 85 | #endif |