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