Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
ch32v003fun
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Horn
ch32v003fun
Commits
cf9e2e6e
Commit
cf9e2e6e
authored
1 year ago
by
charlesl
Browse files
Options
Downloads
Patches
Plain Diff
Update touch
parent
a91ab5b1
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/cap_touch_adc/cap_touch_adc.c
+68
-47
68 additions, 47 deletions
examples/cap_touch_adc/cap_touch_adc.c
with
68 additions
and
47 deletions
examples/cap_touch_adc/cap_touch_adc.c
+
68
−
47
View file @
cf9e2e6e
...
...
@@ -25,6 +25,19 @@
#define ADC_SAMPLE_TIME 2 // Tricky: Don't change this without a lot of experimentation.
#define MAX_SCALECHECK 4
// Can either be 0 or 1.
// If 0: Measurement low and rises high. So more pressed is smaller number.
// If 1: Higher number = harder press. Good to pair with TOUCH_FLAT.
// If you are doing more prox, use mode 0, otherwise, use mode 1.
#define TOUCH_SLOPE 1
// If you set this to 1, it will glitch the line, so it will only read
// anything reasonable if the capacitance can overcome that initial spike.
// Typically, it seems if you use this you probbly don't need to do
// any pre-use calibration.
#define TOUCH_FLAT 0
void
InitTouchADC
(
)
{
// ADCCLK = 24 MHz => RCC_ADCPRE = 0: divide sys clock by 2
...
...
@@ -48,8 +61,8 @@ void InitTouchADC( )
// Run from RAM to get even more stable timing.
// This function call takes about 8.1uS to execute.
uint32_t
ReadTouchPin
(
GPIO_TypeDef
*
io
,
int
portpin
,
int
adcno
)
__attribute__
((
noinline
,
section
(
".srodata"
)));
uint32_t
ReadTouchPin
(
GPIO_TypeDef
*
io
,
int
portpin
,
int
adcno
)
uint32_t
ReadTouchPin
(
GPIO_TypeDef
*
io
,
int
portpin
,
int
adcno
,
int
iterations
)
__attribute__
((
noinline
,
section
(
".srodata"
)));
uint32_t
ReadTouchPin
(
GPIO_TypeDef
*
io
,
int
portpin
,
int
adcno
,
int
iterations
)
{
uint32_t
ret
=
0
;
...
...
@@ -58,40 +71,49 @@ uint32_t ReadTouchPin( GPIO_TypeDef * io, int portpin, int adcno )
uint32_t
CFGBASE
=
io
->
CFGLR
&
(
~
(
0xf
<<
(
4
*
portpin
)));
uint32_t
CFGFLOAT
=
((
GPIO_CFGLR_IN_PUPD
)
<<
(
4
*
portpin
))
|
CFGBASE
;
uint32_t
CFGDRIVE
=
(
GPIO_CFGLR_OUT_10Mhz_OD
)
<<
(
4
*
portpin
)
|
CFGBASE
;
int
n
;
uint32_t
CFGDRIVE
=
(
GPIO_CFGLR_OUT_2Mhz_PP
)
<<
(
4
*
portpin
)
|
CFGBASE
;
// If we run multiple times with slightly different wait times, we can
// reduce the impact of the ADC's DNL.
for
(
n
=
MAX_SCALECHECK
;
n
;
n
--
)
#if TOUCH_FLAT == 1
#define RELEASEIO io->OUTDR = 1<<(portpin+16*TOUCH_SLOPE); io->CFGLR = CFGFLOAT;
#else
#define RELEASEIO io->CFGLR = CFGFLOAT; io->OUTDR = 1<<(portpin+16*TOUCH_SLOPE);
#endif
#define INNER_LOOP( n ) \
{ \
/* Only lock IRQ for a very narrow window. */
\
__disable_irq(); \
\
/* Tricky - we start the ADC BEFORE we transition the pin. By doing \
this We are catching it onthe slope much more effectively. */
\
ADC1->CTLR2 = ADC_SWSTART | ADC_ADON | ADC_EXTSEL; \
\
ADD_N_NOPS( n ) \
\
RELEASEIO \
\
/* Sampling actually starts here, somewhere, so we can let other \
interrupts run */
\
__enable_irq(); \
while(!(ADC1->STATR & ADC_EOC)); \
io->CFGLR = CFGDRIVE; \
io->OUTDR = 1<<(portpin+(16*(1-TOUCH_SLOPE))); \
ret += ADC1->RDATAR; \
}
int
i
;
for
(
i
=
0
;
i
<
iterations
;
i
++
)
{
// Only lock IRQ for a very narrow window.
__disable_irq
();
// Tricky - we start the ADC BEFORE we transition the pin. By doing this
// We are catching it onthe slope much more effectively.
ADC1
->
CTLR2
=
ADC_SWSTART
|
ADC_ADON
|
ADC_EXTSEL
;
switch
(
n
)
{
case
4
:
asm
volatile
(
"c.nop"
);
case
3
:
asm
volatile
(
"c.nop"
);
case
2
:
asm
volatile
(
"c.nop"
);
case
1
:
asm
volatile
(
"c.nop"
);
}
// Then we allow it to float. By here, we typically have started sampling.
io
->
CFGLR
=
CFGFLOAT
;
io
->
OUTDR
=
1
<<
portpin
;
__enable_irq
();
while
(
!
(
ADC1
->
STATR
&
ADC_EOC
));
io
->
CFGLR
=
CFGDRIVE
;
io
->
OUTDR
=
1
<<
(
portpin
+
16
);
ret
+=
ADC1
->
RDATAR
;
// Wait a variable amount of time based on loop iteration, in order
// to get a variety of RC points and minimize DNL.
INNER_LOOP
(
0
);
INNER_LOOP
(
2
);
INNER_LOOP
(
4
);
}
return
ret
;
...
...
@@ -111,25 +133,24 @@ int main()
// should be ready for SW conversion now
while
(
1
)
{
uint32_t
start
=
SysTick
->
CNT
;
uint32_t
sum
[
8
]
=
{
0
};
int
j
;
// Sampling all touch pads should take 3280 cycles, or about 68us.
// So it is possible to sample all channels at 14kHz
for
(
j
=
0
;
j
<
1
;
j
++
)
{
sum
[
0
]
+=
ReadTouchPin
(
GPIOA
,
2
,
0
)
;
sum
[
1
]
+=
ReadTouchPin
(
GPIOA
,
1
,
1
);
sum
[
2
]
+=
ReadTouchPin
(
GPIO
C
,
4
,
2
);
sum
[
3
]
+=
ReadTouchPin
(
GPIO
D
,
2
,
3
);
sum
[
4
]
+=
ReadTouchPin
(
GPIOD
,
3
,
4
);
sum
[
5
]
+=
ReadTouchPin
(
GPIOD
,
5
,
5
);
sum
[
6
]
+=
ReadTouchPin
(
GPIOD
,
6
,
6
);
sum
[
7
]
+=
ReadTouchPin
(
GPIOD
,
4
,
7
);
}
uint32_t
start
=
SysTick
->
CNT
;
// Sampling all touch pads, 3x should take 6030 cycles, and runs at about 8kHz
int
iterations
=
3
;
sum
[
0
]
+=
ReadTouchPin
(
GPIOA
,
2
,
0
,
iterations
);
sum
[
1
]
+=
ReadTouchPin
(
GPIO
A
,
1
,
1
,
iterations
);
sum
[
2
]
+=
ReadTouchPin
(
GPIO
C
,
4
,
2
,
iterations
);
sum
[
3
]
+=
ReadTouchPin
(
GPIOD
,
2
,
3
,
iterations
);
sum
[
4
]
+=
ReadTouchPin
(
GPIOD
,
3
,
4
,
iterations
);
sum
[
5
]
+=
ReadTouchPin
(
GPIOD
,
5
,
5
,
iterations
);
sum
[
6
]
+=
ReadTouchPin
(
GPIOD
,
6
,
6
,
iterations
);
sum
[
7
]
+=
ReadTouchPin
(
GPIOD
,
4
,
7
,
iterations
);
uint32_t
end
=
SysTick
->
CNT
;
printf
(
"%d %d %d %d %d %d %d %d %d
\n
"
,
(
int
)
sum
[
0
],
(
int
)
sum
[
1
],
(
int
)
sum
[
2
],
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment