cancel
Showing results for 
Search instead for 
Did you mean: 

I am trying to get the STM32F0 TSC touch sensing library working, but for some reason after all my initializing and configurations. I get the TSC interrupt to fire, but my counter's always seem to have the max value. I print out the value with USART.

RWill.2
Associate

My code is posted below. Whenever I try to swipe my finger across the touch sensor, nothing seems to be read. However, my TSC interrupt handler is  constantly firing and printing out the value from the TSC counter which for some reason is awlays the max count value of 16838?

What am I doing wrong and why will my touch sensor not read my finger and change th value within the counter?

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "string.h"
#include "stdio.h"
 
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Transmit_Char(char);
void Transmit_String(char[]);
 
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
	
	__HAL_RCC_GPIOA_CLK_ENABLE();
	__HAL_RCC_GPIOB_CLK_ENABLE(); // Enable the GPIOB clock in the RCC
	__HAL_RCC_GPIOC_CLK_ENABLE(); // Enable the GPIOC clock in the RCC
	
	
	// Enable the LEDS
	GPIO_InitTypeDef initStrC = {GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9,
	GPIO_MODE_OUTPUT_PP,
	GPIO_SPEED_FREQ_LOW,
	GPIO_NOPULL};
	HAL_GPIO_Init(GPIOC, &initStrC);
	
	// ************************************ UART *******************************
	
	RCC->APB1ENR |= (1<<18); // ENABLE THE CLOCK USART3
	
	//PC4 (Plug into RX) PC5 (Plug into TX)
	GPIOC->MODER |= ((1<<11) | (1<<9));
	GPIOC->AFR[0] |= ((1<<20) | (1<<16));
	
	USART3->CR1 |= ((1<<3) | (1<<2)); // Enable the transmitter and receiver hardware.
	USART3->BRR = HAL_RCC_GetHCLKFreq()/115200; // SET THE BAUD RATE
	USART3->CR1 |= (1<<5); // ENABLE THE RXNE INTERRUPT
	USART3->CR1 |= (1<<0); // ENABLE
	
	// ****************************** TSC ***************************************
	RCC->AHBENR |= (1<<24); // enable the tsc clock
	
	// Set PA2/PA3 to AF mode
	GPIOA->MODER |= (1<<5) | (1<<7); // alternate function mode
	GPIOA->AFR[0] |= (1<<8) | (1<<9); // AF3 mode for TSC
	GPIOA->OTYPER |= (1<<3); // set pa3 to open-drain / leave pa2 to push-pull
	
	
	/* Configure TCS */
	/* With a charge transfer around 2.5 µs */
	/* (1) Select fPGCLK = fHCLK/32,
		  Set pulse high = 2xtPGCLK,Master
			Set pulse low = 2xtPGCLK
			Set Max count value = 16383 pulses
			Enable TSC */
	/* (2) Disable hysteresis */
	/* (3) Enable end of acquisition IT */
	/* (4) Sampling enabled, G2IO4 */
	/* (5) Channel enabled, G2IO3 */
	/* (6) Enable group, G2 */
	/* (7) Start Acquisition */
	TSC->CR = TSC_CR_PGPSC_2 | TSC_CR_PGPSC_0 | TSC_CR_CTPH_0 | TSC_CR_CTPL_0 | TSC_CR_MCV_2 | TSC_CR_MCV_1 | TSC_CR_TSCE; /* (1) */
	TSC->IOHCR &= (uint32_t)(~(TSC_IOHCR_G2_IO4 | TSC_IOHCR_G2_IO3)); /* (2) */
	TSC->IER = TSC_IER_EOAIE; /* (3) */
	TSC->IOSCR = TSC_IOSCR_G2_IO4; /* (4) */
	TSC->IOCCR = TSC_IOCCR_G2_IO3; /* (5) */
	TSC->IOGCSR |= TSC_IOGCSR_G2E; /* (6) */
	TSC->CR |= TSC_CR_START; /* (7) */
	
		
	NVIC_EnableIRQ(USART3_4_IRQn);
	NVIC_SetPriority(USART3_4_IRQn, 4);
	NVIC_EnableIRQ(TSC_IRQn);
	NVIC_SetPriority(TSC_IRQn, 3);
 
	// Main loop
  while (1)
  {	
 
  }
}
 
 
/**
* handlers
**/
 
/* This handler is for the TSC ...  An interrupt request is generated if the
																		EOAIE bit in the TSC_IER register is set.
																		In the case that a max count error is detected, */
void TSC_IRQHandler(void){
	/* End of acquisition flag */
	if ((TSC->ISR & TSC_ISR_EOAF) == TSC_ISR_EOAF)
	{
		TSC->ICR = TSC_ICR_EOAIC; /* Clear flag */
	  int AcquisitionValue = TSC->IOGXCR[1]; /* Get G2 counter value */
		char value[10];
		sprintf(value, "%d", AcquisitionValue);
		strcat(value, "\r\n");
		Transmit_String(value);
		TSC->CR |= TSC_CR_START; /* (7) */
	}
}
 
/* This is for handling strokes on the keyboard */
void USART3_4_IRQHandler(void)
{
	char input = USART3->RDR;
	char output[3] = {input};
	strcat(output, "p");
	strcat(output, "\r\n");
	Transmit_String(output);
}
 
 
/**
* Put send into the transmit register
**/
void Transmit_Char(char send)
{
	while(!(USART3->ISR & 0x0080)) // ISR[7] is reset automatically -> 2^7 = 128 which is the bit were checking
	{
		// wait until transmit register is empty
	}
	USART3->TDR = send;
}
 
/**
* Transmit a string
**/
void Transmit_String(char send[])
{
	int i = 0;
	while(send[i] != 0)
	{
		Transmit_Char(send[i]);
		i++;
	}
}
 
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/* USER CODE BEGIN 4 */
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
 
  /* USER CODE END Error_Handler_Debug */
}
 
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(char *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 REPLIES 0