mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

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