Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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