/*
 * uart_drv.h
 *
 * Created: 27.2.2016 12:41:45
 *  Author: Chenda
 */ 


#ifndef UART_DRV_H_
#define UART_DRV_H_

/************************************************************************/
/*  UART 0 macros default: asynchronous mode, parity none, 1-stop bit   */
/************************************************************************/
#define mUart0TransX2()			UCSR0A|=(1<<U2X0)
#define mUart0TransX1()			UCSR0A&=~(1<<U2X0)
#define mUart0Rx_Enab()			UCSR0B|=(1<<RXEN0)
#define mUart0Rx_Dis()			UCSR0B&=~(1<<RXEN0)
#define mUart0Tx_Enab()			UCSR0B|=(1<<TXEN0)
#define mUart0Tx_Dis()			UCSR0B&=~(1<<TXEN0)
#define mUart0IntRx_Enab()		UCSR0B|=(1<<RXCIE0)
#define mUart0IntRx_Dis()		UCSR0B&=~(1<<RXCIE0)
#define mUart0IntTx_Enab()		UCSR0B|=(1<<TXCIE0)
#define mUart0IntTx_Dis()		UCSR0B&=~(1<<TXCIE0)
#define mUart0TxFlag_Clr()		UCSR0A|=(1<<TXC0)
#define mUart0IntUdrEmp_Enab()	UCSR0B|=(1<<UDRIE0)
#define mUart0IntUdrEmp_Dis()	UCSR0B&=~(1<<UDRIE0)
#define mUart0NoBit_8()			UCSR0C&=~(3<<UCSZ00);UCSR0B&=~(1<<UCSZ02);UCSR0C|=(3<<UCSZ00)
#define mUart0BaudRate(l,h)		UBRR0L=l;UBRR0H=h&0xF

/************************************************************************/
/* Timer/counter 1 macros                                               */
/************************************************************************/
#define mTimer1Mode_CTC()		TCCR1A&=~(3<<WGM10);TCCR1B&=~(3<<WGM12);TCCR1B|=(1<<WGM12)		//  pocita do hodnoty OCRnA registru a pak se vynuluje
#define mTimer1Mode_NORM()		TCCR1A&=~(3<<WGM10);TCCR1B&=~(3<<WGM12)							//  pocita do hodnoty 0xFFFF a pak se nuluje
#define mTimer1Stop()			TCCR1B&=~(7<<CS10)
#define mTimer1Run_Clk_1()		TCCR1B|=(0<<CS12)|(0<<CS11)|(1<<CS10)
#define mTimer1Run_Clk_8()		TCCR1B|=(0<<CS12)|(1<<CS11)|(0<<CS10)
#define mTimer1Run_Clk_64()		TCCR1B|=(0<<CS12)|(1<<CS11)|(1<<CS10)
#define mTimer1Run_Clk_256()	TCCR1B|=(1<<CS12)|(0<<CS11)|(0<<CS10)
#define mTimer1Run_Clk_1024()	TCCR1B|=(1<<CS12)|(0<<CS11)|(1<<CS10)
#define mTimer1Clear()			TCNT1H=0;TCNT1L=0												//  pri zapisu do TCNTn se nejdrive zapisuje do TCNTnH a az pote do TCNTnL, pri cteni naopak
#define mTimer1IntOvf_Enab()	TIMSK1|=(1<<TOIE1)												//  for normal mode
#define mTimer1IntOvf_Clear()	TIFR1|=(1<<TOV1)												//  for normal mode
#define mTimer1IntOvf_Dis()		TIMSK1&=~(1<<TOIE1)												//  for normal mode
#define mTimer1CompRegA_Upd(x)	OCR1AH=((uint8_t)(x>>8));OCR1AL=((uint8_t)x)
#define mTimer1IntOcrA_Enab()	TIMSK1|=(1<<OCIE1A)
#define mTimer1IntOcrA_Clear()	TIFR1|=(1<<OCF1A)
#define mTimer1IntOcrA_Dis()	TIMSK1&=~(1<<OCIE1A)
#define mTimer1CompRegB_Upd(x)	OCR1BH=((uint8_t)(x>>8));OCR1BL=((uint8_t)x)
#define mTimer1IntOcrB_Enab()	TIMSK1|=(1<<OCIE1B)
#define mTimer1IntOcrB_Clear()	TIFR1|=(1<<OCF1B)
#define mTimer1IntOcrB_Dis()	TIMSK1&=~(1<<OCIE1B)

/************************************************************************/
/* UART functions                                                       */
/************************************************************************/


#endif /* UART_DRV_H_ */