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
mbed library release version 165

Who changed what in which revision?

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