Example #1
0
int LSM303DLH_m_set_ctrl_reg(int reg, uint8_t val){
	i2c_master_xact_t mag;
	uint8_t reg_addr;
	switch(reg){
	case 1:
		reg_addr = C_CRA_REG_M;
		break;
	case 2:
		reg_addr = C_CRB_REG_M;
		break;
	case 3:
		reg_addr = C_MR_REG_M;
		break;
	default:
		return 0;
	}


	mag.i2c_tx_buffer[0] = i2c_create_write_address(LSM303DLH_ADDR_M);
	mag.i2c_tx_buffer[1] = reg_addr;
	mag.i2c_tx_buffer[2] = val;
	mag.write_length     = 0x3;

	start_i2c_master_xact(i2c_channel, &mag, empty_callback);
	return 1;
}
Example #2
0
void LSM303DLH_m_get_data(XACT_FnCallback* callback_fn){
	i2c_master_xact_t       gyrodata;
	gyrodata.i2c_tx_buffer[0]  = i2c_create_write_address(LSM303DLH_ADDR_M);
	gyrodata.i2c_tx_buffer[1]  = C_OUT_X_L_M | C_AUTO_INCREMENT; //Auto increment through, temperature, data status,
	gyrodata.write_length      = 0x2;					   //then all the axis data registers
	gyrodata.i2c_tx_buffer[2] =  i2c_create_read_address(LSM303DLH_ADDR_M);
	gyrodata.read_length       = 0x7;

	start_i2c_master_xact(i2c_channel, &gyrodata, callback_fn);
}
Example #3
0
void LSM303DLH_init_m(i2c_iface i2c_ch){
	i2c_master_xact_t       gyroinit;

	i2c_channel = i2c_ch;

	gyroinit.i2c_tx_buffer[0]  = i2c_create_write_address(LSM303DLH_ADDR_M);
    gyroinit.i2c_tx_buffer[1]  = C_CRA_REG_M | C_AUTO_INCREMENT;
    gyroinit.i2c_tx_buffer[2]  = C_ODR_7_5 & C_CRA_REG_M_MASK; 	//CRA_REG_M
    gyroinit.i2c_tx_buffer[3]  = C_RANGE_1_9 & C_CRB_REG_M_MASK; 	//CRB_REG_M
    gyroinit.i2c_tx_buffer[4]  = C_CONT_CONV & C_MR_REG_M_MASK;	//MR_REG_M
    gyroinit.write_length      = 0x5;
    gyroinit.read_length       = 0x0;

    start_i2c_master_xact(i2c_channel, &gyroinit, &empty_callback);
}
/*
 * i2ceepromtask
 */
