task #5
11
drv/stk.c
11
drv/stk.c
@ -21,6 +21,7 @@ static volatile struct STK* regs = (struct STK*)STK_BASE_ADDRESS;
|
|||||||
//--local variables-------------------------------------------------------------
|
//--local variables-------------------------------------------------------------
|
||||||
|
|
||||||
static StkCallback callback;
|
static StkCallback callback;
|
||||||
|
static uint32_t prescaler;
|
||||||
|
|
||||||
|
|
||||||
//--public functions------------------------------------------------------------
|
//--public functions------------------------------------------------------------
|
||||||
@ -32,10 +33,12 @@ uint32_t stk_configure(uint32_t period_us, StkCallback cb)
|
|||||||
struct RccClocks clocks;
|
struct RccClocks clocks;
|
||||||
rcc_get_clocks(&clocks);
|
rcc_get_clocks(&clocks);
|
||||||
|
|
||||||
uint32_t reload = period_us * (clocks.ahb_freq / 1000000 / 8);
|
uint32_t prescaler = (clocks.ahb_freq / 1000000 / 8);
|
||||||
|
uint32_t reload = period_us * prescaler;
|
||||||
if (reload < 1) {
|
if (reload < 1) {
|
||||||
//period is too small, try using the non-prescaled clock
|
//period is too small, try using the non-prescaled clock
|
||||||
reload = period_us * (clocks.ahb_freq / 1000000);
|
prescaler *= 8;
|
||||||
|
reload = period_us * prescaler;
|
||||||
if (reload < 1) {
|
if (reload < 1) {
|
||||||
//period is still too small
|
//period is still too small
|
||||||
return 1;
|
return 1;
|
||||||
@ -71,9 +74,9 @@ void stk_stop(void)
|
|||||||
reg_reset(regs->CTRL, STK_CTRL_ENABLE);
|
reg_reset(regs->CTRL, STK_CTRL_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t stk_read(void)
|
uint32_t stk_read_us(void)
|
||||||
{
|
{
|
||||||
return regs->VAL.word & 0x00FFFFFF;
|
return (regs->VAL.word & 0x00FFFFFF) / prescaler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -46,9 +46,9 @@ void stk_start(void);
|
|||||||
void stk_stop(void);
|
void stk_stop(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read the current value of the timer's counter
|
* Read the current value of the timer's counter in µs
|
||||||
*/
|
*/
|
||||||
uint32_t stk_read(void);
|
uint32_t stk_read_us(void);
|
||||||
|
|
||||||
|
|
||||||
#endif //_STK_H_
|
#endif //_STK_H_
|
||||||
|
|||||||
@ -73,7 +73,7 @@ void task_start_scheduler(void)
|
|||||||
stk_start();
|
stk_start();
|
||||||
__asm("wfi");
|
__asm("wfi");
|
||||||
stk_stop();
|
stk_stop();
|
||||||
uint32_t stk_delay = stk_read();
|
uint32_t stk_delay = stk_read_us();
|
||||||
if (stk_delay != 0) {
|
if (stk_delay != 0) {
|
||||||
delay = stk_delay;
|
delay = stk_delay;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user