diff --git a/Src/usbd_cdc_if.c b/Src/usbd_cdc_if.c index ca2db06..583ee90 100644 --- a/Src/usbd_cdc_if.c +++ b/Src/usbd_cdc_if.c @@ -55,7 +55,7 @@ #include "UsbPort.h" #include "main.h" #include "cmsis_os.h" -extern osMessageQId ioEventQueueHandle; +#include "IOEventTask.h" /* USER CODE END INCLUDE */ @@ -65,7 +65,6 @@ extern osMessageQId ioEventQueueHandle; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ -static int connected = 0; /* USER CODE END PV */ /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY @@ -262,10 +261,6 @@ static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ /*******************************************************************************/ case CDC_SET_LINE_CODING: - if (!connected) { - connected = 1; - osMessagePut(ioEventQueueHandle, CMD_USB_CDC_CONNECT, 0); - } LineCoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) | (pbuf[2] << 16) | (pbuf[3] << 24)); LineCoding.format = pbuf[4]; LineCoding.paritytype = pbuf[5]; @@ -285,11 +280,9 @@ static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) case CDC_SET_CONTROL_LINE_STATE: if (length == 0) { USBD_SetupReqTypedef* req = (USBD_SetupReqTypedef*) pbuf; - if ((req->wValue & 1) && (!connected)) { - connected = 1; + if ((req->wValue & 1)) { osMessagePut(ioEventQueueHandle, CMD_USB_CDC_CONNECT, 0); - } else if (connected) { - connected = 0; + } else { osMessagePut(ioEventQueueHandle, CMD_USB_CDC_DISCONNECT, 0); } } @@ -313,7 +306,7 @@ static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) * * @note * This function will block any OUT packet reception on USB endpoint - * untill exiting this function. If you exit this function before transfer + * until exiting this function. If you exit this function before transfer * is complete on CDC interface (ie. using DMA controller) it will result * in receiving more data while previous ones are still not sent. * @@ -324,8 +317,7 @@ static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len) { /* USER CODE BEGIN 6 */ - if (!connected) { - connected = 1; + if (!cdc_connected) { osMessagePut(ioEventQueueHandle, CMD_USB_CDC_CONNECT, 0); } cdc_receive(Buf, *Len); diff --git a/TNC/IOEventTask.cpp b/TNC/IOEventTask.cpp index 7fdeeb2..2c9e242 100644 --- a/TNC/IOEventTask.cpp +++ b/TNC/IOEventTask.cpp @@ -32,6 +32,8 @@ extern "C" void stop2(void); extern "C" void shutdown(void const * argument); extern "C" void startLedBlinkerTask(void const*); +volatile int cdc_connected{0}; + static PTT getPttStyle(const mobilinkd::tnc::kiss::Hardware& hardware) { return hardware.options & KISS_OPTION_PTT_SIMPLEX ? PTT::SIMPLEX : PTT::MULTIPLEX; @@ -123,8 +125,9 @@ void startIOEventTask(void const*) { switch (cmd) { case CMD_USB_CDC_CONNECT: - if (openCDC()) + if (!cdc_connected && openCDC()) { + cdc_connected = true; // Disable Bluetooth Module HAL_NVIC_DisableIRQ(EXTI4_IRQn); HAL_NVIC_DisableIRQ(EXTI9_5_IRQn); @@ -152,21 +155,24 @@ void startIOEventTask(void const*) } [[ fallthrough ]]; // when the CDC part was connected. case CMD_USB_CDC_DISCONNECT: - osMessagePut(audioInputQueueHandle, audio::IDLE, - osWaitForever); - kiss::getAFSKTestTone().stop(); - closeCDC(); - INFO("CDC Closed"); + if (cdc_connected) { + cdc_connected = false; + osMessagePut(audioInputQueueHandle, audio::IDLE, + osWaitForever); + kiss::getAFSKTestTone().stop(); + closeCDC(); + INFO("CDC Closed"); - // Enable Bluetooth Module - HAL_GPIO_WritePin(BT_SLEEP_GPIO_Port, BT_SLEEP_Pin, - GPIO_PIN_SET); - bm78_wait_until_ready(); + // Enable Bluetooth Module + HAL_GPIO_WritePin(BT_SLEEP_GPIO_Port, BT_SLEEP_Pin, + GPIO_PIN_SET); + bm78_wait_until_ready(); - HAL_NVIC_EnableIRQ(EXTI4_IRQn); - HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); + HAL_NVIC_EnableIRQ(EXTI4_IRQn); + HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); - indicate_waiting_to_connect(); + indicate_waiting_to_connect(); + } break; case CMD_POWER_BUTTON_DOWN: INFO("Power Down"); diff --git a/TNC/IOEventTask.h b/TNC/IOEventTask.h index ea89bd9..60829f4 100644 --- a/TNC/IOEventTask.h +++ b/TNC/IOEventTask.h @@ -13,11 +13,12 @@ extern "C" { void startIOEventTask(void const* argument); void startCdcBlinker(void const* argument); +extern osMessageQId ioEventQueueHandle; +extern volatile int cdc_connected; + #ifdef __cplusplus } -extern osMessageQId ioEventQueueHandle; - namespace mobilinkd { namespace tnc { void print_startup_banner() __attribute__((noinline));