Exemplo n.º 1
0
void DS1820::pinChanged(uint8_t level)
{
    if (this->level == level) {
        return;
    }

    const uint32_t duration = avr_cycles_to_usec(avr, avr->cycle - lastChange);
    lastChange = avr->cycle;
    this->level = level;

    const bool low = (level == 0);
    if (low) {
        return;
    }

    /* RESET works during all states. */
    if (duration > MASTER_RESET_MIN) {
        DEBUG("%s: RESET", __PRETTY_FUNCTION__);
        in = 0;
        incount = 0;
        out = 0;
        outcount = 0;
        state = RESET_WAIT;
        wait(DS1820_RESET_WAIT_MIN);
        return;
    }

    switch (state) {

    case ROM_COMMAND:

        in |= read(duration) << incount;
        incount++;

        if (incount == 8) {
            romCommand();
        }

        break;

    case FUNCTION_COMMAND:

        in |= read(duration) << incount;
        incount++;

        if (incount == 8) {
            functionCommand();
        }

        break;

    case SEARCH_ROM:

        if (outcount > 0) { /* One of the two write slots per bit. */
            write(out & 1);
            outcount--;
            out >>= 1;
        } else { /* A read slot. */
            if ((read(duration) ^ (ROM_ID >> incount)) & 1) {
Exemplo n.º 2
0
static void
avr_timer_configure(
		avr_timer_t * p,
		uint32_t clock,
		uint32_t top,
		uint8_t reset)
{
	p->tov_cycles = 0;
	p->tov_top = top;

	p->tov_cycles = clock * (top+1);

	AVR_LOG(p->io.avr, LOG_TRACE, "TIMER: %s-%c TOP %.2fHz = %d cycles = %dusec\n",
			__FUNCTION__, p->name, (p->io.avr->frequency / (float)p->tov_cycles),
			(int)p->tov_cycles, (int)avr_cycles_to_usec(p->io.avr, p->tov_cycles));

	for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
		if (!p->comp[compi].r_ocr)
			continue;
		uint32_t ocr = _timer_get_ocr(p, compi);
		uint32_t comp_cycles = clock * (ocr + 1);

		p->comp[compi].comp_cycles = 0;

		if (p->trace & (avr_timer_trace_compa << compi))
			printf("%s-%c clock %f top %d OCR%c %d\n", __FUNCTION__, p->name,
				(float)(p->io.avr->frequency / clock), top, 'A'+compi, ocr);

		if (ocr && ocr <= top) {
			p->comp[compi].comp_cycles = comp_cycles; // avr_hz_to_cycles(p->io.avr, fa);

			if (p->trace & (avr_timer_trace_compa << compi)) printf(
					"TIMER: %s-%c %c %.2fHz = %d cycles\n",
					__FUNCTION__, p->name,
					'A'+compi, (float)(p->io.avr->frequency / ocr),
					(int)p->comp[compi].comp_cycles);
		}
	}

	if (p->tov_cycles > 1) {
		if (reset)
		{
			avr_cycle_timer_register(p->io.avr, p->tov_cycles, avr_timer_tov, p);
			// calling it once, with when == 0 tells it to arm the A/B/C timers if needed
			p->tov_base = 0;
			avr_timer_tov(p->io.avr, p->io.avr->cycle, p);
		}
		else
		{
			uint64_t orig_tov_base = p->tov_base;
			avr_cycle_timer_register(p->io.avr, p->tov_cycles - (p->io.avr->cycle - orig_tov_base), avr_timer_tov, p);
			// calling it once, with when == 0 tells it to arm the A/B/C timers if needed
			p->tov_base = 0;
			avr_timer_tov(p->io.avr, orig_tov_base, p);
		}
	}
}
Exemplo n.º 3
0
/**
 * Accumulates sleep requests (and returns a sleep time of 0) until
 * a minimum count of requested sleep microseconds are reached
 * (low amounts cannot be handled accurately).
 */
uint32_t
avr_pending_sleep_usec(
		avr_t * avr,
		avr_cycle_count_t howLong)
{
	avr->sleep_usec += avr_cycles_to_usec(avr, howLong);
	uint32_t usec = avr->sleep_usec;
	if (usec > 200) {
		avr->sleep_usec = 0;
		return usec;
	}
	return 0;
}
Exemplo n.º 4
0
static void avr_timer_configure(avr_timer_t * p, uint32_t clock, uint32_t top)
{
	float t = clock / (float)(top+1);
	float frequency = p->io.avr->frequency;

	p->tov_cycles = 0;
	p->tov_top = top;

	p->tov_cycles = frequency / t; // avr_hz_to_cycles(frequency, t);

	AVR_LOG(p->io.avr, LOG_TRACE, "TIMER: %s-%c TOP %.2fHz = %d cycles = %dusec\n",
			__FUNCTION__, p->name, t, (int)p->tov_cycles,
			(int)avr_cycles_to_usec(p->io.avr, p->tov_cycles));

	for (int compi = 0; compi < AVR_TIMER_COMP_COUNT; compi++) {
		if (!p->comp[compi].r_ocr)
			continue;
		uint32_t ocr = _timer_get_ocr(p, compi);
		float fc = clock / (float)(ocr+1);

		p->comp[compi].comp_cycles = 0;
	//	printf("%s-%c clock %d top %d OCR%c %d\n", __FUNCTION__, p->name, clock, top, 'A'+compi, ocr);

		if (ocr && ocr <= top) {
			p->comp[compi].comp_cycles = frequency / fc; // avr_hz_to_cycles(p->io.avr, fa);
			AVR_LOG(p->io.avr, LOG_TRACE, "TIMER: %s-%c %c %.2fHz = %d cycles\n", 
					__FUNCTION__, p->name,
					'A'+compi, fc, (int)p->comp[compi].comp_cycles);
		}
	}

	if (p->tov_cycles > 1) {
		avr_cycle_timer_register(p->io.avr, p->tov_cycles, avr_timer_tov, p);
		// calling it once, with when == 0 tells it to arm the A/B/C timers if needed
		p->tov_base = 0;
		avr_timer_tov(p->io.avr, p->io.avr->cycle, p);
	}
}
Exemplo n.º 5
0
void avr_callback_sleep_raw(avr_t * avr, avr_cycle_count_t howLong)
{
	uint32_t usec = avr_cycles_to_usec(avr, howLong);
	usleep(usec);
}
Exemplo n.º 6
0
void avr_callback_sleep_gdb(avr_t * avr, avr_cycle_count_t howLong)
{
	uint32_t usec = avr_cycles_to_usec(avr, howLong);
	while (avr_gdb_processor(avr, usec))
		;
}