hackwaw-fw/uart_echo.c

367 lines
10 KiB
C

//*****************************************************************************
//
// uart_echo.c - Example for reading data from and writing data to the UART in
// an interrupt driven fashion.
//
// Copyright (c) 2012 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// 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. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 9453 of the EK-LM4F120XL Firmware Package.
//
//*****************************************************************************
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/timer.h"
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>UART Echo (uart_echo)</h1>
//!
//! This example application utilizes the UART to echo text. The first UART
//! (connected to the USB debug virtual serial port on the evaluation board)
//! will be configured in 115,200 baud, 8-n-1 mode. All characters received on
//! the UART are transmitted back to the UART.
//
//*****************************************************************************
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
//
// The UART interrupt handler.
//
//*****************************************************************************
//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************
void
UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
//
// Loop while there are more characters to send.
//
while(ulCount--)
{
//
// Write the next character to the UART.
//
ROM_UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++);
}
}
#define DSET_SERVO1 'a'
#define DSET_SERVO2 'b'
#define DSET_DA 'c'
#define DEST_DB 'd'
#define DSET_DC 'e'
typedef unsigned char u8;
typedef char s8;
typedef unsigned int u32;
volatile u32 g_FiredTime = 0;
struct {
struct {
u32 Servo1 : 1,
Servo2 : 1;
struct {
u32 A : 1,
B : 1,
C : 1;
} Digital;
} NextReceive;
u8 Servo1;
u8 Servo2;
struct {
u32 A : 1,
B : 1,
C : 1;
} Digital;
} g_OutputControl;
void CommitState(void)
{
// F4 = fire
//GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, g_OutputControl.Digital.A ? GPIO_PIN_4 : 0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, g_OutputControl.Digital.A ? GPIO_PIN_2 : 0);
}
void ClearNext(void)
{
g_OutputControl.NextReceive.Servo1 = 0;
g_OutputControl.NextReceive.Servo2 = 0;
g_OutputControl.NextReceive.Digital.A = 0;
g_OutputControl.NextReceive.Digital.B = 0;
g_OutputControl.NextReceive.Digital.C = 0;
}
volatile u32 Timer0Counter = 0;
volatile u32 MiliCounter = 0;
u32 MiliSubCounter = 0;
void Timer0Handler(void)
{
ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
MiliSubCounter++;
if (MiliSubCounter >= 100)
{
MiliSubCounter = 0;
MiliCounter++;
}
Timer0Counter++;
if (Timer0Counter >= 2000)
Timer0Counter = 0;
if (Timer0Counter == 0)
{
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2, GPIO_PIN_2);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, GPIO_PIN_3);
}
if (Timer0Counter == 50 + g_OutputControl.Servo1)
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_2, 0);
if (Timer0Counter == 50 + g_OutputControl.Servo2)
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, 0);
if (g_OutputControl.Digital.A)
{
if (MiliCounter > (g_FiredTime + 200))
{
g_OutputControl.Digital.A = 0;
CommitState();
}
}
}
void
UARTIntHandler(void)
{
unsigned long ulStatus;
//
// Get the interrrupt status.
//
ulStatus = ROM_UARTIntStatus(UART0_BASE, true);
//
// Clear the asserted interrupts.
//
ROM_UARTIntClear(UART0_BASE, ulStatus);
//
// Loop while there are characters in the receive FIFO.
//
while(ROM_UARTCharsAvail(UART0_BASE))
{
//
// Read the next character from the UART and write it back to the UART.
//
u8 Data = ROM_UARTCharGetNonBlocking(UART0_BASE);
u8 Sent = 0;
if (g_OutputControl.NextReceive.Digital.A)
{
if (Data > 0)
{
UARTSend("start ", 6);
g_FiredTime = MiliCounter;
g_OutputControl.Digital.A = 1;
}
Sent = 1;
}
else if (g_OutputControl.NextReceive.Servo1)
{
UARTSend("s1 ", 3);
g_OutputControl.Servo1 = (Data * 200) / 256;
Sent = 1;
}
else if (g_OutputControl.NextReceive.Servo2)
{
UARTSend("s2, ", 4);
g_OutputControl.Servo2 = (Data * 200) / 256;
//UARTSend("b", 1);
Sent = 1;
}
ClearNext();
if (!Sent)
{
if (Data == 'a')
{
g_OutputControl.NextReceive.Servo1 = 1;
//UARTSend("A", 1);
}
else if (Data == 'b')
{
g_OutputControl.NextReceive.Servo2 = 1;
//UARTSend("B", 1);
}
else if (Data == 'c')
{
g_OutputControl.NextReceive.Digital.A = 1;
}
// else if (Data == 'r')
// {
// UARTSend("R", 1);;
// }
}
CommitState();
//
// Blink the LED to show a character transfer is occuring.
//
//GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, GPIO_PIN_4);
//
// Delay for 1 millisecond. Each SysCtlDelay is about 3 clocks.
//
//SysCtlDelay(SysCtlClockGet() / (1000 * 3));
//
// Turn off the LED
//
//GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
}
}
//*****************************************************************************
//
// This example demonstrates how to send a string of data to the UART.
//
//*****************************************************************************
int
main(void)
{
// initialize crap
g_OutputControl.Servo1 = 0;
g_OutputControl.Servo2 = 0;
g_OutputControl.Digital.A = 0;
g_OutputControl.Digital.B = 0;
g_OutputControl.Digital.C = 0;
g_OutputControl.NextReceive.Servo1 = 0;
g_OutputControl.NextReceive.Servo2 = 0;
g_OutputControl.NextReceive.Digital.A = 0;
g_OutputControl.NextReceive.Digital.B = 0;
g_OutputControl.NextReceive.Digital.C = 0;
//
// Enable lazy stacking for interrupt handlers. This allows floating-point
// instructions to be used within interrupt handlers, but at the expense of
// extra stack usage.
//
ROM_FPUEnable();
ROM_FPULazyStackingEnable();
//
// Set the clocking to run directly from the crystal.
//
ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
//
// Enable the GPIO port that is used for the on-board LED.
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable the GPIO pins for the LED (PF2).
//
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);
// digital
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4);
// servos (PA2, PA3)
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_2);
ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_3);
//
// Enable the peripherals used by this example.
//
// Enable processor interrupts.
//
ROM_IntMasterEnable();
//
// Set GPIO A0 and A1 as UART pins.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Configure the UART for 115,200, 8-N-1 operation.
//
ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));
ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet() / 100000);
//
// Enable the UART interrupt.
//
ROM_IntEnable(INT_UART0);
ROM_IntEnable(INT_TIMER0A);
ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
ROM_TimerEnable(TIMER0_BASE, TIMER_A);
//
// Loop forever echoing data through the UART.
//
while(1)
{
}
}