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