⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 irmem.c

📁 IRDA 1.0 标准协议源代码
💻 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 <string.h>#include <pgmspace.h>#include <eeprom.h>#include <io.h>#include <interrupt.h>#include <irphy.h>#include <irlap.h>#include <irobex.h>#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<IRMEM_EEPROM_NAME_START#error EEPROM storage is too small#endif#if defined(__AVR_ATmega8__)#define IRMEM_SLOW_EEPROM#endiftypedef struct Parse_Context {  uint16_t data_offset, name_len;#ifdef IRMEM_SLOW_EEPROM  uint8_t tmp_eeprom[E2END+1];#endif} Parse_Context;static int16_t get_name_cb(char *name, uint16_t size, void *user_data){  uint16_t len;  len=eeprom_rw(IRMEM_EEPROM_NAME_LEN);  if(len==0xFFFF)    /* EEPROM is still uninitialized */    return -1;  if(len>=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; i<n; i++)    eeprom_wb(IRMEM_EEPROM_NAME_START+i, name[i]);#endif  return 1;}static uint8_t put_data_cb(uint8_t *data, uint16_t size, void *user_data){  Parse_Context *context_p=(Parse_Context *)user_data;  uint16_t n, addr;#ifndef IRMEM_SLOW_EEPROM  uint16_t i;#endif  n=E2END+1-IRMEM_EEPROM_NAME_START-    context_p->name_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; i<n; i++)    eeprom_wb(addr+i, data[i]);#endif  context_p->data_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<IRMEM_RECEIVE_ATTEMPTS; attempt++) {	parse_ctx.data_offset=0;	parse_ctx.name_len=0;	if(irobex_receive(&irlap_ctx, put_name_cb, put_data_cb, &parse_ctx))	  break;      }      if(attempt<IRMEM_RECEIVE_ATTEMPTS) {	eeprom_wb(IRMEM_EEPROM_NAME_LEN, parse_ctx.name_len);	eeprom_wb(IRMEM_EEPROM_NAME_LEN+1, parse_ctx.name_len>>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<addr_limit) {	  eeprom_wb(addr, parse_ctx.tmp_eeprom[addr]);	  addr++;	}#endif      } else {	eeprom_wb(IRMEM_EEPROM_NAME_LEN, 0xFF);	eeprom_wb(IRMEM_EEPROM_NAME_LEN+1, 0xFF);      }      sbi(PORTD, PD7);      continue;    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -