Exemple #1
0
byte uart2_receive_packet(void)
{
  byte i;

  // debug !!!!
  //ext_pending_data = 2;

  if (ext_pending_data>8) {
    ext_buffer[0] = CMD_RCV;
    ext_buffer[1] = 9; // length to send
    i2cMasterSend(1, 2, ext_buffer);
    _delay_us(400);
    if (i2cGetLastError() == I2C_NACK) {
      // module is not responding
      // ToDo: status change
      uart2_status |= BV(0);
     } else {
      // module ok
      i2cMasterReceive(1, 10, ext_buffer);
      if (ext_buffer[0] == CMD_RCV) {
        // answer ok
        for (i=(0+1); i<(9+1); i++) {
          uart2_receive_char(ext_buffer[i]);
        }
        ext_pending_data = 0;
       } else {
        // bad answer
        uart2_status |= BV(1);
        ext_pending_data = 0;
      }
    }
  }
  else if (ext_pending_data>0) {
    ext_buffer[0] = CMD_RCV;
    ext_buffer[1] = ext_pending_data; // length to send
    i2cMasterSend(1, 2, ext_buffer);
    _delay_us(400);
    if (i2cGetLastError() == I2C_NACK) {
      // module is not responding
      // ToDo: status change
      uart2_status |= BV(0);
      } else {
      // module ok
      i2cMasterReceive(1, ext_pending_data+1, ext_buffer); // data len + cmd byte
      if (ext_buffer[0] == CMD_RCV) {
        // answer ok
        for (i=(0+1); i<(ext_pending_data+1); i++) {
          uart2_receive_char(ext_buffer[i]);
        }
        ext_pending_data = 0;
        } else {
        // bad answer
        uart2_status |= BV(1);
        ext_pending_data = 0;
      }
    }
  }
  return 0;
}
Exemple #2
0
static void __HMC6343_read(SENSOR* sensor){
	if(sensor){
		HMC6343* compass = (HMC6343*)sensor;
		const I2C_DEVICE* i2c = &(compass->i2cInfo);
		uint8_t  cmd = CMD_GET_HEADING;			// post heading and pitch
		HEADING_REPLY reply;

		// Post heading data
		if(i2cMasterSend(i2c,1,&cmd)){
			// Datasheet says to wait for 1ms before reading response
			delay_ms(1);

			// receive the response
			if(i2cMasterReceive(i2c, sizeof(HEADING_REPLY), (uint8_t*)(&reply))){
				// Get heading in 10ths of a degree 0->3600 and round to degrees
				uint16_t heading10ths = reply.heading_msb;
				heading10ths <<= 8;
				heading10ths |= reply.heading_lsb;
				compass->compass.bearingDegrees = (heading10ths + 5)/10;

				// Get roll +- 900 tenths of a degree
				int16_t roll10ths = reply.roll_msb;
				roll10ths <<= 8;
				roll10ths |= reply.roll_lsb;
				compass->compass.rollDegrees = (roll10ths + 5)/10;

				// Get pitch +- 900 tenths of a degree
				int16_t pitch10ths = reply.pitch_msb;
				pitch10ths <<= 8;
				pitch10ths |= reply.pitch_lsb;
				compass->compass.pitchDegrees = (pitch10ths + 5)/10;
			}
		}
	}
}
int hmc5843Read (int16_t *xouth, int16_t *youth, int16_t *zouth,
                 int16_t *xoutl, int16_t *youtl, int16_t *zoutl)
{
    int s = 0;
    i2cAcquireBus(d.i2cp);

    /* Read X/Y/Z high/low bits and status from 8-bit registers */
    d.rx[0] = d.rx[1] = d.rx[2] = d.rx[3] = d.rx[4] = d.rx[5] = d.rx[6] = 0;
    i2cMasterReceive(d.i2cp, &d.cfg, d.addr, d.rx, 2);

#if 1
    d.tx[0] = HMC5843_MODE;
    d.tx[1] = HMC5843_MODE_SINGLE;
    i2cMasterTransmit(d.i2cp, &dummycfg, d.addr, d.tx, 2, NULL, 0);
#endif

    i2cReleaseBus(d.i2cp);

    chThdSleepMilliseconds(60);

    *xouth = d.rx[0];
    *xoutl = d.rx[1];
    *youth = d.rx[2];
    *youtl = d.rx[3];
    *zouth = d.rx[4];
    *zoutl = d.rx[5];

    return s * 5;
}
Exemple #4
0
uint8_t ds1307ReadRegister(uint8_t reg)
{
	uint8_t device_data[2];
	device_data[0] = reg;
	i2cMasterSend(DS1307_BASE_ADDRESS,1,device_data);
	i2cMasterReceive(DS1307_BASE_ADDRESS,1,device_data);
	return device_data[0];
}
// read/write register
u16 po1030RegRead(u08 regaddr)
{
    u08 packet[2];
    // set the register to access
    packet[0] = (regaddr & PO1030_REG_MASK);
    i2cMasterSend(PO1030_I2C_BASE_ADDR, 1, &packet[0]);
    // read the value
    if(regaddr&PO1030_REG_16BIT)
    {
        i2cMasterReceive(PO1030_I2C_BASE_ADDR, 2, &packet[0]);
        // return value
        return (packet[0]<<8)|packet[1];
    }
    else
    {
        i2cMasterReceive(PO1030_I2C_BASE_ADDR, 1, &packet[0]);
        // return value
        return packet[0];
    }
}
void read_ms4515(void) {
    //float Differential_pressure = 0
    float Temperature_MS4515 = 0;
    //chprintf((BaseSequentialStream *)&OUTPUT, "Inside Diff open drain mode and  pressure measurement address new %x \r\n", MS4515_addr);

    /*Acquire I2C bus before starting I2C communication - Pending */
    i2cMasterReceive(&I2C_MS4515, MS4515_ADDR, MS4515_rxbuf, 4);
    /*Release I2C bus once communication is done - Pending */

    /*Isolate status, temperature and pressure data */
    MS4515_status = MS4515_rxbuf[0] >> 6;
    dp_raw = (MS4515_rxbuf[0] << 8) + MS4515_rxbuf[1];
    dp_raw = 0x3FFF & dp_raw;
    dT_raw = (MS4515_rxbuf[2] << 8) + MS4515_rxbuf[3];
    dT_raw = (0xFFE0 & dT_raw)>>5;
    Temperature_MS4515 = ((200*dT_raw)/2047) - 50;

    chprintf((BaseSequentialStream *)&OUTPUT,"MS4515 raw bytes output  %x , %x , %x , %x, %d , %d , %d \r\n", MS4515_rxbuf[0], MS4515_rxbuf[1], MS4515_rxbuf[2],MS4515_rxbuf[3], dp_raw, dT_raw, Temperature_MS4515);
}
Exemple #7
0
static void __HMC6352_read(SENSOR* sensor){
	HMC6352* compass = (HMC6352*)sensor;
	const I2C_DEVICE* i2c = &(compass->i2cInfo);
	HEADING_REPLY reply;

	// Post heading data
// Not need in continuous mode
//	uint8_t  cmd = CMD_GET_HEADING;			// post heading and pitch
//	i2cMasterSend(compass->i2cAddress,1,&cmd);
//	delay_ms(6);							// Datasheet says to wait for 6ms before reading response

	// receive the response
	if(i2cMasterReceive(i2c, sizeof(HEADING_REPLY), (uint8_t*)(&reply))){
		// Get heading in 10ths of a degree 0->3600 and round to degrees
		uint16_t heading10ths = reply.heading_msb;
		heading10ths <<= 8;
		heading10ths |= reply.heading_lsb;
		compass->compass.bearingDegrees = (heading10ths + 5)/10;
	}
}
Exemple #8
0
void uart2_receive_packet_query(void)
{
  ext_buffer[0] = CMD_RCV_SIZE;
  i2cMasterSend(1, 1, ext_buffer);
  _delay_us(400);
  if (i2cGetLastError() == I2C_NACK) {
    // module is not responding
    // ToDo: status change
    uart2_status |= BV(0);
   } else {
    // module ok
    i2cMasterReceive(1, 2, ext_buffer);
    if (ext_buffer[0] == CMD_RCV_SIZE) {
      ext_pending_data = ext_buffer[1];
     } else {
       // bad answer
       uart2_status |= BV(1);
      ext_pending_data = 0;
    }
  }
}
Exemple #9
0
/**
 * Fonctions
 */
