Uart

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

/****************************************************************************

Header file for template Flat State Machine


based on the Gen2 Events and Services Framework

****************************************************************************/

#ifndef UART_H
#define UART_H

// Event Definitions
#include "ES_Configure.h" /* gets us event definitions */
#include "ES_Types.h" /* gets bool type for returns */
#include "Inputs.h"

// typedefs for the states


typedef union{
struct
{
uint8_t ShipHit : 1;
uint8_t WeaponFired : 1;
uint8_t TVS : 1;
uint8_t OPAMP : 1;
uint8_t ZENER : 1;
uint8_t AssumeCommand : 1;
uint8_t bit7 : 1;
uint8_t bit8 : 1;
}byBits;
uint8_t Teamstatus;
}TeamStatus_t;

typedef union{
struct
{
uint8_t WeaponFired : 1;
uint8_t Alive : 1;
uint8_t bit3 : 1;
uint8_t bit4 : 1;
uint8_t bit5 : 1;
uint8_t TeamNumber : 3;

}byBits;
uint8_t BroadcastStatus;
}BroadcastStatus_t;

typedef struct{
int16_t XCoord;
int16_t YCoord;
}WeaponCoord_t;

typedef struct{
uint16_t XCoord;
uint16_t YCoord;
}ShipCoord_t;

typedef struct{
TeamStatus_t status;
uint8_t hullStrength;
uint8_t MSBweaponDX;
uint8_t LSBweaponDX;
uint8_t MSBweaponDY;
uint8_t LSBweaponDY;
uint8_t weaponsPower;
uint8_t navigationPower;
uint8_t weaponsCapacity;
uint8_t shieldCapacity;
}TeamMessage_t;

typedef struct{
BroadcastStatus_t status;
uint8_t hullStrength;
uint8_t xLocMSB;
uint8_t xLocLSB;
uint8_t yLocMSB;
uint8_t yLocLSB;
uint8_t weaponDXMSB;
uint8_t weaponDXLSB;
uint8_t weaponDYMSB;
uint8_t weaponDYLSB;
uint8_t shieldStrength;
}BroadcastMessage_t;

typedef enum
{
InitUARTrxState, Waitingrx,
}UARTrxState_t;
// State definitions for use with the query function for tx
typedef enum
{
InitUARTtxState, Waitingtx, Transmitting, Zener,
}UARTtxState_t;

// Public Function Prototypes

//for the tx side


bool InitUARTtx(uint8_t Priority);

ES_Event_t RunUARTtx(ES_Event_t ThisEvent);

//for the rx side


bool InitUARTrx(uint8_t Priority);
bool PostUART(ES_Event_t ThisEvent);
ES_Event_t RunUARTrx(ES_Event_t ThisEvent);
WeaponCoord_t getOurWeaponCoords(void);
WeaponCoord_t getEnemyWeaponCoords(void);
ShipCoord_t getOurShipCoords(void);
ShipCoord_t getEnemyShipCoords(void);

#endif /* UART_H */

/****************************************************************************
Module
UART.c

Revision
1.0.1

Description
This is a template file for implementing flat state machines under the
Gen2 Events and Services Framework.

Notes

History
When Who What/Why
-------------- --- --------
01/15/12 11:12 jec revisions for Gen2 framework
11/07/11 11:26 jec made the queue static
10/30/11 17:59 jec fixed references to CurrentEvent in RunTemplateSM()
10/23/11 18:20 jec began conversion from SMTemplate.c (02/20/07 rev)
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/
//hardware includes
#include <xc.h>
#include <proc/p32mx170f256b.h>
#include <sys/attribs.h> // for ISR macors

#include "ES_Configure.h"
#include "ES_Framework.h"
#include "UART.h"
#include "math.h"
#include "TVS.h"
#include "ZENER.h"
#include "OPAMP.h"
#include "Inputs.h"

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


#define TX_TIMER_WAIT 200
//addresses for the two other pics in your team
#define ADDRESS1 0x2182
#define ADDRESS2 0x2194

#define OUR_TEAM 4

//longest message possible our of a team message and broadcast


#define MAX_MESSAGE_LENGTH 20

#define XBEE_DATA_LENGTH 3

//how long without the checksum and first three bytes


#define TEAM_MESSAGE_LENGTH 15
#define BROADCAST_MESSAGE_LENGTH 16

/*---------------------------- Module Functions ---------------------------*/


/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/

/*---------------------------- Module Variables ---------------------------*/


