Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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