Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 10 → Rev 11

/luminary/PWM-Test/PWM-Test.c
0,0 → 1,325
/****************************************************************************
*
* Copyright (c) 2006 Dave Hylands <dhylands@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*
****************************************************************************
*
* Test program for monitoring the ADC lines.
*
****************************************************************************/
 
/* ---- Include Files ---------------------------------------------------- */
 
#include <stdio.h>
#include <stdint.h>
 
 
#include "hw_types.h"
#include "hw_ints.h"
#include "hw_memmap.h"
 
#include "adc.h"
#include "gpio.h"
#include "interrupt.h"
#include "pwm.h"
#include "sysctl.h"
#include "timer.h"
#include "uart.h"
 
#include "lcd.h"
#include "Str.h"
/* ---- Public Variables ------------------------------------------------- */
/* ---- Private Constants and Types -------------------------------------- */
 
#define PUSH_BUTTON GPIO_PIN_4
#define USER_LED GPIO_PIN_5
 
/* ---- Private Variables ------------------------------------------------ */
 
unsigned long gADC[ 4 ];
 
unsigned long gFlags;
 
#define FLAG_ADC_SAMPLES_AVAIL 0
 
uint32_t gMotorPeriod;
 
/* ---- Private Function Prototypes -------------------------------------- */
 
void PutStr( const char *str );
void ADC_isr( void );
void InitADC( void );
void InitMotors( void );
 
/* ---- Functions -------------------------------------------------------- */
 
/****************************************************************************
*
* PutStr
*
****************************************************************************/
 
void PutStr( const char *str )
{
while ( *str != '\0' )
{
if ( *str == '\n' )
{
UARTCharPut( UART0_BASE, '\r' );
}
UARTCharPut( UART0_BASE, *str );
 
str++;
}
}
 
/****************************************************************************
*
* Interrupt handler which fires when a sequence has been collected.
*
****************************************************************************/
 
void ADC_isr( void )
{
// Clear the interrupt
 
ADCIntClear( ADC_BASE, 0 );
 
// Grab the data from the ADC
 
ADCSequenceDataGet( ADC_BASE, 0, &gADC[ 0 ]);
 
HWREGBITW( &gFlags, FLAG_ADC_SAMPLES_AVAIL ) = 1;
 
} // ADC_isr
 
/****************************************************************************
*
* Initialize the ADC to collect sample from our 4 line sensors.
*
****************************************************************************/
 
void InitADC( void )
{
// Enable the ADC peripheral
 
SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );
 
// Setup the sequence
 
//
// Configure the ADC to sample the potentiometer when the timer expires.
// After sampling, the ADC will interrupt the processor; this is used as
// the heartbeat for the game.
//
ADCSequenceConfigure( ADC_BASE, // base
0, // sequence num
ADC_TRIGGER_TIMER, // trigger
0 ); // priority
 
ADCSequenceStepConfigure( ADC_BASE, // base
0, // sequence num
0, // step num
ADC_CTL_CH0 ); // config
 
ADCSequenceStepConfigure( ADC_BASE, // base
0, // sequence num
1, // step num
ADC_CTL_CH1 ); // config
 
ADCSequenceStepConfigure( ADC_BASE, // base
0, // sequence num
2, // step num
ADC_CTL_CH2 ); // config
 
ADCSequenceStepConfigure( ADC_BASE, // base
0, // sequence num
3, // step num
ADC_CTL_CH3 | ADC_CTL_IE | ADC_CTL_END ); //config
 
ADCSequenceEnable( ADC_BASE, 0 );
 
ADCIntEnable( ADC_BASE, 0);
IntEnable(INT_ADC0);
 
} // InitADC
 
/****************************************************************************
*
* Initialize the PWM channels for driving a pair of motors
*
****************************************************************************/
 
void InitMotors( void )
{
// Enable the peripherals that we need
 
// PWM 0/1 is used for the left motor, and PWM 2/3 is used for the righr
// motor.
//
// PWM 0/1 is found on Port D 0/1
// PWM 2/3 is found on Port B 0/1
 
SysCtlPeripheralEnable( SYSCTL_PERIPH_PWM );
SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOD );
SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOB );
 
// Configure PWM 0/1 pins as output
 
GPIOPinTypePWM( GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1 );
 
// Configure PWM 2/3 pins as output
 
GPIOPinTypePWM( GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1 );
 
// Set the PWM period.
 
gMotorPeriod = SysCtlClockGet() / 4096; // 4 kHz
 
PWMGenConfigure( PWM_BASE, PWM_GEN_0, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC );
PWMGenConfigure( PWM_BASE, PWM_GEN_1, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC );
 
PWMGenPeriodSet( PWM_BASE, PWM_GEN_0, gMotorPeriod );
PWMGenPeriodSet( PWM_BASE, PWM_GEN_1, gMotorPeriod );
 
PWMPulseWidthSet( PWM_BASE, PWM_OUT_0, 0 );
PWMPulseWidthSet( PWM_BASE, PWM_OUT_1, 0 );
 
PWMPulseWidthSet( PWM_BASE, PWM_OUT_2, 0 );
PWMPulseWidthSet( PWM_BASE, PWM_OUT_3, 0 );
 
// Enable the PWM output signals
 
PWMOutputState( PWM_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT | PWM_OUT_2_BIT | PWM_OUT_3_BIT, true );
 
// Enable the PWM generators
 
PWMGenEnable( PWM_BASE, PWM_GEN_0 );
PWMGenEnable( PWM_BASE, PWM_GEN_1 );
 
} // InitMotor
 
/****************************************************************************
*
* Main loop for the test program
*
****************************************************************************/
 
int main( void )
{
int spinIdx = 0;
const char *spinner = "-\\|/";
char buf[ 20 ];
 
// Set the clocking to run at 20MHz from the PLL.
 
SysCtlClockSet( SYSCTL_SYSDIV_10
| SYSCTL_USE_PLL
| SYSCTL_OSC_MAIN
| SYSCTL_XTAL_6MHZ );
 
 
LCD_Init( 2, 16 );
LCD_PutStr( "PWM-Test" );
 
InitADC();
InitMotors();
 
// Configure Timer0 to generate a 10 kHz PWM signal for driving
// the user LED.
 
SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER0 );
TimerConfigure( TIMER0_BASE, TIMER_CFG_16_BIT_PAIR | TIMER_CFG_B_PWM );
TimerLoadSet(TIMER0_BASE, TIMER_B, (SysCtlClockGet() / 10000) - 1 );
TimerMatchSet( TIMER0_BASE, TIMER_B, 0 );
TimerControlLevel( TIMER0_BASE, TIMER_B, true );
TimerEnable( TIMER0_BASE, TIMER_B );
 
// Confgure Timer 1 to generate triggers for the ADC
 
SysCtlPeripheralEnable( SYSCTL_PERIPH_TIMER1 );
 
HWREG( ADC_BASE + 0x30 ) = 6;
 
TimerConfigure( TIMER1_BASE, TIMER_CFG_32_BIT_PER );
TimerLoadSet( TIMER1_BASE, TIMER_A, SysCtlClockGet() / 20 );
TimerControlStall( TIMER1_BASE, TIMER_A, true );
TimerControlTrigger( TIMER1_BASE, TIMER_A, true );
TimerEnable( TIMER1_BASE, TIMER_A );
 
SysCtlPeripheralEnable( SYSCTL_PERIPH_GPIOA );
SysCtlPeripheralEnable( SYSCTL_PERIPH_UART0 );
 
// Configure the first UART for 115,200, 8-N-1 operation.
 
GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
 
UARTConfigSet( UART0_BASE,
115200,
( UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE ));
UARTEnable( UART0_BASE );
 
// Setup the User LED & pushbutton
 
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
GPIODirModeSet( GPIO_PORTC_BASE, USER_LED, GPIO_DIR_MODE_OUT );
GPIOPinWrite( GPIO_PORTC_BASE, USER_LED, 0 );
 
 
PutStr( "*****\n" );
PutStr( "***** PWM-Test program\n" );
PutStr( "*****\n" );
 
TimerMatchSet( TIMER0_BASE, TIMER_B, 0 );
GPIODirModeSet( GPIO_PORTC_BASE, USER_LED, GPIO_DIR_MODE_HW );
 
while ( 1 )
{
uint32_t pulseWidth;
uint32_t pulsePct;
 
while ( HWREGBITW( &gFlags, FLAG_ADC_SAMPLES_AVAIL ) == 0 )
{
;
}
HWREGBITW( &gFlags, FLAG_ADC_SAMPLES_AVAIL ) = 0;
 
pulseWidth = gADC[ 0 ] * gMotorPeriod / 1023;
pulsePct = gADC[ 0 ] * 100 / 1023;
 
StrPrintf( buf, sizeof( buf ), "%c PWM-Test", spinner[ spinIdx ]);
 
LCD_MoveTo( 0, 0 );
LCD_PutStr( buf );
 
PutStr( "\r" );
PutStr( buf );
 
StrPrintf( buf, sizeof( buf ), "%3lx %3d%%", gADC[0], pulsePct );
 
PutStr( " " );
PutStr( buf );
 
LCD_MoveTo( 0, 1 );
LCD_PutStr( buf );
 
PWMPulseWidthSet( PWM_BASE, PWM_OUT_0, pulseWidth );
PWMPulseWidthSet( PWM_BASE, PWM_OUT_1, pulseWidth );
PWMPulseWidthSet( PWM_BASE, PWM_OUT_2, pulseWidth );
PWMPulseWidthSet( PWM_BASE, PWM_OUT_3, pulseWidth );
 
spinIdx = ( spinIdx + 1 ) % 4;
}
 
} // main
 