static void i2ceepromtask(void *pvParameters) {
    uint32_t  x             = 0;
    uint32_t  i             = 0;
    uint32_t  write         = 1;

    signed    portCHAR      theChar;
    signed    portBASE_TYPE status;

    const int interval      = 100000;

    i2c_master_xact_t       xact_s;

    FIO0CLR = (1<<6);            // turn off p0.6on olimex 2378 Sdev board

    for(i=0; i<I2C_MAX_BUFFER; i++) {
        xact_s.I2C_TX_buffer[i]  = 0;
        xact_s.I2C_RD_buffer[i]  = 0;
    }
    xact_s.write_length          = 0;
    xact_s.read_length           = 0;
    xact_s.I2Cext_slave_address  = 0;

    for(;;) {
        x++;

        if (x == interval) {
            FIO0CLR = (1<<6);    // turn off  p0.6 on olimex 2378 Sdev board
            FIO1CLR = (1<<19);   // turn off led  on olimex 2378 Sdev board

        } else if (x >= (2*interval)) {
            FIO0SET = (1<<6);    // turn on p0.6 on olimex 2378 Sdev board
            FIO1SET = (1<<19);   // turn on led on olimex 2378 dev board

            x = 0;

            printf2("i2c eeprom 1 read byte...\r\n");
            xact_s.I2C_TX_buffer[0] =  i2c_create_write_address(EEPROM_ADDR);
            xact_s.I2C_TX_buffer[1] =  0x0;
            xact_s.I2C_TX_buffer[2] =  0x0;
            xact_s.write_length     =  0x3;
            xact_s.I2C_TX_buffer[3] =  i2c_create_read_address(EEPROM_ADDR);
//            printf2("read address is: 0x%X\r\n", xact_s.I2C_TX_buffer[4] );
            xact_s.read_length      =  0x1;
            I2C0_master_xact(&xact_s);

            I2C0_get_read_data(&xact_s);

            printf2("Read data is 0x%X\n\r",xact_s.I2C_RD_buffer[0]);


            /*
            printf2("i2c write byte task...\r\n");

            xact_s.I2C_TX_buffer[0] =  i2c_create_write_address(EEPROM_ADDR);
            xact_s.I2C_TX_buffer[1] =  0x0;
            xact_s.I2C_TX_buffer[2] =  0x0;
            xact_s.I2C_TX_buffer[3] =  0x0b;
            xact_s.write_length     =  0x4;
            xact_s.read_length      =  0x0;
            I2C0_master_xact(&xact_s);

            printf2("i2c write bytes task...\r\n");

            xact_s.I2C_TX_buffer[0] =  i2c_create_write_address(EEPROM_ADDR);
            xact_s.I2C_TX_buffer[1] =  0x0;
            xact_s.I2C_TX_buffer[2] =  0x0;
            xact_s.I2C_TX_buffer[3] =  0x0b;
            xact_s.I2C_TX_buffer[4] =  0x0c;
            xact_s.I2C_TX_buffer[5] =  0x0d;
            xact_s.I2C_TX_buffer[6] =  0x0e;
            xact_s.I2C_TX_buffer[7] =  0x0f;
            xact_s.write_length     =  0x8;
            xact_s.read_length      =  0x0;
            I2C0_master_xact(&xact_s);


            printf2("i2c eeprom read byte using repeated start...\r\n");
            xact_s.I2C_TX_buffer[0] =  i2c_create_write_address(EEPROM_ADDR);
            xact_s.I2C_TX_buffer[1] =  0x0;
            xact_s.I2C_TX_buffer[2] =  0x0;
            xact_s.write_length     =  0x3;
            xact_s.I2C_TX_buffer[3] =  i2c_create_read_address(EEPROM_ADDR);
            printf2("read address is: 0x%X\r\n", xact_s.I2C_TX_buffer[3] );
            xact_s.read_length      =  0x1;
            I2C0_master_xact(&xact_s);

            I2C0_get_read_data(&xact_s);

            printf2("Read data is 0x%X\n\r",xact_s.I2C_RD_buffer[0]);

            printf2("i2c sequential read\r\n");
            xact_s.I2C_TX_buffer[0] =  i2c_create_read_address(EEPROM_ADDR);
            xact_s.write_length     =  0x1;
            printf2("read address is: 0x%X\r\n", xact_s.I2C_TX_buffer[3] );
            xact_s.read_length      =  0x10;
            I2C0_master_xact(&xact_s);
            
            I2C0_get_read_data(&xact_s);

            for (i=0; i<0x10; i++){
                printf2("Read data is 0x%X\n\r",xact_s.I2C_RD_buffer[i]);
            }
            */
        }
    }
}
Example #5
0
/*
 * blinkm_task_i2c0
 * 
 */
