This is my quadcopter prototype software, still in development!

Committer:
Anaesthetix
Date:
Tue Jul 23 14:01:42 2013 +0000
Revision:
1:ac68f0368a77
Parent:
0:978110f7f027
Other accelerometer added

Who changed what in which revision?

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