// /usr/local/share/sdcc/include/pic16 == headerit

#include "pic18fregs.h"

#ifndef LED_TRIS
#define LED_TRIS  TRISBbits.TRISB4
#endif

#ifndef LED_PIN
#define LED_PIN   PORTBbits.RB4
#endif


code char at 0x300000 CONFIG1L = 0x20; // USBDIV=1, CPUDIV=00, PLLDIV = 000

// 	PLLDIV	PLL freq		XTAL/OSC (for PLL freq 96 MHz)
// 	000		OSC x 24 / 1 	4 MHz        <= THIS
// 	001		OSC x 24 / 2 	8 MHz
// 	010		OSC x 24 / 3    12 MHz
// 	011		OSC x 24 / 4	16 MHz
// 	100		OSC x 24 / 5    20 MHz
// 	101		OSC x 24 / 6    24 MHz
// 	110		OSC x 24 / 10   40 MHz
// 	111		OSC x 24 / 12   48 MHz


// 	USBDIV		USB clock (requires UCFG:FSEN=1, FSEN=0, USB clock = CPU clock
//	0			XTAL/OSC
//	1			PLL / 2       			<= THIS 48 MHz => 12 mb/s USB hi-speed

// 	CPUDIV     	CPU clock
// 	00  		PLL / 2					<= THIS 48 MHz ???
// 	01  		PLL / 3
// 	10  		PLL / 4
// 	11  		PLL / 6


code char at 0x300001 CONFIG1H = 0x0E; // IESO=0, FCMEN=0, FOSC = 1110

// FOSC
// 1110 		HS oscillator, PLL enabled (HSPLL)

code char at 0x300002 CONFIG2L = 0x20; // Brown out off, PWRT On

code char at 0x300003 CONFIG2H = 0x00; // WDT off

code char at 0x300004 CONFIG3L = 0xff; // Unused configuration bits

code char at 0x300005 CONFIG3H = 0x81; // Yes MCLR, PORTB digital, CCP2 - RC1

code char at 0x300006 CONFIG4L = 0x80; // ICD off, ext off, LVP off, stk ovr off

code char at 0x300007 CONFIG4H = 0xff; // Unused configuration bits

code char at 0x300008 CONFIG5L = 0xff; // No code read protection

code char at 0x300009 CONFIG5H = 0xff; // No data/boot read protection

code char at 0x30000A CONFIG6L = 0xff; // No code write protection

code char at 0x30000B CONFIG6H = 0xff; // No data/boot/table protection

code char at 0x30000C CONFIG7L = 0xff; // No table read protection

code char at 0x30000D CONFIG7H = 0xff; // No boot table protection




void delay_ms(long ms)
{
    long i;

    while (ms--)
        for (i=0; i < 330; i++)
            ;
}

#include "stdio.h"
#include "usart.h"

void stdio_init()
{
    usart_open(
        USART_TX_INT_OFF
            & USART_RX_INT_OFF
            & USART_BRGH_LOW
            & USART_ASYNCH_MODE
            & USART_EIGHT_BIT,
        77 // = SPBRG, 9600 baud @ 48 MHz CPU clock
        );
    // baudrate = FOSC/(16*(SPBRG+1)) for USART_BRGH_HIGH
    // baudrate = FOSC/(64*(SPBRG+1))) for USART_BRGH_LOW
    stdout = STREAM_USART;
}



// _XINST_OFF_4L to the __CONFIG4L initialisation. DISABLE extended instruction set

static void Intr(void) interrupt 1 // high priority interrupt vector == only interrupt vector if priorit not enabled
{
	INTCONbits.TMR0IF = 0;	// Clear flag
	//PORTA++;	// Toggle the state of the LSB of the port bits


	PORTBbits.RB4 = !PORTBbits.RB4;

//	GIE=1;		// Globally enable interrupts.
}



void main2()
{
    stdio_init();
    printf("hello, world\n");
}


void main()
{
	OSCCON=0x70;
    // set pin to output
    LED_TRIS = 0;
    //LED_PIN = 0;

    stdio_init();

    T0CONbits.T0CS = 0;	// Internal instruction clock as source for timer 0
    T0CONbits.PSA = 0;	// Assign prescaler to Timer 0.
    T0CONbits.T08BIT=1; //  8 bit
    T0CONbits.T0PS2 = 0;	// Set up prescaler to 1:256.
    T0CONbits.T0PS1 = 0;
    T0CONbits.T0PS0 = 0;
    T0CONbits.TMR0ON=1;
    T0CONbits.T0SE= 1 ; // high to low (no effect when timer clock coming from instruction clock...)

	INTCON = 0;	// Clear interrupt flag bits. ????
	INTCONbits.GIE = 1;	// Enable all interrupts.

	INTCONbits.TMR0IE = 1;	// Set Timer 0 to 0.
	INTCONbits.PEIE = 1;	// Enable peripheral interrupts.
	TMR0L = 0;	// Clear
	TMR0H = 0;	// Clear



     for (;;)
    {
 //     LED_PIN = 0;
      delay_ms(100);
  //    LED_PIN = 1;
      delay_ms(100);
    printf("hello, world\n",usart_getc());
  // while (!usart_drdy());
   //     printf("got %d\n",usart_getc());

    }
}

