NMSIS-Core  Version 1.4.0
NMSIS-Core support for Nuclei processor-based devices
SysTimer Functions

Functions that configure the Core System Timer. More...

Macros

#define SysTimer_GetHartID()   (__get_hart_index())
 SysTimer_GetHartID() is used to get timer hartid which might not be the same as cpu hart id, for example, cpu hartid may be 1, but timer hartid may be 0, then timer hartid offset is 1. More...
 
#define SysTimer_GetHartID_S()   (__get_hart_index_s())
 

Functions

__STATIC_FORCEINLINE void SysTimer_SetLoadValue (uint64_t value)
 Set system timer load value. More...
 
__STATIC_FORCEINLINE uint64_t SysTimer_GetLoadValue (void)
 Get system timer load value. More...
 
__STATIC_FORCEINLINE void SysTimer_SetHartCompareValue (uint64_t value, unsigned long hartid)
 Set system timer compare value by hartid. More...
 
__STATIC_FORCEINLINE void SysTimer_SetCompareValue (uint64_t value)
 Set system timer compare value in machine mode. More...
 
__STATIC_FORCEINLINE uint64_t SysTimer_GetHartCompareValue (unsigned long hartid)
 Get system timer compare value by hartid. More...
 
__STATIC_FORCEINLINE uint64_t SysTimer_GetCompareValue (void)
 Get system timer compare value in machine mode. More...
 
__STATIC_FORCEINLINE void SysTimer_Start (void)
 Enable system timer counter running. More...
 
__STATIC_FORCEINLINE void SysTimer_Stop (void)
 Stop system timer counter running. More...
 
__STATIC_FORCEINLINE void SysTimer_SetControlValue (uint32_t mctl)
 Set system timer control value. More...
 
__STATIC_FORCEINLINE uint32_t SysTimer_GetControlValue (void)
 Get system timer control value. More...
 
__STATIC_FORCEINLINE void SysTimer_SetHartSWIRQ (unsigned long hartid)
 Trigger or set software interrupt via system timer by hartid. More...
 
__STATIC_FORCEINLINE void SysTimer_SetSWIRQ (void)
 Trigger or set software interrupt via system timer in machine mode. More...
 
__STATIC_FORCEINLINE void SysTimer_ClearHartSWIRQ (unsigned long hartid)
 Clear system timer software interrupt pending request by hartid. More...
 
__STATIC_FORCEINLINE void SysTimer_ClearSWIRQ (void)
 Clear system timer software interrupt pending request in machine mode. More...
 
__STATIC_FORCEINLINE uint32_t SysTimer_GetHartMsipValue (unsigned long hartid)
 Get system timer MSIP register value by hartid. More...
 
__STATIC_FORCEINLINE uint32_t SysTimer_GetMsipValue (void)
 Get system timer MSIP register value in machine mode. More...
 
__STATIC_FORCEINLINE void SysTimer_SetHartMsipValue (uint32_t msip, unsigned long hartid)
 Set system timer MSIP register value by hartid. More...
 
__STATIC_FORCEINLINE void SysTimer_SetMsipValue (uint32_t msip)
 Set system timer MSIP register value in machine mode. More...
 
__STATIC_FORCEINLINE void SysTimer_SetCompareValue_S (uint64_t value)
 Set system timer smode compare value. More...
 
__STATIC_FORCEINLINE uint64_t SysTimer_GetCompareValue_S (void)
 Get system timer smode compare value. More...
 
__STATIC_FORCEINLINE void SysTimer_SetHartSWIRQ_S (unsigned long hartid)
 Trigger or set supervisor mode software interrupt via system timer by hartid. More...
 
__STATIC_FORCEINLINE void SysTimer_SetSWIRQ_S (void)
 Trigger or set supervisor software interrupt via system timer. More...
 
__STATIC_FORCEINLINE void SysTimer_ClearHartSWIRQ_S (unsigned long hartid)
 Clear system timer supervisor mode software interrupt pending request by hartid. More...
 
__STATIC_FORCEINLINE void SysTimer_ClearSWIRQ_S (void)
 Clear system timer supervisor mode software interrupt pending request. More...
 
__STATIC_FORCEINLINE uint32_t SysTimer_GetHartSsipValue (unsigned long hartid)
 Get system timer SSIP register value by hartid. More...
 
__STATIC_FORCEINLINE uint32_t SysTimer_GetSsipValue (void)
 Get system timer SSIP register value. More...
 
__STATIC_FORCEINLINE void SysTimer_SetHartSsipValue (uint32_t ssip, unsigned long hartid)
 Set system timer SSIP register value by hartid. More...
 
__STATIC_FORCEINLINE void SysTimer_SetSsipValue (uint32_t ssip)
 Set system timer SSIP register value. More...
 
__STATIC_FORCEINLINE void SysTimer_SendIPI_S (unsigned long hartid)
 send ipi to target hart using Systimer Clint SSIP More...
 
__STATIC_FORCEINLINE void SysTimer_ClearIPI_S (unsigned long hartid)
 clear ipi to target hart using Systimer Clint SSIP More...
 
__STATIC_FORCEINLINE void SysTimer_EnableSSTC (void)
 Enable SSTC access right for supervisor mode. More...
 
__STATIC_FORCEINLINE void SysTimer_DisableSSTC (void)
 Disable SSTC stimecmp csr access right for supervisor mode. More...
 
__STATIC_FORCEINLINE void SysTimer_SoftwareReset (void)
 Do software reset request. More...
 
__STATIC_FORCEINLINE void SysTimer_SendIPI (unsigned long hartid)
 send ipi to target hart using Systimer Clint MSIP More...
 
__STATIC_FORCEINLINE void SysTimer_ClearIPI (unsigned long hartid)
 clear ipi to target hart using Systimer Clint MSIP More...
 
__STATIC_FORCEINLINE void SysTimer_EnableSAccess (void)
 Enable supervisor mode systimer register read/write access right. More...
 
__STATIC_FORCEINLINE void SysTimer_DisableSAccess (void)
 Disable supervisor mode systimer register read/write access right. More...
 
__STATIC_INLINE uint32_t SysTick_Config (uint64_t ticks)
 System Tick Configuration. More...
 
__STATIC_INLINE uint32_t SysTick_HartConfig (uint64_t ticks, unsigned long hartid)
 System Tick Configuration By hartid. More...
 
__STATIC_FORCEINLINE uint32_t SysTick_Reload (uint64_t ticks)
 System Tick Reload. More...
 
__STATIC_FORCEINLINE uint32_t SysTick_HartReload (uint64_t ticks, unsigned long hartid)
 System Tick Reload By hartid. More...
 
__STATIC_INLINE uint32_t SysTick_Config_S (uint64_t ticks)
 System Tick Configuration in supervisor mode. More...
 
__STATIC_FORCEINLINE uint32_t SysTick_Reload_S (uint64_t ticks)
 System Tick Reload in supervisor mode. More...
 

Detailed Description

Functions that configure the Core System Timer.

Macro Definition Documentation

◆ SysTimer_GetHartID

#define SysTimer_GetHartID ( )    (__get_hart_index())

SysTimer_GetHartID() is used to get timer hartid which might not be the same as cpu hart id, for example, cpu hartid may be 1, but timer hartid may be 0, then timer hartid offset is 1.

If defined __SYSTIMER_HARTID, it will use __SYSTIMER_HARTID as timer hartid, otherwise, it will use __get_hart_index(). The cpu hartid is get by using __get_hart_id function

Definition at line 127 of file core_feature_timer.h.

◆ SysTimer_GetHartID_S

#define SysTimer_GetHartID_S ( )    (__get_hart_index_s())

Definition at line 134 of file core_feature_timer.h.

Function Documentation

◆ SysTick_Config()

__STATIC_INLINE uint32_t SysTick_Config ( uint64_t  ticks)

System Tick Configuration.

Initializes the System Timer and its non-vector interrupt, and starts the System Tick Timer.

In our default implementation, the timer counter will be set to zero, and it will start a timer compare non-vector interrupt when it matchs the ticks user set, during the timer interrupt user should reload the system tick using SysTick_Reload function or similar function written by user, so it can produce period timer interrupt.

Parameters
[in]ticksNumber of ticks between two interrupts.
Returns
0 Function succeeded.
1 Function failed.
Remarks
  • For __NUCLEI_N_REV >= 0x0104, the CMPCLREN bit in MTIMECTL is introduced, but we assume that the CMPCLREN bit is set to 0, so MTIMER register will not be auto cleared to 0 when MTIMER >= MTIMERCMP.
  • When the variable __Vendor_SysTickConfig is set to 1, then the function SysTick_Config is not included.
  • In this case, the file <Device>.h must contain a vendor-specific implementation of this function.
  • If user need this function to start a period timer interrupt, then in timer interrupt handler routine code, user should call SysTick_Reload with ticks to reload the timer.
  • This function only available when __SYSTIMER_PRESENT == 1 and __ECLIC_PRESENT == 1 and __Vendor_SysTickConfig == 0
See also

Definition at line 834 of file core_feature_timer.h.

835 {
836  uint64_t loadticks = SysTimer_GetLoadValue();
837  SysTimer_SetCompareValue(ticks + loadticks);
838 
839 #if defined(__ECLIC_PRESENT) && (__ECLIC_PRESENT == 1)
843 #endif
844  return (0UL);
845 }
#define ECLIC_NON_VECTOR_INTERRUPT
Non-Vector Interrupt Mode of ECLIC.
#define ECLIC_SetShvIRQ
#define ECLIC_SetLevelIRQ
#define ECLIC_EnableIRQ
@ SysTimer_IRQn
System Timer Interrupt.
__STATIC_FORCEINLINE void SysTimer_SetCompareValue(uint64_t value)
Set system timer compare value in machine mode.
__STATIC_FORCEINLINE uint64_t SysTimer_GetLoadValue(void)
Get system timer load value.

References ECLIC_EnableIRQ, ECLIC_NON_VECTOR_INTERRUPT, ECLIC_SetLevelIRQ, ECLIC_SetShvIRQ, SysTimer_GetLoadValue(), SysTimer_IRQn, and SysTimer_SetCompareValue().

◆ SysTick_Config_S()

__STATIC_INLINE uint32_t SysTick_Config_S ( uint64_t  ticks)

System Tick Configuration in supervisor mode.

Initializes the system timer supervisor mode interrupt and registered as non-vector interrupt

The smode timecmp csr register will be set to proper value according to the ticks passed when it matchs the ticks user set, during the smode timer interrupt user should reload the system tick using SysTick_Reload_S function or similar function written by user, so it can produce period smode timer interrupt.

Parameters
[in]ticksNumber of ticks between two interrupts.
Returns
0 Function succeeded.

Definition at line 967 of file core_feature_timer.h.

968 {
969  uint64_t loadticks = __get_rv_time();
970  SysTimer_SetCompareValue_S(ticks + loadticks);
971 
972 #if defined(__ECLIC_PRESENT) && (__ECLIC_PRESENT == 1)
973 #if defined(__TEE_PRESENT) && (__TEE_PRESENT == 1)
975  ECLIC_SetLevelIRQ_S(SysTimer_S_IRQn, 0);
976  ECLIC_EnableIRQ_S(SysTimer_S_IRQn);
977 #endif
978 #endif
979  return (0UL);
980 }
__STATIC_FORCEINLINE uint64_t __get_rv_time(void)
Read whole 64 bits value of real-time clock.
#define ECLIC_SetShvIRQ_S
#define ECLIC_EnableIRQ_S
#define ECLIC_SetLevelIRQ_S
__STATIC_FORCEINLINE void SysTimer_SetCompareValue_S(uint64_t value)
Set system timer smode compare value.

References __get_rv_time(), ECLIC_EnableIRQ_S, ECLIC_NON_VECTOR_INTERRUPT, ECLIC_SetLevelIRQ_S, ECLIC_SetShvIRQ_S, and SysTimer_SetCompareValue_S().

◆ SysTick_HartConfig()

__STATIC_INLINE uint32_t SysTick_HartConfig ( uint64_t  ticks,
unsigned long  hartid 
)

System Tick Configuration By hartid.

Initializes the System Timer and its non-vector interrupt, and starts the System Tick Timer.

In our default implementation, the timer counter will be set to zero, and it will start a timer compare non-vector interrupt when it matchs the ticks user set, during the timer interrupt user should reload the system tick using SysTick_Reload function or similar function written by user, so it can produce period timer interrupt.

Parameters
[in]ticksNumber of ticks between two interrupts.
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Returns
0 Function succeeded.
1 Function failed.
Remarks
  • For __NUCLEI_N_REV >= 0x0104, the CMPCLREN bit in MTIMECTL is introduced, but we assume that the CMPCLREN bit is set to 0, so MTIMER register will not be auto cleared to 0 when MTIMER >= MTIMERCMP.
  • When the variable __Vendor_SysTickConfig is set to 1, then the function SysTick_Config is not included.
  • In this case, the file <Device>.h must contain a vendor-specific implementation of this function.
  • If user need this function to start a period timer interrupt, then in timer interrupt handler routine code, user should call SysTick_Reload with ticks to reload the timer.
  • This function only available __Vendor_SysTickConfig == 0
  • When ECLIC present, it will set eclic timer interrupt as non-vector mode, and level configured to 0, and enable it
  • In S-mode, hartid can't be get by using __get_hart_id function, so this api suits S-mode particularly.
See also

Definition at line 874 of file core_feature_timer.h.

875 {
876  uint64_t loadticks = SysTimer_GetLoadValue();
877  SysTimer_SetHartCompareValue(ticks + loadticks, hartid);
878 
879 #if defined(__ECLIC_PRESENT) && (__ECLIC_PRESENT == 1)
883 #endif
884  return (0UL);
885 }
__STATIC_FORCEINLINE void SysTimer_SetHartCompareValue(uint64_t value, unsigned long hartid)
Set system timer compare value by hartid.

References ECLIC_EnableIRQ, ECLIC_NON_VECTOR_INTERRUPT, ECLIC_SetLevelIRQ, ECLIC_SetShvIRQ, SysTimer_GetLoadValue(), SysTimer_IRQn, and SysTimer_SetHartCompareValue().

◆ SysTick_HartReload()

__STATIC_FORCEINLINE uint32_t SysTick_HartReload ( uint64_t  ticks,
unsigned long  hartid 
)

System Tick Reload By hartid.

Reload the System Timer Tick when the MTIMECMP reached TIME value

Parameters
[in]ticksNumber of ticks between two interrupts.
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Returns
0 Function succeeded.
1 Function failed.
Remarks
  • For __NUCLEI_N_REV >= 0x0104, the CMPCLREN bit in MTIMECTL is introduced, but for this SysTick_Config function, we assume this CMPCLREN bit is set to 0, so in interrupt handler function, user still need to set the MTIMERCMP or MTIMER to reload the system tick, if vendor want to use this timer's auto clear feature, they can define __Vendor_SysTickConfig to 1, and implement SysTick_Config and SysTick_Reload functions.
  • When the variable __Vendor_SysTickConfig is set to 1, then the function SysTick_Reload is not included.
  • In this case, the file <Device>.h must contain a vendor-specific implementation of this function.
  • This function only available when __SYSTIMER_PRESENT == 1 and __ECLIC_PRESENT == 1 and __Vendor_SysTickConfig == 0
  • Since the MTIMERCMP value might overflow, if overflowed, MTIMER will be set to 0, and MTIMERCMP set to ticks
  • In S-mode, hartid can't be get by using __get_hart_id function, so this api suits S-mode particularly.
See also

Definition at line 946 of file core_feature_timer.h.

947 {
948  uint64_t cur_ticks = SysTimer_GetLoadValue();
949  uint64_t reload_ticks = ticks + cur_ticks;
950 
951  SysTimer_SetHartCompareValue(reload_ticks, hartid);
952 
953  return (0UL);
954 }

References SysTimer_GetLoadValue(), and SysTimer_SetHartCompareValue().

◆ SysTick_Reload()

__STATIC_FORCEINLINE uint32_t SysTick_Reload ( uint64_t  ticks)

System Tick Reload.

Reload the System Timer Tick when the MTIMECMP reached TIME value

Parameters
[in]ticksNumber of ticks between two interrupts.
Returns
0 Function succeeded.
1 Function failed.
Remarks
  • For __NUCLEI_N_REV >= 0x0104, the CMPCLREN bit in MTIMECTL is introduced, but for this SysTick_Config function, we assume this CMPCLREN bit is set to 0, so in interrupt handler function, user still need to set the MTIMERCMP or MTIMER to reload the system tick, if vendor want to use this timer's auto clear feature, they can define __Vendor_SysTickConfig to 1, and implement SysTick_Config and SysTick_Reload functions.
  • When the variable __Vendor_SysTickConfig is set to 1, then the function SysTick_Reload is not included.
  • In this case, the file <Device>.h must contain a vendor-specific implementation of this function.
  • This function only available when __SYSTIMER_PRESENT == 1
  • Since the MTIMERCMP value might overflow, if overflowed, MTIMER will be set to 0, and MTIMERCMP set to ticks
See also

Definition at line 910 of file core_feature_timer.h.

911 {
912  uint64_t cur_ticks = SysTimer_GetLoadValue();
913 
914  uint64_t reload_ticks = ticks + cur_ticks;
915 
916  SysTimer_SetCompareValue(reload_ticks);
917 
918  return (0UL);
919 }

References SysTimer_GetLoadValue(), and SysTimer_SetCompareValue().

◆ SysTick_Reload_S()

__STATIC_FORCEINLINE uint32_t SysTick_Reload_S ( uint64_t  ticks)

System Tick Reload in supervisor mode.

Reload the System Timer Tick when the STIMECMP CSR reached TIME value

Parameters
[in]ticksNumber of ticks between two interrupts.
Returns
0 Function succeeded.
1 Function failed.

Definition at line 990 of file core_feature_timer.h.

991 {
992  uint64_t cur_ticks = __get_rv_time();
993 
994  uint64_t reload_ticks = ticks + cur_ticks;
995 
996  SysTimer_SetCompareValue_S(reload_ticks);
997 
998  return (0UL);
999 }

References __get_rv_time(), and SysTimer_SetCompareValue_S().

◆ SysTimer_ClearHartSWIRQ()

__STATIC_FORCEINLINE void SysTimer_ClearHartSWIRQ ( unsigned long  hartid)

Clear system timer software interrupt pending request by hartid.

This function clear the system timer MSIP bit in MSIP register.

Parameters
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Remarks
  • Clear system timer MSIP bit in MSIP register to clear the software interrupt pending.
  • In S-mode, hartid can't be get by using __get_hart_id function, so this api suits S-mode particularly.
  • SysTimer_SetHartSWIRQ
  • SysTimer_GetHartMsipValue

Definition at line 396 of file core_feature_timer.h.

397 {
398  if (hartid == 0) {
400  } else {
401  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_MSIP_BASE(hartid));
402  __SW(addr, 0);
403  }
404 }
__STATIC_FORCEINLINE void __SW(volatile void *addr, uint32_t val)
Write 32bit value to address (32 bit)
#define SysTimer
SysTick configuration struct.
#define SysTimer_MSIP_MSIP_Msk
SysTick Timer MSIP: MSIP Mask.
#define SysTimer_CLINT_MSIP_BASE(hartid)

References __SW(), SysTimer, SysTimer_CLINT_MSIP_BASE, and SysTimer_MSIP_MSIP_Msk.

Referenced by SysTimer_ClearSWIRQ().

◆ SysTimer_ClearHartSWIRQ_S()

__STATIC_FORCEINLINE void SysTimer_ClearHartSWIRQ_S ( unsigned long  hartid)

Clear system timer supervisor mode software interrupt pending request by hartid.

This function clear the system timer SSIP bit in SSIP register.

Parameters
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Remarks

Definition at line 589 of file core_feature_timer.h.

590 {
591  if (hartid == 0) {
593  } else {
594  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_SSIP_BASE(hartid));
595  __SW(addr, 0);
596  }
597 }
#define SysTimer_CLINT_SSIP_BASE(hartid)
#define SysTimer_SSIP_SSIP_Msk
SysTick Timer SSIP: SSIP Mask.

References __SW(), SysTimer, SysTimer_CLINT_SSIP_BASE, and SysTimer_SSIP_SSIP_Msk.

Referenced by SysTimer_ClearSWIRQ_S().

◆ SysTimer_ClearIPI()

__STATIC_FORCEINLINE void SysTimer_ClearIPI ( unsigned long  hartid)

clear ipi to target hart using Systimer Clint MSIP

This function clear ipi using clint timer's MSIP register.

Parameters
[in]harttarget hart

Definition at line 780 of file core_feature_timer.h.

781 {
782  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_MSIP_BASE(hartid));
783  __SW(addr, 0);
784 }

References __SW(), and SysTimer_CLINT_MSIP_BASE.

◆ SysTimer_ClearIPI_S()

__STATIC_FORCEINLINE void SysTimer_ClearIPI_S ( unsigned long  hartid)

clear ipi to target hart using Systimer Clint SSIP

This function clear ipi using clint timer's SSIP register.

Parameters
[in]harttarget hart

Definition at line 705 of file core_feature_timer.h.

706 {
707  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_SSIP_BASE(hartid));
708  __SW(addr, 0);
709 }

References __SW(), and SysTimer_CLINT_SSIP_BASE.

◆ SysTimer_ClearSWIRQ()

__STATIC_FORCEINLINE void SysTimer_ClearSWIRQ ( void  )

Clear system timer software interrupt pending request in machine mode.

This function clear the system timer MSIP bit in MSIP register.

Remarks

Definition at line 415 of file core_feature_timer.h.

416 {
417  unsigned long hartid = SysTimer_GetHartID();
418  SysTimer_ClearHartSWIRQ(hartid);
419 }
#define SysTimer_GetHartID()
SysTimer_GetHartID() is used to get timer hartid which might not be the same as cpu hart id,...
__STATIC_FORCEINLINE void SysTimer_ClearHartSWIRQ(unsigned long hartid)
Clear system timer software interrupt pending request by hartid.

References SysTimer_ClearHartSWIRQ(), and SysTimer_GetHartID.

◆ SysTimer_ClearSWIRQ_S()

__STATIC_FORCEINLINE void SysTimer_ClearSWIRQ_S ( void  )

Clear system timer supervisor mode software interrupt pending request.

This function clear the system timer SSIP bit in SSIP register.

Remarks

Definition at line 608 of file core_feature_timer.h.

609 {
610  unsigned long hartid = SysTimer_GetHartID_S();
612 }
#define SysTimer_GetHartID_S()
__STATIC_FORCEINLINE void SysTimer_ClearHartSWIRQ_S(unsigned long hartid)
Clear system timer supervisor mode software interrupt pending request by hartid.

References SysTimer_ClearHartSWIRQ_S(), and SysTimer_GetHartID_S.

◆ SysTimer_DisableSAccess()

__STATIC_FORCEINLINE void SysTimer_DisableSAccess ( void  )

Disable supervisor mode systimer register read/write access right.

This will make some systimer registers can not be accessed in supervisor mode.

Attention
This can be only executed in machine mode

Definition at line 803 of file core_feature_timer.h.

804 {
805  SysTimer->MTIME_SRW_CTRL = 1;
806 }

References SysTimer.

◆ SysTimer_DisableSSTC()

__STATIC_FORCEINLINE void SysTimer_DisableSSTC ( void  )

Disable SSTC stimecmp csr access right for supervisor mode.

This will disable SSTC stimecmp(h) csr register read/write access right in supervisor mode and make time(h) csr can not be read

Definition at line 734 of file core_feature_timer.h.

735 {
737 #if __RISCV_XLEN == 32
739 #else
741 #endif
742 }
#define MCOUNTEREN_TIME
#define MENVCFGH_STCE
#define MENVCFG_STCE
#define __RV_CSR_CLEAR(csr, val)
CSR operation Macro for csrc instruction.
#define CSR_MCOUNTEREN
#define CSR_MENVCFG
#define CSR_MENVCFGH

References __RV_CSR_CLEAR, CSR_MCOUNTEREN, CSR_MENVCFG, CSR_MENVCFGH, MCOUNTEREN_TIME, MENVCFG_STCE, and MENVCFGH_STCE.

◆ SysTimer_EnableSAccess()

__STATIC_FORCEINLINE void SysTimer_EnableSAccess ( void  )

Enable supervisor mode systimer register read/write access right.

This will make some systimer registers can be accessed in supervisor mode, which is reset state.

Attention
This can be only executed in machine mode

Definition at line 792 of file core_feature_timer.h.

793 {
794  SysTimer->MTIME_SRW_CTRL = 0;
795 }

References SysTimer.

◆ SysTimer_EnableSSTC()

__STATIC_FORCEINLINE void SysTimer_EnableSSTC ( void  )

Enable SSTC access right for supervisor mode.

This will enable SSTC stimecmp(h) csr register read/write access right in supervisor mode and make time(h) csr can be read

Attention
This can be only executed in machine mode

Definition at line 718 of file core_feature_timer.h.

719 {
721 #if __RISCV_XLEN == 32
723 #else
725 #endif
726 }
#define __RV_CSR_SET(csr, val)
CSR operation Macro for csrs instruction.

References __RV_CSR_SET, CSR_MCOUNTEREN, CSR_MENVCFG, CSR_MENVCFGH, MCOUNTEREN_TIME, MENVCFG_STCE, and MENVCFGH_STCE.

◆ SysTimer_GetCompareValue()

__STATIC_FORCEINLINE uint64_t SysTimer_GetCompareValue ( void  )

Get system timer compare value in machine mode.

This function get the system timer compare value in MTIMERCMP register.

Returns
compare value of system timer MTIMERCMP register.
Remarks

Definition at line 289 of file core_feature_timer.h.

290 {
291  unsigned long hartid = SysTimer_GetHartID();
292  return SysTimer_GetHartCompareValue(hartid);
293 }
__STATIC_FORCEINLINE uint64_t SysTimer_GetHartCompareValue(unsigned long hartid)
Get system timer compare value by hartid.

References SysTimer_GetHartCompareValue(), and SysTimer_GetHartID.

◆ SysTimer_GetCompareValue_S()

__STATIC_FORCEINLINE uint64_t SysTimer_GetCompareValue_S ( void  )

Get system timer smode compare value.

This function get the system timer smode compare value in STIMERCMP and STIMECMPH CSR register.

Returns
compare value of system timer smode compare csr register in 64bit.
Remarks

Definition at line 529 of file core_feature_timer.h.

530 {
531  uint64_t full;
532 #if __RISCV_XLEN == 32
533  uint32_t high, low;
534 
537  full = (((uint64_t)high) << 32) | low;
538 #else
539  full = __RV_CSR_READ(CSR_STIMECMP);
540 #endif
541  return full;
542 }
#define __RV_CSR_READ(csr)
CSR operation Macro for csrr instruction.
#define CSR_STIMECMPH
#define CSR_STIMECMP

References __RV_CSR_READ, CSR_STIMECMP, and CSR_STIMECMPH.

◆ SysTimer_GetControlValue()

__STATIC_FORCEINLINE uint32_t SysTimer_GetControlValue ( void  )

Get system timer control value.

This function get the system timer MTIMECTL register value.

Returns
MTIMECTL register value
Remarks

Definition at line 344 of file core_feature_timer.h.

345 {
346  return (SysTimer->MTIMECTL & SysTimer_MTIMECTL_Msk);
347 }
#define SysTimer_MTIMECTL_Msk
SysTick Timer MTIMECTL/MSTOP value Mask.

References SysTimer, and SysTimer_MTIMECTL_Msk.

◆ SysTimer_GetHartCompareValue()

__STATIC_FORCEINLINE uint64_t SysTimer_GetHartCompareValue ( unsigned long  hartid)

Get system timer compare value by hartid.

This function get the system timer compare value in MTIMERCMP register.

Parameters
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Returns
compare value of system timer MTIMERCMP register.
Remarks
  • Compare value is 64bits wide.
  • In S-mode, hartid can't be get by using __get_hart_id function, so this api suits S-mode particularly.
  • SysTimer_SetHartCompareValue

Definition at line 259 of file core_feature_timer.h.

260 {
261  if (hartid == 0) {
262  return SysTimer->MTIMERCMP;
263  } else {
264  uint64_t full;
265  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_MTIMECMP_BASE(hartid));
266 #if __RISCV_XLEN == 32
267  // MTIMECMP didn't increase
268  uint32_t high, low;
269 
270  high = __LW(addr + 4);
271  low = __LW(addr);
272  full = (((uint64_t)high) << 32) | low;
273 #else
274  full = __LD(addr);
275 #endif
276  return full;
277  }
278 }
__STATIC_FORCEINLINE uint32_t __LW(volatile void *addr)
Load 32bit value from address (32 bit)
#define SysTimer_CLINT_MTIMECMP_BASE(hartid)

References __LW(), SysTimer, and SysTimer_CLINT_MTIMECMP_BASE.

Referenced by SysTimer_GetCompareValue().

◆ SysTimer_GetHartMsipValue()

__STATIC_FORCEINLINE uint32_t SysTimer_GetHartMsipValue ( unsigned long  hartid)

Get system timer MSIP register value by hartid.

This function get the system timer MSIP register value.

Parameters
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Returns
Value of Timer MSIP register.
Remarks

Definition at line 435 of file core_feature_timer.h.

436 {
437  if (hartid == 0) {
438  return (uint32_t)(SysTimer->MSIP);
439  } else {
440  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_MSIP_BASE(hartid));
441  return __LW(addr);
442  }
443 }

References __LW(), SysTimer, and SysTimer_CLINT_MSIP_BASE.

Referenced by SysTimer_GetMsipValue().

◆ SysTimer_GetHartSsipValue()

__STATIC_FORCEINLINE uint32_t SysTimer_GetHartSsipValue ( unsigned long  hartid)

Get system timer SSIP register value by hartid.

This function get the system timer SSIP register value.

Parameters
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Returns
Value of Timer SSIP register.
Remarks

Definition at line 627 of file core_feature_timer.h.

628 {
629  if (hartid == 0) {
630  return (uint32_t)(SysTimer->SSIP);
631  } else {
632  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_SSIP_BASE(hartid));
633  return __LW(addr);
634  }
635 }

References __LW(), SysTimer, and SysTimer_CLINT_SSIP_BASE.

Referenced by SysTimer_GetSsipValue().

◆ SysTimer_GetLoadValue()

__STATIC_FORCEINLINE uint64_t SysTimer_GetLoadValue ( void  )

Get system timer load value.

This function get the system timer current value in MTIMER register.

Returns
current value(64bit) of system timer MTIMER register.
Remarks

Definition at line 171 of file core_feature_timer.h.

172 {
173 #if __RISCV_XLEN == 32
174  volatile uint32_t high0, low, high;
175  uint64_t full;
176  uint8_t *addr;
177 
178  addr = (uint8_t *)(&(SysTimer->MTIMER));
179 
180  high0 = __LW(addr + 4);
181  low = __LW(addr);
182  high = __LW(addr + 4);
183  if (high0 != high) {
184  low = __LW(addr);
185  }
186  full = (((uint64_t)high) << 32) | low;
187  return full;
188 #else
189  return SysTimer->MTIMER;
190 #endif
191 }

References __LW(), and SysTimer.

Referenced by SysTick_Config(), SysTick_HartConfig(), SysTick_HartReload(), and SysTick_Reload().

◆ SysTimer_GetMsipValue()

__STATIC_FORCEINLINE uint32_t SysTimer_GetMsipValue ( void  )

Get system timer MSIP register value in machine mode.

This function get the system timer MSIP register value.

Returns
Value of Timer MSIP register.
Remarks

Definition at line 457 of file core_feature_timer.h.

458 {
459  unsigned long hartid = SysTimer_GetHartID();
460  return SysTimer_GetHartMsipValue(hartid);
461 }
__STATIC_FORCEINLINE uint32_t SysTimer_GetHartMsipValue(unsigned long hartid)
Get system timer MSIP register value by hartid.

References SysTimer_GetHartID, and SysTimer_GetHartMsipValue().

◆ SysTimer_GetSsipValue()

__STATIC_FORCEINLINE uint32_t SysTimer_GetSsipValue ( void  )

Get system timer SSIP register value.

This function get the system timer MSIP register value.

Returns
Value of Timer MSIP register.
Remarks

Definition at line 649 of file core_feature_timer.h.

650 {
651  unsigned long hartid = SysTimer_GetHartID_S();
652  return SysTimer_GetHartSsipValue(hartid);
653 }
__STATIC_FORCEINLINE uint32_t SysTimer_GetHartSsipValue(unsigned long hartid)
Get system timer SSIP register value by hartid.

References SysTimer_GetHartID_S, and SysTimer_GetHartSsipValue().

◆ SysTimer_SendIPI()

__STATIC_FORCEINLINE void SysTimer_SendIPI ( unsigned long  hartid)

send ipi to target hart using Systimer Clint MSIP

This function send ipi using clint timer's MSIP register.

Parameters
[in]harttarget hart

Definition at line 768 of file core_feature_timer.h.

769 {
770  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_MSIP_BASE(hartid));
771  __SW(addr, 1);
772 }

References __SW(), and SysTimer_CLINT_MSIP_BASE.

◆ SysTimer_SendIPI_S()

__STATIC_FORCEINLINE void SysTimer_SendIPI_S ( unsigned long  hartid)

send ipi to target hart using Systimer Clint SSIP

This function send ipi using clint timer's sSIP register.

Parameters
[in]harttarget hart

Definition at line 693 of file core_feature_timer.h.

694 {
695  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_SSIP_BASE(hartid));
696  __SW(addr, 1);
697 }

References __SW(), and SysTimer_CLINT_SSIP_BASE.

◆ SysTimer_SetCompareValue()

__STATIC_FORCEINLINE void SysTimer_SetCompareValue ( uint64_t  value)

Set system timer compare value in machine mode.

This function set the system Timer compare value in MTIMERCMP register.

Parameters
[in]valuecompare value to set system timer MTIMERCMP register.
Remarks
  • Compare value is 64bits wide.
  • If compare value is larger than current value timer interrupt generate.
  • Modify the load value or compare value less to clear the interrupt.
  • __get_hart_id function can only be accessed in machine mode, or else exception will occur.
  • SysTimer_GetCompareValue

Definition at line 242 of file core_feature_timer.h.

243 {
244  unsigned long hartid = SysTimer_GetHartID();
245  SysTimer_SetHartCompareValue(value, hartid);
246 }

References SysTimer_GetHartID, and SysTimer_SetHartCompareValue().

Referenced by SysTick_Config(), and SysTick_Reload().

◆ SysTimer_SetCompareValue_S()

__STATIC_FORCEINLINE void SysTimer_SetCompareValue_S ( uint64_t  value)

Set system timer smode compare value.

This function set the system timer smode compare value in STIMECMP and STIMECMPH CSR register.

Parameters
[in]valuecompare value to set system timer smode compare register.
Remarks
  • smode timer compare regiser required the SSTC extension
  • smode timer compare value located in stimecmp/stimecmph for rv32 and stimecmp for rv64.
  • If compare value is larger than current value smode timer interrupt generate.
  • Modify the load value or compare value less to clear the interrupt.
  • SysTimer_GetCompareValue_S

Definition at line 509 of file core_feature_timer.h.

510 {
511 #if __RISCV_XLEN == 32
512  __RV_CSR_WRITE(CSR_STIMECMP, -1U); // prevent load > timecmp
513  __RV_CSR_WRITE(CSR_STIMECMPH, (uint32_t)(value >> 32));
514  __RV_CSR_WRITE(CSR_STIMECMP, (uint32_t)value);
515 #else
517 #endif
518 }
#define __RV_CSR_WRITE(csr, val)
CSR operation Macro for csrw instruction.

References __RV_CSR_WRITE, CSR_STIMECMP, and CSR_STIMECMPH.

Referenced by SysTick_Config_S(), and SysTick_Reload_S().

◆ SysTimer_SetControlValue()

__STATIC_FORCEINLINE void SysTimer_SetControlValue ( uint32_t  mctl)

Set system timer control value.

This function set the system timer MTIMECTL register value.

Parameters
[in]mctlvalue to set MTIMECTL register
Remarks
  • Bit TIMESTOP is used to start and stop timer. Clear TIMESTOP bit to 0 to start timer, otherwise to stop timer.
  • Bit CMPCLREN is used to enable auto MTIMER clear to zero when MTIMER >= MTIMERCMP. Clear CMPCLREN bit to 0 to stop auto clear MTIMER feature, otherwise to enable it.
  • Bit CLKSRC is used to select timer clock source. Clear CLKSRC bit to 0 to use mtime_toggle_a, otherwise use core_clk_aon
  • SysTimer_GetControlValue

Definition at line 331 of file core_feature_timer.h.

332 {
333  SysTimer->MTIMECTL = (mctl & SysTimer_MTIMECTL_Msk);
334 }

References SysTimer, and SysTimer_MTIMECTL_Msk.

◆ SysTimer_SetHartCompareValue()

__STATIC_FORCEINLINE void SysTimer_SetHartCompareValue ( uint64_t  value,
unsigned long  hartid 
)

Set system timer compare value by hartid.

This function set the system Timer compare value in MTIMERCMP register.

Parameters
[in]valuecompare value to set system timer MTIMERCMP register.
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Remarks
  • Compare value is 64bits wide.
  • If compare value is larger than current value timer interrupt generate.
  • Modify the load value or compare value less to clear the interrupt.
  • In S-mode, hartid can't be get by using __get_hart_id function, so this api suits S-mode particularly.
  • SysTimer_GetHartCompareValue

Definition at line 206 of file core_feature_timer.h.

207 {
208  if (hartid == 0) {
209 #if __RISCV_XLEN == 32
210  uint8_t *addr;
211  addr = (uint8_t *)(&(SysTimer->MTIMERCMP));
212  __SW(addr, -1U); // prevent load > timecmp
213  __SW(addr + 4, (uint32_t)(value >> 32));
214  __SW(addr, (uint32_t)(value));
215 #else
216  SysTimer->MTIMERCMP = value;
217 #endif
218  } else {
219  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_MTIMECMP_BASE(hartid));
220 #if __RISCV_XLEN == 32
221  __SW(addr, -1U); // prevent load > timecmp
222  __SW(addr + 4, (uint32_t)(value >> 32));
223  __SW(addr, (uint32_t)value);
224 #else
225  __SD(addr, value);
226 #endif
227  }
228 }

References __SW(), SysTimer, and SysTimer_CLINT_MTIMECMP_BASE.

Referenced by SysTick_HartConfig(), SysTick_HartReload(), and SysTimer_SetCompareValue().

◆ SysTimer_SetHartMsipValue()

__STATIC_FORCEINLINE void SysTimer_SetHartMsipValue ( uint32_t  msip,
unsigned long  hartid 
)

Set system timer MSIP register value by hartid.

This function set the system timer MSIP register value.

Parameters
[in]msipvalue to set MSIP register
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Remarks
  • In S-mode, hartid can't be get using __get_hart_id function, so this api suits S-mode particularly.
  • SysTimer_GetHartMsipValue

Definition at line 473 of file core_feature_timer.h.

474 {
475  if (hartid == 0) {
476  SysTimer->MSIP = (msip);
477  } else {
478  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_MSIP_BASE(hartid));
479  __SW(addr, msip);
480  }
481 }

References __SW(), SysTimer, and SysTimer_CLINT_MSIP_BASE.

Referenced by SysTimer_SetMsipValue().

◆ SysTimer_SetHartSsipValue()

__STATIC_FORCEINLINE void SysTimer_SetHartSsipValue ( uint32_t  ssip,
unsigned long  hartid 
)

Set system timer SSIP register value by hartid.

This function set the system timer SSIP register value.

Parameters
[in]msipvalue to set SSIP register
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Remarks

Definition at line 664 of file core_feature_timer.h.

665 {
666  if (hartid == 0) {
667  SysTimer->SSIP = (ssip);
668  } else {
669  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_SSIP_BASE(hartid));
670  __SW(addr, ssip);
671  }
672 }

References __SW(), SysTimer, and SysTimer_CLINT_SSIP_BASE.

Referenced by SysTimer_SetSsipValue().

◆ SysTimer_SetHartSWIRQ()

__STATIC_FORCEINLINE void SysTimer_SetHartSWIRQ ( unsigned long  hartid)

Trigger or set software interrupt via system timer by hartid.

This function set the system timer MSIP bit in MSIP register.

Parameters
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Remarks
  • Set system timer MSIP bit and generate a machine mode software interrupt.
  • In S-mode, hartid can't be get by using __get_hart_id function, so this api suite S-mode particularly when MTIME_SRW_CTRL.SRW equal 0.
  • SysTimer_ClearHartSWIRQ
  • SysTimer_GetHartMsipValue

Definition at line 360 of file core_feature_timer.h.

361 {
362  if (hartid == 0) {
364  } else {
365  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_MSIP_BASE(hartid));
367  }
368 }

References __SW(), SysTimer, SysTimer_CLINT_MSIP_BASE, and SysTimer_MSIP_MSIP_Msk.

Referenced by SysTimer_SetSWIRQ().

◆ SysTimer_SetHartSWIRQ_S()

__STATIC_FORCEINLINE void SysTimer_SetHartSWIRQ_S ( unsigned long  hartid)

Trigger or set supervisor mode software interrupt via system timer by hartid.

This function set the system timer SSIP bit in SSIP register.

Parameters
[in]hartidhart ID, one hart is required to have a known hart ID of 0, other harts ID can be in 1~1023.
Remarks

Definition at line 554 of file core_feature_timer.h.

555 {
556  if (hartid == 0) {
558  } else {
559  uint8_t *addr = (uint8_t *)(SysTimer_CLINT_SSIP_BASE(hartid));
561  }
562 }

References __SW(), SysTimer, SysTimer_CLINT_SSIP_BASE, and SysTimer_SSIP_SSIP_Msk.

Referenced by SysTimer_SetSWIRQ_S().

◆ SysTimer_SetLoadValue()

__STATIC_FORCEINLINE void SysTimer_SetLoadValue ( uint64_t  value)

Set system timer load value.

This function set the system timer load value in MTIMER register.

Parameters
[in]valuevalue to set system timer MTIMER register.
Remarks

Definition at line 149 of file core_feature_timer.h.

150 {
151 #if __RISCV_XLEN == 32
152  uint8_t *addr;
153  addr = (uint8_t *)(&(SysTimer->MTIMER));
154  __SW(addr, 0); // prevent carry
155  __SW(addr + 4, (uint32_t)(value >> 32));
156  __SW(addr, (uint32_t)(value));
157 #else
158  SysTimer->MTIMER = value;
159 #endif
160 }

References __SW(), and SysTimer.

◆ SysTimer_SetMsipValue()

__STATIC_FORCEINLINE void SysTimer_SetMsipValue ( uint32_t  msip)

Set system timer MSIP register value in machine mode.

This function set the system timer MSIP register value.

Parameters
[in]msipvalue to set MSIP register

Definition at line 490 of file core_feature_timer.h.

491 {
492  unsigned long hartid = SysTimer_GetHartID();
493  SysTimer_SetHartMsipValue(msip, hartid);
494 }
__STATIC_FORCEINLINE void SysTimer_SetHartMsipValue(uint32_t msip, unsigned long hartid)
Set system timer MSIP register value by hartid.

References SysTimer_GetHartID, and SysTimer_SetHartMsipValue().

◆ SysTimer_SetSsipValue()

__STATIC_FORCEINLINE void SysTimer_SetSsipValue ( uint32_t  ssip)

Set system timer SSIP register value.

This function set the system timer SSIP register value.

Parameters
[in]msipvalue to set SSIP register

Definition at line 681 of file core_feature_timer.h.

682 {
683  unsigned long hartid = SysTimer_GetHartID();
684  SysTimer_SetHartSsipValue(ssip, hartid);
685 }
__STATIC_FORCEINLINE void SysTimer_SetHartSsipValue(uint32_t ssip, unsigned long hartid)
Set system timer SSIP register value by hartid.

References SysTimer_GetHartID, and SysTimer_SetHartSsipValue().

◆ SysTimer_SetSWIRQ()

__STATIC_FORCEINLINE void SysTimer_SetSWIRQ ( void  )

Trigger or set software interrupt via system timer in machine mode.

This function set the system timer MSIP bit in MSIP register.

Remarks

Definition at line 379 of file core_feature_timer.h.

380 {
381  unsigned long hartid = SysTimer_GetHartID();
382  SysTimer_SetHartSWIRQ(hartid);
383 }
__STATIC_FORCEINLINE void SysTimer_SetHartSWIRQ(unsigned long hartid)
Trigger or set software interrupt via system timer by hartid.

References SysTimer_GetHartID, and SysTimer_SetHartSWIRQ().

◆ SysTimer_SetSWIRQ_S()

__STATIC_FORCEINLINE void SysTimer_SetSWIRQ_S ( void  )

Trigger or set supervisor software interrupt via system timer.

This function set the system timer SSIP bit in SSIP register.

Remarks

Definition at line 573 of file core_feature_timer.h.

574 {
575  unsigned long hartid = SysTimer_GetHartID_S();
576  SysTimer_SetHartSWIRQ_S(hartid);
577 }
__STATIC_FORCEINLINE void SysTimer_SetHartSWIRQ_S(unsigned long hartid)
Trigger or set supervisor mode software interrupt via system timer by hartid.

References SysTimer_GetHartID_S, and SysTimer_SetHartSWIRQ_S().

◆ SysTimer_SoftwareReset()

__STATIC_FORCEINLINE void SysTimer_SoftwareReset ( void  )

Do software reset request.

This function will do software reset request through MTIMER

  • Software need to write SysTimer_MSFRST_KEY to generate software reset request
  • The software request flag can be cleared by reset operation to clear
    Remarks
  • The software reset is sent to SoC, SoC need to generate reset signal and send back to Core
  • This function will not return, it will do while(1) to wait the Core reset happened

Definition at line 755 of file core_feature_timer.h.

756 {
757  SysTimer->MSFTRST = SysTimer_MSFRST_KEY;
758  // will reset cpu, never return
759  while (1);
760 }
#define SysTimer_MSFRST_KEY
SysTick Timer Software Reset Request Key.

References SysTimer, and SysTimer_MSFRST_KEY.

◆ SysTimer_Start()

__STATIC_FORCEINLINE void SysTimer_Start ( void  )

Enable system timer counter running.

Enable system timer counter running by clear TIMESTOP bit in MTIMECTL register.

Definition at line 301 of file core_feature_timer.h.

302 {
304 }
#define SysTimer_MTIMECTL_TIMESTOP_Msk
SysTick Timer MTIMECTL: TIMESTOP Mask.

References SysTimer, and SysTimer_MTIMECTL_TIMESTOP_Msk.

◆ SysTimer_Stop()

__STATIC_FORCEINLINE void SysTimer_Stop ( void  )

Stop system timer counter running.

Stop system timer counter running by set TIMESTOP bit in MTIMECTL register.

Definition at line 312 of file core_feature_timer.h.

313 {
315 }

References SysTimer, and SysTimer_MTIMECTL_TIMESTOP_Msk.