void reply_FeatureList(void) { if (UEINTX & (1<<RWAL)) { //wenn Buffer nicht voll ist CIM_frame.reply_code |= (CIM_ERROR_CIM_NOT_READY<<8) ; // LEDs_TurnOnLEDs(LED6); // indicate frame loss } if (!(UEINTX & (1<<RWAL))) { //wenn Buffer voll ist CIM_frame.data_size=sizeof(TEENSY_CIM_FEATURELIST); // feature list length usb_serial_write ( (uint8_t *)&CIM_frame, CIM_HEADER_LEN); usb_serial_write ( (uint8_t *)&TEENSY_CIM_FEATURELIST, CIM_frame.data_size); } }
uint8_t spi_read_block() { return hwspi_read_block(); uint16_t i; uint8_t sreg; /* Save global interrupt flag and disable interrupts */ sreg = SREG; cli(); //spi_enable(); /* read command */ //SPI_CS_LOW; //SPI_COMMAND(SPI_COMMAND_EN4B); //SPI_CS_HIGH; SPI_CS_LOW; SPI_COMMAND(SPI_COMMAND_4B_READ); /* address */ SPI_IO_SET(buf_addr[0]); SPI_IO_SET(buf_addr[1]); SPI_IO_SET(buf_addr[2]); SPI_IO_SET(buf_addr[3]); //SPI_IO_INPUT; /* wait for the nand to read this page to the internal page register */ //wait_ryby(); for (uint8_t k = 0; k < SPI_BLOCK_SIZE / BUF_SIZE_RW; ++k) { for (i = 0; i < BUF_SIZE_RW; ++i) { SPI_IO_READ(buf_rw[i]); } usb_serial_write(buf_rw, BUF_SIZE_RW); } uint16_t rest = SPI_BLOCK_SIZE - ((SPI_BLOCK_SIZE / BUF_SIZE_RW) * BUF_SIZE_RW); for (i = 0; i < rest; ++i) { SPI_IO_READ(buf_rw[i]); } SPI_CS_HIGH; usb_serial_write(buf_rw, rest); /* Restore global interrupt flag */ SREG = sreg; return 1; }
uint8_t hwspi_read_block() { uint16_t i; uint8_t sreg; if (SPI_USE_3B_CMDS && SPI_ADDRESS_LENGTH == 4) { HWSPI_CS_LOW; SPI_SendByte(SPI_COMMAND_EN4B); HWSPI_CS_HIGH; } /* Save global interrupt flag and disable interrupts */ sreg = SREG; cli(); HWSPI_CS_LOW; if (SPI_USE_3B_CMDS || SPI_ADDRESS_LENGTH == 3) SPI_SendByte(SPI_COMMAND_3B_READ); else SPI_SendByte(SPI_COMMAND_4B_READ); /* address */ if (SPI_ADDRESS_LENGTH == 4) SPI_SendByte(buf_addr[0]); SPI_SendByte(buf_addr[1]); SPI_SendByte(buf_addr[2]); SPI_SendByte(buf_addr[3]); for (uint8_t k = 0; k < SPI_BLOCK_SIZE / BUF_SIZE_RW; ++k) { for (i = 0; i < BUF_SIZE_RW; ++i) { buf_rw[i] = SPI_ReceiveByte(); } usb_serial_write(buf_rw, BUF_SIZE_RW); } uint16_t rest = SPI_BLOCK_SIZE - ((SPI_BLOCK_SIZE / BUF_SIZE_RW) * BUF_SIZE_RW); for (i = 0; i < rest; ++i) { buf_rw[i] = SPI_ReceiveByte(); } HWSPI_CS_HIGH; usb_serial_write(buf_rw, rest); /* Restore global interrupt flag */ SREG = sreg; return 1; }
void reply_UniqueNumber(void) { CIM_frame.data_size=4; // lenght of unique number usb_serial_write ((uint8_t *) &CIM_frame, CIM_HEADER_LEN); usb_serial_write ((uint8_t *) &TEENSY_CIM_UNIQUE_NUMBER, CIM_frame.data_size); /* if (UEINTX & (1<<RWAL)) { //wenn Buffer nicht voll ist CIM_frame.reply_code |= (CIM_ERROR_CIM_NOT_READY<<8); // LEDs_TurnOnLEDs(LED6); // indicate frame loss } if (!(UEINTX & (1<<RWAL))) { //wenn Buffer voll ist CIM_frame.data_size=4; // lenght of unique number usb_serial_write ((char *) &CIM_frame, CIM_HEADER_LEN); usb_serial_write ((char *) &TEENSY_CIM_UNIQUE_NUMBER, CIM_frame.data_size); } */ }
void reply_DataFrame(void) { usb_serial_write ((uint8_t *) &CIM_frame, CIM_HEADER_LEN); usb_serial_write ((uint8_t *) CIM_frame.data, CIM_frame.data_size); /*if ((UEINTX & (1<<RWAL)) < CIM_HEADER_LEN+CIM_frame.data_size) { CIM_frame.reply_code |= (CIM_ERROR_CIM_NOT_READY<<8) ; // LEDs_TurnOnLEDs(LED6); // indicate frame loss } else { usb_serial_write ((char *) &CIM_frame, CIM_HEADER_LEN); usb_serial_write ((char *) CIM_frame.data, CIM_frame.data_size); }*/ }
void reply_Acknowledge(void) { if ((UEINTX & (1<<RWAL))) { //wenn Buffer nicht voll ist CIM_frame.reply_code |= (CIM_ERROR_CIM_NOT_READY<<8) ; } if (!(UEINTX & (1<<RWAL))) { //wenn Buffer voll ist CIM_frame.data_size=0; // no data in ack frame usb_serial_write ((uint8_t *) &CIM_frame, CIM_HEADER_LEN); } }
// USB Send String to output buffer, null terminated inline int Output_putstr( char* str ) { #if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR uint16_t count = 0; #elif defined(_mk20dx128_) || defined(_mk20dx128vlf5_) || defined(_mk20dx256_) || defined(_mk20dx256vlh7_) // ARM uint32_t count = 0; #endif // Count characters until NULL character, then send the amount counted while ( str[count] != '\0' ) count++; return usb_serial_write( str, count ); }
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'); }
int main(){ PORTC_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); GPIOC_PDDR |= 1<<5; int i, j; while(1){ usb_serial_write(message,12); for(i = 0; i < 48000000; i++){ for(j = 0; j <5; j++){ GPIOC_PSOR = 1<<5; } } } }
/** * 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'); }
// transmit a character. 0 returned on success, -1 on error int usb_serial_putchar(uint8_t c) { #if 1 return usb_serial_write(&c, 1); #endif #if 0 uint32_t wait_count; tx_noautoflush = 1; if (!tx_packet) { wait_count = 0; while (1) { if (!usb_configuration) { tx_noautoflush = 0; return -1; } if (usb_tx_packet_count(CDC_TX_ENDPOINT) < TX_PACKET_LIMIT) { tx_noautoflush = 1; tx_packet = usb_malloc(); if (tx_packet) break; tx_noautoflush = 0; } if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) { transmit_previous_timeout = 1; return -1; } } } transmit_previous_timeout = 0; tx_packet->buf[tx_packet->index++] = c; if (tx_packet->index < CDC_TX_SIZE) { usb_cdc_transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT; } else { tx_packet->len = CDC_TX_SIZE; usb_cdc_transmit_flush_timer = 0; usb_tx(CDC_TX_ENDPOINT, tx_packet); tx_packet = NULL; } tx_noautoflush = 0; return 0; #endif }
// Basic command interpreter for controlling port pins int main(void) { int16_t adc_result; char adc_result_str[10]; // set for 16 MHz clock, and turn on the LED CPU_PRESCALE(0); LED_CONFIG; // Set PF0 as input for ADC DDRF &= ~(1 << 0); // 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(); adc_result = adc_read(0); // Read ADC pin 0 sprintf(adc_result_str, "ADC: %d ", adc_result); usb_serial_write(adc_result_str, 9); _delay_ms(100); } }
// Run at most one waiting stream operation. static void stream_poll(void) { char output[64]; output[0] = 0; for (uint8_t i = 0; i < NUM_STREAM_TABLE; i++) { if (!g_stream_table[i].stream_id) { continue; } if (g_stream_table[i].queued_runs) { g_stream_table[i].queued_runs--; const struct fw_command_struct* fw_command = &fw_command_table[g_stream_table[i].cmd_index]; cmd_func_ptr cmd_func = (cmd_func_ptr) pgm_read_word(&fw_command->func); char* ptr = output; *ptr++ = '!'; strcpy_P(ptr, PSTR("STM ")); ptr += 4; uint8_to_hex(&ptr, g_stream_table[i].stream_id); *ptr++ = ' '; strncpy_P(ptr, fw_command->code, 3); ptr += 3; *ptr = 0; uint8_t err = (*cmd_func)(g_stream_table[i].extra_args, ptr, sizeof(output) - (ptr - output), 1); if (err == 0xff) { // Nothing to do. return; } else if (err != 0) { // Yikes, error! Report the error and stop the stream. g_stream_table[i].stream_id = 0; g_stream_table[i].extra_args[0] = 0; } strcat_P(output, PSTR(EOL)); usb_serial_write((uint8_t*) output, strlen(output)); return; } } }
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 }
static void handle_line(char* line_buf) { char output[80]; char *ptr = output; *ptr++ = '<'; uint8_t len = strlen((const char*) line_buf); if (len == 0) { return; } if (line_buf[0] == MAGIC_OVERRUN_CODE) { strcpy_P(ptr, PSTR("ERR overrun")); } else { // Try to parse the command. if (len < 3) { strcpy_P(ptr, PSTR("ERR short cmd")); } else if (len >= 4 && line_buf[3] != ' ') { strcpy_P(ptr, PSTR("ERR cmd not followed by space")); } else { int8_t index = find_message_index(line_buf); if (index < 0) { strcpy_P(ptr, PSTR("ERR" STR_UNKNOWN_CMD)); } else { const struct fw_command_struct* fw_command = &fw_command_table[index]; memcpy(ptr, line_buf, 3); ptr += 3; *ptr = 0; // so that commands start with an empty string cmd_func_ptr cmd_func = (cmd_func_ptr) pgm_read_word(&fw_command->func); (*cmd_func)( &line_buf[3], ptr, sizeof(output) - (ptr - output) - 3, 0); } } } strcat_P(output, PSTR(EOL)); usb_serial_write((const uint8_t*) output, strlen(output)); }
/** 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(); } }
/** * Sets the correct damper to be active. * Damper is high when button isn't pressed * for simplicity it would be better to switch this to string comparisons. */ void ActivateDamper(uint8_t damper) { // converts decimal to binary digits for ABCD // could be backwards. // example input A hex or 1010 bin or 10 dec uint8_t D = damper / 8;// D = 1bin or 1 dec uint8_t temp = damper % 8;// temp = 10bin and 2 dec uint8_t C = temp / 4;// C = (2)/4 = 0 temp = temp % 4;// temp = (2)%4 = 2 uint8_t B = temp / 2;// 1 temp = temp % 2;// 0 uint8_t A = temp; char tempBuf[4]; //itoa(Command, tempBuf,10); tempBuf[0] = (char)A + '0'; tempBuf[1] = (char)B + '0'; tempBuf[2] = (char)C + '0'; tempBuf[3] = (char)D + '0'; send_str(PSTR("Test Outputs of ABCD:\r\n")); usb_serial_write(tempBuf, 4); //usb_serial_write(itemBuf, 2); send_str(PSTR("\r\n")); //set the output pins as such. //d to A if(D==1) { DEMUX_D_ON; } else { DEMUX_D_OFF; } if(C==1) { DEMUX_C_ON; } else { DEMUX_C_OFF; } if(B==1) { DEMUX_B_ON; } else { DEMUX_B_OFF; } if(A==1) { DEMUX_A_ON; } else { DEMUX_A_OFF; } }
void serial_write_string(const char *str) { usb_serial_write(str,strlen(str)); }
// transmit a character. 0 returned on success, -1 on error int usb_serial_putchar( uint8_t c ) { return usb_serial_write( &c, 1 ); }
/** * 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; }