Beispiel #1
0
void mma7660_init(void)
{
  int32_t val;

  i2c_enable();

  buf[0] = INTSU;
  buf[1] = 0x00;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = MODE;
  buf[1] = 0x01;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = SR;
  buf[1] = 0x1F;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = PDET;
  buf[1] = 0xFF;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  buf[0] = PD;
  buf[1] = 0xFF;
  i2c_transmitinit(ADDR, 2, buf);
  while(!i2c_transferred()) ;

  i2c_disable();
}
Beispiel #2
0
int sht21_humidity(void)
{
  int val;

  i2c_enable();
  /* For for about 15ms before the SHT11 can be used */
  sht21_timer = RTIMER_NOW();
  while(RTIMER_CLOCK_LT(RTIMER_NOW(), sht21_timer + (RTIMER_SECOND/1000)*15));

  buf[0] = 0xe5;
  i2c_transmitinit(0x40, 1, buf);
  while(!i2c_transferred()) ;

  /* Wait for measurement about 85ms */
  sht21_timer = RTIMER_NOW();
  while(RTIMER_CLOCK_LT(RTIMER_NOW(), sht21_timer + (RTIMER_SECOND/1000)*85));

  i2c_receiveinit(0x40, 3, buf);
  while(!i2c_transferred()) ;

  val = (int)(buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3]);

//  i2c_disable();
  /* return relative humidity * 100 (0.04 % accuracy) */
  return (-6.0 + (125.0*((val>>16)&0x0000fffc))/0x10000)*100;
}
Beispiel #3
0
void resetFIFO() {
  uint8_t user_config= 0b01000100;
  uint8_t cmd[] = {MPU9150_USER_CTRL, user_config} ;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("User control configured!\n");
}
Beispiel #4
0
//turn cursor on/off (1/0)
void lcd_cursor(char state)
{
  if (state == 0) cursor &= ~CURSOR;
  else cursor |= CURSOR;
  char cmd[] = {0x80, cursor};
  i2c_transmitinit(LCD_ADDR,2,cmd);
  wait_for_transfer();
}
Beispiel #5
0
//turn blink on/off (1/0)
void lcd_blink(char state)
{
  if (state == 0) cursor &= ~BLINK;
  else cursor |= BLINK;
  char cmd[] = {0x80, cursor};
  i2c_transmitinit(LCD_ADDR,2,cmd);
  wait_for_transfer();
}
Beispiel #6
0
//print string str to display
void lcd_print(char* str)
{
  char len = strlen(str);
  char cmd[len+1];
  cmd[0] = 0x40;
  memcpy(&cmd[1], str, len);
  i2c_transmitinit(LCD_ADDR,len+1,cmd);
  wait_for_transfer();
}
Beispiel #7
0
//move cursor to the left (dir = CURSOR_LEFT) or right (dir = CURSOR_RIGHT)
void lcd_move_cursor(char dir)
{
  char cur;
  if (dir == CURSOR_LEFT) cur = 0x10;
  else if (dir == CURSOR_RIGHT) cur = 0x14;

  char cmd[] = {0x80, cur};
  i2c_transmitinit(LCD_ADDR,2,cmd);
  wait_for_transfer();
}
Beispiel #8
0
void
accm_write_stream(u8_t len, u8_t *data) {
  i2c_transmitinit(ADXL345_ADDR);
  while (i2c_busy());
  PRINTFDEBUG("I2C Ready to TX(stream)\n");

  i2c_transmit_n(len, data);	// start tx and send conf reg 
  while (i2c_busy());
  PRINTFDEBUG("WRITE_STR %u B to 0x%02X\n", len, data[0]);
}
Beispiel #9
0
//move cursor to row r(0/1), column c(0-15)
void lcd_set_cursor(int r, int c)
{
  char addr = 0;
  lcd_home();
  if(r != 0) addr |= LINE2;
  if(c<=0x0F) addr += c;
  char cmd[] = {0x80, DDRAM_SET | addr};
  i2c_transmitinit(LCD_ADDR,2,cmd);
  wait_for_transfer();
}
Beispiel #10
0
void
tlc59116_write_stream(uint8_t len, uint8_t *data)
{
  i2c_transmitinit(TLC59116_ADDR);
  while(i2c_busy());
  PRINTFDEBUG("I2C Ready to TX(stream)\n");

  i2c_transmit_n(len, data);    /* start tx and send conf reg */
  while(i2c_busy());
  PRINTFDEBUG("WRITE_STR %u B to 0x%02X\n", len, data[0]);
}
Beispiel #11
0
/*############################## INIT FUNCTION ###################################  */
void mpu9150_init(void) {
  printf("Initialising power management...\n");
  // Chip starts in sleep mode and we need to turn it on: (REG:107)
  uint8_t cmd[] = {MPU9150_PWR_MGMT_1, 0x00} ;


  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd ) ;
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("Power management initialised!\n");
  file = fopen("testfile","w");
}
Beispiel #12
0
void
accm_write_reg(u8_t reg, u8_t val) {
  u8_t tx_buf[] = {reg, val};

  i2c_transmitinit(ADXL345_ADDR);
  while (i2c_busy());
  PRINTFDEBUG("I2C Ready to TX\n");

  i2c_transmit_n(2, tx_buf);
  while (i2c_busy());
  PRINTFDEBUG("WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg);
}
Beispiel #13
0
void
tlc59116_write_reg(uint8_t reg, uint8_t val)
{
  uint8_t tx_buf[] = { reg, val };

  i2c_transmitinit(TLC59116_ADDR);
  while(i2c_busy());
  PRINTFDEBUG("I2C Ready to TX\n");

  i2c_transmit_n(2, tx_buf);
  while(i2c_busy());
  PRINTFDEBUG("WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg);
}
Beispiel #14
0
void
accm_read_stream(u8_t reg, u8_t len, u8_t *whereto) {
  u8_t rtx = reg;
  PRINTFDEBUG("READ_STR %u B from 0x%02X\n", len, reg);

  /* transmit the register to start reading from */
  i2c_transmitinit(ADXL345_ADDR);
  while (i2c_busy());
  i2c_transmit_n(1, &rtx);
  while (i2c_busy());

  /* receive the data */
  i2c_receiveinit(ADXL345_ADDR);
  while (i2c_busy());
  i2c_receive_n(len, whereto);
  while (i2c_busy());
}
Beispiel #15
0
u8_t
accm_read_reg(u8_t reg) {
  u8_t retVal = 0;
  u8_t rtx = reg;
  PRINTFDEBUG("READ_REG 0x%02X\n", reg);

  /* transmit the register to read */
  i2c_transmitinit(ADXL345_ADDR);
  while (i2c_busy());
  i2c_transmit_n(1, &rtx);
  while (i2c_busy());

  /* receive the data */
  i2c_receiveinit(ADXL345_ADDR);
  while (i2c_busy());
  i2c_receive_n(1, &retVal);
  while (i2c_busy());

  return retVal;
}
Beispiel #16
0
//initialize the display
void lcd_init()
{
  printf("initialize lcd...");

  i2c_enable();
  const char init_commands[] = {0x38, 0x39, 0x14, 0x74, 0x54, 0x6f, 0x0c, 0x01};
  i2c_transmitinit(LCD_ADDR,8,init_commands);
  wait_for_transfer();
  cursor = 0x0f;
  line = 0x00;
  
  // turn backlight on
  lcd_backlight(1);

  lcd_print("hello boss :)");
  lcd_set_cursor(1,0);
  lcd_print("contiki booted!");

  printf("[OK]\n\r");
}
Beispiel #17
0
/*---------------------------------------------------------------------------*/
static uint16_t
sht25_read_reg(uint8_t reg)
{
  uint8_t buf[] = { 0x00, 0x00 };
  uint16_t retval;
  uint8_t rtx = reg;

  /* transmit the register to read */
  i2c_transmitinit(SHT25_ADDR);
  while(i2c_busy());
  i2c_transmit_n(1, &rtx);
  while(i2c_busy());
  /* receive the data */
  i2c_receiveinit(SHT25_ADDR);
  while(i2c_busy());
  i2c_receive_n(2, &buf[0]);
  while(i2c_busy());

  retval = (uint16_t)(buf[0] << 8 | (buf[1]));
  return retval;
}
Beispiel #18
0
void mma7660_acc(int *x, int *y, int *z)
{
  int tmp;
  i2c_enable();

  /* Read data */
  buf[0] = XOUT;
  i2c_transmitinit(ADDR, 1, buf);
  while(!i2c_transferred()) ;
  i2c_receiveinit(ADDR, 3, buf);
  while(!i2c_transferred()) ;

  i2c_disable();

  tmp = (signed char)((buf[0] & 0x20) ? (buf[0] | 0xC0) : (buf[0] & 0x3F));
  *x = (tmp*150)/32;
  tmp = (signed char)((buf[1] & 0x20) ? (buf[1] | 0xC0) : (buf[1] & 0x3F));
  *y = (tmp*150)/32;
  tmp = (signed char)((buf[2] & 0x20) ? (buf[2] | 0xC0) : (buf[2] & 0x3F));
  *z = (tmp*150)/32;
}
Beispiel #19
0
uint8_t
tlc59116_read_reg(uint8_t reg)
{
  uint8_t retVal = 0;
  uint8_t rtx = reg;

  PRINTFDEBUG("READ_REG 0x%02X\n", reg);

  /* transmit the register to read */
  i2c_transmitinit(TLC59116_ADDR);
  while(i2c_busy());
  i2c_transmit_n(1, &rtx);
  while(i2c_busy());

  /* receive the data */
  i2c_receiveinit(TLC59116_ADDR);
  while(i2c_busy());
  i2c_receive_n(1, &retVal);
  while(i2c_busy());

  return retVal;
}
Beispiel #20
0
//delete current character at cursor 
void lcd_delete()
{
  const char cmd[] = {0x40, 0x20};
  i2c_transmitinit(LCD_ADDR,2,cmd);
  wait_for_transfer();
}
Beispiel #21
0
/*############################## START FUNCTION ##################################  */
void mpu9150_start(void) {
  printf("Setting sample rate...\n");
  /*=== Set sample rate divider: (REG:25) ===*/
  uint8_t cmd[] = {MPU9150_SMPLRT_DIV, SAMPLE_RATE_CONFIG};
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("Sample rate set to: %i!\n", SAMPLE_RATE_CONFIG);
  
  /*=== Set up FSYNC and dlpf: (REG:26) ===*/
  printf("Setting fsync dlpf config...\n");
  uint8_t fsync_dlpf_config = 0b00000011; //FSYNC disabled and dlpf_cfg set to 3.
  cmd[0] = MPU9150_CONFIG;
  cmd[1] = fsync_dlpf_config;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("FSYNC rate set!\n");
  /*
   * With current dlpf_cfg (3) the sample rate of the accelerometer is 1kHz and the 
   * gyroscope is 1kHz too. The sample rate divider has been set to 20 (register 20).
   * Sample rate = Gyroscope output rate / (1 + sample rate divider)
   *             =                  1000 / (1 + 19)
   *             =                       50Hz
   */
  /* Set up the fifo_config parameter and enable sensors if they are defined in the header file: */
  uint8_t fifo_config = 0b00000000;
  /*=== Trigger the gyroscopes self-test and set the scale range: (REG:27) ===*/
  if (INV_XYZ_GYRO) {
    fifo_config = fifo_config | 0b01110000;
    printf("Starting up gyroscope...\n");
    //Self test xyz and set full scale range to +-2000s:
    uint8_t gyro_config = 0b11111000;
    cmd[0] = MPU9150_GYRO_CONFIG;
    cmd[1] = gyro_config;
    i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd ) ;
    while(!i2c_transferred()) /* Wait for transfer */ ;
    printf("Gyroscope started!\n");
  }
 
  /*=== Trigger the accelorometer self-test and set the scale range: (REG:28) ===*/
  if (INV_XYZ_ACCEL) {
     fifo_config = fifo_config | 0b00001000;
     printf("Starting up accelorometer...\n");
     uint8_t accel_config = 0b11111000; //self test xyz, with full scale range +-16g and reset hpf
     cmd[0] = MPU9150_ACCEL_CONFIG;
     cmd[1] = accel_config;
     i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd ) ;
     while(!i2c_transferred()) /* Wait for transfer */ ;
     printf("Accelorometer started!\n");
  }
  
  /*=== Enable Temp on FIFO: ===*/
  if (INV_TEMP) {
    fifo_config = fifo_config | 0b10000000;
    printf("Adding thermometer to FIFO...\n");
  }
  
  if (INV_XYZ_ACCEL) {
        packet_size += 6;}
    if (INV_TEMP) {
        packet_size += 2;}
    if (INV_XYZ_GYRO) {
        packet_size += 6;}
    
    printf("packet size: %i\n", packet_size);
    //exit(0);
  
  /*
   * SKIPPING FREE FALL, MOTION DETECTION, ETC. CONFIGURATION
   */
  
   /*=== Setup FIFO queue: (REG:35) ===
    Bit order [1: Temp, 2-4: Gyro{x,y.z}, 5: Accel, 6-8: SLV{2,1,0}] */
  int BUF_SIZE = 9;
  char buffer[BUF_SIZE];
  buffer[BUF_SIZE - 1] = '\0';
  printf("Configuring FIFO with parameter %s...\n", int2bin(fifo_config, buffer, BUF_SIZE-1));
  cmd[0] = MPU9150_FIFO_EN;
  cmd[1] = fifo_config;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("FIFO configured!\n");
  
  /* Setup i2c master control (REG:36)
     First bit controls multi-master capability (not needed for us I think)
     Second bit controls wait for external sensors (to keep data in sync), which is not needed by us, because we do ot use external sensors
     Third bit controls FIFO_EN for salve 3 (other slaves controled in REG35)
     Fourth bit controls slave read and write mode (don't really know waht the implications of the different write modes are, think it's not significant for us?)
     Last four bits control the i2c master clock. We set the 8mhz clock divider to 19, so set this to 12 1100??? */
  printf("Setting up I2C master controller...\n");
  uint8_t i2cmc_config= 0b00001101;
  cmd[0] = MPU9150_I2C_MST_CTRL;
  cmd[1] = i2cmc_config;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("I2C master configured!\n");
  
  /* Setup user control */
  printf("Setting up user control...\n");
  uint8_t user_config= 0b01000100;
  cmd[0] = MPU9150_USER_CTRL;
  cmd[1] = user_config;
  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd );
  while(!i2c_transferred()) /* Wait for transfer */ ;
  printf("User control configured!\n");
  
  /*
   * SLAVE CONTROL REGISTERS (37-53) WE DON'T NEED TO CONFIGURE (I THINK). 54 IS THE I2C MASTER CONTROLLER,
   * ALSO NOT USED BY US, 55 IS USED TO CONFIGURE INTERRUPTS (MAYBE WE SHOULD CONFIGURE), 56 ENABLES
   * INTERRUPTS, MAYBE WE SHOULD SET THE FIFO INTERRUPT TO ONE, THEN WAIT FOR THE INTERRUPT TO OCCOUR
   * AND THEN READ ALL SENSORS IN ONE GO AND WAIT FOR NEXT INTERRUPT. REG 58 CONTAINS THE INTERRUPT STATA.
   * 59-96 CONTAINS THE LATEST VALUES FROM THE SENOSRS, BUT WE READ THEM FROM THE FIFO INSTEAD. REG 97 IS
   * MOTION DETETION STATUS, 98-102 HAS THE DATAOUT FOR THE IC2 SLAVES, 103 CONTROLS IC2 MASTER DEALY, 104
   * CAN BE USED TO RESET THE PATHS FOR THE INTERNAL SENSORS, 105 IS MOTION DETECTION CONTROL, 106 USER CONTROL,
   * BUT WE ENABLE THE FIFO IN A DIFFERENT REGISTER, SO SON'T NEED TO REENABLE IT HERE. 107 AND 108 ARE
   * POWER MANAGEMENT.
   * NOW TO GET DATA, WE SHOULD LOOK AT REGISTERS 114 AND 115 TO GET THE COUNT OF NEW MEASUREMENTS AND THEN
   * READ THEM FROM THE FIFO QUEUE:
   */
}
Beispiel #22
0
/*############################## STOP FUNCTION ###################################  */
void mpu9150_stop(void) {
  uint8_t cmd[] = {MPU9150_PWR_MGMT_1, 0x40} ;

  i2c_transmitinit( MPU9150_I2C_ADDR, 2, cmd ) ;
  while(!i2c_transferred()) /* Wait for transfer */ ;
}
Beispiel #23
0
//put cursor home (pos 1,1)
void lcd_home()
{
  const char cmd[] = {0x80, 0x02};
  i2c_transmitinit(LCD_ADDR,2,cmd);
  wait_for_transfer();
}
Beispiel #24
0
/*################ FUNCTION TO CALL BEFORE READING DATA FROM A REGISTER ##############  */
void prepareRead(uint8_t slv_adr) {
  i2c_transmitinit( MPU9150_I2C_ADDR, 1, &slv_adr ) ;
  while(!i2c_transferred()) /* Wait for transfer */ ;
}