Subversion Repositories Projects

Rev

Rev 10 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10 dhylands 1
/****************************************************************************
2
*
3
*   Copyright (c) 2006 Dave Hylands     <dhylands@gmail.com>
4
*
5
*   This program is free software; you can redistribute it and/or modify
6
*   it under the terms of the GNU General Public License version 2 as
7
*   published by the Free Software Foundation.
8
*
9
*   Alternatively, this software may be distributed under the terms of BSD
10
*   license.
11
*
12
*   See README and COPYING for more details.
13
*
14
****************************************************************************
15
*
16
*   Test program for monitoring the ADC lines.
17
*
18
****************************************************************************/
19
 
20
/* ---- Include Files ---------------------------------------------------- */
21
 
22
#include <stdio.h>
23
#include <stdint.h>
24
 
25
 
26
#include "hw_types.h"
27
#include "hw_ints.h"
28
#include "hw_memmap.h"
29
 
30
#include "adc.h"
31
#include "gpio.h"
32
#include "interrupt.h"
33
#include "sysctl.h"
34
#include "timer.h"
35
#include "uart.h"
36
 
37
#include "lcd.h"
38
#include "Str.h"
39
 
40
/* ---- Public Variables ------------------------------------------------- */
41
/* ---- Private Constants and Types -------------------------------------- */
42
 
43
#define PUSH_BUTTON             GPIO_PIN_4
44
#define USER_LED                GPIO_PIN_5
45
 
46
/* ---- Private Variables ------------------------------------------------ */
47
 
48
unsigned        long    gADC[ 4 ];
49
 
50
unsigned    long    gFlags;
51
 
52
#define FLAG_ADC_SAMPLES_AVAIL  0
53
 
54
/* ---- Private Function Prototypes -------------------------------------- */
55
 
13 dhylands 56
void ADC_isr( void ) __attribute__(( __interrupt__ ));
57
 
10 dhylands 58
void PutStr( const char *str );
59
void InitADC( void );
60
 
61
/* ---- Functions -------------------------------------------------------- */
62
 
63
/****************************************************************************
64
*
65
*   PutStr
66
*
67
****************************************************************************/
68
 
69
void PutStr( const char *str )
70
{
71
    while ( *str != '\0' )
72
    {
73
        if ( *str == '\n' )
74
        {
75
            UARTCharPut( UART0_BASE, '\r' );
76
        }
77
        UARTCharPut( UART0_BASE, *str );
78
 
79
        str++;
80
    }
81
}
82
 
83
/****************************************************************************
84
*
85
*   Interrupt handler which fires when a sequence has been collected.
86
*
87
****************************************************************************/
88
 
89
void ADC_isr( void )
90
{
91
    // Clear the interrupt
92
 
93
    ADCIntClear( ADC_BASE, 0 );
94
 
95
    // Grab the data from the ADC
96
 
97
    ADCSequenceDataGet( ADC_BASE, 0, &gADC[ 0 ]);
98
 
99
    HWREGBITW( &gFlags, FLAG_ADC_SAMPLES_AVAIL ) = 1;
100
 
101
} // ADC_isr
102
 
103
/****************************************************************************
104
*
105
*   Initialize the ADC to collect sample from our 4 line sensors.
106
*
107
****************************************************************************/
108
 
109
void InitADC( void )
110
{
111
    // Enable the ADC peripheral
112
 
113
    SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );
114
 
115
    // Setup the sequence
116
 
117
    //
118
    // Configure the ADC to sample the potentiometer when the timer expires.
119
    // After sampling, the ADC will interrupt the processor; this is used as
120
    // the heartbeat for the game.
121
    //
122
    ADCSequenceConfigure( ADC_BASE,             // base
123
                          0,                    // sequence num
124
                          ADC_TRIGGER_TIMER,    // trigger
125
 
126
 
127
    ADCSequenceStepConfigure( ADC_BASE,         // base
128
                              0,                // sequence num
129
                              0,                // step num
130
                              ADC_CTL_CH0 );    // config
131
 
132
    ADCSequenceStepConfigure( ADC_BASE,         // base
133
                              0,                // sequence num
134
                              1,                // step num
135
                              ADC_CTL_CH1 );    // config
136
 
137
    ADCSequenceStepConfigure( ADC_BASE,         // base
138
                              0,                // sequence num
139
                              2,                // step num
140
                              ADC_CTL_CH2 );    // config
141
 
142
    ADCSequenceStepConfigure( ADC_BASE,         // base
143
                              0,                // sequence num
144
                              3,                // step num
145
                              ADC_CTL_CH3 | ADC_CTL_IE | ADC_CTL_END ); //config
146
 
147
    ADCSequenceEnable( ADC_BASE, 0 );
148
 
149
    ADCIntEnable( ADC_BASE, 0);
150
    IntEnable(INT_ADC0);
151
 
152
} // InitADC
153
 
154
/****************************************************************************
155
*
156
*   Main loop for the test program
157
*
158
****************************************************************************/
159
 
160
int main( void )
161
{
162
    int         spinIdx = 0;
163
    const char *spinner = "-\\|/";
164
    char        buf[ 20 ];
165
    int         ledCounter;
166
 
167
    // Set the clocking to run at 20MHz from the PLL.
168
 
169
    SysCtlClockSet( SYSCTL_SYSDIV_10
170
                  | SYSCTL_USE_PLL
171
                  | SYSCTL_OSC_MAIN
172
                  | SYSCTL_XTAL_6MHZ );
173
 
174
 
175
    LCD_Init( 2, 16 );
176
    LCD_PutStr( "ADC-Test" );
177
 
178
        InitADC();
179
 
180
    // Configure Timer0 to generate a 10 kHz PWM signal for driving
181
    // the user LED.
182
 
183
    SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );
184
    TimerConfigure( TIMER0_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_PWM );
185
    TimerLoadSet(TIMER0_BASE, TIMER_B, (SysCtlClockGet() / 10000) - 1 );
186
    TimerMatchSet( TIMER0_BASE, TIMER_B, 0 );
187
    TimerControlLevel( TIMER0_BASE, TIMER_B, true );
188
    TimerEnable( TIMER0_BASE, TIMER_B );
189
 
190
    // Confgure Timer 1 to generate triggers for the ADC
191
 
192
    SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );
193
 
194
    HWREG( ADC_BASE + 0x30 ) = 6;
195
 
196
    TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );
197
    TimerLoadSet( TIMER1_BASE, TIMER_A, SysCtlClockGet() / 20 );
198
    TimerControlStall( TIMER1_BASE, TIMER_A, true );
199
    TimerControlTrigger( TIMER1_BASE, TIMER_A, true );
200
    TimerEnable( TIMER1_BASE, TIMER_A );
201
 
202
    SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOA );
203
    SysCtlPeripheralEnable( SYSCTL_PERIPH_UART0 );
204
 
205
    // Configure the first UART for 115,200, 8-N-1 operation.
206
 
207
    GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
208
 
209
    UARTConfigSet( UART0_BASE,
210
                   115200,
211
                   ( UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE ));
212
    UARTEnable( UART0_BASE );
213
 
214
    // Setup the User LED & pushbutton
215
 
216
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
217
    GPIODirModeSet( GPIO_PORTC_BASE, USER_LED, GPIO_DIR_MODE_OUT );
218
    GPIOPinWrite( GPIO_PORTC_BASE, USER_LED, 0 );
219
 
220
 
221
    PutStr( "*****\n" );
222
    PutStr( "***** ADC-Test program\n" );
223
    PutStr( "*****\n" );
224
 
225
    ledCounter = 0;
226
 
227
    TimerMatchSet( TIMER0_BASE, TIMER_B, 0 );
228
    GPIODirModeSet( GPIO_PORTC_BASE, USER_LED, GPIO_DIR_MODE_HW );
229
 
230
    while ( 1 )
231
    {
232
        while ( HWREGBITW( &gFlags, FLAG_ADC_SAMPLES_AVAIL ) == 0 )
233
        {
234
            ;
235
        }
236
        HWREGBITW( &gFlags, FLAG_ADC_SAMPLES_AVAIL ) = 0;
237
 
238
        if ( GPIOPinRead( GPIO_PORTC_BASE, PUSH_BUTTON ) != 0 )
239
        {
240
            StrPrintf( buf, sizeof( buf ), "%c ADC-Test 2", spinner[ spinIdx ]);
241
 
242
            LCD_MoveTo( 0, 0 );
243
            LCD_PutStr( buf );
244
 
245
            PutStr( "\r" );
246
            PutStr(  buf );
247
 
248
            StrPrintf( buf, sizeof( buf ), "%3lx %3lx %3lx %3lx", gADC[0], gADC[1], gADC[2], gADC[3] );
249
 
250
            PutStr( " " );
251
            PutStr( buf );
252
 
253
            LCD_MoveTo( 0, 1 );
254
            LCD_PutStr( buf );
255
        }
256
 
257
        ledCounter++;
258
 
259
        if (( ledCounter > 0  ) && ( ledCounter <= 16 ))
260
        {
261
            TimerMatchSet( TIMER0_BASE, TIMER_B,
262
                           ((ledCounter * SysCtlClockGet()) / 160000) - 2);
263
        }
264
        if (( ledCounter > 16  ) && ( ledCounter <= 32 ))
265
        {
266
            TimerMatchSet( TIMER0_BASE, TIMER_B,
267
                           ((( 32 - ledCounter ) * SysCtlClockGet()) / 160000) - 2);
268
        }
269
        ledCounter &= 31;
270
 
271
        spinIdx = ( spinIdx + 1 ) % 4;
272
    }
273
 
274
} // main
275