max32630fthr quad spi , unexpected spi behavior

Committer:
boonshen
Date:
Tue Mar 13 21:12:00 2018 +0000
Revision:
0:a35c40f49345
MAX32630FTHR QuadSPI test

Who changed what in which revision?

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