Você está na página 1de 18

HovercraftSM.

6/1/16, 5:53 PM

/****************************************************************************
Module
HovercraftSM.c
Revision
1.0.1
Description
This is the first service for the Test Harness under the
Gen2 Events and Services Framework.
Notes
History
When
Who
What/Why
-------------- ---------5/5/2016
Derrick Starting
5/5/2016
Matty
Adding interrupt response for data line
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"
#include "HovercraftSM.h"
#include "PoundDefines.h"
#include "PWMService.h"
#include "CommunicateSM.h"
#include "LiftMotorSM.h"
#include "DirectionMotorSM.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

"inc/hw_memmap.h"
"inc/hw_types.h"
"inc/hw_gpio.h"
"inc/hw_sysctl.h"
"driverlib/sysctl.h"
"driverlib/pin_map.h"
"driverlib/gpio.h"
"inc/hw_uart.h"
"inc/hw_nvic.h"
"termio.h"
"ADMulti.h"

/*----------------------------- Module Defines ----------------------------*/


#define ONE_SEC 976
// these times assume a 1.000mS/tick
timing
#define HALF_SEC (ONE_SEC/2)
#define TWO_SEC (ONE_SEC*2)
#define FIVE_SEC (ONE_SEC*5)
/*---------------------------- Module Functions ---------------------------*/
static void InitUART(void);
static void InitMotors(void);
static void TXStatusByte(void);
static uint8_t CheckSumCalc(uint8_t Array[]);
static uint8_t GetHovercraftBadge(void);
static void DecryptData(uint8_t Array[]);
Page 1 of 18

HovercraftSM.c

6/1/16, 5:53 PM

static void SaveEncryptionKey(uint8_t Array[]);