void blinkm_task_i2c0() {

    uint32_t i, on;

    uart0_putstring("i2c0 Write Color Task...\n");
    xact0_s.i2c_tx_buffer[0]  =  i2c_create_write_address(BLINKM_ADDR);
    xact0_s.i2c_tx_buffer[1]  =  'o';
    xact0_s.i2c_tx_buffer[2]  =  'f';
    xact0_s.i2c_tx_buffer[3]  =   5;
    xact0_s.i2c_tx_buffer[4]  =  'c';
    xact0_s.i2c_tx_buffer[5]  =  0x50;
    xact0_s.i2c_tx_buffer[6]  =  0x10;
    xact0_s.i2c_tx_buffer[7]  =  0x03;
    xact0_s.write_length      =  0x08;
    xact0_s.read_length       =  0x0;
    xact0_s.xact_active       =  0x1;
    xact0_s.xact_success      =  0x0;

    start_i2c0_master_xact(&xact0_s, &xact_callback);

    // poll
    STAT_LED_OFF;
    i  = 0;
    on = 0;

    while(is_binsem_locked(&i2c0_binsem_g)== 1) {
        i++;
        if(i % BLINKM_POLL_WAITTICKS == 0) {
            if(on==0) {
                STAT_LED_ON;
                on = 1;
            } else {
                on = 0;
                STAT_LED_OFF;
            }
            i = 0;
        }
    }

    if(xact0_s.xact_success == 1) {
        uart0_putstring("i2c0 write xaction success.\n");
    } else {
        uart0_putstring("i2c0 write xaction fail.\n");
    }

    uart0_putstring("i2c0 Read Color Task...\n");

    xact0_s.i2c_tx_buffer[0] =  i2c_create_write_address(BLINKM_ADDR);
    xact0_s.i2c_tx_buffer[1] =  'g';
    xact0_s.write_length     =  0x02;
    xact0_s.i2c_tx_buffer[2] =  i2c_create_read_address(BLINKM_ADDR);
    xact0_s.read_length      =  0x03;
    xact0_s.xact_active      =  0x1;
    xact0_s.xact_success     =  0x0;
    start_i2c0_master_xact(&xact0_s, &xact_callback);

    // poll
    STAT_LED_OFF;
    i  = 0;
    on = 0;

    while(is_binsem_locked(&i2c0_binsem_g)== 1) {
        i++;
        if(i % BLINKM_POLL_WAITTICKS == 0) {
            if(on==0) {
                STAT_LED_ON;
                on = 1;
            } else {
                on = 0;
                STAT_LED_OFF;
            }
            i = 0;
        }
    }

    if(xact0_s.xact_success == 1) {
        uart0_putstring("i2c0 read xaction success.\n");
    } else {
        uart0_putstring("i2c0 read xaction fail.\n");
    }

    uart0_putstring("Read data 0 is 0x");
    uart0_putstring(util_uitoa(xact0_s.i2c_rd_buffer[0],16));
    if(xact0_s.i2c_rd_buffer[0] != 0x50) uart0_putstring("Error, wrong value, should be 0x50"); 
    uart0_putstring("\n");
    uart0_putstring("Read data 1 is 0x");
    uart0_putstring(util_uitoa(xact0_s.i2c_rd_buffer[1],16));
    if(xact0_s.i2c_rd_buffer[1] != 0x10) uart0_putstring("Error, wrong value, should be 0x10"); 
    uart0_putstring("\n");
    uart0_putstring("Read data 2 is 0x");
    uart0_putstring(util_uitoa(xact0_s.i2c_rd_buffer[2],16));
    if(xact0_s.i2c_rd_buffer[2] != 0x3) uart0_putstring("Error, wrong value, should be 0x3"); 
    uart0_putstring("\n");
}
Example #6
0
/*
 * eeprom_task
 * 
 */
