tom dunigan
/
L476_touch
attempt at TSC on STM32L4. not working
main.cpp@0:6cc44b2a3d49, 2016-06-01 (annotated)
- Committer:
- manitou
- Date:
- Wed Jun 01 00:05:21 2016 +0000
- Revision:
- 0:6cc44b2a3d49
first try at TSC (not working)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
manitou | 0:6cc44b2a3d49 | 1 | // capacitive touch TSC |
manitou | 0:6cc44b2a3d49 | 2 | // select cap pin and sensor pin in same group datasheet table 17 |
manitou | 0:6cc44b2a3d49 | 3 | // http://www.codeforge.com/read/253659/bsp.c__html |
manitou | 0:6cc44b2a3d49 | 4 | |
manitou | 0:6cc44b2a3d49 | 5 | #include "mbed.h" |
manitou | 0:6cc44b2a3d49 | 6 | #include "PeripheralPins.h" |
manitou | 0:6cc44b2a3d49 | 7 | TSC_HandleTypeDef TscHandle; |
manitou | 0:6cc44b2a3d49 | 8 | const PinMap PinMap_TSC[] = { |
manitou | 0:6cc44b2a3d49 | 9 | {PB_6, (int)TSC, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, 9)}, // AF9 OUTPUT or AF? |
manitou | 0:6cc44b2a3d49 | 10 | {PB_7, (int)TSC, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, 9)}, // need capacitor |
manitou | 0:6cc44b2a3d49 | 11 | {NC, NC, 0} |
manitou | 0:6cc44b2a3d49 | 12 | }; |
manitou | 0:6cc44b2a3d49 | 13 | |
manitou | 0:6cc44b2a3d49 | 14 | #define PRREG(z) printf(#z" 0x%x\n",z) |
manitou | 0:6cc44b2a3d49 | 15 | Timer tmr; |
manitou | 0:6cc44b2a3d49 | 16 | |
manitou | 0:6cc44b2a3d49 | 17 | main() { |
manitou | 0:6cc44b2a3d49 | 18 | tmr.start(); |
manitou | 0:6cc44b2a3d49 | 19 | __HAL_RCC_TSC_CLK_ENABLE(); // enable TSC |
manitou | 0:6cc44b2a3d49 | 20 | // enable GPIO and config pins |
manitou | 0:6cc44b2a3d49 | 21 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
manitou | 0:6cc44b2a3d49 | 22 | //HAL_GPIO_Init(); // mux function 9 |
manitou | 0:6cc44b2a3d49 | 23 | pin_function(PB_6,pinmap_find_function(PB_6,PinMap_TSC)); |
manitou | 0:6cc44b2a3d49 | 24 | pin_function(PB_7,pinmap_find_function(PB_7,PinMap_TSC)); |
manitou | 0:6cc44b2a3d49 | 25 | |
manitou | 0:6cc44b2a3d49 | 26 | TscHandle.Instance = TSC; |
manitou | 0:6cc44b2a3d49 | 27 | TscHandle.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL; |
manitou | 0:6cc44b2a3d49 | 28 | TscHandle.Init.CTPulseHighLength = TSC_CTPH_2CYCLES; |
manitou | 0:6cc44b2a3d49 | 29 | TscHandle.Init.CTPulseLowLength = TSC_CTPL_2CYCLES; |
manitou | 0:6cc44b2a3d49 | 30 | TscHandle.Init.IODefaultMode = TSC_IODEF_IN_FLOAT; |
manitou | 0:6cc44b2a3d49 | 31 | TscHandle.Init.MaxCountInterrupt = DISABLE; |
manitou | 0:6cc44b2a3d49 | 32 | TscHandle.Init.MaxCountValue = TSC_MCV_8191; |
manitou | 0:6cc44b2a3d49 | 33 | TscHandle.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV32; |
manitou | 0:6cc44b2a3d49 | 34 | TscHandle.Init.SpreadSpectrum = DISABLE; |
manitou | 0:6cc44b2a3d49 | 35 | TscHandle.Init.SpreadSpectrumDeviation = 127; |
manitou | 0:6cc44b2a3d49 | 36 | TscHandle.Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1; |
manitou | 0:6cc44b2a3d49 | 37 | TscHandle.Init.SynchroPinPolarity = TSC_SYNC_POL_FALL; |
manitou | 0:6cc44b2a3d49 | 38 | /* All channel, shield and sampling IOs must be declared below */ |
manitou | 0:6cc44b2a3d49 | 39 | TscHandle.Init.ChannelIOs = TSC_GROUP2_IO3; // PB6 output push-pull sensor |
manitou | 0:6cc44b2a3d49 | 40 | TscHandle.Init.SamplingIOs = TSC_GROUP2_IO4; // PB7 output open drain need capacitor |
manitou | 0:6cc44b2a3d49 | 41 | TscHandle.Init.ShieldIOs = 0; |
manitou | 0:6cc44b2a3d49 | 42 | HAL_TSC_Init(&TscHandle); |
manitou | 0:6cc44b2a3d49 | 43 | PRREG(GPIOB->MODER); |
manitou | 0:6cc44b2a3d49 | 44 | PRREG(GPIOB->OTYPER); |
manitou | 0:6cc44b2a3d49 | 45 | PRREG(GPIOB->PUPDR); |
manitou | 0:6cc44b2a3d49 | 46 | PRREG(GPIOB->AFR[0]); |
manitou | 0:6cc44b2a3d49 | 47 | PRREG(GPIOB->AFR[1]); |
manitou | 0:6cc44b2a3d49 | 48 | PRREG(TSC->CR); |
manitou | 0:6cc44b2a3d49 | 49 | PRREG(TSC->IOASCR); |
manitou | 0:6cc44b2a3d49 | 50 | PRREG(TSC->IOSCR); |
manitou | 0:6cc44b2a3d49 | 51 | PRREG(TSC->IOCCR); |
manitou | 0:6cc44b2a3d49 | 52 | |
manitou | 0:6cc44b2a3d49 | 53 | while(1) { |
manitou | 0:6cc44b2a3d49 | 54 | uint32_t us = tmr.read_us(); |
manitou | 0:6cc44b2a3d49 | 55 | HAL_TSC_Start(&TscHandle); |
manitou | 0:6cc44b2a3d49 | 56 | HAL_TSC_PollForAcquisition(&TscHandle); |
manitou | 0:6cc44b2a3d49 | 57 | uint32_t val = HAL_TSC_GroupGetValue(&TscHandle,1); // group 2 0 to 7 or 1 to 8 |
manitou | 0:6cc44b2a3d49 | 58 | us=tmr.read_us() - us; |
manitou | 0:6cc44b2a3d49 | 59 | int avrg=0; |
manitou | 0:6cc44b2a3d49 | 60 | for(int i=0;i<100;i++) { |
manitou | 0:6cc44b2a3d49 | 61 | HAL_TSC_Start(&TscHandle); |
manitou | 0:6cc44b2a3d49 | 62 | HAL_TSC_PollForAcquisition(&TscHandle); |
manitou | 0:6cc44b2a3d49 | 63 | avrg += HAL_TSC_GroupGetValue(&TscHandle,1); |
manitou | 0:6cc44b2a3d49 | 64 | } |
manitou | 0:6cc44b2a3d49 | 65 | printf("%d %d us avrg %d\n",val,us,avrg/100); |
manitou | 0:6cc44b2a3d49 | 66 | PRREG(TSC->IOGCSR); |
manitou | 0:6cc44b2a3d49 | 67 | wait(2.0); |
manitou | 0:6cc44b2a3d49 | 68 | } |
manitou | 0:6cc44b2a3d49 | 69 | |
manitou | 0:6cc44b2a3d49 | 70 | } |