Use TIMER4 in 32-bit mode to replace SysTick. master
authorMatt Mullins <mmullins@mmlx.us>
Mon, 24 Dec 2012 03:17:52 +0000 (19:17 -0800)
committerMatt Mullins <mmullins@mmlx.us>
Mon, 24 Dec 2012 03:17:52 +0000 (19:17 -0800)
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
led_blink.c
led_blink.h

index 33306c8..818b7bf 100644 (file)
@@ -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,
index ddf5325..3e0fe7a 100644 (file)
@@ -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;
 }
index ed70066..df7bb46 100644 (file)
@@ -2,6 +2,6 @@
 #define __led_blink_H__
 
 extern void main_isr();
-extern void systick_isr();
+extern void timer4a_isr();
 
 #endif