void eeprom_task() {

    uint32_t i;

    /**************************************/
    i2c_init_state( &xact_s) ;

    uart0_putstring("eeprom i2c write 1 byte...\n");

    xact_s.i2c_tx_buffer[0] =  i2c_create_write_address(EEPROM_ADDR);
    // printf_lpc(UART0,"address is: 0x%x\n",xact_s.i2c_tx_buffer[0]);
    xact_s.i2c_tx_buffer[1] =  0x0;
    xact_s.i2c_tx_buffer[2] =  0x0;
    xact_s.i2c_tx_buffer[3] =  0xe;
    xact_s.write_length     =  0x4;
    xact_s.read_length      =  0x0;
    xact_s.xact_active      =  0x1;
    xact_s.xact_success     =  0x0;

    start_i2c0_master_xact(&xact_s, &xact_callback);

    poll_wait();

    if(xact_s.xact_success == 1) {
        uart0_putstring("eeprom i2c write byte xaction success.\n");
    } else {
        uart0_putstring("eeprom i2c write byte  xaction fail.\n");
    }

   /**************************************/
   i2c_init_state( &xact_s) ;
   uart0_putstring("eeprom i2c read byte using repeated start...\n");
   xact_s.i2c_tx_buffer[0] =  i2c_create_write_address(EEPROM_ADDR);
   xact_s.i2c_tx_buffer[1] =  0x0;
   xact_s.i2c_tx_buffer[2] =  0x0;
   xact_s.write_length     =  0x3;
   xact_s.i2c_tx_buffer[3] =  i2c_create_read_address(EEPROM_ADDR);
   xact_s.read_length      =  0x1;
   xact_s.xact_active      =  0x1;
   xact_s.xact_success     =  0x0;

   start_i2c0_master_xact(&xact_s, &xact_callback);

   poll_wait();

   if(xact_s.xact_success == 1) {
       uart0_putstring("eeprom i2c read byte xaction success.\n");
   } else {
       uart0_putstring("eeprom i2c read byte xaction fail.\n");
   }

   uart0_putstring("Read data 0 is 0x");
   uart0_putstring(util_uitoa(xact_s.i2c_rd_buffer[0],16));
   if(xact_s.i2c_rd_buffer[0] != 0xe) uart0_putstring("Error, wrong value, should be 0xe"); 
   uart0_putstring("\n");

   /**************************************/

   i2c_init_state( &xact_s) ;
   uart0_putstring("i2c write bytes task...\n");

   xact_s.i2c_tx_buffer[0] =  i2c_create_write_address(EEPROM_ADDR);
   xact_s.i2c_tx_buffer[1] =  0x0;
   xact_s.i2c_tx_buffer[2] =  0x0;
   xact_s.i2c_tx_buffer[3] =  0x0b;
   xact_s.i2c_tx_buffer[4] =  0x0c;
   xact_s.i2c_tx_buffer[5] =  0x0d;
   xact_s.i2c_tx_buffer[6] =  0x1e;
   xact_s.i2c_tx_buffer[7] =  0x0f;
   xact_s.write_length     =  0x8;
   xact_s.read_length      =  0x0;
   xact_s.xact_active      =  0x1;
   xact_s.xact_success     =  0x0;

   start_i2c0_master_xact(&xact_s, &xact_callback);

   poll_wait();

   if(xact_s.xact_success == 1) {
       uart0_putstring("eeprom i2c write bytes xaction success.\n");
   } else {
       uart0_putstring("eeprom i2c write bytes xaction fail.\n");
   }


   /**************************************/
   i2c_init_state( &xact_s) ;
   uart0_putstring("i2c read bytes task...\n");

   xact_s.i2c_tx_buffer[0] =  i2c_create_write_address(EEPROM_ADDR);
   xact_s.i2c_tx_buffer[1] =  0x0;
   xact_s.i2c_tx_buffer[2] =  0x0;
   xact_s.i2c_tx_buffer[3] =  i2c_create_read_address(EEPROM_ADDR);
   xact_s.write_length     =  0x3;
   xact_s.read_length      =  0x5;
   xact_s.xact_active      =  0x1;
   xact_s.xact_success     =  0x0;

   start_i2c0_master_xact(&xact_s, &xact_callback);

   poll_wait();

   if(xact_s.xact_success == 1) {
       uart0_putstring("eeprom i2c write bytes xaction success.\n");
   } else {
       uart0_putstring("eeprom i2c write bytes xaction fail.\n");
   }

   uint8_t check_buff[5] = {0xb,0xc,0xd,0x1e,0xf};

   for(i=0; i<5; ++i) {
       uart0_putstring(util_uitoa(i,10));
       uart0_putstring(": Read data is 0x");
       uart0_putstring(util_uitoa(xact_s.i2c_rd_buffer[i],16));
       uart0_putchar('\n');
       if(check_buff[i] != xact_s.i2c_rd_buffer[i]) {
           uart0_putstring("Wrong value in read bytes-> 0x");
           uart0_putstring(util_uitoa(xact_s.i2c_rd_buffer[i],16));
           uart0_putstring("\n...Should be-> 0x");
           uart0_putstring(util_uitoa(check_buff[i],16));
       }
       uart0_putchar('\n');
   }
}