mbed library for NZ32-SC151

Committer:
modtronix
Date:
Fri Jul 24 21:01:44 2015 +1000
Revision:
1:71204b8406f2
Current mbed v103 (594)

Who changed what in which revision?

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