Skip to content

Commit 24fdeb5

Browse files
committed
[QSPI] Fixed registers and pin config on STM32U5
1 parent 6f83a79 commit 24fdeb5

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-10
lines changed

hal/spi/spi_drv_stm32.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,13 @@ static void stm_pins_release(void)
185185
static void RAMFUNCTION spi_reset(void)
186186
{
187187
#if defined(QSPI_FLASH) || defined(OCTOSPI_FLASH)
188-
AHB3_CLOCK_RST |= RCC_AHB3ENR_QSPIEN;
189-
AHB3_CLOCK_RST &= ~RCC_AHB3ENR_QSPIEN;
188+
#ifndef TARGET_stm32u5
189+
AHB3_CLOCK_RST |= RCC_AHB3ENR_QSPIEN;
190+
AHB3_CLOCK_RST &= ~RCC_AHB3ENR_QSPIEN;
191+
#else
192+
AHB2_CLOCK_RST |= RCC_AHB2ENR_QSPIEN;
193+
AHB2_CLOCK_RST &= ~RCC_AHB2ENR_QSPIEN;
194+
#endif
190195
#endif
191196
#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM)
192197
APB2_CLOCK_RST |= SPI1_APB2_CLOCK_ER_VAL;
@@ -390,13 +395,21 @@ void RAMFUNCTION spi_init(int polarity, int phase)
390395
if (!initialized) {
391396
initialized++;
392397

393-
/* Setup clocks */
398+
/* Setup clocks */
394399
#if defined(QSPI_FLASH) || defined(OCTOSPI_FLASH)
400+
401+
#ifdef PLATFORM_stm32u5
402+
/* Clock configuration for QSPI defaults to SYSCLK
403+
* (RM0456 section 11.8.47)
404+
*/
405+
#else
395406
/* Select QUADSPI clock source */
396407
RCC_D1CCIPR &= ~RCC_D1CCIPR_QSPISEL_MASK;
397408
RCC_D1CCIPR |= RCC_D1CCIPR_QSPISEL(QSPI_CLOCK_SEL);
398409
AHB3_CLOCK_EN |= RCC_AHB3ENR_QSPIEN;
410+
#endif
399411
#endif
412+
400413
#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM)
401414
APB2_CLOCK_ER |= SPI1_APB2_CLOCK_ER_VAL;
402415
#endif

hal/spi/spi_drv_stm32.h

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,30 @@
4040
#define SPI_MOSI_PIN 5 /* SPI_MOSI PB5 */
4141
#endif /* PLATFORM_stm32f4 */
4242

43-
4443
#ifdef PLATFORM_stm32u5
44+
4545
#ifdef TZEN
4646
#define PERIPH_BASE (0x50000000UL)
4747
#else
4848
#define PERIPH_BASE (0x40000000UL)
4949
#endif
5050

51-
#define APB2PERIPH_BASE (PERIPH_BASE + 0x00010000UL)
51+
/* Registers mapping */
52+
#define APB2PERIPH_BASE (PERIPH_BASE + 0x00012C00UL)
53+
#define RCC_BASE (PERIPH_BASE + 0x06020C00UL)
54+
#define GPIO_BASE (PERIPH_BASE + 0x02020000UL)
5255
#define AHB2PERIPH_BASE (PERIPH_BASE + 0x02020000UL)
53-
54-
#define SPI1_BASE (APB2PERIPH_BASE + 0x3000UL)
55-
56-
#define RCC_BASE (APB2PERIPH_BASE + 0x0C00UL)
56+
#define SPI1_BASE (APB2PERIPH_BASE + 0x3000UL)
5757
#define APB2_CLOCK_ER (*(volatile uint32_t *)(RCC_BASE + 0xA4))
5858
#define APB2_CLOCK_RST (*(volatile uint32_t *)(RCC_BASE + 0x7C))
59+
#define AHB2_CLOCK_ER (*(volatile uint32_t *)(RCC_BASE + 0x90))
60+
#define AHB2_CLOCK_RST (*(volatile uint32_t *)(RCC_BASE + 0x68))
61+
5962
#define RCC_GPIO_CLOCK_ER (*(volatile uint32_t *)(RCC_BASE + 0x8C))
6063

61-
#define GPIO_BASE (APB2PERIPH_BASE + 0x02020000UL)
64+
65+
#define RCC_AHB2ENR_QSPIEN (1 << 8) /* OCTOSPI2 = bit 8, OCTOSPI1 = bit 4 */
66+
6267
#define GPIOA_BASE (GPIO_BASE + 0x00000UL)
6368
#define GPIOB_BASE (GPIO_BASE + 0x00400UL)
6469
#define GPIOC_BASE (GPIO_BASE + 0x00800UL)
@@ -69,6 +74,64 @@
6974
#define GPIOH_BASE (GPIO_BASE + 0x01C00UL)
7075
#define GPIOI_BASE (GPIO_BASE + 0x02000UL)
7176

77+
/** QSPI **/
78+
/* Default Base (H) and AF (alternate function=5) for QSPI */
79+
#define QSPI_GPIO GPIOH_BASE
80+
#define QSPI_PIN_AF 5
81+
82+
#ifndef QSPI_CLOCK_BASE
83+
#define QSPI_CLOCK_BASE 160000000
84+
#endif
85+
86+
#ifndef QSPI_CLOCK_MHZ
87+
#define QSPI_CLOCK_MHZ 10000000
88+
#endif
89+
90+
/* Default to flash bank 1 */
91+
#ifndef QSPI_FLASH_BANK
92+
#define QSPI_FLASH_BANK 1
93+
#endif
94+
95+
/* Default flash size to 16MB */
96+
#ifndef QSPI_FLASH_SIZE
97+
#define QSPI_FLASH_SIZE 23 /* 2^24 = 16 MB */
98+
#endif
99+
100+
/* QSPI CLK PB2 (alt OCTOSPIM_P1_CLK)*/
101+
#define QSPI_CLOCK_PIO_BASE GPIOH_BASE
102+
#define QSPI_CLOCK_PIN 6
103+
#define QSPI_CLOCK_PIN_AF 5
104+
105+
/* nQSPI_CS PG6 (alt PB6 -> OCTOSPIM_P1_NCS) */
106+
#ifndef QSPI_ALT_CONFIGURATION
107+
#define QSPI_CS_PIO_BASE GPIOH_BASE
108+
#define QSPI_CS_FLASH_PIN 5
109+
#else
110+
#define QSPI_CS_PIO_BASE GPIOH_BASE
111+
#define QSPI_CS_FLASH_PIN 6
112+
#endif
113+
114+
/* QSPI_IO0 */
115+
#define QSPI_IO0_PIO_BASE GPIOH_BASE
116+
#define QSPI_IO0_PIN 9
117+
#define QSPI_IO0_PIN_AF 5
118+
119+
/* QSPI_IO1 */
120+
#define QSPI_IO1_PIO_BASE GPIOH_BASE
121+
#define QSPI_IO1_PIN 10
122+
#define QSPI_IO1_PIN_AF 5
123+
124+
/* QSPI_IO2 */
125+
#define QSPI_IO2_PIO_BASE GPIOH_BASE
126+
#define QSPI_IO2_PIN 11
127+
#define QSPI_IO2_PIN_AF 5
128+
129+
/* QSPI_IO3 */
130+
#define QSPI_IO3_PIO_BASE GPIOH_BASE
131+
#define QSPI_IO3_PIN 12
132+
#define QSPI_IO3_PIN_AF 5
133+
134+
72135
/* STMOD+ Port */
73136
#define SPI_GPIO GPIOE_BASE
74137
#define SPI_CS_GPIO GPIOE_BASE

0 commit comments

Comments
 (0)