Implement systick module

This commit is contained in:
Steins7 2024-04-28 22:04:10 +02:00
parent 7ba9063d02
commit ddd05da6eb
2 changed files with 115 additions and 0 deletions

84
drv/stk.c Normal file
View File

@ -0,0 +1,84 @@
/** @file stk.c
* Module handling the system timer (systick or STK for short)
*
* The module provides functions to configure and use the system timer embedded
* in the cortex m core
*/
//--includes--------------------------------------------------------------------
#include "stk.h"
#include "stk_regs.h"
#include "rcc.h"
//--local definitions-----------------------------------------------------------
static volatile struct STK* regs = (struct STK*)STK_BASE_ADDRESS;
//--local variables-------------------------------------------------------------
static StkCallback callback;
//--public functions------------------------------------------------------------
uint32_t stk_configure(uint32_t period_us, StkCallback cb)
{
stk_reset();
struct RccClocks clocks;
rcc_get_clocks(&clocks);
uint32_t reload = period_us * (clocks.ahb_freq / 1000000 / 8);
if (reload < 1) {
//period is too small, try using the non-prescaled clock
reload = period_us * (clocks.ahb_freq / 1000000);
if (reload < 1) {
//period is still too small
return 1;
}
reg_set(regs->CTRL, STK_CTRL_CLKSOURCE);
}
reg_write(regs->LOAD, STK_LOAD_RELOAD, reload);
reg_set(regs->CTRL, STK_CTRL_TICKINT);
callback = cb;
return 0;
}
void stk_reset(void)
{
reg_reset(regs->CTRL, STK_CTRL_ENABLE);
reg_reset(regs->CTRL, STK_CTRL_TICKINT);
reg_reset(regs->CTRL, STK_CTRL_CLKSOURCE);
reg_reset(regs->CTRL, STK_CTRL_COUNTFLAG);
reg_write(regs->LOAD, STK_LOAD_RELOAD, 0);
reg_write(regs->VAL, STK_VAL_CURRENT, 0);
}
void stk_start(void)
{
reg_set(regs->CTRL, STK_CTRL_ENABLE);
}
void stk_stop(void)
{
reg_reset(regs->CTRL, STK_CTRL_ENABLE);
}
uint32_t stk_read(void)
{
return regs->VAL.word & 0x00FFFFFF;
}
//--local functions-------------------------------------------------------------
void hdr_sys_tick(void)
{
callback();
}

31
drv/stk.h Normal file
View File

@ -0,0 +1,31 @@
/** @file stk.h
* Module handling the system timer (systick or STK for short)
*
* The module provides functions to configure and use the system timer embedded
* in the cortex m core
*/
#ifndef _STK_H_
#define _STK_H_
//--includes--------------------------------------------------------------------
#include <stdint.h>
//--type definitions------------------------------------------------------------
typedef void (*StkCallback)(void);
//--functions-------------------------------------------------------------------
uint32_t stk_configure(uint32_t period_us, StkCallback cb);
void stk_reset(void);
void stk_start(void);
void stk_stop(void);
uint32_t stk_read(void);
#endif //_STK_H_