test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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