
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "My_usart.h"
#include "PIN_config.h"				// Definice nazvu pinu a portu a makra pro jejich ovladani

static uint8_t MUTEX_USART1_ = 0;
volatile uint8_t Dummy=0;

/* ----------------------------------------------
 Inicializace serioveho kanalu USART1
 --------------------------------------------- */
void USART1_Init(void)
{
	// Definice struktur
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC->APB2ENR |= (RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO);
	USART1_ON();																	// Nastaveni smeru pinu RX a TX a aktivace jejich alternativni funkce
 	// Konfigurace USARTu
    USART_InitStructure.USART_BaudRate = 19200;										// Rychlost 9600 bd
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;						// 8 bitove data
    USART_InitStructure.USART_StopBits = USART_StopBits_1;							// 1 stop bit
    USART_InitStructure.USART_Parity = USART_Parity_No;								// parita zadna
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;	// HW rizeni toku
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;					// Mod vysilac i prijimac
    USART_Init(USART1, &USART_InitStructure);										 
	USART_Cmd(USART1, ENABLE);														// Zapnuti UASRT1 jednotky
}

/* ----------------------------------------------
 Odeslani jednoho byte na seriovou linku
 s cekanim na dokonceni
 --------------------------------------------- */
void USART1_Write(uint8_t data)
{
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);	// Cekani na dokonceni predchoziho odesilani
	USART_SendData(USART1, data);									// Odeslani dat do vystupniho bfferu
}

/* ----------------------------------------------
 Prijeti jednoho byte ze seriove linky s cekanim
 nez data prijdou
 --------------------------------------------- */
uint8_t USART1_Read(void)
{
	while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
	return (uint16_t)(USART1->DR & (uint16_t)0x01FF);
}

/* ----------------------------------------------
 Funkce vrati true pokud byla na  seriovou linku 
 prijata data (jeden byte) a do promenne na kterou 
 ukazuje UART_Get_Buffer ulozi tento prijaty byte. 
 Pokud ne, tak vrati false
 --------------------------------------------- */
uint8_t USART1_ReadIf(uint8_t *UART_Get_Buffer)
{
	if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
	{
		*UART_Get_Buffer = USART_ReceiveData(USART1);
		return true;
	}
	else
	{
		return false;
	}
}

/* ----------------------------------------------
 Odeslani retezce na ktery ukazuje text na 
 seriovou linku. Pokud je enter = true, tak se na 
 konci retezce odesle znak pro konec radku LF LR
 --------------------------------------------- */
void USART1_Send(uint8_t *text, uint8_t enter)
{
	static uint8_t znak;
	
	while(znak=*text++)
	{
		USART1_Write(znak);
	}
	if(enter==true)	
	{
		USART1_Send_Enter();
	}
}

void USART1_SendNumber(unsigned char cislo)
{
	uint8_t tmp;

	tmp = cislo / 100;
	USART1_Write('0'+tmp);
	cislo = cislo - 100*tmp;

	tmp = cislo / 10;
	USART1_Write('0'+tmp);
	USART1_Write('0'+(cislo - 10*tmp));
}
void USART1_SendInteger(uint16_t cislo)
{
	uint16_t temp;
	uint8_t zobrazitnulu = 0;

	temp = cislo/10000;				// desetitisice
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	cislo = cislo-(temp*10000);
	
	temp=cislo/1000;				// tisice
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*1000);

	temp=cislo/100;				// stovky
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*100);

	temp=cislo/10;				// desitky
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*10);
	USART1_Write('0'+cislo);		// jednotky
}

void USART1_SendInt32(uint32_t cislo)
{
	uint32_t temp;
	uint8_t zobrazitnulu = 0;

	temp = cislo/1000000000;				// desetitisice
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	cislo = cislo-(temp*1000000000);
	
	temp=cislo/100000000;				// 2^32
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*100000000);
	
	temp=cislo/10000000;				// 2^28
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*10000000);
	
	temp=cislo/1000000;				// 2^24
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*1000000);
	
	temp=cislo/100000;				// 2^20
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*100000);
	
	temp=cislo/10000;				// 2^16
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*10000);
	
	temp=cislo/1000;				// 2^12
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*1000);

	temp=cislo/100;				// 2^8
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*100);

	temp=cislo/10;				// 2^4
	if (temp!=0)
	{
		USART1_Write('0'+temp);
		zobrazitnulu = 1;
	}
	else if (zobrazitnulu==1)
	{
		USART1_Write('0');
	}
	cislo = cislo-(temp*10);
	USART1_Write('0'+cislo);		// 2^0
}

/* --------------------------------------
   Prevod binarniho cisla na HEX
   ----------------------------------- */
uint8_t USART1_BinToHex(uint8_t bin)
{
	return ((bin)%16) | (((bin)/16)<<4); /* Binary 2 HEX conversion */
}

/* --------------------------------------
 Zobrazi na LCD HEX cislo
 ------------------------------------- */
void USART1_Send_Hex(uint8_t hex)
{
	uint8_t tmp;
	uint8_t i;

	for(i=0;i<2;i++)
	{
		if (i==0)
		{
			tmp = (USART1_BinToHex(hex)&0xF0) >> 4;
		}
		else
		{
			tmp = (USART1_BinToHex(hex)&0x0F);
		}
		if (tmp>9)
		{
			USART1_Write('A' + tmp-10);
		}
		else
		{
			USART1_Write('0' + tmp);
		}
	}
}

/* ------------------------------------------

 ----------------------------------------- */
void USART1_WaitForMUTEX(void)
{
	while(MUTEX_USART1_)
	{
		Dummy++;
		vTaskDelay(1*portTICK_FOR_1MS);
	}
}

void USART1_SetMUTEX(void)
{
	MUTEX_USART1_=1;
}

void USART1_FreeMUTEX(void)
{
	MUTEX_USART1_=0;
}
// Konec souboru =================================================================
