connect to application

This commit is contained in:
Conor Patrick 2019-05-09 02:26:32 -04:00
parent 898d45f871
commit aff8d10432
3 changed files with 109 additions and 49 deletions

View File

@ -44,7 +44,18 @@ extern PCD_HandleTypeDef hpcd;
static bool haveNFC = 0;
static bool isLowFreq = 0;
#define IS_BUTTON_PRESSED() (0 == (LL_GPIO_ReadInputPort(SOLO_BUTTON_PORT) & SOLO_BUTTON_PIN))
// #define IS_BUTTON_PRESSED() (0 == (LL_GPIO_ReadInputPort(SOLO_BUTTON_PORT) & SOLO_BUTTON_PIN))
static int is_physical_button_pressed()
{
return (0 == (LL_GPIO_ReadInputPort(SOLO_BUTTON_PORT) & SOLO_BUTTON_PIN));
}
static int is_touch_button_pressed()
{
return tsc_read_button(0) || tsc_read_button(1);
}
int (*IS_BUTTON_PRESSED)() = is_physical_button_pressed;
// Timer6 overflow handler. happens every ~90ms.
void TIM6_DAC_IRQHandler()
@ -108,23 +119,53 @@ void device_reboot()
NVIC_SystemReset();
}
static int does_device_have_touch_sensor()
{
int does;
LL_GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = 0;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
does = (LL_GPIO_ReadInputPort(GPIOB) & 1) == 0;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
return does;
}
void device_init(int argc, char *argv[])
{
hw_init(LOW_FREQUENCY);
// haveNFC = nfc_init();
if (haveNFC)
{
printf1(TAG_NFC, "Have NFC\r\n");
isLowFreq = 1;
IS_BUTTON_PRESSED = is_physical_button_pressed;
}
else
{
printf1(TAG_NFC, "Have NO NFC\r\n");
hw_init(HIGH_FREQUENCY);
isLowFreq = 0;
if (does_device_have_touch_sensor())
{
tsc_init();
IS_BUTTON_PRESSED = is_touch_button_pressed;
}
else
{
IS_BUTTON_PRESSED = is_physical_button_pressed;
}
}
usbhid_init();
@ -436,9 +477,6 @@ void device_manage()
#ifndef IS_BOOTLOADER
if(device_is_nfc())
nfc_loop();
sense_run();
#endif
}

View File

@ -8,11 +8,12 @@
int _run_sense_app = 0;
static int _has_init = 0;
#define ELECTRODE_0 TSC_GROUP2_IO1
#define ELECTRODE_1 TSC_GROUP2_IO2
void sense_init()
void tsc_init()
{
LL_GPIO_InitTypeDef GPIO_InitStruct1;
LL_GPIO_InitTypeDef GPIO_InitStruct2;
LL_GPIO_InitTypeDef GPIO_InitStruct;
// Enable TSC clock
RCC->AHB1ENR |= (1<<16);
@ -20,36 +21,36 @@ void sense_init()
PA4 ------> Channel 1
PA5 ------> Channel 2
*/
GPIO_InitStruct1.Pin = LL_GPIO_PIN_5|LL_GPIO_PIN_4;
GPIO_InitStruct1.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct1.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct1.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct1.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct1.Alternate = LL_GPIO_AF_9;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct1);
GPIO_InitStruct.Pin = LL_GPIO_PIN_5|LL_GPIO_PIN_4;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_9;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/** TSC GPIO Configuration
PA6 ------> sampling cap
*/
GPIO_InitStruct2.Pin = LL_GPIO_PIN_6;
GPIO_InitStruct2.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct2.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct2.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
GPIO_InitStruct2.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct2.Alternate = LL_GPIO_AF_9;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct2);
GPIO_InitStruct.Pin = LL_GPIO_PIN_6;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_9;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// Channel IOs
uint32_t channel_ios = TSC_GROUP2_IO1;
uint32_t channel_ios = TSC_GROUP2_IO1 | TSC_GROUP2_IO2;
// enable
TSC->CR = TSC_CR_TSCE;
TSC->CR |= (TSC_CTPH_16CYCLES |
TSC_CTPL_16CYCLES |
TSC->CR |= (TSC_CTPH_8CYCLES |
TSC_CTPL_8CYCLES |
(uint32_t)(1 << TSC_CR_SSD_Pos) |
TSC_SS_PRESC_DIV1 |
TSC_PG_PRESC_DIV128 |
TSC_PG_PRESC_DIV16 |
TSC_MCV_16383 |
TSC_SYNC_POLARITY_FALLING |
TSC_ACQ_MODE_NORMAL);
@ -63,9 +64,6 @@ void sense_init()
// Schmitt trigger and hysteresis
TSC->IOHCR = (uint32_t)(~(channel_ios | 0 | TSC_GROUP2_IO3));
// Channel IDs
TSC->IOCCR = (channel_ios | 0);
// Sampling IOs
TSC->IOSCR = TSC_GROUP2_IO3;
@ -78,6 +76,11 @@ void sense_init()
}
void tsc_set_electrode(uint32_t channel_ids)
{
TSC->IOCCR = (channel_ids);
}
void tsc_start_acq()
{
TSC->CR &= ~(TSC_CR_START);
@ -105,32 +108,47 @@ uint32_t tsc_read(uint32_t indx)
return TSC->IOGXCR[indx];
}
// Read button 0 or 1
// Returns 1 if pressed, 0 if not.
uint32_t tsc_read_button(uint32_t index)
{
switch(index)
{
case 0:
tsc_set_electrode(ELECTRODE_0);
break;
case 1:
tsc_set_electrode(ELECTRODE_1);
break;
}
tsc_start_acq();
tsc_wait_on_acq();
return tsc_read(1) < 50;
}
void sense_run()
{
static uint32_t t1 = 0;
uint32_t samp;
static uint32_t tlim = 0;
uint32_t t1,t2;
uint32_t but0,but1;
if (!_has_init)
{
sense_init();
tsc_init();
_has_init = 1;
}
if ((millis() - t1) > 200)
if ((millis() - tlim) > 200)
{
tsc_start_acq();
tsc_wait_on_acq();
// delay(4);
samp = tsc_read(1);
printf1(TAG_GREEN, "sensing %02d %02d %02d %02d\r\n",
tsc_read(0),
tsc_read(1),
tsc_read(2),
tsc_read(3)
);
t1 = millis();
}
but0 = tsc_read_button(0);
but1 = tsc_read_button(1);
t2 = millis();
printf1(TAG_GREEN, "but0: %02d but1: %02d (%d ms)\r\n", but0, but1, t2-t1);
t1 = millis();
tlim = millis();
}
}

View File

@ -1,11 +1,15 @@
#ifndef _SENSE_H_
#define _SENSE_H_
#include <stdint.h>
extern int _run_sense_app;
void sense_init();
void tsc_init();
uint32_t tsc_read_button(uint32_t index);
// For testing
void sense_run();
#endif