Skip to content

Commit

Permalink
improve resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
GorgonMeducer committed Jan 22, 2024
1 parent fbfad9a commit e964738
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 49 deletions.
4 changes: 2 additions & 2 deletions GorgonMeducer.perf_counter.pdsc
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@
<component Cclass="Utilities" Cversion="2.3.0" Cgroup="perf_counter" Csub="Porting" Cvariant="User Defined" isDefaultVariant="true" condition="perf_counter">
<description>A user define system timer</description>
<files>
<file category="sourceC" name="template/perfc_port_user.c" attr="config" version="1.0.1"/>
<file category="header" name="template/perfc_port_user.h" attr="config" version="1.0.1"/>
<file category="sourceC" name="template/perfc_port_user.c" attr="config" version="1.0.2"/>
<file category="header" name="template/perfc_port_user.h" attr="config" version="1.0.2"/>
</files>

<Pre_Include_Global_h>
Expand Down
71 changes: 37 additions & 34 deletions perf_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ volatile static uint32_t s_wUSUnit = 1;
volatile static uint32_t s_wMSUnit = 1;
volatile static uint32_t s_wMSResidule = 0;
volatile static uint32_t s_wUSResidule = 0;
volatile static uint32_t s_wSystemMS = 0;
volatile static uint32_t s_wSystemUS = 0;
volatile static int64_t s_lSystemMS = 0;
volatile static int64_t s_lSystemUS = 0;

volatile static int64_t s_lSystemClockCounts = 0;

Expand All @@ -82,13 +82,13 @@ volatile static int64_t s_lSystemClockCounts = 0;
extern
uint32_t perfc_port_get_system_freq(void);
extern
uint32_t perfc_port_get_system_timer_top(void);
int64_t perfc_port_get_system_timer_top(void);
extern
bool perfc_port_is_system_timer_ovf_pending(void);
extern
void perfc_port_init_system_timer(void);
void perfc_port_init_system_timer(bool bTimerOccupied);
extern
uint32_t perfc_port_get_system_timer_elapsed(void);
int64_t perfc_port_get_system_timer_elapsed(void);
extern
void perfc_port_clear_system_timer_ovf_pending(void);
extern
Expand All @@ -101,23 +101,28 @@ void perfc_port_clear_system_timer_counter(void);

void user_code_insert_to_systick_handler(void)
{
uint32_t wLoad = perfc_port_get_system_timer_top() + 1;
s_lSystemClockCounts += wLoad;
int64_t lLoad = perfc_port_get_system_timer_top() + 1;
s_lSystemClockCounts += lLoad;

// update system ms counter
do {
s_wMSResidule += wLoad;
uint32_t wMS = s_wMSResidule / s_wMSUnit;
s_wMSResidule -= wMS * s_wMSUnit;
s_wSystemMS += wMS;
int64_t lTemp = s_wMSResidule + lLoad;

int64_t lMS = lTemp / s_wMSUnit;
s_lSystemMS += lMS;
s_wMSResidule = (uint32_t)((int64_t)lTemp - (int64_t)lMS * s_wMSUnit);

} while(0);

// update system us counter
do {
s_wUSResidule += wLoad;
uint32_t wUS = s_wUSResidule / s_wUSUnit;
s_wUSResidule -= wUS * s_wUSUnit;
s_wSystemUS += wUS;
int64_t lTemp = s_wUSResidule + lLoad;

int64_t lUS = lTemp / s_wUSUnit;
s_lSystemUS += lUS;

s_wUSResidule = (uint32_t)((int64_t)lTemp - (int64_t)lUS * s_wUSUnit);

} while(0);

}
Expand All @@ -143,16 +148,14 @@ void update_perf_counter(void)
void init_cycle_counter(bool bIsSysTickOccupied)
{
__IRQ_SAFE {
if (!bIsSysTickOccupied) {
perfc_port_init_system_timer(); // use the longest period
}
perfc_port_init_system_timer(bIsSysTickOccupied); // use the longest period
perfc_port_clear_system_timer_ovf_pending();
}

update_perf_counter();
s_lSystemClockCounts = 0; // reset system cycle counter
s_wSystemMS = 0; // reset system millisecond counter
s_wSystemUS = 0; // reset system microsecond counter
s_lSystemMS = 0; // reset system millisecond counter
s_lSystemUS = 0; // reset system microsecond counter

__perf_os_patch_init();
}
Expand All @@ -161,10 +164,10 @@ void init_cycle_counter(bool bIsSysTickOccupied)
* hence SysTick-LOAD and (SCB->ICSR & SCB_ICSR_PENDSTSET_Msk)
* won't change.
*/
__STATIC_INLINE uint32_t check_systick(void)
__STATIC_INLINE int64_t check_systick(void)
{
uint32_t wLoad = perfc_port_get_system_timer_top() + 1;
uint32_t wTemp = perfc_port_get_system_timer_elapsed();
int64_t lLoad = perfc_port_get_system_timer_top() + 1;
int64_t lTemp = perfc_port_get_system_timer_elapsed();

/* Since we cannot stop counting temporarily, there are several
* conditions which we should take into consideration:
Expand All @@ -187,12 +190,12 @@ __STATIC_INLINE uint32_t check_systick(void)
* The following code implements an equivalent logic.
*/
if (perfc_port_is_system_timer_ovf_pending()){
if ((wLoad - wTemp) >= PERF_CNT_COMPENSATION_THRESHOLD) {
wTemp += wLoad;
if ((lLoad - lTemp) >= PERF_CNT_COMPENSATION_THRESHOLD) {
lTemp += lLoad;
}
}

return wTemp;
return lTemp;
}

void before_cycle_counter_reconfiguration(void)
Expand Down Expand Up @@ -311,26 +314,26 @@ int64_t clock(void)
return get_system_ticks();
}