// everybody needs a state variable, you may need others as well.
// type of state variable should match that of enum in header file

static UARTtxState_t CurrenttxState;


static UARTrxState_t CurrentrxState;

volatile uint8_t TxIdx;


volatile uint8_t RxIdx;

volatile uint8_t Sumtx;


volatile uint8_t Sumrx;

static uint8_t TXArray[21];


static uint8_t TXLength;

volatile uint8_t RXLength;

static TeamStatus_t OriginalTeamStatus;


static TeamStatus_t AssumedStatus; //start out with no assumed stations
static TeamStatus_t ActiveStatus;

static BroadcastStatus_t BroadcasttxStatus;

volatile uint8_t MessageNum;


static uint8_t RXArray[42];

static WeaponCoord_t OurWeaponCoords;


static WeaponCoord_t EnemyWeaponCoords;

static ShipCoord_t EnemyShipCoords;


static ShipCoord_t OurShipCoords;
// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPrioritytx;
//for the rx module
static uint8_t MyPriorityrx;

/*------------------------------ Module Code ------------------------------*/


/****************************************************************************
Function
InitUART

Parameters
uint8_t : the priorty of this service

Returns
bool, false if error in initialization, true otherwise

Description
initializes UART and change notification pins
Notes

Author
J. Edward Carryer, 10/23/11, 18:55
****************************************************************************/
bool InitUARTtx(uint8_t Priority)
{
ES_Event_t ThisEvent;

MyPrioritytx = Priority;
// put us into the Initial PseudoState

TRISAbits.TRISA3 = 0; //TX OUTPUT


U2MODEbits.ON = 0; //turn uart off
RPA3R = 2; //SET A3 AS TRANSMIT PIN FOR UART

//clear all these guys


U2MODEbits.SIDL = 0;
U2MODEbits.IREN = 0;
U2MODEbits.RTSMD = 0;
U2MODEbits.UEN = 0;
U2MODEbits.WAKE = 0;
U2MODEbits.LPBACK = 0;
U2MODEbits.ABAUD = 0;
U2MODEbits.RXINV = 0;

U2MODEbits.BRGH = 0; //use standard speed


U2MODEbits.PDSEL = 0; //8 bit data, no parity
U2MODEbits.STSEL = 0; //one stop bit

//clear these
U2STAbits.UTXINV = 0; //idle state is 1
U2STAbits.UTXBRK = 0; //disable break bit
U2STAbits.ADDEN = 0; //turn off address mode

U2STAbits.UTXISEL = 0; //interrupt enabled whenever a character can be


written
IPC9bits.U2IP = 6; //transfer priority is 6

U2BRG = 129; //set up for 9600 baud communication


U2MODEbits.ON = 1; //turn on uart

CurrenttxState = InitUARTtxState;
// post the initial transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPrioritytx, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitUARTrx

Parameters
uint8_t : the priorty of this service

Returns
bool, false if error in initialization, true otherwise

Description
initializes UART
Notes

Author
J. Edward Carryer, 10/23/11, 18:55
****************************************************************************/
bool InitUARTrx(uint8_t Priority)
{
ES_Event_t ThisEvent;

MyPriorityrx = Priority;
// put us into the Initial PseudoState
U2MODEbits.ON = 0; //turn off the UART
U2STAbits.URXISEL = 0; //interrupt will be asserted whenever there is data.
TRISAbits.TRISA1 = 1; //set as input

//set RB11 as rx ,
U2RXRbits.U2RXR = 3;
U2MODEbits.ON = 1; //turn on the UART
//initially we shouldnt assume any stations
AssumedStatus.Teamstatus = 0;

CurrentrxState = InitUARTrxState;
// post the initial transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriorityrx, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}

/****************************************************************************
Function
PostUART

Parameters
EF_Event_t ThisEvent , the event to post to the queue

Returns
boolean False if the Enqueue operation failed, True otherwise

Description
Posts an event to this state machine's queue
Notes

Author
J. Edward Carryer, 10/23/11, 19:25
****************************************************************************/
bool PostUART(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPrioritytx, ThisEvent);
}

/****************************************************************************
Function
RunUARTtx

Parameters
ES_Event_t : the event to process

Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description
sends things in uart when requested, turns on and off LED
Notes
uses nested switch/case to implement the machine.
Author
C.Paullin
****************************************************************************/
ES_Event_t RunUARTrx(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors

switch (CurrentrxState)
{
case InitUARTrxState: // If current state is initial Psedudo State
{
if (ThisEvent.EventType == ES_INIT) // only respond to ES_Init
{
U2STAbits.URXEN = 1; //receive turn on
CurrentrxState = Waitingrx;
IEC1SET = _IEC1_U2RXIE_MASK; //turn it on!!

}
}
break;

case Waitingrx: // If current state is state one


{
if(ThisEvent.EventType == ES_TEAM_MESSAGE_RECEIVED){
uint8_t ArrayIdx = ThisEvent.EventParam;

TeamStatus_t status;
status.Teamstatus = RXArray[8+(ArrayIdx*MAX_MESSAGE_LENGTH)];
ES_Event_t Event2Send;
//if message received was from OPAMP
if(status.byBits.OPAMP){
Event2Send.EventType = ES_WEAPONSPOWER_UP; //send a weapons
power update
Event2Send.EventParam =
RXArray[14+(ArrayIdx*MAX_MESSAGE_LENGTH)];
PostOpamp(Event2Send);
Event2Send.EventType = ES_NAVPOWER_UP;
Event2Send.EventParam =
RXArray[15+(ArrayIdx*MAX_MESSAGE_LENGTH)];
PostOpamp(Event2Send);
Event2Send.EventType = ES_SHIELDCAP_UP;
Event2Send.EventParam =
RXArray[17+(ArrayIdx*MAX_MESSAGE_LENGTH)];
PostOpamp(Event2Send);
Event2Send.EventType = ES_HULLCAP_UP;
Event2Send.EventParam =
RXArray[9+(ArrayIdx*MAX_MESSAGE_LENGTH)];
PostOpamp(Event2Send);
}
if(status.byBits.TVS){
//if we fired a weapon, tell the zener so it can broadcast
if(status.byBits.WeaponFired){
Event2Send.EventType = ES_OUR_WEAPON_FIRED;
Event2Send.EventParam = 0;
PostTvs(Event2Send);
PostUART(Event2Send);
//these are the coordinates it will send
OurWeaponCoords.XCoord=
RXArray[10+(ArrayIdx*MAX_MESSAGE_LENGTH)] |
(RXArray[11+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8);
OurWeaponCoords.YCoord=
RXArray[12+(ArrayIdx*MAX_MESSAGE_LENGTH)] |
(RXArray[13+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8);
}

//send a weapons capacity update to the TVS in order to keep


it actualized
Event2Send.EventType = ES_WEAPONSCAP_UP;
Event2Send.EventParam =
RXArray[16+(ArrayIdx*MAX_MESSAGE_LENGTH)];
PostTvs(Event2Send);
}

if(status.byBits.ZENER){
if(status.byBits.ShipHit){
Event2Send.EventType = ES_SHIPHIT;
PostOpamp(Event2Send);
}
}

}
if(ThisEvent.EventType == ES_BROADCAST_RECEIVED){
uint8_t ArrayIdx = ThisEvent.EventParam;
ES_Event_t Event2Send;
//heres the address it came from
uint16_t Address = RXArray[5+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8 |
RXArray[6+(ArrayIdx*MAX_MESSAGE_LENGTH)];
BroadcastStatus_t BroadcastrxStatus;
BroadcastrxStatus.BroadcastStatus =
RXArray[8+(ArrayIdx*MAX_MESSAGE_LENGTH)];

//make sure we aren't checking our own broadcasts


if(Address != ADDRESS1 && Address != ADDRESS2){
//only thing this pic needs is if there was a weapon fired
if(BroadcastrxStatus.byBits.WeaponFired){
//only send if we are zener right now
if(ActiveStatus.byBits.ZENER){
//these are the weapon delta x and y and the enemy
ship xy pos
EnemyShipCoords.XCoord =
RXArray[11+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8 |
RXArray[10+(ArrayIdx*MAX_MESSAGE_LENGTH)];
EnemyShipCoords.YCoord =
RXArray[13+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8 |
RXArray[12+(ArrayIdx*MAX_MESSAGE_LENGTH)];
EnemyWeaponCoords.XCoord =
RXArray[15+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8 |
RXArray[14+(ArrayIdx*MAX_MESSAGE_LENGTH)];
EnemyWeaponCoords.YCoord =
RXArray[17+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8 |
RXArray[16+(ArrayIdx*MAX_MESSAGE_LENGTH)];
if(EnemyWeaponCoords.XCoord!= 0 &&
EnemyWeaponCoords.YCoord!= 0){
Event2Send.EventType = ES_ENEMYWEAPONFIRED;
PostZener(Event2Send);
}
}
}else{
if(BroadcastrxStatus.byBits.TeamNumber == OUR_TEAM){
//update for the getter functions
OurShipCoords.XCoord =
RXArray[11+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8 |
RXArray[10+(ArrayIdx*MAX_MESSAGE_LENGTH)];
OurShipCoords.YCoord =
RXArray[13+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8 |
RXArray[12+(ArrayIdx*MAX_MESSAGE_LENGTH)];
OurWeaponCoords.XCoord =
RXArray[15+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8 |
RXArray[14+(ArrayIdx*MAX_MESSAGE_LENGTH)];
OurWeaponCoords.YCoord =
RXArray[17+(ArrayIdx*MAX_MESSAGE_LENGTH)]<<8 |
RXArray[16+(ArrayIdx*MAX_MESSAGE_LENGTH)];
}
}

}
break;
// repeat state pattern as required for other states
default:
;
} // end switch on Current State

}
return ReturnEvent;
}
/****************************************************************************
Function
RunUARTtx

Parameters
ES_Event_t : the event to process

Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description
sends things in uart when requested, turns on and off LED
Notes
uses nested switch/case to implement the machine.
Author
C.Paullin
****************************************************************************/
ES_Event_t RunUARTtx(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
static uint16_t xLoc;
static uint16_t yLoc;
//this is the current active status, this will change depending on
potential connectivity
//issues and the info comes from uartrx
//capacity of the capacitor banks
static uint8_t shieldCapacity;
static uint8_t hullCapacity;
static uint8_t weaponsCapacity;
//different power amounts that we may need
static uint8_t weaponsPower;
static uint8_t navigationPower;

//delta x and y
static int16_t weaponX;
static int16_t weaponY;

//these are flags so this knows whether to send or not


static uint8_t ShipHit = 0;
static uint8_t WeaponFired = 0;

if(ThisEvent.EventType == ES_SHIPHIT){
ShipHit = 1;
}
if(ThisEvent.EventType == ES_OUR_WEAPON_FIRED){
WeaponFired = 1;
}
if(ThisEvent.EventType == ES_DEAD){
//indicate that we are in fact, dead
BroadcasttxStatus.byBits.Alive = 0;
}

switch (CurrenttxState)
{
case InitUARTtxState: // If current state is initial Psedudo State
{
if (ThisEvent.EventType == ES_INIT) // only respond to ES_Init
{
BroadcasttxStatus.BroadcastStatus = 0;
//get the status that will be sent to team members based on switch
states
gameMode originalMode = getGameMode();
if(originalMode.OPAMP == 1){
OriginalTeamStatus.byBits.OPAMP = 1;
}
if(originalMode.TVS == 1){
OriginalTeamStatus.byBits.TVS = 1;
}
if(originalMode.ZENER == 1){
OriginalTeamStatus.byBits.ZENER = 1;
}
ActiveStatus.Teamstatus = OriginalTeamStatus.Teamstatus;

BroadcasttxStatus.byBits.Alive = 1;
BroadcasttxStatus.byBits.TeamNumber = OUR_TEAM;

CurrenttxState = Waitingtx;
ES_Timer_InitTimer(TX_TIMER,TX_TIMER_WAIT); //every 200 ms
}
}
break;

case Waitingtx: // If current state is state one


{
if(ThisEvent.EventType == ES_TIMEOUT){
if(ThisEvent.EventParam == TX_TIMER){

//this is the active status, in other words the station we


originally were assigned
//and the stations we may have taken on due to poor
connection
gameMode originalMode = getGameMode();
//reset team status
if(originalMode.OPAMP == 1){
ActiveStatus.byBits.OPAMP = 1;
}else{
ActiveStatus.byBits.OPAMP = 0;
}
if(originalMode.TVS == 1){
ActiveStatus.byBits.TVS = 1;
}else{
ActiveStatus.byBits.TVS = 0;
}
if(originalMode.ZENER == 1){
ActiveStatus.byBits.ZENER = 1;
}else{
ActiveStatus.byBits.ZENER = 0;
}

shieldCapacity = 0;
hullCapacity = 0;
weaponsPower = 0;
navigationPower = 0;
weaponsCapacity = 0;
weaponX = 0;
weaponY = 0;
xLoc = 0;
yLoc = 0;

//do the things the op amp does


if(ActiveStatus.byBits.OPAMP==1){
powerLevels Levels;
//get the data we need from the opamp state machine
Levels = getPowerLevels();
shieldCapacity = getShieldLevel();
hullCapacity = getHullLevel();
weaponsPower = Levels.weapons;
navigationPower = Levels.navigation;
}
if(ActiveStatus.byBits.TVS == 1){
weaponsCapacity =
getWeaponsCapacity();//QueryWeaponsCapacity();
if(WeaponFired){//WeaponFired()){
ActiveStatus.byBits.WeaponFired = 1;
//get these from TVS
OurWeaponCoords = getWeaponLocation();
OurWeaponCoords.YCoord = -1*OurWeaponCoords.YCoord;

}
WeaponFired = 0;
}
//if we are acting as zener
if(ActiveStatus.byBits.ZENER == 1){
//update ship hit status
if(ShipHit){
//set the bit to send
ActiveStatus.byBits.ShipHit = 1;
//reset flag
ShipHit = 0;
}
//update
if(WeaponFired){//WeaponFired()){
BroadcasttxStatus.byBits.WeaponFired = 1;
WeaponFired = 0;
//reset weaponFired
}
//get the locations of the ship to send
OurShipCoords= getShipLocation();

shieldCapacity =
getShieldLevel();//QueryShieldCapacity();
hullCapacity = getHullLevel(); //QueryHullCapacity();
}
TXLength = 19;
TxIdx = 0;
TXArray[0] = 0x7E; //first byte
TXArray[1] = 0; //msb of length
TXArray[2] = TEAM_MESSAGE_LENGTH; //length of data frame
TXArray[3] = 0x01; //always send this
TXArray[4] = 0; //frame id turn off ack
TXArray[5] = ADDRESS1>>8 ; //send addy
TXArray[6] = (uint8_t)ADDRESS1 ; //send addy
TXArray[7] = 0x01; //disable ack and who knows what that
PAN thing does
TXArray[8] = ActiveStatus.Teamstatus ; //ship is alive
TXArray[9] = hullCapacity; //hull capacity
TXArray[10] = (uint8_t)OurWeaponCoords.XCoord; //send lsb of
beam delta x
TXArray[11] = OurWeaponCoords.XCoord>>8; //MSB of beam
delta x
TXArray[12] = (uint8_t)OurWeaponCoords.YCoord;//lsb of beam
delta y
TXArray[13] = OurWeaponCoords.YCoord>>8; //msb of beam
delta y
TXArray[14] = weaponsPower; //weapons power
TXArray[15] = navigationPower; //navigation power
TXArray[16] = weaponsCapacity; //weapons capacity
TXArray[17] = shieldCapacity; //shield capacity
//reset weapon fired flag
ActiveStatus.byBits.WeaponFired = 0;
CurrenttxState = Transmitting;
ES_Timer_InitTimer(TX_TIMER,TX_TIMER_WAIT); //every 200 ms
send again
IEC1SET = _IEC1_U2TXIE_MASK; //turn on interrupts for
transmit
U2STAbits.UTXEN = 1; //enable transmit

}
}

}
break;
case Transmitting:
{
if(ThisEvent.EventType == ES_TRANSMIT_COMPLETE){
Sumtx = 0x00; //make checksum full of 00
TxIdx = 0; // reset index
TXArray[5] = ADDRESS2>>8; //msb of address 2
TXArray[6] = (uint8_t)ADDRESS2; //lsb of address 2
IEC1SET = _IEC1_U2TXIE_MASK; //turn on interrupts for
transmit

U2STAbits.UTXEN = 1; //enable transmit


if( ActiveStatus.byBits.ZENER == 1){
//if we are zener we have to send the next part.
CurrenttxState = Zener;
}else{
//we have sent out two team transmits so we can wait
CurrenttxState = Waitingtx;
}
}
}
break;
case Zener:
{
if(ThisEvent.EventType == ES_TRANSMIT_COMPLETE){
TxIdx = 0;
TXLength = 20; //length including checksum
TXArray[0] = 0x7E; //first byte
TXArray[1] = 0; //msb of length
TXArray[2] = BROADCAST_MESSAGE_LENGTH; //length of data
frame
TXArray[3] = 0x01; //always send this
TXArray[4] = 0; //frame id turn off response frame
TXArray[5] = 0xFF; //broadcast
TXArray[6] = 0xFF; //broadcast
TXArray[7] = 0; //allow ack and what is this pan
thing!
TXArray[8] = BroadcasttxStatus.BroadcastStatus; //status
byte
TXArray[9] = hullCapacity; //hull strength
TXArray[10] = (uint8_t)OurShipCoords.XCoord; //send lsb of
ship x location
TXArray[11] = OurShipCoords.XCoord>>8; //MSB of ship x
location
TXArray[12] = (uint8_t)OurShipCoords.YCoord;//lsb of ship y
loc
TXArray[13] = OurShipCoords.YCoord>>8; //msb of ship y
location
TXArray[14] = (uint8_t)OurWeaponCoords.XCoord; //beam
delta x lsb
TXArray[15] = OurWeaponCoords.XCoord>>8; //beam
delta x msb
TXArray[16] = (uint8_t)OurWeaponCoords.YCoord; //beam
delta y lsb
TXArray[17] = OurWeaponCoords.YCoord>>8; //beam
delta y msb
TXArray[18] = shieldCapacity; //shield strength
//reset weapon fired flag
BroadcasttxStatus.byBits.WeaponFired = 0;
IEC1SET = _IEC1_U2TXIE_MASK; //turn on interrupts for
transmit
U2STAbits.UTXEN = 1; //enable transmit

CurrenttxState = Waitingtx;
}
}
break;

// repeat state pattern as required for other states


default:
;
} // end switch on Current State
return ReturnEvent;
}

/***************************************************************************
getter functions
***************************************************************************/

WeaponCoord_t getOurWeaponCoords(void){
return OurWeaponCoords;
}

WeaponCoord_t getEnemyWeaponCoords(void){
return EnemyWeaponCoords;
}

ShipCoord_t getOurShipCoords(void){
return OurShipCoords;
}

ShipCoord_t getEnemyShipCoords(void){
return EnemyShipCoords;
}

/***************************************************************************
private functions
***************************************************************************/

void __ISR(_UART_2_VECTOR, IPL6SOFT) UARTInterrupts(void){


if(IFS1bits.U2TXIF && IEC1bits.U2TXIE){

while(U2STAbits.UTXBF == 0){ //while buffer not full


if(TxIdx == TXLength-1){
IEC1CLR = _IEC1_U2TXIE_MASK; //turn off interrupts for
transmit we are done with transmission
uint8_t Checksumtx = 0xFF - Sumtx;
U2TXREG = Checksumtx; //send checksum
Sumtx = 0x00; //reset checksum
TxIdx = 0; //reset the txIdx
ES_Event_t ThisEvent;
//tell this service we have completed a transmit
ThisEvent.EventType = ES_TRANSMIT_COMPLETE;
ES_PostToService(MyPrioritytx, ThisEvent);
break;
}
U2TXREG = TXArray[TxIdx]; //send switch val
if(TxIdx>2){
Sumtx += TXArray[TxIdx];
}
TxIdx++; //increment the index
}
IFS1CLR = _IFS1_U2TXIF_MASK; //clear flag
}
//if interrupt is result of receive
if(IFS1bits.U2RXIF){
//while stuff is in buffer
while(U2STAbits.URXDA){
//save away in the correct spot in the array
RXArray[RxIdx+(MessageNum * MAX_MESSAGE_LENGTH)] = U2RXREG;
//check if were on the right byte
if(RxIdx == 0){
//if it isnt 0x7E leave the while loop and dont increment
if (RXArray[0] != 0x7E){
break;
}
}
if(RxIdx == 2){
RXLength = RXArray[RxIdx+(MessageNum * MAX_MESSAGE_LENGTH)];
//reinitialize sum
Sumrx = 0x00;
}
//begin calculating checksum
if(RxIdx>= 3){
Sumrx += RXArray[RxIdx+(MessageNum* MAX_MESSAGE_LENGTH)];
}
//this is when we should post the message and check the checksum
if(RxIdx > (RXLength+ XBEE_DATA_LENGTH -1)){
//if the checksum is good
if(0xFF == Sumrx){
ES_Event_t Event2Send;
if(RXLength == TEAM_MESSAGE_LENGTH){
Event2Send.EventType = ES_TEAM_MESSAGE_RECEIVED;
}
if(RXLength == BROADCAST_MESSAGE_LENGTH){
Event2Send.EventType = ES_BROADCAST_RECEIVED;
}
Event2Send.EventParam = MessageNum;
ES_PostToService(MyPriorityrx, Event2Send);
}
//reset the RxIdx
RxIdx = 0;
//now switch which part of the array we are writing to.
MessageNum++;
if(MessageNum> 1){
MessageNum = 0;
}
break;
}
RxIdx++;
}
IFS1CLR = _IFS1_U2RXIF_MASK; //clear flag
}

You might also like