Skip to content

Commit

Permalink
Enable timer, use for delay functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dougg3 committed Sep 10, 2023
1 parent 6a9b75d commit 9df4cd0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
9 changes: 9 additions & 0 deletions hal/m258ke/board.c
Expand Up @@ -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 |
Expand All @@ -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
Expand Down
21 changes: 13 additions & 8 deletions hal/m258ke/hardware.h
Expand Up @@ -44,22 +44,27 @@ static inline void EnableInterrupts(void)
__enable_irq();
}

/** Blocks for the specified number of milliseconds
/** Blocks for the specified number of microseconds
*
* @param ms The number of milliseconds to wait
* @param us The number of microseconds to wait
*/
static inline void DelayMS(uint32_t ms)
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 microseconds
/** Blocks for the specified number of milliseconds
*
* @param us The number of microseconds to wait
* @param ms The number of milliseconds to wait
*/
static inline void DelayUS(uint32_t us)
static inline void DelayMS(uint32_t ms)
{

DelayUS(ms * 1000UL);
}

#endif /* HAL_M258KE_HARDWARE_H_ */

0 comments on commit 9df4cd0

Please sign in to comment.