2018-03-02 13:17:08 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/mphal.h"
|
2019-07-02 14:50:32 +00:00
|
|
|
#include "mpu.h"
|
2018-03-02 13:17:08 +00:00
|
|
|
#include "qspi.h"
|
2019-02-11 06:22:37 +00:00
|
|
|
#include "pin_static_af.h"
|
2018-03-02 13:17:08 +00:00
|
|
|
|
|
|
|
#if defined(MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2)
|
|
|
|
|
2019-07-02 14:50:32 +00:00
|
|
|
#define QSPI_MAP_ADDR (0x90000000)
|
|
|
|
|
2019-03-01 02:39:44 +00:00
|
|
|
#ifndef MICROPY_HW_QSPI_PRESCALER
|
|
|
|
#define MICROPY_HW_QSPI_PRESCALER 3 // F_CLK = F_AHB/3 (72MHz when CPU is 216MHz)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MICROPY_HW_QSPI_SAMPLE_SHIFT
|
|
|
|
#define MICROPY_HW_QSPI_SAMPLE_SHIFT 1 // sample shift enabled
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MICROPY_HW_QSPI_TIMEOUT_COUNTER
|
|
|
|
#define MICROPY_HW_QSPI_TIMEOUT_COUNTER 0 // timeout counter disabled (see F7 errata)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MICROPY_HW_QSPI_CS_HIGH_CYCLES
|
|
|
|
#define MICROPY_HW_QSPI_CS_HIGH_CYCLES 2 // nCS stays high for 2 cycles
|
|
|
|
#endif
|
|
|
|
|
2022-04-13 10:21:42 +00:00
|
|
|
#ifndef MICROPY_HW_QSPI_MPU_REGION_SIZE
|
|
|
|
#define MICROPY_HW_QSPI_MPU_REGION_SIZE ((1 << (MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3)) >> 20)
|
|
|
|
#endif
|
|
|
|
|
2020-01-28 03:59:05 +00:00
|
|
|
#if (MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3 - 1) >= 24
|
|
|
|
#define QSPI_CMD 0xec
|
|
|
|
#define QSPI_ADSIZE 3
|
|
|
|
#else
|
|
|
|
#define QSPI_CMD 0xeb
|
|
|
|
#define QSPI_ADSIZE 2
|
|
|
|
#endif
|
|
|
|
|
2019-07-02 14:50:32 +00:00
|
|
|
static inline void qspi_mpu_disable_all(void) {
|
|
|
|
// Configure MPU to disable access to entire QSPI region, to prevent CPU
|
|
|
|
// speculative execution from accessing this region and modifying QSPI registers.
|
2019-10-16 12:12:06 +00:00
|
|
|
uint32_t irq_state = mpu_config_start();
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x00, MPU_REGION_SIZE_256MB));
|
2019-10-16 12:12:06 +00:00
|
|
|
mpu_config_end(irq_state);
|
2019-07-02 14:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void qspi_mpu_enable_mapped(void) {
|
|
|
|
// Configure MPU to allow access to only the valid part of external SPI flash.
|
|
|
|
// The memory accesses to the mapped QSPI are faster if the MPU is not used
|
|
|
|
// for the memory-mapped region, so 3 MPU regions are used to disable access
|
|
|
|
// to everything except the valid address space, using holes in the bottom
|
|
|
|
// of the regions and nesting them.
|
2022-04-13 10:21:42 +00:00
|
|
|
// Note: Disabling a subregion (by setting its corresponding SRD bit to 1)
|
|
|
|
// means another region overlapping the disabled range matches instead. If no
|
|
|
|
// other enabled region overlaps the disabled subregion, and the access is
|
|
|
|
// unprivileged or the background region is disabled, the MPU issues a fault.
|
2019-10-16 12:12:06 +00:00
|
|
|
uint32_t irq_state = mpu_config_start();
|
2022-04-13 10:21:42 +00:00
|
|
|
#if MICROPY_HW_QSPI_MPU_REGION_SIZE > 128
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0xFF, MPU_REGION_SIZE_256MB));
|
2022-04-13 10:21:42 +00:00
|
|
|
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 64
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x0F, MPU_REGION_SIZE_256MB));
|
2022-04-13 10:21:42 +00:00
|
|
|
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 32
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x03, MPU_REGION_SIZE_256MB));
|
2022-04-13 10:21:42 +00:00
|
|
|
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 16
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x01, MPU_REGION_SIZE_256MB));
|
2022-04-13 10:21:42 +00:00
|
|
|
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 8
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x01, MPU_REGION_SIZE_256MB));
|
|
|
|
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x0F, MPU_REGION_SIZE_32MB));
|
2022-04-13 10:21:42 +00:00
|
|
|
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 4
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x01, MPU_REGION_SIZE_256MB));
|
|
|
|
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x03, MPU_REGION_SIZE_32MB));
|
2022-04-13 10:21:42 +00:00
|
|
|
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 2
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x01, MPU_REGION_SIZE_256MB));
|
|
|
|
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x01, MPU_REGION_SIZE_32MB));
|
2022-04-13 10:21:42 +00:00
|
|
|
#elif MICROPY_HW_QSPI_MPU_REGION_SIZE > 1
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x01, MPU_REGION_SIZE_256MB));
|
|
|
|
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x0F, MPU_REGION_SIZE_32MB));
|
|
|
|
mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x01, MPU_REGION_SIZE_16MB));
|
2022-04-13 10:21:42 +00:00
|
|
|
#else
|
2024-02-21 01:31:11 +00:00
|
|
|
mpu_config_region(MPU_REGION_QSPI1, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x01, MPU_REGION_SIZE_256MB));
|
|
|
|
mpu_config_region(MPU_REGION_QSPI2, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x01, MPU_REGION_SIZE_32MB));
|
|
|
|
mpu_config_region(MPU_REGION_QSPI3, QSPI_MAP_ADDR, MPU_CONFIG_NOACCESS(0x03, MPU_REGION_SIZE_4MB));
|
2022-04-13 10:21:42 +00:00
|
|
|
#endif
|
2019-10-16 12:12:06 +00:00
|
|
|
mpu_config_end(irq_state);
|
2019-07-02 14:50:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 13:17:08 +00:00
|
|
|
void qspi_init(void) {
|
2019-07-02 14:50:32 +00:00
|
|
|
qspi_mpu_disable_all();
|
|
|
|
|
2018-03-02 13:17:08 +00:00
|
|
|
// Configure pins
|
2019-03-01 02:39:44 +00:00
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_QSPIFLASH_CS, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_QUADSPI_BK1_NCS);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_QSPIFLASH_SCK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_QUADSPI_CLK);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_QSPIFLASH_IO0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_QUADSPI_BK1_IO0);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_QSPIFLASH_IO1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_QUADSPI_BK1_IO1);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_QSPIFLASH_IO2, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_QUADSPI_BK1_IO2);
|
|
|
|
mp_hal_pin_config_alt_static_speed(MICROPY_HW_QSPIFLASH_IO3, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_VERY_HIGH, STATIC_AF_QUADSPI_BK1_IO3);
|
2018-03-02 13:17:08 +00:00
|
|
|
|
|
|
|
// Bring up the QSPI peripheral
|
|
|
|
|
|
|
|
__HAL_RCC_QSPI_CLK_ENABLE();
|
2019-07-02 14:59:56 +00:00
|
|
|
__HAL_RCC_QSPI_FORCE_RESET();
|
|
|
|
__HAL_RCC_QSPI_RELEASE_RESET();
|
2018-03-02 13:17:08 +00:00
|
|
|
|
|
|
|
QUADSPI->CR =
|
2019-03-01 02:39:44 +00:00
|
|
|
(MICROPY_HW_QSPI_PRESCALER - 1) << QUADSPI_CR_PRESCALER_Pos
|
2020-02-27 04:36:53 +00:00
|
|
|
| 3 << QUADSPI_CR_FTHRES_Pos // 4 bytes must be available to read/write
|
2018-03-02 13:17:08 +00:00
|
|
|
#if defined(QUADSPI_CR_FSEL_Pos)
|
|
|
|
| 0 << QUADSPI_CR_FSEL_Pos // FLASH 1 selected
|
|
|
|
#endif
|
|
|
|
#if defined(QUADSPI_CR_DFM_Pos)
|
|
|
|
| 0 << QUADSPI_CR_DFM_Pos // dual-flash mode disabled
|
|
|
|
#endif
|
2019-03-01 02:39:44 +00:00
|
|
|
| MICROPY_HW_QSPI_SAMPLE_SHIFT << QUADSPI_CR_SSHIFT_Pos
|
2020-02-27 04:36:53 +00:00
|
|
|
| MICROPY_HW_QSPI_TIMEOUT_COUNTER << QUADSPI_CR_TCEN_Pos
|
|
|
|
| 1 << QUADSPI_CR_EN_Pos // enable the peripheral
|
|
|
|
;
|
2018-03-02 13:17:08 +00:00
|
|
|
|
|
|
|
QUADSPI->DCR =
|
|
|
|
(MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 - 3 - 1) << QUADSPI_DCR_FSIZE_Pos
|
2020-02-27 04:36:53 +00:00
|
|
|
| (MICROPY_HW_QSPI_CS_HIGH_CYCLES - 1) << QUADSPI_DCR_CSHT_Pos
|
|
|
|
| 0 << QUADSPI_DCR_CKMODE_Pos // CLK idles at low state
|
|
|
|
;
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void qspi_memory_map(void) {
|
|
|
|
// Enable memory-mapped mode
|
|
|
|
|
|
|
|
QUADSPI->ABR = 0; // disable continuous read mode
|
2020-01-28 03:59:05 +00:00
|
|
|
|
2018-03-02 13:17:08 +00:00
|
|
|
QUADSPI->CCR =
|
|
|
|
0 << QUADSPI_CCR_DDRM_Pos // DDR mode disabled
|
2020-02-27 04:36:53 +00:00
|
|
|
| 0 << QUADSPI_CCR_SIOO_Pos // send instruction every transaction
|
|
|
|
| 3 << QUADSPI_CCR_FMODE_Pos // memory-mapped mode
|
|
|
|
| 3 << QUADSPI_CCR_DMODE_Pos // data on 4 lines
|
|
|
|
| 4 << QUADSPI_CCR_DCYC_Pos // 4 dummy cycles
|
|
|
|
| 0 << QUADSPI_CCR_ABSIZE_Pos // 8-bit alternate byte
|
|
|
|
| 3 << QUADSPI_CCR_ABMODE_Pos // alternate byte on 4 lines
|
|
|
|
| QSPI_ADSIZE << QUADSPI_CCR_ADSIZE_Pos
|
|
|
|
| 3 << QUADSPI_CCR_ADMODE_Pos // address on 4 lines
|
|
|
|
| 1 << QUADSPI_CCR_IMODE_Pos // instruction on 1 line
|
|
|
|
| QSPI_CMD << QUADSPI_CCR_INSTRUCTION_Pos
|
|
|
|
;
|
2019-07-02 14:50:32 +00:00
|
|
|
|
|
|
|
qspi_mpu_enable_mapped();
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 04:32:29 +00:00
|
|
|
static int qspi_ioctl(void *self_in, uint32_t cmd) {
|
2018-03-02 13:17:08 +00:00
|
|
|
(void)self_in;
|
|
|
|
switch (cmd) {
|
|
|
|
case MP_QSPI_IOCTL_INIT:
|
|
|
|
qspi_init();
|
|
|
|
break;
|
2019-07-02 15:00:40 +00:00
|
|
|
case MP_QSPI_IOCTL_BUS_ACQUIRE:
|
|
|
|
// Disable memory-mapped region during bus access
|
|
|
|
qspi_mpu_disable_all();
|
|
|
|
// Abort any ongoing transfer if peripheral is busy
|
|
|
|
if (QUADSPI->SR & QUADSPI_SR_BUSY) {
|
|
|
|
QUADSPI->CR |= QUADSPI_CR_ABORT;
|
|
|
|
while (QUADSPI->CR & QUADSPI_CR_ABORT) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2018-03-02 13:17:08 +00:00
|
|
|
case MP_QSPI_IOCTL_BUS_RELEASE:
|
|
|
|
// Switch to memory-map mode when bus is idle
|
|
|
|
qspi_memory_map();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0; // success
|
|
|
|
}
|
|
|
|
|
2024-02-27 04:32:29 +00:00
|
|
|
static int qspi_write_cmd_data(void *self_in, uint8_t cmd, size_t len, uint32_t data) {
|
2018-03-02 13:17:08 +00:00
|
|
|
(void)self_in;
|
|
|
|
|
|
|
|
QUADSPI->FCR = QUADSPI_FCR_CTCF; // clear TC flag
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
QUADSPI->CCR =
|
|
|
|
0 << QUADSPI_CCR_DDRM_Pos // DDR mode disabled
|
2020-02-27 04:36:53 +00:00
|
|
|
| 0 << QUADSPI_CCR_SIOO_Pos // send instruction every transaction
|
|
|
|
| 0 << QUADSPI_CCR_FMODE_Pos // indirect write mode
|
|
|
|
| 0 << QUADSPI_CCR_DMODE_Pos // no data
|
|
|
|
| 0 << QUADSPI_CCR_DCYC_Pos // 0 dummy cycles
|
|
|
|
| 0 << QUADSPI_CCR_ABMODE_Pos // no alternate byte
|
|
|
|
| 0 << QUADSPI_CCR_ADMODE_Pos // no address
|
|
|
|
| 1 << QUADSPI_CCR_IMODE_Pos // instruction on 1 line
|
|
|
|
| cmd << QUADSPI_CCR_INSTRUCTION_Pos // write opcode
|
|
|
|
;
|
2018-03-02 13:17:08 +00:00
|
|
|
} else {
|
|
|
|
QUADSPI->DLR = len - 1;
|
|
|
|
|
|
|
|
QUADSPI->CCR =
|
|
|
|
0 << QUADSPI_CCR_DDRM_Pos // DDR mode disabled
|
2020-02-27 04:36:53 +00:00
|
|
|
| 0 << QUADSPI_CCR_SIOO_Pos // send instruction every transaction
|
|
|
|
| 0 << QUADSPI_CCR_FMODE_Pos // indirect write mode
|
|
|
|
| 1 << QUADSPI_CCR_DMODE_Pos // data on 1 line
|
|
|
|
| 0 << QUADSPI_CCR_DCYC_Pos // 0 dummy cycles
|
|
|
|
| 0 << QUADSPI_CCR_ABMODE_Pos // no alternate byte
|
|
|
|
| 0 << QUADSPI_CCR_ADMODE_Pos // no address
|
|
|
|
| 1 << QUADSPI_CCR_IMODE_Pos // instruction on 1 line
|
|
|
|
| cmd << QUADSPI_CCR_INSTRUCTION_Pos // write opcode
|
|
|
|
;
|
2018-03-02 13:17:08 +00:00
|
|
|
|
2022-04-13 10:40:11 +00:00
|
|
|
// Wait for at least 1 free byte location in the FIFO.
|
|
|
|
while (!(QUADSPI->SR & QUADSPI_SR_FTF)) {
|
|
|
|
}
|
|
|
|
|
2023-07-04 05:11:06 +00:00
|
|
|
if (len == 1) {
|
|
|
|
*(uint8_t *)&QUADSPI->DR = data;
|
|
|
|
} else {
|
|
|
|
// This assumes len==2
|
|
|
|
*(uint16_t *)&QUADSPI->DR = data;
|
|
|
|
}
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for write to finish
|
|
|
|
while (!(QUADSPI->SR & QUADSPI_SR_TCF)) {
|
2021-03-04 23:15:29 +00:00
|
|
|
if (QUADSPI->SR & QUADSPI_SR_TEF) {
|
|
|
|
return -MP_EIO;
|
|
|
|
}
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QUADSPI->FCR = QUADSPI_FCR_CTCF; // clear TC flag
|
2021-03-04 23:15:29 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 04:32:29 +00:00
|
|
|
static int qspi_write_cmd_addr_data(void *self_in, uint8_t cmd, uint32_t addr, size_t len, const uint8_t *src) {
|
2018-03-02 13:17:08 +00:00
|
|
|
(void)self_in;
|
|
|
|
|
2022-06-01 08:50:43 +00:00
|
|
|
uint8_t adsize = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? 3 : 2;
|
2020-01-28 03:59:05 +00:00
|
|
|
|
2018-03-02 13:17:08 +00:00
|
|
|
QUADSPI->FCR = QUADSPI_FCR_CTCF; // clear TC flag
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
QUADSPI->CCR =
|
|
|
|
0 << QUADSPI_CCR_DDRM_Pos // DDR mode disabled
|
2020-02-27 04:36:53 +00:00
|
|
|
| 0 << QUADSPI_CCR_SIOO_Pos // send instruction every transaction
|
|
|
|
| 0 << QUADSPI_CCR_FMODE_Pos // indirect write mode
|
|
|
|
| 0 << QUADSPI_CCR_DMODE_Pos // no data
|
|
|
|
| 0 << QUADSPI_CCR_DCYC_Pos // 0 dummy cycles
|
|
|
|
| 0 << QUADSPI_CCR_ABMODE_Pos // no alternate byte
|
|
|
|
| adsize << QUADSPI_CCR_ADSIZE_Pos // 32/24-bit address size
|
|
|
|
| 1 << QUADSPI_CCR_ADMODE_Pos // address on 1 line
|
|
|
|
| 1 << QUADSPI_CCR_IMODE_Pos // instruction on 1 line
|
|
|
|
| cmd << QUADSPI_CCR_INSTRUCTION_Pos // write opcode
|
|
|
|
;
|
2018-03-02 13:17:08 +00:00
|
|
|
|
|
|
|
QUADSPI->AR = addr;
|
|
|
|
} else {
|
|
|
|
QUADSPI->DLR = len - 1;
|
|
|
|
|
|
|
|
QUADSPI->CCR =
|
|
|
|
0 << QUADSPI_CCR_DDRM_Pos // DDR mode disabled
|
2020-02-27 04:36:53 +00:00
|
|
|
| 0 << QUADSPI_CCR_SIOO_Pos // send instruction every transaction
|
|
|
|
| 0 << QUADSPI_CCR_FMODE_Pos // indirect write mode
|
|
|
|
| 1 << QUADSPI_CCR_DMODE_Pos // data on 1 line
|
|
|
|
| 0 << QUADSPI_CCR_DCYC_Pos // 0 dummy cycles
|
|
|
|
| 0 << QUADSPI_CCR_ABMODE_Pos // no alternate byte
|
|
|
|
| adsize << QUADSPI_CCR_ADSIZE_Pos // 32/24-bit address size
|
|
|
|
| 1 << QUADSPI_CCR_ADMODE_Pos // address on 1 line
|
|
|
|
| 1 << QUADSPI_CCR_IMODE_Pos // instruction on 1 line
|
|
|
|
| cmd << QUADSPI_CCR_INSTRUCTION_Pos // write opcode
|
|
|
|
;
|
2018-03-02 13:17:08 +00:00
|
|
|
|
|
|
|
QUADSPI->AR = addr;
|
|
|
|
|
2018-06-22 05:07:01 +00:00
|
|
|
// Write out the data 1 byte at a time
|
2018-03-02 13:17:08 +00:00
|
|
|
while (len) {
|
|
|
|
while (!(QUADSPI->SR & QUADSPI_SR_FTF)) {
|
2021-03-04 23:15:29 +00:00
|
|
|
if (QUADSPI->SR & QUADSPI_SR_TEF) {
|
|
|
|
return -MP_EIO;
|
|
|
|
}
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
2020-02-27 04:36:53 +00:00
|
|
|
*(volatile uint8_t *)&QUADSPI->DR = *src++;
|
2018-06-22 05:07:01 +00:00
|
|
|
--len;
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for write to finish
|
|
|
|
while (!(QUADSPI->SR & QUADSPI_SR_TCF)) {
|
2021-03-04 23:15:29 +00:00
|
|
|
if (QUADSPI->SR & QUADSPI_SR_TEF) {
|
|
|
|
return -MP_EIO;
|
|
|
|
}
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QUADSPI->FCR = QUADSPI_FCR_CTCF; // clear TC flag
|
2021-03-04 23:15:29 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 04:32:29 +00:00
|
|
|
static int qspi_read_cmd(void *self_in, uint8_t cmd, size_t len, uint32_t *dest) {
|
2018-03-02 13:17:08 +00:00
|
|
|
(void)self_in;
|
|
|
|
|
|
|
|
QUADSPI->FCR = QUADSPI_FCR_CTCF; // clear TC flag
|
|
|
|
|
|
|
|
QUADSPI->DLR = len - 1; // number of bytes to read
|
|
|
|
|
|
|
|
QUADSPI->CCR =
|
|
|
|
0 << QUADSPI_CCR_DDRM_Pos // DDR mode disabled
|
2020-02-27 04:36:53 +00:00
|
|
|
| 0 << QUADSPI_CCR_SIOO_Pos // send instruction every transaction
|
|
|
|
| 1 << QUADSPI_CCR_FMODE_Pos // indirect read mode
|
|
|
|
| 1 << QUADSPI_CCR_DMODE_Pos // data on 1 line
|
|
|
|
| 0 << QUADSPI_CCR_DCYC_Pos // 0 dummy cycles
|
|
|
|
| 0 << QUADSPI_CCR_ABMODE_Pos // no alternate byte
|
|
|
|
| 0 << QUADSPI_CCR_ADMODE_Pos // no address
|
|
|
|
| 1 << QUADSPI_CCR_IMODE_Pos // instruction on 1 line
|
|
|
|
| cmd << QUADSPI_CCR_INSTRUCTION_Pos // read opcode
|
|
|
|
;
|
2018-03-02 13:17:08 +00:00
|
|
|
|
|
|
|
// Wait for read to finish
|
|
|
|
while (!(QUADSPI->SR & QUADSPI_SR_TCF)) {
|
2021-03-04 23:15:29 +00:00
|
|
|
if (QUADSPI->SR & QUADSPI_SR_TEF) {
|
|
|
|
return -MP_EIO;
|
|
|
|
}
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QUADSPI->FCR = QUADSPI_FCR_CTCF; // clear TC flag
|
|
|
|
|
|
|
|
// Read result
|
2022-12-09 01:28:54 +00:00
|
|
|
*dest = QUADSPI->DR;
|
|
|
|
|
|
|
|
return 0;
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 03:11:59 +00:00
|
|
|
static int qspi_read_cmd_addr_data(void *self_in, uint8_t cmd, uint32_t addr, size_t len, uint8_t *dest, uint8_t mode) {
|
2018-03-02 13:17:08 +00:00
|
|
|
(void)self_in;
|
2022-06-01 08:50:43 +00:00
|
|
|
uint8_t adsize = MICROPY_HW_SPI_ADDR_IS_32BIT(addr) ? 3 : 2;
|
2020-01-28 03:59:05 +00:00
|
|
|
|
2023-11-17 03:11:59 +00:00
|
|
|
uint32_t dmode = 0;
|
|
|
|
uint32_t admode = 0;
|
|
|
|
uint32_t dcyc = 0;
|
|
|
|
uint32_t abmode = 0;
|
|
|
|
|
|
|
|
if (mode == MP_QSPI_TRANSFER_CMD_QADDR_QDATA) {
|
|
|
|
dmode = 3; // 4 data lines used
|
|
|
|
admode = 3; // 4 address lines used
|
|
|
|
dcyc = 4; // 4 dummy cycles (2 bytes)
|
|
|
|
abmode = 3; // alternate-byte bytes sent on 4 lines
|
|
|
|
} else if (mode == MP_QSPI_TRANSFER_CMD_ADDR_DATA) {
|
|
|
|
dmode = 1; // 1 data lines used
|
|
|
|
admode = 1; // 1 address lines used
|
|
|
|
dcyc = 8; // 8 dummy cycles (1 byte)
|
|
|
|
abmode = 0; // No alternate-byte bytes sent
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-11 07:28:48 +00:00
|
|
|
QUADSPI->FCR = QUADSPI_FCR_CTCF; // clear TC flag
|
|
|
|
|
|
|
|
QUADSPI->DLR = len - 1; // number of bytes to read
|
|
|
|
|
|
|
|
QUADSPI->CCR =
|
|
|
|
0 << QUADSPI_CCR_DDRM_Pos // DDR mode disabled
|
2020-02-27 04:36:53 +00:00
|
|
|
| 0 << QUADSPI_CCR_SIOO_Pos // send instruction every transaction
|
|
|
|
| 1 << QUADSPI_CCR_FMODE_Pos // indirect read mode
|
2023-11-17 03:11:59 +00:00
|
|
|
| dmode << QUADSPI_CCR_DMODE_Pos // data lines
|
|
|
|
| dcyc << QUADSPI_CCR_DCYC_Pos // dummy cycles
|
2020-02-27 04:36:53 +00:00
|
|
|
| 0 << QUADSPI_CCR_ABSIZE_Pos // 8-bit alternate byte
|
2023-11-17 03:11:59 +00:00
|
|
|
| abmode << QUADSPI_CCR_ABMODE_Pos // alternate byte count / lines
|
2020-02-27 04:36:53 +00:00
|
|
|
| adsize << QUADSPI_CCR_ADSIZE_Pos // 32 or 24-bit address size
|
2023-11-17 03:11:59 +00:00
|
|
|
| admode << QUADSPI_CCR_ADMODE_Pos // address lines
|
2020-02-27 04:36:53 +00:00
|
|
|
| 1 << QUADSPI_CCR_IMODE_Pos // instruction on 1 line
|
|
|
|
| cmd << QUADSPI_CCR_INSTRUCTION_Pos // quad read opcode
|
|
|
|
;
|
2018-03-11 07:28:48 +00:00
|
|
|
|
|
|
|
QUADSPI->ABR = 0; // alternate byte: disable continuous read mode
|
2021-12-18 17:32:06 +00:00
|
|
|
QUADSPI->AR = addr; // address to read from
|
2018-03-11 07:28:48 +00:00
|
|
|
|
2022-04-13 10:41:28 +00:00
|
|
|
#if defined(STM32H7)
|
|
|
|
// Workaround for SR getting set immediately after setting the address.
|
|
|
|
if (QUADSPI->SR & 0x01) {
|
|
|
|
QUADSPI->FCR |= QUADSPI_FCR_CTEF;
|
|
|
|
QUADSPI->AR = addr; // address to read from
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-22 05:07:01 +00:00
|
|
|
// Read in the data 4 bytes at a time if dest is aligned
|
|
|
|
if (((uintptr_t)dest & 3) == 0) {
|
|
|
|
while (len >= 4) {
|
|
|
|
while (!(QUADSPI->SR & QUADSPI_SR_FTF)) {
|
2021-03-04 23:15:29 +00:00
|
|
|
if (QUADSPI->SR & QUADSPI_SR_TEF) {
|
|
|
|
return -MP_EIO;
|
|
|
|
}
|
2018-06-22 05:07:01 +00:00
|
|
|
}
|
2020-02-27 04:36:53 +00:00
|
|
|
*(uint32_t *)dest = QUADSPI->DR;
|
2018-06-22 05:07:01 +00:00
|
|
|
dest += 4;
|
|
|
|
len -= 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read in remaining data 1 byte at a time
|
2018-03-11 07:28:48 +00:00
|
|
|
while (len) {
|
2018-06-22 05:07:01 +00:00
|
|
|
while (!((QUADSPI->SR >> QUADSPI_SR_FLEVEL_Pos) & 0x3f)) {
|
2021-03-04 23:15:29 +00:00
|
|
|
if (QUADSPI->SR & QUADSPI_SR_TEF) {
|
|
|
|
return -MP_EIO;
|
|
|
|
}
|
2018-03-11 07:28:48 +00:00
|
|
|
}
|
2020-02-27 04:36:53 +00:00
|
|
|
*dest++ = *(volatile uint8_t *)&QUADSPI->DR;
|
2018-06-22 05:07:01 +00:00
|
|
|
--len;
|
2018-03-11 07:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QUADSPI->FCR = QUADSPI_FCR_CTCF; // clear TC flag
|
2021-03-04 23:15:29 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-03-02 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mp_qspi_proto_t qspi_proto = {
|
|
|
|
.ioctl = qspi_ioctl,
|
|
|
|
.write_cmd_data = qspi_write_cmd_data,
|
|
|
|
.write_cmd_addr_data = qspi_write_cmd_addr_data,
|
|
|
|
.read_cmd = qspi_read_cmd,
|
2023-11-17 03:11:59 +00:00
|
|
|
.read_cmd_addr_data = qspi_read_cmd_addr_data,
|
2018-03-02 13:17:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // defined(MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2)
|