/*
Copyright 2006, Freie Universitaet Berlin. All rights reserved.

These sources were developed at the Freie Universität Berlin, Computer
Systems and Telematics group.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
 
- Neither the name of Freie Universitaet Berlin (FUB) nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

This software is provided by FUB and the contributors on an "as is"
basis, without any representations or warranties of any kind, express
or implied including, but not limited to, representations or
warranties of non-infringement, merchantability or fitness for a
particular purpose. In no event shall FUB or contributors be liable
for any direct, indirect, incidental, special, exemplary, or
consequential damages (including, but not limited to, procurement of
substitute goods or services; loss of use, data, or profits; or
business interruption) however caused and on any theory of liability,
whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software, even
if advised of the possibility of such damage.

This implementation was developed by the CST group at the FUB.

For documentation and questions please use the web site
http://scatterweb.mi.fu-berlin.de and the mailinglist
scatterweb@lists.spline.inf.fu-berlin.de (subscription via the Website).
Berlin, 2006
*/

#include <ScatterWeb.System.h>
#include "ScatterWeb.CC1020.internal.h"
#include "ScatterWeb.CC1020.h"

#define PDO            (P2IN & 0x01)                    // PDI (Data in) is on P21
#define PSEL_ON        do { P3OUT &=  ~0x01; } while(0) // PSEL is on P30 and low active
#define PSEL_OFF       do { P3OUT |=   0x01; } while(0)
#define PCLK_HIGH      do { P2OUT |=  0x08; }  while(0)
#define PCLK_LOW       do { P2OUT &= ~0x08; }  while(0)
#define PDI_HIGH       do { P2OUT |=  0x02; }  while(0) // PDO (Data out) is on P22
#define PDI_LOW        do { P2OUT &= ~0x02; }  while(0)

void CC1020_writeRegister(enum cc1020_register addr, uint8_t adata) {
  char  BitCounter;
  unsigned char Data ;
  PSEL_OFF;
  Data = (uint8_t)addr << 1;
  PSEL_ON;
  // Send address bits 
  for (BitCounter=0;BitCounter<7;BitCounter++) {
    nop();         // Delay50ns();
  	PCLK_LOW;
    nop();         // Delay50ns();
    if(Data & 0x80)  PDI_HIGH;
    else PDI_LOW;
    Data=Data<<1;
    PCLK_HIGH;
  }
  // Send read/write bit 
  // Ignore bit in data, always use 1 
  nop();           // Delay50ns();
  PCLK_LOW;
  PDI_HIGH;
  nop();           // Delay50ns();
  PCLK_HIGH;
  nop();           // Delay50ns();
  PCLK_LOW;
  Data=adata ;    
  // Send data bits 
  for (BitCounter=0;BitCounter<8;BitCounter++) {
    nop();         // Delay50ns();
  	PCLK_LOW;
    nop();         // Delay50ns();
    if(Data & 0x80) PDI_HIGH;
    else PDI_LOW;
    Data=Data<<1;
    PCLK_HIGH;
  }
  nop();           // Delay50ns();
  PCLK_LOW;
  nop();           // Delay50ns();
  PSEL_OFF;
}

uint8_t CC1020_readRegister(uint8_t addr) {
  char BitCounter;
  unsigned char Data = 0;
  PSEL_OFF; 
  Data=addr << 1;
  PSEL_ON;
  nop();           // Delay50ns();
  // Send address bits 
  for (BitCounter=0;BitCounter<7;BitCounter++) {
    nop();           // Delay50ns();
  	PCLK_LOW;
    nop();           // Delay50ns();
    if(Data & 0x80)  PDI_HIGH;
    else PDI_LOW;
    Data=Data<<1;
    PCLK_HIGH;
  }
  // Send read/write bit 
  // Ignore bit in data, always use 0 
  nop();             // Delay50ns();
  PCLK_LOW;
  PDI_LOW;
  nop();             // Delay50ns();
  PCLK_HIGH;
  nop();             // Delay50ns();
  PCLK_LOW;
  // Receive data bits       
  for (BitCounter=0;BitCounter<8;BitCounter++) {
    nop();           // Delay50ns();
  	PCLK_HIGH;
    nop();           // Delay50ns();
    Data=Data<<1;
    if (PDO) Data++; // Data = Data | PDO ;
    nop();           // Delay50ns();
    PCLK_LOW;
  }
  nop();             // Delay50ns();
  PSEL_OFF; 
  return Data;
}
