Exemple #1
0
/* This example attempts to read the device id of the MMA8451Q accelerometer that is included on the Kinetis K20 eval
   boards. */
int main(void)
{
  int counter = 0;
  uint32_t status;
  uint16_t init_sequence[] = {0x3a, 0x0d, I2C_RESTART, 0x3b, I2C_READ};
  uint8_t device_id = 0;		/* will contain the device id after sequence has been processed */

  enable_irq(INT_I2C0 - 16);

  SIM_SCGC4 |= SIM_SCGC4_I2C0_MASK;
  SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;

  PORTB_PCR0 = PORT_PCR_MUX(0x02) | PORT_PCR_ODE_MASK;
  PORTB_PCR1 = PORT_PCR_MUX(0x02) | PORT_PCR_ODE_MASK;

  status = i2c_init(0, 0x01, 0x20);
  status = i2c_send_sequence(0, init_sequence, 5, &device_id, my_callback, (void*)0x1234);
  
  /* This endless loop is here so that you can check the result of the I2C transmission. It is performed asynchronously,
	 so without the loop the program would terminate before the transmission ended. */
  for(;;) {
	counter++;
  }

  return 0;
}
Exemple #2
0
int I2CreadByte(uint16_t chipAddress, uint16_t registerAddress, uint8_t *byte_from_I2Cdevice, int I2Chandle)
{
/*
//	uint16_t i2c_event_sequence[] = {chipAddress<<1, registerAddress, I2C_RESTART, (chipAddress<<1)|1, I2C_READ, I2C_READ};
	uint16_t i2c_event_sequence[] = {chipAddress<<1, registerAddress, I2C_RESTART, (chipAddress<<1)|1, I2C_READ};	
	uint8_t data[2];   //data[0] is dummy.
	
//	int ioctlret = i2c_send_sequence(I2Chandle, i2c_event_sequence, 6, data);
	int ioctlret = i2c_send_sequence(I2Chandle, i2c_event_sequence, 5, data);
	if(ioctlret == 2)
	{
//		*byte_from_I2Cdevice = data[1];
		*byte_from_I2Cdevice = data[0];
		return 1;
	}
	else
	{
		return -1;
	}
*/
	uint16_t i2c_event_sequence[] = {chipAddress<<1, registerAddress, I2C_RESTART, (chipAddress<<1)|1, I2C_READ};	
	uint8_t data;
	int ioctlret = i2c_send_sequence(I2Chandle, i2c_event_sequence, 5, &data);
	printf("ioctlret=%d\n", ioctlret);
	if(ioctlret == 2)
	{
		*byte_from_I2Cdevice = data;
		return 1;
	}
	else
	{
		return -1;
	}
}
Exemple #3
0
void read_xyz(uint8_t *buff)
{
    uint16_t msg[] =
    {0x3a, 0x01, I2C_RESTART, 0x3b, I2C_READ, I2C_READ, I2C_READ, I2C_READ};
    i2c_send_sequence(msg, 8, buff, LPM0_bits);
    LPM0;
    while(!i2c_done());
}
Exemple #4
0
/**
 *  @fn wakeup_tsl2561
 *  @author TheFwGuy
 *  @brief Wakeup TSL2561
 *
 *  @param None
 *  @return None
 */
void wakeup_tsl2561(void)
{
   while(!i2c_done());
   i2c_send_sequence((uint16_t *)seqwq, TSL2561_WAKEUP_LEN, (uint8_t *)recseq, 0);

   __delay_cycles(500000);		/* Approx 35 ms */

}
Exemple #5
0
/**
 *  @fn config_tsl2561
 *  @author TheFwGuy
 *  @brief Configure TSL2561
 *
 *  @param Integration time
 *  @param Gain
 *  @return None
 */
void config_tsl2561()
{
   seqcn[2] = _tsl2561IntegrationTime | _tsl2561Gain;

   while(!i2c_done());
   i2c_send_sequence((uint16_t *)seqcn, TSL2561_CONFIG_LEN, (uint8_t *)recseq, 0);
   
}
Exemple #6
0
  /* =============================================================================
    =========================================================================== */
  void Lidar::write(char myAddress, char myValue){
	uint16_t init_sequence1[] = {0xc4, (int)myAddress, (int)myValue};
    //sends sequence over i2c
    int nackCatcher = i2c_send_sequence(Lidar::handle, init_sequence1, 3, 0);
	//write failed catcher
    if(nackCatcher != 1){Lidar::nack = true;}
	//sleeps for 1 ms
    usleep(1000);
  }
