/
main.c
640 lines (566 loc) · 17.6 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//*****************************************************************************
//
//*****************************************************************************
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "lm4f120h5qr.h"
#include "board_config.h"
#include "team.h"
#include "led_chars.h"
#include "gpio.h"
#include "SPI.h"
#include "UART.h"
#include "uart_cmds.h"
/******************************************************************************
* Defines
*****************************************************************************/
#define PORTA 0x40004000
#define PORTB 0x40005000
#define PORTC 0x40006000
#define PORTD 0x40007000
#define PORTE 0x40024000
#define PORTF 0x40025000
#define OUTPUT_ENABLE_B 0xEF
#define ENABLES_B 0x0F
#define nROW_0 ~PIN_0
#define RED_EN PIN_4
#define GREEN_EN PIN_5
#define BLUE_EN PIN_6
#define ROW_EN PIN_7
#define ENABLES_OFF 0x00
#define OUT_EN_B PIN_4
#define SAVED_MSG_LEN_MAX 20
#define POT_LEFT 1
#define POT_RIGHT 0
#define PHASE 1
#define POLARITY 1
/******************************************************************************
* Global Variables
*****************************************************************************/
GPIO_PORT *GpioPortA = (GPIO_PORT *)PORTA;
GPIO_PORT *GpioPortB = (GPIO_PORT *)PORTB;
GPIO_PORT *GpioPortC = (GPIO_PORT *)PORTC;
GPIO_PORT *GpioPortD = (GPIO_PORT *)PORTD;
GPIO_PORT *GpioPortE = (GPIO_PORT *)PORTE;
GPIO_PORT *GpioPortF = (GPIO_PORT *)PORTF;
volatile bool refreshLED = false;
volatile bool checkADC;
volatile uint32_t pwm = 0;
volatile bool nextLEDrow = false;
volatile bool buttonPoll = false;
//*****************************************************************************
// External Functions
//*****************************************************************************
extern void PLL_Init(void);
extern void initPortC(void);
extern void EnableInterrupts(void);
extern void DisableInterrupts(void);
/******************************************************************************
* Functions
*****************************************************************************/
// *******************************************
// Configure GPIO PA5-PA2 as SPI
// *******************************************
void initializePortASpi0(void)
{
uint32_t delay;
// Turn on the clock gating register for GPIO port A
// Make sure not to turn of any of the other ports
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R0;
// Delay while clock starts up
delay = SYSCTL_RCGCGPIO_R;
// Set the 4 pins used for the SPI interface in the Digital Enable Register
GpioPortA->DigitalEnable |= PIN_5 | PIN_4 | PIN_3 | PIN_2;
// Set the 4 pins used for the SPI interface in the Alternate Function Register
GpioPortA->AlternateFunctionSelect |= PIN_5 | PIN_4 | PIN_3 | PIN_2;
// Set the Port Control Register ( See lm4f120h5qr.h starting at line 2045)
GpioPortA->PortControl |= GPIO_PCTL_PA5_SSI0TX | GPIO_PCTL_PA4_SSI0RX | GPIO_PCTL_PA3_SSI0FSS | GPIO_PCTL_PA2_SSI0CLK;
}
// *******************************************
// Configure SPI
// *******************************************
bool initializeSPI( uint32_t base, uint8_t phase, uint8_t polarity)
{
uint32_t delay;
SPI_PERIPH *myPeriph = (SPI_PERIPH *)base;
// Turn on the Clock Gating Register
switch (base)
{
case SSI0 :
{
// Configure GPIO Port A to support SSI0/SPI0
initializePortASpi0();
SYSCTL_RCGCSSI_R |= SYSCTL_RCGCSSI_R0;
break;
}
default:
return false;
}
delay = SYSCTL_RCGCSSI_R;
// Disable the SSI interface
myPeriph->SSICR1 &= ~SSI_CR1_SSE;
// Enable Master Mode
myPeriph->SSICR1 &= ~SSI_CR1_MS;
// Assume that we hvae a 80MHz clock and want a 4MHz SPI clock
// FSSIClk = FSysClk / (CPSDVSR * (1 + SCR))
myPeriph->SSICPSR = SPI_CLK_CPSDVSR;
myPeriph->SSICR0 &= ~SSI_CR0_SCR_M; // Set SCR to 0
// Cleare the phse and polarity bits
myPeriph->SSICR0 &= ~(SSI_CR0_SPH | SSI_CR0_SPO);
if (phase == 1)
myPeriph->SSICR0 |= SSI_CR0_SPH;
if (polarity ==1)
myPeriph->SSICR0 |= SSI_CR0_SPO;
// Freescale SPI Mode with 8-Bit data
myPeriph->SSICR0 = ( myPeriph->SSICR0 & ~( SSI_CR0_FRF_M | SSI_CR0_DSS_M )) | ( SSI_CR0_FRF_MOTO | SSI_CR0_DSS_8);
//Enable SSI
myPeriph->SSICR1 |= SSI_CR1_SSE;
return true;
}
//*****************************************************************************
// Initialize the GPIO port
//*****************************************************************************
bool initializeGPIOPort(
uint32_t baseAddress,
GPIO_CONFIG * configAddress
)
{
uint32_t delay;
GPIO_PORT *myPort = (GPIO_PORT *)baseAddress;
GPIO_CONFIG *myConfig = (GPIO_CONFIG *)configAddress;
// Validate that a correct base address has been passed
// Turn on the Clock Gating Register
switch (baseAddress)
{
case PORTA :
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R0;
break;
case PORTB :
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R1;
break;
case PORTC :
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R2;
break;
case PORTD :
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R3;
break;
case PORTE :
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R4;
break;
case PORTF :
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R5;
break;
default:
return false;
}
// Delay a bit
delay = SYSCTL_RCGCGPIO_R;
// Set the Direction Register
myPort->Direction &= ~(myConfig->Input);
myPort->Direction |= myConfig->Output;
// Enable Pull-Up Resistors
myPort->PullUpSelect |= myConfig->PullUp;
// Disable the Alternate Function Select
myPort->AlternateFunctionSelect |= myConfig->AlternateFunctionEnable;
// Enable Pins as Digital Pins
myPort->DigitalEnable |= myConfig->DigitalEnable;
//Select control register values
myPort->PortControl |= myConfig->PortControl;
return true;
}
//*****************************************************************************
// Display the LED character
//*****************************************************************************
void displayLEDChar(uint8_t symbol, uint8_t color){
static uint8_t rowIndex = 7;
static uint8_t charIndex = 0;
static uint8_t currPWM = 0;
// Disable all Outputs
GpioPortF->Data |= ~OUTPUT_ENABLE_B;
// Activate rowIndex
GpioPortC->Data = ROW_EN;
GpioPortB->Data = ~(1 << rowIndex);
GpioPortC->Data = ENABLES_OFF;
// CLEAR LEDs
GpioPortC->Data = RED_EN;
GpioPortB->Data = 0xFF;
GpioPortC->Data = ENABLES_OFF;
GpioPortC->Data = BLUE_EN;
GpioPortB->Data = 0xFF;
GpioPortC->Data = ENABLES_OFF;
GpioPortC->Data = GREEN_EN;
GpioPortB->Data = 0xFF;
GpioPortC->Data = ENABLES_OFF;
//Turn on LEDs only if pwm allows it
currPWM++;
if(currPWM == 100)
currPWM = 0;
if(currPWM < pwm){
// Set color LEDs
//RED
if(color == 0){
GpioPortC->Data = RED_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
}
//YEllOW
else if (color == 1){
GpioPortC->Data = RED_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
GpioPortC->Data = GREEN_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
}
//GREEN
else if (color == 2){
GpioPortC->Data = GREEN_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
}
//BLUE
else if (color == 3){
GpioPortC->Data = BLUE_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
}
//INDIGO
else if (color == 4){
GpioPortC->Data = GREEN_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
GpioPortC->Data = BLUE_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
}
//PURPLE
else if (color == 5){
GpioPortC->Data = BLUE_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
GpioPortC->Data = RED_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
}
//White
else if (color == 6){
GpioPortC->Data = RED_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
GpioPortC->Data = BLUE_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
GpioPortC->Data = GREEN_EN;
GpioPortB->Data = ~(ucDispChar[symbol][charIndex]);
}
GpioPortC->Data = ENABLES_OFF;
}
// Enable All Output
GpioPortF->Data &= OUTPUT_ENABLE_B;
//Increment the current row index to turn on
if(nextLEDrow){
nextLEDrow = false;
if (rowIndex == 0)
rowIndex = 7;
else
rowIndex--;
//Increment the LEDchars that turn on
if (charIndex == 7)
charIndex = 0;
else
charIndex++;
}
}
//*****************************************************************************
// Debounce the shift registers
//*****************************************************************************
uint16_t debounce(uint32_t base, uint8_t pin, uint16_t shiftReg){
//A 16 bit int that needs to be in form
//1000 0000 0000 0000 to have been debounced
//Tells whether the pin is currently high or low
uint8_t high = 1;
GPIO_PORT *currPort = (GPIO_PORT *)base;
//Keep sampling the button every 1 ms until it's debounced
//The timer interrupts every 1ms
if ((currPort->Data & pin) == 0)
high = 0;
else
high = 1;
shiftReg = (shiftReg << 1) + high;
return shiftReg;
}
//*****************************************************************************
// Determine whether there is a button press
//*****************************************************************************
bool checkPB(uint16_t shiftReg){
if (shiftReg == 0x8000){
return true;
}
else
return false;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
// Initialize the ADC on portE
//*****************************************************************************
void ADCInit(){
uint32_t delay= 10;
// Enable Port E
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R4;
//Wait 10ms for enable
sysTickSleep(delay);
// Set the direction as an input
GPIO_PORTE_DIR_R &= ~(PIN_2 | PIN_3);
// Set the alternate function
GPIO_PORTE_AFSEL_R |= ( PIN_2 | PIN_3 );
// Disable the Digital Enable
GPIO_PORTE_DEN_R &= ~( PIN_2 | PIN_3);
// Enable Analog
GPIO_PORTE_AMSEL_R |= ( PIN_2 | PIN_3);
SYSCTL_RCGCADC_R |= SYSCTL_RCGCADC_R0;
//Wait 10ms for enable
sysTickSleep(delay);
// disable the sample sequencer by writing a 0 to the corresponding ASENn bit in the ADCACTSS register
ADC0_ACTSS_R &= ~ADC_ACTSS_ASEN3;
// Sequencer 3 is the lowest priority
ADC0_SSPRI_R = ADC_SSPRI_SS3_4TH | ADC_SSPRI_SS2_3RD | ADC_SSPRI_SS1_2ND | ADC_SSPRI_SS0_1ST;
ADC0_EMUX_R &= ~ADC_EMUX_EM3_ALWAYS;
ADC0_SSMUX3_R &= ~ADC_SSMUX3_MUX0_M;
ADC0_SSCTL3_R = ADC_SSCTL3_IE0 | ADC_SSCTL3_END0;
// Clear Averaging Bits
ADC0_SAC_R &= ~ADC_SAC_AVG_M ;
// Average 64 samples
ADC0_SAC_R |= ADC_SAC_AVG_64X;
}
//*****************************************************************************
// Get the ADC value of a given ADC Channel
//*****************************************************************************
uint32_t GetADCval(uint32_t Channel)
{
uint32_t result;
ADC0_SSMUX3_R = Channel; // Set the channel
ADC0_ACTSS_R |= ADC_ACTSS_ASEN3; // Enable SS3
ADC0_PSSI_R = ADC_PSSI_SS3; // Initiate SS3
while(0 == (ADC0_RIS_R & ADC_RIS_INR3)); // Wait for END of conversion
result = ADC0_SSFIFO3_R & 0x0FFF; // Read the 12-bit ADC result
ADC0_ISC_R = ADC_ISC_IN3; // Acknowledge completion
return result;
}
void initializeDisplay(){
int i = 0;
// Disable all Outputs
GpioPortF->Data |= ~OUTPUT_ENABLE_B;
while(i < 8){
// Activate rowIndex
GpioPortC->Data = ROW_EN;
GpioPortB->Data = ~(1 << i);
GpioPortC->Data = ENABLES_OFF;
// CLEAR LEDs
GpioPortC->Data = RED_EN;
GpioPortB->Data = 0xFF;
GpioPortC->Data = ENABLES_OFF;
GpioPortC->Data = BLUE_EN;
GpioPortB->Data = 0xFF;
GpioPortC->Data = ENABLES_OFF;
GpioPortC->Data = GREEN_EN;
GpioPortB->Data = 0xFF;
GpioPortC->Data = ENABLES_OFF;
i++;
}
// Enable All Output
GpioPortF->Data &= OUTPUT_ENABLE_B;
}
int
main(void)
{
//Local Variables
bool displayMode = true;//True if display mode, false if in data entry mode
//Push buttons
bool upPB = false;
bool rightPB = false;
bool downPB = false;
bool leftPB = false;
bool modePB = false;
//Push buttons shift registers
uint16_t shiftRegSW2 = 0xFFFF;
uint16_t shiftRegSW3 = 0xFFFF;
uint16_t shiftRegSW4 = 0xFFFF;
uint16_t shiftRegSW5 = 0xFFFF;
uint16_t shiftRegSW6 = 0xFFFF;
//Initial display message
uint8_t displayArray [SAVED_MSG_LEN_MAX] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
uint8_t inputArray [17] =
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
//Mark the indexcies of the array
uint8_t endIndex = 19;
uint8_t currIndex = 0;
uint8_t inputIndex = 0;
uint8_t tempIndex = 0;
//Flag for overflwoing max char array length
bool overflow = false;
//Init color
uint8_t color;
//ADCval1
uint32_t ADCval1 = 0;
uint32_t ADCval2 = 0;
//Current string
char myString[21] = "hello world";
char myChar;
int8_t stringIndex=-1;
// Initialize the PLLs so the the main CPU frequency is 80MHz
PLL_Init();
//Configure Port C
initPortC();
//Configure the SYSTICK timer 12.5uS ticks
SYSTICKConfig(1000, true);
//Configure Timer0 1mS ticks
TIMER0Config(80000);
//Configure watchdog with 1s reset
WatchdogTIMERConfig();
// Initialize the GPIO Ports
initializeGPIOPort(PORTA, &portA_config);
initializeGPIOPort(PORTB, &portB_config);
initializeGPIOPort(PORTC, &portC_config);
initializeGPIOPort(PORTD, &portD_config);
initializeGPIOPort(PORTE, &portE_config);
initializeGPIOPort(PORTF, &portF_config);
// Initialize SPI0 interface
initializeSPI(SSI0, PHASE, POLARITY);
//Init ADC
ADCInit();
initializeDisplay();
// Set up the UARTS for 115200 8N1
InitializeUART(UART0);
InitializeUART(UART2);
InitializeUART(UART5);
// Since PD7 is shared with the NMI, we need to clear the lock register and
// set the commit register so that all the pins alternate functions can
// be used.
GPIO_PORTD_LOCK_R = 0x4C4F434B;
GPIO_PORTD_CR_R = 0xFF;
initializeGPIOPort(PORTD, &portD_config);
EnableInterrupts();
//Get initial ADC values
pwm = GetADCval(POT_RIGHT) / 40;
ADCval2 = GetADCval(POT_LEFT) / 575;
// Print out the current string
uartTxPoll(UART0,"Hello World\n\r");
while(1)
{
if(checkADC){
pwm = GetADCval(POT_RIGHT) / 40;
ADCval2 = GetADCval(POT_LEFT) / 600;
checkADC = false;
}
//On systick interrupt display the current character and poll the buttons
if (refreshLED){
if (displayMode){
displayLEDChar(displayArray[currIndex], ADCval2);
}
else{
displayLEDChar(inputArray[inputIndex], ADCval2);
}
refreshLED = false;
}
if (buttonPoll){
//Check if any buttons have been pushed
//If so debounce them
buttonPoll = false;
//SW2
shiftRegSW2 = debounce(PORTA, SW2, shiftRegSW2);
upPB = checkPB(shiftRegSW2);
//SW3
shiftRegSW3 = debounce(PORTA, SW3, shiftRegSW3);
rightPB = checkPB(shiftRegSW3);
//SW4
shiftRegSW4 = debounce(PORTD, SW4, shiftRegSW4);
downPB = checkPB(shiftRegSW4);
//SW5
shiftRegSW5 = debounce(PORTD, SW5, shiftRegSW5);
leftPB = checkPB(shiftRegSW5);
//SW6
shiftRegSW6 = debounce(PORTF, SW6, shiftRegSW6);
modePB = checkPB(shiftRegSW6);
}//End polling
//Display Mode
if (displayMode){
//Check if right button is pressed
if(rightPB){
//Clear the button press
rightPB = false;
//Display the next character in order in green
color = GREEN_EN;
if(currIndex == endIndex)
currIndex = 0;
else
currIndex++;
}
//Check if left button is pressed
else if(leftPB){
//Clear the button press
leftPB = false;
//Display the next character in reverse order in red
color = RED_EN;
if(currIndex == 0)
currIndex = endIndex;
else
currIndex--;
}
//Check if mode button is pushed
if(modePB){
//Clear the button press
modePB = false;
//change mode and reset parameters
displayMode = false;
inputIndex = 0;
currIndex = 0;
endIndex = 0;
overflow = false;
tempIndex = 0;
displayArray[0] = 0;
}
}
//Input Mode
else{
//Check if right button is pressed
if(rightPB || leftPB){
//Clear the button press
rightPB = false;
leftPB = false;
//Save the current input character in the display array if
//there is enough space
if (tempIndex >= SAVED_MSG_LEN_MAX){
tempIndex= 0;
overflow = true;
}
displayArray[tempIndex] = inputArray[inputIndex];
tempIndex++;
}
//Check if up button is pressed
else if(upPB){
//Clear the button press
upPB = false;
//Display the next character in order in input array
if(inputIndex == 15)
inputIndex = 0;
else
inputIndex++;
}
//Check if down button is pressed
else if(downPB){
//Clear the button press
downPB = false;
//Display the next character in reverse order input array
if(inputIndex == 0)
inputIndex = 15;
else
inputIndex--;
}
if(modePB){
//Clear the button press
modePB = false;
//change mode to display and reset parameters
displayMode = true;
color = GREEN_EN;
if(overflow){
endIndex = SAVED_MSG_LEN_MAX - 1;
}
else if (tempIndex != 0)
endIndex = tempIndex - 1;
}
}
}
}