RTC auf true

Committer:
kevman
Date:
Wed Mar 13 11:03:24 2019 +0000
Revision:
2:7aab896b1a3b
Parent:
0:38ceb79fef03
2019-03-13

Who changed what in which revision?

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