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