USB Communications Device Class (Serial Port) routines. More...
Go to the source code of this file.
Defines | |
#define | PARITY_EVEN 2 |
#define | PARITY_MARK 3 |
#define | PARITY_NONE 0 |
#define | PARITY_ODD 1 |
#define | PARITY_SPACE 4 |
Functions | |
uns8 | usb_cdc_getc () |
Retrieve a received character from the USB serial port. | |
void | usb_cdc_handle_tx () |
Transmit any buffered characters over ther USB virtual serial port. | |
void | usb_cdc_print_int (uns16 i) |
Print a 16 bit number to the USB virtual serial port. | |
void | usb_cdc_print_str (char *str) |
Print a string out to the USB virtual serial port. | |
void | usb_cdc_putc (uns8 c) |
Sends a single character to the USB serial port. | |
uns8 | usb_cdc_rx_avail () |
Check to see if a character is available in the receive buffer. | |
void | usb_cdc_setup () |
Set up data structures ready for USB CDC use. | |
uns8 | usb_cdc_tx_empty () |
Check to see if the transmit buffer is empty. | |
Variables | |
uns8 | data_bits |
uns8 | parity |
#define PARITY_EVEN 2 |
#define PARITY_MARK 3 |
#define PARITY_NONE 0 |
#define PARITY_ODD 1 |
#define PARITY_SPACE 4 |
uns8 usb_cdc_getc | ( | ) |
Receive a character from the USB serial port. If a character has not been received, this routine will wait indefinately.
00377 { 00378 uns8 cdc_rx_char, cdc_rx_next; 00379 00380 while(cdc_rx_end == cdc_rx_start); // wait until there is something received 00381 00382 start_crit_sec(); // make sure nobody else can muck with the buffer 00383 00384 cdc_rx_char = cdc_rx_buffer[cdc_rx_start]; // get character from the front of the buffer 00385 cdc_rx_start++; // increment fifo start 00386 if (cdc_rx_start == USB_CDC_RX_BUFFER_SIZE) { // if we're at the end 00387 cdc_rx_start = 0; // then wrap to the beginning 00388 } 00389 00390 end_crit_sec(); // now they can muck with the buffer 00391 00392 return (cdc_rx_char); // return the result we first thought of 00393 00394 } // -- getc
void usb_cdc_handle_tx | ( | ) |
Generally only used internally, this routine will attempt to place any characters in the transmit queue into the USB buffers. It will fail gracefully if the USB buffer is already owned by the SIE.
00398 { 00399 uns8 cdc_tx_next; 00400 uns8 count; 00401 uns16 buffer_size; 00402 uns8 *buffer; 00403 buffer_descriptor *bd; 00404 00405 bd = ep_in_bd_location[USB_CDC_DATA_ENDPOINT]; 00406 if (test_bit(bd->stat, UOWN)) { // if there's already something in play 00407 return; // give up 00408 } 00409 00410 buffer_size = ep_in_buffer_size[USB_CDC_DATA_ENDPOINT]; 00411 buffer = ep_in_buffer_location[USB_CDC_DATA_ENDPOINT]; 00412 00413 if (cdc_tx_end == cdc_tx_start) { // anything in the fifo? 00414 return; // nope 00415 } 00416 #ifdef CDC_DEBUG 00417 serial_putc('<'); 00418 #endif 00419 start_crit_sec(); 00420 00421 count = 0; 00422 while ((cdc_tx_end != cdc_tx_start) && (count < buffer_size)) { 00423 00424 cdc_tx_next = cdc_tx_start + 1; // get next position 00425 if (cdc_tx_next == USB_CDC_TX_BUFFER_SIZE) { // if we're at the end of the buffer 00426 cdc_tx_next = 0; // wrap to the beginning 00427 } 00428 buffer[count] = cdc_tx_buffer[cdc_tx_start]; // transmit the character 00429 #ifdef CDC_DEBUG 00430 serial_putc(buffer[count]); 00431 #endif 00432 count++; 00433 cdc_tx_start = cdc_tx_next; // move start position of fifo 00434 } 00435 if (count > 0) { 00436 bd->count = count; 00437 bd->addr = (uns16)buffer; 00438 00439 toggle_bit(bd->stat, DTS); 00440 clear_bit(bd->stat, KEN); // clear the keep bit 00441 clear_bit(bd->stat, INCDIS); // clear the increment disable 00442 set_bit (bd->stat, DTSEN); 00443 clear_bit(bd->stat, BSTALL); // clear stall bit 00444 clear_bit(bd->stat, BC9); 00445 clear_bit(bd->stat, BC8); 00446 00447 set_bit (bd->stat, UOWN); // SIE owns the buffer 00448 } 00449 end_crit_sec(); 00450 #ifdef CDC_DEBUG 00451 serial_putc('>'); 00452 serial_print_str("send="); 00453 serial_print_int(count); 00454 serial_putc(' '); 00455 #endif 00456 #ifdef USB_CDC_USE_LEDS 00457 platform_leds_flash(2); 00458 #endif 00459 }
void usb_cdc_print_int | ( | uns16 | i | ) |
Print a 16 bit unsigned number in decimal to the USB virtual serial port
i | the 16 bit number to be printed |
00497 { 00498 00499 char buffer[6]; // up to 5 characters plus \0 00500 uns8 count = 5; 00501 buffer[5] = '\0'; 00502 do { 00503 count--; 00504 buffer[count] = '0' + i % 10; 00505 i = i / 10; 00506 } while (i > 0); 00507 while (buffer[count]) { 00508 usb_cdc_putc(buffer[count]); 00509 count++; 00510 } 00511 //serial_print_str(&buffer[count]); // print it out 00512 // for(count = 0 ; str[count] != 0; count++) 00513 // { 00514 // } 00515 00516 }
void usb_cdc_print_str | ( | char * | str | ) |
Send a null terminated string out the virtual serial port
str | the string to be sent |
00464 { 00465 00466 uns8 count; 00467 buffer_descriptor *bd; 00468 00469 for(count = 0 ; str[count] != 0; count++) 00470 { 00471 usb_cdc_putc(str[count]); 00472 } 00473 00474 // This will give possibly quicker send: 00475 00476 //bd = ep_in_bd_location[CDC_DATA_ENDPOINT]; 00477 //if (!test_bit(bd->stat, UOWN)) { 00478 // usb_cdc_handle_tx(); 00479 //} 00480 // otherwise we wait for the SOF interrupt to send 00481 }
void usb_cdc_putc | ( | uns8 | c | ) |
Deliver a single character out the virtual serial port. This routine will add the character to the transmit buffer. The actual buffer will be physically sent on the Start Of Frame (SOF) interrupt (each 1ms) or on the end point interrupt - ie, when the last character or chunk of characters was sent.
c | The 8 bit byte to be transmitted. |
00348 { 00349 uns8 cdc_tx_next; 00350 bit my_store_gie; 00351 #ifdef CDC_IDE_DEBUG 00352 return; 00353 #endif 00354 00355 cdc_tx_next = cdc_tx_end + 1; // get next buffer position 00356 if (cdc_tx_next == USB_CDC_TX_BUFFER_SIZE) { // if we're at the end 00357 cdc_tx_next = 0; // wrap to the beginning 00358 } 00359 00360 if ((!intcon.GIE) && (cdc_tx_next == cdc_tx_start)) { 00361 return; 00362 } 00363 while (cdc_tx_next == cdc_tx_start) { 00364 00365 } 00366 00367 start_crit_sec(); 00368 00369 cdc_tx_buffer[cdc_tx_end] = c; // put it in 00370 cdc_tx_end = cdc_tx_next; // move pointer along 00371 00372 end_crit_sec(); 00373 00374 }
uns8 usb_cdc_rx_avail | ( | ) |
If one or more bytes are available in the USB serial port receive buffer, this routine will return true. If there are no bytes available, it will return false.
00461 { return cdc_rx_start != cdc_rx_end; }
void usb_cdc_setup | ( | ) |
Configures the default DTE rate, stop bits etc.
00488 { 00489 00490 current_bit_rate = SPBRG_9600; 00491 parity = 0; 00492 // 9600 00493 dte_rate.as_long = 9600; 00494 00495 }
uns8 usb_cdc_tx_empty | ( | ) |
Sometimes it is useful to see if the transmit buffer is empty, since then you can be sure your data is well on its way. In the case of USB, this means that the data has been at least placed into the outbound USB buffer; it's not possible to tell until after the fact if the data has actually been squirted out the USB port.
00462 { return cdc_tx_start == cdc_tx_end; }
uns8 data_bits |
uns8 parity |