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