Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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