void init_MMA7660FC(void)
{
  int x, y, z;
  unsigned char val = 0;
  bt_accelInitFlag = 1;
  if(!eei2cLockFlag)
  {
    leds(0b111111);
    eei2cLock = locknew();
    lockclr(eei2cLock);
    eei2cLockFlag = 1;
  }
  if(!st_eeInitFlag) ee_init();
  while(lockset(eei2cLock));  
  i2c_out(st_eeprom, MMA7660_I2C, 
          MODE, 1, &val, 1);
  i2c_out(st_eeprom, MMA7660_I2C, 
          INTSU, 1, &val, 1);
  i2c_out(st_eeprom, MMA7660_I2C, 
          SR, 1, &val, 1);
  val = 0xC1;
  i2c_out(st_eeprom, MMA7660_I2C, 
          MODE, 1, &val, 1);
  i2c_stop(st_eeprom);        
  lockclr(eei2cLock);
  accels(&x, &y, &z);        
}
Example #2
0
void i2c_write(unsigned char addr, unsigned char *buf, int len)
{
    int i;

    i2c_start();
    i2c_out(addr << 1);

    for (i = 0; i < len; i++)
	i2c_out(*buf++);
    i2c_stop();
}
void ee_putByte(char value, int addr)
{
  if(!eeInitFlag) ee_init();
  unsigned char addrArray[] = {(char)(addr >> 8), (char)(addr&0xFF)};
  int n = i2c_out(eeprom, 0xA0, addrArray, 2, &value, 1);
  while(i2c_poll(eeprom, 0xA0)); 
}
Example #4
0
/**
 * Turn display backlight on or off.
 * \param p      Pointer to driver's private data structure.
 * \param state  New backlight status.
 */
void i2c_HD44780_backlight(PrivateData *p, unsigned char state)
{
	if ( p->i2c_backlight_invert == 0 )
		p->backlight_bit = ((!p->have_backlight||state) ? 0 : p->i2c_line_BL);
	else // Inverted backlight - npn transistor
		p->backlight_bit = ((p->have_backlight && state) ? p->i2c_line_BL : 0);
	i2c_out(p, p->backlight_bit);
}
Example #5
0
void i2c_read(unsigned char addr, unsigned char *buf, int len)
{
    int i;

    i2c_start();
    i2c_out((addr << 1) | 1);

    for (i = 0; i < len; i++)
	*buf++ = i2c_inACK();
    i2c_stop();
}
Example #6
0
/**
 * Send data or commands to the display.
 * \param p          Pointer to driver's private data structure.
 * \param displayID  ID of the display (or 0 for all) to send data to.
 * \param flags      Defines whether to end a command or data.
 * \param ch         The value to send.
 */
void
i2c_HD44780_senddata(PrivateData *p, unsigned char displayID, unsigned char flags, unsigned char ch)
{
	unsigned char enableLines = 0, portControl = 0;
	unsigned char h = (ch >> 4) & 0x0f;     // high and low nibbles
	unsigned char l = ch & 0x0f;

	if (flags == RS_INSTR)
		portControl = 0;
	else //if (flags == RS_DATA)
		portControl = RS;

	portControl |= p->backlight_bit;

	enableLines = EN;

	i2c_out(p, portControl | h);
	if (p->delayBus)
		p->hd44780_functions->uPause(p, 1);
	i2c_out(p, enableLines | portControl | h);
	if (p->delayBus)
		p->hd44780_functions->uPause(p, 1);
	i2c_out(p, portControl | h);

	i2c_out(p, portControl | l);
	if (p->delayBus)
		p->hd44780_functions->uPause(p, 1);
	i2c_out(p, enableLines | portControl | l);
	if (p->delayBus)
		p->hd44780_functions->uPause(p, 1);
	i2c_out(p, portControl | l);
}
Example #7
0
/**
  * @brief  Set i2c interface speed
  * @param[in] dev i2c device structure pointer
  * @param[in] sp i2c speed
  * @return always 0
  */
static int32_t _i2cSetSpeed(i2c_dev *dev, int32_t sp)
{
    uint32_t d;

    if( sp != 100 && sp != 400)
        return(I2C_ERR_NOTTY);

    d = I2C_INPUT_CLOCK/(sp * 5) -1;

    i2c_out(dev, d & 0xffff, I2C_DIVIDER);

    return 0;
}
Example #8
0
/**
  * @brief Read data from I2C slave.
  * @param[in] fd is interface number.
  * @param[in] buf is receive buffer pointer.
  * @param[in] len is receive buffer length.
  * @return read status.
  * @retval >0 length when success.
  * @retval I2C_ERR_BUSY Interface busy.
  * @retval I2C_ERR_IO Interface not opened.
  * @retval I2C_ERR_NODEV No such device.
  * @retval I2C_ERR_NACK Slave returns an erroneous ACK.
  * @retval I2C_ERR_LOSTARBITRATION arbitration lost happen.
  */
int32_t i2cRead(int32_t fd, uint8_t* buf, uint32_t len)
{
    i2c_dev *dev;

    if(fd != 1 && fd != 0)
        return(I2C_ERR_NODEV);

    dev = (i2c_dev *)( (uint32_t)&i2c_device[fd] );

    if(dev->openflag == 0)
        return(I2C_ERR_IO);

    if(len == 0)
        return 0;

    if(!i2cIsBusFree(dev))
        return(I2C_ERR_BUSY);

    if(len > I2C_MAX_BUF_LEN - 10)
        len = I2C_MAX_BUF_LEN - 10;

    dev->state = I2C_STATE_READ;
    dev->pos = 1;
    /* Current ISR design will get one garbage byte */
    dev->len = dev->subaddr_len + 1 + len + 2;  /* plus 1 unused char                           */
    dev->last_error = 0;

    _i2cCalcAddr(dev, I2C_STATE_READ);

    i2cEnable(dev);

    i2c_out(dev, dev->buffer[0] & 0xff, I2C_TxR);

    if(!i2cIsBusFree(dev))
        return(I2C_ERR_BUSY);

    _i2cCommand(dev, I2C_CMD_START | I2C_CMD_WRITE);

    while(dev->state != I2C_STATE_NOP);

    i2cDisable(dev);

    if(dev->last_error)
        return(dev->last_error);

    memcpy(buf, dev->buffer + dev->subaddr_len + 3, len);

    dev->subaddr += len;

    return len;
}
Example #9
0
/**
  * @brief Write data from I2C slave.
  * @param[in] fd is interface number.
  * @param[in] buf is transmit buffer pointer.
  * @param[in] len is transmit buffer length.
  * @return write status.
  * @retval >0 length when success.
  * @retval I2C_ERR_BUSY Interface busy.
  * @retval I2C_ERR_IO Interface not opened.
  * @retval I2C_ERR_NODEV No such device.
  * @retval I2C_ERR_NACK Slave returns an erroneous ACK.
  * @retval I2C_ERR_LOSTARBITRATION arbitration lost happen.
  */
