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