//*****************************************************************************
//
//  UARTdriver.c - Knihovna pro komunikaci pomoci UART / LM3S2276
//
// Copyright (c) 2005-2008 Luminary Micro, Inc.  All rights reserved.
// 
// Software License Agreement
// 
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
// 
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  You may not combine
// this software with "viral" open-source software in order to form a larger
// program.  Any use in violation of the foregoing restrictions may subject
// the user to criminal sanctions under applicable laws, as well as to civil
// liability for the breach of the terms and conditions of this license.
// 
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 3416 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************


#include <common.h>
#include <uart.h>
#include <string.h>
#include <UARTCmds.h>
#include <UARTdriver.h>



// Promenne**********************
char UARTreceiveBuffer[50];
char *ptrRecBuffer;
//struct UARTData UARTComm;

//Definice funkci****************

//Inicializace UART modulu-----------------------------------------------------
void UARTInit(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);                                //Povoleni modulu UART na portu A
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    ptrRecBuffer = &UARTreceiveBuffer[0];                                       //Ulozeni ukazatele na pocatek prijimaciho bufferu

    GPIOPinTypeUART(GPIO_PORTA_BASE, UART_PINS);                                //Nastaveni pinu na portu A (0 a 1) pro UART

    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), UARTBAUD_115200,          //konfigurace UART / modul O, 115kb, 8datovych bitu, zadny paritni bit a jeden stop bit
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));

    UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);                       // Povoleni preruseni pri prijmu a vysilani na UART modulu

    IntEnable(INT_UART0);		                                        // Povoleni preruseni od UART modulu
    IntMasterEnable();			                                        // Povoleni preruseni procesoru
    IntRegister(INT_UART0,&UARTIntHandler);                                     // Definovani fce pro obsluhu preruseni UART
}



// Fce pro obsluhu preruseni / UART--------------------------------------------
void UARTIntHandler(void)
{
    unsigned long ulStatus;
    unsigned char RecByte;

    ulStatus = UARTIntStatus(UART0_BASE, true);                                 // Nacteni vektoru preruseni
    UARTIntClear(UART0_BASE, ulStatus);                                         //Smazani priznaku preruseni od UART 
    while(UARTCharsAvail(UART0_BASE))                                           // Dokud jsou v prijimacim FIFO znaky, opakuji
    {
        RecByte=UARTCharGetNonBlocking(UART0_BASE);             //Prijem bytu
        if(UARTComm.State<UART_STATE_PROCCESS)                  //Pokud neni zpracovavan jiny ramec
          RecieveSM(RecByte);                                   //predani do stavoveho automatu
       
        /*
        *ptrRecBuffer++=UARTCharGetNonBlocking(UART0_BASE);                     //Cteni 1znaku a ukladani do pole na prvni volnou pozici
        if(*ptrRecBuffer=='\n')                                                 // Jestli je prijat zakoncovaci znak zpravy
        {
          ptrRecBuffer = &UARTreceiveBuffer[0];                                 //Nastaveni ukazatele na zacatek prijimaciho bufferu pro novou zpravu        
        }
        */
    }
    //request.flag.uart_message_received=1;                                       //Jakmile prijata kompletni zprava, nastaven priznak
}



//Odeslani UART zpravy--------------------------------------------------------
void UARTSendMessage(const char *data)                                          //parametr data ukazuje na data k odeslani na UART
{
  const char *count = strlen(data) + data;  
  while(data<count) UARTCharPutNonBlocking(UART0_BASE, *data++);
}

//Odeslani dat na UART-------------------------------------------------------
void UARTSendData(const char *data, int size)                                   //parametr data ukazuje na data k odeslani na UART, size pocet bytu
{
  const char *count = size + data;                                              //ulozeni ukazatele na posledni byte k odeslani +1 
  while(data<count) UARTCharPutNonBlocking(UART0_BASE, *data++);                //dokud neni posledni byte k odeslani, posilam dale
  //UARTCharPutNonBlocking(UART0_BASE, '/n');
}



