Browse Source

Adding timeout to virtual serial port writes

- Was causing lock-ups until the serial port was read
- Also checking each of the NKRO key types in each send loop
simple
Jacob Alexander 9 years ago
parent
commit
358b1f33bb
1 changed files with 12 additions and 12 deletions
  1. 12
    12
      Output/pjrcUSB/avr/usb_keyboard_serial.c

+ 12
- 12
Output/pjrcUSB/avr/usb_keyboard_serial.c View File

USBKeys_Changed &= ~USBKeyChangeState_Modifiers; // Mark sent USBKeys_Changed &= ~USBKeyChangeState_Modifiers; // Mark sent
} }
// Check main key section // Check main key section
else if ( USBKeys_Changed & USBKeyChangeState_MainKeys )
if ( USBKeys_Changed & USBKeyChangeState_MainKeys )
{ {
UEDATX = 0x03; // ID UEDATX = 0x03; // ID


USBKeys_Changed &= ~USBKeyChangeState_MainKeys; // Mark sent USBKeys_Changed &= ~USBKeyChangeState_MainKeys; // Mark sent
} }
// Check secondary key section // Check secondary key section
else if ( USBKeys_Changed & USBKeyChangeState_SecondaryKeys )
if ( USBKeys_Changed & USBKeyChangeState_SecondaryKeys )
{ {
UEDATX = 0x04; // ID UEDATX = 0x04; // ID


USBKeys_Changed &= ~USBKeyChangeState_SecondaryKeys; // Mark sent USBKeys_Changed &= ~USBKeyChangeState_SecondaryKeys; // Mark sent
} }
// Check tertiary key section // Check tertiary key section
else if ( USBKeys_Changed & USBKeyChangeState_TertiaryKeys )
if ( USBKeys_Changed & USBKeyChangeState_TertiaryKeys )
{ {
UEDATX = 0x05; // ID UEDATX = 0x05; // ID


USBKeys_Changed &= ~USBKeyChangeState_TertiaryKeys; // Mark sent USBKeys_Changed &= ~USBKeyChangeState_TertiaryKeys; // Mark sent
} }
// Check system control keys // Check system control keys
else if ( USBKeys_Changed & USBKeyChangeState_System )
if ( USBKeys_Changed & USBKeyChangeState_System )
{ {
UEDATX = 0x06; // ID UEDATX = 0x06; // ID
UEDATX = USBKeys_SysCtrl; UEDATX = USBKeys_SysCtrl;
USBKeys_Changed &= ~USBKeyChangeState_System; // Mark sent USBKeys_Changed &= ~USBKeyChangeState_System; // Mark sent
} }
// Check consumer control keys // Check consumer control keys
else if ( USBKeys_Changed & USBKeyChangeState_Consumer )
if ( USBKeys_Changed & USBKeyChangeState_Consumer )
{ {
UEDATX = 0x07; // ID UEDATX = 0x07; // ID
UEDATX = (uint8_t)(USBKeys_ConsCtrl & 0x00FF); UEDATX = (uint8_t)(USBKeys_ConsCtrl & 0x00FF);
} }


// transmit a character. 0 returned on success, -1 on error // transmit a character. 0 returned on success, -1 on error
int8_t usb_serial_putchar(uint8_t c)
int8_t usb_serial_putchar( uint8_t c )
{ {
uint8_t timeout, intr_state; uint8_t timeout, intr_state;




// transmit a character, but do not wait if the buffer is full, // transmit a character, but do not wait if the buffer is full,
// 0 returned on success, -1 on buffer full or error // 0 returned on success, -1 on buffer full or error
int8_t usb_serial_putchar_nowait(uint8_t c)
int8_t usb_serial_putchar_nowait( uint8_t c )
{ {
uint8_t intr_state; uint8_t intr_state;


// controller in the PC will not allocate bandwitdh without a pending read request. // controller in the PC will not allocate bandwitdh without a pending read request.
// (thanks to Victor Suarez for testing and feedback and initial code) // (thanks to Victor Suarez for testing and feedback and initial code)


int8_t usb_serial_write(const char *buffer, uint16_t size)
int8_t usb_serial_write( const char *buffer, uint16_t size )
{ {
uint8_t timeout, intr_state, write_size; uint8_t timeout, intr_state, write_size;


cli(); cli();
UENUM = CDC_TX_ENDPOINT; UENUM = CDC_TX_ENDPOINT;
// if we gave up due to timeout before, don't wait again // if we gave up due to timeout before, don't wait again
/*
if (transmit_previous_timeout) { if (transmit_previous_timeout) {
if (!(UEINTX & (1<<RWAL))) { if (!(UEINTX & (1<<RWAL))) {
SREG = intr_state; SREG = intr_state;
} }
transmit_previous_timeout = 0; transmit_previous_timeout = 0;
} }
*/
// each iteration of this loop transmits a packet // each iteration of this loop transmits a packet
while (size) { while (size) {
// wait for the FIFO to be ready to accept data // wait for the FIFO to be ready to accept data
// This doesn't actually transmit the data - that is impossible! // This doesn't actually transmit the data - that is impossible!
// USB devices only transmit when the host allows, so the best // USB devices only transmit when the host allows, so the best
// we can do is release the FIFO buffer for when the host wants it // we can do is release the FIFO buffer for when the host wants it
void usb_serial_flush_output(void)
void usb_serial_flush_output()
{ {
uint8_t intr_state; uint8_t intr_state;


// it remains buffered (either here or on the host) and can not be // it remains buffered (either here or on the host) and can not be
// lost because you weren't listening at the right time, like it // lost because you weren't listening at the right time, like it
// would in real serial communication. // would in real serial communication.
int8_t usb_serial_set_control(uint8_t signals)
int8_t usb_serial_set_control( uint8_t signals )
{ {
uint8_t intr_state; uint8_t intr_state;