SD card interface
mbed-export/SPI.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 - SPI |
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_SPI_H |
lharoon | 0:22612ae617a0 | 6 | #define MBED_SPI_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 "platform.h" |
lharoon | 0:22612ae617a0 | 13 | #include "PinNames.h" |
lharoon | 0:22612ae617a0 | 14 | #include "PeripheralNames.h" |
lharoon | 0:22612ae617a0 | 15 | #include "Base.h" |
lharoon | 0:22612ae617a0 | 16 | |
lharoon | 0:22612ae617a0 | 17 | namespace mbed { |
lharoon | 0:22612ae617a0 | 18 | |
lharoon | 0:22612ae617a0 | 19 | /* Class: SPI |
lharoon | 0:22612ae617a0 | 20 | * A SPI Master, used for communicating with SPI slave devices |
lharoon | 0:22612ae617a0 | 21 | * |
lharoon | 0:22612ae617a0 | 22 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
lharoon | 0:22612ae617a0 | 23 | * |
lharoon | 0:22612ae617a0 | 24 | * Most SPI devices will also require Chip Select and Reset signals. These |
lharoon | 0:22612ae617a0 | 25 | * can be controlled using <DigitalOut> pins |
lharoon | 0:22612ae617a0 | 26 | * |
lharoon | 0:22612ae617a0 | 27 | * Example: |
lharoon | 0:22612ae617a0 | 28 | * > // Send a byte to a SPI slave, and record the response |
lharoon | 0:22612ae617a0 | 29 | * > |
lharoon | 0:22612ae617a0 | 30 | * > #include "mbed.h" |
lharoon | 0:22612ae617a0 | 31 | * > |
lharoon | 0:22612ae617a0 | 32 | * > SPI device(p5, p6, p7); // mosi, miso, sclk |
lharoon | 0:22612ae617a0 | 33 | * > |
lharoon | 0:22612ae617a0 | 34 | * > int main() { |
lharoon | 0:22612ae617a0 | 35 | * > int response = device.write(0xFF); |
lharoon | 0:22612ae617a0 | 36 | * > } |
lharoon | 0:22612ae617a0 | 37 | */ |
lharoon | 0:22612ae617a0 | 38 | class SPI : public Base { |
lharoon | 0:22612ae617a0 | 39 | |
lharoon | 0:22612ae617a0 | 40 | public: |
lharoon | 0:22612ae617a0 | 41 | |
lharoon | 0:22612ae617a0 | 42 | /* Constructor: SPI |
lharoon | 0:22612ae617a0 | 43 | * Create a SPI master connected to the specified pins |
lharoon | 0:22612ae617a0 | 44 | * |
lharoon | 0:22612ae617a0 | 45 | * Variables: |
lharoon | 0:22612ae617a0 | 46 | * mosi - SPI Master Out, Slave In pin |
lharoon | 0:22612ae617a0 | 47 | * miso - SPI Master In, Slave Out pin |
lharoon | 0:22612ae617a0 | 48 | * sclk - SPI Clock pin |
lharoon | 0:22612ae617a0 | 49 | * name - (optional) A string to identify the object |
lharoon | 0:22612ae617a0 | 50 | * |
lharoon | 0:22612ae617a0 | 51 | * Pin Options: |
lharoon | 0:22612ae617a0 | 52 | * (5, 6, 7) or (11, 12, 13) |
lharoon | 0:22612ae617a0 | 53 | * |
lharoon | 0:22612ae617a0 | 54 | * mosi or miso can be specfied as NC if not used |
lharoon | 0:22612ae617a0 | 55 | */ |
lharoon | 0:22612ae617a0 | 56 | SPI(PinName mosi, PinName miso, PinName sclk, const char *name = NULL); |
lharoon | 0:22612ae617a0 | 57 | |
lharoon | 0:22612ae617a0 | 58 | /* Function: format |
lharoon | 0:22612ae617a0 | 59 | * Configure the data transmission format |
lharoon | 0:22612ae617a0 | 60 | * |
lharoon | 0:22612ae617a0 | 61 | * Variables: |
lharoon | 0:22612ae617a0 | 62 | * bits - Number of bits per SPI frame (4 - 16) |
lharoon | 0:22612ae617a0 | 63 | * mode - Clock polarity and phase mode (0 - 3) |
lharoon | 0:22612ae617a0 | 64 | * |
lharoon | 0:22612ae617a0 | 65 | * > mode | POL PHA |
lharoon | 0:22612ae617a0 | 66 | * > -----+-------- |
lharoon | 0:22612ae617a0 | 67 | * > 0 | 0 0 |
lharoon | 0:22612ae617a0 | 68 | * > 1 | 0 1 |
lharoon | 0:22612ae617a0 | 69 | * > 2 | 1 0 |
lharoon | 0:22612ae617a0 | 70 | * > 3 | 1 1 |
lharoon | 0:22612ae617a0 | 71 | */ |
lharoon | 0:22612ae617a0 | 72 | void format(int bits, int mode = 0); |
lharoon | 0:22612ae617a0 | 73 | |
lharoon | 0:22612ae617a0 | 74 | /* Function: frequency |
lharoon | 0:22612ae617a0 | 75 | * Set the spi bus clock frequency |
lharoon | 0:22612ae617a0 | 76 | * |
lharoon | 0:22612ae617a0 | 77 | * Variables: |
lharoon | 0:22612ae617a0 | 78 | * hz - SCLK frequency in hz (default = 1MHz) |
lharoon | 0:22612ae617a0 | 79 | */ |
lharoon | 0:22612ae617a0 | 80 | void frequency(int hz = 1000000); |
lharoon | 0:22612ae617a0 | 81 | |
lharoon | 0:22612ae617a0 | 82 | /* Function: write |
lharoon | 0:22612ae617a0 | 83 | * Write to the SPI Slave and return the response |
lharoon | 0:22612ae617a0 | 84 | * |
lharoon | 0:22612ae617a0 | 85 | * Variables: |
lharoon | 0:22612ae617a0 | 86 | * value - Data to be sent to the SPI slave |
lharoon | 0:22612ae617a0 | 87 | * returns - Response from the SPI slave |
lharoon | 0:22612ae617a0 | 88 | */ |
lharoon | 0:22612ae617a0 | 89 | virtual int write(int value); |
lharoon | 0:22612ae617a0 | 90 | |
lharoon | 0:22612ae617a0 | 91 | |
lharoon | 0:22612ae617a0 | 92 | #ifdef MBED_RPC |
lharoon | 0:22612ae617a0 | 93 | virtual const struct rpc_method *get_rpc_methods(); |
lharoon | 0:22612ae617a0 | 94 | static struct rpc_class *get_rpc_class(); |
lharoon | 0:22612ae617a0 | 95 | #endif |
lharoon | 0:22612ae617a0 | 96 | |
lharoon | 0:22612ae617a0 | 97 | protected: |
lharoon | 0:22612ae617a0 | 98 | |
lharoon | 0:22612ae617a0 | 99 | SPIName _spi; |
lharoon | 0:22612ae617a0 | 100 | |
lharoon | 0:22612ae617a0 | 101 | void aquire(void); |
lharoon | 0:22612ae617a0 | 102 | static SPI *_owner; |
lharoon | 0:22612ae617a0 | 103 | int _bits; |
lharoon | 0:22612ae617a0 | 104 | int _mode; |
lharoon | 0:22612ae617a0 | 105 | int _hz; |
lharoon | 0:22612ae617a0 | 106 | |
lharoon | 0:22612ae617a0 | 107 | }; |
lharoon | 0:22612ae617a0 | 108 | |
lharoon | 0:22612ae617a0 | 109 | } // namespace mbed |
lharoon | 0:22612ae617a0 | 110 | |
lharoon | 0:22612ae617a0 | 111 | #endif |
lharoon | 0:22612ae617a0 | 112 | |
lharoon | 0:22612ae617a0 | 113 | #endif |