Example #1
0
int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr)
{
    dev->i2c = i2c;
    dev->addr = (addr >> 1);    /* internally we right align the 7-bit addr */
    uint8_t rev;

    /* Acquire exclusive access to the bus. */
    i2c_acquire(dev->i2c);
    /* initialize i2c interface */
    if (i2c_init_master(dev->i2c, BUS_SPEED) < 0) {
        DEBUG("[srf02] error initializing I2C bus\n");
        return -1;
    }
    /* try to read the software revision (read the CMD reg) from the device */
    i2c_read_reg(i2c, dev->addr, REG_CMD, &rev);
    if (rev == 0 || rev == 255) {
        i2c_release(dev->i2c);
        DEBUG("[srf02] error reading the devices software revision\n");
        return -1;
    } else {
        DEBUG("[srf02] software revision: 0x%02x\n", rev);
    }
    /* Release the bus for other threads. */
    i2c_release(dev->i2c);

    DEBUG("[srf02] initialization successful\n");
    return 0;
}
Example #2
0
uint8_t u8x8_byte_riotos_hw_i2c(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
    static uint8_t buffer[255];
    static uint8_t index;

    i2c_t dev = (i2c_t) u8g2->dev;

    switch (msg) {
        case U8X8_MSG_BYTE_SEND:
            memcpy(&buffer[index], arg_ptr, arg_int);
            index += arg_int;
            break;
        case U8X8_MSG_BYTE_INIT:
            i2c_init_master(dev, I2C_SPEED_FAST);
            break;
        case U8X8_MSG_BYTE_SET_DC:
            break;
        case U8X8_MSG_BYTE_START_TRANSFER:
            i2c_acquire(dev);
            index = 0;
            break;
        case U8X8_MSG_BYTE_END_TRANSFER:
            i2c_write_bytes(dev, u8x8_GetI2CAddress(u8g2), buffer, index);
            i2c_release(dev);
            break;
        default:
            return 0;
    }

    return 1;
}
Example #3
0
int lps331ap_init(lps331ap_t *dev, i2c_t i2c, uint8_t address, lps331ap_rate_t rate)
{
    uint8_t tmp;

    /* save device specifics */
    dev->i2c = i2c;
    dev->address = address;

    /* Acquire exclusive access to the bus. */
    i2c_acquire(dev->i2c);
    /* initialize underlying I2C bus */
    if (i2c_init_master(dev->i2c, BUS_SPEED) < 0) {
        /* Release the bus for other threads. */
        i2c_release(dev->i2c);
        return -1;
    }

    /* configure device, for simple operation only CTRL_REG1 needs to be touched */
    tmp = LPS331AP_CTRL_REG1_DBDU | LPS331AP_CTRL_REG1_PD |
          (rate << LPS331AP_CTRL_REG1_ODR_POS);
    if (i2c_write_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, tmp) != 1) {
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);

    return 0;
}
Example #4
0
File: board.c Project: drmrboy/RIOT
static void board_pic_init(void)
{
    gpio_init(PIC_INT_WAKE_PIN, GPIO_OD);
    gpio_set(PIC_INT_WAKE_PIN);

    i2c_init_master(PIC_I2C, I2C_SPEED_NORMAL);
}
Example #5
0
int isl29125_init(isl29125_t *dev, i2c_t i2c, gpio_t gpio,
                  isl29125_mode_t mode, isl29125_range_t range,
                  isl29125_resolution_t resolution)
{
    DEBUG("isl29125_init\n");

    /* initialize device descriptor */
    dev->i2c = i2c;
    dev->res = resolution;
    dev->range = range;
    dev->gpio = gpio;

    /* configuration 1: operation mode, range, resolution */
    uint8_t conf1 = 0x00;
    conf1 |= mode;
    conf1 |= range;
    conf1 |= resolution;
    conf1 |= ISL29125_CON1_SYNCOFF; /* TODO: implement SYNC mode configuration */

    /* TODO: implement configuration 2: infrared compensation configuration */

    /* acquire exclusive access to the bus */
    DEBUG("isl29125_init: i2c_acquire\n");
    (void) i2c_acquire(dev->i2c);

    /* initialize the I2C bus */
    DEBUG("isl29125_init: i2c_init_master\n");
    (void) i2c_init_master(i2c, I2C_SPEED_NORMAL);

    /* verify the device ID */
    DEBUG("isl29125_init: i2c_read_reg\n");
    uint8_t reg_id;
    int ret = i2c_read_reg(dev->i2c, ISL29125_I2C_ADDRESS, ISL29125_REG_ID, &reg_id);
    if ((reg_id == ISL29125_ID) && (ret == 1)) {
        DEBUG("isl29125_init: ID successfully verified\n");
    }
    else {
        DEBUG("isl29125_init: ID could not be verified, ret: %i\n", ret);
        (void) i2c_release(dev->i2c);
        return -1;
    }

    /* configure and enable the sensor */
    DEBUG("isl29125_init: i2c_write_reg(ISL29125_REG_RESET)\n");
    (void) i2c_write_reg(dev->i2c, ISL29125_I2C_ADDRESS, ISL29125_REG_RESET, ISL29125_CMD_RESET);

    DEBUG("isl29125_init: i2c_write_reg(ISL29125_REG_CONF1)\n");
    (void) i2c_write_reg(dev->i2c, ISL29125_I2C_ADDRESS, ISL29125_REG_CONF1, conf1);

    /* release the I2C bus */
    DEBUG("isl29125_init: i2c_release\n");
    (void) i2c_release(dev->i2c);

    DEBUG("isl29125_init: success\n");
    return 0;
}
Example #6
0
int mpu9150_init(mpu9150_t *dev, i2c_t i2c, mpu9150_hw_addr_t hw_addr,
        mpu9150_comp_addr_t comp_addr)
{
    char temp;

    dev->i2c_dev = i2c;
    dev->hw_addr = hw_addr;
    dev->comp_addr = comp_addr;
    dev->conf = DEFAULT_STATUS;

    /* Initialize I2C interface */
    if (i2c_init_master(dev->i2c_dev, I2C_SPEED_FAST)) {
        DEBUG("[Error] I2C device not enabled\n");
        return -1;
    }

    /* Acquire exclusive access */
    i2c_acquire(dev->i2c_dev);

    /* Reset MPU9150 registers and afterwards wake up the chip */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_RESET);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_RESET_SLEEP_US));
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_WAKEUP);

    /* Release the bus, it is acquired again inside each function */
    i2c_release(dev->i2c_dev);

    /* Set default full scale ranges and sample rate */
    mpu9150_set_gyro_fsr(dev, MPU9150_GYRO_FSR_2000DPS);
    mpu9150_set_accel_fsr(dev, MPU9150_ACCEL_FSR_2G);
    mpu9150_set_sample_rate(dev, MPU9150_DEFAULT_SAMPLE_RATE);

    /* Disable interrupt generation */
    i2c_acquire(dev->i2c_dev);
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_INT_ENABLE_REG, REG_RESET);

    /* Initialize magnetometer */
    if (compass_init(dev)) {
        i2c_release(dev->i2c_dev);
        return -2;
    }
    /* Release the bus, it is acquired again inside each function */
    i2c_release(dev->i2c_dev);
    mpu9150_set_compass_sample_rate(dev, 10);
    /* Enable all sensors */
    i2c_acquire(dev->i2c_dev);
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_PLL);
    i2c_read_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, &temp);
    temp &= ~(MPU9150_PWR_ACCEL | MPU9150_PWR_GYRO);
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, temp);
    i2c_release(dev->i2c_dev);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_PWR_CHANGE_SLEEP_US));

    return 0;
}
Example #7
0
int l3g4200d_init(l3g4200d_t *dev, i2c_t i2c, uint8_t address,
                  gpio_t int1_pin, gpio_t int2_pin,
                  l3g4200d_mode_t mode, l3g4200d_scale_t scale)
{
    char tmp;

    /* Acquire exclusive access to the bus. */
    i2c_acquire(dev->i2c);
    /* initialize the I2C bus */
    if (i2c_init_master(i2c, I2C_SPEED) < 0) {
        /* Release the bus for other threads. */
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);

    /* write device descriptor */
    dev->i2c = i2c;
    dev->addr = address;
    dev->int1 = int1_pin;
    dev->int2 = int2_pin;

    /* set scale */
    switch (scale) {
        case L3G4200D_SCALE_250DPS:
            dev->scale = 250;
            break;
        case L3G4200D_SCALE_500DPS:
            dev->scale = 500;
            break;
        case L3G4200D_SCALE_2000DPS:
            dev->scale = 2000;
            break;
        default:
            dev->scale = 500;
            break;
    }

    /* configure CTRL_REG1 */
    tmp = ((mode & 0xf) << L3G4200D_CTRL1_MODE_POS) | L3G4200D_CTRL1_ALLON;
    i2c_acquire(dev->i2c);
    if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, tmp) != 1) {
        i2c_release(dev->i2c);
        return -1;
    }
    tmp = ((scale & 0x3) << L3G4200D_CTRL4_FS_POS) | L3G4200D_CTRL4_BDU;
    if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL4, tmp) != 1) {
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);
    return 0;
}
Example #8
0
int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t range, uint8_t type)
{
    uint8_t reg;

    /* write device descriptor */
    dev->i2c = i2c;
    dev->addr = address;
    dev->initialized = false;

    if (dr > MMA8652_DATARATE_1HZ56 || range > MMA8652_FS_RANGE_8G || type >= MMA8x5x_TYPE_MAX) {
        return -1;
    }

    dev->type = type;

    i2c_acquire(dev->i2c);
    /* initialize the I2C bus */
    if (i2c_init_master(i2c, I2C_SPEED) < 0) {
        i2c_release(dev->i2c);
        return -2;
    }
    i2c_release(dev->i2c);

    if (mma8652_test(dev)) {
        return -3;
    }

    if (mma8652_set_standby(dev) < 0) {
        return -4;
    }

    reg = MMA8652_XYZ_DATA_CFG_FS(range);

    i2c_acquire(dev->i2c);
    if (i2c_write_regs(dev->i2c, dev->addr, MMA8652_XYZ_DATA_CFG, &reg, 1) != 1) {
        i2c_release(dev->i2c);
        return -5;
    }

    reg = MMA8652_CTRL_REG1_DR(dr);

    if (i2c_write_regs(dev->i2c, dev->addr, MMA8652_CTRL_REG1, &reg, 1) != 1) {
        i2c_release(dev->i2c);
        return -5;
    }
    i2c_release(dev->i2c);

    dev->initialized = true;
    dev->scale = 1024 >> range;

    return 0;
}
Example #9
0
int tsl2561_init(tsl2561_t *dev,
                 i2c_t i2c, uint8_t addr, uint8_t gain, uint8_t integration)
{
    dev->i2c_dev = i2c;
    dev->addr = addr;
    dev->gain = gain;
    dev->integration = integration;
    _print_init_info(dev);

    /* Initialize I2C interface */
    if (i2c_init_master(dev->i2c_dev, I2C_SPEED_NORMAL)) {
        DEBUG("[Error] I2C device not enabled\n");
        return TSL2561_NOI2C;
    }

    DEBUG("[Info] I2C device initialized with success!\n");

    /* Acquire exclusive access */
    i2c_acquire(dev->i2c_dev);

    DEBUG("[Info] Access acquired !\n");

    /* Verify sensor ID */
    uint8_t id;
    i2c_read_reg(dev->i2c_dev, dev->addr,
                 TSL2561_COMMAND_MODE | TSL2561_REGISTER_ID, &id);
    DEBUG("[Info] ID ? %d\n", id);
    if (id != TSL2561_ID ) {
        DEBUG("[Error] not a TSL2561 sensor\n");
        return TSL2561_BADDEV;
    }

    _enable(dev);

    /* configuring gain and integration time */
    i2c_write_reg(dev->i2c_dev, dev->addr,
                  TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING,
                  dev->integration | dev->gain);

#if ENABLE_DEBUG
    uint8_t timing;
    i2c_read_reg(dev->i2c_dev, dev->addr,
                 TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING, &timing);
    DEBUG("[Info] Timing ? %d (expected: %d)\n",
          timing, dev->integration | dev->gain);
#endif

    _disable(dev);

    return TSL2561_OK;
}
Example #10
0
int main(void)
{
    hih6130_t dev;

    puts("HIH6130 sensor driver test application\n");
    printf("Initializing I2C_%i... ", TEST_HIH6130_I2C);
    if (i2c_init_master(TEST_HIH6130_I2C, I2C_SPEED_FAST) < 0) {
        puts("[Failed]");
        return -1;
    }
    puts("[OK]");

    printf("Initializing HIH6130 sensor at I2C_%i, address 0x%02x... ",
        TEST_HIH6130_I2C, TEST_HIH6130_ADDR);
    hih6130_init(&dev, TEST_HIH6130_I2C, TEST_HIH6130_ADDR);
    puts("[OK]");

    while (1) {
        float hum = 0.f;
        float temp = 0.f;
        int status;
        float integral = 0.f;
        float fractional;

        vtimer_usleep(SLEEP);

        status = hih6130_get_humidity_temperature_float(&dev, &hum, &temp);
        if (status < 0) {
            printf("Communication error: %d\n", status);
            continue;
        } else if (status == 1) {
            puts("Stale values");
        }
        /* Several platforms usually build with nano.specs, (without float printf) */
        /* Split value into two integer parts for printing. */
        fractional = modff(hum, &integral);
        printf("humidity: %4d.%04u %%",
            (int)integral, (unsigned int)abs(fractional * 10000.f));
        fractional = modff(temp, &integral);
        printf("  temperature: %4d.%04u C\n",
            (int)integral, (unsigned int)abs(fractional * 10000.f));
    }

    return 0;
}
Example #11
0
int lis3mdl_init(lis3mdl_t *dev,
                 i2c_t i2c,
                 uint8_t address,
                 lis3mdl_xy_mode_t xy_mode,
                 lis3mdl_z_mode_t z_mode,
                 lis3mdl_odr_t odr,
                 lis3mdl_scale_t scale,
                 lis3mdl_op_t op_mode) {
    uint8_t tmp;

    dev->i2c = i2c;
    dev->addr = address;

    i2c_acquire(dev->i2c);

    if (i2c_init_master(i2c, I2C_SPEED_NORMAL) < 0) {
        DEBUG("LIS3MDL: Master initialization failed\n");
        return -1;
    }

    i2c_read_reg(dev->i2c, dev->addr, LIS3DML_WHO_AM_I_REG, &tmp);
    if (tmp != LIS3MDL_CHIP_ID) {
        DEBUG("LIS3MDL: Identification failed\n");
        return -1;
    }

    tmp = ( LIS3MDL_MASK_REG1_TEMP_EN   /* enable temperature sensor */
          | xy_mode                     /* set x-, y-axis operative mode */
          | odr);                       /* set output data rate */
    i2c_write_reg(dev->i2c, dev->addr, LIS3MDL_CTRL_REG1, tmp);

    /* set Full-scale configuration */
    i2c_write_reg(dev->i2c, dev->addr, LIS3MDL_CTRL_REG2, scale);

    /* set continuous-conversion mode */
    i2c_write_reg(dev->i2c, dev->addr, LIS3MDL_CTRL_REG3, op_mode);

    /* set z-axis operative mode */
    i2c_write_reg(dev->i2c, dev->addr, LIS3MDL_CTRL_REG4, z_mode);

    i2c_release(dev->i2c);

    return 0;
}
Example #12
0
int tcs37727_init(tcs37727_t *dev, i2c_t i2c, uint8_t address, int atime_us)
{
    /* write device descriptor */
    dev->i2c = i2c;
    dev->addr = address;
    dev->initialized = false;

    i2c_acquire(dev->i2c);

    /* initialize the I2C bus */
    if (i2c_init_master(i2c, I2C_SPEED) < 0) {
        i2c_release(dev->i2c);
        return -1;
    }

    i2c_release(dev->i2c);

    if (tcs37727_test(dev)) {
        return -2;
    }

    i2c_acquire(dev->i2c);

    if (i2c_write_reg(dev->i2c, dev->addr, TCS37727_CONTROL,
                      TCS37727_CONTROL_AGAIN_4) != 1) {
        i2c_release(dev->i2c);
        return -3;
    }
    dev->again = 4;

    if (i2c_write_reg(dev->i2c, dev->addr, TCS37727_ATIME,
                      TCS37727_ATIME_TO_REG(atime_us)) != 1) {
        i2c_release(dev->i2c);
        return -3;
    }
    dev->atime_us = atime_us;

    dev->initialized = true;

    i2c_release(dev->i2c);
    return 0;
}
Example #13
0
File: main.c Project: bumpy-b/RIOT
int cmd_init_master(int argc, char **argv)
{
    int dev, speed, res;

    if (argc != 3) {
        puts("Error: Init: Invalid number of arguments!");
        printf("Usage:\n%s: [DEVICE] [SPEED]\n", argv[0]);
        puts("    with DEVICE:");
        for (int i = 0; i < I2C_NUMOF; i++) {
            printf("          %i -> I2C_%i\n", i, i);
        }
        puts("         SPEED:");
        puts("          0 -> SPEED_LOW (10kbit/s)");
        puts("          1 -> SPEED_NORMAL (100kbit/s)");
        puts("          2 -> SPEED_FAST (400kbit/s)");
        puts("          3 -> SPEED_FAST_PLUS (1Mbit/s)");
        puts("          4 -> SPEED_HIGH (3.4Mbit/s)\n");
        return 1;
    }

    dev = atoi(argv[1]);
    speed = atoi(argv[2]);

    res = i2c_init_master(dev, speed);
    if (res == -1) {
        puts("Error: Init: Given device not available");
        return 1;
    }
    else if (res == -2) {
        puts("Error: Init: Unsupported speed value");
        return 1;
    }
    else {
        printf("I2C_%i successfully initialized as master!\n", dev);
        i2c_dev = dev;
    }

    return 0;
}
Example #14
0
int tcs37727_init(tcs37727_t *dev, const tcs37727_params_t *params)
{
    uint8_t tmp;

    /* check parameters */
    assert(dev && params);

    /* initialize the device descriptor */
    memcpy(&dev->p, params, sizeof(tcs37727_params_t));

    /* setup the I2C bus */
    i2c_acquire(BUS);
    if (i2c_init_master(BUS, I2C_SPEED) < 0) {
        i2c_release(BUS);
        LOG_ERROR("[tcs37727] init: error initializing I2C bus\n");
        return TCS37727_NOBUS;
    }

    /* check if we can communicate with the device */
    i2c_read_reg(BUS, ADR, TCS37727_ID, &tmp);
    if (tmp != TCS37727_ID_VALUE) {
        i2c_release(BUS);
        LOG_ERROR("[tcs37727] init: error while reading ID register\n");
        return TCS37727_NODEV;
    }

    /* configure gain and conversion time */
    i2c_write_reg(BUS, ADR, TCS37727_ATIME, TCS37727_ATIME_TO_REG(dev->p.atime));
    i2c_write_reg(BUS, ADR, TCS37727_CONTROL, TCS37727_CONTROL_AGAIN_4);
    dev->again = 4;

    /* enable the device */
    tmp = (TCS37727_ENABLE_AEN | TCS37727_ENABLE_PON);
    i2c_write_reg(BUS, ADR, TCS37727_ENABLE, tmp);

    i2c_release(BUS);

    return TCS37727_OK;
}
Example #15
0
int lis3mdl_init(lis3mdl_t *dev, const lis3mdl_params_t *params)
{
    dev->params = *params;

    uint8_t tmp;

    i2c_acquire(DEV_I2C);

    if (i2c_init_master(DEV_I2C, I2C_SPEED_NORMAL) < 0) {
        DEBUG("LIS3MDL: Master initialization failed\n");
        return -1;
    }

    i2c_read_reg(DEV_I2C, DEV_ADDR, LIS3DML_WHO_AM_I_REG, &tmp);
    if (tmp != LIS3MDL_CHIP_ID) {
        DEBUG("LIS3MDL: Identification failed, %02X != %02X\n",
              tmp, LIS3MDL_CHIP_ID);
        return -1;
    }

    tmp = ( LIS3MDL_MASK_REG1_TEMP_EN   /* enable temperature sensor */
          | dev->params.xy_mode         /* set x-, y-axis operative mode */
          | dev->params.odr);           /* set output data rate */
    i2c_write_reg(DEV_I2C, DEV_ADDR, LIS3MDL_CTRL_REG1, tmp);

    /* set Full-scale configuration */
    i2c_write_reg(DEV_I2C, DEV_ADDR, LIS3MDL_CTRL_REG2, dev->params.scale);

    /* set continuous-conversion mode */
    i2c_write_reg(DEV_I2C, DEV_ADDR, LIS3MDL_CTRL_REG3, dev->params.op_mode);

    /* set z-axis operative mode */
    i2c_write_reg(DEV_I2C, DEV_ADDR, LIS3MDL_CTRL_REG4, dev->params.z_mode);

    i2c_release(DEV_I2C);

    return 0;
}
Example #16
0
int si70xx_init(si70xx_t *dev, i2c_t i2c_dev, uint8_t address)
{
    dev->i2c_dev = i2c_dev;
    dev->address = address;

    /* setup the i2c bus */
    i2c_acquire(dev->i2c_dev);
    int result = i2c_init_master(dev->i2c_dev, I2C_SPEED_NORMAL);

    if (result != 0) {
        i2c_release(dev->i2c_dev);
        return result;
    }

    /* initialize the peripheral */
    i2c_write_byte(dev->i2c_dev, dev->address, SI70XX_RESET);

    /* sensor is ready after at most 25 ms */
    xtimer_usleep(25 * MS_IN_USEC);
    i2c_release(dev->i2c_dev);

    return 0;
}
Example #17
0
int hdc1000_init(hdc1000_t *dev, i2c_t i2c, uint8_t address)
{
    char reg[2];

    /* write device descriptor */
    dev->i2c = i2c;
    dev->addr = address;
    dev->initialized = false;

    i2c_acquire(dev->i2c);
    /* initialize the I2C bus */
    if (i2c_init_master(i2c, I2C_SPEED) < 0) {
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);

    if (hdc1000_test(dev)) {
        return -2;
    }

    /* set 14 bit resolution for both sensors and sequence mode */
    uint16_t tmp = HDC1000_CONFG_SEQ_MOD;
    reg[0] = (uint8_t)(tmp >> 8);
    reg[1] = (uint8_t)tmp;

    i2c_acquire(dev->i2c);
    if (i2c_write_regs(dev->i2c, dev->addr, HDC1000_CONFG, reg, 2) != 2) {
        i2c_release(dev->i2c);
        return -3;
    }
    dev->initialized = true;

    i2c_release(dev->i2c);
    return 0;
}
Example #18
0
int main(void)
{   
	int test_mode;
	int vm_present;
	int i;
	unsigned int seed;

	// Needs to be called ASAP as rf need a looooooong time to wake up.
	// This function is just sending a pulse over the SCL line.
	rf_wakeup();
	
	clock_set_speed(16000000UL,16);	
	
	setup_pps();
	setup_io();
	
	leds_init();

	CHARGE_500MA = 0; // Switch back to 100mA charge.
	
	// Switch on one led to say we are powered on
	leds_set(LED_BATTERY_0, 32);

	// Enable the poweroff softirq.
	_INT3IF = 0;
	_INT3IP = 1;
	_INT3IE = 1;

	// Sound must be enabled before analog, as 
	// The analog interrupt callback into sound processing ... 
	// But must be initialised _after_ leds as it use one led IO for enabling amp.
	sound_init();
	tone_init(); // Init tone generator
	
	pwm_motor_init();
	pid_motor_init();

	// We need the settings for the horizontal prox.
	load_settings_from_flash();


	for (i = 0; i < 2; i++) {
		// Settings is definitely wrong....
		if(settings.mot256[i] <= 0)
			settings.mot256[i] = 256;

		// 1024 (AD resolution is 10 bits) * 256 / 9 fits in signed 16 bits.
		if (settings.mot256[i] < 9)
			settings.mot256[i] = 9;
	}
	
	// This is the horizontal prox. Vertical one are handled by the ADC
	// but ADC sync the motor mesurment with the prox, so we don't pullute it with noise ...
	
	timer_init(TIMER_IR_COMM, 0,-1); // The period will be changed later.
	prox_init(PRIO_SENSORS);  // Same priority as analog (maybe should be at 7 ...)
	
	// Warning: We cannot use the SD before the analog init as some pin are on the analog port.
	analog_init(TIMER_ANALOG, PRIO_SENSORS);

        wait_valid_vbat();
        
	log_init(); // We will need to read vbat to be sure we can flash.

	ntc_init(ntc_callback, PRIO_1KHZ);

//	i2c_init(I2C_3);

	i2c_init_master(I2C_3, 400000, PRIO_I2C);
	I2C3CON = 0x9000;

	
	mma7660_init(I2C_3, MMA7660_DEFAULT_ADDRESS, acc_cb, 0);
	mma7660_set_mode(MMA7660_120HZ, 1);
	
	rc5_init(TIMER_RC5, rc5_callback, PRIO_RC5);
	
	sd_init();

	timer_init(TIMER_1KHZ, 1000, 6);
	timer_enable_interrupt(TIMER_1KHZ, timer_1khz, PRIO_1KHZ);
	
	rf_init(I2C_3);
	
	timer_enable(TIMER_1KHZ);
	
	sd_log_file();
	
	vm_present = init_aseba_and_fifo();
	
	if(vm_present) 
		log_analyse_bytecode();

	vmVariables.fwversion[0] = FW_VERSION;
	vmVariables.fwversion[1] = FW_VARIANT;
	
	// SD file is more important than internal flash
	if(!sd_load_aseba_code()) {
		log_set_flag(LOG_FLAG_VMCODESD);
		vm_present = 1;
		log_analyse_bytecode();
	}

	// Behavior is on INT4 (softirq trigged by 1khz timer).
	behavior_init(PRIO_BEHAVIOR);
	
	
	test_mode = sd_test_file_present();
	
	if(!test_mode)
		mode_init(vm_present);

	
	
	// Enable the LVD interrupt
	_LVDIE = 1;
	
	play_sound(SOUND_POWERON);
	
	if(test_mode) {	
		test_mode_start();
		while(1) 
			idle_without_aseba();
	}
	
	while(behavior_enabled(B_MODE)) 
		idle_without_aseba();
	
	// If usb did not put us out of behavior mode, then start the rf link
	if(!usb_uart_serial_port_open() && (rf_get_status() & RF_PRESENT)) {
		rf_set_link(RF_UP);
	}

	// get the random seed
	seed = 0;
	for(i = 0; i < 5; i++) {
		seed += vmVariables.buttons_mean[i];
		seed += vmVariables.buttons_noise[i];
	}
	seed += vmVariables.vbat[0];
	seed += vmVariables.vbat[1];
	
	for(i = 0; i < 3; i++) 
		seed += vmVariables.acc[i];
	
	AsebaSetRandomSeed(seed);
	
	for(i = 0; i < 3; i++)
		AsebaGetRandom();
	
	// Give full control to aseba. No way out (except reset).
	run_aseba_main_loop();
}