Exemple #7
0
void write_register(uint16_t reg, uint16_t val)
{
    uint16_t msg[] = {0x3a, 0, 0};
    msg[1] = reg;
    msg[2] = val;
    i2c_send_sequence(msg, 3, 0, LPM0_bits);
    LPM0;
    while(!i2c_done());
}
Exemple #8
0
uint8_t read_register(uint16_t reg)
{
    uint8_t val = 0;
    uint16_t msg[] = {0x3a, 0, I2C_RESTART, 0x3b, I2C_READ};
    msg[1] = reg;
    i2c_send_sequence(msg, 5, &val, LPM0_bits);
    LPM0;
    while(!i2c_done());
    return val;
}
Exemple #9
0
int I2CwriteByte(uint16_t chipAddress, uint16_t registerAddress, uint8_t byte_to_write, int handle)
{
	uint16_t i2c_event_sequence[] = {chipAddress<<1, registerAddress, 0xFF&byte_to_write};
	int ioctlret = i2c_send_sequence(handle, i2c_event_sequence, 3, &byte_to_write);
	
	if(ioctlret != 1)
	{
		return -1;
	}
	
	return 1;
}
Exemple #10
0
int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer

    unsigned char readin[32];
    memset(readin, 0xFF, sizeof(readin));

    /* Initiate the I2C bus */
    i2c_init(USIDIV_5, USISSEL_2);
#if 0
    writeEEPROM(text, strlen(text));
#else
    i2c_send_sequence(arrayWrite, sizeof(arrayWrite), readin, 0);
    while(!i2c_done()) ;
    __delay_cycles(25000); // Wait 25 ms
#endif

#if 1
    i2c_send_sequence(arrayRead, sizeof(arrayRead), readin, CPUOFF);
    while(!i2c_done())
        ;
#endif
    return 0;
}
Exemple #11
0
/**
 *  @fn read_tsl2561
 *  @author TheFwGuy
 *  @brief Read the TSL2561
 *
 *  @param Reading (TSL2561_FULLSPECTRUM, TSL2561_VISIBLE or TSL2561_INFRARED)
 *  @return Intensity (LUX)
 */
int read_tsl2561(unsigned char reading)
{
   uint16_t ch0 = 0;
	uint16_t ch1 = 0;
   uint32_t luxres;

#if defined(READWORD)	
   uint16_t seqread_CH0[] = TSL2561_READ16_CH0;
   uint16_t seqread_CH1[] = TSL2561_READ16_CH1;
#else
   uint8_t result = 0;
   uint16_t seqread_lsb_CH0[] = TSL2561_READ8_CH0_LSB;
   uint16_t seqread_lsb_CH1[] = TSL2561_READ8_CH1_LSB;
   uint16_t seqread_msb_CH0[] = TSL2561_READ8_CH0_MSB;
   uint16_t seqread_msb_CH1[] = TSL2561_READ8_CH1_MSB;
#endif

	/* Wake up sensor */
   wakeup_tsl2561();

	switch(reading)
	{
		case TSL2561_FULLSPECTRUM:
#if defined(READWORD)	
         while(!i2c_done());
         i2c_send_sequence((uint16_t *)seqread_CH0, TSL2561_READ16_CH0_LEN, (uint8_t *)recseq, 0);
 			ch0 = 256 * recseq[0] + recseq[1];
         while(!i2c_done());
         i2c_send_sequence((uint16_t *)seqread_CH1, TSL2561_READ16_CH1_LEN, (uint8_t *)recseq, 0);
			ch1 = 256 * recseq[0] + recseq[1];
#else
         while(!i2c_done());
         i2c_send_sequence((uint16_t *)seqread_lsb_CH0, TSL2561_READ8_CH0_LSB_LEN, (uint8_t *)&result, 0);
			ch0 = result;
         while(!i2c_done());
         i2c_send_sequence((uint16_t *)seqread_msb_CH0, TSL2561_READ8_CH0_MSB_LEN, (uint8_t *)&result, 0);
			ch0 += (result * 256);

         while(!i2c_done());
         i2c_send_sequence((uint16_t *)seqread_lsb_CH1, TSL2561_READ8_CH1_LSB_LEN, (uint8_t *)&result, 0);
			ch1 = result;
         while(!i2c_done());
         i2c_send_sequence((uint16_t *)seqread_msb_CH1, TSL2561_READ8_CH1_MSB_LEN, (uint8_t *)&result, 0);
			ch1 += (result * 256);

#endif
			break;
	}
	
   sleep_tsl2561();

   luxres = luxcalc(ch0, ch1);

   return (luxres);
}
Exemple #12
0
/**
 *  @fn init_tsl2561
 *  @author TheFwGuy
 *  @brief Init TSL2561
 *
 *  @param None
 *  @return None
 */
