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