/*****************************************************************************************
*
* File:		em4095.h
* Author:	Roman Mego
* Date:		8. April 2011
*
* Brief:	Definitions and macros for RFID reader EM4095
*			On migrating to another platform or circiuit, change pin connection,
*			registers, wait macro and capture edge macro
*
*****************************************************************************************/

#define _XTAL_FREQ 4000000	// oscillator frequency (used for __delay_ms())

/*****************************************************************************************
* Pin connection and register definition
*****************************************************************************************/

#define MOD_pin		4		// pin number
#define MOD_port	PORTB	// port register
#define MOD_dir		TRISB	// port direction register

#define	SHDN_pin	5		// pin number
#define	SHDN_port	PORTB	// port register
#define	SHDN_dir	TRISB	// port direction register

#define	CLK_pin		0		// pin number
#define	CLK_port	PORTB	// port register
#define	CLK_dir		TRISB	// port direction register

#define	DEMOD_pin	3		// pin number
#define	DEMOD_port	PORTB	// port register
#define	DEMOD_dir	TRISB	// port direction register

#define TIM_REG		TMR1	// timer register
#define CAP_REG		CCPR1	// capture register

/*****************************************************************************************
* Macros
*****************************************************************************************/

#define WAIT_35_MS			__delay_ms(35)			// delay 35 ms

#define CAP_EDGE_TOGGLE		CCP1CON ^= 0x01			// change capture edge
#define CAP_EDGE_RISING		CCP1CON = 0x05			// capture on rising edge

// High level on MOD modulates the antenna
#define	RFID_MOD_ON		MOD_port |= 1 << MOD_pin	// set high level on pin
#define RFID_MOD_OFF	MOD_port &= ~(1 << MOD_pin)	// sel low level on pin


/*****************************************************************************************
* Functions
*****************************************************************************************/

void init_rfid(void);			// initialize ports on MCU for EM4095
void rfid_on(void);				// return from sleep mode
void rfid_off(void);			// turn off modulation and set EM4095 to sleep mode
void rfid_reset(void);			// reset input buffer and counters
char check_rfid_data(void);		// check data in buffer
char rfid_read(char *code);		// returns string containing code and checksum
void rfid_interrupt(void);		// call in capture interrupt
void buffer_add_bit(void);		// add next bit to buffer
