Enable timer, use for delay functions

This commit is contained in:
Doug Brown 2023-08-06 20:35:29 -07:00 committed by Doug Brown
parent 6a9b75d01b
commit 9df4cd0c84
2 changed files with 23 additions and 9 deletions

View File

@ -51,6 +51,12 @@ void Board_Init(void)
// Enable USB device controller
CLK->APBCLK0 |= CLK_APBCLK0_USBDCKEN_Msk;
// Enable timer 0
CLK->APBCLK0 |= CLK_APBCLK0_TMR0CKEN_Msk;
// Timer 0 clock source = 48 MHz HIRC
CLK->CLKSEL1 = (CLK->CLKSEL1 & (~(CLK_CLKSEL1_TMR0SEL_Msk))) | (7UL << CLK_CLKSEL1_TMR0SEL_Pos);
// Enable all GPIO
CLK->AHBCLK |=
CLK_AHBCLK_GPACKEN_Msk |
@ -59,6 +65,9 @@ void Board_Init(void)
CLK_AHBCLK_GPDCKEN_Msk |
CLK_AHBCLK_GPECKEN_Msk |
CLK_AHBCLK_GPFCKEN_Msk;
// Start the timer, prescaler = 48, so 1 MHz
TIMER0->CTL = TIMER_CTL_CNTEN_Msk | (3UL << TIMER_CTL_OPMODE_Pos) | 47;
}
/** Determines if a brownout was detected at startup

View File

@ -44,22 +44,27 @@ static inline void EnableInterrupts(void)
__enable_irq();
}
/** Blocks for the specified number of milliseconds
*
* @param ms The number of milliseconds to wait
*/
static inline void DelayMS(uint32_t ms)
{
}
/** Blocks for the specified number of microseconds
*
* @param us The number of microseconds to wait
*/
static inline void DelayUS(uint32_t us)
{
const uint32_t startTime = TIMER0->CNT & 0xFFFFFFUL;
uint32_t nowTime;
do
{
nowTime = TIMER0->CNT & 0xFFFFFFUL;
} while (((nowTime - startTime) & 0xFFFFFFUL) < us);
}
/** Blocks for the specified number of milliseconds
*
* @param ms The number of milliseconds to wait
*/
static inline void DelayMS(uint32_t ms)
{
DelayUS(ms * 1000UL);
}
#endif /* HAL_M258KE_HARDWARE_H_ */