svn / mbed / trunk / SPI.h

Revision 29, 2.5 kB (checked in by emilmont, 6 months ago)

New Libraries 11.11

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