Skip to content

Commit 34d5ec1

Browse files
danielinuxdgarske
authored andcommitted
Fixed UART devices in NS-mode, fixed H5 clock
1 parent 059868e commit 34d5ec1

4 files changed

Lines changed: 95 additions & 61 deletions

File tree

hal/stm32h5.c

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "hal.h"
2727
#include "hal/stm32h5.h"
2828

29+
#define PLL_SRC_HSE 1
30+
2931
static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
3032
{
3133
uint32_t reg = FLASH_ACR;
@@ -230,14 +232,30 @@ static void clock_pll_on(void)
230232
uint32_t reg32;
231233
uint32_t plln, pllm, pllq, pllp, pllr, hpre, apb1pre, apb2pre, apb3pre, flash_waitstates;
232234

233-
/* Select clock parameters (CPU Speed = 125 MHz) */
235+
236+
#if PLL_SRC_HSE
234237
pllm = 4;
235-
plln = 125; /* TODO: increase to 250 MHz */
238+
plln = 250;
236239
pllp = 2;
237240
pllq = 2;
238241
pllr = 2;
242+
#else
243+
pllm = 1;
244+
plln = 129;
245+
pllp = 2;
246+
pllq = 2;
247+
pllr = 2;
248+
#endif
239249
flash_waitstates = 5;
240250

251+
/* Set voltage scaler */
252+
reg32 = PWR_VOSCR & (~PWR_VOS_MASK);
253+
PWR_VOSCR = reg32 | PWR_VOS_SCALE_0;
254+
255+
/* Wait until scale has changed */
256+
while ((PWR_VOSSR & PWR_VOSRDY) == 0)
257+
;
258+
241259
/* Disable PLL1 */
242260
RCC_CR &= ~RCC_CR_PLL1ON;
243261

@@ -248,8 +266,9 @@ static void clock_pll_on(void)
248266
/* Set flash wait states */
249267
flash_set_waitstates(flash_waitstates);
250268

269+
#if PLL_SRC_HSE
251270
/* PLL Oscillator configuration */
252-
RCC_CR |= RCC_CR_HSEON | RCC_CR_HSEBYP | RCC_CR_HSEEXT;
271+
RCC_CR |= RCC_CR_HSEON | RCC_CR_HSEBYP;
253272

254273
/* Wait until HSE is Ready */
255274
while ((RCC_CR & RCC_CR_HSERDY) == 0)
@@ -260,6 +279,26 @@ static void clock_pll_on(void)
260279
reg32 &= ~((0x3F << RCC_PLLCFGR_PLLM_SHIFT) | (0x03));
261280
reg32 |= (pllm << RCC_PLLCFGR_PLLM_SHIFT) | RCC_PLLCFGR_PLLSRC_HSE;
262281
RCC_PLL1CFGR = reg32;
282+
#else
283+
RCC_CR |= RCC_CR_HSION;
284+
285+
/* Wait until HSI is Ready */
286+
while ((RCC_CR & RCC_CR_HSIRDY) == 0)
287+
;
288+
289+
RCC_CR |= RCC_CR_CSION;
290+
291+
/* Wait until CSI is Ready */
292+
while ((RCC_CR & RCC_CR_HSIRDY) == 0)
293+
;
294+
295+
/* Configure PLL1 div/mul factors */
296+
reg32 = RCC_PLL1CFGR;
297+
reg32 &= ~((0x3F << RCC_PLLCFGR_PLLM_SHIFT) | (0x03));
298+
reg32 |= (pllm << RCC_PLLCFGR_PLLM_SHIFT) | RCC_PLLCFGR_PLLSRC_CSI;
299+
RCC_PLL1CFGR = reg32;
300+
301+
#endif
263302
DMB();
264303

265304
RCC_PLL1DIVR = ((plln - 1) << RCC_PLLDIVR_DIVN_SHIFT) | ((pllp - 1) << RCC_PLLDIVR_DIVP_SHIFT) |
@@ -281,7 +320,7 @@ static void clock_pll_on(void)
281320
DMB();
282321

283322
/* Select PLL1 Input frequency range: VCI */
284-
RCC_PLL1CFGR |= RCC_PLLCFGR_RGE_1_2 << RCC_PLLCFGR_PLLRGE_SHIFT;
323+
RCC_PLL1CFGR |= RCC_PLLCFGR_RGE_2_4 << RCC_PLLCFGR_PLLRGE_SHIFT;
285324

286325
/* Select PLL1 Output frequency range: VCO = 0 */
287326
RCC_PLL1CFGR &= ~RCC_PLLCFGR_PLLVCOSEL;
@@ -321,47 +360,8 @@ static void clock_pll_on(void)
321360
while ((RCC_CFGR1 & (RCC_CFGR1_SW_PLL1 << RCC_CFGR1_SWS_SHIFT)) == 0)
322361
;
323362

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-
;
363+
/* Set PLL1 as system clock */
364+
RCC_PLL1CFGR |= RCC_PLLCFGR_PLL1PEN;
365365

366366
}
367367

