Ejemplo n.º 1
0
static uchar
i2c_set_dev_offset (uchar dev_addr, unsigned int offset, int ten_bit,
		    int alen)
{
	uchar status;
	unsigned int table[2];

/* initialize the table of address offset bytes */
/* utilized for 2 byte address offsets */
/* NOTE: the order is high byte first! */
	table[1] = offset & 0xff;	/* low byte */
	table[0] = offset / 0x100;	/* high byte */

	DP (puts ("i2c_set_dev_offset\n"));

	status = i2c_select_device (dev_addr, 0, ten_bit);
	if (status) {
#ifdef DEBUG_I2C
		printf ("Failed to select device setting offset: 0x%02x\n",
			status);
#endif
		return status;
	}
/* check the address offset length */
	if (alen == 0)
		/* no address offset */
		return (0);
	else if (alen == 1) {
		/* 1 byte address offset */
		status = i2c_write_data (&offset, 1);
		if (status) {
#ifdef DEBUG_I2C
			printf ("Failed to write data: 0x%02x\n", status);
#endif
			return status;
		}
	} else if (alen == 2) {
		/* 2 bytes address offset */
		status = i2c_write_data (table, 2);
		if (status) {
#ifdef DEBUG_I2C
			printf ("Failed to write data: 0x%02x\n", status);
#endif
			return status;
		}
	} else {
		/* address offset unknown or not supported */
		printf ("Address length offset %d is not supported\n", alen);
		return 1;
	}
	return 0;		/* sucessful completion */
}
Ejemplo n.º 2
0
void start_ping(void)
{
	unsigned char Data[1];
	unsigned char addr = 0xE0;
	Data[0] = 0x51;    // write 0x51 to reg 0 of the ranger
	i2c_write_data(addr, 0, Data, 1);    // write one byte of data to reg 0 at addr
}
Ejemplo n.º 3
0
/* Read bytes */
int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
{
	uint offset;
	int i;

	debug("i2c_read: chip=0x%x, addr=0x%x, len=0x%x\n",
				chip, addr, len);
	if (!i2c_addr_ok(addr, alen)) {
		debug("i2c_read: Bad address %x.%d.\n", addr, alen);
		return 1;
	}
	for (offset = 0; offset < len; offset++) {
		if (alen) {
			uchar data[alen];
			for (i = 0; i < alen; i++) {
				data[alen - i - 1] =
					(addr + offset) >> (8 * i);
			}
			if (i2c_write_data(chip, data, alen)) {
				debug("i2c_read: error sending (0x%x)\n",
					addr);
				return 1;
			}
		}
		if (i2c_read_data(chip, buffer + offset, 1)) {
			debug("i2c_read: error reading (0x%x)\n", addr);
			return 1;
		}
	}

	return 0;
}
Ejemplo n.º 4
0
void Range_Update(void)
{
	if (new_range)
	{
		new_range = 0; //reset the new_range flag
		cmrange = ReadRanger(); //get the range back from the ranger
		Data[0] = 0x51;  //write 0x51 to reg 0 of the ranger:
		i2c_write_data(ranger_addr, 0,  Data, 1) ;  // write one byte of data to reg 0 at addr
	}
}
void Ranger(void)  {
	if (new_range){ //80ms passed
		printf("\r\n new range");
		new_range = 0;
		range=ReadRanger();
		printf("\r\nrange2: %d", range);
		comp_and_range_Data[0] = 0x51 ;  // write 0x51 to reg 0 of the ranger:
		i2c_write_data(0xE0, 0, comp_and_range_Data, 1) ; // write one byte of data to reg 0 at addr_r
	}//end if new range
	Drive_Motor();	//even if not new range info, run the drive motor
}//end ranger
Ejemplo n.º 6
0
int fmradio_i2c_write(unsigned char address, const unsigned char* buf, int count)
{
#ifdef SANSA_FUZEV2
    bitclr32(&CCU_IO, 1<<12);
#endif
    int ret = i2c_write_data(fm_i2c_bus, address, -1, buf, count);
#ifdef SANSA_FUZEV2
    bitset32(&CCU_IO, 1<<12);
#endif
    return ret;
}
Ejemplo n.º 7
0
//------------------------------------------------------------------------------------
//ranger function
//------------------------------------------------------------------------------------
void Ranger(void)  {
	if (new_range) //80ms passed
	{
		//printf("/r/n new range");
		new_range = 0;
		range=read_ranger();
		//printf("range2: %d", range);
		Data1[0] = 0x51 ;  // write 0x51 to reg 0 of the ranger:
		i2c_write_data(0xE0, 0, Data1, 1) ; // write one byte of data to reg 0 at addr_r
	}
	Drive_Motor();	//even if not new range info, run the drive motor
}
Ejemplo n.º 8
0
void main(void) {
    // initialize board
    Sys_Init();
    putchar(' '); //the quotes in this line may not format correctly
    Port_Init();
    PCA_Init();
    SMB_Init();
    Interrupt_Init();
    printf("Starting\n\r");

    //print beginning message
    printf("Embedded Control Drive Motor Control\r\n");
    // Initialize motor in neutral and wait for 1 second
    MOTOR_PW = PW_NEUT;
	motorPW = 0xFFFF-MOTOR_PW;
    PCA0CPL2 = motorPW;
    PCA0CPH2 = motorPW>>8;
	printf("Pulse Width = %d\r\n",MOTOR_PW);
    c = 0;
    while (c < 50);								//wait 1 second in neutral
	printf("end wait \r\n");
    
    
    
    //Main Functionality
    while (1) {
        if (!SS1) { // If the slide switch is active, set PW to center
            PW = PWCENTER;
            PCA0CP0 = 0xFFFF - PW; // Update comparator with new PW value
            while (!SS1); // Wait...
        } else if (take_heading) { // Otherwise take a new heading
            reading = Read_Compass(); // Get current heading
            printf("%d\n\r", reading);
            Steering_Servo(reading); // Change PW based on current heading
            PCA0CP0 = 0xFFFF - PW; // Update comparator with new PW value
        }


        if (getRange) {
            getRange = 0; // Reset 80 ms flag
            range_val = read_ranger(); // Read the distance from the ranger
            printf("Range:			%d cm \r\n", range_val);
            printf("Pulse Width:	%d \r\n", MOTOR_PW);

            // Start a new ping
            Data[0] = 0x51; // write 0x51 to reg 0 of the ranger:
            i2c_write_data(addr, 0, Data, 1); // write one byte of data to reg 0 at addr
        }

        if (SS0) Drive_Motor(range_val);
        else Drive_Motor(45); // Hold the motor in neutral if the slide switch is off
    }
}
Ejemplo n.º 9
0
/* Probe to see if a chip is present. */
int i2c_probe(uchar chip)
{
	int rc;
	uchar reg;

	debug("i2c_probe: addr=0x%x\n", chip);
	reg = 0;
	rc = i2c_write_data(chip, &reg, 1);
	if (rc) {
		debug("Error probing 0x%x.\n", chip);
		return 1;
	}
	return 0;
}
Ejemplo n.º 10
0
void main(void)
{
	Sys_Init();
	putchar(' ');
	XBR0_Init();
	PCA_Init();
	i2c_Init();

	while(1)
	{
		if (new_range)
		{
			new_range = 0;
			cmrange = ReadRanger();
			Data[0] = 0x51;  //write 0x51 to reg 0 of the ranger:
			i2c_write_data(ranger_addr, 0,  Data, 1) ;  // write one byte of data to reg 0 at addr
			printf("range = %d\r\n", cmrange);
		}
	}
}
void start_ping(void) {	//The start ping function
	ping_Data[0] = 0x51;    // write 0x51 to reg 0 of the ranger
	i2c_write_data(range_and_ping_addr, 0, ping_Data, 1);    // write one byte of data to reg 0 at addr
}//end start ping
Ejemplo n.º 12
0
int fmradio_i2c_write(unsigned char address, const unsigned char* buf, int count)
{
    return i2c_write_data(fm_i2c_bus, address, -1, buf, count);
}