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