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