Skip to content
Snippets Groups Projects
Unverified Commit e8305bfc authored by CNLohr's avatar CNLohr Committed by GitHub
Browse files

Merge pull request #92 from cnlohr/proposed_update_to_systick

Update systick examples to reflect new sysclk usage.
parents 321f90cb f20dd4e4
No related branches found
No related tags found
No related merge requests found
......@@ -2,8 +2,6 @@ all : flash
TARGET:=systick_irq
CFLAGS+=-DSTDOUT_UART
include ../../ch32v003fun/ch32v003fun.mk
flash : cv_flash
......
/*
* 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,19 +33,16 @@ 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;
SysTick->CMP = (SYSTEM_CORE_CLOCK/1000)-1;
/* Start at zero */
SysTick->CNT = 0;
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( "Print #: %lu / Milliseconds: %lu / CNT: %lu\n\r", count++, systick_cnt, SysTick->CNT );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment