Exemplo n.º 1
0
int main(void) {
  sei(); // enable interrupts
  leds_init();
  pwm_init();
  bot_init();
  i2c_init();

  if (display_init()!=0) {
    leds_set_displaylight(50);
    if (display_type==2) {
      gfx_init();
    }
  }
  
  gfx_fill(0x00);
  gfx_move(15, 0);
  gfx_set_proportional(1);
  gfx_print_text("nibo");
  gfx_set_proportional(0);

  uint8_t pos=0;
  uint8_t state=0;

  gfx_term_print("Move 0\n");
  nds3_move(0);
  while (nds3_get_busy()) {
    delay(1);
  }
  
  while (1) {
    gfx_draw_mode(GFX_DM_JAM2);

    check_voltage();

   
    //gfx_term_print("Measure 90, 15\n");
      delay(1);
    nds3_measure(180, 1);
    while (nds3_get_busy()) {
      delay(1);
    }

      delay(1);
    //gfx_term_print("Read -90, 15\n");
    do_plot();

      delay(1);
    nds3_measure(0, 1);
    while (nds3_get_busy()) {
      delay(1);
    }

      delay(1);
    //gfx_term_print("Read 90, 15\n");
    nds3_read(0, 15);
    do_plot();
    
    
  }
}
Exemplo n.º 2
0
Arquivo: main.c Projeto: ksrm/redox
void enter_sleep_mode(void) {

    get_voltage();

    xprintf("sleeping\r\n");

    cli();
    PORTD = 0x73;           // Select all rows
    PCMSK0 = 0xFF;          // Enable all pin change interrupts
    PCIFR = (1 << PCIF0);
    PCICR = (1 << PCIE0);


    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sei();                  // Enable interrupts       
    sleep_mode();

    check_voltage();

}
Exemplo n.º 3
0
int do_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	uint32_t ret, base_flags;
	bool prompt_user, force_read;
	uint32_t (*otp_func)(uint32_t page, uint32_t flags, uint64_t *page_content);

	if (argc < 4) {
 usage:
		cmd_usage(cmdtp);
		return 1;
	}

	prompt_user = false;
	base_flags = 0;
	if (!strcmp(argv[1], "read"))
		otp_func = bfrom_OtpRead;
	else if (!strcmp(argv[1], "dump")) {
		otp_func = bfrom_OtpRead;
		force_read = true;
	} else if (!strcmp(argv[1], "write")) {
		otp_func = bfrom_OtpWrite;
		base_flags = OTP_CHECK_FOR_PREV_WRITE;
		if (!strcmp(argv[2], "--force")) {
			argv[2] = argv[1];
			argv++;
			--argc;
		} else
			prompt_user = false;
	} else if (!strcmp(argv[1], "lock")) {
		if (argc != 4)
			goto usage;
		otp_func = bfrom_OtpWrite;
		base_flags = OTP_LOCK;
	} else
		goto usage;

	uint64_t *addr = (uint64_t *)simple_strtoul(argv[2], NULL, 16);
	uint32_t page = simple_strtoul(argv[3], NULL, 16);
	uint32_t flags;
	size_t i, count;
	ulong half;

	if (argc > 4)
		count = simple_strtoul(argv[4], NULL, 16);
	else
		count = 2;

	if (argc > 5) {
		half = simple_strtoul(argv[5], NULL, 16);
		if (half != 0 && half != 1) {
			puts("Error: 'half' can only be '0' or '1'\n");
			goto usage;
		}
	} else
		half = 0;

	/* "otp lock" has slightly different semantics */
	if (base_flags & OTP_LOCK) {
		count = page;
		page = (uint32_t)addr;
		addr = NULL;
	}

	/* do to the nature of OTP, make sure users are sure */
	if (prompt_user) {
		printf(
			"Writing one time programmable memory\n"
			"Make sure your operating voltages and temperature are within spec\n"
			"   source address:  0x%p\n"
			"   OTP destination: %s page 0x%03X - %s page 0x%03lX\n"
			"   number to write: %lu halfpages\n"
			" type \"YES\" (no quotes) to confirm: ",
			addr,
			lowup(half), page,
			lowup(half + count - 1), page + (half + count - 1) / 2,
			half + count
		);

		i = 0;
		while (1) {
			if (tstc()) {
				const char exp_ans[] = "YES\r";
				char c;
				putc(c = getc());
				if (exp_ans[i++] != c) {
					printf(" Aborting\n");
					return 1;
				} else if (!exp_ans[i]) {
					puts("\n");
					break;
				}
			}
		}
	}

	printf("OTP memory %s: addr 0x%p  page 0x%03X  count %zu ... ",
		argv[1], addr, page, count);

	set_otp_timing(otp_func == bfrom_OtpWrite);
	if (otp_func == bfrom_OtpWrite && check_voltage()) {
		puts("ERROR: VDDINT voltage is out of spec for writing\n");
		return -1;
	}

	/* Do the actual reading/writing stuff */
	ret = 0;
	for (i = half; i < count + half; ++i) {
		flags = base_flags | (i % 2 ? OTP_UPPER_HALF : OTP_LOWER_HALF);
 try_again:
		ret = otp_func(page, flags, addr);
		if (ret & OTP_MASTER_ERROR) {
			if (force_read) {
				if (flags & OTP_NO_ECC)
					break;
				else
					flags |= OTP_NO_ECC;
				puts("E");
				goto try_again;
			} else
				break;
		} else if (ret)
			puts("W");
		else
			puts(".");
		if (!(base_flags & OTP_LOCK)) {
			++addr;
			if (i % 2)
				++page;
		} else
			++page;
	}
	if (ret & 0x1)
		printf("\nERROR at page 0x%03X (%s-halfpage): 0x%03X: %s\n",
			page, lowup(i), ret, otp_strerror(ret));
	else
		puts(" done\n");

	/* Make sure we disable writing */
	set_otp_timing(false);
	bfrom_OtpCommand(OTP_CLOSE, 0);

	return ret;
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: ksrm/redox
int main() {

    uint8_t i, change;
    uint8_t hand;
    uint32_t timeout = 0;

    uart_init();
    xdev_out(uart_putchar);

    // Determine which hand this is from PE2
    // Left is hand 0, right is hand 1
    PORTE = (1 << 2);
    DDRE = 0;
    hand = (PINE & 0x04) ? 0 : 1;
    xprintf("\r\nHand %d\r\n", hand);
    
    // Initialise NRF24
    // Set the last byte of the address to the hand ID
    rx_address[4] = hand;
    nrf24_init();
    nrf24_config(CHANNEL, sizeof msg);
    nrf24_tx_address(tx_address);
    nrf24_rx_address(rx_address);

    matrix_init();

    msg[0] = hand & 0x01;
    msg[1] = 0;
    msg[2] = 0;

    // Set up LED and flash it briefly
    DDRE |= 1<<6;
    PORTE = 1<<6;
    _delay_ms(500);
    PORTE = 0;

    get_voltage();
    check_voltage();

    // Scan the matrix and detect any changes.
    // Modified rows are sent to the receiver.
    while (1) {  
        timeout++;
        matrix_scan();

        for (i=0; i<ROWS; i++) {
            change = matrix_prev[i] ^ matrix[i];

            // If this row has changed, send the row number and its current state
            if (change) {
                if (DEBUG) xprintf("%d %08b -> %08b   %ld\r\n", i, matrix_prev[i], matrix[i], timeout);
                msg[1] = i;
                msg[2] = matrix[i];

                nrf24_send(msg);
                while (nrf24_isSending());
                timeout = 0;
            }

            matrix_prev[i] = matrix[i];
        }

        // Sleep if there has been no activity for a while
        if (timeout > SLEEP_TIMEOUT) {
            timeout = 0;
            enter_sleep_mode();
        }
    }

}
Exemplo n.º 5
0
int main(void) {
    wdt_enable(WDTO_4S);
    wdt_reset();
    /* configure PPM output port */
    PPM_DDR |= (1<<PPM_BIT);
    PPM_PORT &= ~(1<<PPM_BIT);

    /* configure LED output port */
    LED_DDR |= (1<<LED_BIT);
    LED_PORT |= (1<<LED_BIT);

    /* configure VOL(tage) warning port */
    VOL_DDR &= ~(1<<VOL_BIT);
    VOL_PORT |= (1<<VOL_BIT); // enable pullup

#ifdef ENABLE_SERIAL
    serial_init();
#endif
#ifdef ENABLE_TWI
    twi_init();
#endif
#ifdef USE_NUNCHUK
    nunchuk_init();
    wdt_reset();
#endif

    /* configure switches */
    sw_init();

    /* configure ADC */
    adc_init();

#if defined(USE_TWI_ADC)
    twi_adc_init();
    wdt_reset();
#endif
#if defined(USE_MAG)
    mag_init();
    wdt_reset();
#endif
#if defined(USE_ACC)
    acc_init();
#endif
#if defined(USE_LCD)
    /* initialize LCD twice (due to timing issues?) */
    lcd_init();
    wdt_reset();
    _delay_ms(100);
    wdt_reset();
    lcd_init();
    wdt_reset();
    lcd_splash();
#endif

    /* configure watchfog timer to reset after 60ms */
    wdt_enable(WDTO_60MS);

    /* configure timer */

    /* enable CTC waveform generation (TOP == OCR1A) */
    TCCR1B |= (1<<WGM12);
    /* set compare value for the stop pulse to 300µs */
    OCR1B = STOP_US;
    /* set pulse width to max for now */
    OCR1A = ~0;
    /* set Timer 1 to clk/8, giving us ticks of 1 µs */
    TCCR1B |= (1<<CS11);

    /* Timer 2 generates overflows at 1kHz */
#if defined(TCCR2) /* e.g. ATMega8 */
#define TIMER2_COMP_IRQ TIMER2_COMP_vect
    TCCR2 = (1<<WGM21 | 1<<CS22);
    OCR2 = 0x7D;
    /* enable compare and overflow interrupts */
    TIMSK = (1<<OCIE2 | 1<<OCIE1B | 1<<OCIE1A);
#elif defined(TCCR2A) /* e.g. ATMega{8,16,32}8 */
#define TIMER2_COMP_IRQ TIMER2_COMPA_vect
    TCCR2A = (1<<WGM21);
    TCCR2B = (1<<CS22);
    OCR2A = 0x7D;
    /* enable compare and overflow interrupts */
    TIMSK1 = (1<<OCIE1B | 1<<OCIE1A);
    TIMSK2 = (1<<OCIE2A);
#else
#error "Unable to determine timer 2 configuration registers"
#endif

    /* initialize channel data */
    start_ppm_frame();
    set_ppm(1);
    start_ppm_pulse();

    /* enable interrupts */
    sei();

    serial_write_str("Welcome!\n");
    while (1) {
        /* reset watchdog */
        wdt_reset();

        /* keep sampling adc data */
        adc_query();

        /* query switches */
        sw_query();

        /* prepare Datenschlag data frames */
        ds_prepare();

#ifdef USE_TWI_ADC
        /* query TWI/I²C ADC */
        twi_adc_query();
#endif
#if defined(USE_MAG)
#ifdef MAG_CENTER_CALIBRATION_TRIGGER_INPUT
        if (get_input_scaled( (MAG_CENTER_CALIBRATION_TRIGGER_INPUT), 1, -1) == (MAG_CENTER_CALIBRATION_TRIGGER_VALUE)) {
            mag_set_calibration(millis + 10000L);
        }
#endif
        if (mag_is_calibrating()) {
            mag_calibrate(0);
        } else {
            mag_calibrate(1);
            mag_query();
            mag_dump();
        }
#endif
#if defined(USE_ACC)
        acc_query();
        acc_dump();
#endif

#ifdef USE_NUNCHUK
        nunchuk_query();
#endif

        check_voltage();

        /* switch LED */
        if (!low_voltage || (millis/250 % 2)) {
            LED_PORT |= (1<<LED_BIT);
        } else {
            LED_PORT &= ~(1<<LED_BIT);
        }

#ifdef USE_LCD
        static enum {
            LCD_MODE_STATUS,
#ifdef LCD_MENU
            LCD_MODE_MENU,
#endif
            LCD_MODE_CNT
        } lcd_mode;

        uint8_t lcd_mode_changed = 0;
#ifdef LCD_MODE_SWITCH_INPUT
        static int8_t old_sw_state = 0;
        int8_t sw_state = get_input_scaled(LCD_MODE_SWITCH_INPUT, -1, 1);
        if (old_sw_state != sw_state) {
            lcd_mode += LCD_MODE_CNT;
            lcd_mode += sw_state;
            lcd_mode %= LCD_MODE_CNT;
            old_sw_state = sw_state;
            lcd_mode_changed = 1;
        }
#endif
        switch (lcd_mode) {
        case LCD_MODE_STATUS:
            lcd_status_update(lcd_mode_changed);
            break;
#ifdef LCD_MENU
        case LCD_MODE_MENU:
            lcd_menu_update(lcd_mode_changed);
            break;
#endif
        default:
            break;
        }
#endif
    }
    return 0;
}