From c2202006bfa80441eecef3ff0ed2c9d39e2556e7 Mon Sep 17 00:00:00 2001 From: Matt Mullins Date: Sun, 23 Dec 2012 19:17:52 -0800 Subject: [PATCH] Use TIMER4 in 32-bit mode to replace SysTick. This cleans up a lot of code, since we no longer need to keep a software counter. 32-bits is plenty of counting to turn 80MHz into 2Hz. --- isr_table.c | 2 +- led_blink.c | 35 +++++++++++++++-------------------- led_blink.h | 2 +- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/isr_table.c b/isr_table.c index 33306c8..818b7bf 100644 --- a/isr_table.c +++ b/isr_table.c @@ -19,7 +19,6 @@ void (*reset_vectors[])() = { infinite_loop_isr, infinite_loop_isr, infinite_loop_isr, - systick_isr, infinite_loop_isr, infinite_loop_isr, infinite_loop_isr, @@ -91,6 +90,7 @@ void (*reset_vectors[])() = { infinite_loop_isr, infinite_loop_isr, infinite_loop_isr, + timer4a_isr, infinite_loop_isr, infinite_loop_isr, infinite_loop_isr, diff --git a/led_blink.c b/led_blink.c index ddf5325..3e0fe7a 100644 --- a/led_blink.c +++ b/led_blink.c @@ -3,12 +3,6 @@ static void spin(int); -// Since SysTick is only 24-bits, we'll need to divide it by 4 to produce a -// 2Hz value from the 80MHz system clock. -// Yes, I know I can just run the system clock slower, but this is more fun. -static const int initial_systick_count = 3; -static int systick_count; - __attribute__((isr)) void main_isr() { // Power on the PLL, set to 16MHz XTAL @@ -52,13 +46,16 @@ void main_isr() { // Set RGB pins to 1 *(GPIO_PORTF_DATA_BITS_R + 0xE) = 0xF; - // Initialize our SysTick prescaler - systick_count = initial_systick_count; + // Initialize the Timer 4 module + SYSCTL_RCGCTIMER_R |= (1 << 4); + TIMER4_CFG_R = 0x0; // 32-bit timer on shared 16/32-bit GPTM + TIMER4_TAMR_R = TIMER_TAMR_TAMR_PERIOD; + TIMER4_TAILR_R = 40000000; // produce 2Hz from 80MHz clock - // Initialize the SysTick module - NVIC_ST_RELOAD_R = 10000000; // produce 8Hz from 80MHz clock - NVIC_ST_CURRENT_R = 0; - NVIC_ST_CTRL_R |= 0x7; + // Enable interrupts for Timer 4A + TIMER4_IMR_R |= TIMER_IMR_TATOIM; + TIMER4_CTL_R |= TIMER_CTL_TAEN; + NVIC_EN2_R = (1 << (70 & 0x1F)); // infinite loop for (;;) { @@ -79,13 +76,11 @@ void spin(int cycles) { } } -__attribute__((isr)) -void systick_isr() { - if (systick_count) { - systick_count--; - return; - } - - systick_count = initial_systick_count; +void __attribute__((isr)) +timer4a_isr() { + // Blink the LED [invert status twice per second] *(GPIO_PORTF_DATA_BITS_R + 0xE) ^= 0xF; + + // Clear interrupt status so this ISR doesn't infinite-loop + TIMER4_ICR_R = TIMER_ICR_TATOCINT; } diff --git a/led_blink.h b/led_blink.h index ed70066..df7bb46 100644 --- a/led_blink.h +++ b/led_blink.h @@ -2,6 +2,6 @@ #define __led_blink_H__ extern void main_isr(); -extern void systick_isr(); +extern void timer4a_isr(); #endif -- 2.11.0