Skip to content

Commit 059868e

Browse files
danielinuxdgarske
authored andcommitted
STM32H5 uart support, work in progress
1 parent 78c9e11 commit 059868e

6 files changed

Lines changed: 454 additions & 70 deletions

File tree

hal/stm32_tz.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,11 @@ void hal_tz_sau_init(void)
255255
/* Non-secure: internal peripherals */
256256
sau_init_region(5, 0x40000000, 0x4FFFFFFF, 0);
257257

258+
/* Secure mapped peripherals */
259+
sau_init_region(6, 0x50000000, 0x5FFFFFFF, 1);
260+
258261
/* Set as non-secure: OTP + RO area */
259-
sau_init_region(6, 0x08FFF000, 0x08FFFFFF, 0);
262+
sau_init_region(7, 0x08FFF000, 0x08FFFFFF, 0);
260263

261264
/* Enable SAU */
262265
SAU_CTRL = SAU_INIT_CTRL_ENABLE;

hal/stm32h5.c

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,26 @@ static void clock_pll_off(void)
201201
/* Select HSI as SYSCLK source. */
202202
RCC_CFGR1 &= ~(0x07 << RCC_CFGR1_SW_SHIFT);
203203
DMB();
204+
204205
/* Turn off PLL1 */
205-
RCC_PLL1CFGR &= ~RCC_PLL1CFGR_PLL1PEN;
206+
RCC_PLL1CFGR &= ~RCC_PLLCFGR_PLL1PEN;
206207
DMB();
207208
RCC_CR &= ~RCC_CR_PLL1ON;
208209
DMB();
209-
210210
/* Wait until PLL1 is disabled */
211211
while ((RCC_CR & RCC_CR_PLL1RDY) != 0)
212212
;
213213

214+
/* Turn off PLL2 */
215+
RCC_PLL2CFGR &= ~RCC_PLLCFGR_PLLPEN;
216+
DMB();
217+
RCC_CR &= ~RCC_CR_PLL2ON;
218+
DMB();
219+
/* Wait until PLL2 is disabled */
220+
while ((RCC_CR & RCC_CR_PLL2RDY) != 0)
221+
;
222+
223+
214224
}
215225

216226
/*This implementation will setup MSI 48 MHz as PLL Source Mux, PLLCLK as System Clock Source*/
@@ -247,18 +257,18 @@ static void clock_pll_on(void)
247257

248258
/* Configure PLL1 div/mul factors */
249259
reg32 = RCC_PLL1CFGR;
250-
reg32 &= ~((0x3F << RCC_PLL1CFGR_PLL1M_SHIFT) | (0x03));
251-
reg32 |= (pllm << RCC_PLL1CFGR_PLL1M_SHIFT) | RCC_PLL1CFGR_PLL1SRC_HSE;
260+
reg32 &= ~((0x3F << RCC_PLLCFGR_PLLM_SHIFT) | (0x03));
261+
reg32 |= (pllm << RCC_PLLCFGR_PLLM_SHIFT) | RCC_PLLCFGR_PLLSRC_HSE;
252262
RCC_PLL1CFGR = reg32;
253263
DMB();
254264

255-
RCC_PLL1DIVR = ((plln - 1) << RCC_PLL1DIVR_DIVN_SHIFT) | ((pllp - 1) << RCC_PLL1DIVR_DIVP_SHIFT) |
256-
((pllq - 1) << RCC_PLL1DIVR_DIVQ_SHIFT) | ((pllr - 1) << RCC_PLL1DIVR_DIVR_SHIFT);
265+
RCC_PLL1DIVR = ((plln - 1) << RCC_PLLDIVR_DIVN_SHIFT) | ((pllp - 1) << RCC_PLLDIVR_DIVP_SHIFT) |
266+
((pllq - 1) << RCC_PLLDIVR_DIVQ_SHIFT) | ((pllr - 1) << RCC_PLLDIVR_DIVR_SHIFT);
257267
DMB();
258268

259269

260270
/* Disable Fractional PLL */
261-
RCC_PLL1CFGR &= ~RCC_PLL1CFGR_PLL1FRACEN;
271+
RCC_PLL1CFGR &= ~RCC_PLLCFGR_PLLFRACEN;
262272
DMB();
263273

264274

@@ -267,18 +277,18 @@ static void clock_pll_on(void)
267277
DMB();
268278

269279
/* Enable Fractional PLL */
270-
RCC_PLL1CFGR |= RCC_PLL1CFGR_PLL1FRACEN;
280+
RCC_PLL1CFGR |= RCC_PLLCFGR_PLLFRACEN;
271281
DMB();
272282

273283
/* Select PLL1 Input frequency range: VCI */
274-
RCC_PLL1CFGR |= RCC_PLL1CFGR_RGE_1_2 << RCC_PLL1CFGR_PLL1RGE_SHIFT;
284+
RCC_PLL1CFGR |= RCC_PLLCFGR_RGE_1_2 << RCC_PLLCFGR_PLLRGE_SHIFT;
275285

276286
/* Select PLL1 Output frequency range: VCO = 0 */
277-
RCC_PLL1CFGR &= ~RCC_PLL1CFGR_PLL1VCOSEL;
287+
RCC_PLL1CFGR &= ~RCC_PLLCFGR_PLLVCOSEL;
278288
DMB();
279289

280290
/* Enable PLL1 system clock out (DIV: P) */
281-
RCC_PLL1CFGR |= RCC_PLL1CFGR_PLL1PEN;
291+
RCC_PLL1CFGR |= RCC_PLLCFGR_PLL1PEN;
282292

