Escolar Documentos
Profissional Documentos
Cultura Documentos
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"
HovercraftSM.c
6/1/16, 5:53 PM
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
Page 5 of 18
HovercraftSM.c
6/1/16, 5:53 PM
Page 6 of 18
HovercraftSM.c
6/1/16, 5:53 PM
HovercraftSM.c
6/1/16, 5:53 PM
HovercraftSM.c
6/1/16, 5:53 PM
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
HovercraftSM.c
6/1/16, 5:53 PM
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
HovercraftSM.c
6/1/16, 5:53 PM
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