1.5 Adding Application Code to Non-Secure Project
- In the
main()
function belowSYS_Initialize()
add the following code to register callback event handlers.DMAC_ChannelCallbackRegister(DMAC_CHANNEL_0, usartTxDmaChannelHandler, 0);
- Implement the registered callback
event handler before the
main()
function.static void usartTxDmaChannelHandler(DMAC_TRANSFER_EVENT event, uintptr_t contextHandle) { if (event == DMAC_TRANSFER_EVENT_COMPLETE) { isUSARTTxComplete = true; } }
- Remove
SYS_Tasks( );
function call and replace with the below code to enter the Secure functionality from this Non-Secure application inside thewhile
loop.while ( true ) { if (readUartTxData(nonSecureUartTxBuffer) == true) { /* Maintain state machines of all polled MPLAB Harmony modules. */ DMAC_ChannelTransfer(DMAC_CHANNEL_0, nonSecureUartTxBuffer, \ (const void *)&(SERCOM3_REGS->USART_INT.SERCOM_DATA), \ strlen((const char*)nonSecureUartTxBuffer)); } secureAppEntry(); }
Figure 1-20. Adding Application Logic to Enter Secure Functionality from Non-Secure App Add the following code snippet to include the header files and declaration of variables used in the Non-Securemain.c
file.#include <stdio.h> #include <string.h> #include "trustZone/nonsecure_entry.h" static volatile bool isUSARTTxComplete = false; static volatile bool isUSARTRxComplete = false; static uint8_t nonSecureUartTxBuffer[100] = {0};
- In the
nonsecure_entry.h
file, available under Header Files > trustZone, add the following code by declaring NSCs with extern keyword to access and request the Secure application from the Non-Secure application.Note: Delete the generated template code and add the following code.extern bool readUartTxData(uint8_t *lcluartTxBuffer); extern void secureAppEntry(void);
Figure 1-21. Global NSCs to Access and Request Secure App from Non-Secure App