/************************************************************
* Percepio Tracealyzer - ITM Trace Exporter for Keil uVision
* Copyright (c) 2018, Percepio AB.
* https://percepio.com
************************************************************/

FUNC void tzSetEnable(int enable)
{	
	if (enable == 1)
	{
		printf("Starting Tracealyzer recorder\n"); 		
	
		// Forward the ITM data to file
		exec("ITMLOG 1 > .\\tracealyzer.psf");
		
		// Send start command to Tracealyzer (not required if using vTraceEnable(TRC_START))
		exec("E CHAR tz_host_command_data = 1, 1, 0, 0, 0, 0, 0xFD, 0xFF");
		exec("tz_host_command_bytes_to_read = 8");
	}
	else if (enable == 0)
	{	
		printf("Stopping Tracealyzer recorder...\n"); 
		
		// Send stop command to Tracealyzer, to stop writing ITM data.		
		exec("E CHAR tz_host_command_data = 1, 0, 0, 0, 0, 0, 0xFE, 0xFF");
		exec("tz_host_command_bytes_to_read = 8");

		_sleep_(2000); // Wait a while to let all data be written the host file.
		
		// Stop forwarding the ITM data to file and close the file.
		exec("ITMLOG 1 OFF");
		
		printf("Tracealyzer recorder stopped.\n"); 
		
	}
	else printf("Usage: tzSetEnable(0 or 1), where 0 is disable (stops recorder) and 1 enable (starts recording)");
	
} 


// The Tracealyzer ITM stream port for Keil µVision can be used in two ways.
//
// 1. Start tracing directly from startup. 
// Make sure tzSetEnable(1) is called below and vTraceEnable(TRC_START) in your target startup.
//
// 2. Start the trace manually, using the "Start Recording" button in Keil µVision. 
// In this case, comment out the below call to tzSetEnable and make sure you call vTraceEnable(TRC_INIT) in your target startup (not TRC_START).

tzSetEnable(1);

DEFINE BUTTON "Start Recording", "tzSetEnable(1)";
DEFINE BUTTON "Stop Recording", "tzSetEnable(0)";