Einstein Filho
/
MANGUEBAJA2019_BOX
Mangue Baja Box
mbed/SPISlave.h@0:0dee8840a1c0, 2019-07-29 (annotated)
- Committer:
- einsteingustavo
- Date:
- Mon Jul 29 20:38:00 2019 +0000
- Revision:
- 0:0dee8840a1c0
Mangue Baja Box
Who changed what in which revision?
User | Revision | Line number | New 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_SPISLAVE_H |
einsteingustavo | 0:0dee8840a1c0 | 17 | #define MBED_SPISLAVE_H |
einsteingustavo | 0:0dee8840a1c0 | 18 | |
einsteingustavo | 0:0dee8840a1c0 | 19 | #include "platform.h" |
einsteingustavo | 0:0dee8840a1c0 | 20 | |
einsteingustavo | 0:0dee8840a1c0 | 21 | #if DEVICE_SPISLAVE |
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 slave, used for communicating with a SPI Master device |
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 | * Example: |
einsteingustavo | 0:0dee8840a1c0 | 32 | * @code |
einsteingustavo | 0:0dee8840a1c0 | 33 | * // Reply to a SPI master as slave |
einsteingustavo | 0:0dee8840a1c0 | 34 | * |
einsteingustavo | 0:0dee8840a1c0 | 35 | * #include "mbed.h" |
einsteingustavo | 0:0dee8840a1c0 | 36 | * |
einsteingustavo | 0:0dee8840a1c0 | 37 | * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel |
einsteingustavo | 0:0dee8840a1c0 | 38 | * |
einsteingustavo | 0:0dee8840a1c0 | 39 | * int main() { |
einsteingustavo | 0:0dee8840a1c0 | 40 | * device.reply(0x00); // Prime SPI with first reply |
einsteingustavo | 0:0dee8840a1c0 | 41 | * while(1) { |
einsteingustavo | 0:0dee8840a1c0 | 42 | * if(device.receive()) { |
einsteingustavo | 0:0dee8840a1c0 | 43 | * int v = device.read(); // Read byte from master |
einsteingustavo | 0:0dee8840a1c0 | 44 | * v = (v + 1) % 0x100; // Add one to it, modulo 256 |
einsteingustavo | 0:0dee8840a1c0 | 45 | * device.reply(v); // Make this the next reply |
einsteingustavo | 0:0dee8840a1c0 | 46 | * } |
einsteingustavo | 0:0dee8840a1c0 | 47 | * } |
einsteingustavo | 0:0dee8840a1c0 | 48 | * } |
einsteingustavo | 0:0dee8840a1c0 | 49 | * @endcode |
einsteingustavo | 0:0dee8840a1c0 | 50 | */ |
einsteingustavo | 0:0dee8840a1c0 | 51 | class SPISlave { |
einsteingustavo | 0:0dee8840a1c0 | 52 | |
einsteingustavo | 0:0dee8840a1c0 | 53 | public: |
einsteingustavo | 0:0dee8840a1c0 | 54 | |
einsteingustavo | 0:0dee8840a1c0 | 55 | /** Create a SPI slave connected to the specified pins |
einsteingustavo | 0:0dee8840a1c0 | 56 | * |
einsteingustavo | 0:0dee8840a1c0 | 57 | * Pin Options: |
einsteingustavo | 0:0dee8840a1c0 | 58 | * (5, 6, 7i, 8) or (11, 12, 13, 14) |
einsteingustavo | 0:0dee8840a1c0 | 59 | * |
einsteingustavo | 0:0dee8840a1c0 | 60 | * mosi or miso can be specfied as NC if not used |
einsteingustavo | 0:0dee8840a1c0 | 61 | * |
einsteingustavo | 0:0dee8840a1c0 | 62 | * @param mosi SPI Master Out, Slave In pin |
einsteingustavo | 0:0dee8840a1c0 | 63 | * @param miso SPI Master In, Slave Out pin |
einsteingustavo | 0:0dee8840a1c0 | 64 | * @param sclk SPI Clock pin |
einsteingustavo | 0:0dee8840a1c0 | 65 | * @param ssel SPI chip select pin |
einsteingustavo | 0:0dee8840a1c0 | 66 | * @param name (optional) A string to identify the object |
einsteingustavo | 0:0dee8840a1c0 | 67 | */ |
einsteingustavo | 0:0dee8840a1c0 | 68 | SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel); |
einsteingustavo | 0:0dee8840a1c0 | 69 | |
einsteingustavo | 0:0dee8840a1c0 | 70 | /** Configure the data transmission format |
einsteingustavo | 0:0dee8840a1c0 | 71 | * |
einsteingustavo | 0:0dee8840a1c0 | 72 | * @param bits Number of bits per SPI frame (4 - 16) |
einsteingustavo | 0:0dee8840a1c0 | 73 | * @param mode Clock polarity and phase mode (0 - 3) |
einsteingustavo | 0:0dee8840a1c0 | 74 | * |
einsteingustavo | 0:0dee8840a1c0 | 75 | * @code |
einsteingustavo | 0:0dee8840a1c0 | 76 | * mode | POL PHA |
einsteingustavo | 0:0dee8840a1c0 | 77 | * -----+-------- |
einsteingustavo | 0:0dee8840a1c0 | 78 | * 0 | 0 0 |
einsteingustavo | 0:0dee8840a1c0 | 79 | * 1 | 0 1 |
einsteingustavo | 0:0dee8840a1c0 | 80 | * 2 | 1 0 |
einsteingustavo | 0:0dee8840a1c0 | 81 | * 3 | 1 1 |
einsteingustavo | 0:0dee8840a1c0 | 82 | * @endcode |
einsteingustavo | 0:0dee8840a1c0 | 83 | */ |
einsteingustavo | 0:0dee8840a1c0 | 84 | void format(int bits, int mode = 0); |
einsteingustavo | 0:0dee8840a1c0 | 85 | |
einsteingustavo | 0:0dee8840a1c0 | 86 | /** Set the spi bus clock frequency |
einsteingustavo | 0:0dee8840a1c0 | 87 | * |
einsteingustavo | 0:0dee8840a1c0 | 88 | * @param hz SCLK frequency in hz (default = 1MHz) |
einsteingustavo | 0:0dee8840a1c0 | 89 | */ |
einsteingustavo | 0:0dee8840a1c0 | 90 | void frequency(int hz = 1000000); |
einsteingustavo | 0:0dee8840a1c0 | 91 | |
einsteingustavo | 0:0dee8840a1c0 | 92 | /** Polls the SPI to see if data has been received |
einsteingustavo | 0:0dee8840a1c0 | 93 | * |
einsteingustavo | 0:0dee8840a1c0 | 94 | * @returns |
einsteingustavo | 0:0dee8840a1c0 | 95 | * 0 if no data, |
einsteingustavo | 0:0dee8840a1c0 | 96 | * 1 otherwise |
einsteingustavo | 0:0dee8840a1c0 | 97 | */ |
einsteingustavo | 0:0dee8840a1c0 | 98 | int receive(void); |
einsteingustavo | 0:0dee8840a1c0 | 99 | |
einsteingustavo | 0:0dee8840a1c0 | 100 | /** Retrieve data from receive buffer as slave |
einsteingustavo | 0:0dee8840a1c0 | 101 | * |
einsteingustavo | 0:0dee8840a1c0 | 102 | * @returns |
einsteingustavo | 0:0dee8840a1c0 | 103 | * the data in the receive buffer |
einsteingustavo | 0:0dee8840a1c0 | 104 | */ |
einsteingustavo | 0:0dee8840a1c0 | 105 | int read(void); |
einsteingustavo | 0:0dee8840a1c0 | 106 | |
einsteingustavo | 0:0dee8840a1c0 | 107 | /** Fill the transmission buffer with the value to be written out |
einsteingustavo | 0:0dee8840a1c0 | 108 | * as slave on the next received message from the master. |
einsteingustavo | 0:0dee8840a1c0 | 109 | * |
einsteingustavo | 0:0dee8840a1c0 | 110 | * @param value the data to be transmitted next |
einsteingustavo | 0:0dee8840a1c0 | 111 | */ |
einsteingustavo | 0:0dee8840a1c0 | 112 | void reply(int value); |
einsteingustavo | 0:0dee8840a1c0 | 113 | |
einsteingustavo | 0:0dee8840a1c0 | 114 | protected: |
einsteingustavo | 0:0dee8840a1c0 | 115 | spi_t _spi; |
einsteingustavo | 0:0dee8840a1c0 | 116 | |
einsteingustavo | 0:0dee8840a1c0 | 117 | int _bits; |
einsteingustavo | 0:0dee8840a1c0 | 118 | int _mode; |
einsteingustavo | 0:0dee8840a1c0 | 119 | int _hz; |
einsteingustavo | 0:0dee8840a1c0 | 120 | }; |
einsteingustavo | 0:0dee8840a1c0 | 121 | |
einsteingustavo | 0:0dee8840a1c0 | 122 | } // namespace mbed |
einsteingustavo | 0:0dee8840a1c0 | 123 | |
einsteingustavo | 0:0dee8840a1c0 | 124 | #endif |
einsteingustavo | 0:0dee8840a1c0 | 125 | |
einsteingustavo | 0:0dee8840a1c0 | 126 | #endif |
einsteingustavo | 0:0dee8840a1c0 | 127 |