Exemplo n.º 1
0
void test_ltc_tick(void *ptr)
{
    int pid = (int) ptr;

    hwtimer_tick_id = hwtimer_set(tick_ticks, test_ltc_tick, ptr);
    thread_wakeup(pid);
}
Exemplo n.º 2
0
/**
 * register signal handler
 */
void ltc4150_enable_int(void)
{
    DEBUG("ltc4150_enable_int()\n");
    _int_enabled = 1;
    if (hwtimer_set(100000, _int_handler, NULL) == -1) {
        errx(1, "ltc4150_enable_int: hwtimer_set");
    };
}
Exemplo n.º 3
0
int main(void)
{
    puts("hwtimer test project.");

    puts("Initializing hwtimer...");
    hwtimer_init();

    puts("Initializing hwtimer [OK].");
   

//    while (TA0R < 20000);

    hwtimer_set(20000LU, callback, (void*)"callback1");
    hwtimer_set(50000LU, callback, (void*)"callback2");
    hwtimer_set(30000LU, callback, (void*)"callback3");
    
    puts("hwtimer set.");
}
Exemplo n.º 4
0
/**
 * native ltc4150 hwtimer - interrupt handler proxy
 */
static void _int_handler()
{
    DEBUG("ltc4150 _int_handler()\n");
    ltc4150_interrupt();
    if (_int_enabled == 1) {
        if (hwtimer_set(100000, _int_handler, NULL) == -1) {
            errx(1, "_int_handler: hwtimer_set");
        };
    }
}
Exemplo n.º 5
0
Arquivo: main.c Projeto: 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;
}
Exemplo n.º 6
0
int main(void)
{
    ltc4150_start();

    hwtimer_tick_id = hwtimer_set(tick_ticks,
                                  test_ltc_tick,
                                  (void *) thread_pid);

    while (1) {
        thread_sleep();
        printf("Power usage: %.4f mA (%.4f mA avg/ %.4f mAh total / %i usec)\n",
                ltc4150_get_current_mA(), ltc4150_get_avg_mA(),
                ltc4150_get_total_mAh(), ltc4150_get_interval());
    }

    return 0;
}
Exemplo n.º 7
0
void hwtimer_wait(unsigned long ticks)
{
    DEBUG("hwtimer_wait ticks=%lu\n", ticks);

    mutex_t mutex;

    if (ticks <= 6 || inISR()) {
        hwtimer_spin(ticks);
        return;
    }
    mutex_init(&mutex);
    mutex_lock(&mutex);
    /* -2 is to adjust the real value */
    int res = hwtimer_set(ticks - 2, hwtimer_releasemutex, &mutex);
    if (res == -1) {
        mutex_unlock(&mutex);
        hwtimer_spin(ticks);
        return;
    }

    /* try to lock mutex again will cause the thread to go into
     * STATUS_MUTEX_BLOCKED until hwtimer fires the releasemutex */
    mutex_lock(&mutex);
}
Exemplo n.º 8
0
/*---------------------------------------------------------------------------*/
int cc1100_send_csmaca(radio_address_t address, protocol_t protocol, int priority, char *payload, radio_packet_length_t payload_len)
{
    uint16_t min_window_size;
    uint16_t max_window_size;
    uint16_t difs;
    uint16_t slottime;

    switch(priority) {
        case PRIORITY_ALARM:
            min_window_size = PRIO_ALARM_MIN_WINDOW_SIZE;
            max_window_size = PRIO_ALARM_MAX_WINDOW_SIZE;
            difs = PRIO_ALARM_DIFS;
            slottime = PRIO_ALARM_SLOTTIME;
            break;

        case PRIORITY_WARNING:
            min_window_size = PRIO_WARN_MIN_WINDOW_SIZE;
            max_window_size = PRIO_WARN_MAX_WINDOW_SIZE;
            difs = PRIO_WARN_DIFS;
            slottime = PRIO_WARN_SLOTTIME;
            break;

        default:
            min_window_size = PRIO_DATA_MIN_WINDOW_SIZE;
            max_window_size = PRIO_DATA_MAX_WINDOW_SIZE;
            difs = PRIO_DATA_DIFS;
            slottime = PRIO_DATA_SLOTTIME;
    }

    /* Calculate collisions per second */
    if (collision_state == COLLISION_STATE_INITIAL) {
        vtimer_now(&collision_measurement_start);
        collision_count = 0;
        collisions_per_sec = 0;
        collision_state = COLLISION_STATE_MEASURE;
    }
    else if (collision_state == COLLISION_STATE_MEASURE) {
        timex_t now;
        vtimer_now(&now);
        timex_t timespan = timex_sub(now, collision_measurement_start);

        if (timex_cmp(timespan, timex_set(1, 0)) > 0) {
            collisions_per_sec = (collision_count * 1000000) / (double) timex_uint64(timespan);

            if (collisions_per_sec > 0.5 && collisions_per_sec <= 2.2) {
                timex_t now;
                vtimer_now(&now);
                collision_measurement_start = now;
                collision_state = COLLISION_STATE_KEEP;
            }
            else if (collisions_per_sec > 2.2) {
                timex_t now;
                vtimer_now(&now);
                collision_measurement_start = now;
                collision_state = COLLISION_STATE_KEEP;
            }
            else {
                collision_state = COLLISION_STATE_INITIAL;
            }
        }
    }
    else if (collision_state == COLLISION_STATE_KEEP) {
        timex_t now;
        vtimer_now(&now);
        timex_t timespan = timex_sub(now, collision_measurement_start);

        if (timex_cmp(timespan, timex_set(5, 0)) > 0) {
            collision_state = COLLISION_STATE_INITIAL;
        }
    }

    /* Adjust initial window size according to collision rate */
    if (collisions_per_sec > 0.5 && collisions_per_sec <= 2.2) {
        min_window_size *= 2;
    }
    else if (collisions_per_sec > 2.2) {
        min_window_size *= 4;
    }

    uint16_t windowSize = min_window_size;      /* Start with window size of PRIO_XXX_MIN_WINDOW_SIZE */
    uint16_t backoff = 0;                       /* Backoff between 1 and windowSize */
    uint32_t total;                             /* Holds the total wait time before send try */
    uint32_t cs_timeout;                        /* Current carrier sense timeout value */

    if (protocol == 0) {
        return RADIO_INVALID_PARAM;             /* Not allowed, protocol id must be greater zero */
    }

    cc1100_phy_mutex_lock();                    /* Lock radio for exclusive access */

    /* Get carrier sense timeout based on overall error rate till now */
    send_csmaca_calls++;
    int fail_percentage = (send_csmaca_calls_cs_timeout * 100) / send_csmaca_calls;

    if (fail_percentage == 0) {
        fail_percentage = 1;
    }

    cs_timeout = CARRIER_SENSE_TIMEOUT / fail_percentage;

    if (cs_timeout < CARRIER_SENSE_TIMEOUT_MIN) {
        cs_timeout = CARRIER_SENSE_TIMEOUT_MIN;
    }

    cc1100_cs_init();                           /* Initialize carrier sensing */

window:

    if (backoff != 0) {
        goto cycle;    /* If backoff was 0 */
    }

    windowSize *= 2;                        /* ...double the current window size */

    if (windowSize > max_window_size) {
        windowSize = max_window_size;       /* This is the maximum size allowed */
    }

    backoff = rand() % windowSize;          /* ...and choose new backoff */

    backoff += (uint16_t) 1;
cycle:
    cs_timeout_flag = 0;                    /* Carrier sense timeout flag */
    cs_hwtimer_id = hwtimer_set(cs_timeout, /* Set hwtimer to set CS timeout flag */
                                cs_timeout_cb, NULL);

    while (cc1100_cs_read()) {          /* Wait until air is free */
        if (cs_timeout_flag) {
            send_csmaca_calls_cs_timeout++;
#ifndef CSMACA_MAC_AGGRESSIVE_MODE
            cc1100_phy_mutex_unlock();
            cc1100_go_after_tx();           /* Go from RX to default mode */
            return RADIO_CS_TIMEOUT;        /* Return immediately */
#endif
#ifdef CSMACA_MAC_AGGRESSIVE_MODE
            goto send;                      /* Send anyway */
#endif
        }
    }

    hwtimer_remove(cs_hwtimer_id);          /* Remove hwtimer */
    cc1100_cs_write_cca(1);                 /* Air is free now */
    cc1100_cs_set_enabled(true);

    if (cc1100_cs_read()) {
        goto window;    /* GDO0 triggers on rising edge, so */
    }

    /* test once after interrupt is enabled */
    if (backoff > 0) {
        backoff--;    /* Decrement backoff counter */
    }

    total = slottime;                       /* Calculate total wait time */
    total *= (uint32_t)backoff;             /* Slot vector set */
    total += difs;                          /* ...and standard DIFS wait time */
    cs_timeout_flag = 0;                    /* Carrier sense timeout flag */
    cs_hwtimer_id = hwtimer_set(total,      /* Set hwtimer to set CS timeout flag */
                                cs_timeout_cb, NULL);

    while (!cs_timeout_flag
          || !cc1100_cs_read_cca()) {   /* Wait until timeout is finished */
        if (cc1100_cs_read_cca() == 0) {    /* Is the air still free? */
            hwtimer_remove(cs_hwtimer_id);
            goto window;                    /* No. Go back to new wait period. */
        }
    }

    cc1100_cs_set_enabled(false);
#ifdef CSMACA_MAC_AGGRESSIVE_MODE
send:
#endif
    int res = cc1100_send(address, protocol, priority, payload, payload_len);

    if (res < 0) {
        collision_count++;
    }

    return res;
}
Exemplo n.º 9
0
/*---------------------------------------------------------------------------*/
int cc1100_send_csmaca(radio_address_t address, protocol_t protocol, int priority, char *payload, int payload_len)
{
	uint16_t min_window_size;
	uint16_t max_window_size;
	uint16_t difs;
	uint16_t slottime;

	switch (priority)
	{
		case PRIORITY_ALARM:
			min_window_size = PRIO_ALARM_MIN_WINDOW_SIZE;
			max_window_size = PRIO_ALARM_MAX_WINDOW_SIZE;
			difs = PRIO_ALARM_DIFS;
			slottime = PRIO_ALARM_SLOTTIME;
			break;
		case PRIORITY_WARNING:
			min_window_size = PRIO_WARN_MIN_WINDOW_SIZE;
			max_window_size = PRIO_WARN_MAX_WINDOW_SIZE;
			difs = PRIO_WARN_DIFS;
			slottime = PRIO_WARN_SLOTTIME;
			break;
		default:
			min_window_size = PRIO_DATA_MIN_WINDOW_SIZE;
			max_window_size = PRIO_DATA_MAX_WINDOW_SIZE;
			difs = PRIO_DATA_DIFS;
			slottime = PRIO_DATA_SLOTTIME;
	}

	// Calculate collisions per second
	if (collision_state == COLLISION_STATE_INITIAL) {
		timex_t now = vtimer_now();
		collision_measurement_start =  now.microseconds;
		collision_count = 0;
		collisions_per_sec = 0;
		collision_state = COLLISION_STATE_MEASURE;
	} else if (collision_state == COLLISION_STATE_MEASURE) {
			timex_t now = vtimer_now();
			uint64_t timespan = now.microseconds - collision_measurement_start;
			if (timespan > 1000000) {
				collisions_per_sec = (collision_count * 1000000) / (double) timespan;
			if (collisions_per_sec > 0.5 && collisions_per_sec <= 2.2) {
				timex_t now = vtimer_now();
				collision_measurement_start = now.microseconds;
				collision_state = COLLISION_STATE_KEEP;
			} else if (collisions_per_sec > 2.2) {
				timex_t now = vtimer_now();
				collision_measurement_start = now.microseconds;
				collision_state = COLLISION_STATE_KEEP;
			} else {
				collision_state = COLLISION_STATE_INITIAL;
			}
		}
	} else if (collision_state == COLLISION_STATE_KEEP) {
		timex_t now = vtimer_now();
        uint64_t timespan = now.microseconds - collision_measurement_start;
        if (timespan > 5000000) {
			collision_state = COLLISION_STATE_INITIAL;
		}
	}

	// Adjust initial window size according to collision rate
	if (collisions_per_sec > 0.5 && collisions_per_sec <= 2.2) {
		min_window_size *= 2;
	} else if (collisions_per_sec > 2.2) {
		min_window_size *= 4;
	}

	uint16_t windowSize = min_window_size;		// Start with window size of PRIO_XXX_MIN_WINDOW_SIZE
	uint16_t backoff = 0;						// Backoff between 1 and windowSize
	uint32_t total;								// Holds the total wait time before send try
	uint32_t cs_timeout;						// Current carrier sense timeout value

	if (protocol == 0)
	{
		return RADIO_INVALID_PARAM;				// Not allowed, protocol id must be greater zero
	}

	cc1100_phy_mutex_lock();					// Lock radio for exclusive access

	// Get carrier sense timeout based on overall error rate till now
	send_csmaca_calls++;
	int fail_percentage = (send_csmaca_calls_cs_timeout * 100) / send_csmaca_calls;
	if (fail_percentage == 0) fail_percentage = 1;
	cs_timeout = CARRIER_SENSE_TIMEOUT / fail_percentage;
	if (cs_timeout < CARRIER_SENSE_TIMEOUT_MIN) cs_timeout = CARRIER_SENSE_TIMEOUT_MIN;

	cc1100_cs_init();							// Initialize carrier sensing

	window:
		if (backoff != 0) goto cycle;			// If backoff was 0
		windowSize *= 2;						// ...double the current window size
		if (windowSize > max_window_size)
		{
			windowSize = max_window_size;		// This is the maximum size allowed
		}
		backoff = rand() % windowSize;			// ...and choose new backoff
		if (backoff < 0) backoff *= -1;
		backoff += (uint16_t) 1;
	cycle:
		cs_timeout_flag = 0;					// Carrier sense timeout flag
		cs_hwtimer_id = hwtimer_set(cs_timeout,	// Set hwtimer to set CS timeout flag
				cs_timeout_cb, NULL);
		while (cc1100_cs_read())				// Wait until air is free
		{
			if (cs_timeout_flag)
			{
				send_csmaca_calls_cs_timeout++;
#ifndef CSMACA_MAC_AGGRESSIVE_MODE
				cc1100_phy_mutex_unlock();
				cc1100_go_after_tx();			// Go from RX to default mode
				return RADIO_CS_TIMEOUT;		// Return immediately
#endif
#ifdef CSMACA_MAC_AGGRESSIVE_MODE
				goto send;						// Send anyway
#endif
			}
		}
		hwtimer_remove(cs_hwtimer_id);			// Remove hwtimer
		cc1100_cs_write_cca(1);					// Air is free now
		cc1100_cs_set_enabled(true);
		if (cc1100_cs_read()) goto window;		// GDO0 triggers on rising edge, so
												// test once after interrupt is enabled
		if (backoff > 0) backoff--;				// Decrement backoff counter
		total = slottime;						// Calculate total wait time
		total *= (uint32_t)backoff;				// Slot vector set
		total += difs;							// ...and standard DIFS wait time
		cs_timeout_flag = 0;					// Carrier sense timeout flag
		cs_hwtimer_id = hwtimer_set(total,		// Set hwtimer to set CS timeout flag
				cs_timeout_cb, NULL);
		while (!cs_timeout_flag
				|| !cc1100_cs_read_cca())		// Wait until timeout is finished
		{
			if (cc1100_cs_read_cca() == 0)		// Is the air still free?
			{
				hwtimer_remove(cs_hwtimer_id);
				goto window;					// No. Go back to new wait period.
			}
		}
		cc1100_cs_set_enabled(false);
#ifdef CSMACA_MAC_AGGRESSIVE_MODE
	send:
#endif
		int res = cc1100_send(address, protocol, priority, payload, payload_len);
		if (res < 0) {
			collision_count++;
		}
		return res;
}