//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*

CCS compiler PCWHD


July- 2008

www.techdesign.be
info@techdesign.be


*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 
#include <33FJ256GP506.h>			// device selection 
//#include <P33FJ256GP506.inc>
#device *=16

/*
//#EXPORT(HEX)
#define MAX_FLASH getenv("PROGRAM_MEMORY") //for PICs with 32KB of mem 
#define LOADER_SIZE 0xff //tinybld size + a bit more (200 bytes is enough) 
#org MAX_FLASH-LOADER_SIZE , MAX_FLASH-1 void boot_loader(void) {} 
*/

#include <string.h>
#include <stdlib.h>

//#org 0x00200,0x0201 void dummy(){}

//////// Program memory: 87552x24  Data RAM: 16384  Stack: 31
//////// I/O: 61   Analog Pins: 18
//////// Fuses: ICS0,ICS1,ICS2,ICS3,JTAG,NOJTAG,COE,NOCOE,DEBUG,NODEBUG
//////// Fuses: NOPUT,PUT2,PUT4,PUT8,PUT16,PUT32,PUT64,PUT128,WPOSTS1
//////// Fuses: WPOSTS2,WPOSTS3,WPOSTS4,WPOSTS5,WPOSTS6,WPOST7,WPOSTS8
//////// Fuses: WPOSTS9,WPOSTS10,WPOSTS11,WPOSTS12,WPOSTS13,WPOSTS14
//////// Fuses: WPOSTS15,WPOSTS16,WPRES128,WPRES32,WINDIS,NOWINDIS,WDT,NOWDT
//////// Fuses: EC,XT,HS,NOPR,OSCIO,NOOSCIO,CKSFSM,NOCKSFSM,FRC_PLL,PR
//////// Fuses: PR_PLL,LP,LPRC,FRC,TEMP,NOTEMP,IESO,NOIESO,WRT,NOWRT
//////// Fuses: PROTECTH,NOPROTECT,PROTECTS,WRTSS,NOWRTSS,SSSH,SSSS,NOSSS
//////// Fuses: NORSS,RSS,WRTB,NOWRTB,BSSH,BSSS,NOBSS,RBS,NORBS,CKSNOFSM

/* from 048 hs40
*/

#FUSES NORBS,NOBSS,NOWRTB    			// 0           
#FUSES NORSS,NOSSS,NOWRTSS		// 2
#FUSES NOWRT,NOPROTECT					// 4
#FUSES PR,TEMP,IESO 					// 6
#FUSES HS,OSCIO 						// 8 
#FUSES WPOSTS16,WPRES32,NOWINDIS,NOWDT  // A 
#FUSES PUT128							// C
#FUSES ICS1,NOJTAG,NOCOE  				// E
    
#FUSES NODEBUG	//NOCKSFSM,
#FUSES RESERVED                 //Used to set the reserved FUSE bits 


//#rom 0xf80000 = {0xcf,0xcf,0x07,0xa1,0x83,0x5f,0xe7,0xe3}



/*
config __FBS, RBS_NO_RAM & BSS_NO_BOOT_CODE & BWRP_WRPROTECT_OFF
config __FSS, RSS_NO_SEC_RAM & SSS_NO_SEC_CODE & SWRP_WRPROTECT_OFF
config __FGS, GSS_OFF & GCP_OFF & GWRP_OFF
config __FOSCSEL, FNOSC_PRI & IESO_ON
config __FOSC, FCKSM_CSECME & OSCIOFNC_OFF & POSCMD_HS
config __FWDT, FWDTEN_OFF & WINDIS_OFF & WDTPRE_PR32 & WDTPOST_PS32768
config __FPOR, FPWRT_PWR2
config __FICD, BKBUG_ON & COE_ON & JTAGEN_OFF & ICS_PGD2
*/

#use delay(clock=40000000) 	// 40 MhZ 


#use rs232(UART2,baud=115200,stream=ser2)




#use standard_io(B)
#use standard_io(C)
#use standard_io(D)
#use standard_io(F)
#use standard_io(G)

/*
#use standard_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(F)
#use fast_io(G)
*/

#define rs232_2_en	PIN_B0


#define NOP #asm nop #endasm



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// declaration of global variables and routines


#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

unsigned int32 st_addr=0x2a800;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#int_default
default_isr() {

   fprintf(ser2,"Default interrupt\r\n");
	return 0;

}

#int_ext0
default_ext0() {

   fprintf(ser2,"ext0 interrupt\r\n");
	return 0;
}

#int_ext1
default_ext1() {

   fprintf(ser2,"ext1 interrupt\r\n");
	return 0;
}

#int_ext2
default_ext2() {

   fprintf(ser2,"ext2 interrupt\r\n");
	return 0;
}

#int_ext3
default_ext3() {

   fprintf(ser2,"ext3 interrupt\r\n");
	return 0;
}





#int_rda
void serial_isr() {
   int t;

   buffer[next_in]=getc(ser2);
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}

#define bkbhit (next_in!=next_out)

BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}




#zero_ram			// all ram variables to zero


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------
//#org 0x00200,0x00201 void dummy(){}
//

// dummy(){}
//#build(reset=0x200:0x200,interrupt=0x004:0x1ff)
//

//#build(memory=0x00000:0x2ab00)
//#build(reset=0x00200:0x00200)
//#build(reset=0x200)

//#build(reset=0x200:0x201)



//#rom 0x00000={1,2,3,4,5,6,7,8}

//#org 0x2a800,0x2ab00 auto=1
//
//
//#build (interrupt= 0x004:0x1fe)

//#define LOADER_ISR 0x104
//#build(interrupt=LOADER_ISR)

//#define LOADER_res 0x200
//#build(reset=LOADER_res)

//#asm .org #st_addr #endasm

#org 0x200,0x1000 void main()
{
//
//#build(reset=0x00200)
//write_configuration_memory(cdata,1);
//
//goto_address(0x2a800);



SETUP_ADC_PORTS(NO_ANALOGS);

// set port directions   // o= output   // i=input
SET_TRIS_B(0x4002);      // oioo oooo oooo ooio   
SET_TRIS_C(0x4080);      // oioo oooo iooo oooo
SET_TRIS_D(0xff00);      // iiii iiii oooo oooo   
SET_TRIS_F(0x14);      	 // oooi oioo
SET_TRIS_G(0x1200);      // oooi ooio oooo oooo

      
//setup_spi(SPI_SS_DISABLED); 
setup_spi2(SPI_SS_DISABLED); 

setup_wdt(WDT_OFF);   // needed in PLL mode, otherwise auto reset after approx. 9 min. ???? <--- todo: check
disABLE_INTERRUPTS(INT_RDA);
disaBLE_INTERRUPTS(INT_EXT0);
disable_interrupts(INTR_NORMAL);
disable_interrupts(INTR_GLOBAL);


OUTPUT_BIT(rs232_2_en,0);


///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////
while(true){ 		// BOF while(true)



delay_ms(100);
fprintf(ser2," hi there! ");
fprintf(ser2," ** hello world ** ");
			} 		// EOF while(true)


} 	// EOF main 
/////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////



// The End!
//

