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