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