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