mbed library sources

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Mon Jun 10 16:03:00 2013 +0100
Revision:
9:0ce32e54c9a7
Parent:
cpp/SPISlave.h@2:143cac498751
Child:
10:3bc89ef62ce7
Refactoring of the mbed SDK:
- Provide a well defined HAL and API
- Keep separated the HAL implementations for the different targets

Who changed what in which revision?

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