Property changes:
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: PWM-Test/startup-gcc.c
===================================================================
--- PWM-Test/startup-gcc.c (nonexistent)
+++ PWM-Test/startup-gcc.c (revision 11)
@@ -0,0 +1,238 @@
+//*****************************************************************************
+//
+// startup.c - Boot code for Stellaris.
+//
+// Copyright (c) 2005,2006 Luminary Micro, Inc. All rights reserved.
+//
+// Software License Agreement
+//
+// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
+// exclusively on LMI's Stellaris Family of microcontroller products.
+//
+// The software is owned by LMI and/or its suppliers, and is protected under
+// applicable copyright laws. All rights are reserved. Any use in violation
+// of the foregoing restrictions may subject the user to criminal sanctions
+// under applicable laws, as well as to civil liability for the breach of the
+// terms and conditions of this license.
+//
+// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
+// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
+// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
+// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
+//
+// This is part of revision 852 of the Stellaris Driver Library.
+//
+//*****************************************************************************
+
+// modified by Martin Thomas - don't blame Luminary if it does
+// not work for you.
+
+//*****************************************************************************
+//
+// Forward declaration of the default fault handlers.
+//
+//*****************************************************************************
+// mthomas: attribute for stack-aligment (see README_mthomas.txt)
+#ifdef __GNUC__
+void ResetISR(void) __attribute__((__interrupt__));
+static void NmiSR(void) __attribute__((__interrupt__));
+static void FaultISR(void) __attribute__((__interrupt__));
+static void IntDefaultHandler(void) __attribute__((__interrupt__));
+#else
+void ResetISR(void);
+static void NmiSR(void);
+static void FaultISR(void);
+static void IntDefaultHandler(void);
+#endif
+
+//*****************************************************************************
+//
+// The entry point for the application.
+//
+//*****************************************************************************
+extern int main(void);
+
+//*****************************************************************************
+//
+// External declaration for the interrupt handler used by the application.
+//
+//*****************************************************************************
+extern void ADC_isr( void );
+
+//*****************************************************************************
+//
+// Reserve space for the system stack.
+//
+//*****************************************************************************
+#ifndef STACK_SIZE
+#define STACK_SIZE 64
+#endif
+
+// mthomas: added section -> alignment thru linker-script
+__attribute__ ((section(".stackarea")))
+static unsigned long pulStack[STACK_SIZE];
+
+//*****************************************************************************
+//
+// The minimal vector table for a Cortex M3. Note that the proper constructs
+// must be placed on this to ensure that it ends up at physical address
+// 0x0000.0000.
+//
+//*****************************************************************************
+__attribute__ ((section(".isr_vector")))
+void (* const g_pfnVectors[])(void) =
+{
+ (void (*)(void))((unsigned long)pulStack + sizeof(pulStack)),
+ // The initial stack pointer
+ ResetISR, // The reset handler
+ NmiSR, // The NMI handler
+ FaultISR, // The hard fault handler
+ IntDefaultHandler, // The MPU fault handler
+ IntDefaultHandler, // The bus fault handler
+ IntDefaultHandler, // The usage fault handler
+ 0, // Reserved
+ 0, // Reserved
+ 0, // Reserved
+ 0, // Reserved
+ IntDefaultHandler, // SVCall handler
+ IntDefaultHandler, // Debug monitor handler
+ 0, // Reserved
+ IntDefaultHandler, // The PendSV handler
+ IntDefaultHandler, // The SysTick handler
+ IntDefaultHandler, // GPIO Port A
+ IntDefaultHandler, // GPIO Port B
+ IntDefaultHandler, // GPIO Port C
+ IntDefaultHandler, // GPIO Port D
+ IntDefaultHandler, // GPIO Port E
+ IntDefaultHandler, // UART0 Rx and Tx
+ IntDefaultHandler, // UART1 Rx and Tx
+ IntDefaultHandler, // SSI Rx and Tx
+ IntDefaultHandler, // I2C Master and Slave
+ IntDefaultHandler, // PWM Fault
+ IntDefaultHandler, // PWM Generator 0
+ IntDefaultHandler, // PWM Generator 1
+ IntDefaultHandler, // PWM Generator 2
+ IntDefaultHandler, // Quadrature Encoder
+ ADC_isr, // ADC Sequence 0
+ IntDefaultHandler, // ADC Sequence 1
+ IntDefaultHandler, // ADC Sequence 2
+ IntDefaultHandler, // ADC Sequence 3
+ IntDefaultHandler, // Watchdog timer
+ IntDefaultHandler, // Timer 0 subtimer A
+ IntDefaultHandler, // Timer 0 subtimer B
+ IntDefaultHandler, // Timer 1 subtimer A
+ IntDefaultHandler, // Timer 1 subtimer B
+ IntDefaultHandler, // Timer 2 subtimer A
+ IntDefaultHandler, // Timer 2 subtimer B
+ IntDefaultHandler, // Analog Comparator 0
+ IntDefaultHandler, // Analog Comparator 1
+ IntDefaultHandler, // Analog Comparator 2
+ IntDefaultHandler, // System Control (PLL, OSC, BO)
+ IntDefaultHandler // FLASH Control
+};
+
+//*****************************************************************************
+//
+// The following are constructs created by the linker, indicating where the
+// the "data" and "bss" segments reside in memory. The initializers for the
+// for the "data" segment resides immediately following the "text" segment.
+//
+//*****************************************************************************
+extern unsigned long _etext;
+extern unsigned long _data;
+extern unsigned long _edata;
+extern unsigned long _bss;
+extern unsigned long _ebss;
+
+//*****************************************************************************
+//
+// This is the code that gets called when the processor first starts execution
+// following a reset event. Only the absolutely necessary set is performed,
+// after which the application supplied main() routine is called. Any fancy
+// actions (such as making decisions based on the reset cause register, and
+// resetting the bits in that register) are left solely in the hands of the
+// application.
+//
+//*****************************************************************************
+void
+ResetISR(void)
+{
+ unsigned long *pulSrc, *pulDest;
+
+ //
+ // Copy the data segment initializers from flash to SRAM.
+ //
+ pulSrc = &_etext;
+ for(pulDest = &_data; pulDest < &_edata; )
+ {
+ *pulDest++ = *pulSrc++;
+ }
+
+ //
+ // Zero fill the bss segment.
+ //
+ for(pulDest = &_bss; pulDest < &_ebss; )
+ {
+ *pulDest++ = 0;
+ }
+
+ //
+ // Call the application's entry point.
+ //
+ main();
+}
+
+//*****************************************************************************
+//
+// This is the code that gets called when the processor receives a NMI. This
+// simply enters an infinite loop, preserving the system state for examination
+// by a debugger.
+//
+//*****************************************************************************
+static void
+NmiSR(void)
+{
+ //
+ // Enter an infinite loop.
+ //
+ while(1)
+ {
+ }
+}
+
+//*****************************************************************************
+//
+// This is the code that gets called when the processor receives a fault
+// interrupt. This simply enters an infinite loop, preserving the system state
+// for examination by a debugger.
+//
+//*****************************************************************************
+static void
+FaultISR(void)
+{
+ //
+ // Enter an infinite loop.
+ //
+ while(1)
+ {
+ }
+}
+
+//*****************************************************************************
+//
+// This is the code that gets called when the processor receives an unexpected
+// interrupt. This simply enters an infinite loop, preserving the system state
+// for examination by a debugger.
+//
+//*****************************************************************************
+static void
+IntDefaultHandler(void)
+{
+ //
+ // Go into an infinite loop.
+ //
+ while(1)
+ {
+ }
+}
Index: PWM-Test/Makefile
===================================================================
--- PWM-Test/Makefile (nonexistent)
+++ PWM-Test/Makefile (revision 11)
@@ -0,0 +1,19 @@
+###########################################################################
+#
+# Makefile to build crc32
+#
+###########################################################################
+
+MK_OS = cortex-m3
+MK_ELF_TARGET = PWM-Test
+
+MK_SRC_FILES = PWM-Test.c StrPrintf.c lcd-printf.c lcd-osram96x16. startup-gcc.c osram96x16.c
+
+LM3S811_DIR = $(MK_ROOT)/WinARM/lm3s811_evalboard
+
+CPPFLAGS += -I $(LM3S811_DIR) -I $(LM3S811_DIR)/src -I$(LM3S811_DIR)/ev-lm3s811
+LDFLAGS += -L $(LM3S811_DIR)
+LOADLIBES += -lluminary
+
+include ../../rules/mkRules.mk
+