From 05c5e405ac10dfa55136c8d0c965811459397618 Mon Sep 17 00:00:00 2001 From: CNLohr <lohr85@gmail.com> Date: Fri, 12 May 2023 03:14:48 -0400 Subject: [PATCH] Update systick examples to reflect new sysclk usage. --- examples/systick_irq/Makefile | 2 -- examples/systick_irq/systick_irq.c | 46 ++++++++++++------------------ 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/examples/systick_irq/Makefile b/examples/systick_irq/Makefile index 42b59e6..afac4cd 100644 --- a/examples/systick_irq/Makefile +++ b/examples/systick_irq/Makefile @@ -2,8 +2,6 @@ all : flash TARGET:=systick_irq -CFLAGS+=-DSTDOUT_UART - include ../../ch32v003fun/ch32v003fun.mk flash : cv_flash diff --git a/examples/systick_irq/systick_irq.c b/examples/systick_irq/systick_irq.c index 6091b1e..352f4d5 100644 --- a/examples/systick_irq/systick_irq.c +++ b/examples/systick_irq/systick_irq.c @@ -1,10 +1,12 @@ /* * Example for using SysTick with IRQs * 03-25-2023 E. Brombaugh + * 05-12-2023 C. Lohr (Modified to reflect updated sysclk) */ // Could be defined here, or in the processor defines. #define SYSTEM_CORE_CLOCK 48000000 +#define SYSTICK_USE_HCLK #define APB_CLOCK SYSTEM_CORE_CLOCK #include "ch32v003fun.h" @@ -31,9 +33,6 @@ void systick_init(void) /* enable the SysTick IRQ */ NVIC_EnableIRQ(SysTicK_IRQn); - /* Clear any existing IRQ */ - SysTick->SR &= ~SYSTICK_SR_CNTIF; - /* Set the tick interval to 1ms for normal op */ SysTick->CMP = (SYSTEM_CORE_CLOCK/1000)-1; @@ -41,9 +40,9 @@ void systick_init(void) SysTick->CNT = 0; systick_cnt = 0; - /* Enable SysTick counter, IRQ, HCLK/1, auto reload */ - SysTick->CTLR = SYSTICK_CTLR_STE | SYSTICK_CTLR_STIE | - SYSTICK_CTLR_STCLK | SYSTICK_CTLR_STRE; + /* Enable SysTick counter, IRQ, HCLK/1 */ + SysTick->CTLR = SYSTICK_CTLR_STE | SYSTICK_CTLR_STIE | + SYSTICK_CTLR_STCLK; } /* @@ -53,25 +52,19 @@ void systick_init(void) void SysTick_Handler(void) __attribute__((interrupt)); void SysTick_Handler(void) { + // move the compare further ahead in time. + // as a warning, if more than this length of time + // passes before triggering, you may miss your + // interrupt. + SysTick->CMP += (SYSTEM_CORE_CLOCK/1000); + /* clear IRQ */ - SysTick->SR &= 0; - + SysTick->SR = 0; + /* update counter */ systick_cnt++; } -/* - * Millisecond delay routine - */ -void systick_delay_ms(uint32_t milliseconds) -{ - /* compute end time */ - uint32_t etime = systick_cnt + milliseconds; - - /* wait for current time == end time */ - while(systick_cnt != etime); -} - /* * entry */ @@ -81,8 +74,7 @@ int main() SystemInit48HSI(); - // start serial @ default 115200bps - SetupUART( UART_BRR ); + SetupDebugPrintf(); printf("\r\r\n\nsystick_irq example\n\r"); // init systick @ 1ms rate @@ -109,13 +101,13 @@ int main() while(1) { GPIOD->BSHR = 1 | (1<<4); // Turn on GPIOs - systick_delay_ms( 250 ); + Delay_Ms( 250 ); GPIOC->BSHR = 1; - systick_delay_ms( 250 ); + Delay_Ms( 250 ); GPIOD->BSHR = (1<<16) | (1<<(16+4)); // Turn off GPIODs - systick_delay_ms( 250 ); + Delay_Ms( 250 ); GPIOC->BSHR = (1<<16); - systick_delay_ms( 250 ); - printf( "Count: %lu\n\r", count++ ); + Delay_Ms( 250 ); + printf( "Count: %lu / Milliseconds: %lu\n\r", count++, systick_cnt ); } } -- GitLab