上一个帖子里说到,按照教程里的步骤设置完以后,看不到EXTI Line 13,看到的是14。这个问题的解决办法是在“Pinout view”里设置一下:
但接下来还有问题——在外部中断13(也包括14)的右边勾选框里,却不能选中,但在下方的图例中却有提示——灰色标示“Enabled”:
既然是这样,不妨继续往下走,生成的工程代码是这样(看左上角):
教程(https://www.stmcu.com.cn/ecosystem/chip/chipfamily-STM32L5-4)里的设想是这样:
1、PC7(绿色LED)和 PB7(蓝色LED)作为指示灯分别设置到S(安全)和NS(非安全)区域;
2、PC13(蓝色的用户按键开关)作为外部中断的接入口;
3、PG7和PG8分别作为LPUART1的TX和RX,用来实现PC与mcu的交互。设在NS非安全区;
4、在安全和非安全区的中断服务子程序(EXTI_ISR()中设置同一段代码——翻转蓝绿两个LED。
实际要看到的效果是:默认的情况下,EXTI 13的中断指向安全区,但是,通过API调用Secure代码,可以重置EXTI 13的指向——安全区或者非安全区的NVIC。这样,就能观察到,安全区内的EXTI_ISR()对两个灯都有效,而非安全区,仅对一个灯有效。
本帖最后由 MianQi 于 2020-12-29 10:53 编辑“项目配置代码生成”按键在这里:(左上角)
生成的项目文件树是这样:
注意:安全区比非安全区多了一个文件:secure_nsc.c
按照教程(https://www.stmcu.com.cn/ecosystem/chip/chipfamily-STM32L5-4)里的说明:在S(安全)区和NS(非安全)区分别做两项工作:
安全区:MX_MX_GPIO_Init() 和 MX_GTZC_Init()
非安全区:MX_MX_GPIO_Init() 和 MX_LPUART1_UART_Init()
本帖最后由 MianQi 于 2020-12-31 17:32 编辑在S和 NS区各自添加两段代码:第一段代码是相同的,都是“EXTI.13_ISR”,位置都在main.c的这里:
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
代码的内容是:
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
UNUSED(GPIO_Pin);
HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
HAL_GPIO_TogglePin(LED_BLUE_GPIO_Port, LED_BLUE_Pin);
}
第二段代码,S区的内容是:
CMSE_NS_ENTRY void SECURE_RetargetISR(uint8_t flag)
{
if (flag == 0x31)
{
NVIC_DisableIRQ (EXTI13_IRQn);
HAL_EXTI_ConfigLineAttributes(EXTI_LINE_13, EXTI_LINE_NSEC);
NVIC_SetTargetState (EXTI13_IRQn);
NVIC_EnableIRQ (EXTI13_IRQn);
}
else if (flag == 0x30)
{
NVIC_DisableIRQ(EXTI13_IRQn);
HAL_EXTI_ConfigLineAttributes (EXTI_LINE_13, EXTI_LINE_SEC);
NVIC_ClearTargetState (EXTI13_IRQn);
NVIC_EnableIRQ (EXTI13_IRQn);
}
}
目的是:实现供NS调用的切换EXTI.13 target函数。位置在secure_nsc.c里。
NS区的内容是:
while (HAL_UART_Receive (&hlpuart1, &InputChar, 1, 1000) == HAL_OK)
{
if (InputChar != Status)
{
if (InputChar == 0x30)
HAL_UART_Transmit (&hlpuart1, “\r\n to be switched to S world.”);
else if (InputChar == 0x31)
HAL_UART_Transmit (&hlpuart1, “\r\n to be switched to NS world.”);
SECURE_RetargetISR (InputChar);
Status = InputChar;
if (Status = 0x30)
HAL_UART_Transmit(&hlpuart1, “\r\n User Button has been switched to S world.”);
else if (Status == 0x31)
HAL_UART_Transmit (&hlpuart1, “\r\n User Button has been switched to NS world.”);
else
HAL_UART_Transmit (&hlpuart1, “\r\n the same state, do nothing. \n”);
}
位置在:main.c里的while(1)中。
插上板子,会提示“NODE_L552ZE”。
编译,会报错:
08:02:08 **** Incremental Build of configuration Debug for project test_TrustZone-1_NonSecure ****
make -j8 all
arm-none-eabi-gcc "../Core/Src/main.c" -mcpu=cortex-m33 -std=gnu11 -g3 -DUSE_HAL_DRIVER -DSTM32L552xx -DDEBUG -c -I../Core/Inc -I../../Secure_nsclib -I../../Drivers/STM32L5xx_HAL_Driver/Inc -I../../Drivers/CMSIS/Device/ST/STM32L5xx/Include -I../../Drivers/STM32L5xx_HAL_Driver/Inc/Legacy -I../../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -MMD -MP -MF"Core/Src/main.d" -MT"Core/Src/main.o" --specs=nano.specs -mfpu=fpv5-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/main.o"
../Core/Src/main.c: In function 'main':
../Core/Src/main.c:102:41: error: 'InputChar' undeclared (first use in this function)
while (HAL_UART_Receive (&hlpuart1, &InputChar, 1, 1000) == HAL_OK)
^~~~~~~~~
../Core/Src/main.c:102:41: note: each undeclared identifier is reported only once for each function it appears in
../Core/Src/main.c:104:22: error: 'Status' undeclared (first use in this function); did you mean 'ITStatus'?
if (InputChar != Status)
^~~~~~
ITStatus
../Core/Src/main.c:107:37: warning: pointer targets in passing argument 2 of 'HAL_UART_Transmit' differ in signedness [-Wpointer-sign]
HAL_UART_Transmit (&hlpuart1, "\r\n to be switched to S world.");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: expected 'uint8_t * {aka unsigned char *}' but argument is of type 'char *'
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c:107:7: error: too few arguments to function 'HAL_UART_Transmit'
HAL_UART_Transmit (&hlpuart1, "\r\n to be switched to S world.");
^~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: declared here
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c:109:37: warning: pointer targets in passing argument 2 of 'HAL_UART_Transmit' differ in signedness [-Wpointer-sign]
HAL_UART_Transmit (&hlpuart1, "\r\n to be switched to NS world.");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: expected 'uint8_t * {aka unsigned char *}' but argument is of type 'char *'
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c:109:7: error: too few arguments to function 'HAL_UART_Transmit'
HAL_UART_Transmit (&hlpuart1, "\r\n to be switched to NS world.");
^~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: declared here
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c:111:6: warning: implicit declaration of function 'SECURE_RetargetISR' [-Wimplicit-function-declaration]
SECURE_RetargetISR (InputChar);
^~~~~~~~~~~~~~~~~~
../Core/Src/main.c:115:36: warning: pointer targets in passing argument 2 of 'HAL_UART_Transmit' differ in signedness [-Wpointer-sign]
HAL_UART_Transmit(&hlpuart1, "\r\n User Button has been switched to S world.");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: expected 'uint8_t * {aka unsigned char *}' but argument is of type 'char *'
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c:115:7: error: too few arguments to function 'HAL_UART_Transmit'
HAL_UART_Transmit(&hlpuart1, "\r\n User Button has been switched to S world.");
^~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: declared here
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c:117:36: warning: pointer targets in passing argument 2 of 'HAL_UART_Transmit' differ in signedness [-Wpointer-sign]
HAL_UART_Transmit(&hlpuart1, "\r\n User Button has been switched to NS world.");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: expected 'uint8_t * {aka unsigned char *}' but argument is of type 'char *'
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c:117:7: error: too few arguments to function 'HAL_UART_Transmit'
HAL_UART_Transmit(&hlpuart1, "\r\n User Button has been switched to NS world.");
^~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: declared here
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c:120:36: warning: pointer targets in passing argument 2 of 'HAL_UART_Transmit' differ in signedness [-Wpointer-sign]
HAL_UART_Transmit(&hlpuart1, "\r\n the same state, do nothing. \n");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: expected 'uint8_t * {aka unsigned char *}' but argument is of type 'char *'
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c:120:7: error: too few arguments to function 'HAL_UART_Transmit'
HAL_UART_Transmit(&hlpuart1, "\r\n the same state, do nothing. \n");
^~~~~~~~~~~~~~~~~
In file included from ../Core/Inc/stm32l5xx_hal_conf.h:402:0,
from ../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../../Drivers/STM32L5xx_HAL_Driver/Inc/stm32l5xx_hal_uart.h:1581:19: note: declared here
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
^~~~~~~~~~~~~~~~~
../Core/Src/main.c: In function 'HAL_GPIO_EXTI_Rising_Callback':
../Core/Src/main.c:293:21: error: 'LED_GREEN_GPIO_Port' undeclared (first use in this function); did you mean 'LED_BLUE_GPIO_Port'?
HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
^~~~~~~~~~~~~~~~~~~
LED_BLUE_GPIO_Port
../Core/Src/main.c:293:42: error: 'LED_GREEN_Pin' undeclared (first use in this function); did you mean 'LED_BLUE_Pin'?
HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin);
^~~~~~~~~~~~~
LED_BLUE_Pin
make: *** [Core/Src/subdir.mk:33: Core/Src/main.o] Error 1
"make -j8 all" terminated with exit code 2. Build might be incomplete.
08:02:08 Build Failed. 10 errors, 6 warnings. (took 665ms)
对TrustZone的探索还在继续,这次,换了一台机子,操作系统从Ubuntu 20.04 换成 Windows 10。
测试的步骤也变了:
1、先用STM32CubeProgrammer进行选项字节配置,再用独立的STM32CubeMX进行初始设置,然后从STM32CubeMX的提示中直接进入STM32CubeIDE。
2、将示例中的四段代码逐个输入,每输入一个进行一次build。
测试的结果是,问题出在第四段,也就是NS区域的第二段代码处。
本帖最后由 MianQi 于 2021-1-8 17:44 编辑
新的进展 —— 可以Run了:
于是,用户按键(User Button)也生效了:https://v.youku.com/v_show/id_XNTA3NDAxMDI2OA==.html
现正的问题是,不能实现串口通信,试了Putty和VS Code,两个都不能键盘输入,也没有对话。还在查找原因。