static uint8_t CtrlCheckSum(uint8_t Array[]);
/*---------------------------- Module Variables ---------------------------*/
static uint8_t MyPriority;
static HovercraftState_t CurrentState;
static uint8_t TXData[11];
static uint8_t Color;
static uint8_t PairedStatus;
static uint8_t PICByte;
static uint8_t OnTime[5] = {0,15,9,5,3};
static uint8_t PicTimeCounter;
static uint8_t PairingTimeState = 0;
static uint8_t PairedByte = 0x00;
static uint32_t results[HOW_MANY];
static uint8_t DestMSB;
static uint8_t DestLSB;
static uint8_t LastMSB = 0;
static uint8_t LastLSB = 0;
static uint8_t EncryptedCheckSum;
static uint8_t EncryptKey[32];
static uint8_t NewDataArray[42];
static uint8_t CtrlData[5];
static uint8_t EncryptCtr;
static uint8_t ControllerBadge;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitializeHovercraftSM
Parameters
uint8_t : the priorty of this service
Returns
bool, false if error in initialization, true otherwise
Description
Saves away the priority, and does any
other required initialization for this service
Notes
Author
D. Contreras, 5/5/16
****************************************************************************/
bool InitializeHovercraftSM ( uint8_t Priority )
{
MyPriority = Priority;
//Init Uart
InitUART();
//Init ports for motors
InitMotors();
//Init PWM
InitAllPWM();
//init analog
ADC_MultiInit(HOW_MANY);
//Current State is Unpaired
CurrentState = Unpaired;
Page 2 of 18

HovercraftSM.c

6/1/16, 5:53 PM

ES_Event ThisEvent;
ThisEvent.EventType = ES_INIT;
if (PostHovercraftSM(ThisEvent) == true) {
return true;
} else {
return false;
}
}
/****************************************************************************
Function
PostHovercraftSM
Parameters
EF_Event ThisEvent ,the event to post to the queue
Returns
bool false if the Enqueue operation failed, true otherwise
Description
Posts an event to this state machine's queue
Notes
Author
D. Contreras 5/5/16
****************************************************************************/
bool PostHovercraftSM( ES_Event ThisEvent )
{
return ES_PostToService( MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunHovercraftSM
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
Notes
Author
D. Contreras 5/5/16
Anne & Derrick - PIC timers - 5/13/16
Anne added encryption - 5/18/16
****************************************************************************/
ES_Event RunHovercraftSM( ES_Event ThisEvent )
{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors

Page 3 of 18

HovercraftSM.c

6/1/16, 5:53 PM

// Switch CurrentHovercraftState
switch (CurrentState) {
// Case UnPaired
case Unpaired:
//if new data event
if (ThisEvent.EventType == NEW_DATA) {
//get new data
uint8_t* NewDataArray = GetNewData();
//read value of badge (0x00 thru 0x03)
ControllerBadge = (NewDataArray[6] & (BIT0HI|BIT1HI));
//check if pairing request and addressed to my correct port
number in byte 9
if ((NewDataArray[5] == REQ_PAIR_HEADER) && (ControllerBadge ==
GetHovercraftBadge()) && (NewDataArray[1] != DestMSB) &&
(NewDataArray[2] != DestLSB))
{
//save source address
DestMSB = NewDataArray[1];
DestLSB = NewDataArray[2];
//figure out what color we are - mask req pair byte
if ((NewDataArray[6] & BIT7HI) == BIT7HI)
{
Color = BLUE;
}
else
{
Color = RED;
}
//set paired status hi (for PIC byte)
PairedStatus = PAIRED;
//set color and pair bits on PIC byte
PICByte = (PairedStatus | Color);
//check TX flag in masked interrupt for PIC
if ((HWREG(UART3_BASE + UART_O_FR) & UART_FR_TXFE) ==
UART_FR_TXFE)
{
printf("UART3 FREE\r\n");
//write data to PIC UART line
HWREG(UART3_BASE + UART_O_DR) = PICByte;
//start PIC 200ms Tx timer
ES_Timer_InitTimer(PIC_TX_TIMER, PIC_TX_TIME);
//start Pair Timer (45s)
ES_Timer_InitTimer(PAIR_TIME_TIMER, 9*ONE_SEC);
}
//load up data we want to send back to controller;
PairedByte = 0x01;
EncryptedCheckSum = 0x00;
TXStatusByte();
HWREG(UART1_BASE + UART_O_IM) = ~(UART_IM_RXIM); //disable rx
interrupt
HWREG(UART1_BASE + UART_O_DR) = 0x7E;
HWREG(UART1_BASE + UART_O_IM) |= UART_IM_TXIM;

Page 4 of 18

HovercraftSM.c

6/1/16, 5:53 PM

//start Communications Timer (1 sec)


ES_Timer_InitTimer(COMMUNICATIONS_TIMER, COMMUNICATIONS_TIME)
;
//send event to Motors with Lift Off event
ES_Event LiftEvent;
LiftEvent.EventType = LIFT_EVENT;
PostLiftMotorSM(LiftEvent);
ES_Event MotorEvent;
MotorEvent.EventType = LIFT_EVENT;
PostDirectionMotorSM(MotorEvent);
//move to wait4encryption state
CurrentState = PairedWait4Encryption;
}
}
break;
// Case PairedWait4Encryption
case PairedWait4Encryption:
if (ThisEvent.EventType == NEW_DATA)
{
uint8_t* NewDataArray = GetNewData();
//if data is from paired source
if((NewDataArray[1] == DestMSB) && (NewDataArray[2] == DestLSB))
{
//save encryption key locally
SaveEncryptionKey(NewDataArray);
//set encryption ctr to 0
EncryptCtr = 0;
//load up data we want to send back to PAC;
PairedByte = 0x01;
EncryptedCheckSum = 0x00;
TXStatusByte();
HWREG(UART1_BASE + UART_O_IM) = ~(UART_IM_RXIM); //disable rx
interrupt
HWREG(UART1_BASE + UART_O_DR) = 0x7E;
HWREG(UART1_BASE + UART_O_IM) |= UART_IM_TXIM;
//start Communications Timer (1 sec)
ES_Timer_InitTimer(COMMUNICATIONS_TIMER, COMMUNICATIONS_TIME)
;
//set state to PairedWait4Ctrl
CurrentState = PairedWait4Ctrl;
}
}
else if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam
== COMMUNICATIONS_TIMER))
{
//turn off lift fan and propulsion fans
ES_Event TurnOffEvent;
TurnOffEvent.EventType = TURN_LIFT_OFF;
PostDirectionMotorSM(TurnOffEvent);
PostLiftMotorSM(TurnOffEvent);

Page 5 of 18

HovercraftSM.c

6/1/16, 5:53 PM

//indicate available for pairing on DMC


PairingTimeState = 0;
// send 0 for Pair state for PIC (available for pairing)
PICByte = 0;
//write data to PIC UART line
HWREG(UART3_BASE + UART_O_DR) = PICByte;
//disable 45 second pair time timer
ES_Timer_StopTimer(PAIR_TIME_TIMER);
//1 second transmit timer timeout - haven't heard from controller
within 1 second
CurrentState = Unpaired;
}
break;
// Case Paired
case PairedWait4Ctrl:
//new data event
if (ThisEvent.EventType == NEW_DATA)
{
//save new data
uint8_t* NewDataArray = GetNewData();
//set encrypted check sum variable, which will transmit later
EncryptedCheckSum = NewDataArray[9];
//if msg is from paired controller
if((NewDataArray[1] == DestMSB) && (NewDataArray[2] == DestLSB) &
& (GetLength() == 10))
{
//decrypt data with encryption key
DecryptData(NewDataArray);
//if decrypted check sum matches calculated check sum and
unpair bit not set
if(CtrlData[4] == CtrlCheckSum(CtrlData) && ((CtrlData[3] &
BIT1HI) != BIT1HI) && ((CtrlData[3] & BIT0HI) != BIT0HI))
{
//post lift event to lift sm
ES_Event LiftEvent;
LiftEvent.EventType = LIFT_EVENT;
PostLiftMotorSM(LiftEvent);
if((DestMSB == 0x21) && (DestLSB == 0x86))
{
//post event to direction motor indicating we are
paired with our controller
ES_Event MotorEvent;
MotorEvent.EventType = OUR_BOT;
PostDirectionMotorSM(MotorEvent);
}
else
{
//post direction event to direction sm
ES_Event MotorEvent;
MotorEvent.EventType = NEW_DATA;
PostDirectionMotorSM(MotorEvent);
}

Page 6 of 18

HovercraftSM.c

6/1/16, 5:53 PM

//start communications timer - 1 second timer


ES_Timer_InitTimer(COMMUNICATIONS_TIMER,
COMMUNICATIONS_TIME);
//send back byte with paired bit set and encrypted check
sum set (set above ^^)
PairedByte = 0x01;
}
//else if brake event
else if ((CtrlData[3] & BIT0HI) == BIT0HI)
{
ES_Event LiftEvent;
LiftEvent.EventType = TURN_LIFT_OFF;
PostLiftMotorSM(LiftEvent);
}
//else unpair
else
{
//turn off lift fan and propulsion fans
ES_Event TurnOffEvent;
TurnOffEvent.EventType = TURN_LIFT_OFF;
PostDirectionMotorSM(TurnOffEvent);
PostLiftMotorSM(TurnOffEvent);
//indicate available for pairing on DMC
PairingTimeState = 0;
// send 0 for Pair state for PIC (available for pairing)
PICByte = 0;
//write data to PIC UART line
HWREG(UART3_BASE + UART_O_DR) = PICByte;
//disable 1s communications timer
ES_Timer_StopTimer(COMMUNICATIONS_TIMER);
//disable 45 second pair time timer
ES_Timer_StopTimer(PAIR_TIME_TIMER);
//decryption error
if(CtrlData[4] != CtrlCheckSum(CtrlData)){
//set decryption error bit hi and pair bit low
PairedByte = 0x02;
}
//controller manual unpair set
else{
PairedByte = 0x00;
}
//set state to unpaired
CurrentState = Unpaired;
}
//transmit status byte
TXStatusByte();
HWREG(UART1_BASE + UART_O_IM) = ~(UART_IM_RXIM); //disable rx
interrupt
HWREG(UART1_BASE + UART_O_DR) = 0x7E;
HWREG(UART1_BASE + UART_O_IM) |= UART_IM_TXIM;
Page 7 of 18

HovercraftSM.c

6/1/16, 5:53 PM

//start Communications Timer (1 sec)


ES_Timer_InitTimer(COMMUNICATIONS_TIMER, COMMUNICATIONS_TIME)
;
}
}
else if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam
== COMMUNICATIONS_TIMER))
{
//turn off lift fan and propulsion fans
ES_Event TurnOffEvent;
TurnOffEvent.EventType = TURN_LIFT_OFF;
PostDirectionMotorSM(TurnOffEvent);
PostLiftMotorSM(TurnOffEvent);
//indicate available for pairing on DMC
PairingTimeState = 0;
// send 0 for Pair state for PIC (available for pairing)
PICByte = 0;
//write data to PIC UART line
HWREG(UART3_BASE + UART_O_DR) = PICByte;
//disable 45 second pair time timer
ES_Timer_StopTimer(PAIR_TIME_TIMER);
//1 second transmit timer timeout - haven't heard from PAC within
1 second
CurrentState = Unpaired;
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam
== PAIR_TIME_TIMER))
{
//reset pic time counter
PicTimeCounter = 0;
//if pairingtimestate less than 4, continue to stay paired
if (PairingTimeState < 4)
{
//increment pairing time state
PairingTimeState++;
// Init Pair Time Timer for 9 seconds (when you incremen
flashing light counter)
ES_Timer_InitTimer(PAIR_TIME_TIMER,9*ONE_SEC);
}
//else unpair with controller
else
{
//set pairing time state back to zero
PairingTimeState = 0;
// send 0 for Pair state for PIC (available for pairing)
PICByte = 0;
//write data to PIC UART line
HWREG(UART3_BASE + UART_O_DR) = PICByte;
//turn off lift fan and propulsion fans
ES_Event TurnOffEvent;
TurnOffEvent.EventType = TURN_LIFT_OFF;
PostDirectionMotorSM(TurnOffEvent);
PostLiftMotorSM(TurnOffEvent);
//stop communicaitons timer
ES_Timer_StopTimer(COMMUNICATIONS_TIMER);
//tell controller we're unpairing
Page 8 of 18

HovercraftSM.c

6/1/16, 5:53 PM

PairedByte = 0x00; // set PairedByte appropriately


TXStatusByte();
HWREG(UART1_BASE + UART_O_IM) = ~(UART_IM_RXIM); //disable rx
interrupt
HWREG(UART1_BASE + UART_O_DR) = 0x7E;
HWREG(UART1_BASE + UART_O_IM) |= UART_IM_TXIM;
//unpair
CurrentState = Unpaired;
}
}
else if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam
== PIC_TX_TIMER) && (PairedStatus == PAIRED) )
{
if(PairingTimeState == 0)
{
//write LED on
if ((HWREG(UART3_BASE + UART_O_FR) & UART_FR_TXFE) ==
UART_FR_TXFE)
{
//load up PICByte for LED on
PICByte = (PairedStatus | Color);
//write data to PIC UART line for LED on
HWREG(UART3_BASE + UART_O_DR) = PICByte;
//printf("PState %d on\r\n",PairingTimeState);
}
}
else
{
if(PicTimeCounter < 2)
{
//reset PICByte -- LEDs off
PICByte = (PairedStatus);
if ((HWREG(UART3_BASE + UART_O_FR) & UART_FR_TXFE) ==
UART_FR_TXFE)
{
//write data to PIC UART line
HWREG(UART3_BASE + UART_O_DR) = PICByte;
}
PicTimeCounter++;
//printf("PState %d off\r\n",PairingTimeState);
}
else if (PicTimeCounter < OnTime[PairingTimeState])
{
//reset PICByte -- Red/Blue LED on
PICByte = (Color | PairedStatus);
if ((HWREG(UART3_BASE + UART_O_FR) & UART_FR_TXFE) ==
UART_FR_TXFE)
{
//write data to PIC UART line
HWREG(UART3_BASE + UART_O_DR) = PICByte;
}
//printf("PState %d on\r\n",PairingTimeState);
if (PicTimeCounter == (OnTime[PairingTimeState] - 1)) {
PicTimeCounter = 0;
} else {
Page 9 of 18

HovercraftSM.c

6/1/16, 5:53 PM

PicTimeCounter++;
}
}
}
//initialize pic_tx_timer
ES_Timer_InitTimer(PIC_TX_TIMER, PIC_TX_TIME);
}
break;
}
return ReturnEvent;
}
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
TXStatusByte
Parameters
Nothing
Returns
Nothing
Description
Sets up bytes to send back on a successful receive
Notes
Author
D. Contreras 5/9/16
Matty + Anne edited Baud rate 5/6/2016
****************************************************************************/
static void TXStatusByte(void)
{
TXData[0] = 0x00;
//MSB (length)
TXData[1] = 0x08;
//LSB (length)
TXData[2] = 0x01;
//API identifier
TXData[3] = 0x00;
//frame ID ----may need
to change this and/or inc/dec
TXData[4] = DestMSB;
//destination address MSB
TXData[5] = DestLSB;
//destination address LSB
TXData[6] = 0x00;
//options
TXData[7] = STAT_HEADER;
//data - header for status byte
TXData[8] = PairedByte;
//data - request pair byte CHANGE LATER FOR DECRYPTION ERROR
TXData[9] = EncryptedCheckSum;
//encrypted check sum
TXData[10] = CheckSumCalc(TXData);//check sum - call function to calc
checksum?
}
/****************************************************************************
Function
CheckSumCalc
Page 10 of 18

HovercraftSM.c

6/1/16, 5:53 PM

Parameters
void
Returns
void
Description
Calculates check sum
Author
Anne Alter, 5/7/16
****************************************************************************/
static uint8_t CheckSumCalc(uint8_t Array[])
{
uint8_t ArrayLength;
uint8_t AntiCheckSum = 0x00;
//0xff - chk sum
uint8_t CheckSum;
//set byte 2 from array as array length
ArrayLength = Array[1];
//loop thru bytes 3 to 3+arraylength
for(uint8_t i = 2; i < ArrayLength+2; i++)
{
AntiCheckSum += Array[i];
}
CheckSum = 0xff - AntiCheckSum;
return CheckSum;
}
/****************************************************************************
Function
InitUART
Parameters
Nothing
Returns
Nothing
Description
Setup UART registers for awesome communications
Notes
Author
Derrick & Anne, 5/13/16
****************************************************************************/
static void InitUART(void)
{
//enable the UART module 1 and 3 using the RCGCUART register
HWREG(SYSCTL_RCGCUART) |= (SYSCTL_RCGCUART_R1|SYSCTL_RCGCUART_R3);

Page 11 of 18

HovercraftSM.c

6/1/16, 5:53 PM

//wait for the UART1 to be ready (PRUART)


while ((HWREG(SYSCTL_PRUART) & SYSCTL_PRUART_R1) != SYSCTL_PRUART_R1);
//enable the clock to the PortC via the RCGCGPIO
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R2;
//wait for the GPIO module to be ready (PRGPIO)
while ((HWREG(SYSCTL_PRGPIO) & SYSCTL_PRGPIO_R2) != SYSCTL_PRGPIO_R2);
//configure the GPIO pins for in/out/drive-level/drive-type
//program the port lines for digital I/O
HWREG(GPIO_PORTC_BASE+GPIO_O_DEN) |= (BIT4HI | BIT5HI | BIT6HI | BIT7HI);
//program the required data directions on the port lines
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR) &= ~(BIT4HI|BIT6HI); // receive lines,
input
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR) |= (BIT5HI|BIT7HI); // transmit lines,
output
//select the Alternate function for the UART pins (AFSEL) on pins B0,B1
HWREG(GPIO_PORTC_BASE+GPIO_O_AFSEL) |= (BIT4HI | BIT5HI | BIT6HI | BIT7HI);
//configure the PMCn fields in the GPIOPCTL register to assign the UART pins
//set mux position in GPIOPCTL to select the UART use of the pins
//1 is the mux value to select UART3, 2 is the mux value to select UART1,
//16/20/24/28 to shift it over to the right nibble for bit 4/5/6/7
//(4 bits/nibble * 4 bits)
HWREG(GPIO_PORTC_BASE+GPIO_O_PCTL) =
((HWREG(GPIO_PORTC_BASE+GPIO_O_PCTL) & 0x0000ffff) + (2<<16) + (2<<20) +
(1<<24) + (1<<28));
//disable the UART by clearing the UARTEN bit in the UARTCTL register.
HWREG(UART1_BASE + UART_O_CTL) &= ~UART_CTL_UARTEN;
HWREG(UART3_BASE + UART_O_CTL) &= ~UART_CTL_UARTEN;
//write the integer portion of the BRD to the UARTIBRD register
HWREG(UART1_BASE + UART_O_IBRD) = 0x0104;
HWREG(UART3_BASE + UART_O_IBRD) = 0x0104; // could be different timing
//write the fractional portion of the BRD to the UARTFBRD register
HWREG(UART1_BASE + UART_O_FBRD) = 0x1B;
HWREG(UART3_BASE + UART_O_FBRD) = 0x1B;
//write the desired serial parameters to the UARTLCRH register
//8 bit word length, 1 stop bit
HWREG(UART1_BASE + UART_O_LCRH) = UART_LCRH_WLEN_8;
HWREG(UART3_BASE + UART_O_LCRH) = UART_LCRH_WLEN_8;
//configure the UART operation using the UARTCTL register
//default UART clock speed is sysclk / 16, and receive/transmit enabled
HWREG(UART1_BASE + UART_O_CTL) = (UART_CTL_RXE|UART_CTL_TXE);
HWREG(UART3_BASE + UART_O_CTL) = (UART_CTL_RXE|UART_CTL_TXE);
//enable the UART by setting the UARTEN bit in the UARTCTL register
HWREG(UART1_BASE + UART_O_CTL) |= UART_CTL_UARTEN;
Page 12 of 18