int32_t i2cWrite(int32_t fd, uint8_t* buf, uint32_t len)
{
    i2c_dev *dev;

    if(fd != 1 && fd != 0)
        return(I2C_ERR_NODEV);

    dev = (i2c_dev *)( (uint32_t)&i2c_device[fd] );

    if(dev->openflag == 0)
        return(I2C_ERR_IO);

    if(len == 0)
        return 0;

    if(!i2cIsBusFree(dev))
        return(I2C_ERR_BUSY);

    if(len > I2C_MAX_BUF_LEN - 10)
        len = I2C_MAX_BUF_LEN - 10;

    memcpy(dev->buffer + dev->subaddr_len + 1 , buf, len);

    dev->state = I2C_STATE_WRITE;
    dev->pos = 1;
    dev->len = dev->subaddr_len + 1 + len;
    dev->last_error = 0;

    _i2cCalcAddr(dev, I2C_STATE_WRITE);

    i2cEnable(dev);

    i2c_out(dev, dev->buffer[0] & 0xff, I2C_TxR);

    if(!i2cIsBusFree(dev))
        return(I2C_ERR_BUSY);

    _i2cCommand(dev, I2C_CMD_START | I2C_CMD_WRITE);

    while(dev->state != I2C_STATE_NOP);

    i2cDisable(dev);

    if(dev->last_error)
        return(dev->last_error);

    dev->subaddr += len;

    return len;
}
Example #10
0
void I2C_ByteWrite(I2C_ADDR_t slaveAddress, uint8_t reg, uint8_t data)
{
    uint8_t num_bytes;
    uint8_t size = 0;
    
    if (reg > 0)
    {
        size = sizeof(reg);
    }

    lockset(lock);
    if (I2C_Ready(slaveAddress))
    {
        num_bytes = i2c_out(i2c_bus, slaveAddress, reg, size, &data, sizeof(data));
    }    
    lockclr(lock);
    print("\t\tWrote - addr %02x, reg %02x, data %02x, total %d\n", slaveAddress, reg, data, num_bytes);
}
int main()                                    // Main function
{
  eeBus = i2c_newbus(28,  29,   0);           // Set up I2C bus, get bus ID

                                              // Use eeBus to write to device
  i2c_out(eeBus, 0b1010000,                   // with I2C address 0b1010000,
          32768, 2, "abcdefg", 8);            // send address 32768 (2 bytes)
                                              // and "abc..." data (8 bytes)

  while(i2c_busy(eeBus, 0b1010000));          // Wait for EEPROM to finish  

  char testStr[] = {0, 0, 0, 0, 0, 0, 0, 0};  // Set up test string   

                                              // Use eeBus to read from device
  i2c_in(eeBus, 0b1010000,                    // with I2C address 0b1010000,
         32768, 2, testStr, 8);               // send address 32768 (2 bytes)
                                              // data in to testStr (8 bytes)

  print("testStr = %s \n", testStr);          // Display result
}
Example #12
0
/**
 * Send data or commands to the display.
 * \param p          Pointer to driver's private data structure.
 * \param displayID  ID of the display (or 0 for all) to send data to.
 * \param flags      Defines whether to end a command or data.
 * \param ch         The value to send.
 */
void
i2c_HD44780_senddata(PrivateData *p, unsigned char displayID, unsigned char flags, unsigned char ch)
{
	unsigned char portControl = 0;
	unsigned char h=0;
	unsigned char l=0;
	if( ch & 0x80 ) h |= p->i2c_line_D7;
	if( ch & 0x40 ) h |= p->i2c_line_D6;
	if( ch & 0x20 ) h |= p->i2c_line_D5;
	if( ch & 0x10 ) h |= p->i2c_line_D4;
	if( ch & 0x08 ) l |= p->i2c_line_D7;
	if( ch & 0x04 ) l |= p->i2c_line_D6;
	if( ch & 0x02 ) l |= p->i2c_line_D5;
	if( ch & 0x01 ) l |= p->i2c_line_D4;
	if (flags == RS_INSTR)
		portControl = 0;
	else //if (flags == RS_DATA)
		portControl = p->i2c_line_RS;

	portControl |= p->backlight_bit;

	i2c_out(p, portControl | h);
	if (p->delayBus)
		p->hd44780_functions->uPause(p, 1);
	i2c_out(p, p->i2c_line_EN | portControl | h);
	if (p->delayBus)
		p->hd44780_functions->uPause(p, 1);
	i2c_out(p, portControl | h);

	i2c_out(p, portControl | l);
	if (p->delayBus)
		p->hd44780_functions->uPause(p, 1);
	i2c_out(p, p->i2c_line_EN | portControl | l);
	if (p->delayBus)
		p->hd44780_functions->uPause(p, 1);
	i2c_out(p, portControl | l);
}
Example #13
0
/**
 * Turn display backlight on or off.
 * \param p      Pointer to driver's private data structure.
 * \param state  New backlight status.
 */
