collab/PineapplePad

git clone https://git.julia.blue/collab/PineapplePad

Joseph RyanMerge branch 'master' of github.com:p1n3appl3/PineapplePad7d01d63

master
2.5 KiB56 linesraw
1// Timer1.c
2// Runs on LM4F120/TM4C123
3// Use TIMER1 in 32-bit periodic mode to request interrupts at a periodic rate
4// Daniel Valvano
5// Last Modified: 3/6/2015
6// You can use this timer only if you learn how it works
7
8/* This example accompanies the book
9   "Embedded Systems: Real Time Interfacing to Arm Cortex M Microcontrollers",
10   ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2013
11  Program 7.5, example 7.6
12
13 Copyright 2013 by Jonathan W. Valvano, valvano@mail.utexas.edu
14    You may use, edit, run or distribute this file
15    as long as the above copyright notice remains
16 THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
17 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
19 VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
20 OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
21 For more information about my classes, my research, and my books, see
22 http://users.ece.utexas.edu/~valvano/
23 */
24#include <stdint.h>
25#include "../inc/Timer1.h"
26#include "../inc/tm4c123gh6pm.h"
27
28void (*PeriodicTask1)(void);   // user function
29extern "C" void TIMER1A_Handler(void);
30
31// ***************** TIMER1_Init ****************
32// Activate TIMER1 interrupts to run user task periodically
33// Inputs:  task is a pointer to a user function
34//          period in units (1/clockfreq)
35// Outputs: none
36void Timer1_Init(void(*task)(void), uint32_t period){
37  SYSCTL_RCGCTIMER_R |= 0x02;   // 0) activate TIMER1
38  PeriodicTask1 = task;          // user function
39  TIMER1_CTL_R = 0x00000000;    // 1) disable TIMER1A during setup
40  TIMER1_CFG_R = 0x00000000;    // 2) configure for 32-bit mode
41  TIMER1_TAMR_R = 0x00000002;   // 3) configure for periodic mode, default down-count settings
42  TIMER1_TAILR_R = period-1;    // 4) reload value
43  TIMER1_TAPR_R = 0;            // 5) bus clock resolution
44  TIMER1_ICR_R = 0x00000001;    // 6) clear TIMER1A timeout flag
45  TIMER1_IMR_R = 0x00000001;    // 7) arm timeout interrupt
46  NVIC_PRI5_R = (NVIC_PRI5_R&NVIC_PRI5_INT21_M)| NVIC_PRI5_INT21_M; // 8) priority 7
47// interrupts enabled in the main program after all devices initialized
48// vector number 37, interrupt number 21
49  NVIC_EN0_R = 1<<21;           // 9) enable IRQ 21 in NVIC
50  TIMER1_CTL_R = 0x00000001;    // 10) enable TIMER1A
51}
52extern "C" void TIMER1A_Handler(void);
53void TIMER1A_Handler(void){
54  TIMER1_ICR_R = TIMER_ICR_TATOCINT;// acknowledge TIMER1A timeout
55  (*PeriodicTask1)();                // execute user task
56}