/*******************************************************************************
* UART.c    Initialize the UART connection for the Bluetooth module as well as
*           the receive and transmit data.
*
* Created on: April 20, 2015
* By: Dylan Kleist and Joshua Goertz
* Last Edited on: May 21, 2015
********************************************************************************
* Include Master Header File
*******************************************************************************/
#include "includes.h"
#include "UART.h"
/*******************************************************************************
  Global Variables
*******************************************************************************/
INT8U DataReceived = 0x00;
INT8U DataSent;
#define  UART_BUFF_SIZE         4
static INT8U BuffIndex = 0;
static volatile INT8U UARTBuff[UART_BUFF_SIZE];
/*******************************************************************************
 * *  Public Functions
*******************************************************************************/
void UARTInit(void);
void UARTTransmitByte(INT8U send);
INT8U UARTReceiveByte(void);

/*******************************************************************************
 * *  Public Interrupt
*******************************************************************************/
void UART4_Handler(void);
/*******************************************************************************
* UARTInit() - UART Initialization for the baud rate and  enabling the
*              correct bits.
*******************************************************************************/
void UARTInit(void){
    /*UART Register Initializations*/
    //Setting the mux for all four UART4 ports
    PORTC_PCR15 |= PORT_PCR_MUX(3);     //UART4_TX
    PORTC_PCR14 |= PORT_PCR_MUX(3);     //UART4_RX
    PORTC_PCR13= PORT_PCR_MUX(3);     //UART4_CTS
    PORTC_PCR12= PORT_PCR_MUX(3);     //UART4_RTS
    SIM_SCGC1 |= SIM_SCGC1_UART4_MASK;    //UART Mod Clock = Bus Clock = 60 MHz
    UART4_BDH = UART_BDH_SBR(0x00);       //BDH:BDL = 32
    UART4_BDL = UART_BDL_SBR(0x20);       //BRFD = .5625
    UART4_C4  = UART_C4_BRFA(0x12);
    UART4_PFIFO |= UART_PFIFO_RXFE_MASK;      //FIFO settings for UART
    UART4_CFIFO |= UART_CFIFO_RXFLUSH_MASK;
//    UART4_C1 |= UART_C1_LOOPS_MASK;  //a loop bit orginally set to test Rx/Tx
    UART4_C2 |= UART_C2_TE_MASK;
    UART4_C2 |= UART_C2_RIE_MASK;
    UART4_C2 |= UART_C2_RE_MASK;
    NVIC_EnableIRQ(UART4_RX_TX_IRQn);
    NVIC_SetPriority(UART4_RX_TX_IRQn,0x00);
    //need to set the priority to FIVE
}
/*******************************************************************************
* UARTTransmitByte ()
* Description: Sends a single byte through UART to Bluetooth communication.
* Input: The byte to send.
*******************************************************************************/
void UARTTransmitByte(INT8U send) {
	while(!(UART4_S1 & UART_S1_TDRE_MASK)){}  //wait for transmit ready
	UART4_D = send;                                        //write byte to send
	while ((UART4_S1 & UART_S1_TC_MASK) == 0) {}
    //wait for transmission complete flag
}
/*******************************************************************************
* UART4_RX_TX_IRQHandler
* Description: When UART data is received, stores data in global buffer
*******************************************************************************/
void UART4_Handler(void) {
    OSIntEnter();
    UART4_S1;                //read the S1 register to clear the bit
	BuffIndex++;                                              //increment BuffIndex for adding new byte
	UARTBuff[BuffIndex] = UART4_D;
    OSIntExit();
}


Previous Page