void i2c_HD44780_backlight(PrivateData *p, unsigned char state)
{
	p->backlight_bit = ((!p->have_backlight||state) ? 0 : BL);

	i2c_out(p, p->backlight_bit);
}
Example #14
0
static void
i2c_out(PrivateData *p, unsigned char val)
{
	char data[2];
	int datalen;
	static int no_more_errormsgs=0;
#ifdef HAVE_DEV_IICBUS_IIC_H
	struct iiccmd cmd;
	bzero(&cmd, sizeof(cmd));
#endif

	if (p->port & I2C_PCAX_MASK) { // we have a PCA9554 or similar, that needs a 2-byte command
		data[0]=1; // command: read/write output port register
		data[1]=val;
		datalen=2;
	} else { // we have a PCF8574 or similar, that needs a 1-byte command
		data[0]=val;
		datalen=1;
	}

#ifdef HAVE_DEV_IICBUS_IIC_H
	cmd.slave = (p->port & I2C_ADDR_MASK) << 1;
	cmd.last = 1;
	cmd.count = datalen;
	cmd.buf = data;

	if (ioctl(p->fd, I2CWRITE, &cmd) < 0) {
#else /* HAVE_LINUX_I2C_DEV_H */
	if (write(p->fd,data,datalen) != datalen) {
#endif
		p->hd44780_functions->drv_report(no_more_errormsgs?RPT_DEBUG:RPT_ERR, "HD44780: I2C: i2c write data %u to address 0x%02X failed: %s",
			val, p->port & I2C_ADDR_MASK, strerror(errno));
		no_more_errormsgs=1;
	}
}

#define DEFAULT_DEVICE		"/dev/i2c-0"


/**
 * Initialize the driver.
 * \param drvthis  Pointer to driver structure.
 * \retval 0       Success.
 * \retval -1      Error.
 */
int
hd_init_i2c(Driver *drvthis)
{
	PrivateData *p = (PrivateData*) drvthis->private_data;
	HD44780_functions *hd44780_functions = p->hd44780_functions;

	int enableLines = EN;
	char device[256] = DEFAULT_DEVICE;
#ifdef HAVE_DEV_IICBUS_IIC_H
	struct iiccmd cmd;
	bzero(&cmd, sizeof(cmd));
#endif

	p->backlight_bit = BL;

	/* READ CONFIG FILE */

	/* Get serial device to use */
	strncpy(device, drvthis->config_get_string(drvthis->name, "Device", 0, DEFAULT_DEVICE), sizeof(device));
	device[sizeof(device)-1] = '\0';
	report(RPT_INFO,"HD44780: I2C: Using device '%s' and address 0x%02X for a %s",
		device, p->port & I2C_ADDR_MASK, (p->port & I2C_PCAX_MASK) ? "PCA9554(A)" : "PCF8574(A)");

	// Open the I2C device
	p->fd = open(device, O_RDWR);
	if (p->fd < 0) {
		report(RPT_ERR, "HD44780: I2C: open i2c device '%s' failed: %s", device, strerror(errno));
		return(-1);
	}

	// Set I2C address
#ifdef HAVE_DEV_IICBUS_IIC_H
	cmd.slave = (p->port & I2C_ADDR_MASK) << 1;
	cmd.last = 0;
	cmd.count = 0;
	if (ioctl(p->fd, I2CRSTCARD, &cmd) < 0) {
		report(RPT_ERR, "HD44780: I2C: reset bus failed: %s", strerror(errno));
		return -1;
	}
	if (ioctl(p->fd, I2CSTART, &cmd) < 0) {
		report(RPT_ERR, "HD44780: I2C: set address to 0x%02X: %s", p->port & I2C_ADDR_MASK, strerror(errno));
		return -1;
	}
#else /* HAVE_LINUX_I2C_DEV_H */
	if (ioctl(p->fd,I2C_SLAVE, p->port & I2C_ADDR_MASK) < 0) {
		report(RPT_ERR, "HD44780: I2C: set address to '%i': %s", p->port & I2C_ADDR_MASK, strerror(errno));
		return(-1);
	}
#endif

	if (p->port & I2C_PCAX_MASK) { // we have a PCA9554 or similar, that needs special config
		char data[2];
		data[0] = 2; // command: set polarity inversion
		data[1] = 0; // -> no polarity inversion
		if (write(p->fd,data,2) != 2) {
			report(RPT_ERR, "HD44780: I2C: i2c set polarity inversion failed: %s", strerror(errno));
		}
		data[0] = 3; // command: set output direction
		data[1] = 0; // -> all pins are outputs
		if (write(p->fd,data,2) != 2) {
			report(RPT_ERR, "HD44780: I2C: i2c set output direction failed: %s", strerror(errno));
		}
	}

	hd44780_functions->senddata = i2c_HD44780_senddata;
	hd44780_functions->backlight = i2c_HD44780_backlight;
	hd44780_functions->close = i2c_HD44780_close;

	// powerup the lcd now
	/* We'll now send 0x03 a couple of times,
	 * which is in fact (FUNCSET | IF_8BIT) >> 4 */
	i2c_out(p, 0x03);
	if (p->delayBus)
		hd44780_functions->uPause(p, 1);

	i2c_out(p, enableLines | 0x03);
	if (p->delayBus)
		hd44780_functions->uPause(p, 1);
	i2c_out(p, 0x03);
	hd44780_functions->uPause(p, 15000);

	i2c_out(p, enableLines | 0x03);
	if (p->delayBus)
		hd44780_functions->uPause(p, 1);
	i2c_out(p, 0x03);
	hd44780_functions->uPause(p, 5000);

	i2c_out(p, enableLines | 0x03);
	if (p->delayBus)
		hd44780_functions->uPause(p, 1);
	i2c_out(p, 0x03);
	hd44780_functions->uPause(p, 100);

	i2c_out(p, enableLines | 0x03);
	if (p->delayBus)
		hd44780_functions->uPause(p, 1);
	i2c_out(p, 0x03);
	hd44780_functions->uPause(p, 100);

	// now in 8-bit mode...  set 4-bit mode
	i2c_out(p, 0x02);
	if (p->delayBus)
		hd44780_functions->uPause(p, 1);

	i2c_out(p, enableLines | 0x02);
	if (p->delayBus)
		hd44780_functions->uPause(p, 1);
	i2c_out(p, 0x02);
	hd44780_functions->uPause(p, 100);

	// Set up two-line, small character (5x8) mode
	hd44780_functions->senddata(p, 0, RS_INSTR, FUNCSET | IF_4BIT | TWOLINE | SMALLCHAR);
	hd44780_functions->uPause(p, 40);

	common_init(p, IF_4BIT);

	return 0;
}
void hyperterminal(void)
{
  int menu_no;          //hyperterminal switch variable
  char reg_in[1];       //char to store single byte read in by I2C
  char reg_out[1];      //char to store single byte to be written to part with I2C
  int x, y, z;          //Stores XYZ output after conversion from reister values
  int dt, t;            //stores timing values: t holds a clock value, dt is compared to it
  char buffer[6];       //buffer which stores 56 register read backs from a multibyte i2c readback
  
  

  int tic = CNT; 
  float temp = 0;
       
  float sum = 0;
  
  float xsum = 0;
  float ysum = 0;
  float zsum = 0;
  
  float prev = 0;
  float current = 0;
  




	
  while(1)
	{
    //menu options
	 print("\n\nSelect a menu option with a number, followed by enter\n");
    print("1. Exit hyperterminal \n");
    print("2. Read back ID register\n");
    print("3. Setup: set clock to Xgyro, sample rate div to 9 (100Hz output), filter 98Hz\n");
    print("4. Stream 5 seconds of data\n");
    print("5. Stream 30 seconds of data \n");
    print("6. 2 second bias calculation\n");
    print("7. 10 second cumulative angle calculation\n");
    print("8. Timing readout debugger\n");
    
    //get user input. Must be a number! follow with enter
    scanf("%d", &menu_no);

    switch(menu_no)
    {
      
      case 1:
        return;  //Exit hyperterminal
        break; 
        
        
    	case 2:    //dev ID readback
    	i2c_in(DofBus, GyroAddr, 0x00, 1, (unsigned char*)reg_in, 1);  
      print("ID readback: 0x%x\n\n", reg_in[0]);
      break;

      case 3: //see ITG3200 data sheet
       	reg_out[0] = 0x01; //Power control: Set clk to xGyro, all gyros on
  		  i2c_out(DofBus, GyroAddr, 0x3E, 1, (unsigned char*)reg_out, 1);
        i2c_in(DofBus, GyroAddr, 0x3E, 1, (unsigned char*)reg_in, 1);
      	print("Power control: 0x%x\n", reg_in[0]);

    		reg_out[0] = 0x09; //Sample rate divider: set to 9, gives x/10 = sample output rate, where x is internal rate
  			i2c_out(DofBus, GyroAddr, 0x15, 1, (unsigned char*)reg_out, 1);
        i2c_in(DofBus, GyroAddr, 0x15, 1, (unsigned char*)reg_in, 1);
        print("Rate divider: 0x%x\n", reg_in[0]);

        reg_out[0] = 0x1A; //scale selection and digital filter: bits 4&5: = 0b11: full scale. bits 2 10: 
    	  i2c_out(DofBus, GyroAddr, 0x16, 1, (unsigned char*)reg_out, 1);
        i2c_in(DofBus, GyroAddr, 0x16, 1, (unsigned char*)reg_in, 1);
        print("Digital Filter: 0x%x\n", reg_in[0]);

    		break;
    		


  		case 4: 
        dt = CLKFREQ*5;    // 5 second timeout
        //CLKFREQ contains the number of ticks in one second, CNT returns current tick no
        t = CNT;           // Mark current time by storing in t
        while(CNT - t < dt)    // Repeat until timeout
        {
    		  i2c_in(DofBus, GyroAddr, 0x1D, 1, (unsigned char*)buffer, 6);   //read 6 registers starting at 0x1D - data registers
          x = twos2signed(buffer[0], buffer[1]) - xGyroDrift;    //convert 2s compliment split into 2 registers into signed
          y = twos2signed(buffer[2], buffer[3]) - yGyroDrift;    //and subtract gyro drift
          z = twos2signed(buffer[4], buffer[5]) - zGyroDrift;
          print("\nX: %d  Y: %d  Z: %d\n", x, y, z); 
          pause(500);
          
        }
    		break;

      case 5: 
        dt = CLKFREQ*30;    // 30 second timeout
        t = CNT;           // Mark current time by storing in t
        while(CNT - t < dt)    // Repeat until timeout
        {
          i2c_in(DofBus, GyroAddr, 0x1D, 1, (unsigned char*)buffer, 6);
          x = twos2signed(buffer[0], buffer[1]) - xGyroDrift;
          y = twos2signed(buffer[2], buffer[3]) - yGyroDrift;
          z = twos2signed(buffer[4], buffer[5]) - zGyroDrift;
          print("\nX: %d  Y: %d  Z: %d\n", x, y, z);
          pause(500);
        }
        break;
      
      
      
        
      case 6:
        tic = CNT; 
        dt = CLKFREQ/100;   //100HZ cycle  NB. integer math 
        xsum = 0;
        ysum = 0;
        zsum = 0;
       
        
        for (int i = 0; i < 400; i++) //  4 seconds
        {
          t = CNT; // Mark current time by storing in t
          i2c_in(DofBus, GyroAddr, 0x1D, 1, (unsigned char*)buffer, 6);
          xsum += ((float)twos2signed(buffer[0], buffer[1])) / 400;   //running 400 piece average
          ysum += ((float)twos2signed(buffer[2], buffer[3])) / 400;
          zsum += ((float)twos2signed(buffer[4], buffer[5])) / 400;
        
          while(CNT - t < dt){}
        }                   
        
      
        
              
        
        print("\nelapsed time: %f\n", ((float)(CNT-tic))/CLKFREQ );
        print("xdrift: %f, ydrift: %f, zdrift: %f\n", xsum, ysum, zsum);
        break;
        
        
        
        
      case 7:
        //currently set up for y axis
      
        //by taking the variance over a known total rotation (ie rotating exactly 90 degrees, and reading 93 degrees)
        //a variance in the  Gyro scaling factor can be estimated as (93-90)/90 = 3.33%
        //print("\nGyro conversion factor: %f\n", GyroConv);
        dt = CLKFREQ/100;   //200HZ cycle  NB integer math here
        prev = 0;
        current = 0;
        sum = 0;
                
        //aqquire first sample
        t = CNT; // Mark current time by storing in t
        i2c_in(DofBus, GyroAddr, 0x1D, 1, (unsigned char*)buffer, 6);
        prev= (    (float)twos2signed(buffer[2], buffer[3])   -yGyroDrift) * yGyroConv;    
        while(CNT - t < dt);
        
        //calculate angle over time by taking consecutive values, and using tustins approximation to integration (check name?)
        //sum this angle over consecutive points
        for (int i = 0; i < 1000; i++) //  1000 samples/100HZ = 10 seconds.
        {
          t = CNT; // Mark current time by storing in t
          i2c_in(DofBus, GyroAddr, 0x1D, 1, (unsigned char*)buffer, 6);
          current= (    (float)twos2signed(buffer[2], buffer[3])   -yGyroDrift) * yGyroConv;


          sum +=     (current +prev) * 0.01/2;    //Tustins approximation to integration
          
          prev = current;

          while(CNT - t < dt){}
        }          
        print("\nFinal Rotation: %f\n", sum);

        break;
        
        
        
        
        
        
        
        
        
        
        
        
        
      case 8:
        //debugging case - replace code with whatever suits
        
        print("\n\nCLKFREQ: %d\n", CLKFREQ);
        tic = CNT; 
        dt = CLKFREQ/2;   //2HZ cycle  NB. integer math 
        
        
        sum = 0;
        
        for (int i = 0; i < 10; i++) //  5 seconds
        {
          t = CNT; // Mark current time by storing in t
          pause(1);
          //print("Reading: %f      sum: %f\n", temp, sum);
          
          while(CNT - t < dt){}
        }          
        
       
              
        print("\nCNT-tic: %d\n", CNT-tic);
        print("elapsed time: %f\n", ((float)(CNT-tic))/CLKFREQ );
        
        
        
        break;

        
      default:
        //code
        break;
    }
	}
}
Example #16
0
/**
  * @brief Interrupt service routine for i2c-1
  * @param[in] None
  * @return None
  */
static void i2c1ISR(void)
{
    int32_t csr, val;
    i2c_dev *dev;

    dev = (i2c_dev *) ( (uint32_t)&i2c_device[1] );

    csr = i2c_in(dev, I2C_CSR);
    csr |= 0x04;

    i2c_out(dev, csr, I2C_CSR);  /* clear interrupt flag */

    if(dev->state == I2C_STATE_NOP)
        return;

    if((csr & 0x800) && bNackValid)     /* NACK only valid in WRITE */
    {
        dev->last_error = I2C_ERR_NACK;
        _i2cCommand(dev, I2C_CMD_STOP);
        dev->state = I2C_STATE_NOP;
    }
    else if(csr & 0x200)                /* Arbitration lost */
    {
        sysprintf("Arbitration lost\n");
        dev->last_error = I2C_ERR_LOSTARBITRATION;
        dev->state = I2C_STATE_NOP;
    }
    else if(!(csr & 0x100))             /* transmit complete */
    {
        if(dev->pos < dev->subaddr_len + 1)     /* send address state */
        {
            val = dev->buffer[dev->pos++] & 0xff;
            i2c_out(dev, val, I2C_TxR);
            _i2cCommand(dev, I2C_CMD_WRITE);
        }
        else if(dev->state == I2C_STATE_READ)
        {

            /* sub address send over , begin restart a read command */

            if(dev->pos == dev->subaddr_len + 1)
            {
                val = dev->buffer[dev->pos++];
                i2c_out(dev, val, I2C_TxR);
                _i2cCommand(dev, I2C_CMD_START | I2C_CMD_WRITE);
            }
            else
            {

                dev->buffer[dev->pos++] = i2c_in(dev, I2C_RxR) & 0xff;

                if( dev->pos < dev->len)
                {
                    if(dev->pos == dev->len -1 )    /* last character */
                        _i2cCommand(dev, I2C_CMD_READ |
                                    I2C_CMD_STOP |
                                    I2C_CMD_NACK);
                    else
                        _i2cCommand(dev, I2C_CMD_READ);
                }
                else
                {
                    dev->state = I2C_STATE_NOP;
                }
            }
        }
        else if(dev->state == I2C_STATE_WRITE)  /* write data */
        {

            if( dev->pos < dev->len)
            {
                val = dev->buffer[dev->pos];

                i2c_out(dev, val, I2C_TxR);

                if(dev->pos == dev->len -1 )    /* last character */
                    _i2cCommand(dev, I2C_CMD_WRITE| I2C_CMD_STOP);
                else
                    _i2cCommand(dev, I2C_CMD_WRITE);

                dev->pos ++;
            }
            else
            {
                dev->state = I2C_STATE_NOP;
            }
        }
    }
}
Example #17
0
/**
  * @brief  Configure i2c command
  * @param[in] dev i2c device structure pointer
  * @param[in] cmd command
  * @return None
  */
static void _i2cCommand(i2c_dev *dev, int32_t cmd)
{
    bNackValid = (cmd & I2C_CMD_WRITE) ? 1 : 0;
    i2c_out(dev, cmd, I2C_CMDR);
}