@@ -374,6 +374,12 @@ static void periph_unsecure(void)
374374
/*Enable clock for User LED GPIOs */
375375
RCC_AHB2_CLOCK_ER|= LED_AHB2_ENABLE;
376376

377+
/* Enable GPIO clock for accessing SECCFGR registers */
378+
RCC_AHB2_CLOCK_ER |= GPIOA_AHB2_CLOCK_ER;
379+
RCC_AHB2_CLOCK_ER |= GPIOB_AHB2_CLOCK_ER;
380+
RCC_AHB2_CLOCK_ER |= GPIOC_AHB2_CLOCK_ER;
381+
RCC_AHB2_CLOCK_ER |= GPIOD_AHB2_CLOCK_ER;
382+
377383
/* Enable clock for LPUART1 */
378384
RCC_APB2_CLOCK_ER |= UART1_APB2_CLOCK_ER_VAL;
379385

@@ -406,6 +412,15 @@ static void periph_unsecure(void)
406412
TZSC_SECCFGR1 = reg;
407413
}
408414

415+
/* Disable GPIOs clock used previously for accessing SECCFGR registers */
416+
#if 0
417+
RCC_AHB2_CLOCK_ER &= ~GPIOA_AHB2_CLOCK_ER;
418+
RCC_AHB2_CLOCK_ER &= ~GPIOB_AHB2_CLOCK_ER;
419+
RCC_AHB2_CLOCK_ER &= ~GPIOC_AHB2_CLOCK_ER;
420+
RCC_AHB2_CLOCK_ER &= ~GPIOD_AHB2_CLOCK_ER;
421+
#endif
422+
423+
409424
}
410425
#endif
411426

@@ -500,9 +515,12 @@ void hal_init(void)
500515

501516
void hal_prepare_boot(void)
502517
{
503-
clock_pll_off();
518+
519+
/* Keep clock settings when staging a NS-application */
504520
#if (TZ_SECURE())
505521
periph_unsecure();
522+
#else
523+
clock_pll_off();
506524
#endif
507525
}
508526

hal/stm32h5.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
#define RCC_PLL2FRACR (*(volatile uint32_t *)(RCC_BASE + 0x40)) /* RM0481 - Table 108 */
9898

9999
#define RCC_PLLCFGR_PLLSRC_SHIFT (0x0)
100+
#define RCC_PLLCFGR_PLLSRC_HSI (0x1)
101+
#define RCC_PLLCFGR_PLLSRC_CSI (0x2)
100102
#define RCC_PLLCFGR_PLLSRC_HSE (0x3)
101103
#define RCC_PLLCFGR_PLLRGE_SHIFT (0x2)
102104
#define RCC_PLLCFGR_RGE_1_2 (0x0) /* Default at boot: 1-2 MHz */
@@ -153,10 +155,10 @@
153155

154156
#define RCC_CCIPR1 (*(volatile uint32_t *)(RCC_BASE + 0xD8))
155157
#define RCC_CCIPR3 (*(volatile uint32_t *)(RCC_BASE + 0xE0))
156-
#define RCC_CCIPR3_LPUART1SEL_SHIFT (24)
157-
#define RCC_CCIPR3_LPUART1SEL_MASK (0x3)
158158
#define RCC_CCIPR1_USART3SEL_SHIFT (6)
159-
#define RCC_CCIPR1_USART3SEL_MASK (0x3)
159+
#define RCC_CCIPR1_USART3SEL_MASK (0x7)
160+
#define RCC_CCIPR3_LPUART1SEL_SHIFT (24)
161+
#define RCC_CCIPR3_LPUART1SEL_MASK (0x7)
160162

161163

