Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Almost Cycle-accurate measurements #2

Open
cnlohr opened this issue Nov 7, 2023 · 1 comment
Open

Almost Cycle-accurate measurements #2

cnlohr opened this issue Nov 7, 2023 · 1 comment

Comments

@cnlohr
Copy link

cnlohr commented Nov 7, 2023

  • You can use SYSCLK to get a 32-bit timer count for cycles from the system clock, so you don't have to worry about overflows
  • You can use the AWU IRQ Handler to get interrupts when the AWU triggers.

This lets you capture the data very quickly, and with much more precision than currently available.

#include "ch32v003fun.h"
#include <stdio.h>

#define ITERATIONS 1
#define SERIALPLOT

volatile uint32_t ldelta;
uint32_t llast;

void AWU_IRQHandler( void ) __attribute__((section(".text.vector_handler")))  __attribute__((interrupt));

void AWU_IRQHandler( void )
{
	uint32_t l = SysTick->CNT;
	ldelta = (l - llast);
	llast = l;
	EXTI->INTFR = EXTI_Line9;
}

int main()
{
	SystemInit();

	// enable power interface module clock
	RCC->APB1PCENR |= RCC_APB1Periph_PWR | RCC_APB1Periph_WWDG;

	// enable low speed oscillator (LSI)
	RCC->RSTSCKR |= RCC_LSION;
	while ((RCC->RSTSCKR & RCC_LSIRDY) == 0) {}

	// enable AutoWakeUp event
	EXTI->FTENR |= EXTI_Line9;
	EXTI->INTENR |= EXTI_Line9;

	// configure AWU prescaler and configure AWU window comparison value
	// /16 /(63+1) ~= 120Hz
	PWR->AWUPSC = PWR_AWU_Prescaler_8;
	PWR->AWUWR = 63;
	PWR->AWUCSR = (1 << 1); // AWUEN

	// enable interrupt
	NVIC_EnableIRQ( AWU_IRQn );

	uint32_t t;
	uint32_t lastt = TIM1->CNT;

	while(1)
	{
		// Wait for data to become available.	
		uint32_t printtime = 0;
		while( !( printtime = ldelta ) );
		ldelta = 0;
		printf("%d\n", printtime ); //raw_to_temperature(count));
	}
}

Just tested it at my desk here.

Also, I am seeing significant variation per part.

Part Test Count at 25C Count at -19 C Counts Per Deg C
1 200300 203100 63
2 211000 215000 90
3 189500 192100 59

Additionally flex your PCB

Quescent at 25 C: 189900
Diagonal Torsion: 1888537
Inverse Torsion: 191050

Just using my little PCBs I had at my desk applying about 1 N*cm of force to the PCB.

@cnlohr cnlohr changed the title Cycle-accurate measurements Almost Cycle-accurate measurements Nov 7, 2023
@eeucalyptus
Copy link
Owner

I'll look into it tonight, thank you for the input!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants