RISCV_TIMER USER GUIDE
REVISION HISTORY¶
| Revision No. | Description |
Date |
|---|---|---|
| 1.0 | 04/21/2023 |
1. OVERVIEW¶
Timer is a hardware module used in computer and microcontroller systems to measure time intervals or generate precise timing events. The basic working principle of a timer is to drive a counter through a clock source. When the counter reaches a preset value, an event is generated, usually through an interrupt to notify the processor.
2. KEYWORD DESCRIPTION¶
-
PM-timer
Timer powered by PM domain.
-
NonPMtimer
Timer powered by NonPM domain.
-
OneShoot mode
A working mode of Timer that triggers an interrupt when the timer reaches a certain time.
-
RunLoop mode
A working mode of Timer, which will reset the counting core and trigger a loop after the timer is triggered.
3. FUNCTION DESCRIPTION¶
Whether it is PM timer or NonPM timer, the use of timers has the same steps. From a development perspective, there are five steps involved
- Register Timer
- Start the timer
- Interrupt functions for executing timers and processing functions for calling timers in the system
- Stop timer
- Unregister timer
In addition, when using a timer, the following factors need to be considered::
-
Quantity of hardware
-
8 PM-timer
-
4 NonPM-timer
-
-
CLK
-
PM-timer0 ~ PM-timer3 and NonPM-timers use the same clock, with a default frequency of 12MHz, PM timer4~PM timer7 each use different clocks.
-
PM-timer0 ~ PM-timer3 each use different hardware interrupt numbers, while PM-timer4 ~ PM-timer7 use the same hardware interrupt number.
-
-
Limitation
- Software limitation: Riscv side and arm side cannot use the same timer at the same time, which may cause interrupt confusion.
-
working mode
- Timer has two working modes, one is oneshook mode and the other is RunLoop mode.
4. HARDWARE CONNECTION INTRODUCTION¶
NA
5. Usage Introduction In RTOS¶
5.1. DRIVER PATH¶
sc/driver/sysdriver/timer/drv/pub/drv_pm_timer.h sc/driver/sysdriver/timer/drv/pub/drv_timer_api.h sc/driver/sysdriver/timer/drv/pub/drv_timer.h sc/driver/sysdriver/timer/drv/src/abi_headers_timer.c sc/driver/sysdriver/timer/drv/src/drv_pm_timer.c sc/driver/sysdriver/timer/drv/src/drv_timer.c sc/driver/sysdriver/timer/drv/src/drv_timer_test.c
5.2. CONFIG Configuration¶
No configuration is required.
5.3. SYSDESC Configuration¶
No configuration is required.
5.4. Padmux Configuration¶
No configuration is required.
5.5. Sample code¶
static void timer_isr(void)
{
CamOsPrintf("I am timer%d!\n", timer.timer_id);
}
int timer_teset(void)
{
drv_timer_id timer_id = 1;
u32 time = 20000;
drv_timer_register_isr(timer_id, timer_isr);
drv_timer_start(timer_id, time);
CamOsMsSleep(20000);
drv_timer_stop(timer_id);
drv_timer_release_isr(timer_id)
}
5.6. UT case¶
1) Open timer to test the demo macro VER_TIMER
2) Test demo path: sc/driver/sysdriver/timer/drv/src/drv_timer_test.c
3) Perform testing
SS-RTOS # timer
Plz key in : timer [timerID] [time]
[timerID] 1 - timer1
2 - timer2
3 - timer3
4 - timer4
5 - timer5
6 - timer4
7 - timer5
[time] timing time(ms)
timer [timerId] [time]
Example:
# timer 1 1000 /*Enable Timer 1, report an interrupt every 1000 ms, and report it 10 times.*/
6. API参考¶
This functional module provides the following interfaces:
| API Name | Function |
|---|---|
| drv_timer_register_isr | Register timer interrupt function |
| drv_timer_release_isr | Release timer interrupt function |
| drv_timer_disable_intr | Disable timer interrupt function |
| drv_timer_enable_intr | Enable timer interrupt function |
| drv_timer_clear_intr | Clear timer interrupt |
| drv_timer_get_intr_status | Get timer interrupt status |
| drv_timer_stop | Stop timer |
| drv_timer_start | Start timer, unit ms |
| drv_timer_start_us | Start timer, unit us |
| drv_timer_get_timeout_length | Get the count value corresponding to the timer expiration time |
| drv_timer_get_count | Get the value of the timer from the start of the timer to the current timer counter |
| drv_timer_get_kickoff_time_us | Get how much time has passed since the timer started, unit us |
6.1. drv_timer_register_isr¶
-
Function
Register timer interrupt function
-
Grammar
static inline void drv_timer_register_isr(drv_timer_id timer_id, timer_isr_t isr)
-
Parameters
Parameter Name Description timer_id timer ID isr Interrupt function -
Return value
Return value Description NA NA
6.2. drv_timer_release_isr¶
-
Function
Release timer interrupt function
-
Grammar
static inline void drv_timer_release_isr(drv_timer_id timer_id)
-
Parameters
Parameter Name Description timer_id timer ID -
Return value
Return value Description NA NA
6.3. drv_timer_disable_intr¶
-
Function
Disable timer interrupt function
-
Grammar
static inline void drv_timer_disable_intr(drv_timer_id timer_id)
-
Parameters
Parameter Name Description timer_id timer ID -
Return value
Return value Description NA NA
6.4. drv_timer_enable_intr¶
-
Function
Enable timer interrupt function.
-
Grammar
static inline void drv_timer_enable_intr(drv_timer_id timer_id)
-
Parameters
Parameter Name Description timer_id timer ID -
Return value
Return value Description NA NA
6.5. drv_timer_clear_intr¶
-
Function
Clear timer interrupt
-
Grammar
static inline void drv_timer_clear_intr(drv_timer_id timer_id)
-
Parameters
Parameter Name Description timer_id timer ID -
Return value
Return value Description NA NA
6.6. drv_timer_get_intr_status¶
-
Function
Get timer interrupt status
-
Grammar
static inline u32 drv_timer_get_intr_status(drv_timer_id timer_id)
-
Parameters
Parameter Name Description timer_id timer ID -
Return value
Return value Description NA NA
6.7. drv_timer_stop¶
-
Function
Stop timer
-
Grammar
static inline void drv_timer_stop(drv_timer_id timer_id)
-
Parameters
Parameter Name Description timer_id timer ID -
Return value
Return value Description NA NA
6.8. drv_timer_start¶
-
Function
Start timer. (The minimum unit of the timer's expiration time is 1 ms.)
-
Grammar
static inline void drv_timer_start(drv_timer_id timer_id, u32 expect_time)
-
Parameters
Parameter Name Description timer_id timer ID expect_time Timer expiration time(ms) -
Return value
Return value Description NA NA
6.9. drv_timer_start_us¶
-
Function
Start timer. (The minimum unit of the timer's expiration time is 1 us.)
-
Grammar
static inline void drv_timer_start_us(drv_timer_id timer_id, u32 expect_time)
-
Parameters
Parameter Name Description timer_id timer ID expect_time Timer expiration time(us) -
Return value
Return value Description NA NA
6.10. drv_timer_get_timeout_length¶
-
Function
Get the count value corresponding to the timer expiration time
-
Grammar
static inline u32 drv_timer_get_timeout_length(drv_timer_id timer_id)
-
Parameters
Parameter Name Description timer_id timer ID -
Return value
Return value Description NA NA
6.11. drv_timer_get_count¶
-
Function
Get the value of the timer from the start of the timer to the current timer counter
-
Grammar
static inline u32 drv_timer_get_count(drv_timer_id timer_id)
-
Parameters
Parameter Name Description timer_id timer ID -
Return value
Return value Description u32 Get the value on the timer counter from the start of the timer until now
6.12. drv_timer_get_kickoff_time_us¶
-
Function
How much time has passed since the timer was started.
-
Grammar
static inline u32 drv_timer_get_kickoff_time_us(drv_timer_id timer_id)
-
Parameters
Parameter Name Description timer_id timer ID -
Return value
Return value Description u32 How much time has passed since the timer was started, unit is us