HovercraftSM.c

6/1/16, 5:53 PM

HWREG(UART3_BASE + UART_O_CTL) |= UART_CTL_UARTEN;


//locally enable interrupts on receive (only module 1)
HWREG(UART1_BASE+UART_O_IM) |= UART_IM_RXIM;
// enable the interrupt in the NVIC
// UART1 is interrupt number 6 so appears in EN0 at bit 6 (pg. 104)
// UART3 is interrupt number 59 so appears in EN1 at bit 27
HWREG(NVIC_EN0) |= BIT6HI;
HWREG(NVIC_EN1) |= BIT27HI;
// make sure interrupts are enabled globally
__enable_irq();
}
/****************************************************************************
Function
InitMotors
Parameters
Nothing
Returns
Nothing
Description
Intitialize direction control lines for fans
Notes
Author
D. Contreras 5/5/16
****************************************************************************/
static void InitMotors(void)
{
//set bit 1 to enable port B in tiva
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R1;
//after enabling the clock, wait until the peripheral reports that clock is
ready
while ((HWREG(SYSCTL_PRGPIO) & SYSCTL_RCGCGPIO_R1) != SYSCTL_PRGPIO_R1);
//set bit 0 to enable port A in tiva
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R0;
//wait for clock to be ready
while ((HWREG(SYSCTL_PRGPIO) & SYSCTL_RCGCGPIO_R0) != SYSCTL_PRGPIO_R0);
//Two Direction Pins for motors
//set pins on port B to be used as a digital i/o line
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= (BIT0HI|BIT1HI);
//set pins 0,1 on port B to 1 to make them outputs
HWREG(GPIO_PORTA_BASE+GPIO_O_DIR) |= (BIT0HI|BIT1HI);
//sets pin 3 to be an output for the lift motor
HWREG(GPIO_PORTA_BASE+GPIO_O_DEN) |= (BIT3HI);
Page 13 of 18

HovercraftSM.c

6/1/16, 5:53 PM

HWREG(GPIO_PORTA_BASE+GPIO_O_DIR) |= (BIT3HI);
}
/****************************************************************************
Function
UARTInterruptResponse
Parameters
Nothing
Returns
Nothing
Description
Interrupt response
Notes
Author
M. Metlitz 5/5/16
****************************************************************************/
void XbeeUARTInterruptResponse(void)
{
static uint8_t TxDataIndex = 0x00;
// RX Response //
// Check RX flag in masked interrupt
if ((HWREG(UART1_BASE + UART_O_MIS) & UART_MIS_RXMIS) != 0)
{
//always clear source of interrupt!
HWREG(UART1_BASE + UART_O_ICR) |= UART_ICR_RXIC;
//post ByteReceived to CommunicateSM with byte parameter from UART Data
reg
ES_Event ThisEvent;
ThisEvent.EventType = BYTE_RECEIVED;
ThisEvent.EventParam = HWREG(UART1_BASE + UART_O_DR);
PostCommunicateSM(ThisEvent);
}
// TX Response //
// Check TX flag in masked interrupt
if ((HWREG(UART1_BASE + UART_O_MIS) & UART_MIS_TXMIS) != 0)
{
//always clear source of interrupt!
HWREG(UART1_BASE + UART_O_ICR) |= UART_ICR_TXIC;
//write data to data reg
HWREG(UART1_BASE + UART_O_DR) = TXData[TxDataIndex];
//increment index
TxDataIndex++;
if (TxDataIndex == (TXData[1]+0x03)) {
TxDataIndex = 0x00;
//disable tx interrupt
HWREG(UART1_BASE + UART_O_IM) &= ~(UART_IM_TXIM);
//enable rx interrupt
HWREG(UART1_BASE + UART_O_IM) |= (UART_IM_RXIM);
}
}
Page 14 of 18

HovercraftSM.c

6/1/16, 5:53 PM

}
/****************************************************************************
Function
GetHovercraftBadge
Parameters
Nothing
Returns
Nothing
Description
Returns the badge value (1-4) of hovercraft
Notes
Author
Anne Alter, 5/18/16
****************************************************************************/
static uint8_t GetHovercraftBadge(void)
{
static uint32_t AnalogValue;
static uint8_t HovercraftBadge;
//read results from analog port line
ADC_MultiRead(results);
AnalogValue = results[PE0];
if((AnalogValue <= 3300) && (AnalogValue > 2900)){
HovercraftBadge = 0x00;}
else if((AnalogValue <= 2275) && (AnalogValue > 1875)){
HovercraftBadge = 0x01;}
else if((AnalogValue <= 1750) && (AnalogValue > 1350)){
HovercraftBadge = 0x02;}
else if((AnalogValue <= 1440) && (AnalogValue > 1040)){
HovercraftBadge = 0x03;}
return HovercraftBadge;
}
/****************************************************************************
Function
SaveEncryptionKey
Parameters
Nothing
Returns
Nothing
Description
saves encryption key in an array
Notes
Author
Anne Alter, 5/18/16
****************************************************************************/
Page 15 of 18