162164
#define RCC_CRRCR (*(volatile uint32_t *)(RCC_BASE + 0x98))
@@ -167,10 +169,10 @@
167169
/*!< Memory & Instance aliases and base addresses for Non-Secure/Secure peripherals */
168170
#if TZ_SECURE()
169171
/*Secure */
170-
#define PWR_BASE (0x50020800) //RM0481 - Table 3
172+
#define PWR_BASE (0x54020800) //RM0481 - Table 3
171173
#else
172174
/*Non-Secure */
173-
#define PWR_BASE (0x40020800) //RM0481 - Table 3
175+
#define PWR_BASE (0x44020800) //RM0481 - Table 3
174176
#endif
175177

176178
#define PWR_VOSCR (*(volatile uint32_t *)(PWR_BASE + 0x10))
@@ -382,6 +384,7 @@
382384
#define UART1_ICR (*(volatile uint32_t *)(UART1 + 0x20))
383385
#define UART1_RDR (*(volatile uint32_t *)(UART1 + 0x24))
384386
#define UART1_TDR (*(volatile uint32_t *)(UART1 + 0x28))
387+
#define UART1_PRE (*(volatile uint32_t *)(UART1 + 0x2C))
385388

386389
#define UART3_CR1 (*(volatile uint32_t *)(UART3 + 0x00))
387390
#define UART3_CR2 (*(volatile uint32_t *)(UART3 + 0x04))
@@ -391,8 +394,10 @@
391394
#define UART3_ICR (*(volatile uint32_t *)(UART3 + 0x20))
392395
#define UART3_RDR (*(volatile uint32_t *)(UART3 + 0x24))
393396
#define UART3_TDR (*(volatile uint32_t *)(UART3 + 0x28))
397+
#define UART3_PRE (*(volatile uint32_t *)(UART3 + 0x2C))
394398

395399
#define UART_CR1_UART_ENABLE (1 << 0)
400+
#define UART_CR1_OVER8 (1 << 15)
396401
#define UART_CR1_SYMBOL_LEN (1 << 12)
397402
#define UART_CR1_PARITY_ENABLED (1 << 10)
398403
#define UART_CR1_PARITY_ODD (1 << 9)

hal/uart/uart_drv_stm32h5.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@
3030

3131
/* USE_UART1
3232
* Set to 0 for VCP over USB
33-
* Set to 1 for Arduino D0, D1 pins on nucleo
33+
* Set to 1 for Arduino D0, D1 pins on nucleo
3434
* */
3535
#define USE_UART1 0
3636

37+
#define RCC_AHB2ENR1_CLOCK_ER (*(volatile uint32_t *)(RCC_BASE + 0x8C ))
38+
#define GPIOB_AHB2ENR1_CLOCK_ER (1 << 1)
39+
#define GPIOD_AHB2ENR1_CLOCK_ER (1 << 3)
40+
3741

3842
#define GPIOB_MODE (*(volatile uint32_t *)(GPIOB_BASE + 0x00))
3943
#define GPIOB_OTYPE (*(volatile uint32_t *)(GPIOB_BASE + 0x04))
@@ -53,18 +57,18 @@
5357
#define GPIOD_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20))
5458
#define GPIOD_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24))
5559

56-
//#define CLOCK_FREQ (125000000)
5760
#define CLOCK_FREQ (64000000)
5861

