mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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