SAML11 TrustZone Touch Project

Last modified by Microchip on 2023/11/09 09:21

Warning: This tutorial uses the old Harmony configuration tool (MPLAB® Harmony Configurator (MHC)). New Harmony projects should use the MPLAB Code Configurator (MCC) instead.

 

Objective

SAML11 supports the use of TrustZone®. The following describes how to configure a TrustZone project with touch. Two options will be covered.

  1. Touch and application code are entirely within the secure area
  2. 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

Create a SAML11 Harmony 3 project.

MPLAB X View:  New Project

Back to Top


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.

MPLAB X View: TrustZone Enabled checkbox

Back to Top


Microchip Harmony Configurator (MHC)

Start the Microchip Harmony Configurator (MHC) when the projects are created.

MPLAB X View: Microchip Harmony Configurator (MHC)

Secure projects are used with MHC. The secure project should be set to the Main Project (highlighted in the tree view).

MPLAB X View: Secure project set to the Main Project

Back to Top


Touch Library

Add the Touch Library to the project and allow the Real-Time Clock (RTC) to be added.

Clock configuration will also update.

MPLAB X View: Add Touch Library and allow the Real-Time Clock (RTC) to be added

Back to Top


Touch Configurator

Configure touch sensors as required using the Touch Configurator.

MPLAB X View: Configure touch sensors using Touch Configurator

QT7 uses one slider and two touch buttons.

MPLAB X View: Slider and two touch buttons

Back to Top


Configure TrustZone

MPLAB X View:  TrustZone Manager selection

Adjust Memory Configuration to suit the user application.

MPLAB X View: Memory Configuration

The RTC and Peripheral Touch Controller (PTC) should be green (indicates placed in secure memory region).

MPLAB X View: RTC and Peripheral Touch Controller green

Back to Top


Assign pins and SERCOM as necessary

If LED toggling or data visualizer is required.

Pin Settings Table View

Note the addition of the Security mode column in the pin configurator.

Pin configurator Security mode column

Back to Top


Generate the project code

Generate the project code Selection

Back to Top


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>

Back to Top


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:

Note: Veneer.h in both projects should contain the same function list.

Back to Top

Non-Secure Project

  • Main.c
  • Veneer.h

Back to Top

Secure Project

  • Veneer.h
  • Veneer.c
  • Secure.c
  • Secure.h

Back to Top

Non-Secure Project : Main.c

int main(void) {
   /* 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:

int main ( void )
{
   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 void secure_touch_status_display1(void);
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 void secure_touch_status_display1(void);
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);

Back to Top

Secure Project: Veneer.c

void __attribute__((cmse_nonsecure_entry)) secure_touch_init(void) {
   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.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();

Back to Top

Secure Project: Secure.c

#include "../config/default/peripheral/port/plib_port.h"
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();
        }
    }
}

Back to Top