283293
/* Enable PLL1 */
284294
RCC_CR |= RCC_CR_PLL1ON;
@@ -311,32 +321,90 @@ static void clock_pll_on(void)
311321
while ((RCC_CFGR1 & (RCC_CFGR1_SW_PLL1 << RCC_CFGR1_SWS_SHIFT)) == 0)
312322
;
313323

324+
/* Configure PLL2 div/mul factors */
325+
reg32 = RCC_PLL2CFGR;
326+
reg32 &= ~((0x3F << RCC_PLLCFGR_PLLM_SHIFT) | (0x03));
327+
reg32 |= (pllm << RCC_PLLCFGR_PLLM_SHIFT) | RCC_PLLCFGR_PLLSRC_HSE;
328+
RCC_PLL2CFGR = reg32;
329+
DMB();
330+
331+
RCC_PLL2DIVR = ((plln - 1) << RCC_PLLDIVR_DIVN_SHIFT) | ((pllp - 1) << RCC_PLLDIVR_DIVP_SHIFT) |
332+
((pllq - 1) << RCC_PLLDIVR_DIVQ_SHIFT) | ((pllr - 1) << RCC_PLLDIVR_DIVR_SHIFT);
333+
DMB();
334+
335+
336+
/* Disable Fractional PLL */
337+
RCC_PLL2CFGR &= ~RCC_PLLCFGR_PLLFRACEN;
338+
DMB();
339+
340+
341+
/* Configure Fractional PLL factor */
342+
RCC_PLL2FRACR = 0x00000000;
343+
DMB();
344+
345+
/* Enable Fractional PLL */
346+
RCC_PLL2CFGR |= RCC_PLLCFGR_PLLFRACEN;
347+
DMB();
348+
349+
/* Select PLL2 Input frequency range: VCI */
350+
RCC_PLL2CFGR |= RCC_PLLCFGR_RGE_1_2 << RCC_PLLCFGR_PLLRGE_SHIFT;
351+
352+
/* Select PLL2 Output frequency range: VCO = 0 */
353+
RCC_PLL2CFGR &= ~RCC_PLLCFGR_PLLVCOSEL;
354+
DMB();
355+
356+
/* Enable PLL2 system clock out (DIV: P) */
357+
RCC_PLL2CFGR |= RCC_PLLCFGR_PLLPEN;
358+
359+
/* Enable PLL2 */
360+
RCC_CR |= RCC_CR_PLL2ON;
361+
362+
/* Wait until PLL2 is Ready */
363+
while ((RCC_CR & RCC_CR_PLL2RDY) == 0)
364+
;
365+
314366
}
315367

316368
#if (TZ_SECURE())
317369
static void periph_unsecure(void)
318370
{
319371
uint32_t pin;
372+
volatile uint32_t reg;
320373

321374
/*Enable clock for User LED GPIOs */
322375
RCC_AHB2_CLOCK_ER|= LED_AHB2_ENABLE;
323376

324377
/* Enable clock for LPUART1 */
325378
RCC_APB2_CLOCK_ER |= UART1_APB2_CLOCK_ER_VAL;
326379

380+
/* Enable clock for USART3 */
381+
RCC_APB1L_CLOCK_ER |= UART3_APB1L_CLOCK_ER_VAL;
382+
327383

328384
PWR_CR2 |= PWR_CR2_IOSV;
329385
/*Un-secure User LED GPIO pins */
330386
GPIO_SECCFGR(GPIOG_BASE) &= ~(1 << 4);
331387
GPIO_SECCFGR(GPIOB_BASE) &= ~(1 << 0);
332388
GPIO_SECCFGR(GPIOF_BASE) &= ~(1 << 4);
333389

334-
#if 0
335390
/* Unsecure LPUART1 */
336-
TZSC_PRIVCFGR2 &= ~(TZSC_PRIVCFG2_LPUARTPRIV);
337-
GPIO_SECCFGR(GPIOG_BASE) &= ~(1<<UART1_TX_PIN);
338-
GPIO_SECCFGR(GPIOG_BASE) &= ~(1<<UART1_RX_PIN);
339-
#endif
391+
GPIO_SECCFGR(GPIOB_BASE) &= ~(1<<UART1_TX_PIN);
392+
GPIO_SECCFGR(GPIOB_BASE) &= ~(1<<UART1_RX_PIN);
393+
reg = TZSC_SECCFGR2;
394+
if (reg & TZSC_SECCFGR2_LPUART1SEC) {
395+
reg &= (~TZSC_SECCFGR2_LPUART1SEC);
396+
DMB();
397+
TZSC_SECCFGR2 = reg;
398+
}
399+
/* Unsecure USART3 */
400+
GPIO_SECCFGR(GPIOD_BASE) &= ~(1<<UART3_TX_PIN);
401+
GPIO_SECCFGR(GPIOD_BASE) &= ~(1<<UART3_RX_PIN);
402+
reg = TZSC_SECCFGR1;
403+
if (reg & TZSC_SECCFGR1_USART3SEC) {
404+
reg &= (~TZSC_SECCFGR1_USART3SEC);
405+
DMB();
406+
TZSC_SECCFGR1 = reg;
407+
}
340408

341409
}
342410
#endif
@@ -412,6 +480,7 @@ static void fork_bootloader(void)
412480
}
413481
#endif
414482

483+
#include "uart_drv.h"
415484
void hal_init(void)
416485
{
417486
#if TZ_SECURE()
@@ -420,8 +489,6 @@ void hal_init(void)
420489
#endif
421490
clock_pll_on();
422491

423-
424-
425492
#if defined(DUALBANK_SWAP) && defined(__WOLFBOOT)
426493
if ((FLASH_OPTSR_CUR & (FLASH_OPTSR_SWAP_BANK)) == 0)
427494
fork_bootloader();

0 commit comments

Comments
 (0)