/* OK teste le 10/06/2011 sur 64MC802 								*/
/* ATTENTION: adapter InitClock2() choix quartz- choix TCy			*/
/* ATTENTION: adapter InitPORT_AND_PIN() en fct de votre cablage	*/

#include <p33FJ64MC802.h>
#include <stdio.h>
#include <timer.h>
#include <uart.h>
#include "delay.h"

#define FCY (80000000/2)
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16) - 1

//config oscillateur
// demarrage RC puis switch sur quart+pll	
_FOSCSEL(FNOSC_FRC); 		
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF  & POSCMD_XT);	// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
													// OSC2 Pin Function: OSC2 is Clock Output
													// Primary Oscillator Mode: XT Crystal
_FWDT(FWDTEN_OFF); 


void __attribute__ ((interrupt, no_auto_psv)) _U1RXInterrupt(void) 
{	char c;

	c = U1RXREG;
	IFS0bits.U1RXIF = 0;
	while(U1STAbits.UTXBF); //attend buffer vide
	U1TXREG = c; // ECHO DEBUG envoi sur TX
}
void InitPORT_AND_PIN() 
{  // depend du schema de la carte
	_U1RXR=3;	                    //RX sur RP3 autre ecriture RPINR0bits.INT1R=0
	_RP4R=3;                        //TX sur RP4

	TRISBbits.TRISB3=1;     			// RX pin sur RP3/RB3 (broche 7)
	AD1PCFGLbits.PCFG5=1;			// RP3/RB3/AN5  en digital

	TRISBbits.TRISB4=0; 				// TX pin sur RP4/RB4 rq:pas de AN

	TRISBbits.TRISB0=0;				// led vie
	AD1PCFGLbits.PCFG2=1;			// RB0/AN2 en digital
}

void InitClock2() 
{   /*Quartz 10 Mhz => coef x8 */
	PLLFBD = 30;	//  ,M = 32
	CLKDIVbits.PLLPOST = 0;	// N1 = 2
	CLKDIVbits.PLLPRE = 0;	// N2 = 2
	OSCTUN = 0;
	RCONbits.SWDTEN = 0;
	// Clock switch to incorporate PLL
	__builtin_write_OSCCONH(0x03);		// Initiate Clock Switch to 
													// FRC with PLL (NOSC=0b001)
	__builtin_write_OSCCONL(0x01);		// Start clock switching
	while (OSCCONbits.COSC != 0b011);	// Wait for Clock switch to occur	
	while(OSCCONbits.LOCK != 1) {};
}

void InitUART1()
{
	U1MODEbits.UARTEN=0;             // desactive l'UART1  
	
	U1MODEbits.USIDL = 0;	// Bit13 Continue in Idle
	U1MODEbits.IREN = 0;	// Bit12 No IR translation
	U1MODEbits.RTSMD = 0;	// Bit11 Simplex Mode
	U1MODEbits.UEN = 0;		// Bits8,9 TX,RX enabled, CTS,RTS not
	U1MODEbits.WAKE = 0;	// Bit7 No Wake up (since we don't sleep here)
	U1MODEbits.LPBACK = 0;	// Bit6 No Loop Back
	U1MODEbits.ABAUD = 0;	// Bit5 No Autobaud (would require sending '55')
	U1MODEbits.URXINV = 0;	// Bit4 IdleState = 1  (for dsPIC)
	U1MODEbits.BRGH = 0;	// Bit3 16 clocks per bit period
	U1MODEbits.PDSEL = 0;	// Bits1,2 8bit, No Parity
	U1MODEbits.STSEL = 0;	// Bit0 One Stop Bit

	U1STAbits.URXISEL = 0;

	U1BRG = BRGVAL;

	IPC2bits.U1RXIP = 4;	        // Mid Range Interrupt Priority level, no urgent reason
	
	_U1TXIF = 0;	// Clear the Transmit Interrupt Flag
	_U1TXIE = 0;	// Disable Transmit Interrupts
	_U1RXIF = 0;	// Clear the Recieve Interrupt Flag
	_U1RXIE = 1;	// ENABLE Recieve Interrupts

	U1MODEbits.UARTEN = 1;          //Active l'uart1
	
	U1STAbits.UTXEN=1;              //Active la transmision
	Delay_Us(Delay200uS_count); // 200us avant 1er envoi
	
}

void UART_puts(char * BUFF)
{
	unsigned int i=0;
	while (BUFF[i]!='\0') // tant que fin tableau non atteinte
	{
		while(U1STAbits.UTXBF); //attend buffer vide
		U1TXREG = BUFF[i]; // envoi sur TX
		i++;
	}
}

unsigned char BUFF[50]="test";

int main (void)

{	char c=0;

	InitClock2() ; //init oscillateur

	InitPORT_AND_PIN(); // init broche remapable
	InitUART1(); // init broche remapable

	// exemple 1 : appel à la fct  UART_puts
	//passage du tableau à envoyer
	UART_puts(BUFF);
	///////////////////////////


	for (;;)
	{
		
	
	}
}