SAML11 TrustZone Touch Project
Objective
SAML11 supports the use of TrustZone®. The following describes how to configure a TrustZone project with touch. Two options will be covered.
- Touch and application code are entirely within the secure area
- Touch is within the secure area, with control from the non-secure area
In both cases, a SAMQT7 was used in this guide.
Touch and Application Code Entirely Within the Secure Area
TrustZone Enabled checkbox
Select the TrustZone Enabled checkbox when choosing the SAML11 as the target device.
This will create two projects in the tree view, one for the secure region, the other for the non-secure region.
Microchip Harmony Configurator (MHC)
Start the Microchip Harmony Configurator (MHC) when the projects are created.
Secure projects are used with MHC. The secure project should be set to the Main Project (highlighted in the tree view).
Touch Library
Add the Touch Library to the project and allow the Real-Time Clock (RTC) to be added.
Clock configuration will also update.
Touch Configurator
Configure touch sensors as required using the Touch Configurator.
QT7 uses one slider and two touch buttons.
Configure TrustZone
Adjust Memory Configuration to suit the user application.
The RTC and Peripheral Touch Controller (PTC) should be green (indicates placed in secure memory region).
Main.c
Main.c within the Secure project should be updated as follows:
#include <stddef.h> //Defines NULL
#include <stdbool.h> //Defines true
#include <stdlib.h> //Defines EXIT_FAILURE
#include "definitions.h"
#include "touch/touch_api_ptc.h" //SYS function prototypes
extern volatile uint8_t measurement_done_touch;
void touch_status_display1(void);
int main(void) {
/* Initialize all modules */
SYS_Initialize(NULL);
touch_init();
while (1) {
touch_process();
if (measurement_done_touch == 1) {
touch_status_display1();
measurement_done_touch =0;
}
}
/* Execution should not come here during normal operation */
return ( EXIT_FAILURE);
}
void touch_status_display1()
{
uint8_t key_status1 = 0;
uint8_t scroller_status1 = 0;
uint16_t scroller_position1 = 0;
key_status1 = get_sensor_state(0) & 0x80;
if (0u != key_status1) {
LED_BUTTON_0_Clear();
} else {
LED_BUTTON_0_Set();
}
key_status1 = get_sensor_state(1) & 0x80;
if (0u != key_status1) {
LED_BUTTON_1_Clear();
} else {
LED_BUTTON_1_Set();
}
scroller_status1 = get_scroller_state(0);
scroller_position1 = get_scroller_position(0);
LED_SLIDER_0_Set();
LED_SLIDER_1_Set();
LED_SLIDER_2_Set();
LED_SLIDER_3_Set();
LED_SLIDER_4_Set();
LED_SLIDER_5_Set();
if (0u != scroller_status1) {
LED_SLIDER_0_Clear();
if (scroller_position1 > 43) {
LED_SLIDER_1_Clear();
}
if (scroller_position1 > 85) {
LED_SLIDER_2_Clear();
}
if (scroller_position1 > 120) {
LED_SLIDER_3_Clear();
}
if (scroller_position1 > 165) {
LED_SLIDER_4_Clear();
}
if (scroller_position1 > 213) {
LED_SLIDER_5_Clear();
}
}
}
</stdlib.h></stdbool.h></stddef.h>
Touch is Within the Secure Area, With Control From the Non-Secure Area
The process of controlling touch operation from the non-secure project can be achieved using the TrustZone veneer.
The following files need to be modified:
Non-Secure Project
- Main.c
- Veneer.h
Secure Project
- Veneer.h
- Veneer.c
- Secure.c
- Secure.h
Non-Secure Project : Main.c
/* Initialize all modules */
SYS_Initialize(NULL);
secure_touch_init();
while (1) {
SYS_Tasks();
secure_touch_process();
if (secure_get_measurement_done_touch() == 1) {
secure_touch_status_display1();
secure_set_measurement_done_touch(0);
}
}
/* Execution should not come here during normal operation */
return ( EXIT_FAILURE);
}
Secure Project Main.c is standard as per a new TrustZone project. It should contain:
{
volatile funcptr_void NonSecure_ResetHandler;
/* Initialize all modules */
SYS_Initialize ( NULL );
/* Set non-secure main stack (MSP_NS) */
__TZ_set_MSP_NS(*((uint32_t *)(TZ_START_NS)));
/* Get non-secure reset handler */
NonSecure_ResetHandler = (funcptr_void)(*((uint32_t *)((TZ_START_NS) + 4U)));
/* Start non-secure state software application */
NonSecure_ResetHandler();
/* Execution should not come here during normal operation */
return ( EXIT_FAILURE );
}
Non-Secure Project: Veneer.h
extern uint8_t secure_get_measurement_done_touch(void);
extern void secure_set_measurement_done_touch(uint8_t val);
extern void secure_touch_process(void);
extern void secure_touch_init(void);
Secure Project: Veneer.h
extern uint8_t secure_get_measurement_done_touch(void);
extern void secure_set_measurement_done_touch(uint8_t val);
extern void secure_touch_process(void);
extern void secure_touch_init(void);
Secure Project: Veneer.c
touch_init();
}
void __attribute__((cmse_nonsecure_entry)) secure_touch_process(void){
touch_process();
}
void __attribute__((cmse_nonsecure_entry)) secure_touch_status_display1(void){
touch_status_display1();
}
uint8_t __attribute__((cmse_nonsecure_entry)) secure_get_measurement_done_touch(void){
return get_measurement_done_touch();
}
void __attribute__((cmse_nonsecure_entry)) secure_set_measurement_done_touch(uint8_t setVal){
set_measurement_done_touch(setVal);
}
Secure Project: Secure.h
#include "../config/default/touch/touch_api_ptc.h"
extern int add(int x, int y);
void touch_status_display1();
uint8_t get_measurement_done_touch();
void set_measurement_done_touch(uint8_t val);
extern void touch_process();
extern void touch_init();
Secure Project: Secure.c
extern volatile uint8_t measurement_done_touch;
uint8_t get_measurement_done_touch()
{
return measurement_done_touch;
}
void set_measurement_done_touch(uint8_t val)
{
measurement_done_touch = val;
}
void touch_status_display1()
{
uint8_t key_status1 = 0;
uint8_t scroller_status1 = 0;
uint16_t scroller_position1 = 0;
key_status1 = get_sensor_state(0) & 0x80;
if (0u != key_status1) {
LED_BUTTON_0_Clear();
} else {
LED_BUTTON_0_Set();
}
key_status1 = get_sensor_state(1) & 0x80;
if (0u != key_status1) {
LED_BUTTON_1_Clear();
} else {
LED_BUTTON_1_Set();
}
scroller_status1 = get_scroller_state(0);
scroller_position1 = get_scroller_position(0);
LED_SLIDER_0_Set();
LED_SLIDER_1_Set();
LED_SLIDER_2_Set();
LED_SLIDER_3_Set();
LED_SLIDER_4_Set();
LED_SLIDER_5_Set();
if (0u != scroller_status1) {
LED_SLIDER_0_Clear();
if (scroller_position1 > 43) {
LED_SLIDER_1_Clear();
}
if (scroller_position1 > 85) {
LED_SLIDER_2_Clear();
}
if (scroller_position1 > 120) {
LED_SLIDER_3_Clear();
}
if (scroller_position1 > 165) {
LED_SLIDER_4_Clear();
}
if (scroller_position1 > 213) {
LED_SLIDER_5_Clear();
}
}
}