mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Thu Nov 08 11:46:34 2018 +0000
Revision:
188:bcfe06ba3d64
Child:
189:f392fc9709a3
mbed-dev library. Release version 164

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 188:bcfe06ba3d64 1 /* mbed Microcontroller Library
AnnaBridge 188:bcfe06ba3d64 2 * Copyright (c) 2006-2018 ARM Limited
AnnaBridge 188:bcfe06ba3d64 3 *
AnnaBridge 188:bcfe06ba3d64 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 188:bcfe06ba3d64 5 * you may not use this file except in compliance with the License.
AnnaBridge 188:bcfe06ba3d64 6 * You may obtain a copy of the License at
AnnaBridge 188:bcfe06ba3d64 7 *
AnnaBridge 188:bcfe06ba3d64 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 188:bcfe06ba3d64 9 *
AnnaBridge 188:bcfe06ba3d64 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 188:bcfe06ba3d64 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 188:bcfe06ba3d64 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 188:bcfe06ba3d64 13 * See the License for the specific language governing permissions and
AnnaBridge 188:bcfe06ba3d64 14 * limitations under the License.
AnnaBridge 188:bcfe06ba3d64 15 */
AnnaBridge 188:bcfe06ba3d64 16 #ifndef MBED_QSPI_H
AnnaBridge 188:bcfe06ba3d64 17 #define MBED_QSPI_H
AnnaBridge 188:bcfe06ba3d64 18
AnnaBridge 188:bcfe06ba3d64 19 #include "platform/platform.h"
AnnaBridge 188:bcfe06ba3d64 20
AnnaBridge 188:bcfe06ba3d64 21 #if defined (DEVICE_QSPI) || defined(DOXYGEN_ONLY)
AnnaBridge 188:bcfe06ba3d64 22
AnnaBridge 188:bcfe06ba3d64 23 #include "hal/qspi_api.h"
AnnaBridge 188:bcfe06ba3d64 24 #include "platform/PlatformMutex.h"
AnnaBridge 188:bcfe06ba3d64 25 #include "platform/SingletonPtr.h"
AnnaBridge 188:bcfe06ba3d64 26 #include "platform/NonCopyable.h"
AnnaBridge 188:bcfe06ba3d64 27
AnnaBridge 188:bcfe06ba3d64 28 #define ONE_MHZ 1000000
AnnaBridge 188:bcfe06ba3d64 29
AnnaBridge 188:bcfe06ba3d64 30 namespace mbed {
AnnaBridge 188:bcfe06ba3d64 31
AnnaBridge 188:bcfe06ba3d64 32 /** \addtogroup drivers */
AnnaBridge 188:bcfe06ba3d64 33
AnnaBridge 188:bcfe06ba3d64 34 /** A QSPI Driver, used for communicating with QSPI slave devices
AnnaBridge 188:bcfe06ba3d64 35 *
AnnaBridge 188:bcfe06ba3d64 36 * The default format is set to Quad-SPI(1-1-1), and a clock frequency of 1MHz
AnnaBridge 188:bcfe06ba3d64 37 * Most QSPI devices will also require Chip Select which is indicated by ssel.
AnnaBridge 188:bcfe06ba3d64 38 *
AnnaBridge 188:bcfe06ba3d64 39 * @note Synchronization level: Thread safe
AnnaBridge 188:bcfe06ba3d64 40 *
AnnaBridge 188:bcfe06ba3d64 41 * Example:
AnnaBridge 188:bcfe06ba3d64 42 * @code
AnnaBridge 188:bcfe06ba3d64 43 * // Write 4 byte array to a QSPI slave, and read the response, note that each device will have its specific read/write/alt values defined
AnnaBridge 188:bcfe06ba3d64 44 *
AnnaBridge 188:bcfe06ba3d64 45 * #include "mbed.h"
AnnaBridge 188:bcfe06ba3d64 46 *
AnnaBridge 188:bcfe06ba3d64 47 * #define CMD_WRITE 0x02
AnnaBridge 188:bcfe06ba3d64 48 * #define CMD_READ 0x03
AnnaBridge 188:bcfe06ba3d64 49 * #define ADDRESS 0x1000
AnnaBridge 188:bcfe06ba3d64 50 *
AnnaBridge 188:bcfe06ba3d64 51 * // hardware ssel (where applicable)
AnnaBridge 188:bcfe06ba3d64 52 * QSPI qspi_device(QSPI_FLASH1_IO0, QSPI_FLASH1_IO1, QSPI_FLASH1_IO2, QSPI_FLASH1_IO3, QSPI_FLASH1_SCK, QSPI_FLASH1_CSN); // io0, io1, io2, io3, sclk, ssel
AnnaBridge 188:bcfe06ba3d64 53 *
AnnaBridge 188:bcfe06ba3d64 54 *
AnnaBridge 188:bcfe06ba3d64 55 * int main() {
AnnaBridge 188:bcfe06ba3d64 56 * char tx_buf[] = { 0x11, 0x22, 0x33, 0x44 };
AnnaBridge 188:bcfe06ba3d64 57 * char rx_buf[4];
AnnaBridge 188:bcfe06ba3d64 58 * int buf_len = sizeof(tx_buf);
AnnaBridge 188:bcfe06ba3d64 59 *
AnnaBridge 188:bcfe06ba3d64 60 * qspi_status_t result = qspi_device.write(CMD_WRITE, 0, ADDRESS, tx_buf, &buf_len);
AnnaBridge 188:bcfe06ba3d64 61 * if (result != QSPI_STATUS_OK) {
AnnaBridge 188:bcfe06ba3d64 62 * printf("Write failed");
AnnaBridge 188:bcfe06ba3d64 63 * }
AnnaBridge 188:bcfe06ba3d64 64 * result = qspi_device.read(CMD_READ, 0, ADDRESS, rx_buf, &buf_len);
AnnaBridge 188:bcfe06ba3d64 65 * if (result != QSPI_STATUS_OK) {
AnnaBridge 188:bcfe06ba3d64 66 * printf("Read failed");
AnnaBridge 188:bcfe06ba3d64 67 * }
AnnaBridge 188:bcfe06ba3d64 68 *
AnnaBridge 188:bcfe06ba3d64 69 * }
AnnaBridge 188:bcfe06ba3d64 70 * @endcode
AnnaBridge 188:bcfe06ba3d64 71 * @ingroup drivers
AnnaBridge 188:bcfe06ba3d64 72 */
AnnaBridge 188:bcfe06ba3d64 73 class QSPI : private NonCopyable<QSPI> {
AnnaBridge 188:bcfe06ba3d64 74
AnnaBridge 188:bcfe06ba3d64 75 public:
AnnaBridge 188:bcfe06ba3d64 76
AnnaBridge 188:bcfe06ba3d64 77 /** Create a QSPI master connected to the specified pins
AnnaBridge 188:bcfe06ba3d64 78 *
AnnaBridge 188:bcfe06ba3d64 79 * io0-io3 is used to specify the Pins used for Quad SPI mode
AnnaBridge 188:bcfe06ba3d64 80 *
AnnaBridge 188:bcfe06ba3d64 81 * @param io0 1st IO pin used for sending/receiving data during data phase of a transaction
AnnaBridge 188:bcfe06ba3d64 82 * @param io1 2nd IO pin used for sending/receiving data during data phase of a transaction
AnnaBridge 188:bcfe06ba3d64 83 * @param io2 3rd IO pin used for sending/receiving data during data phase of a transaction
AnnaBridge 188:bcfe06ba3d64 84 * @param io3 4th IO pin used for sending/receiving data during data phase of a transaction
AnnaBridge 188:bcfe06ba3d64 85 * @param sclk QSPI Clock pin
AnnaBridge 188:bcfe06ba3d64 86 * @param ssel QSPI chip select pin
AnnaBridge 188:bcfe06ba3d64 87 * @param mode Clock polarity and phase mode (0 - 3) of SPI
AnnaBridge 188:bcfe06ba3d64 88 * (Default: Mode=0 uses CPOL=0, CPHA=0, Mode=1 uses CPOL=1, CPHA=1)
AnnaBridge 188:bcfe06ba3d64 89 *
AnnaBridge 188:bcfe06ba3d64 90 */
AnnaBridge 188:bcfe06ba3d64 91 QSPI(PinName io0, PinName io1, PinName io2, PinName io3, PinName sclk, PinName ssel = NC, int mode = 0);
AnnaBridge 188:bcfe06ba3d64 92 virtual ~QSPI()
AnnaBridge 188:bcfe06ba3d64 93 {
AnnaBridge 188:bcfe06ba3d64 94 }
AnnaBridge 188:bcfe06ba3d64 95
AnnaBridge 188:bcfe06ba3d64 96 /** Configure the data transmission format
AnnaBridge 188:bcfe06ba3d64 97 *
AnnaBridge 188:bcfe06ba3d64 98 * @param inst_width Bus width used by instruction phase(Valid values are QSPI_CFG_BUS_SINGLE, QSPI_CFG_BUS_DUAL, QSPI_CFG_BUS_QUAD)
AnnaBridge 188:bcfe06ba3d64 99 * @param address_width Bus width used by address phase(Valid values are QSPI_CFG_BUS_SINGLE, QSPI_CFG_BUS_DUAL, QSPI_CFG_BUS_QUAD)
AnnaBridge 188:bcfe06ba3d64 100 * @param address_size Size in bits used by address phase(Valid values are QSPI_CFG_ADDR_SIZE_8, QSPI_CFG_ADDR_SIZE_16, QSPI_CFG_ADDR_SIZE_24, QSPI_CFG_ADDR_SIZE_32)
AnnaBridge 188:bcfe06ba3d64 101 * @param alt_width Bus width used by alt phase(Valid values are QSPI_CFG_BUS_SINGLE, QSPI_CFG_BUS_DUAL, QSPI_CFG_BUS_QUAD)
AnnaBridge 188:bcfe06ba3d64 102 * @param alt_size Size in bits used by alt phase(Valid values are QSPI_CFG_ALT_SIZE_8, QSPI_CFG_ALT_SIZE_16, QSPI_CFG_ALT_SIZE_24, QSPI_CFG_ALT_SIZE_32)
AnnaBridge 188:bcfe06ba3d64 103 * @param data_width Bus width used by data phase(Valid values are QSPI_CFG_BUS_SINGLE, QSPI_CFG_BUS_DUAL, QSPI_CFG_BUS_QUAD)
AnnaBridge 188:bcfe06ba3d64 104 * @param dummy_cycles Number of dummy clock cycles to be used after alt phase
AnnaBridge 188:bcfe06ba3d64 105 *
AnnaBridge 188:bcfe06ba3d64 106 */
AnnaBridge 188:bcfe06ba3d64 107 qspi_status_t configure_format(qspi_bus_width_t inst_width,
AnnaBridge 188:bcfe06ba3d64 108 qspi_bus_width_t address_width,
AnnaBridge 188:bcfe06ba3d64 109 qspi_address_size_t address_size,
AnnaBridge 188:bcfe06ba3d64 110 qspi_bus_width_t alt_width,
AnnaBridge 188:bcfe06ba3d64 111 qspi_alt_size_t alt_size,
AnnaBridge 188:bcfe06ba3d64 112 qspi_bus_width_t data_width,
AnnaBridge 188:bcfe06ba3d64 113 int dummy_cycles);
AnnaBridge 188:bcfe06ba3d64 114
AnnaBridge 188:bcfe06ba3d64 115 /** Set the qspi bus clock frequency
AnnaBridge 188:bcfe06ba3d64 116 *
AnnaBridge 188:bcfe06ba3d64 117 * @param hz SCLK frequency in hz (default = 1MHz)
AnnaBridge 188:bcfe06ba3d64 118 * @returns
AnnaBridge 188:bcfe06ba3d64 119 * Returns QSPI_STATUS_SUCCESS on successful, fails if the interface is already init-ed
AnnaBridge 188:bcfe06ba3d64 120 */
AnnaBridge 188:bcfe06ba3d64 121 qspi_status_t set_frequency(int hz = ONE_MHZ);
AnnaBridge 188:bcfe06ba3d64 122
AnnaBridge 188:bcfe06ba3d64 123 /** Read from QSPI peripheral with the preset read_instruction and alt_value
AnnaBridge 188:bcfe06ba3d64 124 *
AnnaBridge 188:bcfe06ba3d64 125 * @param address Address to be accessed in QSPI peripheral
AnnaBridge 188:bcfe06ba3d64 126 * @param rx_buffer Buffer for data to be read from the peripheral
AnnaBridge 188:bcfe06ba3d64 127 * @param rx_length Pointer to a variable containing the length of rx_buffer, and on return this variable will be updated with the actual number of bytes read
AnnaBridge 188:bcfe06ba3d64 128 *
AnnaBridge 188:bcfe06ba3d64 129 * @returns
AnnaBridge 188:bcfe06ba3d64 130 * Returns QSPI_STATUS_SUCCESS on successful reads and QSPI_STATUS_ERROR on failed reads.
AnnaBridge 188:bcfe06ba3d64 131 */
AnnaBridge 188:bcfe06ba3d64 132 qspi_status_t read(int address, char *rx_buffer, size_t *rx_length);
AnnaBridge 188:bcfe06ba3d64 133
AnnaBridge 188:bcfe06ba3d64 134 /** Write to QSPI peripheral using custom write instruction
AnnaBridge 188:bcfe06ba3d64 135 *
AnnaBridge 188:bcfe06ba3d64 136 * @param address Address to be accessed in QSPI peripheral
AnnaBridge 188:bcfe06ba3d64 137 * @param tx_buffer Buffer containing data to be sent to peripheral
AnnaBridge 188:bcfe06ba3d64 138 * @param tx_length Pointer to a variable containing the length of data to be transmitted, and on return this variable will be updated with the actual number of bytes written
AnnaBridge 188:bcfe06ba3d64 139 *
AnnaBridge 188:bcfe06ba3d64 140 * @returns
AnnaBridge 188:bcfe06ba3d64 141 * Returns QSPI_STATUS_SUCCESS on successful reads and QSPI_STATUS_ERROR on failed reads.
AnnaBridge 188:bcfe06ba3d64 142 */
AnnaBridge 188:bcfe06ba3d64 143 qspi_status_t write(int address, const char *tx_buffer, size_t *tx_length);
AnnaBridge 188:bcfe06ba3d64 144
AnnaBridge 188:bcfe06ba3d64 145 /** Read from QSPI peripheral using custom read instruction, alt values
AnnaBridge 188:bcfe06ba3d64 146 *
AnnaBridge 188:bcfe06ba3d64 147 * @param instruction Instruction value to be used in instruction phase
AnnaBridge 188:bcfe06ba3d64 148 * @param alt Alt value to be used in Alternate-byte phase. Use -1 for ignoring Alternate-byte phase
AnnaBridge 188:bcfe06ba3d64 149 * @param address Address to be accessed in QSPI peripheral
AnnaBridge 188:bcfe06ba3d64 150 * @param rx_buffer Buffer for data to be read from the peripheral
AnnaBridge 188:bcfe06ba3d64 151 * @param rx_length Pointer to a variable containing the length of rx_buffer, and on return this variable will be updated with the actual number of bytes read
AnnaBridge 188:bcfe06ba3d64 152 *
AnnaBridge 188:bcfe06ba3d64 153 * @returns
AnnaBridge 188:bcfe06ba3d64 154 * Returns QSPI_STATUS_SUCCESS on successful reads and QSPI_STATUS_ERROR on failed reads.
AnnaBridge 188:bcfe06ba3d64 155 */
AnnaBridge 188:bcfe06ba3d64 156 qspi_status_t read(int instruction, int alt, int address, char *rx_buffer, size_t *rx_length);
AnnaBridge 188:bcfe06ba3d64 157
AnnaBridge 188:bcfe06ba3d64 158 /** Write to QSPI peripheral using custom write instruction, alt values
AnnaBridge 188:bcfe06ba3d64 159 *
AnnaBridge 188:bcfe06ba3d64 160 * @param instruction Instruction value to be used in instruction phase
AnnaBridge 188:bcfe06ba3d64 161 * @param alt Alt value to be used in Alternate-byte phase. Use -1 for ignoring Alternate-byte phase
AnnaBridge 188:bcfe06ba3d64 162 * @param address Address to be accessed in QSPI peripheral
AnnaBridge 188:bcfe06ba3d64 163 * @param tx_buffer Buffer containing data to be sent to peripheral
AnnaBridge 188:bcfe06ba3d64 164 * @param tx_length Pointer to a variable containing the length of data to be transmitted, and on return this variable will be updated with the actual number of bytes written
AnnaBridge 188:bcfe06ba3d64 165 *
AnnaBridge 188:bcfe06ba3d64 166 * @returns
AnnaBridge 188:bcfe06ba3d64 167 * Returns QSPI_STATUS_SUCCESS on successful reads and QSPI_STATUS_ERROR on failed reads.
AnnaBridge 188:bcfe06ba3d64 168 */
AnnaBridge 188:bcfe06ba3d64 169 qspi_status_t write(int instruction, int alt, int address, const char *tx_buffer, size_t *tx_length);
AnnaBridge 188:bcfe06ba3d64 170
AnnaBridge 188:bcfe06ba3d64 171 /** Perform a transaction to write to an address(a control register) and get the status results
AnnaBridge 188:bcfe06ba3d64 172 *
AnnaBridge 188:bcfe06ba3d64 173 * @param instruction Instruction value to be used in instruction phase
AnnaBridge 188:bcfe06ba3d64 174 * @param address Some instruction might require address. Use -1 if no address
AnnaBridge 188:bcfe06ba3d64 175 * @param tx_buffer Buffer containing data to be sent to peripheral
AnnaBridge 188:bcfe06ba3d64 176 * @param tx_length Pointer to a variable containing the length of data to be transmitted, and on return this variable will be updated with the actual number of bytes written
AnnaBridge 188:bcfe06ba3d64 177 * @param rx_buffer Buffer for data to be read from the peripheral
AnnaBridge 188:bcfe06ba3d64 178 * @param rx_length Pointer to a variable containing the length of rx_buffer, and on return this variable will be updated with the actual number of bytes read
AnnaBridge 188:bcfe06ba3d64 179 *
AnnaBridge 188:bcfe06ba3d64 180 * @returns
AnnaBridge 188:bcfe06ba3d64 181 * Returns QSPI_STATUS_SUCCESS on successful reads and QSPI_STATUS_ERROR on failed reads.
AnnaBridge 188:bcfe06ba3d64 182 */
AnnaBridge 188:bcfe06ba3d64 183 qspi_status_t command_transfer(int instruction, int address, const char *tx_buffer, size_t tx_length, const char *rx_buffer, size_t rx_length);
AnnaBridge 188:bcfe06ba3d64 184
AnnaBridge 188:bcfe06ba3d64 185 #if !defined(DOXYGEN_ONLY)
AnnaBridge 188:bcfe06ba3d64 186 protected:
AnnaBridge 188:bcfe06ba3d64 187 /** Acquire exclusive access to this SPI bus
AnnaBridge 188:bcfe06ba3d64 188 */
AnnaBridge 188:bcfe06ba3d64 189 virtual void lock(void);
AnnaBridge 188:bcfe06ba3d64 190
AnnaBridge 188:bcfe06ba3d64 191 /** Release exclusive access to this SPI bus
AnnaBridge 188:bcfe06ba3d64 192 */
AnnaBridge 188:bcfe06ba3d64 193 virtual void unlock(void);
AnnaBridge 188:bcfe06ba3d64 194
AnnaBridge 188:bcfe06ba3d64 195 qspi_t _qspi;
AnnaBridge 188:bcfe06ba3d64 196
AnnaBridge 188:bcfe06ba3d64 197 bool acquire(void);
AnnaBridge 188:bcfe06ba3d64 198 static QSPI *_owner;
AnnaBridge 188:bcfe06ba3d64 199 static SingletonPtr<PlatformMutex> _mutex;
AnnaBridge 188:bcfe06ba3d64 200 qspi_bus_width_t _inst_width; //Bus width for Instruction phase
AnnaBridge 188:bcfe06ba3d64 201 qspi_bus_width_t _address_width; //Bus width for Address phase
AnnaBridge 188:bcfe06ba3d64 202 qspi_address_size_t _address_size;
AnnaBridge 188:bcfe06ba3d64 203 qspi_bus_width_t _alt_width; //Bus width for Alt phase
AnnaBridge 188:bcfe06ba3d64 204 qspi_alt_size_t _alt_size;
AnnaBridge 188:bcfe06ba3d64 205 qspi_bus_width_t _data_width; //Bus width for Data phase
AnnaBridge 188:bcfe06ba3d64 206 qspi_command_t _qspi_command; //QSPI Hal command struct
AnnaBridge 188:bcfe06ba3d64 207 unsigned int _num_dummy_cycles; //Number of dummy cycles to be used
AnnaBridge 188:bcfe06ba3d64 208 int _hz; //Bus Frequency
AnnaBridge 188:bcfe06ba3d64 209 int _mode; //SPI mode
AnnaBridge 188:bcfe06ba3d64 210 bool _initialized;
AnnaBridge 188:bcfe06ba3d64 211 PinName _qspi_io0, _qspi_io1, _qspi_io2, _qspi_io3, _qspi_clk, _qspi_cs; //IO lines, clock and chip select
AnnaBridge 188:bcfe06ba3d64 212
AnnaBridge 188:bcfe06ba3d64 213 private:
AnnaBridge 188:bcfe06ba3d64 214 /* Private acquire function without locking/unlocking
AnnaBridge 188:bcfe06ba3d64 215 * Implemented in order to avoid duplicate locking and boost performance
AnnaBridge 188:bcfe06ba3d64 216 */
AnnaBridge 188:bcfe06ba3d64 217 bool _acquire(void);
AnnaBridge 188:bcfe06ba3d64 218 bool _initialize();
AnnaBridge 188:bcfe06ba3d64 219
AnnaBridge 188:bcfe06ba3d64 220 /*
AnnaBridge 188:bcfe06ba3d64 221 * This function builds the qspi command struct to be send to Hal
AnnaBridge 188:bcfe06ba3d64 222 */
AnnaBridge 188:bcfe06ba3d64 223 inline void _build_qspi_command(int instruction, int address, int alt);
AnnaBridge 188:bcfe06ba3d64 224 #endif
AnnaBridge 188:bcfe06ba3d64 225 };
AnnaBridge 188:bcfe06ba3d64 226
AnnaBridge 188:bcfe06ba3d64 227 } // namespace mbed
AnnaBridge 188:bcfe06ba3d64 228
AnnaBridge 188:bcfe06ba3d64 229 #endif
AnnaBridge 188:bcfe06ba3d64 230
AnnaBridge 188:bcfe06ba3d64 231 #endif