/*
	Ejemplo para RS-485
	
*/


#include "mcf5213.h"


void init_clock_config (void);
void init_pto_UB(void);
void init_interrupt_controller (void);
void init_uart_1(void);

 int main (void) 
	{
		init_clock_config ();			
		init_pto_UB();	
		init_uart_1();			
		init_interrupt_controller ();

		asm_set_ipl(0);				//Habilita todas las interrupciones

		while (1)
		{ 
			
		}	
		return 0;			
	}


void init_clock_config (void)
	{
		// Clock source is 7.3728 MHz external oscillator
	    	// Clock mode: External clock mode
		// Processor/Bus clock frequency = 7.37 MHz

	    	MCF_CLOCK_SYNCR = 0;				// Utiliza el clock del reloj externo
	}



void init_pto_UB(void)
	{
 	 	MCF_GPIO_DDRUB = 0;
									 //habilita los pines UB para Tx y Rx
		MCF_GPIO_PUBPAR = MCF_GPIO_PUBPAR_PUBPAR1(0x1) |  
                     		MCF_GPIO_PUBPAR_PUBPAR0(0x1);

}


void init_interrupt_controller (void)
{
	
    MCF_INTC_ICR14 = MCF_INTC_ICR_IL(0x6) |	// Nivel
                     MCF_INTC_ICR_IP(0x4);	// Prioridad
    MCF_INTC_IMRL = MCF_INTC_IMRL_MASK31 |	// Enmascara todas las fuentes de Interrupción
                    MCF_INTC_IMRL_MASK30 |	// excepto la fuente No 14
                    MCF_INTC_IMRL_MASK29 |
                    MCF_INTC_IMRL_MASK28 |
                    MCF_INTC_IMRL_MASK27 |
                    MCF_INTC_IMRL_MASK26 |
                    MCF_INTC_IMRL_MASK25 |
                    MCF_INTC_IMRL_MASK24 |
                    MCF_INTC_IMRL_MASK23 |
                    MCF_INTC_IMRL_MASK22 |
                    MCF_INTC_IMRL_MASK21 |
                    MCF_INTC_IMRL_MASK20 |
                    MCF_INTC_IMRL_MASK19 |
                    MCF_INTC_IMRL_MASK18 |
                    MCF_INTC_IMRL_MASK17 |
                    MCF_INTC_IMRL_MASK16 |
                    MCF_INTC_IMRL_MASK15 |
								//EL 14 CORRESPONDE A UART1
	              MCF_INTC_IMRL_MASK13 |								
                    MCF_INTC_IMRL_MASK12 |
                    MCF_INTC_IMRL_MASK11 |
                    MCF_INTC_IMRL_MASK10 |
                    MCF_INTC_IMRL_MASK9  |
                    MCF_INTC_IMRL_MASK8  |
                    MCF_INTC_IMRL_MASK7  |
                    MCF_INTC_IMRL_MASK6  |
                    MCF_INTC_IMRL_MASK5  |
                    MCF_INTC_IMRL_MASK4  |
                    MCF_INTC_IMRL_MASK3  |
                    MCF_INTC_IMRL_MASK2  |
                    MCF_INTC_IMRL_MASK1; 			
}

void init_uart_1(void)
	{
    		// Configure UART 0 (first UART channel)
    		// Baud Rate = 9600
    		// 8 data bits, 1 stop bit, No parity
    		// /CTS input has no effect on the transmitter
    		// /RTS output is not affected by receiver or transmitter
    		// UART 1: Reset transmitter, receiver and mode register pointer

    		MCF_UART1_UCR = 	MCF_UART_UCR_MISC(0x3);		//Reset Transmisión
    		MCF_UART1_UCR = 	MCF_UART_UCR_MISC(0x2);		//Reset Recepción
    		MCF_UART1_UCR = 	MCF_UART_UCR_MISC(0x1);		//Apunta a UMR1
    		MCF_UART1_UMR = 	MCF_UART_UMR_PM(0x2) |		//UMR1 Modo no paridad
                    		MCF_UART_UMR_BC(0x3);		//UMR1 Bits por caracter = 8
    		MCF_UART1_UMR = 	MCF_UART_UMR_SB(0x7);		//UMR2 Bit de Stop = 1
    		MCF_UART1_UCSR =	MCF_UART_UCSR_RCS(0xd) |	//Clock de Recepcion = interno
                     		MCF_UART_UCSR_TCS(0xd);		//Clock de Transmision = interno
    		MCF_UART1_UBG2 = 	0x17;					//Baudios = 9600
    		MCF_UART1_UIMR = 	MCF_UART_UIMR_RXRDY_FU;		//Interrupcion Rx = 1 caracter

    		MCF_UART1_UCR = 	MCF_UART_UCR_TXC(0x1);		//Habilita transmisión
    		MCF_UART1_UCR = 	MCF_UART_UCR_RXC(0x1);		//Habilita recepción
    	}

 


 


