www.pudn.com > IrDA.rar > irmem.c


/*
  Copyright (C) 2002-2003 Gerd Rausch, BlauLogic (http://blaulogic.com)
  All rights reserved.

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

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

  2. 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.

  3. Except as contained in this notice, neither the name of BlauLogic
     nor the name(s) of the author(s) may be used to endorse or promote
     products derived from this software without specific prior written
     permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHOR(S) OR BLAULOGIC BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

#define IRMEM_RECEIVE_ATTEMPTS	60 /* 30 seconds */

#define IRMEM_EEPROM_NAME_LEN	0
#define IRMEM_EEPROM_DATA_SIZE	2
#define IRMEM_EEPROM_NAME_START	4

#if E2END+1=size)
    len=size-1;

  if(len>0)
    eeprom_read_block(name, IRMEM_EEPROM_NAME_START, len);

  name[len]=0;

  return len;
}

static int16_t get_data_cb(uint8_t *data, uint16_t size, void *user_data)
{
  Parse_Context *context_p=(Parse_Context *)user_data;
  uint16_t n, addr;

  n=eeprom_rw(IRMEM_EEPROM_DATA_SIZE);
  if(n==0xFFFF)
    /* EEPROM is still unitialized */
    return -1;
  n-=context_p->data_offset;
  if(n>size)
    n=size;

  if(n>0) {
    addr=eeprom_rw(IRMEM_EEPROM_NAME_LEN);
    if(addr==0xFFFF)
      /* EEPROM is still unitialized */
      return -1;
    addr+=IRMEM_EEPROM_NAME_START+context_p->data_offset;
    
    eeprom_read_block(data, addr, n);
    context_p->data_offset+=n;
  }

  return n;
}

static uint8_t put_name_cb(char *name, void *user_data)
{
  Parse_Context *context_p=(Parse_Context *)user_data;
  uint16_t n;
#ifndef IRMEM_SLOW_EEPROM
  uint16_t i;
#endif

  n=strlen(name);
  if(n>E2END+1-IRMEM_EEPROM_NAME_START)
    n=E2END+1-IRMEM_EEPROM_NAME_START;

  context_p->name_len=n;

#ifdef IRMEM_SLOW_EEPROM
  memcpy(context_p->tmp_eeprom+IRMEM_EEPROM_NAME_START, name, n);
#else
  for(i=0; iname_len-
    context_p->data_offset;
  if(n>size)
    n=size;

  addr=IRMEM_EEPROM_NAME_START+context_p->name_len+context_p->data_offset;
#ifdef IRMEM_SLOW_EEPROM
  memcpy(context_p->tmp_eeprom+addr, data, n);
#else
  for(i=0; idata_offset+=n;

  return 1;
}

int main(void)
{
  IrLAP_Context irlap_ctx;
  Parse_Context parse_ctx;
  uint8_t attempt;
#ifdef IRMEM_SLOW_EEPROM
  uint16_t addr, addr_limit;
#endif

  /* initialize IrDA stack */
  irphy_reset();
  irlap_init_context(&irlap_ctx);

  /* initialize button input */
  cbi(DDRD, DDD2);
  cbi(DDRD, DDD3);

  /* initialize LEDs */
  sbi(DDRD, DDD6);
  sbi(DDRD, DDD7);
  sbi(PORTD, PD6);
  sbi(PORTD, PD7);

  /* select level triggered interrupts */
  cbi(MCUCR, ISC00);
  cbi(MCUCR, ISC01);
  cbi(MCUCR, ISC10);
  cbi(MCUCR, ISC11);

  /* enable interrupts */
  sbi(GIMSK, INT0);
  sbi(GIMSK, INT1);

  for(;;) {
    /* power down until button is pressed */
#ifdef SM0
#ifdef EMCUCR
    cbi(EMCUCR, SM0);
#else /* !EMCUCR */
    cbi(MCUCR, SM0);
#endif
    sbi(MCUCR, SM1);
#ifdef SM2
    cbi(MCUCR, SM2);
#endif
#else /* !SM0 */
    sbi(MCUCR, SM);
#endif
    sbi(MCUCR, SE);

    sei();

    __asm__ __volatile__ ("sleep");

    cli();

    cbi(MCUCR, SE);

    if(bit_is_clear(PIND, PIND2)) {
      /* send out EEPROM content */

      if(eeprom_rw(IRMEM_EEPROM_NAME_LEN)==0xFFFF)
	/* EEPROM is uninitialized */
	continue;

      cbi(PORTD, PD6);

      parse_ctx.data_offset=0;
      irobex_send(&irlap_ctx, 0, get_name_cb, get_data_cb, &parse_ctx);

      sbi(PORTD, PD6);

      continue;
    }

    if(bit_is_clear(PIND, PIND3)) {
      /* receive and store into EEPROM */

      cbi(PORTD, PD7);

      for(attempt=0; attempt>8);
	eeprom_wb(IRMEM_EEPROM_DATA_SIZE, parse_ctx.data_offset);
	eeprom_wb(IRMEM_EEPROM_DATA_SIZE+1, parse_ctx.data_offset>>8);

#ifdef IRMEM_SLOW_EEPROM
	addr=IRMEM_EEPROM_NAME_START;
	addr_limit=IRMEM_EEPROM_NAME_START+parse_ctx.name_len+parse_ctx.data_offset;
	while(addr