/
main.c
398 lines (348 loc) · 10 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
/*
* main.c - Initialisation and core loops
*
* Created on: 07/05/2013
* Author: Coen McClelland
* ENGG4810: Team 23
*
*/
// Library Includes
#include "inc/hw_ints.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "third_party/fatfs/src/diskio.h"
#include "driverlib/gpio.h"
#include "driverlib/adc.h"
#include <stdint.h>
#include "driverlib/usb.h"
#include "driverlib/udma.h"
#include "usblib/usblib.h"
#include "usblib/usb-ids.h"
#include "usblib/device/usbdevice.h"
#include "usblib/device/usbdmsc.h"
#include "usb_msc_structs.h"
// Local Includes
#include "timers.h"
#include "sdcard.h"
#include "uart.h"
#include "dac.h"
#include "btn.h"
#include "fx.h"
#include "adc.h"
// Definitions
////*****************************************************************************
////
//// The number of ticks to wait before falling back to the idle state. Since
//// the tick rate is 100Hz this is approximately 3 seconds.
////
////*****************************************************************************
#define USBMSC_ACTIVITY_TIMEOUT 300
//
//// Global Variables
volatile unsigned long g_ulTimeStamp = 0; // Time since boot in 10ms increments
extern unsigned long g_ulIdleTimeout;
//
////*****************************************************************************
////
//// This enumeration holds the various states that the device can be in during
//// normal operation.
////
////*****************************************************************************
volatile enum
{
//
// Unconfigured.
//
MSC_DEV_DISCONNECTED,
//
// Connected but not yet fully enumerated.
//
MSC_DEV_CONNECTED,
//
// Connected and fully enumerated but not currently handling a command.
//
MSC_DEV_IDLE,
//
// Currently reading the SD card.
//
MSC_DEV_READ,
//
// Currently writing the SD card.
//
MSC_DEV_WRITE,
}
g_eMSCState;
//
////*****************************************************************************
////
//// The Flags that handle updates to the status area to avoid drawing when no
//// updates are required..
////
////*****************************************************************************
#define FLAG_UPDATE_STATUS 1
static unsigned long g_ulFlags;
static unsigned long g_ulIdleTimeout;
//
////******************************************************************************
////
//// The DMA control structure table.
////
////******************************************************************************
#ifdef ewarm
#pragma data_alignment=1024
tDMAControlTable sDMAControlTable[64];
#elif defined(ccs)
#pragma DATA_ALIGN(sDMAControlTable, 1024)
tDMAControlTable sDMAControlTable[64];
#else
tDMAControlTable sDMAControlTable[64] __attribute__ ((aligned(1024)));
#endif
//
////*****************************************************************************
////
//// Handles bulk driver notifications related to the receive channel (data from
//// the USB host).
////
//// \param pvCBData is the client-supplied callback pointer for this channel.
//// \param ulEvent identifies the event we are being notified about.
//// \param ulMsgValue is an event-specific value.
//// \param pvMsgData is an event-specific pointer.
////
//// This function is called by the bulk driver to notify us of any events
//// related to operation of the receive data channel (the OUT channel carrying
//// data from the USB host).
////
//// \return The return value is event-specific.
////
////*****************************************************************************
unsigned long
RxHandler(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgValue, void *pvMsgData)
{
return(0);
}
//
////*****************************************************************************
////
//// Handles bulk driver notifications related to the transmit channel (data to
//// the USB host).
////
//// \param pvCBData is the client-supplied callback pointer for this channel.
//// \param ulEvent identifies the event we are being notified about.
//// \param ulMsgValue is an event-specific value.
//// \param pvMsgData is an event-specific pointer.
////
//// This function is called by the bulk driver to notify us of any events
//// related to operation of the transmit data channel (the IN channel carrying
//// data to the USB host).
////
//// \return The return value is event-specific.
////
////*****************************************************************************
unsigned long
TxHandler(void *pvCBData, unsigned long ulEvent, unsigned long ulMsgValue,
void *pvMsgData)
{
return(0);
}
//
//
////*****************************************************************************
////
//// This function is the call back notification function provided to the USB
//// library's mass storage class.
////
////*****************************************************************************
unsigned long
USBDMSCEventCallback(void *pvCBData, unsigned long ulEvent,
unsigned long ulMsgParam, void *pvMsgData)
{
//
// Reset the time out every time an event occurs.
//
g_ulIdleTimeout = USBMSC_ACTIVITY_TIMEOUT;
switch(ulEvent)
{
//
// Writing to the device.
//
case USBD_MSC_EVENT_WRITING:
{
//
// Only update if this is a change.
//
if(g_eMSCState != MSC_DEV_WRITE)
{
//
// Go to the write state.
//
g_eMSCState = MSC_DEV_WRITE;
//
// Cause the main loop to update the screen.
//
g_ulFlags |= FLAG_UPDATE_STATUS;
}
break;
}
//
// Reading from the device.
//
case USBD_MSC_EVENT_READING:
{
//
// Only update if this is a change.
//
if(g_eMSCState != MSC_DEV_READ)
{
//
// Go to the read state.
//
g_eMSCState = MSC_DEV_READ;
//
// Cause the main loop to update the screen.
//
g_ulFlags |= FLAG_UPDATE_STATUS;
}
break;
}
//
// The USB host has disconnected from the device.
//
case USB_EVENT_DISCONNECTED:
{
//
// Go to the disconnected state.
//
g_eMSCState = MSC_DEV_DISCONNECTED;
//
// Cause the main loop to update the screen.
//
g_ulFlags |= FLAG_UPDATE_STATUS;
break;
}
//
// The USB host has connected to the device.
//
case USB_EVENT_CONNECTED:
{
//
// Go to the idle state to wait for read/writes.
//
g_eMSCState = MSC_DEV_IDLE;
break;
}
case USBD_MSC_EVENT_IDLE:
default:
{
break;
}
}
return(0);
}
/* SysTick Handler
*
* - Required by FatFS for SD card timings
* - On a 10ms interrupt
*/
void SysTickHandler(void) {
// Alert FatFS and increment the timestamp
disk_timerproc();
++g_ulTimeStamp;
if(g_ulIdleTimeout > 0) {
g_ulIdleTimeout--;
}
}
/* Main function
*
* - Initialise device and any global variables
* - Enable Interrupts
* - Start endless loop
*/
void main(void) {
unsigned long ulRetcode;
// Initialise the device clock to 80MHz
ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN );
// Enable SysTick for FatFS at 10ms intervals
ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100);
ROM_SysTickEnable();
ROM_SysTickIntEnable();
//
// Configure and enable uDMA
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);
SysCtlDelay(10);
ROM_uDMAControlBaseSet(&sDMAControlTable[0]);
ROM_uDMAEnable();
//
// Enable the USB controller.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);
//
// Set the USB pins to be controlled by the USB controller.
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
ROM_GPIOPinTypeUSBAnalog(GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5);
// Initialize the idle timeout and reset all flags.
//
g_ulIdleTimeout = 0;
g_ulFlags = 0;
//
// Initialize the state to idle.
//
g_eMSCState = MSC_DEV_DISCONNECTED;
//
// Set the USB stack mode to Device mode with VBUS monitoring.
//
USBStackModeSet(0, USB_MODE_DEVICE, 0);
//
// Pass our device information to the USB library and place the device
// on the bus.
//
USBDMSCInit(0, (tUSBDMSCDevice *)&g_sMSCDevice);
//
// Determine whether or not an SDCard is installed. If not, print a
// warning and have the user install one and restart.
//
ulRetcode = disk_initialize(0);
// Enable Global interrupts
ROM_IntMasterEnable();
// Enable floating point arithmetic unit, but disable stacking
ROM_FPUEnable();
ROM_FPUStackingDisable();
// Initialise GPIO - All ports enabled
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
// Unlock NMI pins for GPIO usage (PD7 + PF0)
HWREG(GPIO_PORTD_BASE + 0x520) = 0x4C4F434B;
HWREG(GPIO_PORTF_BASE + 0x520) = 0x4C4F434B;
HWREG(GPIO_PORTD_BASE + 0x524) = 0x000000FF;
HWREG(GPIO_PORTF_BASE + 0x524) = 0x000000FF;
HWREG(GPIO_PORTD_BASE + 0x520) = 0x00000000;
HWREG(GPIO_PORTF_BASE + 0x520) = 0x00000000;
// Initialise GPIO Expander (probably not happening)
// Initialise Buttons
btn_init();
// Initialise FX
fx_init();
// Initialise ADC
adc_init();
// Initialise DAC
dac_init();
// Initialise SD Card and Mass storage
sdcard_init();
// Initialise UART for debugging
uart_init();
// Initialise Timers
timers_init();
for(;;) {
// Endless Loop
ADCProcessorTrigger(ADC0_BASE, 0);
SysCtlDelay(SysCtlClockGet() / 120); // 25ms
}
}