示例#1
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
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;
}
示例#2
0
void radiotimer_start(PORT_RADIOTIMER_WIDTH period) {
    DEBUG("%s\n", __PRETTY_FUNCTION__);
    timer_set(OWSN_TIMER, 1, ((unsigned int)HWTIMER_TICKS(period)));
    current_period = period;
    radiotimer_vars.currentSlotPeriod = period;
    radiotimer_vars.overflowORcompare = RADIOTIMER_OVERFLOW;
}
示例#3
0
文件: vtimer.c 项目: jferreir/RIOT
int vtimer_sleep(timex_t time)
{
    /**
     * Use spin lock for short periods.
     * Assumes that hardware timer ticks are shorter than a second.
     */
    if (time.seconds == 0) {
        unsigned long ticks = HWTIMER_TICKS(time.microseconds);
        if (ticks <= HWTIMER_SPIN_BARRIER) {
            hwtimer_spin(ticks);
            return 0;
        }
    }

    int ret;
    vtimer_t t;
    mutex_t mutex = MUTEX_INIT;
    mutex_lock(&mutex);

    t.action = vtimer_callback_unlock;
    t.arg = &mutex;
    t.absolute = time;

    ret = vtimer_set(&t);
    mutex_lock(&mutex);
    return ret;
}
示例#4
0
//=========================== interrupt handlers ==============================
void radiotimer_isr(int arg) {
    (void)arg;
    uint8_t taiv_temp = radiotimer_vars.overflowORcompare;
    switch (taiv_temp) {
        case RADIOTIMER_COMPARE:
            DEBUG("%s cmp\n", __PRETTY_FUNCTION__);
            if (radiotimer_vars.compare_cb!=NULL) {
                radiotimer_vars.compare_cb();
                // kick the OS
                // return KICK_SCHEDULER;
            }
            break;
        case RADIOTIMER_OVERFLOW: // timer overflows
            DEBUG("%s of\n", __PRETTY_FUNCTION__);
            if (radiotimer_vars.overflow_cb!=NULL) {
                //Wait until last write operation on RTC registers has finished
                timer_set(OWSN_TIMER, 1, HWTIMER_TICKS(current_period));
                // call the callback
                radiotimer_vars.overflow_cb();
                DEBUG("returned...\n");
                // kick the OS
                // return KICK_SCHEDULER;
            }
            break;
      case RADIOTIMER_NONE:                     // this should not happen
            DEBUG("%s none\n", __PRETTY_FUNCTION__);
      default:
            DEBUG("%s default\n", __PRETTY_FUNCTION__);
            // while(1);                               // this should not happen
    }
    // return DO_NOT_KICK_SCHEDULER;
}
示例#5
0
文件: main.c 项目: JiapengLi/RIOT
void sender(void)
{
    unsigned int i = 0;
    msg_t mesg;
    transceiver_command_t tcmd;
    radio_packet_t p;

    mesg.type = SND_PKT;
    mesg.content.ptr = (char *) &tcmd;

    tcmd.transceivers = TRANSCEIVER_NATIVE;
    tcmd.data = &p;

    p.length = PACKET_SIZE;
    p.dst = 0;

    puts("Start sending packets");

    while (1) {
        /* filling uint8_t buffer with uint16_t sequence number */
        snd_buffer[0] = (i & 0xFF00) >> 8;
        snd_buffer[1] = i & 0x00FF;
        p.data = snd_buffer;
        i++;
        msg_send(&mesg, transceiver_pid);
        hwtimer_wait(HWTIMER_TICKS(SENDING_DELAY));
    }
}
示例#6
0
文件: main.c 项目: JiapengLi/RIOT
int main(void)
{
    int16_t a;
    msg_t mesg;
    transceiver_command_t tcmd;

    printf("\n\tmain(): initializing transceiver\n");
    transceiver_init(TRANSCEIVER_NATIVE);

    printf("\n\tmain(): starting transceiver\n");
    transceiver_start();

#ifndef SENDER
    printf("\n\tmain(): starting radio thread\n");
    kernel_pid_t radio_pid = thread_create(
            radio_stack_buffer, sizeof(radio_stack_buffer),
            PRIORITY_MAIN - 2, CREATE_STACKTEST,
            radio, NULL, "radio");
    transceiver_register(TRANSCEIVER_NATIVE, radio_pid);
#endif

#ifdef SENDER
    a = SENDER_ADDR;
#elif defined ADDR
    a = ADDR;
#else
    a = DEFAULT_RCV_ADDR;
#endif
    tcmd.transceivers = TRANSCEIVER_NATIVE;
    tcmd.data = &a;
    mesg.content.ptr = (char *) &tcmd;
    mesg.type = SET_ADDRESS;

    printf("[nativenet] trying to set address %" PRIi16 "\n", a);
    msg_send_receive(&mesg, &mesg, transceiver_pid);

#ifdef SENDER
    hwtimer_wait(HWTIMER_TICKS(SECOND));
    sender();
#else
    hwtimer_wait(HWTIMER_TICKS(WAIT_TIME * SECOND));
    receiving = 0;
    printf("Missed %u of %u packets after %u seconds\n", missed_cnt, (last_seq - first),  WAIT_TIME);
#endif

    return 0;
}
示例#7
0
void radiotimer_schedule(PORT_RADIOTIMER_WIDTH offset) {
    DEBUG("%s\n", __PRETTY_FUNCTION__);
    timer_irq_disable(OWSN_TIMER);
    timer_set(OWSN_TIMER, 1, ((unsigned int)HWTIMER_TICKS(offset)));
    timer_irq_enable(OWSN_TIMER);
    //set radiotimer irpstatus
    radiotimer_vars.overflowORcompare = RADIOTIMER_COMPARE;
}
示例#8
0
int reboot_arch(int mode)
{
    printf("Going into reboot, mode %i\n", mode);
    /* wait 1 ms to make sure the printf is finished */
    hwtimer_wait(HWTIMER_TICKS(1000));
    NVIC_SystemReset();
    return -1;
}
示例#9
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
/**
 * Configure bypass mode
 * Caution: This internal function does not acquire exclusive access to the I2C bus.
 *          Acquisation and release is supposed to be handled by the calling function.
 */
