Professional Documents
Culture Documents
Uart
Uart
Uart
****************************************************************************/
#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"
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;
#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"
#define OUR_TEAM 4
#define XBEE_DATA_LENGTH 3
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
//clear these
U2STAbits.UTXINV = 0; //idle state is 1
U2STAbits.UTXBRK = 0; //disable break bit
U2STAbits.ADDEN = 0; //turn off address mode
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;
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);
}
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)];
}
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;
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;
shieldCapacity = 0;
hullCapacity = 0;
weaponsPower = 0;
navigationPower = 0;
weaponsCapacity = 0;
weaponX = 0;
weaponY = 0;
xLoc = 0;
yLoc = 0;
}
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
CurrenttxState = Waitingtx;
}
}
break;
/***************************************************************************
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
***************************************************************************/