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'); }
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'); }
void handle_erase_chip() { int8_t res; res = spi_erase_chip(); if (res > 0) usb_serial_putchar('K'); else if (res < 0) usb_serial_putchar('P'); else usb_serial_putchar('V'); }
void handle_read_id() { if (spi_read_id()) { usb_serial_putchar(maker_code); usb_serial_putchar(device_code0); //usb_serial_putchar(device_code1); } else { usb_serial_putchar(0); //maker_code usb_serial_putchar(0); //device_code0 //usb_serial_putchar(0); //device_code1 } }
void send_line(char* string) { // Send all of the characters in the string unsigned char char_count = 0; while (*string != '\0') { usb_serial_putchar(*string); string++; char_count++; } // Go to a new line (force this to be the start of the line) usb_serial_putchar('\r'); usb_serial_putchar('\n'); }
/** * Closes the damper shut. */ void CloseDamper( uint8_t damper) { if(DamperVerbose) { send_str(PSTR("\r\nDamper: ")); usb_serial_putchar((damper + '0')); send_str(PSTR("\r\nEnter CloseDamper Section \r\n")); } ActivateDamper(damper); _delay_ms(500); //step the device close uint8_t DamperStatus = CheckDamper(damper); // if already close, return if(DamperStatus == 1) { return; } unsigned int timeout = 0; // make sure the motor doesn't spin MotorDirection1LOW;// go in opposite direction //MotorDirection2HIGH; // set a timeout to 5 seconds while(DamperStatus != 1 || timeout == 50) { // step a lot MotorStepHIGH; DamperStatus = CheckDamper(damper); _delay_ms(100); MotorStepLOW; timeout++; if(DamperVerbose) { send_str(PSTR("ModTimer: ")); usb_serial_putchar((timeout / '0')); send_str(PSTR("\r\n")); } } if(timeout == 10000) { // report error to main Could be the wrong board that // we are trying to access. if(DamperVerbose) { send_str(PSTR(" Damper Close Error:TIMEOUT\r\n")); } } }
void send_debug_string(char* string) { // Send the debug preamble... usb_serial_write(" - [DEBUG] ", 11); // Send all of the characters in the string unsigned char char_count = 0; while (*string != '\0') { usb_serial_putchar(*string); string++; char_count++; } // Go to a new line (force this to be the start of the line) usb_serial_putchar('\r'); usb_serial_putchar('\n'); }
void printHex(char c) { char n = (c >>4); if(n < 10) n += 48; else n += 55; usb_serial_putchar(n); n = (c & 0xf); if(n < 10) n += 48; else n += 55; usb_serial_putchar(n); }
void usb_serial_readline_refresh(const char *buffer, const uint16_t n, const bool obscure_input) { uint16_t i; // delete the previous stuff on the line (plus one extra space) usb_serial_putchar('\r'); for(i=0; i<=n; i++) usb_serial_putchar(' '); // write out the buffer contents usb_serial_putchar('\r'); for(i=0; i<n; i++) if(obscure_input) usb_serial_putchar('*'); else usb_serial_putchar(buffer[i]); }
// 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; }
/** * Function implementations */ void send_debug_string(char* string) { // Send the debug preamble... char buff[BUFF_LENGTH]; sprintf(buff, "[DEBUG @ %06.2f] ", get_system_time()); usb_serial_write(buff, 18); // Send all of the characters in the string unsigned char char_count = 0; while (*string != '\0') { usb_serial_putchar(*string); string++; char_count++; } // Go to a new line (force this to be the start of the line) usb_serial_putchar('\r'); usb_serial_putchar('\n'); }
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(); } }
// USB Send Character to output buffer inline int Output_putchar( char c ) { #if enableVirtualSerialPort_define == 1 return usb_serial_putchar( c ); #else return 0; #endif }
// Send a string to the USB serial port. The string must be in // flash memory, using PSTR // void send_str(const char *s) { char c; while (1) { c = pgm_read_byte(s++); if (!c) break; usb_serial_putchar(c); } }
void app_debug_putchar(const char arg) { #ifdef EMBEDDED usb_serial_putchar(arg); #else // NOT EMBEDDED printf("%c", arg); #endif }
void tx(char c) { if (USB_SERIAL_ON) { usb_serial_putchar(c); } #ifdef USE_UART serial_putchar(c); #endif }
// 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); } }
void tx(char c) { while(!(UART0_S1 & UART_S1_TDRE)) // pause until transmit data register empty ; UART0_D = c; if (seen_usb) { usb_serial_putchar(c); sent_usb++; } }
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(); }
uint16_t VncServerSendResponse(uint8_t * buffer, uint16_t length) { int i; for(i=0; i<length; i++) { usb_serial_putchar(buffer[i]); } return i; }
// Send a string to the USB serial port. // The string must be in program memory memory. int8_t usb_serial_puts_P(const char *string) { char c; while((c=pgm_read_byte(string++))!=0) { int8_t r=usb_serial_putchar(c); if(r) return r; } return 0; }
// Send a string to the USB serial port. int8_t usb_serial_puts(const char *string) { char c; while((c=*string++)!=0) { int8_t r=usb_serial_putchar(c); if(r) return r; } return 0; }
// parse a user command and execute it, or print an error message // void parse_and_execute_command(const char *buf, uint8_t num) { uint8_t port, pin, val; if (num < 3) { return; } // first character is the port letter if (buf[0] >= 'A' && buf[0] <= 'F') { port = buf[0] - 'A'; } else if (buf[0] >= 'a' && buf[0] <= 'f') { port = buf[0] - 'a'; } else { return; } // second character is the pin number if (buf[1] >= '0' && buf[1] <= '7') { pin = buf[1] - '0'; } else { return; } // if the third character is a question mark, read the pin if (buf[2] == '?') { // make the pin an input *(uint8_t *)(0x21 + port * 3) &= ~(1 << pin); // drive it high for high impedance *(uint8_t *)(0x22 + port * 3) |= (1 << pin); // read the pin val = *(uint8_t *)(0x20 + port * 3) & (1 << pin); usb_serial_putchar(val ? '1' : '0'); send_str(PSTR("\r\n")); return; } // if the third character is an equals sign, write the pin if (num >= 4 && buf[2] == '=') { if (buf[3] == '0') { // make the pin an output *(uint8_t *)(0x21 + port * 3) |= (1 << pin); // drive it low *(uint8_t *)(0x22 + port * 3) &= ~(1 << pin); return; } else if (buf[3] == '1') { // make the pin an output *(uint8_t *)(0x21 + port * 3) |= (1 << pin); // drive it high *(uint8_t *)(0x22 + port * 3) |= (1 << pin); return; } else { return; } } }
void send_str(const char *s) { char c; while (1) { c = *s++; // Take the char. if (c == '\0') // Exit on '\0' byte. break; usb_serial_putchar(c); } usb_serial_flush_output(); }
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 }
/* * UART send char routine MUST send exactly and only the given char; * it should not translate \n to \r\n. * This is because the interactive interface uses binary transfers. */ PmReturn_t plat_putByte(uint8_t b) { PmReturn_t retval = PM_RET_OK; if(usb_serial_putchar(b) == 0) { return retval; } else { PM_RAISE(retval, PM_RET_EX_IO); return retval; } }
/** * this activates the correct damper * Then while checking the Damper's status * runs the stepper motor. */ void OpenDamper( uint8_t damper) { if(DamperVerbose) { send_str(PSTR("\r\nDamper: ")); usb_serial_putchar((damper + '0')); send_str(PSTR("\r\nEnter OpenDamper Section \r\n")); } //set the Damper Demux correctly ActivateDamper(damper); // for(int i; i < 10000; i++);// wait for signal propagation 0.5 seconds is _delay_ms(100); //step the device open uint8_t DamperStatus = CheckDamper(damper); // if already open, return if(DamperStatus == 0) { return; } // if not run the motor MotorDirection1HIGH; // MotorDirection2LOW; // current setting for A4988 // set a timeout unsigned int timeout = 0; // make sure the motor doesn't spin forever // timeout is 5 seconds is 100ms * 50 = 5 seconds while(DamperStatus != 0 || timeout > 50) { // step a lot// need a pause MotorStepHIGH; DamperStatus = CheckDamper(damper); // add ~500 milisec plenty of time for stepper motor not to crush everything _delay_ms(100); MotorStepLOW; timeout++; } if(timeout > 50) { // report error to main Could be the wrong board that // we are trying to access. if(DamperVerbose) { send_str(PSTR(" Damper Open Error:TIMEOUT\r\n")); } } }
void GPS_print() { // Print the UART output straight to the terminal screen char receive; while(1){ // For all time receive = UART_Receive(); if (receive > 0){ usb_serial_putchar(receive); // Send it back to the computer to print in the console } } }
int main(void) { Randomizer randomizer; // set for 16 MHz clock, and turn on the LED CPU_PRESCALE(0); LED_CONFIG; LED_ON; // 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 */ ; _delay_ms(1000); while (1) { // wait for the user to run their terminal emulator program // which sets DTR to indicate it is ready to receive. while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ; // 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(); //Get 128 bits of debiased entropy from the ADCs (using Von Neumann debias) //and add that entropy to the randomizer (cryptographic entropy mixer). //Repeat 4 times. for(int i=0; i<4; i++){ randomizer.add(Entropy::get_entropy()); } //Get cryptographically mixed and decorrelated random data from the randomizer RandomData random_data = randomizer.get(); for(int i=0; i<16; i++){ //Send the random data over the USB serial device usb_serial_putchar(random_data.bytes[i]); } LED_TOGGLE; } }
/** * 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; }