Ejemplo n.º 1
0
int main(int argc, char** argv)
{
	struct eeprom e;
	int op;

	op = 0;

	usage_if(argc != 2 || argv[1][0] != '-' || argv[1][2] != '\0');
	op = argv[1][1];

	fprintf(stderr, "Open /dev/i2c/0 with 8bit mode\n");
	die_if(eeprom_open("/dev/i2c/0", 0x50, EEPROM_TYPE_8BIT_ADDR, &e) < 0, 
			"unable to open eeprom device file "
			"(check that the file exists and that it's readable)");
	switch(op)
	{
	case 'r':
		fprintf(stderr, "  Reading 256 bytes from 0x0\n");
		read_from_eeprom(&e, 0, 256);
		break;
	case 'w':
		fprintf(stderr, "  Writing 0x00-0xff into 24C08 \n");
		write_to_eeprom(&e, 0);
		break;
	default:
		usage_if(1);
		exit(1);
	}
	eeprom_close(&e);

	return 0;
}
Ejemplo n.º 2
0
/* Main */
void main()
{
   unsigned short int count = 0;
   unsigned char data;

   /* Allow peripheral init */
   delay(1000);

   /* Initialize PIC USART to operate at 19200 baud */
   init_serial();

   /* Initialize I2C */
   init_i2c();

   /* Turn ON and setup up LCD */
   init_lcd();
   
   /* Loop reception and echo it back */
   receive_next:
  
   while(!PIR1bits.RCIF);
   /* Echo received character */
   data = RCREG;
   TXREG = data;
   
   /* Send the received byte to EEPROM */
   write_to_eeprom(data, count);

   count++;

   if(count == 16)
   {
      dump_to_lcd();
      count = 0;
   }
  
   goto receive_next;
   
   /* Shouldn't get here! */
   while(1);
}
Ejemplo n.º 3
0
int main(int argc, char** argv)
{
	struct eeprom e;
	int ret, op, i2c_addr, memaddr, size, want_hex, dummy, force, sixteen;
	char *device, *arg = 0, *i2c_addr_s;
	struct stat st;
	int eeprom_type = 0;

	op = want_hex = dummy = force = sixteen = 0;
	g_quiet = 0;

	while((ret = getopt(argc, argv, "1:8fr:qhw:xd")) != -1)
	{
		switch(ret)
		{
		case '1':
			usage_if(*optarg != '6' || strlen(optarg) != 1);
			die_if(eeprom_type, "EEPROM type switch (-8 or -16) used twice");
			eeprom_type = EEPROM_TYPE_16BIT_ADDR;	
			break;
		case 'x':
			want_hex++;
			break;
		case 'd':
			dummy++;
			break;
		case '8':
			die_if(eeprom_type, "EEPROM type switch (-8 or -16) used twice");
			eeprom_type = EEPROM_TYPE_8BIT_ADDR;
			break;
		case 'f':
			force++;
			break;
		case 'q':
			g_quiet++;
			break;
		case 'h':
			usage_if(1);
			break;
		default:
			die_if(op != 0, "Both read and write requested"); 
			arg = optarg;
			op = ret;
		}
	}
	if(!eeprom_type)
		eeprom_type = EEPROM_TYPE_8BIT_ADDR; // default

	usage_if(op == 0); // no switches 
	// set device and i2c_addr reading from cmdline or env
	device = i2c_addr_s = 0;
	switch(argc - optind)
	{
	case 0:
		device = getenv(ENV_DEV);
		i2c_addr_s = getenv(ENV_I2C_ADDR);
		break;
	case 1:
		if(stat(argv[optind], &st) != -1)
		{
			device = argv[optind];
			i2c_addr_s = getenv(ENV_I2C_ADDR);
		} else {
			device = getenv(ENV_DEV);
			i2c_addr_s = argv[optind];
		}
		break;
	case 2:
		device = argv[optind++];
		i2c_addr_s = argv[optind];
		break;
	default:
		usage_if(1);
	}
	usage_if(!device || !i2c_addr_s);
	i2c_addr = strtoul(i2c_addr_s, 0, 0);

	print_info("eeprog %s, a 24Cxx EEPROM reader/writer\n", VERSION);
	print_info("Copyright (c) 2003 by Stefano Barbato - All rights reserved.\n");
	print_info("  Bus: %s, Address: 0x%x, Mode: %dbit\n", 
			device, i2c_addr, 
			(eeprom_type == EEPROM_TYPE_8BIT_ADDR ? 8 : 16) );
	if(dummy)
	{
		fprintf(stderr, "Dummy mode selected, nothing done.\n");
		return 0;
	}
	die_if(eeprom_open(device, i2c_addr, eeprom_type, &e) < 0, 
			"unable to open eeprom device file (check that the file exists and that it's readable)");
	switch(op)
	{
	case 'r':
		if(force == 0)
			confirm_action();
		size = 1; // default
		parse_arg(arg, &memaddr, &size);
		print_info("  Reading %d bytes from 0x%x\n", size, memaddr);
		read_from_eeprom(&e, memaddr, size, want_hex);
		break;
	case 'w':
		if(force == 0)
			confirm_action();
		parse_arg(arg, &memaddr, &size);
		print_info("  Writing stdin starting at address 0x%x\n", 
			memaddr);
		write_to_eeprom(&e, memaddr);
		break;
	default:
		usage_if(1);
		exit(1);
	}
	eeprom_close(&e);

	return 0;
}