static void conf_bypass(mpu9150_t *dev, uint8_t bypass_enable)
{
   char data;
   i2c_read_reg(dev->i2c_dev, dev->hw_addr, MPU9150_USER_CTRL_REG, &data);

   if (bypass_enable) {
       data &= ~(BIT_I2C_MST_EN);
       i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_USER_CTRL_REG, data);
       hwtimer_wait(HWTIMER_TICKS(MPU9150_BYPASS_SLEEP_US));
       i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_INT_PIN_CFG_REG, BIT_I2C_BYPASS_EN);
   }
   else {
       data |= BIT_I2C_MST_EN;
       i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_USER_CTRL_REG, data);
       hwtimer_wait(HWTIMER_TICKS(MPU9150_BYPASS_SLEEP_US));
       i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_INT_PIN_CFG_REG, REG_RESET);
   }
}
示例#10
0
void radiotimer_cancel(void) {
    DEBUG("%s\n", __PRETTY_FUNCTION__);
    timer_irq_disable(OWSN_TIMER);
    timer_clear(OWSN_TIMER, 1);
    timer_set(OWSN_TIMER, 1, ((unsigned int)HWTIMER_TICKS(current_period)));
    timer_irq_enable(OWSN_TIMER);

    //set radiotimer irpstatus
    radiotimer_vars.overflowORcompare = RADIOTIMER_OVERFLOW;
}
示例#11
0
文件: main.c 项目: 4120976/RIOT
int main(void)
{
    puts("hwtimer test application...");

    puts("");
    puts("  Timers should print \"callback x\" once when they run out.");
    printf("  The order for x is 1, n-1, n-2, ..., 2 where n is the number of available hardware timers (%u on this platform).\n", HWTIMER_MAXTIMERS);
    puts("  One timer should fire every second until all timers have run out.");
    puts("  Additionally the message \"hwtimer set.\" should be printed once 1 second from now.");
    puts("");
    puts("Setting timers:");
    puts("");

    unsigned long delay = BASE_DELAY + ((HWTIMER_MAXTIMERS - 1) * DELTA_DELAY);

    /* make the first timer first to fire so timers do not run out linearly */
    char *msgn = msg;
    snprintf(msgn, MSGLEN, "callback %2x", 1);
    hwtimer_set(HWTIMER_TICKS(BASE_DELAY), callback, (void *) msgn);
    printf("set %s\n", msgn);

    /* set up to HWTIMER_MAXTIMERS-1 because hwtimer_wait below also
     * needs a timer */
    for (int i = 1; i < (HWTIMER_MAXTIMERS - 1); i++) {
        msgn = msg + (i * MSGLEN);
        delay -= DELTA_DELAY;
        snprintf(msgn, MSGLEN, "callback %2x", i + 1);
        hwtimer_set(HWTIMER_TICKS(delay), callback, (void *) msgn);
        printf("set %s\n", msgn);
    }

    puts("");
    puts("All timers set.");
    puts("");

    hwtimer_wait(HWTIMER_TICKS(1000UL * 1000UL));

    puts("hwtimer set.");
    thread_sleep();
    return 0;
}
示例#12
0
uint32_t srf02_get_distance(uint8_t ranging_mode)
{
    bool status = false;
    uint8_t reg_size = 1;
    uint8_t range_high_byte = 0;
    uint8_t range_low_byte = 0;
    uint8_t rx_buff[reg_size];
    uint8_t tx_buff[reg_size];
    uint32_t distance = 0;

    tx_buff[0] = ranging_mode;
    status = i2c_write(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
                       SRF02_COMMAND_REG, tx_buff, reg_size);

    if (!status) {
        puts("Write the ranging command to the i2c-interface is failed");
        distance = UINT32_MAX;
        return distance;
    }

    hwtimer_wait(HWTIMER_TICKS(65000));

    status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
                      SRF02_RANGE_HIGH_BYTE, rx_buff, reg_size);

    if (!status) {
        puts("Read the high echo byte from the i2c-interface is failed");
        distance = UINT32_MAX;
        return distance;
    }

    range_high_byte = rx_buff[0];

    status = i2c_read(SRF02_I2C_INTERFACE, SRF02_DEFAULT_ADDR,
                      SRF02_RANGE_LOW_BYTE, rx_buff, reg_size);

    if (!status) {
        puts("Read the low echo byte from the i2c-interface is failed");
        distance = UINT32_MAX;
        return distance;
    }

    range_low_byte = rx_buff[0];

    distance = (range_high_byte << 8) | range_low_byte;
    //printf("%u | %u\n", range_high_byte, range_low_byte);
    return distance;
}
示例#13
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
int mpu9150_set_gyro_power(mpu9150_t *dev, mpu9150_pwr_t pwr_conf)
{
    char pwr_2_setting;

    if (dev->conf.gyro_pwr == pwr_conf) {
        return 0;
    }

    /* Acquire exclusive access */
    if (i2c_acquire(dev->i2c_dev)) {
        return -1;
    }

    /* Read current power management 2 configuration */
    i2c_read_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, &pwr_2_setting);
    /* Prepare power register settings */
    if (pwr_conf == MPU9150_SENSOR_PWR_ON) {
        /* Set clock to pll */
        i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_PLL);
        pwr_2_setting &= ~(MPU9150_PWR_GYRO);
    }
    else {
        /* Configure power management 1 register */
        if ((dev->conf.accel_pwr == MPU9150_SENSOR_PWR_OFF)
                && (dev->conf.compass_pwr == MPU9150_SENSOR_PWR_OFF)) {
            /* All sensors turned off, put the MPU-9150 to sleep */
            i2c_write_reg(dev->i2c_dev, dev->hw_addr,
                    MPU9150_PWR_MGMT_1_REG, BIT_PWR_MGMT1_SLEEP);
        }
        else {
            /* Reset clock to internal oscillator */
            i2c_write_reg(dev->i2c_dev, dev->hw_addr,
                    MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_WAKEUP);
        }
        pwr_2_setting |= MPU9150_PWR_GYRO;
    }
    /* Enable/disable gyroscope standby in power management 2 register */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, pwr_2_setting);

    /* Release the bus */
    i2c_release(dev->i2c_dev);

    dev->conf.gyro_pwr = pwr_conf;
    hwtimer_wait(HWTIMER_TICKS(MPU9150_PWR_CHANGE_SLEEP_US));

    return 0;
}
示例#14
0
文件: vtimer.c 项目: AnonMall/RIOT
static int update_shortterm(void)
{
    if (shortterm_priority_queue_root.first == NULL) {
        /* there is no vtimer to schedule, queue is empty */
        DEBUG("update_shortterm: shortterm_priority_queue_root.next == NULL - dont know what to do here\n");
        return 0;
    }
    if (hwtimer_id != -1) {
        /* there is a running hwtimer for us */
        if (hwtimer_next_absolute != shortterm_priority_queue_root.first->priority) {
            /* the next timer in the vtimer queue is not the next hwtimer */
            /* we have to remove the running hwtimer (and schedule a new one) */
            hwtimer_remove(hwtimer_id);
        }
        else {
            /* the next vtimer is the next hwtimer, nothing to do */
            return 0;
        }
    }

    /* short term part of the next vtimer */
    hwtimer_next_absolute = shortterm_priority_queue_root.first->priority;

    uint32_t next = hwtimer_next_absolute;

    /* current short term time */
    uint32_t now = HWTIMER_TICKS_TO_US(hwtimer_now());

    /* make sure the longterm_tick_timer does not get truncated */
    if (node_get_timer(shortterm_priority_queue_root.first)->action != vtimer_callback_tick) {
        /* the next vtimer to schedule is the long term tick */
        /* it has a shortterm offset of longterm_tick_start */
        next += longterm_tick_start;
    }

    if((next -  HWTIMER_TICKS_TO_US(VTIMER_THRESHOLD) - now) > MICROSECONDS_PER_TICK ) {
        DEBUG("truncating next (next -  HWTIMER_TICKS_TO_US(VTIMER_THRESHOLD) - now): %lu\n", (next -  HWTIMER_TICKS_TO_US(VTIMER_THRESHOLD) - now));
        next = now +  HWTIMER_TICKS_TO_US(VTIMER_BACKOFF);
    }

    DEBUG("update_shortterm: Set hwtimer to %" PRIu32 " (now=%lu)\n", next, HWTIMER_TICKS_TO_US(hwtimer_now()));
    hwtimer_id = hwtimer_set_absolute(HWTIMER_TICKS(next), vtimer_callback, NULL);

    return 0;
}
示例#15
0
文件: ping.c 项目: A-L-E-X/RIOT
void ping(radio_address_t addr, uint8_t channr)
{
    cc1100_set_packet_handler(protocol_id, pong_handler);
    cc1100_set_channel(channr);
    cc1100_set_address(r_address);

    while (1) {
        vtimer_now(&start);
        int trans_ok = cc1100_send_csmaca(addr,
                                          protocol_id, 2, pipa->payload, sizeof(pipa->payload));

        if (trans_ok < 0) {
            print_failed();
        }

        hwtimer_wait(HWTIMER_TICKS(500 * 1000));
    }
}
示例#16
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
int mpu9150_set_compass_power(mpu9150_t *dev, mpu9150_pwr_t pwr_conf)
{
    char pwr_1_setting, usr_ctrl_setting, s1_do_setting;

    if (dev->conf.compass_pwr == pwr_conf) {
        return 0;
    }

    /* Acquire exclusive access */
    if (i2c_acquire(dev->i2c_dev)) {
        return -1;
    }

    /* Read current user control configuration */
    i2c_read_reg(dev->i2c_dev, dev->hw_addr, MPU9150_USER_CTRL_REG, &usr_ctrl_setting);
    /* Prepare power register settings */
    if (pwr_conf == MPU9150_SENSOR_PWR_ON) {
        pwr_1_setting = MPU9150_PWR_WAKEUP;
        s1_do_setting = MPU9150_COMP_SINGLE_MEASURE;
        usr_ctrl_setting |= BIT_I2C_MST_EN;
    }
    else {
        pwr_1_setting = BIT_PWR_MGMT1_SLEEP;
        s1_do_setting = MPU9150_COMP_POWER_DOWN;
        usr_ctrl_setting &= ~(BIT_I2C_MST_EN);
    }
    /* Configure power management 1 register if needed */
    if ((dev->conf.gyro_pwr == MPU9150_SENSOR_PWR_OFF)
            && (dev->conf.accel_pwr == MPU9150_SENSOR_PWR_OFF)) {
        i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, pwr_1_setting);
    }
    /* Configure mode writing by slave line 1 */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE1_DATA_OUT_REG, s1_do_setting);
    /* Enable/disable I2C master mode */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_USER_CTRL_REG, usr_ctrl_setting);

    /* Release the bus */
    i2c_release(dev->i2c_dev);

    dev->conf.compass_pwr = pwr_conf;
    hwtimer_wait(HWTIMER_TICKS(MPU9150_PWR_CHANGE_SLEEP_US));

    return 0;
}
示例#17
0
void srf02_start_ranging(uint16_t ranging_mode)
{
    uint32_t distance = 0;

    while (1) {
        distance = srf02_get_distance(ranging_mode);

        if (distance != UINT32_MAX) {
            switch (ranging_mode) {
                case SRF02_REAL_RANGING_MODE_CM :
                    printf("distance = %lu cm\n", distance);
                    break;

                case SRF02_REAL_RANGING_MODE_INCH :
                    printf("distance = %lu inch\n", distance);
                    break;

                case SRF02_REAL_RANGING_MODE_MICRO_SEC:
                    // dist_m = 0.000172 distance_micro_sec (air)
                    printf("distance = %lu micro_sec\n", distance);
                    break;

                case SRF02_FAKE_RANGING_MODE_CM:
                case SRF02_FAKE_RANGING_MODE_INCH:
                case SRF02_FAKE_RANGING_MODE_MICRO_SEC:
                    printf("distance fake ranging = %lu \n", distance);
                    break;

                default:
                    printf("distance = %lu cm\n", distance);
            }

            hwtimer_wait(HWTIMER_TICKS(50000));
        }
        else {
            break;
        }
    }

    puts("The SRF02 range sampling is ended!!");
}
示例#18
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
int mpu9150_set_accel_power(mpu9150_t *dev, mpu9150_pwr_t pwr_conf)
{
    char pwr_1_setting, pwr_2_setting;

    if (dev->conf.accel_pwr == pwr_conf) {
        return 0;
    }

    /* Acquire exclusive access */
    if (i2c_acquire(dev->i2c_dev)) {
        return -1;
    }

    /* Read current power management 2 configuration */
    i2c_read_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, &pwr_2_setting);
    /* Prepare power register settings */
    if (pwr_conf == MPU9150_SENSOR_PWR_ON) {
        pwr_1_setting = MPU9150_PWR_WAKEUP;
        pwr_2_setting &= ~(MPU9150_PWR_ACCEL);
    }
    else {
        pwr_1_setting = BIT_PWR_MGMT1_SLEEP;
        pwr_2_setting |= MPU9150_PWR_ACCEL;
    }
    /* Configure power management 1 register if needed */
    if ((dev->conf.gyro_pwr == MPU9150_SENSOR_PWR_OFF)
            && (dev->conf.compass_pwr == MPU9150_SENSOR_PWR_OFF)) {
        i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, pwr_1_setting);
    }
    /* Enable/disable accelerometer standby in power management 2 register */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, pwr_2_setting);

    /* Release the bus */
    i2c_release(dev->i2c_dev);

    dev->conf.accel_pwr = pwr_conf;
    hwtimer_wait(HWTIMER_TICKS(MPU9150_PWR_CHANGE_SLEEP_US));

    return 0;
}
示例#19
0
文件: main.c 项目: michz/diy14bus
int main(void)
{
  GPIO_0_CLKEN();
  gpio_init_out(GPIO_0, GPIO_NOPULL);
  GPIO_1_CLKEN();
  gpio_init_out(GPIO_1, GPIO_NOPULL);
  GPIO_2_CLKEN();
  gpio_init_out(GPIO_2, GPIO_NOPULL);
  GPIO_3_CLKEN();
  gpio_init_out(GPIO_3, GPIO_NOPULL);

  GPIO_9_CLKEN();
  gpio_init_in(GPIO_9, GPIO_PULLDOWN);
  GPIO_10_CLKEN();
  gpio_init_in(GPIO_10, GPIO_PULLDOWN);
  GPIO_11_CLKEN();
  gpio_init_in(GPIO_11, GPIO_PULLDOWN);

      gpio_set(GPIO_3);

      gpio_clear(GPIO_1);
      hwtimer_wait(HWTIMER_TICKS(500 * 1000));
      gpio_set(GPIO_0);
      gpio_set(GPIO_1);
      gpio_set(GPIO_2);

    while(1) {
      #if LEEEEDS
      gpio_clear(GPIO_1);
      hwtimer_wait(HWTIMER_TICKS(500 * 1000));

      gpio_clear(GPIO_0);
      hwtimer_wait(HWTIMER_TICKS(500 * 1000));
      gpio_clear(GPIO_2);
      hwtimer_wait(HWTIMER_TICKS(500 * 1000));
      gpio_set(GPIO_0);
      gpio_set(GPIO_1);
      gpio_set(GPIO_2);
      #endif

     if (gpio_read(GPIO_9))
      {
        gpio_clear(GPIO_0);
      }
      else
        {
          gpio_set(GPIO_0);
        }

        if (gpio_read(GPIO_10))
          {
            gpio_clear(GPIO_1);
          }
          else
            {
              gpio_set(GPIO_1);
            }
            
            if (gpio_read(GPIO_11))
              {
                gpio_clear(GPIO_2);
              }
              else
                {
                  gpio_set(GPIO_2);
                }
    }
    return 0;
}
示例#20
0
void ng_at86rf2xx_reset(ng_at86rf2xx_t *dev)
{
#if CPUID_ID_LEN
    uint8_t cpuid[CPUID_ID_LEN];
    eui64_t addr_long;
#endif

    /* trigger hardware reset */
    gpio_clear(dev->reset_pin);
    hwtimer_wait(HWTIMER_TICKS(RESET_DELAY));
    gpio_set(dev->reset_pin);
    /* reset options and sequence number */
    dev->seq_nr = 0;
    dev->options = 0;
    /* set short and long address */
#if CPUID_ID_LEN
    cpuid_get(cpuid);

#if CPUID_ID_LEN < 8
    /* in case CPUID_ID_LEN < 8, fill missing bytes with zeros */
    for (int i = CPUID_ID_LEN; i < 8; i++) {
        cpuid[i] = 0;
    }
#else
    for (int i = 8; i < CPUID_ID_LEN; i++) {
        cpuid[i & 0x07] ^= cpuid[i];
    }
#endif
    /* make sure we mark the address as non-multicast and not globally unique */
    cpuid[0] &= ~(0x01);
    cpuid[0] |= 0x02;
    /* copy and set long address */
    memcpy(&addr_long, cpuid, 8);
    ng_at86rf2xx_set_addr_long(dev, NTOHLL(addr_long.uint64.u64));
    ng_at86rf2xx_set_addr_short(dev, NTOHS(addr_long.uint16[0].u16));
#else
    ng_at86rf2xx_set_addr_long(dev, NG_AT86RF2XX_DEFAULT_ADDR_LONG);
    ng_at86rf2xx_set_addr_short(dev, NG_AT86RF2XX_DEFAULT_ADDR_SHORT);
#endif
    /* set default PAN id */
    ng_at86rf2xx_set_pan(dev, NG_AT86RF2XX_DEFAULT_PANID);
    /* set default channel */
    ng_at86rf2xx_set_chan(dev, NG_AT86RF2XX_DEFAULT_CHANNEL);
    /* set default TX power */
    ng_at86rf2xx_set_txpower(dev, NG_AT86RF2XX_DEFAULT_TXPOWER);
    /* set default options */
    ng_at86rf2xx_set_option(dev, NG_AT86RF2XX_OPT_AUTOACK, true);
    ng_at86rf2xx_set_option(dev, NG_AT86RF2XX_OPT_CSMA, true);
    ng_at86rf2xx_set_option(dev, NG_AT86RF2XX_OPT_TELL_RX_START, false);
    ng_at86rf2xx_set_option(dev, NG_AT86RF2XX_OPT_TELL_RX_END, true);
    /* set default protocol */
#ifdef MODULE_NG_SIXLOWPAN
    dev->proto = NG_NETTYPE_SIXLOWPAN;
#else
    dev->proto = NG_NETTYPE_UNDEF;
#endif
    /* enable safe mode (protect RX FIFO until reading data starts) */
    ng_at86rf2xx_reg_write(dev, NG_AT86RF2XX_REG__TRX_CTRL_2,
                          NG_AT86RF2XX_TRX_CTRL_2_MASK__RX_SAFE_MODE);
#ifdef MODULE_NG_AT86RF212B
    ng_at86rf2xx_set_freq(dev,NG_AT86RF2XX_FREQ_915MHZ);
#endif

    /* don't populate masked interrupt flags to IRQ_STATUS register */
    uint8_t tmp = ng_at86rf2xx_reg_read(dev, NG_AT86RF2XX_REG__TRX_CTRL_1);
    tmp &= ~(NG_AT86RF2XX_TRX_CTRL_1_MASK__IRQ_MASK_MODE);
    ng_at86rf2xx_reg_write(dev, NG_AT86RF2XX_REG__TRX_CTRL_1, tmp);

    /* enable interrupts */
    ng_at86rf2xx_reg_write(dev, NG_AT86RF2XX_REG__IRQ_MASK,
                          NG_AT86RF2XX_IRQ_STATUS_MASK__TRX_END);
    /* clear interrupt flags */
    ng_at86rf2xx_reg_read(dev, NG_AT86RF2XX_REG__IRQ_STATUS);

    /* go into RX state */
    ng_at86rf2xx_set_state(dev, NG_AT86RF2XX_STATE_RX_AACK_ON);

    DEBUG("ng_at86rf2xx_reset(): reset complete.\n");
}
示例#21
0
文件: main.c 项目: michz/diy14bus
int main(void)
{

	//noetig? Nein, LE nicht notwendig fuer SPI-Konfiguration.
    //GPIOA->MODER &= ~(2 << (2 * 4));           /* set pin to output mode */
    //GPIOA->MODER |= (1 << (2 * 4));
    //GPIOA->OTYPER &= ~(1 << 4);                /* set to push-pull configuration */
    //GPIOA->OSPEEDR |= (3 << (2 * 4));          /* set to high speed */
    //GPIOA->PUPDR &= ~(3 << (2 * 4));           /* configure push-pull resistors */
    //GPIOA->PUPDR |= (0b00 << (2 * 4));
    //GPIOA->ODR &= ~(1 << 4);                   /* set pin to low signal */

	LD4_ON;

	if(spi_init_master(SPI_0, SPI_CONF_FIRST_RISING, SPI_SPEED_1MHZ) != 0){
		while(1) LD3_ON; //nix mehr machen

	}
	//GPIOs work but the clock has to be enabled first:
	//RCC->AHBENR |= RCC_AHBENR_GPIOEEN;

	//GPIO_6 : PE1
	//gpio_init_out(GPIO_6, GPIO_NOPULL); //just for debug

	char blubb = 0;
	char *mirwors = &blubb; //null und NULL wollte er nicht, daher halt umstaendlich

    while(1) {
		for(uint8_t i = 0; i<30; i++){
			
			//gpio_set(GPIO_6);

			//blau
			if(spi_transfer_byte(SPI_0, 0xFF, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}

			if(spi_transfer_byte(SPI_0, 0x80, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}
			if(spi_transfer_byte(SPI_0, 0x80, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}

			hwtimer_wait(HWTIMER_TICKS(TEST_WAIT * 1000));


			//gruen
			if(spi_transfer_byte(SPI_0, 0x80, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}
			if(spi_transfer_byte(SPI_0, 0x80, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}
			if(spi_transfer_byte(SPI_0, 0xFF, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}

			hwtimer_wait(HWTIMER_TICKS(TEST_WAIT * 1000));
		}

		for(uint16_t i=((60+31)/32); i>0; i--) {
			if(spi_transfer_byte(SPI_0, 0x00, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}
		}


		//gpio_clear(GPIO_6); //just for debug

		for(uint8_t i = 0; i<30; i++){

			//dunkel
			if(spi_transfer_byte(SPI_0, 0x80, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}
			if(spi_transfer_byte(SPI_0, 0x80, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}
			if(spi_transfer_byte(SPI_0, 0x80, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}

			hwtimer_wait(HWTIMER_TICKS(TEST_WAIT * 1000));

			//rot
			if(spi_transfer_byte(SPI_0, 0x80, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}
			if(spi_transfer_byte(SPI_0, 0xFF, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}
			if(spi_transfer_byte(SPI_0, 0x80, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}

			hwtimer_wait(HWTIMER_TICKS(TEST_WAIT * 1000));
		}


		for(uint16_t i=((60+31)/32); i>0; i--) {
			if(spi_transfer_byte(SPI_0, 0x00, mirwors) != 1){
				while(1) LD3_ON; //nix mehr machen
			}
		}


        //LD3_ON;
        //hwtimer_wait(HWTIMER_TICKS(TEST_WAIT * 1000));
        //LD3_OFF;
        //LD4_ON;
        //hwtimer_wait(HWTIMER_TICKS(500 * 1000));
        //LD4_OFF;
    }
    return 0;
}
示例#22
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
/**
 * Initialize compass
 * Caution: This internal function does not acquire exclusive access to the I2C bus.
 *          Acquisation and release is supposed to be handled by the calling function.
 */
static int compass_init(mpu9150_t *dev)
{
    char data[3];

    /* Enable Bypass Mode to speak to compass directly */
    conf_bypass(dev, 1);

    /* Check whether compass answers correctly */
    i2c_read_reg(dev->i2c_dev, dev->comp_addr, COMPASS_WHOAMI_REG, data);
    if (data[0] != MPU9150_COMP_WHOAMI_ANSWER) {
        DEBUG("[Error] Wrong answer from compass\n");
        return -1;
    }

    /* Configure Power Down mode */
    i2c_write_reg(dev->i2c_dev, dev->comp_addr, COMPASS_CNTL_REG, MPU9150_COMP_POWER_DOWN);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_COMP_MODE_SLEEP_US));
    /* Configure Fuse ROM access */
    i2c_write_reg(dev->i2c_dev, dev->comp_addr, COMPASS_CNTL_REG, MPU9150_COMP_FUSE_ROM);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_COMP_MODE_SLEEP_US));
    /* Read sensitivity adjustment values from Fuse ROM */
    i2c_read_regs(dev->i2c_dev, dev->comp_addr, COMPASS_ASAX_REG, data, 3);
    dev->conf.compass_x_adj = data[0];
    dev->conf.compass_y_adj = data[1];
    dev->conf.compass_z_adj = data[2];
    /* Configure Power Down mode again */
    i2c_write_reg(dev->i2c_dev, dev->comp_addr, COMPASS_CNTL_REG, MPU9150_COMP_POWER_DOWN);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_COMP_MODE_SLEEP_US));

    /* Disable Bypass Mode to configure MPU as master to the compass */
    conf_bypass(dev, 0);

    /* Configure MPU9150 for single master mode */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_I2C_MST_REG, BIT_WAIT_FOR_ES);

    /* Set up slave line 0 */
    /* Slave line 0 reads the compass data */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr,
            MPU9150_SLAVE0_ADDR_REG, (BIT_SLAVE_RW | dev->comp_addr));
    /* Slave line 0 read starts at compass data register */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE0_REG_REG, COMPASS_DATA_START_REG);
    /* Enable slave line 0 and configure read length to 6 consecutive registers */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE0_CTRL_REG, (BIT_SLAVE_EN | 0x06));

    /* Set up slave line 1 */
    /* Slave line 1 writes to the compass */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE1_ADDR_REG, dev->comp_addr);
    /* Slave line 1 write starts at compass control register */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE1_REG_REG, COMPASS_CNTL_REG);
    /* Enable slave line 1 and configure write length to 1 register */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE1_CTRL_REG, (BIT_SLAVE_EN | 0x01));
    /* Configure data which is written by slave line 1 to compass control */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr,
            MPU9150_SLAVE1_DATA_OUT_REG, MPU9150_COMP_SINGLE_MEASURE);

    /* Slave line 0 and 1 operate at each sample */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr,
            MPU9150_I2C_DELAY_CTRL_REG, (BIT_SLV0_DELAY_EN | BIT_SLV1_DELAY_EN));
    /* Set I2C bus to VDD */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_YG_OFFS_TC_REG, BIT_I2C_MST_VDDIO);

    return 0;
}
示例#23
0
 * Copyright (C) 2013 Christian Mehlis <*****@*****.**>
 *
 * This file subject to the terms and conditions of the GNU Lesser General
 * Public License. See the file LICENSE in the top level directory for more
 * details.
 */

#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include "hwtimer.h"
#include "ltc4150.h"
#include "thread.h"

uint32_t tick_ticks = HWTIMER_TICKS(500 * 1000);
int hwtimer_tick_id;

void test_ltc_tick(void *ptr)
{
    int pid = (int) ptr;

    hwtimer_tick_id = hwtimer_set(tick_ticks, test_ltc_tick, ptr);
    thread_wakeup(pid);
}

int main(void)
{
    ltc4150_start();

    hwtimer_tick_id = hwtimer_set(tick_ticks,