Skip to content

Commit

Permalink
added code to measure scan time
Browse files Browse the repository at this point in the history
scan time between 0.853 to 1.2 ms
  • Loading branch information
aburt2 committed Jan 14, 2024
1 parent b1e475a commit 19162f4
Showing 1 changed file with 101 additions and 3 deletions.
104 changes: 101 additions & 3 deletions firmware/tsticktouch_firmware/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
#include "cycfg_capsense.h"
#include "cycfg_peripherals.h"
#include <stdint.h>

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <cy_systick.h>

/*******************************************************************************
* Macros
Expand Down Expand Up @@ -97,6 +100,16 @@ typedef struct touchBuffer
struct touchBuffer touch1Data; // Main board touch data
struct touchBuffer touch2Data; // Auxillary board touch data

/* Timing variables */
int start, end; // timing variable
float slot_scan_time = 0;
float sensor_scan_time = 0;
uint16_t scan_time = 0;
float total_t = 0;
int systick_count = 0;
float TRIALS = 20000.0f;
int SENSORS_PER_TRIAL = 30.0f;

/* Allocate context for SPI operation */
cy_stc_scb_spi_context_t spiContext;
cy_stc_scb_spi_context_t spi_aux_Context;
Expand Down Expand Up @@ -127,12 +140,15 @@ static void initialize_i2c(void);
static void initialize_spi(void);
static void getTouch(void);
static void sendTouch(void);
static float timedifference_msec(void);
static float timedifference_usec(void);
static void systick_isr(void);
static uint32_t get_tick(void);

#if CY_CAPSENSE_BIST_EN
static void measure_sensor_cp(void);
#endif /* CY_CAPSENSE_BIST_EN */


/*******************************************************************************
* Function Name: main
********************************************************************************
Expand Down Expand Up @@ -163,6 +179,10 @@ int main(void)
/* Enable global interrupts */
__enable_irq();

/* Enable timer */
Cy_SysTick_Init(CY_SYSTICK_CLOCK_SOURCE_CLK_CPU, 0x00FFFFFF);
Cy_SysTick_SetCallback(0UL, &systick_isr);

/* touch buffer Initialization */
uint16_t i;

Expand Down Expand Up @@ -203,6 +223,8 @@ int main(void)
/* Measure the self capacitance of sensor electrode using BIST */
measure_sensor_cp();
#endif /* CY_CAPSENSE_BIST_EN */
/* start time */
start = Cy_SysTick_GetValue();

/* Start the first scan */
Cy_CapSense_ScanAllSlots(&cy_capsense_context);
Expand All @@ -211,7 +233,6 @@ int main(void)
{
if(CY_CAPSENSE_NOT_BUSY == Cy_CapSense_IsBusy(&cy_capsense_context))
{
/* */
/* Process all widgets */
Cy_CapSense_ProcessAllWidgets(&cy_capsense_context);

Expand All @@ -226,6 +247,12 @@ int main(void)
getTouch();
}

/* Compute sensor scan time */
end = get_tick();
total_t = timedifference_usec();
scan_time = (uint16_t)total_t;
start = end;

/* Send data to host MCU */
sendTouch();

Expand All @@ -239,6 +266,74 @@ int main(void)
}
}
}
// /* update time */
// end = get_tick();
// total_t = timedifference_sec();
// slot_scan_time = 1000 * total_t / TRIALS; // scan time per 30 sensors (us)
// sensor_scan_time = slot_scan_time / SENSORS_PER_TRIAL; // scan time per sensor (us)
// printf("Time taken to scan %d sensors %f times: %f ms", SENSORS_PER_TRIAL, TRIALS, total_t);
}


/*******************************************************************************
* Function Name: get tick
********************************************************************************
* Summary:
* Get current systick value
*
*******************************************************************************/

static uint32_t get_tick(void)
{
return ((0xFFFFFF - Cy_SysTick_GetValue()) + (systick_count * 0x1000000));
}

/*******************************************************************************
* Function Name: timedifference_msec
********************************************************************************
* Summary:
* This function returns the time difference in ms
*
*******************************************************************************/

static float timedifference_msec(void)
{
int tick_dif = end - start;
if (tick_dif < 0) {
tick_dif = -1 * tick_dif;
}
float time_diff = tick_dif * 1000.0f;
time_diff = time_diff / 16777216.0f;
return time_diff;
}

/*******************************************************************************
* Function Name: timedifference_msec
********************************************************************************
* Summary:
* This function returns the time difference in ms
*
*******************************************************************************/

static float timedifference_usec(void)
{
int tick_dif = end - start;
if (tick_dif < 0) {
tick_dif = -1 * tick_dif;
}
float time_diff = tick_dif * 1000000.0f;
time_diff = time_diff / 16777216.0f;
return time_diff;
}


/*******************************************************************************
* Function Name: SysTick_Callback
****************************************************************************/
static void systick_isr(void)
{
/* Some action */
systick_count++;
}

/*******************************************************************************
Expand Down Expand Up @@ -548,6 +643,9 @@ static void sendTouch(void)
txBuffer[i+60] = touch2Data.u16_signal[i];
}

/* save scan time */
txBuffer[120] = scan_time;

/* Master: start a transfer. Slave: prepare for a transfer. */
Cy_SCB_SPI_Transfer(CYBSP_SPI_HW, (uint8_t *)&txBuffer, NULL, sizeof(txBuffer), &spiContext);
while ((0UL != (CY_SCB_SPI_TRANSFER_ACTIVE & Cy_SCB_SPI_GetTransferStatus(CYBSP_SPI_HW, &spiContext))))
Expand Down

0 comments on commit 19162f4

Please sign in to comment.