Exemplo n.º 1
0
int main(void) {
    CPU_PRESCALE(0);

    GBA_DDR &= ~(1<<MISO_BIT);
    GBA_DDR |= (1<<MOSI_BIT) | (1<<CLK_BIT);
    CLK_HIGH();

    usb_init();
    while (!usb_configured());
    _delay_ms(1000);

    INIT_TIMER();

    while (1) {
        if (usb_serial_available() >= 4) {
            uint32_t data = 0;
            data |= (uint32_t)usb_serial_getchar()<<24;
            data |= (uint32_t)usb_serial_getchar()<<16;
            data |= (uint32_t)usb_serial_getchar()<<8;
            data |= (uint32_t)usb_serial_getchar();
            xfer(&data);
            usb_serial_putchar((data>>24) & 0xff);
            usb_serial_putchar((data>>16) & 0xff);
            usb_serial_putchar((data>>8) & 0xff);
            usb_serial_putchar(data & 0xff);
            usb_serial_flush_output();
        }
    }
Exemplo n.º 2
0
void handle_erase_block() {
	int8_t res;
	uint16_t i;
	int16_t in_data;

	for (i = 0; i < BUF_SIZE_ADDR; i++) {
		if ((in_data = usb_serial_getchar()) != -1)
			buf_addr[i] = in_data;
		else
			break;
	}
	
	if (i < BUF_SIZE_ADDR) {	// if receiving timeout, send FAIL!
		usb_serial_putchar('R');
		return;
	}
	
	res = spi_erase_block();
	if (res > 0)
		usb_serial_putchar('K');
	else if (res < 0)
		usb_serial_putchar('P');
	else
		usb_serial_putchar('V');
}
Exemplo n.º 3
0
// Receive a string from the USB serial port.  The string is stored
// in the buffer and this function will not exceed the buffer size.
// A carriage return or newline completes the string, and is not
// stored into the buffer.
// The return value is the number of characters received, or 255 if
// the virtual serial connection was closed while waiting.
//
uint8_t recv_str(char *buf, uint8_t size)
{
	int16_t r;
	uint8_t count=0;

	while (count < size) {
		r = usb_serial_getchar();
		if (r != -1) {
			if (r == '\r' || r == '\n') return count;
			if (r >= ' ' && r <= '~') {
				*buf++ = r;
				usb_serial_putchar(r);
				count++;
			}
		} else {
			if (!usb_configured() ||
			  !(usb_serial_get_control() & USB_SERIAL_DTR)) {
				// user no longer connected
				return 255;
			}
			// just a normal timeout, keep waiting
		}
	}
	return count;
}
Exemplo n.º 4
0
int8_t hwspi_write_block() {
	uint8_t i;
	uint16_t k;
	int16_t in_data;
	int8_t ret = 1;

	if (SPI_USE_3B_CMDS && SPI_ADDRESS_LENGTH == 4) {
		HWSPI_CS_LOW;
		SPI_SendByte(SPI_COMMAND_EN4B);
		HWSPI_CS_HIGH;
	}

	HWSPI_WP_HIGH;
	
	for (i = 0; i < BUF_SIZE_RW/256; ++i) {
		// set write enable bit
		HWSPI_WREN;
	
		/* Serial Data Input command */
		HWSPI_CS_LOW;
		
		if (SPI_USE_3B_CMDS || SPI_ADDRESS_LENGTH == 3)
			SPI_SendByte(SPI_COMMAND_3B_PAGEPROG);
		else
			SPI_SendByte(SPI_COMMAND_4B_PAGEPROG);

		/* address */
		if (SPI_ADDRESS_LENGTH == 4) SPI_SendByte(buf_addr[0]);
		SPI_SendByte(buf_addr[1]);
		SPI_SendByte(buf_addr[2] | i);
		SPI_SendByte(buf_addr[3]);

		for (k = 0; k < 256; ++k) {
			if ((in_data = usb_serial_getchar()) != -1) {
				SPI_SendByte(in_data);
			}
			else {
				ret = -1;
				break;
			}
		}
		
		HWSPI_CS_HIGH;
		
		// wait for the internal controller to finish the program command
		SPI_BUSY_WAIT;
		
		if (ret == -1) break;
		
		if ((maker_code == 0xC2) && (device_code0 == 0x18)) {
			if (hwspi_security() & SPI_SECURITY_P_FAIL) {
				ret = 0;
			}
		}
	}
	
	HWSPI_WP_LOW;

	return ret;
}
Exemplo n.º 5
0
void handle_write_block() {
	int8_t res;
	uint16_t i;
	int16_t in_data;
	
	for (i = 0; i < BUF_SIZE_ADDR; i++) {
		if ((in_data = usb_serial_getchar()) != -1)
			buf_addr[i] = in_data;
		else
			break;
	}
	
	if (i < BUF_SIZE_ADDR) {	// timeout
		usb_serial_putchar('R');
		return;		// and exit
	}

	res = spi_write_block();
	if (res > 0)
		usb_serial_putchar('K');
	else if (res < 0)
		usb_serial_putchar('R');
	else
		usb_serial_putchar('V');
}
Exemplo n.º 6
0
Arquivo: kitt.c Projeto: built/kitt
int main(void)
{
    configure_teensy();
    configure_LEDs();
    configure_usb();

    int16_t lastcmd = '\0', cmd = '\0';

    while (1)
    {
        wait_for_usb_connection();

        while (1)
        {
            cmd = usb_serial_getchar();

            if(is_valid_control_key(cmd))
            {
                if(cmd == '+') adjust_speed(-10); // Speed up == less time.
                else if(cmd == '-') adjust_speed(10);
            }

            if( is_valid_cmd(cmd) )
            {
                lastcmd = cmd;
            }

            if(lastcmd == 'k') knight_rider();
            else if(lastcmd == 's') step();
            else if(lastcmd == 'f') follow();
            else if(lastcmd == '0') nop();
            else demo();
        }
    }
}
Exemplo n.º 7
0
void enter_breakpoint(unsigned long line_num) {
	// Display Debug message.
	send_debug_string("Entered breakpoint @ line #. Press b to continue...");
	
	// Halt the program until the 'b' character is pressed.
	while (usb_serial_getchar() != 'b') { debug = 0; };
	send_debug_string("exited breakpoint...");
}
Exemplo n.º 8
0
static int mygetchar(FILE *stream) {
	int16_t c;
	(void)stream;
	// just poll for now, don't care about errors/timeouts
	while ((c = usb_serial_getchar()) == -1)
		;
	return c;
}
Exemplo n.º 9
0
// USB Get Character from input buffer
inline int Output_getchar()
{
#if enableVirtualSerialPort_define == 1
	// XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
	return (int)usb_serial_getchar();
#else
	return 0;
#endif
}
Exemplo n.º 10
0
// Very simple character echo test
int main(void)
{
	CPU_PRESCALE(0);
	usb_init();
	while (1) {
		int n = usb_serial_getchar();
		if (n >= 0) usb_serial_putchar(n);
	}
}
Exemplo n.º 11
0
int8_t spi_write_block() {
	return hwspi_write_block();
	
	uint16_t i;
	uint16_t k;
	int16_t in_data;
	int8_t ret = 1;
	
	//spi_enable();


	for (i = 0; i < SPI_BLOCK_SIZE/64; ++i) {
		// set write enable bit
		SPI_CS_LOW;
		SPI_COMMAND(SPI_COMMAND_WREN);
		SPI_CS_HIGH;
	
		/* Serial Data Input command */
		SPI_CS_LOW;
		SPI_COMMAND(SPI_COMMAND_4B_PAGEPROG);

		/* address */
		//buf_addr[2] = i;
		SPI_IO_SET(buf_addr[0]);
		SPI_IO_SET(buf_addr[1]);
		SPI_IO_SET(i);
		SPI_IO_SET(buf_addr[3]);

		for (k = 0; k < 256; ++k) {
			if ((in_data = usb_serial_getchar()) != -1) {
				SPI_IO_SET(in_data);
			}
			else {
				break;
			}
		}
		SPI_CS_HIGH;
		/* wait for the internal controller to finish the program command 
			TBD - up to 200us */
		SPI_BUSY_WAIT;
		if (spi_security() & SPI_SECURITY_P_FAIL) {
			ret = 0;
		}
	}

	/* Page Program confirm command */
	//SPI_COMMAND(SPI_COMMAND_PAGEPROG2);

	if (i < SPI_BLOCK_SIZE/64) { // timeout
		return -1;		// and exit
	}

	return ret; //!(spi_security() & SPI_SECURITY_P_FAIL);
}
Exemplo n.º 12
0
int getkey()
{
    while (!kbhit())
        ;
    // return the next character from the console input device
//    return usb_serial_getchar();
    if(USB_SERIAL_ON && (usb_serial_available() != 0)) {
	return usb_serial_getchar();
    }
#ifdef USE_UART
    return serial_getchar();
#endif
    return 0;
}
Exemplo n.º 13
0
/*
 * UART receive char routine MUST return exactly and only the received char;
 * it should not translate \n to \r\n.
 * This is because the interactive interface uses binary transfers.
 */
PmReturn_t
plat_getByte(uint8_t *b)
{
    PmReturn_t retval = PM_RET_OK;

    int c = usb_serial_getchar();
    if(c == -1)
    {
        PM_RAISE(retval, PM_RET_EX_IO);
        return retval;
    }
    *b = c;
    return retval;
}
Exemplo n.º 14
0
void handle_read_block() {
	uint16_t i;
	int16_t in_data;

	for (i = 0; i < BUF_SIZE_ADDR; i++) {
		if ((in_data = usb_serial_getchar()) != -1)
			buf_addr[i] = in_data;
		else
			break;
	}
	
	if (i < BUF_SIZE_ADDR) {	// timeout
		usb_serial_putchar('R');
		return;		// and exit
	}
	
	usb_serial_putchar('K');
	spi_read_block();
}
Exemplo n.º 15
0
uint8_t serial_read_line(char* buf, uint8_t max_len)
{
	int ret;
	uint8_t num_chars = 0;
	//char out_buffer[32];

	while(num_chars < max_len)
	{
		ret = usb_serial_getchar();
		if(ret == -1)
		{
			//strcpy(out_buffer,"getchar returns -1\r\n");
			//usb_serial_write(out_buffer, strlen(out_buffer));
			//yield();

		}
		else
		{
			if(ret == '\r' || ret == '\n')
			{
				// We've read the whole line so return
				//strcpy(out_buffer, "found EOL, returning\r\n");
				//usb_serial_write(out_buffer,strlen(out_buffer));

				return num_chars;
			}
			else
			{
				//strcpy(out_buffer, "Got character: ");
				//usb_serial_write(out_buffer,strlen(out_buffer));

				//usb_serial_putchar(ret);
				//usb_serial_putchar('\r');
				//usb_serial_putchar('\n');

				buf[num_chars] = ret;
				num_chars++;
			}
		}
	}

	return num_chars;
}
Exemplo n.º 16
0
/**
* Main - Run through the steps of configuring, greeting, getting a name, thanking,
* and then quitting
*/
int main(void) {
	char buff[BUFF_LENGTH];

	// Setup the hardware
	init_hardware();

	// Wait until the USB port is configured and ready to go
	draw_centred(17, "Waiting for");
	draw_centred(24, "computer...");
	show_screen();
	while (!usb_configured() || !usb_serial_get_control());

	// Prompt the user for their name, and wait until they enter it
	clear_screen();
	draw_centred(17, "Waiting for");
	draw_centred(24, "username...");
	show_screen();
	send_line("Hello!");
	send_line("Could you please tell me your name:");
	recv_line(buff, BUFF_LENGTH);
	usb_serial_putchar('\n');

	// Display their name on the Teensy and prompt them to exit
	char buff2[BUFF_LENGTH + 8];
	sprintf(buff2, "Thanks %s!", buff);
	clear_screen();
	draw_centred(21, buff2);
	show_screen();
	send_line("Press 'q' to exit...");
	while (usb_serial_getchar() != 'q');

	// Display the finished information
	clear_screen();
	draw_centred(21, "Goodbye!");
	show_screen();
	send_line("\r");
	send_line("Done! Goodbye!");
	while (1);

	// We'll never get here...
	return 0;
}
Exemplo n.º 17
0
void recv_line(char* buff, unsigned char max_length) {
    // Loop over storing characters until the buffer is full or a newline character is received
    unsigned char char_count = 0;
    int16_t curr_char;
    do {
        // BLOCK here until a character is received
        do {
            curr_char = usb_serial_getchar();
        } while (curr_char == -1);

        // Add to the buffer if it wasn't a newline (accepts other gibberish that may not necessarily be a character!)
        if (curr_char != '\n' && curr_char != '\r') {
            buff[char_count] = curr_char;
            char_count++;
        }
    } while (curr_char != '\n' && curr_char != '\r' && char_count < max_length - 1);

    // Add the null terminator to the end of the string
    buff[char_count] = '\0';
}
int16_t VncServerGetData(uint8_t * buffer, uint16_t maxsize)
{
    uint16_t size = 0;

    if(lastControlState != usb_serial_get_control())
    {
        lastControlState = usb_serial_get_control();

        if(usb_serial_get_control() & USB_SERIAL_DTR)
            usb_serial_set_control(USB_SERIAL_DSR);
        else
            usb_serial_set_control(0);

        // return error if a disconnect from the VNC server is detected
        if(lastControlState == 0)
            return -1;
    }

    while(debugcounter > 64)
    {
        DDRF |= (1 << 0);
        PORTF |= (1 << 0);
        debugcounter -= 64;
        PORTF &= ~(1 << 0);
    }

    while ( size < maxsize )
    {
        int n = usb_serial_getchar();

        if(n < 0)
            break;

        buffer[size++] = (uint8_t)n;
        debugcounter++;
    }

    return size;
}
Exemplo n.º 19
0
uint16_t usb_serial_readline(char *buffer, const uint16_t buffer_size, const bool obscure_input)
{
  uint16_t end = 0;
  char c;

  while(true) {
    if(usb_serial_available() > 0) {
      c = (char)usb_serial_getchar();
      switch((char) c) {
        case '\r': // enter
          buffer[end] = 0;
          usb_serial_putchar('\n');
          usb_serial_putchar('\r');
          return end;
          break;
        case '\b': // ^H
        case 127: // backspace
          end--;
          usb_serial_write("\b \b"); // remove the last char from the display
          //usb_serial_readline_refresh(buffer, end, obscure_input);
          break;
        default:
          if(end < buffer_size-1) {
            buffer[end++] = c;
            if(obscure_input)
              usb_serial_putchar('*');
            else
              usb_serial_putchar(c);
          }
          break;
      }
    }
    usb_tasks();
    service_button();
    _delay_ms(10);
  }
  return 0; // never reached
}
Exemplo n.º 20
0
static int
uart_getc(FILE *stream)
{
    int c = usb_serial_getchar();
    return (c == -1) ? _FDEV_ERR : c;
}
Exemplo n.º 21
0
int main(void)
{	
	char cmd;
	char param1;
	char param2;
	char param3;
	
	cmd = 0;
	param1 = 0;
	param2 = 0;
	param3 = 0;

	DDRB = 0x00;
	PORTB |= 0x01;
	
	// set for 16 MHz clock, and make sure the LED is off
	CPU_PRESCALE(0);
	LED_CONFIG;
	LED_ON;

	SPI_SlaveInit();	
	ACK_CONFIG;
	ACK_HIGH;
	
	TEST_CONFIG;
		
	usb_init();
	_delay_ms(1000);

	usb_serial_flush_input();
	int flashCounter = 0;
	char LEDstate = 1;

	while (1) {

idle:
		TEST_HIGH;
		ACK_HIGH;
		SPDR = 0xff;
		if(ATT_PIN)
		{
			timeoutCounter++;
			if(timeoutCounter >= 30000)//we've been disconnected
			{
				mode = 0x41;
				motorSetting1 = 0xff;
				motorSetting2 = 0xff;
				analogEnabled = 0;
				configMode = 0;
				timeoutCounter = 0;
				flashCounter++;
				if(flashCounter > 5)
				{
					if(LEDstate == 1)
					{
						LED_OFF;
						LEDstate = 0;
					}
					else
					{
						LED_ON;
						LEDstate = 1;	
					}
					flashCounter = 0;
					
				}
			}
			if(usb_serial_available())
			{
				LED_ON;
				int c = usb_serial_getchar();
				if (c >= 0) 
				{
					switch(parseIndex)
					{
					case 0:
						if(c == 0x5A)
							parseIndex++;
						else
							usb_serial_putchar('x');
						break;
					case 1:
						buttons1 &= c;//combine this with any pending presses
						buttons1postReportState = c;
						parseIndex++;
						break;
					case 2:
						buttons2 &= c;//combine this with any pending presses
						buttons2postReportState = c;
						parseIndex++;
						break;
					case 3:
						joyRX = c;
						parseIndex++;
						break;
					case 4:
						joyRY = c;
						parseIndex++;
						break;
					case 5:
						joyLX = c;
						parseIndex++;
						break;
					case 6:
						joyLY = c;
						parseIndex = 0;
						recievedUpdate = 1;
						break;
					}
				}
				else
				{
					parseIndex = 0;
					usb_serial_putchar('x');
				}
			}
			_delay_us(1);
			goto idle;
		}
		timeoutCounter = 0;
		
		if((buttons1 & 0x10) == 0) upPressure = 0xFF; else upPressure = 0x00;
		if((buttons1 & 0x20) == 0) rightPressure = 0xFF; else rightPressure = 0x00;
		if((buttons1 & 0x40) == 0) downPressure = 0xFF; else downPressure = 0x00;
		if((buttons1 & 0x80) == 0) leftPressure = 0xFF; else leftPressure = 0x00;
		if((buttons2 & 0x01) == 0) L2Pressure = 0xFF; else L2Pressure = 0x00;
		if((buttons2 & 0x02) == 0) R2Pressure = 0xFF; else R2Pressure = 0x00;
		if((buttons2 & 0x04) == 0) L1Pressure = 0xFF; else L1Pressure = 0x00;
		if((buttons2 & 0x08) == 0) R1Pressure = 0xFF; else R1Pressure = 0x00;
		if((buttons2 & 0x10) == 0) trianglePressure = 0xFF; else trianglePressure = 0x00;
		if((buttons2 & 0x20) == 0) circlePressure = 0xFF; else circlePressure = 0x00;
		if((buttons2 & 0x40) == 0) crossPressure = 0xFF; else crossPressure = 0x00;
		if((buttons2 & 0x80) == 0) squarePressure = 0xFF; else squarePressure = 0x00;

		cli();//disable usb interrupts
		LED_ON;
		TEST_LOW;
		cmd = SPI_SlaveReceive(0xff,1);		
		TEST_HIGH;
		
		if(cmd != 0x01)
			goto finish;
		
		if(configMode)
		{
			cmd = SPI_SlaveReceive(0xF3,1);		
			SPI_SlaveReceive(0x5A,1);
			switch(cmd)
			{
				case 0x40:
					param1 = SPI_SlaveReceive(0x00,1);
//					if(param1 == 0) //lots on param1 but the reponses are all the same?
					{
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x02,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x5A,0);
					}
					break;
				case 0x41://check response data
					if(analogEnabled==1)
					{
						//button pressures byte mask, appears after 0x44 is called
						SPI_SlaveReceive(0xFF,1);//use mask instead?
						SPI_SlaveReceive(0xFF,1);
						SPI_SlaveReceive(0x03,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x5A,0);
					}
					else
					{
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,0);
					}
					break;
				case 0x43://exit config mode
					param1 = SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,0);
					if(param1 == 0)
					{
						configMode = 0;
					}
					break;
				case 0x44://turn on analog mode
					param1 = SPI_SlaveReceive(0x00,1);
					param2 = SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,0);
					if(param1 == 1)
					{
						mode = 0x73;//no analog button pressures
						analogEnabled = 1;
					}
					else
					{
						mode = 0x41;	
						analogEnabled = 0;
					}
					break;
				case 0x45://query model
					param1 = SPI_SlaveReceive(0x03,1);
					param2 = SPI_SlaveReceive(0x02,1);
					if(analogEnabled==1)
						SPI_SlaveReceive(0x01,1);
					else
						SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x02,1);
					SPI_SlaveReceive(0x01,1);
					SPI_SlaveReceive(0x00,0);
					break;
				case 0x46:
					param1 = SPI_SlaveReceive(0x00,1);
					if(param1 == 0)
					{
						param2 = SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x01,1);
						SPI_SlaveReceive(0x02,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x0F,0);
					}
					if(param1 == 1)
					{
						param2 = SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x01,1);
						SPI_SlaveReceive(0x01,1);
						SPI_SlaveReceive(0x01,1);
						SPI_SlaveReceive(0x0F,0);
					}
					break;
				case 0x47:
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x02,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x01,1);
					SPI_SlaveReceive(0x00,0);
					break;
				case 0x4C:
					param1 = SPI_SlaveReceive(0x00,1);
					if(param1 == 0)
					{
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x04,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,0);
					}
					if(param1 == 1)
					{
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x07,1);
						SPI_SlaveReceive(0x00,1);
						SPI_SlaveReceive(0x00,0);
					}
					break;
				case 0x4D://setup motors
					motorSetting1 = SPI_SlaveReceive(motorSetting1,1);
					motorSetting2 = SPI_SlaveReceive(motorSetting2,1);
					SPI_SlaveReceive(0xFF,1);
					SPI_SlaveReceive(0xFF,1);
					SPI_SlaveReceive(0xFF,1);
					SPI_SlaveReceive(0xFF,0);
					break;
				case 0x4F://set pressure byte mask, extended pressures
					mode = 0x79;//analog button pressures
					param1 = SPI_SlaveReceive(0x00,1);
					param2 = SPI_SlaveReceive(0x00,1);
					param3 = SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x00,1);
					SPI_SlaveReceive(0x5A,0);
					break;
			}
		}
		else
		{
			TEST_LOW;
			cmd = SPI_SlaveReceive(mode,1);	
			
			SPI_SlaveReceive(0x5A,1);
			
			if(mode == 0x41)//digital only
			{
				param1 = SPI_SlaveReceive(buttons1,1);
				param2 = SPI_SlaveReceive(buttons2,0);
			}
			if(mode == 0x73)//digital buttons, analog joysticks
			{
				param1 = SPI_SlaveReceive(buttons1,1);
				if(param1 ==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(buttons2,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyRX,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyRY,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyLX,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyLY,0)==TIME_OUT_ERROR_CODE) goto finish;
			}
			if(mode == 0x79)//analog joysticks, analog buttons
			{
				param1 = SPI_SlaveReceive(buttons1,1);
				if(param1 ==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(buttons2,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyRX,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyRY,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyLX,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(joyLY,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(rightPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(leftPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(upPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(downPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(trianglePressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(circlePressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(crossPressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(squarePressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(L1Pressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(R1Pressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(L2Pressure,1)==TIME_OUT_ERROR_CODE) goto finish;
				if(SPI_SlaveReceive(R2Pressure,0)==TIME_OUT_ERROR_CODE) goto finish;
			}
			//cmd should usually be 0x42 for polling
			if(cmd == 0x43)
			{
				if(param1 == 0x01)
					configMode = 1;
			}
		}
finish:
		TEST_HIGH;
		ACK_HIGH;
		if((buttons1 == 0xff)&&(buttons2 == 0xff))
		{
			LED_OFF;
		}

		//copy queued releases into button state
		buttons1 = buttons1postReportState;
		buttons2 = buttons2postReportState;
		sei();//renable usb interrupts
		
		if(recievedUpdate == 1)
		{
			//ack the update was sent to the console
			usb_serial_putchar('k');
			recievedUpdate = 0;
		}

		while(!ATT_PIN)
		{
			_delay_us(20); //conservative so we aren't still in ATT low at the top of the loop		
		}

		
		counter++;
	}
}
Exemplo n.º 22
0
int main(void) {
	int16_t command = -1;
	uint16_t freemem;
	
	// set for 8 MHz clock because of 3.3v regulator
	CPU_PRESCALE(1);
	
	// set for 16 MHz clock
	//CPU_PRESCALE(0);

	//disable JTAG
	MCUCR = (1<<JTD) | (1<<IVCE) | (0<<PUD);
	MCUCR = (1<<JTD) | (0<<IVSEL) | (0<<IVCE) | (0<<PUD);

	// set all i/o lines to input
	releaseports();

	//Init SPI
	SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_MODE_MASTER);
	hwspi_init();
	
	// Initialize the USB, and then wait for the host to set configuration.
	// If the Teensy is powered without a PC connected to the USB port,
	// this will wait forever.
	usb_init();

	while (!usb_configured()) /* wait */ ;

	// Wait an extra second for the PC's operating system to load drivers
	// and do whatever it does to actually be ready for input
	_delay_ms(1000);

	while (1) {
		// discard anything that was received prior.  Sometimes the
		// operating system or other software will send a modem
		// "AT command", which can still be buffered.
		usb_serial_flush_input();

		while (usb_configured()) { // is user still connected?
			command = usb_serial_getchar();
			if (command == -1) continue;

			switch (command) {
			case CMD_PING1:
				usb_serial_putchar(VERSION_MAJOR);
				break;
				
			case CMD_PING2:
				freemem = freeRam();
				usb_serial_putchar(VERSION_MINOR);
				usb_serial_putchar((freemem >> 8) & 0xFF);
				usb_serial_putchar(freemem & 0xFF);
				break;
				
			case CMD_BOOTLOADER:
				bootloader();
				break;
				
			case CMD_IO_LOCK:
				break;
				
			case CMD_IO_RELEASE:
				break;
				
			case CMD_PULLUPS_DISABLE:
				IO_PULLUPS = 0;
				break;
				
			case CMD_PULLUPS_ENABLE:
				IO_PULLUPS = 0xFF;
				break;
				
			case CMD_SPI_ID:
				handle_read_id();
				break;
				
			case CMD_SPI_READBLOCK:
				handle_read_block();
				break;
				
			case CMD_SPI_WRITESECTOR:
				handle_write_block();
				break;
				
			case CMD_SPI_ERASEBLOCK:
				handle_erase_block();
				break;
				
			case CMD_SPI_ERASECHIP:
				handle_erase_chip();
				break;
				
			case CMD_SPI_3BYTE_ADDRESS:
				SPI_ADDRESS_LENGTH = 3;
				break;
			
			case CMD_SPI_4BYTE_ADDRESS:
				SPI_ADDRESS_LENGTH = 4;
				break;
			
			case CMD_SPI_3BYTE_CMDS:
				SPI_USE_3B_CMDS = 1;
				break;
			
			case CMD_SPI_4BYTE_CMDS:
				SPI_USE_3B_CMDS = 0;
				break;
			
			default:
				break;
			}
		}		
	}
}
Exemplo n.º 23
0
//void parse_CIM_protocol(unsigned char actbyte)
void parse_CIM_protocol(void)
{
    uint32_t checksum=0;
    static uint8_t transmission_mode;
	static uint8_t reply_status_code;
	static uint8_t last_serial;
	uint8_t actbyte;

	

    while (usb_serial_available()) 
	{ //PORTD |= (1<<6);
      actbyte=usb_serial_getchar();
		

      switch (readstate) 
	  {
		  case 0: // first sync byte
		  		  reply_status_code=0;
		  		  if (actbyte=='@') readstate++; 
				  break;
		  case 1: // second sync byte
		  		  if (actbyte=='T')  readstate++;
				  else readstate=0;
				  break;

 		  // packet in sync !

		  case 2: // ARE-ID: SW-version low byte
				  ARE_frame.are_id=actbyte;  
		  		  readstate++; 
				  break;
		  case 3: // ARE-ID: SW-version high byte
		  		  ARE_frame.are_id+=((uint16_t)actbyte)<<8;  
				  if (ARE_frame.are_id < ARE_MINIMAL_VERSION)    // outdated ARE ?
				     reply_status_code |= CIM_ERROR_INVALID_ARE_VERSION;
		  		  readstate++; 
				  break;
		  case 4: // data length low byte
		  		  ARE_frame.data_size=actbyte;
				  readstate++; 
				  break;
		  case 5: // data length high byte
		  		  ARE_frame.data_size+=((uint16_t)actbyte)<<8;
				  if (ARE_frame.data_size > DATABUF_SIZE)  { // dismiss packets of excessive length
				       readstate=0;
					   //ARE_frame.data_size=0;
  				       //reply_status_code |= CIM_ERROR_INVALID_FEATURE;
				  }
				  else readstate++; 
				 
				  break;
	      case 6: // serial_number 
		  		  ARE_frame.serial_number = actbyte;
				  if (first_packet)    // don't check first serial
				      first_packet=0;
				  else if (actbyte != (last_serial+1)%0x80) // check current serial number
				     reply_status_code |= CIM_ERROR_LOST_PACKETS;

				  last_serial=actbyte;
			      readstate++;
				  break;
		  case 7: // CIM-feature low byte
		  		  ARE_frame.cim_feature= actbyte; 
		  		  readstate++; 
				  
				  break;
		  case 8: // CIM-feature high byte
		  		  ARE_frame.cim_feature+=((int)actbyte)<<8; 
		  		  readstate++; 

				  break;
		  case 9: // Request code low byte ( command )
		  		  ARE_frame.request_code=actbyte;
		  		  readstate++; 
				  break;
		  case 10:// Request code high byte (transmission mode)
		  		  transmission_mode=actbyte;  // bit 0: CRC enable
				  // reply_status_code|=(actbyte & CIM_STATUS_CRC);   
				  // remember CRC state for reply
				  
		  		  if (ARE_frame.data_size>0) {
				  	readstate++;
					datapos=0;
				  }
				  else {  // no data in packet 
				    if (transmission_mode & CIM_STATUS_CRC) 
					   readstate+=2;      // proceed with CRC 
					else readstate=FRAME_DONE;  // frame is finished here !
				  }
				  break;

		  case 11: // read out data
		  		  ARE_frame.data[datapos]=actbyte;
				  datapos++;
				  if (datapos==ARE_frame.data_size)
				  {
				     if (transmission_mode & CIM_STATUS_CRC)  // with CRC: get checksum 
 				        readstate++;  					 
					 else  readstate=FRAME_DONE; // no CRC:  frame is finished here !
				  } 
				  break;
		  case 12: // checksum byte 1
		  		  checksum=actbyte;
				  readstate++;
				  break;
		  case 13: // checksum byte 2
		  		  checksum+=((long)actbyte)<<8;
				  readstate++;
				  break;
		  case 14: // checksum byte 3
		  		  checksum+=((long)actbyte)<<16;
				  readstate++;
				  break;
		  case 15: // checksum byte 4
		  		  checksum+=((long)actbyte)<<24;	  

				  // check CRC now (currently not used):
				  // if (checksum != crc32(ARE_frame.data, ARE_frame.data_size))
				  reply_status_code |= CIM_ERROR_CRC_MISMATCH;

				  // frame finished here !  
 				  readstate=FRAME_DONE;
				  break;
		  default: readstate=0; break;
	 }		

     if (readstate==FRAME_DONE)  {  // frame finished: store command in ringbuffer
			process_ARE_frame(reply_status_code);
			readstate=0;
		}
   }
}
Exemplo n.º 24
0
/*!	\fn 	main(void)
*	\brief	Main function
*/
int main(void)
{
    RET_TYPE flash_init_result = RETURN_NOK;
    RET_TYPE card_detect_ret;
    RET_TYPE temp_rettype;

    uint8_t temp_buffer[200];
    char temp_string[20];

    CPU_PRESCALE(0);					// Set for 16MHz clock
    _delay_ms(500);						// Let the power settle
    initPortSMC();						// Init smart card Port
    initIRQ();							// Init interrupts
    usb_init();							// Init USB controller
    while(!usb_configured());			// Wait for host to set configuration
    initOLED();							// Init OLED screen after enum
    flash_init_result = initFlash();	// Init flash memory

//#define TEST_HID_AND_CDC
#ifdef TEST_HID_AND_CDC
    //Show_String("Z",FALSE,2,0);
    //usb_keyboard_press(KEY_S, 0);
    while(1)
    {
        int n = usb_serial_getchar();
        if (n >= 0)
        {
            usb_serial_putchar(n);
            Show_String((char*)&n,FALSE,2,0);
            //usb_keyboard_press(n,0);
        }

    }
#endif /* TEST_HID_AND_CDC */

    //lcd_display_grayscale();
    if(flash_init_result == RETURN_OK)
        Show_String("Flash init ok", FALSE, 2, 0);
    else
        Show_String("Problem flash init", FALSE, 2, 250);

    //display_picture(HACKADAY_BMP, 20, 0);
    //draw_screen_frame();
    //Show_String("Mooltipass", FALSE, 32, 10);
    Show_String("No card detected", FALSE, 25, 45);	//If no card is inserted at boot time

    while(1)
    {
        card_detect_ret = isCardPlugged();
        if(card_detect_ret == RETURN_JDETECT)							// Card just detected
        {
            temp_rettype = cardDetectedRoutine();

            if(temp_rettype == RETURN_MOOLTIPASS_INVALID)				// Invalid card
            {
                _delay_ms(3000);
                printSMCDebugInfoToScreen();
                removeFunctionSMC();									// Shut down card reader
            }
            else if(temp_rettype == RETURN_MOOLTIPASS_PB)				// Problem with card
            {
                _delay_ms(3000);
                printSMCDebugInfoToScreen();
                removeFunctionSMC();									// Shut down card reader
            }
            else if(temp_rettype == RETURN_MOOLTIPASS_BLOCKED)			// Card blocked
            {
                _delay_ms(3000);
                printSMCDebugInfoToScreen();
                removeFunctionSMC();									// Shut down card reader
            }
            else if(temp_rettype == RETURN_MOOLTIPASS_BLANK)			// Blank mooltipass card
            {
                // Here we should ask the user to setup his mooltipass card
                _delay_ms(3000);
                printSMCDebugInfoToScreen();
                removeFunctionSMC();									// Shut down card reader
            }
            else if(temp_rettype == RETURN_MOOLTIPASS_USER)				// Configured mooltipass card
            {
                // Here we should ask the user for his pin and call mooltipassdetect
                _delay_ms(3000);
                printSMCDebugInfoToScreen();
                removeFunctionSMC();									// Shut down card reader
            }
            /*read_credential_block_within_flash_page(2,1,temp_buffer);
            for(i = 0; i < 10; i++)
            {
            	hexachar_to_string(temp_buffer[i], temp_string);
            	Show_String(temp_string, FALSE, 2+i*5, 0);
            }
            temp_buffer[3] = 0x0A;
            write_credential_block_within_flash_page(2,1, temp_buffer);
            read_credential_block_within_flash_page(2,1,temp_buffer);
            for(i = 0; i < 10; i++)
            {
            	hexachar_to_string(temp_buffer[i], temp_string);
            	Show_String(temp_string, FALSE, 2+i*5, 8);
            }*/
        }
        else if(card_detect_ret == RETURN_JRELEASED)	//card just released
        {
            removeFunctionSMC();
            clear_screen();
            Show_String("Please insert card", FALSE, 2, 8);
        }
    }
}
Exemplo n.º 25
0
/*! \fn     afterFlashInitTests(void)
*   \brief  Test functions launched after flash init
*/
void afterFlashInitTests(void)
{    
    //#define TEST_NODE
    #ifdef TEST_NODE
        nodeTest();
        // spin
        while(1);
    #endif
    //#define TEST_HID_AND_CDC
    #ifdef TEST_HID_AND_CDC
        //Show_String("Z",FALSE,2,0);
        //usbKeyboardPress(KEY_S, 0);
        while(1)
        {
            int n = usb_serial_getchar();
            if (n >= 0)
            {
                usb_serial_putchar(n);
                oledSetXY(2,0);
                oledPutch((char)n);

                //usbKeyboardPress(n,0);
            }

        }
    #endif /* TEST_HID_AND_CDC */

    //#define NESSIE_TEST_VECTORS
    #ifdef NESSIE_TEST_VECTORS
        while(1)
        {
            // msg into oled display
            oledSetXY(2,0);
            printf_P(PSTR("send s to start nessie test"));

            int input0 = usb_serial_getchar();

            nessieOutput = &usb_serial_putchar;

            // do nessie test after sending s or S chars
            if (input0 == 's' || input0 == 'S')
            {
                nessieTest(1);
                nessieTest(2);
                nessieTest(3);
                nessieTest(4);
                nessieTest(5);
                nessieTest(6);
                nessieTest(7);
                nessieTest(8);
            }
        }
    #endif
    
    //#define CTR_TEST_VECTORS
    #ifdef CTR_TEST_VECTORS
        while(1)
        {
            // msg into oled display
            oledSetXY(2,0);
            printf_P(PSTR("send s to start CTR test"));

            int input1 = usb_serial_getchar();

            ctrTestOutput = &usb_serial_putchar;

            // do ctr test after sending s or S chars
            if (input1 == 's' || input1 == 'S')
            {
                aes256CtrTest();
            }
        }
    #endif

	//#define TEST_CTR_SPEED
	#ifdef TEST_CTR_SPEED
		// msg into oled display
		oledSetXY(2,0);
		usbPrintf_P(PSTR("CTR speed TEST with 1000 encryptions\n"));
		usbPrintf_P(PSTR("Time:"));
		usbPrintf_P(PSTR("%lu ms"), aes256CtrSpeedTest());
		while(1);
	#endif

    //#define TEST_RNG
    #ifdef TEST_RNG 
        while(1)
        {
            // init avrentropy library
            EntropyInit();

            // msg into oled display
            oledSetXY(2,0);
            printf_P(PSTR("send s to start entropy"));

            int input2 = usb_serial_getchar();

            uint32_t randomNumCtr;

            // do nessie test after sending s or S chars
            if (input2 == 's' || input2 == 'S')
            {
                while(EntropyAvailable() < 2);
                
                EntropyRandom8();

                usb_serial_putchar(EntropyBytesAvailable());

                for(randomNumCtr=0; randomNumCtr<25; randomNumCtr++)
                {
                        usb_serial_putchar(EntropyRandom8());
                }
            }

        }
    #endif    
}
Exemplo n.º 26
0
/** The MAIN loop. */
int main(void) {
  // Turn off the CPU prescale.
  CLKPR = 0x80;
  CLKPR = 0x00;

  // PORTA are general purpose inputs.
  DDRA = 0x00;
  PORTA = 0xff; // pull-ups all enabled

  PORTD = 0xff;
  DDRD = 0x00; // pull-ups all enabled

  DDRF = 0x00; // These are ADC lines.
  PORTF = 0x00;

  usb_init();

  // Set up Timer 0 to match compare every 1ms.
  OCR0A = 250;
  TCCR0A = 0x02; // CTC
  TCCR0B = 0x03; // CK/64 (64 * 250 == 16000)

  sei();
  char line_buf[128] = {};
  uint8_t line_len = 0;
  uint8_t usb_ready = 0;
  while (1) {
    wdt_reset();
    g_main_loop_count++;

    if (usb_configured() && (usb_serial_get_control() & USB_SERIAL_DTR)) {
      if (!usb_ready) {
        usb_ready = 1;
        strcpy_P(line_buf, PSTR("!GNR WELCOME " DEV_VERSION EOL));
        usb_serial_write((uint8_t*)line_buf, strlen(line_buf));
        line_len = 0;
      }
    } else {
      stream_stop_all();
      usb_serial_flush_input();
      usb_ready = 0;
      line_len = 0;
      g_arm_code = 0;
    }

    if (usb_serial_available()) {
      int16_t c = usb_serial_getchar();
      if (c == '\r' || c == '\n') {
        line_buf[line_len] = 0;
        handle_line(line_buf);
        if (g_arm_code) { g_arm_timer = ARM_TIMEOUT_MS; }
        line_len = 0;
      } else {
        line_buf[line_len++] = c & 0xff;

        if ((line_len + 1) >= sizeof(line_buf)) {
          /* Clobber the first byte so that this line will be reported
             as an error. */
          line_buf[0] = MAGIC_OVERRUN_CODE;
          line_len--;
        }
      }
    }

    if (TIFR0 & (1 << OCF0A)) {
      TIFR0 |= (1 << OCF0A);
      g_timer++;
      stream_timer_update();
      hit_timer_update();

      g_main_loop_count = (uint16_t) (((uint32_t) g_main_loop_count) * 7 / 8);
    }

    stream_poll();
    usb_poll();
    hit_poll();
  }
}
Exemplo n.º 27
0
// USB Get Character from input buffer
inline int Output_getchar()
{
	// XXX Make sure to check output_availablechar() first! Information is lost with the cast (error codes) (AVR)
	return (int)usb_serial_getchar();
}
Exemplo n.º 28
0
/**
 * Main - Run the game which repeatedly loops through trying to find the gold
 */
int main() {
    // Setup the hardware
    set_clock_speed(CPU_8MHz);
    init_hardware();

    // Wait until the 'player' is attached...
    draw_centred(17, "Waiting for");
    draw_centred(24, "the player...");
    show_screen();
    while(!usb_configured() || !usb_serial_get_control());

    // Run the main game loop
    unsigned int seed = 0;
    while (1) {
        // Game start screen
        clear_screen();
        draw_centred(16, "--- LUCKY DIP ---");
        draw_centred(25, "'s' to start...");
        show_screen();
        send_line("--- LUCKY DIP ---");
        send_line("Press 's' to start...");

        // Wait until the key has been pressed (perform seeding only if first run)
        unsigned int seed_temp = 0;
        int16_t curr_char;
        do {
            curr_char = usb_serial_getchar();
            seed_temp++;
        } while (curr_char != 's');
        if (seed == 0) {
            seed = seed_temp;
            srand(seed);
        }
        usb_serial_write("\r\n", 2);

        // Set the gold location
        bury_gold();

        // Present the 9 closed boxes
        clear_screen();
        for (unsigned char i = 0; i<BOXES_W*BOXES_H; i++) {
            draw_box(i, 0);
        }
        show_screen();

        // Prompt the user to pick a box by pressing a number key until they find the gold
		send_line("Please enter a number between 1 and 9 to select a box...");
		int dinner = 0;
		while (!dinner) {
			// Get the users input.			
			int16_t input = usb_serial_getchar();
			
			// Convert the input to an intager to be used as an id.
			int box = (input == '1') ? 0 : (input == '2') ? 1 : (input == '3') ? 2 : (input == '4') ? 3 : (input == '5') ? 4 : (input == '6') ? 5 : (input == '7') ? 6 : (input == '8') ? 7 : (input == '9') ? 8 : -1;
			
			if (box != -1) {
				// Debugging - Display selected box (once it's converted to an id from the users input).
				char buff[20];
				sprintf(buff, "User has selected box #%d", box);
				(debug) ? send_debug_string(buff) : 0;
				
				// Is the selected box open? I should probably open it is it's not...
				if (is_open[box] == 1) {
					send_line("  - That box is already open!");
				} else {
					is_open[box] = 1;
					draw_box(box, 1);
				}
				
				// Did that box contain gold!?
				if (is_gold[box] == 1) {
					show_screen();
					dinner = 1;
				}
			}
			show_screen();
		}

        // Winner, winner, chicken dinner
        send_line("You found the $$$!");
        for (unsigned char i = 0; i<10; i++) {
            PORTB ^= (1 << PB2);
            PORTB ^= (1 << PB3);
            _delay_ms(250);
        }
    }

    // We'll never get here...
    return 0;
}