printer

Dependents:   Good_Serial_HelloWorld_Mbed

Fork of mbed by gokmen ascioglu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPISlave.h Source File

SPISlave.h

00001 /* mbed Microcontroller Library - SPISlave
00002  * Copyright (c) 2010-2011 ARM Limited. All rights reserved. 
00003  */
00004 
00005 #ifndef MBED_SPISLAVE_H
00006 #define MBED_SPISLAVE_H
00007 
00008 #include "device.h"
00009 
00010 #if DEVICE_SPISLAVE
00011 
00012 #include "platform.h"
00013 #include "PinNames.h"
00014 #include "PeripheralNames.h"
00015 #include "Base.h"
00016 
00017 namespace mbed {
00018 
00019 /* Class: SPISlave
00020  *  A SPI slave, used for communicating with a SPI Master device
00021  *
00022  * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
00023  *
00024  * Example:
00025  * > // Reply to a SPI master as slave
00026  * >
00027  * > #include "mbed.h"
00028  * >
00029  * > SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
00030  * >
00031  * > int main() {
00032  * >     device.reply(0x00);              // Prime SPI with first reply
00033  * >     while(1) {
00034  * >         if(device.receive()) {
00035  * >             int v = device.read();   // Read byte from master
00036  * >             v = (v + 1) % 0x100;     // Add one to it, modulo 256
00037  * >             device.reply(v);         // Make this the next reply
00038  * >         }
00039  * >     }
00040  * > }
00041  */ 
00042 class SPISlave : public Base {
00043 
00044 public:
00045 
00046     /* Constructor: SPI
00047      *  Create a SPI slave connected to the specified pins
00048      *
00049      * Variables:
00050      *  mosi - SPI Master Out, Slave In pin
00051      *  miso - SPI Master In, Slave Out pin
00052      *  sclk - SPI Clock pin
00053      *  ssel - SPI chip select pin
00054      *  name - (optional) A string to identify the object     
00055      *
00056      * Pin Options:
00057      *  (5, 6, 7i, 8) or (11, 12, 13, 14)
00058      *
00059      *  mosi or miso can be specfied as NC if not used
00060      */
00061     SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel,
00062         const char *name = NULL);
00063 
00064     /* Function: format
00065      *  Configure the data transmission format
00066      *
00067      * Variables:
00068      *  bits - Number of bits per SPI frame (4 - 16)
00069      *  mode - Clock polarity and phase mode (0 - 3)
00070      *
00071      * > mode | POL PHA 
00072      * > -----+--------  
00073      * >   0  |  0   0 
00074      * >   1  |  0   1
00075      * >   2  |  1   0 
00076      * >   3  |  1   1
00077      */
00078     void format(int bits, int mode = 0);
00079 
00080     /* Function: frequency
00081      *  Set the spi bus clock frequency
00082      *
00083      * Variables:
00084      *  hz - SCLK frequency in hz (default = 1MHz)
00085      */
00086     void frequency(int hz = 1000000);
00087 
00088     /* Function: receive
00089      *  Polls the SPI to see if data has been received
00090      *
00091      * Variables:
00092      *  returns - zero if no data, 1 otherwise
00093      */
00094     int receive(void);
00095 
00096     /* Function: read
00097      *  Retrieve  data from receive buffer as slave
00098      *
00099      * Variables:
00100      *  returns - the data in the receive buffer
00101      */
00102     int read(void);
00103 
00104     /* Function: reply
00105      *  Fill the transmission buffer with the value to be written out
00106      *  as slave on the next received message from the master.
00107      *
00108      * Variables:
00109      *  value - the data to be transmitted next
00110      */
00111     void reply(int value);
00112 
00113 protected:
00114 
00115     SPIName _spi;
00116     
00117     int _bits;
00118     int _mode;
00119     int _hz;
00120 
00121 };
00122 
00123 } // namespace mbed
00124 
00125 #endif
00126 
00127 #endif