void init_tsl2561(void)
{
   /*
    *  Read Tsl2561 ID
    */
   recseq[0] = 0;
   while(!i2c_done());
   i2c_send_sequence((uint16_t *)seqid, TSL2561_READID_LEN, (uint8_t *)recseq, 0);

//  if (recseq[0] == 0)
//   {
//      /* No chipset present - TBD what to do */
//   } 

   wakeup_tsl2561();
   config_tsl2561();
   sleep_tsl2561();
}
Exemple #13
0
int I2CwriteBlock(uint16_t chipAddress, uint16_t registerAddress, uint8_t *bytes_to_write, int blockLength, int handle)
{
	
	
	//uint16_t i2c_event_sequence[2+blockLength];
	uint16_t *i2c_event_sequence = (uint16_t *)malloc( sizeof(uint16_t)*(2+blockLength) );
	
	
	i2c_event_sequence[0] = chipAddress<<1;
	i2c_event_sequence[1] = registerAddress;
/*	
	i2c_event_sequence[2] = I2C_RESTART;
	i2c_event_sequence[3] = chipAddress<<1;
*/	
	for(int i=2; i<2+blockLength; i++)
	{
		i2c_event_sequence[i] = 0xFF&bytes_to_write[i-2];
	}
	
	//uint8_t dummy=7;
	int ioctlret = i2c_send_sequence(handle, i2c_event_sequence, 2+blockLength, bytes_to_write);
	
	free(i2c_event_sequence);
	//printf("ioctl_wb = %d\n", ioctlret);
	
	
/*
	int ioctlret;
	for(int i=0; i<blockLength; i++)
	{
		ioctlret = I2CwriteByte(chipAddress, registerAddress++, bytes_to_write[i], handle);
		if(ioctlret != 1)
		{
			return -1;
		}
	}
*/
	return 1;


}
Exemple #14
0
int i2c_transfer(uint16_t chipAddress, uint16_t registerAddress, uint8_t *bytes_to_write, int blockLength, uint8_t *rx_buffer, int handle)
{
	//rx size is 2;
	
	uint16_t *i2c_event_sequence = (uint16_t *)malloc( sizeof(uint16_t)*(6+blockLength) );
	
	i2c_event_sequence[0] = chipAddress<<1;
	i2c_event_sequence[1] = registerAddress;
	
	for(int i=2; i<2+blockLength; i++)
	{
		i2c_event_sequence[i] = 0xFF&bytes_to_write[i-2];
	}
	
	i2c_event_sequence[2+blockLength] = I2C_RESTART;
	i2c_event_sequence[3+blockLength] = (chipAddress<<1)|1;
	//i2c_event_sequence[4+blockLength] = I2C_READ;           //dummy
	
	for(int i=4+blockLength; i<6+blockLength; i++)
	{
		i2c_event_sequence[i] = I2C_READ;
	}

	int ioctlret = i2c_send_sequence(handle, i2c_event_sequence, 6+blockLength, rx_buffer);
	//printf("ioctlret=%d\n", ioctlret);
	
	free(i2c_event_sequence);
	
	if(ioctlret != 2)
	{
		
		return -1;
	}
	
	return 1;

}
Exemple #15
0
int I2CreadBlock(uint16_t chipAddress, uint16_t registerAddress, uint8_t *bytes_from_I2Cdevice, int blockLength, int I2Chandle)
{
	//uint16_t i2c_event_sequence[5+blockLength];
	uint16_t *i2c_event_sequence = (uint16_t *)malloc( sizeof(uint16_t)*(5+blockLength) );
	
	
	i2c_event_sequence[0] = chipAddress<<1;
	i2c_event_sequence[1] = registerAddress;
	i2c_event_sequence[2] = I2C_RESTART;
	i2c_event_sequence[3] = (chipAddress<<1)|1;
	i2c_event_sequence[4] = I2C_READ;           //dummy
	
	for(int i=5; i<5+blockLength; i++)
	{
		i2c_event_sequence[i] = I2C_READ;
	}
	
	uint8_t data_with_dummy[blockLength+1];
	int ioctlret = i2c_send_sequence(I2Chandle, i2c_event_sequence, 5+blockLength, data_with_dummy);
	
	
	free(i2c_event_sequence);
	if(ioctlret != 2)
	{
		
		return -1;
	}
	
	for(int i=1; i<=blockLength; i++)
	{
		bytes_from_I2Cdevice[i-1] = data_with_dummy[i];
	}
	
	
	return 1;
}
Exemple #16
0
/* =============================================================================
  =========================================================================== */
