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