HovercraftSM.c

6/1/16, 5:53 PM

static void SaveEncryptionKey(uint8_t Array[])


{
for(int i = 6; i < 38; i++)
{
EncryptKey[i-6] = Array[i];
}
return;
}
/****************************************************************************
Function
DecryptData
Parameters
Pass in new data received array
Returns
void
Description
Decrypts data
Author
Anne Alter, 5/18/16
****************************************************************************/
static void DecryptData(uint8_t Array[])
{
for(int i = 5; i < 10 ; i++)
{
//xor control data with encryption key
CtrlData[i-5] = Array[i] ^ EncryptKey[EncryptCtr];
EncryptCtr++;
if(EncryptCtr > 31){
EncryptCtr = 0;
}
}
return;
}
/****************************************************************************
Function
CtrlCheckSum
Parameters
Pass in new data received array
Returns
void
Description
Calculates check sum of ctrl data
Author
Anne Alter, 5/19/16
****************************************************************************/
static uint8_t CtrlCheckSum(uint8_t Array[])
{
Page 16 of 18

HovercraftSM.c

uint8_t AntiCheckSum = 0x00;

6/1/16, 5:53 PM

//0xff - chk sum

//loop thru bytes 3 to 3+arraylength


for(uint8_t i = 0; i < 4; i++)
{
AntiCheckSum += Array[i];
}
return AntiCheckSum;
}
/***************************************************************************
public functions
***************************************************************************/
/****************************************************************************
Function
QueryCtrlByte1
Parameters
Pass in new data received array
Returns
void
Description
Allows other services to query this ctrl byte value
Author
Anne Alter, 5/18/16
****************************************************************************/
uint8_t QueryCtrlByte1(void)
{
static uint8_t CtrlByte1;
CtrlByte1 = CtrlData[1];
return CtrlByte1;
}
/****************************************************************************
Function
QueryCtrlByte2
Parameters
Pass in new data received array
Returns
void
Description
Allows other services to query this ctrl byte value
Author
Anne Alter, 5/18/16
****************************************************************************/
uint8_t QueryCtrlByte2(void)
{
static uint8_t CtrlByte2;
CtrlByte2 = CtrlData[2];
Page 17 of 18

HovercraftSM.c

6/1/16, 5:53 PM

return CtrlByte2;
}
/****************************************************************************
Function
QueryCtrlByte3
Parameters
Pass in new data received array
Returns
void
Description
Allows other services to query this ctrl byte value
Author
Anne Alter, 5/18/16
****************************************************************************/
uint8_t QueryCtrlByte3(void)
{
static uint8_t CtrlByte3;
CtrlByte3 = CtrlData[3];
return CtrlByte3;
}
/*------------------------------- Footnotes -------------------------------*/
/*------------------------------ End of file ------------------------------*/

Page 18 of 18

Você também pode gostar