Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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