void Lidar::read(char myAddress, int numOfBytes, uint8_t arrayToSave[2], bool monitorBusyFlag){
  int busyFlag = 0;
  if(monitorBusyFlag)
  {
    busyFlag = 1;
  }
  int busyCounter = 0;
  //run while busy
  while(busyFlag != 0)
  {
	uint16_t busy_sequence[] = {0xc4, 0x01, I2C_RESTART, 0xc5, I2C_READ};
	uint8_t busyCheck;

    int nackCatcher = i2c_send_sequence(Lidar::handle, busy_sequence, 5, &busyCheck);
	//checks for nack
    if(nackCatcher != 1){Lidar::nack = true;}

	//ands busyCheck with 00000001 to only use the last bit
	busyFlag = 0x01 & busyCheck;

    busyCounter++;
    if(busyCounter > 9999)
	{
      goto bailout;
    }
  }
  //runs when not busy
  if(busyFlag == 0)
  {
	uint16_t read_sequence1[] = {0xc4, (int)myAddress, I2C_RESTART, 0xc5, I2C_READ};
	uint16_t read_sequence2[] = {0xc4, 0x0f, I2C_RESTART, 0xc5, I2C_READ};
	uint16_t read_sequence3[] = {0xc4, 0x10, I2C_RESTART, 0xc5, I2C_READ};
	//int16_t twoBytesSave;

	//reads either 1 or 2 bytes and saves to arrayToSave(need to fix 2 byte arrayToSave)
	if (numOfBytes == 1)
	{
	    int nackCatcher = i2c_send_sequence(Lidar::handle, read_sequence1, 5, &arrayToSave[0]);
		//detects failed write
		if(nackCatcher != 1){Lidar::nack = true;}
	}
	else
	{
		//two separate reads of registers
		//read low bit
		int nackCatcher = i2c_send_sequence(Lidar::handle, read_sequence2, 5, &arrayToSave[0]);
		//detects failed read
    if(nackCatcher != 1){Lidar::nack = true;}
    //read high bit
		nackCatcher = i2c_send_sequence(Lidar::handle, read_sequence3, 5, &arrayToSave[1]);
		//detects failed read
		if(nackCatcher != 1){Lidar::nack = true;}
		/*
		Autoincrement read, requires a pointer to a buffer I don't know how to create
	    int nackCatcher = i2c_send_sequence(Lidar::handle, read_sequence2, 6, (uint8_t)&twoBytesSave);
		//detects failed write
		if(nackCatcher != 1){Lidar::nack = true;}
		//separates the 2 Bytes
		arrayToSave[0] = twoBytesSave & 0xFF;
		arrayToSave[1] = twoBytesSave >> 8;
		*/
	}
  }
  //if busy for a long time bails
  if(busyCounter > 9999)
  {
    bailout:
      busyCounter = 0;
  }
}
Exemple #17
0
void LED_linkedSend()
{
	// Check if we've updated all the ISSI chips for this frame
	if ( LED_chipSend >= ISSI_Chips_define )
	{
		// Now ready to update the frame buffer
		Pixel_FrameState = FrameState_Update;

		// Finished sending the buffer, exit linked send
		return;
	}

	// Update ISSI Frame State
	Pixel_FrameState = FrameState_Sending;

	// Lookup bus number
	uint8_t bus = LED_ChannelMapping[ LED_chipSend ].bus;

	/*
	// Debug
	dbug_msg("Linked Send: chip(");
	printHex( LED_chipSend );
	print(")addr(");
	printHex( LED_pageBuffer[ LED_chipSend ].i2c_addr );
	print(")reg(");
	printHex( LED_pageBuffer[ LED_chipSend ].reg_addr );
	print(")len(");
	printHex( sizeof( LED_Buffer ) / 2 );
	print(")data[](");
	//for ( uint8_t c = 0; c < 9; c++ )
	for ( uint8_t c = 0; c < sizeof( LED_Buffer ) / 2 - 2; c++ )
	{
		printHex( LED_pageBuffer[ LED_chipSend ].buffer[c] );
		print(" ");
	}
	print("..)" NL);
	*/

	// Artificial delay to assist i2c bus
	const uint32_t delay_tm = ISSI_SendDelay;
	//delay_us( delay_tm );

	// Send, and recursively call this function when finished
	while ( i2c_send_sequence(
		bus,
#if ISSI_Chip_31FL3731_define == 1
		/* Brightness emulation */
		(uint16_t*)&LED_pageBuffer_brightness[ LED_chipSend ],
#else
		(uint16_t*)&LED_pageBuffer[ LED_chipSend ],
#endif
		sizeof( LED_Buffer ) / 2,
		0,
		LED_linkedSend,
		0
	) == -1 )
		delay_us( delay_tm );

	// Increment chip position
	LED_chipSend++;
}
Exemple #18
0
/**
 *  @fn sleep_tsl2561
 *  @author TheFwGuy
 *  @brief Put TSL2561 in sleep
 *
 *  @param None
 *  @return None
 */
void sleep_tsl2561(void)
{
   while(!i2c_done());
   i2c_send_sequence((uint16_t *)seqsl, TSL2561_SLEEP_LEN, (uint8_t *)recseq, 0);
}