|
|
|
|
|
|
|
|
// ----- USB Virtual Serial Port (CDC) Functions ----- |
|
|
// ----- USB Virtual Serial Port (CDC) Functions ----- |
|
|
|
|
|
|
|
|
// get the next character, or -1 if nothing received |
|
|
// get the next character, or -1 if nothing received |
|
|
int16_t usb_serial_getchar(void) |
|
|
|
|
|
|
|
|
int16_t usb_serial_getchar() |
|
|
{ |
|
|
{ |
|
|
uint8_t c, intr_state; |
|
|
uint8_t c, intr_state; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// number of bytes available in the receive buffer |
|
|
// number of bytes available in the receive buffer |
|
|
uint8_t usb_serial_available(void) |
|
|
|
|
|
|
|
|
uint8_t usb_serial_available() |
|
|
{ |
|
|
{ |
|
|
uint8_t n=0, i, intr_state; |
|
|
uint8_t n=0, i, intr_state; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// discard any buffered input |
|
|
// discard any buffered input |
|
|
void usb_serial_flush_input(void) |
|
|
|
|
|
|
|
|
void usb_serial_flush_input() |
|
|
{ |
|
|
{ |
|
|
uint8_t intr_state; |
|
|
uint8_t intr_state; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// at full USB speed), but they are set by the host so we can |
|
|
// at full USB speed), but they are set by the host so we can |
|
|
// set them properly if we're converting the USB to a real serial |
|
|
// set them properly if we're converting the USB to a real serial |
|
|
// communication |
|
|
// communication |
|
|
uint32_t usb_serial_get_baud(void) |
|
|
|
|
|
|
|
|
uint32_t usb_serial_get_baud() |
|
|
{ |
|
|
{ |
|
|
uint32_t *baud = (uint32_t*)cdc_line_coding; |
|
|
uint32_t *baud = (uint32_t*)cdc_line_coding; |
|
|
return *baud; |
|
|
return *baud; |
|
|
} |
|
|
} |
|
|
uint8_t usb_serial_get_stopbits(void) |
|
|
|
|
|
|
|
|
uint8_t usb_serial_get_stopbits() |
|
|
{ |
|
|
{ |
|
|
return cdc_line_coding[4]; |
|
|
return cdc_line_coding[4]; |
|
|
} |
|
|
} |
|
|
uint8_t usb_serial_get_paritytype(void) |
|
|
|
|
|
|
|
|
uint8_t usb_serial_get_paritytype() |
|
|
{ |
|
|
{ |
|
|
return cdc_line_coding[5]; |
|
|
return cdc_line_coding[5]; |
|
|
} |
|
|
} |
|
|
uint8_t usb_serial_get_numbits(void) |
|
|
|
|
|
|
|
|
uint8_t usb_serial_get_numbits() |
|
|
{ |
|
|
{ |
|
|
return cdc_line_coding[6]; |
|
|
return cdc_line_coding[6]; |
|
|
} |
|
|
} |
|
|
uint8_t usb_serial_get_control(void) |
|
|
|
|
|
|
|
|
uint8_t usb_serial_get_control() |
|
|
{ |
|
|
{ |
|
|
return cdc_line_rtsdtr; |
|
|
return cdc_line_rtsdtr; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WDT Setup for software reset the chip |
|
|
// WDT Setup for software reset the chip |
|
|
void wdt_init(void) |
|
|
|
|
|
|
|
|
void wdt_init() |
|
|
{ |
|
|
{ |
|
|
MCUSR = 0; |
|
|
MCUSR = 0; |
|
|
wdt_disable(); |
|
|
wdt_disable(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// initialize USB |
|
|
// initialize USB |
|
|
void usb_init(void) |
|
|
|
|
|
|
|
|
void usb_init() |
|
|
{ |
|
|
{ |
|
|
HW_CONFIG(); |
|
|
HW_CONFIG(); |
|
|
USB_FREEZE(); // enable USB |
|
|
USB_FREEZE(); // enable USB |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Misc functions to wait for ready and send/receive packets |
|
|
// Misc functions to wait for ready and send/receive packets |
|
|
static inline void usb_wait_in_ready(void) |
|
|
|
|
|
|
|
|
static inline void usb_wait_in_ready() |
|
|
{ |
|
|
{ |
|
|
while (!(UEINTX & (1<<TXINI))) ; |
|
|
while (!(UEINTX & (1<<TXINI))) ; |
|
|
} |
|
|
} |
|
|
static inline void usb_send_in(void) |
|
|
|
|
|
|
|
|
static inline void usb_send_in() |
|
|
{ |
|
|
{ |
|
|
UEINTX = ~(1<<TXINI); |
|
|
UEINTX = ~(1<<TXINI); |
|
|
} |
|
|
} |
|
|
static inline void usb_wait_receive_out(void) |
|
|
|
|
|
|
|
|
static inline void usb_wait_receive_out() |
|
|
{ |
|
|
{ |
|
|
while (!(UEINTX & (1<<RXOUTI))) ; |
|
|
while (!(UEINTX & (1<<RXOUTI))) ; |
|
|
} |
|
|
} |
|
|
static inline void usb_ack_out(void) |
|
|
|
|
|
|
|
|
static inline void usb_ack_out() |
|
|
{ |
|
|
{ |
|
|
UEINTX = ~(1<<RXOUTI); |
|
|
UEINTX = ~(1<<RXOUTI); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// other endpoints are manipulated by the user-callable |
|
|
// other endpoints are manipulated by the user-callable |
|
|
// functions, and the start-of-frame interrupt. |
|
|
// functions, and the start-of-frame interrupt. |
|
|
// |
|
|
// |
|
|
ISR(USB_COM_vect) |
|
|
|
|
|
|
|
|
ISR( USB_COM_vect ) |
|
|
{ |
|
|
{ |
|
|
uint8_t intbits; |
|
|
uint8_t intbits; |
|
|
const uint8_t *list; |
|
|
const uint8_t *list; |