Skip to content

Commit 839118e

Browse files
authored
Interrupt management optimize and minor fixes (#282)
* minor version header fix * interrupt management optimize and cleanup --------- Signed-off-by: Adam BZH <adam@onekey.so>
1 parent 25dfa64 commit 839118e

File tree

6 files changed

+51
-34
lines changed

6 files changed

+51
-34
lines changed

core/embed/bootloader/main.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,23 @@ int main(void) {
648648
// mpu may already running
649649
mpu_ctrl(sectrue); // ensure enabled
650650

651+
// disable all external communication or user input irq
652+
// will be re-enabled later by calling their init function
653+
// bluetooth uart
654+
HAL_NVIC_DisableIRQ(UART4_IRQn);
655+
HAL_NVIC_ClearPendingIRQ(UART4_IRQn);
656+
// bluetooth spi
657+
HAL_NVIC_DisableIRQ(SPI2_IRQn);
658+
HAL_NVIC_ClearPendingIRQ(SPI2_IRQn);
659+
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
660+
HAL_NVIC_ClearPendingIRQ(EXTI15_10_IRQn);
661+
// usb
662+
HAL_NVIC_DisableIRQ(OTG_HS_IRQn);
663+
HAL_NVIC_ClearPendingIRQ(OTG_HS_IRQn);
664+
665+
__enable_irq();
666+
__enable_fault_irq();
667+
651668
lcd_ltdc_dsi_disable();
652669
sdram_reinit();
653670
// lcd_para_init(DISPLAY_RESX, DISPLAY_RESY, LCD_PIXEL_FORMAT_RGB565);
@@ -789,12 +806,10 @@ int main(void) {
789806
}
790807

791808
display_clear();
792-
793809
bus_fault_disable();
794810

795-
// Disable SPI Chip Select interrupt for compatibility with older firmware
796-
// versions
797-
spi_disable_cs_irq();
811+
__disable_irq();
812+
__disable_fault_irq();
798813

799814
// enable firmware region
800815
mpu_config_firmware(sectrue, sectrue);

core/embed/bootloader/startup.s

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
.global reset_handler
66
.type reset_handler, STT_FUNC
77
reset_handler:
8+
9+
// disable all irq and exceptions
10+
cpsid if
11+
812
// setup environment for subsequent stage of code
913
ldr r0, =axiram_start // r0 - point to beginning of axiram
1014
ldr r1, =axiram_end // r1 - point to byte after the end of axiram
@@ -27,12 +31,6 @@ reset_handler:
2731
ldr r1, = __stack_chk_guard
2832
str r0, [r1]
2933

30-
// re-enable exceptions
31-
// according to "ARM Cortex-M Programming Guide to Memory Barrier Instructions" Application Note 321, section 4.7:
32-
// "If it is not necessary to ensure that a pended interrupt is recognized immediately before
33-
// subsequent operations, it is not necessary to insert a memory barrier instruction."
34-
cpsie f
35-
3634
// enter the application code
3735
bl main
3836

core/embed/firmware/main.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ static void copyflash2sdram(void) {
9494
}
9595

9696
int main(void) {
97-
extern uint32_t _vector_offset;
98-
SCB->VTOR = (uint32_t)&_vector_offset;
97+
SystemCoreClockUpdate();
98+
dwt_init();
9999

100100
mpu_config_boardloader(sectrue, secfalse);
101101
mpu_config_bootloader(sectrue, secfalse);
@@ -104,8 +104,24 @@ int main(void) {
104104
// mpu may already running
105105
mpu_ctrl(sectrue); // ensure enabled
106106

107-
SystemCoreClockUpdate();
108-
dwt_init();
107+
// disable all external communication or user input irq
108+
// will be re-enabled later by calling their init function
109+
// bluetooth uart
110+
HAL_NVIC_DisableIRQ(UART4_IRQn);
111+
HAL_NVIC_ClearPendingIRQ(UART4_IRQn);
112+
// bluetooth spi
113+
HAL_NVIC_DisableIRQ(SPI2_IRQn);
114+
HAL_NVIC_ClearPendingIRQ(SPI2_IRQn);
115+
HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
116+
HAL_NVIC_ClearPendingIRQ(EXTI15_10_IRQn);
117+
// usb
118+
HAL_NVIC_DisableIRQ(OTG_HS_IRQn);
119+
HAL_NVIC_ClearPendingIRQ(OTG_HS_IRQn);
120+
121+
// re-enable global irq
122+
__enable_irq();
123+
__enable_fault_irq();
124+
109125
lcd_ltdc_dsi_disable();
110126
sdram_reinit();
111127
// lcd_para_init(DISPLAY_RESX, DISPLAY_RESY, LCD_PIXEL_FORMAT_RGB565);

core/embed/firmware/startup.S

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@
66
.type reset_handler, STT_FUNC
77
reset_handler:
88

9-
// The following loading of VTOR address only works if T1 bootloader was built with PRODUCTION=0
10-
// or the firmware was properly signed. All other variants end up in hard fault due to MPU
11-
// (cf mpu_config_firmware in legacy bootloader)
12-
13-
#if defined TREZOR_MODEL_1
149
cpsid if
1510
ldr r0, =0xE000ED08 // r0 = VTOR address
16-
ldr r1, =0x08010400 // r1 = FLASH_APP_START
11+
ldr r1, =_vector_offset // r1 = FLASH_APP_START
1712
str r1, [r0] // assign
1813

1914
ldr r0, =_estack // r0 = stack pointer
2015
msr msp, r0 // set stack pointer
2116
dsb
2217
isb
23-
#endif
2418

2519
// setup environment for subsequent stage of code
2620
ldr r0, =axiram_start // r0 - point to beginning of axiram
@@ -44,16 +38,6 @@ reset_handler:
4438
ldr r1, = __stack_chk_guard
4539
str r0, [r1]
4640

47-
// re-enable exceptions
48-
// according to "ARM Cortex-M Programming Guide to Memory Barrier Instructions" Application Note 321, section 4.7:
49-
// "If it is not necessary to ensure that a pended interrupt is recognized immediately before
50-
// subsequent operations, it is not necessary to insert a memory barrier instruction."
51-
#if defined TREZOR_MODEL_T || defined TREZOR_MODEL_R
52-
cpsie f
53-
#elif defined TREZOR_MODEL_1
54-
cpsie if
55-
#endif
56-
5741
// enter the application code
5842
bl main
5943

core/embed/firmware/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define VERSION_PATCH 99
44
#define VERSION_BUILD 99
55

6-
#define FIX_VERSION_MAJOR VERSION_MINOR
6+
#define FIX_VERSION_MAJOR VERSION_MAJOR
77
#define FIX_VERSION_MINOR VERSION_MINOR
88
#define FIX_VERSION_PATCH VERSION_PATCH
99
#define FIX_VERSION_BUILD VERSION_BUILD

core/embed/trezorhal/usart.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ void ble_usart_init(void) {
8989
hdma_tx.Init.Priority = DMA_PRIORITY_MEDIUM;
9090

9191
HAL_DMA_Init(&hdma_tx);
92-
9392
__HAL_LINKDMA(huart, hdmatx, hdma_tx);
9493

9594
hdma_rx.Instance = UARTx_RX_DMA_STREAM;
@@ -105,17 +104,22 @@ void ble_usart_init(void) {
105104
hdma_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
106105

107106
HAL_DMA_Init(&hdma_rx);
108-
109107
__HAL_LINKDMA(huart, hdmarx, hdma_rx);
110108

109+
// clear all on going tx/rx
110+
HAL_UART_Abort(huart);
111+
111112
/*##-4- Configure the NVIC for DMA #########################################*/
112113
NVIC_SetPriority(UARTx_DMA_RX_IRQn, IRQ_PRI_DMA);
114+
HAL_NVIC_ClearPendingIRQ(UARTx_DMA_RX_IRQn);
113115
HAL_NVIC_EnableIRQ(UARTx_DMA_RX_IRQn);
114116

115117
NVIC_SetPriority(UARTx_DMA_TX_IRQn, IRQ_PRI_DMA);
118+
HAL_NVIC_ClearPendingIRQ(UARTx_DMA_TX_IRQn);
116119
HAL_NVIC_EnableIRQ(UARTx_DMA_TX_IRQn);
117120

118121
NVIC_SetPriority(UART4_IRQn, IRQ_PRI_UART);
122+
HAL_NVIC_ClearPendingIRQ(UART4_IRQn);
119123
HAL_NVIC_EnableIRQ(UART4_IRQn);
120124

121125
__HAL_UART_ENABLE_IT(huart, UART_IT_IDLE);

0 commit comments

Comments
 (0)