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