SD card interface

Committer:
lharoon
Date:
Mon Oct 08 11:14:07 2012 +0000
Revision:
0:22612ae617a0
1st edition

Who changed what in which revision?

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