uint32_t get_system_ms(void)
int64_t get_system_ms(void)
{
uint32_t nTemp = 0;
int64_t lTemp = 0;

__IRQ_SAFE {
nTemp = s_wSystemMS + (check_systick() + s_wMSResidule) / s_wMSUnit;
lTemp = s_lSystemMS + ((check_systick() + (int64_t)s_wMSResidule) / s_wMSUnit);
}

return nTemp;
return lTemp;
}

uint32_t get_system_us(void)
int64_t get_system_us(void)
{
uint32_t wTemp = 0;
int64_t lTemp = 0;

__IRQ_SAFE {
wTemp = s_wSystemUS + (check_systick() + s_wUSResidule) / s_wUSUnit;
lTemp = s_lSystemUS + ((check_systick() + (int64_t)s_wUSResidule) / s_wUSUnit);
}

return wTemp;
return lTemp;
}

int64_t perfc_convert_ticks_to_ms(int64_t lTick)
Expand Down
10 changes: 4 additions & 6 deletions perf_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,17 +661,15 @@ int64_t stop_cycle_counter(void)

/*!
* \brief get the elapsed milliseconds since perf_counter is initialised
* \return uint32_t the elapsed milliseconds
* \return int64_t the elapsed milliseconds
*/
extern uint32_t get_system_ms(void);
extern int64_t get_system_ms(void);

/*!
* \brief get the elapsed microsecond since perf_counter is initialised
* \return uint32_t the elapsed microsecond
* \return int64_t the elapsed microsecond
*/
extern uint32_t get_system_us(void);


extern int64_t get_system_us(void);

/*!
* \brief delay specified time in microsecond
Expand Down
12 changes: 8 additions & 4 deletions perfc_port_default.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,12 @@ extern uint32_t SystemCoreClock;

#if !__PERFC_CFG_DISABLE_DEFAULT_SYSTICK_PORTING__
__WEAK
void perfc_port_init_system_timer(void)
void perfc_port_init_system_timer(bool bTimerOccupied)
{
if (bTimerOccupied) {
return ;
}

__IRQ_SAFE {
SysTick->CTRL = 0;

Expand All @@ -188,15 +192,15 @@ bool perfc_port_is_system_timer_ovf_pending(void)
}

__WEAK
uint32_t perfc_port_get_system_timer_top(void)
int64_t perfc_port_get_system_timer_top(void)
{
return SysTick->LOAD;
}

__WEAK
uint32_t perfc_port_get_system_timer_elapsed(void)
int64_t perfc_port_get_system_timer_elapsed(void)
{
return (uint32_t)SysTick->LOAD - (uint32_t)SysTick->VAL;
return (int64_t)SysTick->LOAD - (uint32_t)SysTick->VAL;
}

__WEAK
Expand Down
10 changes: 7 additions & 3 deletions template/perfc_port_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@

#if __PERFC_USE_USER_CUSTOM_PORTING__

void perfc_port_init_system_timer(void)
void perfc_port_init_system_timer(bool bIsTimeOccupied)
{
if (bIsTimeOccupied) {
return ;
}

__IRQ_SAFE {
/* Configure the system timer count with the longest possible period
* clear counter
Expand All @@ -73,13 +77,13 @@ bool perfc_port_is_system_timer_ovf_pending(void)
return false;
}

uint32_t perfc_port_get_system_timer_top(void)
int64_t perfc_port_get_system_timer_top(void)
{
/* the top value of the counting */
return 0;
}

uint32_t perfc_port_get_system_timer_elapsed(void)
int64_t perfc_port_get_system_timer_elapsed(void)
{
/* the elapsed count number since last overflow */
return 0;
Expand Down

0 comments on commit e964738

Please sign in to comment.