Skip to content

Commit

Permalink
feat(core): enable LSE on T3W1
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
TychoVrahe committed Feb 28, 2025
1 parent 28a4b79 commit 0b8e138
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
38 changes: 37 additions & 1 deletion core/embed/sys/startup/stm32u5/startup_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,42 @@ void lsi_init(void) {
;
}

/*
* This function replaces calls to universal, but flash-wasting
* function HAL_RCC_OscConfig.
*
* This is the configuration before the optimization:
* osc_init_def.OscillatorType = RCC_OSCILLATORTYPE_LSE;
* osc_init_def.LSEState = RCC_LSE_ON;
* HAL_RCC_OscConfig(&osc_init_def);
*/
void lse_init(void) {
/* Update LSE configuration in Backup Domain control register */
/* Requires to enable write access to Backup Domain of necessary */

if (HAL_IS_BIT_CLR(PWR->DBPR, PWR_DBPR_DBP)) {
/* Enable write access to Backup domain */
SET_BIT(PWR->DBPR, PWR_DBPR_DBP);

while (HAL_IS_BIT_CLR(PWR->DBPR, PWR_DBPR_DBP))
;
}

/* LSE oscillator enable */
SET_BIT(RCC->BDCR, RCC_BDCR_LSEON);

/* Wait till LSE is ready */
while (READ_BIT(RCC->BDCR, RCC_BDCR_LSERDY) == 0U)
;

/* Make sure LSESYSEN/LSESYSRDY are reset */
CLEAR_BIT(RCC->BDCR, RCC_BDCR_LSESYSEN);

/* Wait till LSESYSRDY is cleared */
while (READ_BIT(RCC->BDCR, RCC_BDCR_LSESYSRDY) != 0U)
;
}

void SystemInit(void) {
// set flash wait states for an increasing HCLK frequency

Expand Down Expand Up @@ -228,7 +264,7 @@ void SystemInit(void) {
PWR->SVMCR |= PWR_SVMCR_IO2SV;

#ifdef USE_LSE
// TODO
lse_init();
#else
lsi_init();
#endif
Expand Down
18 changes: 10 additions & 8 deletions core/embed/sys/tamper/stm32u5/tamper.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
*
* This is the configuration before the optimization:
* clk_init_def.PeriphClockSelection = RCC_PERIPHCLK_RTC;
* clk_init_def.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
* HAL_RCCEx_PeriphCLKConfig(&clk_init_def);
* clk_init_def.RTCClockSelection = RCC_RTCCLKSOURCE_LSI (or
* RCC_RTCCLKSOURCE_LSE); HAL_RCCEx_PeriphCLKConfig(&clk_init_def);
*/
HAL_StatusTypeDef clk_init(void) {
HAL_StatusTypeDef clk_init(uint32_t source) {
uint32_t tickstart = 0U;

FlagStatus pwrclkchanged = RESET;
Expand All @@ -67,8 +67,7 @@ HAL_StatusTypeDef clk_init(void) {
* from default */
uint32_t bdcr_temp = READ_BIT(RCC->BDCR, RCC_BDCR_RTCSEL);

if ((bdcr_temp != RCC_RTCCLKSOURCE_NO_CLK) &&
(bdcr_temp != RCC_RTCCLKSOURCE_LSI)) {
if ((bdcr_temp != RCC_RTCCLKSOURCE_NO_CLK) && (bdcr_temp != source)) {
/* Store the content of BDCR register before the reset of Backup Domain */
bdcr_temp = READ_BIT(RCC->BDCR, ~(RCC_BDCR_RTCSEL));
/* RTC Clock selection can be changed only if the Backup Domain is reset */
Expand All @@ -92,7 +91,7 @@ HAL_StatusTypeDef clk_init(void) {
}

/* Apply new RTC clock source selection */
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
__HAL_RCC_RTC_CONFIG(source);

/* Restore clock configuration if changed */
if (pwrclkchanged == SET) {
Expand All @@ -102,8 +101,11 @@ HAL_StatusTypeDef clk_init(void) {
}

void tamper_init(void) {
// TODO LSE
clk_init();
#ifdef USE_LSE
clk_init(RCC_RTCCLKSOURCE_LSE);
#else
clk_init(RCC_RTCCLKSOURCE_LSI);
#endif

// Enable RTC peripheral (tampers are part of it)
__HAL_RCC_RTC_ENABLE();
Expand Down
1 change: 1 addition & 0 deletions core/site_scons/models/T3W1/trezor_t3w1_revA.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def configure(
("HW_REVISION", str(hw_revision)),
("HSE_VALUE", "32000000"),
("USE_HSE", "1"),
("USE_LSE", "1"),
("FIXED_HW_DEINIT", "1"),
]

Expand Down
1 change: 1 addition & 0 deletions core/site_scons/models/T3W1/trezor_t3w1_revA0.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def configure(
("HW_REVISION", str(hw_revision)),
("HSE_VALUE", "32000000"),
("USE_HSE", "1"),
("USE_LSE", "1"),
("FIXED_HW_DEINIT", "1"),
]

Expand Down
1 change: 1 addition & 0 deletions core/site_scons/models/T3W1/trezor_t3w1_revB.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def configure(
("HW_REVISION", str(hw_revision)),
("HSE_VALUE", "32000000"),
("USE_HSE", "1"),
("USE_LSE", "1"),
("FIXED_HW_DEINIT", "1"),
]

Expand Down

0 comments on commit 0b8e138

Please sign in to comment.