+/EE445L

embedded systems

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

Joseph RyanAdd other starter filesc12dcba

master
4.2 KiB121 linesraw
1// LED.c
2// Runs on TM4C123
3// Provide functions that initialize a GPIO as an input pin and
4// allow reading of two negative logic switches on PF0 and PF4
5// Output to LEDs
6// Use bit-banded I/O.
7// Daniel and Jonathan Valvano
8// Feb 23, 2015
9#include "../inc/tm4c123gh6pm.h"
10#include <stdint.h>
11
12/* This example accompanies the book
13   "Embedded Systems: Introduction to ARM Cortex M Microcontrollers",
14   ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2014
15   Section 4.2    Program 4.2
16
17  "Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers",
18   ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2014
19   Example 2.3, Program 2.9, Figure 2.36
20
21 Copyright 2015 by Jonathan W. Valvano, valvano@mail.utexas.edu
22    You may use, edit, run or distribute this file
23    as long as the above copyright notice remains
24 THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
25 OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
26 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
27 VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
28 OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
29 For more information about my classes, my research, and my books, see
30 http://users.ece.utexas.edu/~valvano/
31 */
32
33#define GPIO_LOCK_KEY 0x4C4F434B // Unlocks the GPIO_CR register
34#define PF0 (*((volatile uint32_t*)0x40025004))
35#define PF4 (*((volatile uint32_t*)0x40025040))
36#define SWITCHES (*((volatile uint32_t*)0x40025044))
37#define SW1 0x10 // on the left side of the Launchpad board
38#define SW2 0x01 // on the right side of the Launchpad board
39#define PF1 (*((volatile uint32_t*)0x40025008))
40#define PF2 (*((volatile uint32_t*)0x40025010))
41#define PF3 (*((volatile uint32_t*)0x40025020))
42
43//------------LED_Init------------
44// Initialize GPIO Port F for negative logic switches on PF0 and
45// PF4 as the Launchpad is wired.  Weak internal pull-up
46// resistors are enabled, and the NMI functionality on PF0 is disabled.
47// LEDs on PF3,2,1 are enabled
48// Input: none
49// Output: none
50void LED_Init(void) {
51    SYSCTL_RCGCGPIO_R |= 0x20;                   // 1) activate Port F
52    while ((SYSCTL_PRGPIO_R & 0x20) != 0x20) {}; // wait to finish activating
53    GPIO_PORTF_LOCK_R = GPIO_LOCK_KEY; // 2a) unlock GPIO Port F Commit Register
54    GPIO_PORTF_CR_R = 0x1F;            // 2b) enable commit for PF4-PF0
55    GPIO_PORTF_AMSEL_R &= ~0x1F; // 3) disable analog functionality on PF4-PF0
56    GPIO_PORTF_PCTL_R = 0x00000000; // 4) configure PF0-PF4 as GPIO
57    GPIO_PORTF_DIR_R = 0x0E;        // 5) make PF0 and PF4 in PF3-1 output
58    GPIO_PORTF_AFSEL_R = 0;         // 6) disable alt funct on PF0 and PF4
59    GPIO_PORTF_PUR_R = 0x11;        // enable weak pull-up on PF0 and PF4
60    GPIO_PORTF_DEN_R = 0x1F;        // 7) enable digital I/O on PF0-PF4
61}
62
63//------------Board_Input------------
64// Read and return the status of the switches.
65// Input: none
66// Output: 2 if only Switch 1 is pressed
67//         1 if only Switch 2 is pressed
68//         3 if both switches are pressed
69//         0 if no switches are pressed
70uint32_t Board_Input(void) {
71    return (~((PF4 >> 3) + PF0)) & 0x03;
72}
73//------------LED_RedOn------------
74// Turn on red LED
75// Input: none
76// Output: none
77void LED_RedOn(void) {
78    PF1 = 0x02;
79}
80//------------LED_RedOff------------
81// Turn off red LED
82// Input: none
83// Output: none
84void LED_RedOff(void) {
85    PF1 = 0x00;
86}
87//------------LED_RedToggle------------
88// Toggle redv LED
89// Input: none
90// Output: none
91void LED_RedToggle(void) {
92    PF1 ^= 0x02;
93}
94//------------LED_BlueToggle------------
95// Toggle blue LED
96// Input: none
97// Output: none
98void LED_BlueToggle(void) {
99    PF2 ^= 0x04;
100}
101//------------LED_GreenToggle------------
102// Toggle green LED
103// Input: none
104// Output: none
105void LED_GreenToggle(void) {
106    PF3 ^= 0x08;
107}
108//------------LED_GreenOn------------
109// Turn on green LED
110// Input: none
111// Output: none
112void LED_GreenOn(void) {
113    PF3 = 0x08;
114}
115//------------LED_GreenOff------------
116// Turn off green LED
117// Input: none
118// Output: none
119void LED_GreenOff(void) {
120    PF3 = 0x00;
121}