fix nrf51822 i2c & spi conflict

Dependencies:   BLE_API eMPL_MPU6050 nRF51822

Fork of Seeed_Tiny_BLE_Flash by Darren Huang

Committer:
yihui
Date:
Tue Nov 17 07:48:56 2015 +0000
Revision:
5:b8c02645e6af
fix i2c & spi conflict

Who changed what in which revision?

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