// --------------------------------------------------------------------------------------------
// core definition designed for the Smart-Its Core
// MCU: PIC16F876

// --------------------------------------------------------------------------------------------
// last modified: 09.03.2003 

// --------------------------------------------------------------------------------------------
// COPYRIGHT (c) 2002, 2003 by Albrecht Schmidt - http://www.comp.lancs.ac.uk/~albrecht/
// Lancaster University, http://www.comp.lancs.ac.uk/
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// --------------------------------------------------------------------------------------------
// developed/improved for various prototypes 
// in the Smart-Its Project (http://www.smart-its.org) and
// in the Equator IRC (http://www.equator.ac.uk)

// --------------------------------------------------------------------------------------------
// compiled with
// CCS PCM C Compiler, Version 3.080

// processor
#include "16F876.H" 
#device PIC16F876 *=16 ADC=10 

// set fuses - watchdog timer is ON
#fuses HS, WDT, NOBROWNOUT, NOPROTECT, NOLVP 

// 20MHz crystal used
#use delay(clock=20000000) 
// serial line setup
#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7) 

// connections of the BIM module
#define RF_TX_PIN PIN_C3 
#define RF_RX_PIN PIN_C0 
#define RF_CD_PIN PIN_B0 
#define RF_TX_ENABLE_PIN PIN_C2 
#define RF_RX_ENABLE_PIN PIN_C1 
// ignore old packets
#define FORCE_NEW_PROTOCOL 1  
// run at 19.2k
#define RF_SPEED 19200 

// I2C Bus
#define i2c_sda PIN_C5 
#define i2c_scl PIN_C4 
#use i2c(master, sda=i2c_sda, scl=i2c_scl,fast,RESTART_WDT) 

// include the code for the RF module
#include "bim2rf3.c" 
// include the code for the FRAM
#include "FR24c64.c" 

// serial line setup
#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7) 

// on board led on
void led_on()
{
	output_low(PIN_A4);
	restart_wdt();
}
   
// on board led off
void led_off()
{
	output_high(PIN_A4);
	restart_wdt();
}

// switch leds to show that the device is living...
void boot_sign()
{
	led_on();		   
	delay_ms(400);
	restart_wdt();
   	led_off();
   	delay_ms(400);
   	restart_wdt();
   	led_on();		   
	delay_ms(400);
	restart_wdt();
   	led_off();
}

// setup the core - fram, watchdog timer, a give a sign
int init_core()
{
//	init_fram();
//	init_ext_eeprom();
	init_ext_fram();
   	setup_wdt(WDT_2304MS);
   	printf("core initialized...\r\n");
	boot_sign();
   	
}  

// setup the core - fram, watchdog timer, no sign
int init_core_silent()
{
//	init_fram();
//	init_ext_eeprom();
	init_ext_fram();
   	setup_wdt(WDT_2304MS);
}