fork from Sog

Dependencies:   NNN50_WIFI_API

Fork of mbed_SPIS_multiByte_example_SOG by Sog Yang

Committer:
sog_yang
Date:
Tue Jun 13 04:34:01 2017 +0000
Revision:
5:6a1155885fc9
Parent:
2:c520d7c7739d
Sog update version.

Who changed what in which revision?

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