This is my quadcopter prototype software, still in development!

Committer:
Anaesthetix
Date:
Wed Jan 30 13:14:44 2013 +0000
Revision:
0:978110f7f027
My quadcopter prototype software, still in development.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anaesthetix 0:978110f7f027 1 /* mbed Microcontroller Library - SPISlave
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_SPISLAVE_H
Anaesthetix 0:978110f7f027 6 #define MBED_SPISLAVE_H
Anaesthetix 0:978110f7f027 7
Anaesthetix 0:978110f7f027 8 #include "device.h"
Anaesthetix 0:978110f7f027 9
Anaesthetix 0:978110f7f027 10 #if DEVICE_SPISLAVE
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: SPISlave
Anaesthetix 0:978110f7f027 20 * A SPI slave, used for communicating with a SPI Master device
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 * Example:
Anaesthetix 0:978110f7f027 25 * > // Reply to a SPI master as slave
Anaesthetix 0:978110f7f027 26 * >
Anaesthetix 0:978110f7f027 27 * > #include "mbed.h"
Anaesthetix 0:978110f7f027 28 * >
Anaesthetix 0:978110f7f027 29 * > SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
Anaesthetix 0:978110f7f027 30 * >
Anaesthetix 0:978110f7f027 31 * > int main() {
Anaesthetix 0:978110f7f027 32 * > device.reply(0x00); // Prime SPI with first reply
Anaesthetix 0:978110f7f027 33 * > while(1) {
Anaesthetix 0:978110f7f027 34 * > if(device.receive()) {
Anaesthetix 0:978110f7f027 35 * > int v = device.read(); // Read byte from master
Anaesthetix 0:978110f7f027 36 * > v = (v + 1) % 0x100; // Add one to it, modulo 256
Anaesthetix 0:978110f7f027 37 * > device.reply(v); // Make this the next reply
Anaesthetix 0:978110f7f027 38 * > }
Anaesthetix 0:978110f7f027 39 * > }
Anaesthetix 0:978110f7f027 40 * > }
Anaesthetix 0:978110f7f027 41 */
Anaesthetix 0:978110f7f027 42 class SPISlave : public Base {
Anaesthetix 0:978110f7f027 43
Anaesthetix 0:978110f7f027 44 public:
Anaesthetix 0:978110f7f027 45
Anaesthetix 0:978110f7f027 46 /* Constructor: SPI
Anaesthetix 0:978110f7f027 47 * Create a SPI slave connected to the specified pins
Anaesthetix 0:978110f7f027 48 *
Anaesthetix 0:978110f7f027 49 * Variables:
Anaesthetix 0:978110f7f027 50 * mosi - SPI Master Out, Slave In pin
Anaesthetix 0:978110f7f027 51 * miso - SPI Master In, Slave Out pin
Anaesthetix 0:978110f7f027 52 * sclk - SPI Clock pin
Anaesthetix 0:978110f7f027 53 * ssel - SPI chip select pin
Anaesthetix 0:978110f7f027 54 * name - (optional) A string to identify the object
Anaesthetix 0:978110f7f027 55 *
Anaesthetix 0:978110f7f027 56 * Pin Options:
Anaesthetix 0:978110f7f027 57 * (5, 6, 7i, 8) or (11, 12, 13, 14)
Anaesthetix 0:978110f7f027 58 *
Anaesthetix 0:978110f7f027 59 * mosi or miso can be specfied as NC if not used
Anaesthetix 0:978110f7f027 60 */
Anaesthetix 0:978110f7f027 61 SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel,
Anaesthetix 0:978110f7f027 62 const char *name = NULL);
Anaesthetix 0:978110f7f027 63
Anaesthetix 0:978110f7f027 64 /* Function: format
Anaesthetix 0:978110f7f027 65 * Configure the data transmission format
Anaesthetix 0:978110f7f027 66 *
Anaesthetix 0:978110f7f027 67 * Variables:
Anaesthetix 0:978110f7f027 68 * bits - Number of bits per SPI frame (4 - 16)
Anaesthetix 0:978110f7f027 69 * mode - Clock polarity and phase mode (0 - 3)
Anaesthetix 0:978110f7f027 70 *
Anaesthetix 0:978110f7f027 71 * > mode | POL PHA
Anaesthetix 0:978110f7f027 72 * > -----+--------
Anaesthetix 0:978110f7f027 73 * > 0 | 0 0
Anaesthetix 0:978110f7f027 74 * > 1 | 0 1
Anaesthetix 0:978110f7f027 75 * > 2 | 1 0
Anaesthetix 0:978110f7f027 76 * > 3 | 1 1
Anaesthetix 0:978110f7f027 77 */
Anaesthetix 0:978110f7f027 78 void format(int bits, int mode = 0);
Anaesthetix 0:978110f7f027 79
Anaesthetix 0:978110f7f027 80 /* Function: frequency
Anaesthetix 0:978110f7f027 81 * Set the spi bus clock frequency
Anaesthetix 0:978110f7f027 82 *
Anaesthetix 0:978110f7f027 83 * Variables:
Anaesthetix 0:978110f7f027 84 * hz - SCLK frequency in hz (default = 1MHz)
Anaesthetix 0:978110f7f027 85 */
Anaesthetix 0:978110f7f027 86 void frequency(int hz = 1000000);
Anaesthetix 0:978110f7f027 87
Anaesthetix 0:978110f7f027 88 /* Function: receive
Anaesthetix 0:978110f7f027 89 * Polls the SPI to see if data has been received
Anaesthetix 0:978110f7f027 90 *
Anaesthetix 0:978110f7f027 91 * Variables:
Anaesthetix 0:978110f7f027 92 * returns - zero if no data, 1 otherwise
Anaesthetix 0:978110f7f027 93 */
Anaesthetix 0:978110f7f027 94 int receive(void);
Anaesthetix 0:978110f7f027 95
Anaesthetix 0:978110f7f027 96 /* Function: read
Anaesthetix 0:978110f7f027 97 * Retrieve data from receive buffer as slave
Anaesthetix 0:978110f7f027 98 *
Anaesthetix 0:978110f7f027 99 * Variables:
Anaesthetix 0:978110f7f027 100 * returns - the data in the receive buffer
Anaesthetix 0:978110f7f027 101 */
Anaesthetix 0:978110f7f027 102 int read(void);
Anaesthetix 0:978110f7f027 103
Anaesthetix 0:978110f7f027 104 /* Function: reply
Anaesthetix 0:978110f7f027 105 * Fill the transmission buffer with the value to be written out
Anaesthetix 0:978110f7f027 106 * as slave on the next received message from the master.
Anaesthetix 0:978110f7f027 107 *
Anaesthetix 0:978110f7f027 108 * Variables:
Anaesthetix 0:978110f7f027 109 * value - the data to be transmitted next
Anaesthetix 0:978110f7f027 110 */
Anaesthetix 0:978110f7f027 111 void reply(int value);
Anaesthetix 0:978110f7f027 112
Anaesthetix 0:978110f7f027 113 protected:
Anaesthetix 0:978110f7f027 114
Anaesthetix 0:978110f7f027 115 SPIName _spi;
Anaesthetix 0:978110f7f027 116
Anaesthetix 0:978110f7f027 117 int _bits;
Anaesthetix 0:978110f7f027 118 int _mode;
Anaesthetix 0:978110f7f027 119 int _hz;
Anaesthetix 0:978110f7f027 120
Anaesthetix 0:978110f7f027 121 };
Anaesthetix 0:978110f7f027 122
Anaesthetix 0:978110f7f027 123 } // namespace mbed
Anaesthetix 0:978110f7f027 124
Anaesthetix 0:978110f7f027 125 #endif
Anaesthetix 0:978110f7f027 126
Anaesthetix 0:978110f7f027 127 #endif