5962
static void uart1_pins_setup(void)
6063
{
6164
uint32_t reg;
65+
RCC_AHB2ENR1_CLOCK_ER|= GPIOB_AHB2ENR1_CLOCK_ER;
6266
/* Set mode = AF */
6367
reg = GPIOB_MODE & ~ (0x03 << (UART1_RX_PIN * 2));
6468
GPIOB_MODE = reg | (2 << (UART1_RX_PIN * 2));
6569
reg = GPIOB_MODE & ~ (0x03 << (UART1_TX_PIN * 2));
6670
GPIOB_MODE = reg | (2 << (UART1_TX_PIN * 2));
67-
71+
6872
/* Alternate function: use low pins (6 and 7) */
6973
reg = GPIOB_AFL & ~(0xf << (UART1_TX_PIN * 4));
7074
GPIOB_AFL = reg | (UART1_PIN_AF << (UART1_TX_PIN * 4));
@@ -76,12 +80,13 @@ static void uart1_pins_setup(void)
7680
static void uart3_pins_setup(void)
7781
{
7882
uint32_t reg;
83+
RCC_AHB2ENR1_CLOCK_ER|= GPIOD_AHB2ENR1_CLOCK_ER;
7984
/* Set mode = AF */
8085
reg = GPIOD_MODE & ~ (0x03 << (UART3_RX_PIN * 2));
8186
GPIOD_MODE = reg | (2 << (UART3_RX_PIN * 2));
8287
reg = GPIOD_MODE & ~ (0x03 << (UART3_TX_PIN * 2));
8388
GPIOD_MODE = reg | (2 << (UART3_TX_PIN * 2));
84-
89+
8590
/* Alternate function: use hi pins (8 and 9) */
8691
reg = GPIOD_AFH & ~(0xf << ((UART3_TX_PIN - 8) * 4));
8792
GPIOD_AFH = reg | (UART3_PIN_AF << ((UART3_TX_PIN - 8) * 4));
@@ -97,10 +102,10 @@ static int uart1_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
97102
uart1_pins_setup();
98103

99104
reg = RCC_CCIPR3 & (~ (RCC_CCIPR3_LPUART1SEL_MASK << RCC_CCIPR3_LPUART1SEL_SHIFT));
100-
RCC_CCIPR3 = reg | (3 << RCC_CCIPR3_LPUART1SEL_SHIFT); /* PLL2 */
105+
RCC_CCIPR3 = reg | (0 << RCC_CCIPR3_LPUART1SEL_SHIFT); /* PLL2 */
101106

102107
/* Configure clock */
103-
UART1_BRR |= (uint16_t)(CLOCK_FREQ / bitrate);
108+
UART1_BRR |= (uint16_t)(CLOCK_FREQ / bitrate) + 1;
104109

105110
/* Configure data bits */
106111
if (data == 8)
@@ -126,7 +131,9 @@ static int uart1_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
126131
UART1_CR2 = reg & (2 << 12);
127132
else
128133
UART1_CR2 = reg;
129-
134+
135+
/* Prescaler to DIV1 */
136+
UART1_PRE |= 2;
130137

131138
/* Configure for RX+TX, turn on. */
132139
UART1_CR1 |= UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE | UART_CR1_UART_ENABLE;
@@ -163,10 +170,10 @@ static int uart3_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
163170
uart3_pins_setup();
164171

165172
reg = RCC_CCIPR1 & (~ (RCC_CCIPR1_USART3SEL_MASK << RCC_CCIPR1_USART3SEL_SHIFT));
166-
RCC_CCIPR1 = reg | (3 << RCC_CCIPR1_USART3SEL_SHIFT); /* PLL2 */
173+
RCC_CCIPR1 = reg | (0 << RCC_CCIPR1_USART3SEL_SHIFT); /* PLL2 */
167174

168175
/* Configure clock */
169-
UART3_BRR |= (uint16_t)(CLOCK_FREQ / bitrate);
176+
UART3_BRR = (uint16_t)(CLOCK_FREQ / bitrate) + 1;
170177

171178
/* Configure data bits */
172179
if (data == 8)
@@ -192,7 +199,9 @@ static int uart3_init(uint32_t bitrate, uint8_t data, char parity, uint8_t stop)
192199
UART3_CR2 = reg & (2 << 12);
193200
else
194201
UART3_CR2 = reg;
195-
202+
203+
/* Prescaler to DIV1 */
204+
UART3_PRE |= 2;
196205

197206
/* Configure for RX+TX, turn on. */
198207
UART3_CR1 |= UART_CR1_TX_ENABLE | UART_CR1_RX_ENABLE | UART_CR1_UART_ENABLE;

test-app/app_stm32h5.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ extern const CK_FUNCTION_LIST wolfpkcs11nsFunctionList;
6666
#define GPIOG_AHB2ENR1_CLOCK_ER (1 << 6)
6767
#define GPIOF_AHB2ENR1_CLOCK_ER (1 << 5)
6868
#define GPIOB_AHB2ENR1_CLOCK_ER (1 << 1)
69+
#define GPIOD_AHB2ENR1_CLOCK_ER (1 << 3)
6970

7071
static void boot_led_on(void)
7172
{
@@ -154,6 +155,7 @@ void main(void)
154155

155156
/* Turn on boot LED */
156157
boot_led_on();
158+
157159
uart_init(115200, 8, 'N', 1);
158160
for (i = 0; i < 10000; i++) {
159161
uart_tx('T');

0 commit comments

Comments
 (0)