#include "main.h"
#include "tim.h"
#include "tsc.h"
#include "usart.h"
#include "gpio.h"
#define MAX_TOUCH_VALUE 250
#define DATA_SIZE 14
#define NUM_CHANNELS 5
#define CHANNEL_MASK 0x1F
#define NUM_GROUPS (sizeof(tch_group) / sizeof(Group_touch))
uint8_t trigger_tim3;
uint16_t tpt[10] = {0,};
uint8_t idx_tsc;
typedef struct {
uint8_t seq;
uint8_t pin_count;
uint8_t addr[4];
uint8_t used_group[4];
uint32_t touch_pins;
} tsc_mod;
tsc_mod TModule[] = {
{0, 3, {0, 1, 2}, {0, 1, 3}, TSC_GROUP1_IO2 | TSC_GROUP2_IO2 | TSC_GROUP4_IO2},
{1, 4, {3, 4, 5, 6}, {0, 1, 2, 4}, TSC_GROUP1_IO3 | TSC_GROUP2_IO3 | TSC_GROUP3_IO3| TSC_GROUP5_IO3},
{2, 3, {7, 8, 9}, {0, 1, 4}, TSC_GROUP1_IO4 | TSC_GROUP2_IO4 | TSC_GROUP5_IO4}
};
uint8_t pck[DATA_SIZE] = {0,};
void SystemClock_Config(void);
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_TSC_Init();
MX_TIM3_Init();
HAL_TIM_Base_Start_IT(&htim3);
HAL_TSC_Init(&htsc);
HAL_TSC_Start_IT(&htsc);
while (1) {
if(trigger_tim3 == 1) {
trigger_tim3 = 0;
tsc_mod cur_seq_mod = TModule[idx_tsc];
HAL_TSC_IODischarge(&htsc, ENABLE);
TSC->IOHCR = ~cur_seq_mod.touch_pins;
if(HAL_TSC_PollForAcquisition(&htsc) == HAL_OK) {
if (HAL_TSC_Start(&htsc) == HAL_OK) {
if (HAL_TSC_PollForAcquisition(&htsc) == HAL_OK) {
for(int i = 0; i < cur_seq_mod.pin_count; i++) {
tpt[cur_seq_mod.addr[i]] = TSC->IOGXCR[cur_seq_mod.used_group[i]];
}
}
}
}
idx_tsc++;
if(idx_tsc > 2) {
idx_tsc = 0;
}
}
}
}
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == htim3.Instance) {
if(trigger_tim3 == 0) {
trigger_tim3 = 1;
}
}
}
void Error_Handler(void) {
__disable_irq();
while (1) {}
}