/*
 * $Id: radio_rx.c 208 2005-10-02 22:38:34Z anders $
 * 
 * Copyright (C) 2005 Anders Dubgaard <anders@dubgaard.net>
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <avr/signal.h>
#include <stdio.h>
#include "radio.h"
#include "protocol.h"
#include "cc1000.h"
#include "cc1000registers.h"
#include <uart.h>

/* Base / receiver */
enum rx_states { FIND_PRE, FIND_SOF, GET_PAYLOAD, PKG_REC };
volatile uint8_t rx_state = FIND_PRE;  /* Start with finding the preamble */
volatile uint16_t bitbin = 0;   /* 16bit to easier catch Preamble and SOF */
volatile uint8_t rx_byte_cnt = 0;
volatile uint8_t rx_bit_cnt = 0;
volatile uint8_t package_received = false;
volatile uint8_t MAX_SPACER_BITS = 24; /* atleast 3 * 8bits */
volatile uint8_t spacer_bits = 0;

/* Receiver-interrupt, with states.
 * Uses a 16 bit value to store the incoming bits which makes it easier
 * to find the preamble and the SOF. Used with a cast to collect
 * single bytes of data.
 * If a 1 byte preamble is detected and no SOF has been found after another
 * 16 bits, the routine starts over.
 */
SIGNAL(SIG_INTERRUPT2) {  /* Receive */
  bitbin <<= 1;  /* make room for next bit, MSB first */
  bitbin += (CC1000_PINS & 1<<DIO)? 1: 0;  /* set bit */
  
  if (rx_state == FIND_PRE) { /* Search for Preamble */
    if ((uint8_t) bitbin == 0x55 ) { /* preamble is 0x5555, 0x55 is a substr */
      rx_state = FIND_SOF;
      spacer_bits = 0;
    }
  }
  else if (rx_state == FIND_SOF) { /* Search for SOF */
    if (bitbin == SOF ) {
      rx_state = GET_PAYLOAD; /* the following bytes are the data */
    }
    else {
      spacer_bits += 1;
      if(spacer_bits > MAX_SPACER_BITS) {
        rx_state = FIND_PRE;
      }
    }
  }
  else if (rx_state == GET_PAYLOAD) { /* Collect the data */
    if (rx_byte_cnt < PAYLOAD_LEN) {  /* - but only a certain amount */
      rx_bit_cnt++;
      if (rx_bit_cnt > 7) {  /* Now we have a byte w/ data */
        package[rx_byte_cnt + HEADER_LEN] = (uint8_t) bitbin; 
        rx_byte_cnt++;
        rx_bit_cnt = 0;
      }
    }
    else { /* Start over, reset counters */
      rx_state = FIND_PRE;
      bitbin = 0;
      rx_byte_cnt = 0;
      rx_bit_cnt = 0;
      disable_radio_int();     
      package_received = true;  /* Tell others about the new data      */
    }
  }
}

/* Initialise the radio chip.
 * The CC1000 register variables in ConfigureCC1000() have been
 * generated with smartrf2header.pl from the output data of
 * Chipcon's SmartRF(C) programme.
 *
 * The step wise procedure below is described in Chipcon's AN009.
 */
void radio_init(void) {
	CC1000_PDIR |= 1<<PCLK | 1<<PALE | 1<<PDATA;
	ResetCC1000();

	ConfigureCC1000(rx_numregs, rx_regs);
	setbit(EICRA, ISC20);      // interrupt on rising edge
	setbit(EICRA, ISC21);      // interrupt on rising edge
	
	CalibrateCC1000();

	clrbit(CC1000_PDIR, DIO);
	clrbit(CC1000_PDIR, DCLK);
}

/* Starts the receiving int. routine.
 *
 */
void receive_package(void) {
	package_received = false;
  enable_radio_int();
	while(!package_received) {
		;
  }
}

void disable_radio_int(void) {
	clrbit(EIMSK, INT2);         /* disable interrupt on INT2 */
}

void enable_radio_int(void) {
	setbit(EIMSK, INT2);         /* enable interrupt on INT2 */
}

