Skip to content

Instantly share code, notes, and snippets.

@cnlohr
Last active May 20, 2026 14:33
Show Gist options
  • Select an option

  • Save cnlohr/c512bc9eaa4f54bc157aae0a84037eef to your computer and use it in GitHub Desktop.

Select an option

Save cnlohr/c512bc9eaa4f54bc157aae0a84037eef to your computer and use it in GitHub Desktop.
Inductance measurement on ch32v003
// Assume setup from touch control
// Connect 20 turns of ~2" wide 28AWG wire between PA2 and GND.
void RunTest()__attribute__((noinline, section(".srodata")));
void DelaySysTick_RAM( uint32_t n ) __attribute__((noinline, section(".srodata")));
void DelaySysTick_RAM( uint32_t n )
{
#if defined(CH32V003) || defined(CH32V00x)
uint32_t targend = SysTick->CNT + n;
while( ((int32_t)( SysTick->CNT - targend )) < 0 );
#elif defined(CH32V20x) || defined(CH32V30x) || defined(CH32X03x) || defined(CH32L103) || defined(CH582_CH583) || defined(CH591_CH592) || defined(CH32H41x)
uint64_t targend = SysTick->CNT + n;
while( ((int64_t)( SysTick->CNT - targend )) < 0 );
#elif defined(CH32V10x) || defined(CH570_CH572) || defined(CH584_CH585)
uint32_t targend = SysTick->CNTL + n;
while( ((int32_t)( SysTick->CNTL - targend )) < 0 );
#elif defined(CH571_CH573)
// ch573 insisted on being special, it's counting down
uint64_t targend = SysTick->CNT - n;
while( ((int64_t)( SysTick->CNT - targend )) > 0 );
#else
#error DelaySysTick not defined.
#endif
}
void RunTest()
{
while(1)
{
int d;
//for( d = 0; d < 80; d+= 10 )
{
int it;
int ret = 0;
for( it = 0; it < 1000; it++ )
{
FORCEALIGNADC
ADC1->CTLR2 = ADC_ADON | ADC_EXTSEL;
funDigitalWrite( PA2, 1 );
DelaySysTick_RAM( 300 );
ADC1->CTLR2 = ADC_SWSTART | ADC_ADON | ADC_EXTSEL;
while(!(ADC1->STATR & ADC_EOC));
ret += ADC1->RDATAR;
funDigitalWrite( PA2, 0 );
}
//printf( "%d%c", ret, (d < 70)?' ':'\n' );
printf( "%d\n", ret );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment