+/EE445L

embedded systems

git clone https://git.julia.blue/+/EE445L

jspspikeAdded lab4 files99480b6

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