Repository navigation
Expand file tree
/
Copy pathLab8Main.c
More file actions
285 lines (270 loc) · 8.96 KB
/
Copy pathLab8Main.c
File metadata and controls
285 lines (270 loc) · 8.96 KB
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
// Runs on MSPM0G3507
// Rishaan Malik
// Last Modified: 4/7/26
// Spring 2026: message format {0x3C,digit1,digit2,digit3}
// where each digit is an ASCII character '0' to '9'
#include <stdio.h>
#include <stdint.h>
#include <ti/devices/msp/msp.h>
#include "../inc/ST7735.h"
#include "../inc/Clock.h"
#include "../inc/LaunchPad.h"
#include "../inc/TExaS.h"
#include "../inc/Timer.h"
#include "../inc/ADC1.h"
#include "../inc/FIFO1.h"
#include "UART1.h"
#include "UART2.h"
// the data sheet says the ADC does not work when clock is 80 MHz
// however, the ADC seems to work on my boards at 80 MHz
// I suggest you try 80MHz, but if it doesn't work, switch to 40MHz
void PLL_Init(void){ // set phase lock loop (PLL)
// Clock_Init40MHz(); // run this line for 40MHz
Clock_Init80MHz(0); // run this line for 80MHz
}
void OutFix(uint32_t n){
uint32_t whole = n/1000;
uint32_t part = n%1000;
printf("%u.%03u cm", whole, part);
}
// UART0 routines implemented in TExaS.c
void UART_Init(void); // 11520 baud
void UART_OutChar(char data); // busy-wait
void UART_OutString(char *pt){
while(*pt){
UART_OutChar(*pt);
pt++;
}
}
uint32_t ReceiveCount,TransmitCount;
uint32_t Position; // fixed point 0.001cm, in receiver
uint32_t Data; // 12-bit raw ADC data in transmitter
// *****************************
// use main1 to test your FIFO1
// the object code myFifo.o has my solution
uint32_t M;
uint32_t Random32(void){
M = 1664525*M+1013904223;
return M;
}
uint32_t Random(uint32_t n){
return (Random32()>>16)%n;
}
// these are grader implementations, hidden from students
void myFifo_Init(uint32_t size);
uint32_t myFifo_Put(char data);
char myFifo_Get(void);
uint32_t FifoError;
int main1(void){ // use main1 to test your FIFO1
char me,you;
char data;
__disable_irq();
PLL_Init(); // set bus speed
LaunchPad_Init();
UART_Init();
UART_OutString("main1: Test of FIFO1\n\r");
// your FIFO must be larger than 7 and less than 127
Fifo1_Init(); // your FIFO
M = 4; // seed
FifoError = 0;
// myFifo_Init(16) means FIFO can store up to 15 elements
myFifo_Init(16); // change 16 to match your FIFO size
for(uint32_t i = 0; i<10000; i++){
uint32_t k = Random(4);
for(uint32_t l=0; l<k ;l++){
data = Random(255)+1; // 1 to 255
you = Fifo1_Put(data);
me = myFifo_Put(data);
if(me != you){
FifoError++; // put a breakpoint here
}
}
uint32_t j = Random(40);
for(uint32_t l=0; l<j ;l++){
data = Random(255)+1; // 1 to 255
you = Fifo1_Put(data);
me = myFifo_Put(data);
if(me != you){
FifoError++; // put a breakpoint here
}
}
for(uint32_t l=0; l<j ;l++){
you = Fifo1_Get();
me = myFifo_Get();
if(me != you){
FifoError++; // put a breakpoint here
}
}
for(uint32_t l=0; l<k ;l++){
you = Fifo1_Get();
me = myFifo_Get();
if(me != you){
FifoError++; // put a breakpoint here
}
}
}
if(FifoError == 0){
UART_OutString("Success\n\r");
for(;;){ // success
GPIOB->DOUTTGL31_0 = GREEN; // green means good
for(int i=0; i<50000; i++){
}
}
}else{
UART_OutString("Failure\n\r");
for(;;){ // failure
GPIOB->DOUTTGL31_0 = RED; // red
for(int i=0; i<50000; i++){
}
}
}
}
// use main2 to test UART1 blind transmission
// no interrupts, just PA8 U1Tx output
int main2(void){ // main2
__disable_irq();
PLL_Init(); // set bus speed
LaunchPad_Init();
UART1_Init(); // just transmit, PA8, blind synchronization
TExaS_Init(ADC0,6,0); //PB20 = ADC0 channel 6, connected to PA8
__enable_irq(); // interrupts for TExaS, not UART1
while(1){ // one frame
UART1_OutChar('<'); // 0x3C
Clock_Delay1ms(10); // about 100Hz
}
}
// use main3 to test 4-frame message transmission
// no interrupts, just PA8 U1Tx output
int main3(void){ // main3
__disable_irq();
PLL_Init(); // set bus speed
LaunchPad_Init();
UART1_Init(); // just transmit, PA8, blind synchronization
TExaS_Init(ADC0,6,0); //PB20 = ADC0 channel 6, connected to PA8
__enable_irq(); // interrupts for TExaS, not UART1
while(1){ // message is 1.547 cm
UART1_OutChar('<'); // 0x3C
UART1_OutChar('1'); // 0x31
UART1_OutChar('5'); // 0x35
UART1_OutChar('4'); // 0x34
Clock_Delay1ms(50); // about 20Hz
}
}
// use main4 to test blind transmission and receive timeout interrupt
// connect PA22 U2Rx input to PA8 U1Tx output on the same board
int main4(void){ // main4, loop back test
char data1,data2,data3,data4;
__disable_irq();
PLL_Init(); // set bus speed
LaunchPad_Init();
UART_Init();
UART_OutString("main4: loop back test\n\r");
UART_OutString("Connect PA22 to PA8 on the same board\n\r");
UART1_Init(); // just transmit, PA8, blind synchronization
UART2_Init(); // just receive, PA22, receiver timeout interrupt synchronization
__enable_irq(); // interrupts for UART1
while(1){ // message is 1.547 cm
UART1_OutChar('<'); // 0x3C
UART1_OutChar('1'); // 0x31
UART1_OutChar('5'); // 0x35
UART1_OutChar('4'); // 0x34
Clock_Delay1ms(1000); // about 1Hz
data1 = UART2_InChar(); // should be 3C
UART_OutChar(data1);
data2 = UART2_InChar(); // should be 31
UART_OutChar(data2);
UART_OutChar('.'); // implied
data3 = UART2_InChar(); // should be 35
UART_OutChar(data3);
data4 = UART2_InChar(); // should be 34
UART_OutChar(data4);
UART_OutString("\n\r");
}
}
// sampling frequency is 30 Hz
void TIMG12_IRQHandler(void){uint32_t pos,msg;
if((TIMG12->CPU_INT.IIDX) == 1){ // this will acknowledge
GPIOB->DOUTTGL31_0 = GREEN; // toggle PB27 (minimally intrusive debugging)
// increment TransmitCount
TransmitCount++;
// sample
Data = ADCin();
GPIOB->DOUTTGL31_0 = GREEN; // toggle PB27 (minimally intrusive debugging)
// convert to fixed point distance
pos = Convert(Data);
// output 4-frame message
UART1_OutChar(0x3C); // start byte
UART1_OutChar('0'+(pos/1000)%10); // ones digit
UART1_OutChar('0'+(pos/100)%10); // tenths digit
UART1_OutChar('0'+(pos/10)%10); // hundredths digit
GPIOB->DOUTTGL31_0 = GREEN; // toggle PB27 (minimally intrusive debugging)
}
}
uint8_t TExaS_LaunchPadLogicPB27PB26(void){
return (0x80|((GPIOB->DOUT31_0>>26)&0x03));
}
// use main5 to test entire system in loop back
// connect PA22 U2Rx input to PA8 U1Tx output on the same board
// connect slidepot pin 2 to PB18
// Data should go from 0 to 4095 in transmitter
// Position should go from 0 to 2000 in receiver
// LCD should show 0.00cm to 2.00 cm
int main(void){ // main5
__disable_irq();
PLL_Init(); // set bus speed
LaunchPad_Init();
ReceiveCount=0; TransmitCount=0;
UART1_Init(); // just transmit, PA8, blind synchronization
UART2_Init(); // just receive, PA22, receiver timeout synchronization
ST7735_InitPrintf(INITR_BLACKTAB); // INITR_REDTAB for AdaFruit, INITR_BLACKTAB for HiLetGo
ST7735_FillScreen(ST7735_BLACK);
ADCinit(); //PB18 = ADC1 channel 5, slidepot
TExaS_Init(0,0,&TExaS_LaunchPadLogicPB27PB26); // PB27 and PB26
ST7735_PlotClear(0,2000);
// initialize interrupts on TimerG12 at 30 Hz
TimerG12_IntArm(2666667, 2); // 80MHz/2666667 = 30Hz
__enable_irq();
while(1){uint32_t PositionReceived;
// complete this
// move cursor to top left
ST7735_SetCursor(0, 0);
// wait for first frame
char d1;
do{
d1 = UART2_InChar();
}while(d1 != '<');
// increment ReceiveCount
ReceiveCount++;
// receive next three frames
char d2;
do{
d2 = UART2_InChar();
}while(d2 == 0); // wait until we actually get something
char d3;
do{
d3 = UART2_InChar();
}while(d3 == 0); // wait until we actually get something
char d4;
do{
d4 = UART2_InChar();
}while(d4 == 0); // wait until we actually get something
GPIOB->DOUTTGL31_0 = RED; // toggle PB26 (minimally intrusive debugging)
// output distance to ST7735
// calculate PositionReceived from message
PositionReceived = (d2-'0')*1000 + (d3-'0')*100 + (d4-'0')*10;
OutFix(PositionReceived);
if((ReceiveCount%15)==0){
ST7735_PlotPoint(PositionReceived);
ST7735_PlotNextErase(); // data plotted at about 2 Hz
}
}
}
// use main5 to test entire system in distributed mode
// needs two identical MSPM0 LaunchPads
// connect PA22 U2Rx input on one to PA8 U1Tx output on the other board
// connect PA8 U1Tx output on one to PA22 U2Rx input on the other board
// connect grounds of two boards together
// connect slidepot pin 2 to PB18 on both boards
// Data should go from 0 to 4095 in transmitter
// PositionReceived should go from 0 to 2000 in receiver
// LCD should show 0.000cm to 2.000 cm