5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
group-onsemi
Date:
Wed Jan 25 20:34:15 2017 +0000
Revision:
0:098463de4c5d
Initial commit

Who changed what in which revision?

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