Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

UserRevisionLine numberNew contents of line
einsteingustavo 0:0dee8840a1c0 1 /* mbed Microcontroller Library
einsteingustavo 0:0dee8840a1c0 2 * Copyright (c) 2006-2013 ARM Limited
einsteingustavo 0:0dee8840a1c0 3 *
einsteingustavo 0:0dee8840a1c0 4 * Licensed under the Apache License, Version 2.0 (the "License");
einsteingustavo 0:0dee8840a1c0 5 * you may not use this file except in compliance with the License.
einsteingustavo 0:0dee8840a1c0 6 * You may obtain a copy of the License at
einsteingustavo 0:0dee8840a1c0 7 *
einsteingustavo 0:0dee8840a1c0 8 * http://www.apache.org/licenses/LICENSE-2.0
einsteingustavo 0:0dee8840a1c0 9 *
einsteingustavo 0:0dee8840a1c0 10 * Unless required by applicable law or agreed to in writing, software
einsteingustavo 0:0dee8840a1c0 11 * distributed under the License is distributed on an "AS IS" BASIS,
einsteingustavo 0:0dee8840a1c0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
einsteingustavo 0:0dee8840a1c0 13 * See the License for the specific language governing permissions and
einsteingustavo 0:0dee8840a1c0 14 * limitations under the License.
einsteingustavo 0:0dee8840a1c0 15 */
einsteingustavo 0:0dee8840a1c0 16 #ifndef MBED_SPI_H
einsteingustavo 0:0dee8840a1c0 17 #define MBED_SPI_H
einsteingustavo 0:0dee8840a1c0 18
einsteingustavo 0:0dee8840a1c0 19 #include "platform.h"
einsteingustavo 0:0dee8840a1c0 20
einsteingustavo 0:0dee8840a1c0 21 #if DEVICE_SPI
einsteingustavo 0:0dee8840a1c0 22
einsteingustavo 0:0dee8840a1c0 23 #include "spi_api.h"
einsteingustavo 0:0dee8840a1c0 24
einsteingustavo 0:0dee8840a1c0 25 namespace mbed {
einsteingustavo 0:0dee8840a1c0 26
einsteingustavo 0:0dee8840a1c0 27 /** A SPI Master, used for communicating with SPI slave devices
einsteingustavo 0:0dee8840a1c0 28 *
einsteingustavo 0:0dee8840a1c0 29 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
einsteingustavo 0:0dee8840a1c0 30 *
einsteingustavo 0:0dee8840a1c0 31 * Most SPI devices will also require Chip Select and Reset signals. These
einsteingustavo 0:0dee8840a1c0 32 * can be controlled using <DigitalOut> pins
einsteingustavo 0:0dee8840a1c0 33 *
einsteingustavo 0:0dee8840a1c0 34 * Example:
einsteingustavo 0:0dee8840a1c0 35 * @code
einsteingustavo 0:0dee8840a1c0 36 * // Send a byte to a SPI slave, and record the response
einsteingustavo 0:0dee8840a1c0 37 *
einsteingustavo 0:0dee8840a1c0 38 * #include "mbed.h"
einsteingustavo 0:0dee8840a1c0 39 *
einsteingustavo 0:0dee8840a1c0 40 * SPI device(p5, p6, p7); // mosi, miso, sclk
einsteingustavo 0:0dee8840a1c0 41 *
einsteingustavo 0:0dee8840a1c0 42 * int main() {
einsteingustavo 0:0dee8840a1c0 43 * int response = device.write(0xFF);
einsteingustavo 0:0dee8840a1c0 44 * }
einsteingustavo 0:0dee8840a1c0 45 * @endcode
einsteingustavo 0:0dee8840a1c0 46 */
einsteingustavo 0:0dee8840a1c0 47 class SPI {
einsteingustavo 0:0dee8840a1c0 48
einsteingustavo 0:0dee8840a1c0 49 public:
einsteingustavo 0:0dee8840a1c0 50
einsteingustavo 0:0dee8840a1c0 51 /** Create a SPI master connected to the specified pins
einsteingustavo 0:0dee8840a1c0 52 *
einsteingustavo 0:0dee8840a1c0 53 * Pin Options:
einsteingustavo 0:0dee8840a1c0 54 * (5, 6, 7) or (11, 12, 13)
einsteingustavo 0:0dee8840a1c0 55 *
einsteingustavo 0:0dee8840a1c0 56 * mosi or miso can be specfied as NC if not used
einsteingustavo 0:0dee8840a1c0 57 *
einsteingustavo 0:0dee8840a1c0 58 * @param mosi SPI Master Out, Slave In pin
einsteingustavo 0:0dee8840a1c0 59 * @param miso SPI Master In, Slave Out pin
einsteingustavo 0:0dee8840a1c0 60 * @param sclk SPI Clock pin
einsteingustavo 0:0dee8840a1c0 61 */
einsteingustavo 0:0dee8840a1c0 62 SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused=NC);
einsteingustavo 0:0dee8840a1c0 63
einsteingustavo 0:0dee8840a1c0 64 /** Configure the data transmission format
einsteingustavo 0:0dee8840a1c0 65 *
einsteingustavo 0:0dee8840a1c0 66 * @param bits Number of bits per SPI frame (4 - 16)
einsteingustavo 0:0dee8840a1c0 67 * @param mode Clock polarity and phase mode (0 - 3)
einsteingustavo 0:0dee8840a1c0 68 *
einsteingustavo 0:0dee8840a1c0 69 * @code
einsteingustavo 0:0dee8840a1c0 70 * mode | POL PHA
einsteingustavo 0:0dee8840a1c0 71 * -----+--------
einsteingustavo 0:0dee8840a1c0 72 * 0 | 0 0
einsteingustavo 0:0dee8840a1c0 73 * 1 | 0 1
einsteingustavo 0:0dee8840a1c0 74 * 2 | 1 0
einsteingustavo 0:0dee8840a1c0 75 * 3 | 1 1
einsteingustavo 0:0dee8840a1c0 76 * @endcode
einsteingustavo 0:0dee8840a1c0 77 */
einsteingustavo 0:0dee8840a1c0 78 void format(int bits, int mode = 0);
einsteingustavo 0:0dee8840a1c0 79
einsteingustavo 0:0dee8840a1c0 80 /** Set the spi bus clock frequency
einsteingustavo 0:0dee8840a1c0 81 *
einsteingustavo 0:0dee8840a1c0 82 * @param hz SCLK frequency in hz (default = 1MHz)
einsteingustavo 0:0dee8840a1c0 83 */
einsteingustavo 0:0dee8840a1c0 84 void frequency(int hz = 1000000);
einsteingustavo 0:0dee8840a1c0 85
einsteingustavo 0:0dee8840a1c0 86 /** Write to the SPI Slave and return the response
einsteingustavo 0:0dee8840a1c0 87 *
einsteingustavo 0:0dee8840a1c0 88 * @param value Data to be sent to the SPI slave
einsteingustavo 0:0dee8840a1c0 89 *
einsteingustavo 0:0dee8840a1c0 90 * @returns
einsteingustavo 0:0dee8840a1c0 91 * Response from the SPI slave
einsteingustavo 0:0dee8840a1c0 92 */
einsteingustavo 0:0dee8840a1c0 93 virtual int write(int value);
einsteingustavo 0:0dee8840a1c0 94
einsteingustavo 0:0dee8840a1c0 95 public:
einsteingustavo 0:0dee8840a1c0 96 virtual ~SPI() {
einsteingustavo 0:0dee8840a1c0 97 }
einsteingustavo 0:0dee8840a1c0 98
einsteingustavo 0:0dee8840a1c0 99 protected:
einsteingustavo 0:0dee8840a1c0 100 spi_t _spi;
einsteingustavo 0:0dee8840a1c0 101
einsteingustavo 0:0dee8840a1c0 102 void aquire(void);
einsteingustavo 0:0dee8840a1c0 103 static SPI *_owner;
einsteingustavo 0:0dee8840a1c0 104 int _bits;
einsteingustavo 0:0dee8840a1c0 105 int _mode;
einsteingustavo 0:0dee8840a1c0 106 int _hz;
einsteingustavo 0:0dee8840a1c0 107 };
einsteingustavo 0:0dee8840a1c0 108
einsteingustavo 0:0dee8840a1c0 109 } // namespace mbed
einsteingustavo 0:0dee8840a1c0 110
einsteingustavo 0:0dee8840a1c0 111 #endif
einsteingustavo 0:0dee8840a1c0 112
einsteingustavo 0:0dee8840a1c0 113 #endif
einsteingustavo 0:0dee8840a1c0 114