test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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