int main(void)
{
	// timer
	timerInit();
	i2cInit();
	
	// initialisation des I/O
	sbi(DDRD,3);	// led verte en sortie
	sbi(PORTD,3);	// éteindre la led
	cbi(DDRD,2);	// bouton d'entrée
	cbi(PORTD,2);
	
	// initialisation des blinkm
	
	// arrêter le script
	
	cmd[0] = 'o';
	cmdSize = 1;
	i2cMasterSend(0x00,cmdSize,cmd);
	
	cmd[0] = 'c';
	cmd[1] = 0x00;
	cmd[2] = 0x00;
	cmd[3] = 0x00;
	cmdSize = 4;
	i2cMasterSend(0x00,cmdSize,cmd);

	delay_ms(1000);
	
	cmd[0] = 'f';
	cmd[1] = 20;
	cmdSize = 2;
	i2cMasterSend(0x00,cmdSize,cmd);
	
	/**  Boucle principale **/
	
	//
	while (1) 
	{
		cbi(PORTD,3);
		//
		cmd[0] = 'h';
		cmd[1] = hue;
		cmd[2] = 0xFF;
		cmd[3] = 0xFF;
		cmdSize = 4;
		i2cMasterSend(0x00,cmdSize,cmd);
		//
		if (hue < 1000)
		{
			delay_ms(hue);
		} else {
			delay_ms(700);
		}
		sbi(PORTD,3);
		//
		cmd[0] = 'c';
		cmd[1] = 0x00;
		cmd[2] = 0x00;
		cmd[3] = 0x00;
		cmdSize = 4;
		i2cMasterSend(0x00,cmdSize,cmd);
		//
		delay_ms(500);
		
		// interrogation du détecteur de distance
		cmd[0] = 0x42; // read distance
		cmdSize = 1; 
		i2cMasterSend(0x02,cmdSize,cmd);
		
		i2cMasterReceive(0x03,2,buffer);
		hue = ( 0x00FF & buffer[0] );
        hue += ( (0x00FF & buffer[1]) <<8 );
		
	}	
	return 0;	
}
Exemple #10
0
void i2cTest(void)
{
	u08 c=0;
	showByte(0x55);

	// initialize i2c function library
	i2cInit();
	// set local device address and allow response to general call
	i2cSetLocalDeviceAddr(LOCAL_ADDR, TRUE);
	// set the Slave Receive Handler function
	// (this function will run whenever a master somewhere else on the bus
	//  writes data to us as a slave)
	i2cSetSlaveReceiveHandler( i2cSlaveReceiveService );
	// set the Slave Transmit Handler function
	// (this function will run whenever a master somewhere else on the bus
	//  attempts to read data from us as a slave)
	i2cSetSlaveTransmitHandler( i2cSlaveTransmitService );

	timerPause(500);
	showByte(0xAA);

	while(1)
	{
		rprintf("Command>");
		while(!c) uartReceiveByte(&c);
		
		switch(c)
		{
		case 's':
			rprintf("Send: ");
			// get string from terminal
			localBufferLength = 0;
			c = 0;
			while((c != 0x0D) && (localBufferLength < 0x20))
			{
				while(!uartReceiveByte(&c));
				localBuffer[localBufferLength++] = c;
				uartSendByte(c);
			}
			// switch CR to NULL to terminate string
			localBuffer[localBufferLength-1] = 0;
			// send string over I2C
			i2cMasterSend(TARGET_ADDR, localBufferLength, localBuffer);
			//i2cMasterSendNI(TARGET_ADDR, localBufferLength, localBuffer);
			rprintfCRLF();
			break;
		case 'r':
			rprintf("Receive: ");
			// receive string over I2C
			i2cMasterReceive(TARGET_ADDR, 0x10, localBuffer);
			//i2cMasterReceiveNI(TARGET_ADDR, 0x10, localBuffer);
			// format buffer
			localBuffer[0x10] = 0;
			rprintfStr(localBuffer);
			rprintfCRLF();
			break;
		case 'm':
			testI2cMemory();
			break;
		default:
			rprintf("Unknown Command!");
			break;
		}
		c = 0;
		rprintfCRLF();
	}

}
Exemple #11
0
void bma180_ReadSensorData_p2()
{
  // printf("bma180-p2\n");
  i2cMasterReceive(BMA180_DeviceID, 6, sensorData, bma180_ReadSensorData_p3);
}
float ComputeMagnetTarget() {
    /* Reset periodically */

    /*if ((get_time() - old_time) > 250){
            old_time = get_time();
            i2cMasterSend(Compass,1,ResetCommand);
            pause(10);

            }*/
    i2cMasterSend(Compass,1,ResetCommand);
    pause(10);


    /* Read Heading Information */

    i2cMasterSend(Compass,3,OutputAngleCommand);
    i2cMasterSend(Compass,1,ReadCommand);

    pause(10);

    i2cMasterReceive(Compass,2,Data);
    AngleReading = (Data[0] << 8) | Data[1];

    /* Read Offset-Corrected Magnetometer X */

    i2cMasterSend(Compass,3,OutputMagXCommand);
    i2cMasterSend(Compass,1,ReadCommand);

    pause(10);

    i2cMasterReceive(Compass,2,Data);
    MagXReading = (Data[0] << 8) | Data[1];

    /* Read Offset-Corrected Magnetometer X */

    i2cMasterSend(Compass,3,OutputMagYCommand);
    i2cMasterSend(Compass,1,ReadCommand);

    pause(10);

    i2cMasterReceive(Compass,2,Data);
    MagYReading = (Data[0] << 8) | Data[1];

    //printf("\nX:%.1f,%.1f, Y:%.1f,%.1f",MagXReading/10.0,MagXRawReading/10.0,MagYReading/10.0,MagYRawReading/10.0);
    //printf("\n(%.1f,%.1f)",MagXReading/10.0,MagYReading/10.0);

    FieldStrength = strength(MagXReading,MagYReading);
    /*
       if (FieldStrength > ACTIVATE_THRESH){
       was_overload = 1;
       }
       else if (was_overload && (FieldStrength < DEACTIVATE_THRESH)){
       was_overload = 0;
       old_time = get_time();
       i2cMasterSend(Compass,1,ResetCommand);
       pause(10);
       }
     */
    fangle = ((float)AngleReading)/10.0;
    if (fangle > 180.0) fangle -= 360.0;

    //printf("\nangle:%.1f, strength: %.2f",fangle, strength(MagXReading,MagYReading));


    //i2cMasterSend(Compass,1,ResetCommand);
    //pause(10);

    return fangle;
}