R1 code for micro:bit based train controller code, requires second micro:bit running rx code to operate - see https://meanderingpi.wordpress.com/ for more information
Fork of mbed-dev-bin by
SPISlave.h@0:e1a608bb55e8, 2016-04-07 (annotated)
- Committer:
- jamesadevine
- Date:
- Thu Apr 07 18:33:58 2016 +0100
- Revision:
- 0:e1a608bb55e8
Added binary version of mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesadevine | 0:e1a608bb55e8 | 1 | /* mbed Microcontroller Library |
jamesadevine | 0:e1a608bb55e8 | 2 | * Copyright (c) 2006-2013 ARM Limited |
jamesadevine | 0:e1a608bb55e8 | 3 | * |
jamesadevine | 0:e1a608bb55e8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jamesadevine | 0:e1a608bb55e8 | 5 | * you may not use this file except in compliance with the License. |
jamesadevine | 0:e1a608bb55e8 | 6 | * You may obtain a copy of the License at |
jamesadevine | 0:e1a608bb55e8 | 7 | * |
jamesadevine | 0:e1a608bb55e8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jamesadevine | 0:e1a608bb55e8 | 9 | * |
jamesadevine | 0:e1a608bb55e8 | 10 | * Unless required by applicable law or agreed to in writing, software |
jamesadevine | 0:e1a608bb55e8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jamesadevine | 0:e1a608bb55e8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jamesadevine | 0:e1a608bb55e8 | 13 | * See the License for the specific language governing permissions and |
jamesadevine | 0:e1a608bb55e8 | 14 | * limitations under the License. |
jamesadevine | 0:e1a608bb55e8 | 15 | */ |
jamesadevine | 0:e1a608bb55e8 | 16 | #ifndef MBED_SPISLAVE_H |
jamesadevine | 0:e1a608bb55e8 | 17 | #define MBED_SPISLAVE_H |
jamesadevine | 0:e1a608bb55e8 | 18 | |
jamesadevine | 0:e1a608bb55e8 | 19 | #include "platform.h" |
jamesadevine | 0:e1a608bb55e8 | 20 | |
jamesadevine | 0:e1a608bb55e8 | 21 | #if DEVICE_SPISLAVE |
jamesadevine | 0:e1a608bb55e8 | 22 | |
jamesadevine | 0:e1a608bb55e8 | 23 | #include "spi_api.h" |
jamesadevine | 0:e1a608bb55e8 | 24 | |
jamesadevine | 0:e1a608bb55e8 | 25 | namespace mbed { |
jamesadevine | 0:e1a608bb55e8 | 26 | |
jamesadevine | 0:e1a608bb55e8 | 27 | /** A SPI slave, used for communicating with a SPI Master device |
jamesadevine | 0:e1a608bb55e8 | 28 | * |
jamesadevine | 0:e1a608bb55e8 | 29 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
jamesadevine | 0:e1a608bb55e8 | 30 | * |
jamesadevine | 0:e1a608bb55e8 | 31 | * Example: |
jamesadevine | 0:e1a608bb55e8 | 32 | * @code |
jamesadevine | 0:e1a608bb55e8 | 33 | * // Reply to a SPI master as slave |
jamesadevine | 0:e1a608bb55e8 | 34 | * |
jamesadevine | 0:e1a608bb55e8 | 35 | * #include "mbed.h" |
jamesadevine | 0:e1a608bb55e8 | 36 | * |
jamesadevine | 0:e1a608bb55e8 | 37 | * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel |
jamesadevine | 0:e1a608bb55e8 | 38 | * |
jamesadevine | 0:e1a608bb55e8 | 39 | * int main() { |
jamesadevine | 0:e1a608bb55e8 | 40 | * device.reply(0x00); // Prime SPI with first reply |
jamesadevine | 0:e1a608bb55e8 | 41 | * while(1) { |
jamesadevine | 0:e1a608bb55e8 | 42 | * if(device.receive()) { |
jamesadevine | 0:e1a608bb55e8 | 43 | * int v = device.read(); // Read byte from master |
jamesadevine | 0:e1a608bb55e8 | 44 | * v = (v + 1) % 0x100; // Add one to it, modulo 256 |
jamesadevine | 0:e1a608bb55e8 | 45 | * device.reply(v); // Make this the next reply |
jamesadevine | 0:e1a608bb55e8 | 46 | * } |
jamesadevine | 0:e1a608bb55e8 | 47 | * } |
jamesadevine | 0:e1a608bb55e8 | 48 | * } |
jamesadevine | 0:e1a608bb55e8 | 49 | * @endcode |
jamesadevine | 0:e1a608bb55e8 | 50 | */ |
jamesadevine | 0:e1a608bb55e8 | 51 | class SPISlave { |
jamesadevine | 0:e1a608bb55e8 | 52 | |
jamesadevine | 0:e1a608bb55e8 | 53 | public: |
jamesadevine | 0:e1a608bb55e8 | 54 | |
jamesadevine | 0:e1a608bb55e8 | 55 | /** Create a SPI slave connected to the specified pins |
jamesadevine | 0:e1a608bb55e8 | 56 | * |
jamesadevine | 0:e1a608bb55e8 | 57 | * mosi or miso can be specfied as NC if not used |
jamesadevine | 0:e1a608bb55e8 | 58 | * |
jamesadevine | 0:e1a608bb55e8 | 59 | * @param mosi SPI Master Out, Slave In pin |
jamesadevine | 0:e1a608bb55e8 | 60 | * @param miso SPI Master In, Slave Out pin |
jamesadevine | 0:e1a608bb55e8 | 61 | * @param sclk SPI Clock pin |
jamesadevine | 0:e1a608bb55e8 | 62 | * @param ssel SPI chip select pin |
jamesadevine | 0:e1a608bb55e8 | 63 | */ |
jamesadevine | 0:e1a608bb55e8 | 64 | SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel); |
jamesadevine | 0:e1a608bb55e8 | 65 | |
jamesadevine | 0:e1a608bb55e8 | 66 | /** Configure the data transmission format |
jamesadevine | 0:e1a608bb55e8 | 67 | * |
jamesadevine | 0:e1a608bb55e8 | 68 | * @param bits Number of bits per SPI frame (4 - 16) |
jamesadevine | 0:e1a608bb55e8 | 69 | * @param mode Clock polarity and phase mode (0 - 3) |
jamesadevine | 0:e1a608bb55e8 | 70 | * |
jamesadevine | 0:e1a608bb55e8 | 71 | * @code |
jamesadevine | 0:e1a608bb55e8 | 72 | * mode | POL PHA |
jamesadevine | 0:e1a608bb55e8 | 73 | * -----+-------- |
jamesadevine | 0:e1a608bb55e8 | 74 | * 0 | 0 0 |
jamesadevine | 0:e1a608bb55e8 | 75 | * 1 | 0 1 |
jamesadevine | 0:e1a608bb55e8 | 76 | * 2 | 1 0 |
jamesadevine | 0:e1a608bb55e8 | 77 | * 3 | 1 1 |
jamesadevine | 0:e1a608bb55e8 | 78 | * @endcode |
jamesadevine | 0:e1a608bb55e8 | 79 | */ |
jamesadevine | 0:e1a608bb55e8 | 80 | void format(int bits, int mode = 0); |
jamesadevine | 0:e1a608bb55e8 | 81 | |
jamesadevine | 0:e1a608bb55e8 | 82 | /** Set the spi bus clock frequency |
jamesadevine | 0:e1a608bb55e8 | 83 | * |
jamesadevine | 0:e1a608bb55e8 | 84 | * @param hz SCLK frequency in hz (default = 1MHz) |
jamesadevine | 0:e1a608bb55e8 | 85 | */ |
jamesadevine | 0:e1a608bb55e8 | 86 | void frequency(int hz = 1000000); |
jamesadevine | 0:e1a608bb55e8 | 87 | |
jamesadevine | 0:e1a608bb55e8 | 88 | /** Polls the SPI to see if data has been received |
jamesadevine | 0:e1a608bb55e8 | 89 | * |
jamesadevine | 0:e1a608bb55e8 | 90 | * @returns |
jamesadevine | 0:e1a608bb55e8 | 91 | * 0 if no data, |
jamesadevine | 0:e1a608bb55e8 | 92 | * 1 otherwise |
jamesadevine | 0:e1a608bb55e8 | 93 | */ |
jamesadevine | 0:e1a608bb55e8 | 94 | int receive(void); |
jamesadevine | 0:e1a608bb55e8 | 95 | |
jamesadevine | 0:e1a608bb55e8 | 96 | /** Retrieve data from receive buffer as slave |
jamesadevine | 0:e1a608bb55e8 | 97 | * |
jamesadevine | 0:e1a608bb55e8 | 98 | * @returns |
jamesadevine | 0:e1a608bb55e8 | 99 | * the data in the receive buffer |
jamesadevine | 0:e1a608bb55e8 | 100 | */ |
jamesadevine | 0:e1a608bb55e8 | 101 | int read(void); |
jamesadevine | 0:e1a608bb55e8 | 102 | |
jamesadevine | 0:e1a608bb55e8 | 103 | /** Fill the transmission buffer with the value to be written out |
jamesadevine | 0:e1a608bb55e8 | 104 | * as slave on the next received message from the master. |
jamesadevine | 0:e1a608bb55e8 | 105 | * |
jamesadevine | 0:e1a608bb55e8 | 106 | * @param value the data to be transmitted next |
jamesadevine | 0:e1a608bb55e8 | 107 | */ |
jamesadevine | 0:e1a608bb55e8 | 108 | void reply(int value); |
jamesadevine | 0:e1a608bb55e8 | 109 | |
jamesadevine | 0:e1a608bb55e8 | 110 | protected: |
jamesadevine | 0:e1a608bb55e8 | 111 | spi_t _spi; |
jamesadevine | 0:e1a608bb55e8 | 112 | |
jamesadevine | 0:e1a608bb55e8 | 113 | int _bits; |
jamesadevine | 0:e1a608bb55e8 | 114 | int _mode; |
jamesadevine | 0:e1a608bb55e8 | 115 | int _hz; |
jamesadevine | 0:e1a608bb55e8 | 116 | }; |
jamesadevine | 0:e1a608bb55e8 | 117 | |
jamesadevine | 0:e1a608bb55e8 | 118 | } // namespace mbed |
jamesadevine | 0:e1a608bb55e8 | 119 | |
jamesadevine | 0:e1a608bb55e8 | 120 | #endif |
jamesadevine | 0:e1a608bb55e8 | 121 | |
jamesadevine | 0:e1a608bb55e8 | 122 | #endif |