Пример #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;
}
Пример #2
0
void  ds1307WriteRegister(uint8_t reg, uint8_t data)
{
	uint8_t device_data[2];
	device_data[0] = reg;
	device_data[1] = data;
	i2cMasterSend(DS1307_BASE_ADDRESS,2,device_data);
}
Пример #3
0
static void setSpeed(__ACTUATOR *actuator, DRIVE_SPEED speed){
	SERVO* servo = (SERVO*)actuator;

	DRIVE_SPEED current = servo->actuator.required_speed;
	if(servo->actuator.inverted){
		current *= -1;
	}


	// Can only cope with 21 servos
	if(speed != current && servo->delay< 21){
		uint8_t reg = (servo->delay * 3) + 1;
		uint8_t msg[3];

		struct s_servo_driver* driver = servo->driver;
		const I2C_DEVICE* i2c = &(driver->i2cInfo);

		// Get the pulse length in us
		uint16_t pulse = interpolateU(speed, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX, servo->center_us - servo->range_us , servo->center_us + servo->range_us);

		msg[0] = reg;						// Register
		msg[1] = (uint8_t)(pulse & 0xff); 	// lo
		msg[2] = (uint8_t)(pulse >> 8); 	// hi

		// The I2C address is always C2
		i2cMasterSend(i2c, sizeof(msg), msg);
//		rprintf("%u = %u\n",servo->delay,pulse);
	}
Пример #4
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;
			}
		}
	}
}
Пример #5
0
void bma180_ReadSensorData(u08 *buffer, genericVoidCallbackFn callback)
{
  sensorData = buffer;
  sampleCompleteCallback = callback;
  sensorDataReady=false;
  i2cMasterSend(BMA180_DeviceID, 1, &m_valueRegAddr, bma180_ReadSensorData_p2);
}
Пример #6
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];
}
Пример #7
0
static void speed(HMC6343* compass, uint8_t val){
	if(compass){
		uint8_t cmd[] = {CMD_WRITE_EEPROM, 5, val};

		// Post heading data
		const I2C_DEVICE* i2c = &(compass->i2cInfo);
		i2cMasterSend(i2c,sizeof(cmd),cmd);
	}
}
Пример #8
0
void MagnetInit() {
    old_time = get_time();

    was_overload=0;

    i2cInit();

    i2cMasterSend(Compass,1,ResetCommand);
    pause(10);
}
Пример #9
0
void HMC6352_20Hz(HMC6352* compass){
	// Put into continuous mode 20Hz

	// Post heading data
	if(compass){
		uint8_t cmd[] = {CMD_WRITE_RAM, 0x74, 0b01110010};
		const I2C_DEVICE* i2c = &(compass->i2cInfo);
		i2cMasterSend(i2c,sizeof(cmd),cmd);
	}
}
Пример #10
0
void uart2_send_packet(void)
{
  byte i;

  while (uart2_tx_size() >= 9) {
    for(i=(0+1); i<(9+1); i++) {
      ext_buffer[i] = uart2_send_char();
    }
    ext_buffer[0] = CMD_SEND;
    i2cMasterSend(0x01, 10, ext_buffer);
  }
}
Пример #11
0
void po1030RegWrite(u08 regaddr, u16 value)
{
    u08 packet[3];
    // set the register to access
    packet[0] = (regaddr & PO1030_REG_MASK);
    // read the value
    if(regaddr&PO1030_REG_16BIT)
    {
        // set value to write
        packet[1] = (value>>8);
        packet[2] = (value);
        // write the value
        i2cMasterSend(PO1030_I2C_BASE_ADDR, 3, &packet[0]);
    }
Пример #12
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];
    }
}
Пример #13
0
/*! \brief Set the current time of the realtime clock 
 *  \param data data[0] = seconds, data[1] = minutes, data[2] = hours, data[3] = Day, data[4] = Date, data[5] = month, data[6] = year */
void ds1307_set_time(char *data) {
	//Flag to avoid a read from the real-time clock during 
	//our time adjustment
	allowed_to_read = 0;
		
	unsigned char b0=0,b1=0;
		
	*(time_data+0) = 0x00;							//REG ADDR 0
	
	b1 = data[0] / 10;
	b0 = (data[0] - 10*b1) + (b1<<4);
	*(time_data+1) = b0 & 0x7F;	// seconds, enable oscillator (bit 7=0)
  
	b1 = data[1] / 10;
	b0 = (data[1] - 10*b1) + (b1<<4);
	*(time_data+2) = b0;					// minute
  
	b1 = data[2] / 10;
	b0 = (data[2] - 10*b1) + (b1<<4);
	*(time_data+3) = b0; 				// hour
	
  *(time_data+4) = *(data+3);					// Day 
	
	b1 = data[4] / 10;
	b0 = (data[4] - 10*b1) + (b1<<4);	
	*(time_data+5) = b0;					// Date
	
	b1 = data[5] / 10;
	b0 = (data[5] - 10*b1) + (b1<<4);	

	*(time_data+6) = b0;					// month
	
	b1 = data[6] / 10;
	b0 = (data[6] - 10*b1) + (b1<<4);	
	*(time_data+7) = b0;					// year
	*(time_data+8) = 0x00;
	
	i2cMasterSend(DS1307_ADDR,9,(unsigned char *)time_data);
	//Allowed to read from the real-time clock again
	allowed_to_read = 1;
}
Пример #14
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;
    }
  }
}
Пример #15
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;	
}
Пример #16
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();
	}

}
Пример #17
0
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;
}
Пример #18
0
//Send the "get data" request
void gyroSendDataRequest(void) {
    u08 gyroDataRequest = GYRO_REGISTER_OUT_X_L | 0x80;
    i2cMasterSend(L3GD20_ADDRESS, 1, &gyroDataRequest);
}