Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Wed Oct 29 06:23:47 2014 +0000
Revision:
9:05f0b5a3a70a
initial

Who changed what in which revision?

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