From 72c52d30c894b85e64861704e162ee3873661c7d Mon Sep 17 00:00:00 2001 From: flabbergast Date: Fri, 22 Jan 2016 22:19:02 +0000 Subject: [PATCH] Chibios: add more guards for transmitting (fix a deadlock bug). --- keyboard/infinity_chibios/halconf.h | 12 ++++++++++++ keyboard/stm32_f072_onekey/halconf.h | 12 ++++++++++++ keyboard/stm32_f103_onekey/halconf.h | 12 ++++++++++++ keyboard/teensy_lc_onekey/halconf.h | 12 ++++++++++++ tmk_core/protocol/chibios/usb_main.c | 22 +++++++++++++++++----- 5 files changed, 65 insertions(+), 5 deletions(-) diff --git a/keyboard/infinity_chibios/halconf.h b/keyboard/infinity_chibios/halconf.h index 61a9ac0a..53538de1 100644 --- a/keyboard/infinity_chibios/halconf.h +++ b/keyboard/infinity_chibios/halconf.h @@ -336,6 +336,18 @@ #define SPI_USE_MUTUAL_EXCLUSION TRUE #endif +/*===========================================================================*/ +/* USB driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(USB_USE_WAIT) || defined(__DOXYGEN__) +#define USB_USE_WAIT TRUE +#endif + #endif /* _HALCONF_H_ */ /** @} */ diff --git a/keyboard/stm32_f072_onekey/halconf.h b/keyboard/stm32_f072_onekey/halconf.h index e75c1c36..1a450d63 100644 --- a/keyboard/stm32_f072_onekey/halconf.h +++ b/keyboard/stm32_f072_onekey/halconf.h @@ -336,6 +336,18 @@ #define SPI_USE_MUTUAL_EXCLUSION TRUE #endif +/*===========================================================================*/ +/* USB driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(USB_USE_WAIT) || defined(__DOXYGEN__) +#define USB_USE_WAIT TRUE +#endif + #endif /* _HALCONF_H_ */ /** @} */ diff --git a/keyboard/stm32_f103_onekey/halconf.h b/keyboard/stm32_f103_onekey/halconf.h index e75c1c36..1a450d63 100644 --- a/keyboard/stm32_f103_onekey/halconf.h +++ b/keyboard/stm32_f103_onekey/halconf.h @@ -336,6 +336,18 @@ #define SPI_USE_MUTUAL_EXCLUSION TRUE #endif +/*===========================================================================*/ +/* USB driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(USB_USE_WAIT) || defined(__DOXYGEN__) +#define USB_USE_WAIT TRUE +#endif + #endif /* _HALCONF_H_ */ /** @} */ diff --git a/keyboard/teensy_lc_onekey/halconf.h b/keyboard/teensy_lc_onekey/halconf.h index 86839166..b1d2cd55 100644 --- a/keyboard/teensy_lc_onekey/halconf.h +++ b/keyboard/teensy_lc_onekey/halconf.h @@ -170,6 +170,18 @@ #define HAL_USE_WDG FALSE #endif +/*===========================================================================*/ +/* USB driver related settings. */ +/*===========================================================================*/ + +/** + * @brief Enables synchronous APIs. + * @note Disabling this option saves both code and data space. + */ +#if !defined(USB_USE_WAIT) || defined(__DOXYGEN__) +#define USB_USE_WAIT TRUE +#endif + #endif /* _HALCONF_H_ */ /** @} */ diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 9097d4e8..d248b447 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -1094,7 +1094,9 @@ static void keyboard_idle_timer_cb(void *arg) { if(keyboard_idle) { #endif /* NKRO_ENABLE */ /* TODO: are we sure we want the KBD_ENDPOINT? */ - usbStartTransmitI(usbp, KBD_ENDPOINT, (uint8_t *)&keyboard_report_sent, sizeof(keyboard_report_sent)); + if(!usbGetTransmitStatusI(usbp, KBD_ENDPOINT)) { + usbStartTransmitI(usbp, KBD_ENDPOINT, (uint8_t *)&keyboard_report_sent, KBD_EPSIZE); + } /* rearm the timer */ chVTSetI(&keyboard_idle_timer, 4*MS2ST(keyboard_idle), keyboard_idle_timer_cb, (void *)usbp); } @@ -1127,8 +1129,13 @@ void send_keyboard(report_keyboard_t *report) { * this is more efficient */ /* busy wait, should be short and not very common */ osalSysLock(); - while(usbGetTransmitStatusI(&USB_DRIVER, NKRO_ENDPOINT)) - ; + if(usbGetTransmitStatusI(&USB_DRIVER, NKRO_ENDPOINT)) { + /* Need to either suspend, or loop and call unlock/lock during + * every iteration - otherwise the system will remain locked, + * no interrupts served, so USB not going through as well. + * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ + osalThreadSuspendS(&(&USB_DRIVER)->epc[NKRO_ENDPOINT]->in_state->thread); + } usbStartTransmitI(&USB_DRIVER, NKRO_ENDPOINT, (uint8_t *)report, sizeof(report_keyboard_t)); osalSysUnlock(); } else @@ -1137,8 +1144,13 @@ void send_keyboard(report_keyboard_t *report) { /* need to wait until the previous packet has made it through */ /* busy wait, should be short and not very common */ osalSysLock(); - while(usbGetTransmitStatusI(&USB_DRIVER, KBD_ENDPOINT)) - ; + if(usbGetTransmitStatusI(&USB_DRIVER, KBD_ENDPOINT)) { + /* Need to either suspend, or loop and call unlock/lock during + * every iteration - otherwise the system will remain locked, + * no interrupts served, so USB not going through as well. + * Note: for suspend, need USB_USE_WAIT == TRUE in halconf.h */ + osalThreadSuspendS(&(&USB_DRIVER)->epc[KBD_ENDPOINT]->in_state->thread); + } usbStartTransmitI(&USB_DRIVER, KBD_ENDPOINT, (uint8_t *)report, KBD_EPSIZE); osalSysUnlock(); } -- 2.39.3