diff --git a/hal/m258ke/board.c b/hal/m258ke/board.c index 1cb096d..6c27f2a 100644 --- a/hal/m258ke/board.c +++ b/hal/m258ke/board.c @@ -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 diff --git a/hal/m258ke/hardware.h b/hal/m258ke/hardware.h index e6e8d72..ca2c676 100644 --- a/hal/m258ke/hardware.h +++ b/hal/m258ke/hardware.h @@ -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_ */