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