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