/***************************************************************************
 *   Copyright (C) 30.06.2004 by                                           *
 *   Matthias Kranz (matthias.kranz@ifi.lmu.de)                            *
 *   Paul Holleis (paul.holleis@ifi.lmu.de)                                *
 *                                                                         *
 *   header file for spm2-433-28 space port modem chip 433 28              *
 *   Last modified: 21.10.2004                                             *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 **************************************************************************/
/******************************************************************************
 * defines
 *****************************************************************************/
#ifndef __STD
  #include <std.h>
#endif

#ifndef __SPM243328
  #define __SPM243328
#endif

#ifndef SPM_SPEED
  /// SmartITs standard speed setting for reading/writing from/to the spm
  // alte geschwindingkeit!!! 
  #define SPM_SPEED 38400
#endif

#ifndef SPM_CONFIG_PIN
  /// SmartITs standard pin for entering the spm config mode
  #define SPM_CONFIG_PIN PIN_D1
#endif

#ifndef SPM_RESET_PIN 
  /// SmartITs standard pin for resetting the spm
  #define SPM_RESET_PIN PIN_C1
#endif

#ifndef SPM_DEFAULT_PIN
  /// SmartITs standard pin for default spm pin
  #define SPM_DEFAULT_PIN PIN_C0
#endif

#ifndef SPM_WAKEDTR_PIN
  /// SmartITs standard pin for spm wake dtr pin
  #define SPM_WAKEDTR_PIN PIN_C2
#endif


#ifndef SPM_RX_PIN
  /// SmartITs standard pin for reading from the spm
  #define SPM_RX_PIN PIN_B0
#endif

#ifndef SPM_TX_PIN
  /// SmartITs standard pin for writing to the spm
  #define SPM_TX_PIN PIN_C5
#endif

#ifdef NL
	#undef NL
	/// Newline when used with spm 433 28
  	#define NL \n\r
#endif

#use rs232(baud=STDSTREAM_SPEED, xmit=PIC_TX_PIN, rcv=PIC_RX_PIN,STREAM=STDSTREAM)
#use rs232(baud=SPM_SPEED, xmit=SPM_TX_PIN, rcv=SPM_RX_PIN,STREAM=SPMSTREAM)
/******************************************************************************
 * function signatures 
 *****************************************************************************/
void spm2_bootsign();

void spm2_remove_welcome();
void spm2_reset();
void spm2_default();
void spm2_configMode();
void spm2_standbyMode(); 
void spm2_init();
void spm_init();

/******************************************************************************
 * Code
 *****************************************************************************/

/**
 * Does the complete initialization needed before transmission!
 */
void spm_init()
{
  spm2_init();
  delay_ms(100);
  
  spm2_bootsign();
  delay_ms(100);

  spm2_default();
  
  spm2_standbyMode();
}

/**
 * After entering the spm2 chip setup, a welcome message is displayed. this
 * function removes this message from the input stream.
 */
void spm2_remove_welcomemessage()
{
	char a;
	while((a = fgetc(SPMSTREAM)) && (a != '>')) 
	{
  		fputc(a,STDSTREAM);
	}
}

/**
 * Resets the spm2 chip according to manual, chapter 2.2.
 * This is done via setting the SPM_RESET_PIN to low, waiting for 1 (one)
 * mikrosecond (in the code, were waiting 2 (two)) and then returning
 * the SPM_RESET_PIN to high again.
 */
void spm2_reset() 
{
	/// Reset is done by driving the the SPM_RESET_PIN to low 
  output_low(SPM_RESET_PIN);
  delay_us(2);
  /// and rising it immediately high again.
  output_high(SPM_RESET_PIN);
}

void spm2_default() 
{
	/// Entering the default mode is done by driving the SPM_DEFAULT_PIN to low
  output_low(SPM_DEFAULT_PIN);
  delay_us(2);
  /// doing a reset
  spm2_reset();
  delay_us(2);
  /// and rising it immediately high again.
  output_high(SPM_DEFAULT_PIN);
}

void spm2_configMode() 
{
	/// Entering the config mode is done by driving the SPM_CONFIG_PIN to low.
  output_low(SPM_CONFIG_PIN);
  delay_us(2);
}

void spm2_standbyMode() 
{
	/// Entering the standyby mode is done by rising the SPM_CONFIG_PIN
  output_high(SPM_CONFIG_PIN);
  delay_us(2);
  spm2_reset();
}

/**
 * Initializes the spm2-433-28 space port modem by
 * making sure all pins that are necessary have defined stats.
 */
void spm2_init() 
{
  /// 1. drive SPM_WAKEDTR_PIN to low.
  // no sleep! no energy save! do work!
  output_low(SPM_WAKEDTR_PIN);
  //force 9600 baud
  /// 2. drive SPM_DEFAULT_PIN to low.
  output_low(SPM_DEFAULT_PIN);
  /// 3. do a reset.
  spm2_reset();
  /// 4. drive SPM_DEFAULT_PIN to high.
  output_high(SPM_DEFAULT_PIN);
  //normal operation
  /// 5. drive SPM_CONFIG_PIN to high.
  output_high(SPM_CONFIG_PIN);
  //reset the SPM
  /// 6. do a reset.
  spm2_reset();
  //wait for the reset
  delay_ms(1);
}

/**
 * Gives a sign-of-life, e.g. useful for testing a first spm program.
 * Output is directet to STDOUT, SPMSTREAM and STDSTREAM. STDSTREAM is the 
 * serial line on e.g. the Smart It, the SPMSTREAM is the serial line
 * coming from the spm2-433-28 and STDOUT is what it ever may be.
 */
void spm2_bootsign() 
{ 
  int i;
	
  printf("\n\rbooting the spm2-433-28, make sure you can read from the \n\r");
  printf("serial line and the spm serial line the same output\n\r"); 
	
  fprintf(STDSTREAM, "\nbooting the spm (serial line output)."); 
  fprintf(SPMSTREAM, "\n\rbooting the spm (rf output).");   
  for(i=0;i<11;i++)
    { 
      fprintf(STDSTREAM, ".%i", (10-i)); 
      fprintf(SPMSTREAM, ".%i", (10-i)); 
    } 
  fprintf(STDSTREAM, "\n\r"); 
  fprintf(SPMSTREAM, "\n\r"); 
	
} 

