mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Mon Feb 18 09:41:56 2013 +0000
Revision:
9:663789d7729f
Parent:
8:c14af7958ef5
Update mbed-KL25Z to latest build

Who changed what in which revision?

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