Lab 1 Program C
Fork of mbed by
SPI.h@11:1c1ebd0324fa, 2009-08-28 (annotated)
- Committer:
- rolf.meyer@arm.com
- Date:
- Fri Aug 28 12:10:11 2009 +0000
- Revision:
- 11:1c1ebd0324fa
- Parent:
- 5:62573be585e9
- Child:
- 20:029aa53d7323
A shiny new version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rolf.meyer@arm.com | 11:1c1ebd0324fa | 1 | /* mbed Microcontroller Library - SPI |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 2 | * Copyright (c) 2006-2009 ARM Limited. All rights reserved. |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 3 | * sford |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 4 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 5 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 6 | #ifndef MBED_SPI_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 7 | #define MBED_SPI_H |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 8 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 9 | #include "platform.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 10 | #include "PinNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 11 | #include "PeripheralNames.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 12 | #include "Base.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 13 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 14 | namespace mbed { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 15 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 16 | /* Class: SPI |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 17 | * A SPI Master, used for communicating with SPI slave devices |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 18 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 19 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 20 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 21 | * Most SPI devices will also require Chip Select and Reset signals. These |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 22 | * can be controlled using <DigitalOut> pins |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 23 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 24 | * Example: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 25 | * > // Send a byte to a SPI slave, and record the response |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 26 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 27 | * > #include "mbed.h" |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 28 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 29 | * > SPI device(p5, p6, p7); // mosi, miso, sclk |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 30 | * > |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 31 | * > int main() { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 32 | * > int response = device.write(0xFF); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 33 | * > } |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 34 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 35 | class SPI : public Base { |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 36 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 37 | public: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 38 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 39 | /* Constructor: SPI |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 40 | * Create a SPI master connected to the specified pins |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 41 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 42 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 43 | * mosi - SPI Master Out, Slave In pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 44 | * miso - SPI Master In, Slave Out pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 45 | * sclk - SPI Clock pin |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 46 | * name - (optional) A string to identify the object |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 47 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 48 | * Pin Options: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 49 | * (5, 6, 7) or (11, 12, 13) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 50 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 51 | * mosi or miso can be specfied as NOT_CONNECTED if not used |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 52 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 53 | SPI(PinName mosi, PinName miso, PinName sclk, const char *name = NULL); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 54 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 55 | /* Function: format |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 56 | * Configure the data transmission format |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 57 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 58 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 59 | * bits - Number of bits per SPI frame (4 - 16) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 60 | * mode - Clock polarity and phase mode (0 - 3) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 61 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 62 | * > mode | POL PHA |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 63 | * > -----+-------- |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 64 | * > 0 | 0 0 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 65 | * > 1 | 0 1 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 66 | * > 2 | 1 0 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 67 | * > 3 | 1 1 |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 68 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 69 | void format(int bits, int mode = 0); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 70 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 71 | /* Function: frequency |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 72 | * Set the spi bus clock frequency |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 73 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 74 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 75 | * hz - SCLK frequency in hz (default = 1MHz) |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 76 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 77 | void frequency(int hz = 1000000); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 78 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 79 | /* Function: write |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 80 | * Write to the SPI Slave and return the response |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 81 | * |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 82 | * Variables: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 83 | * value - Data to be sent to the SPI slave |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 84 | * returns - Response from the SPI slave |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 85 | */ |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 86 | int write(int value); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 87 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 88 | #ifdef MBED_RPC |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 89 | virtual const struct rpc_method *get_rpc_methods(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 90 | static struct rpc_class *get_rpc_class(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 91 | #endif |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 92 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 93 | protected: |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 94 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 95 | SPIName _spi; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 96 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 97 | void aquire(); |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 98 | static SPI *_owner; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 99 | int _bits; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 100 | int _mode; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 101 | int _hz; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 102 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 103 | }; |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 104 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 105 | } // namespace mbed |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 106 | |
rolf.meyer@arm.com | 11:1c1ebd0324fa | 107 | #endif |