diff --git a/Src/main.c b/Src/main.c index f495529..5cb813a 100644 --- a/Src/main.c +++ b/Src/main.c @@ -59,6 +59,7 @@ #include "LEDIndicator.h" #include "bm78.h" #include "base64.h" +#include "KissHardware.h" /* USER CODE END Includes */ @@ -276,12 +277,13 @@ void configure_wakeup_gpio() HAL_NVIC_DisableIRQ(EXTI1_IRQn); HAL_GPIO_DeInit(GPIOH, USB_POWER_Pin|SW_POWER_Pin); - // Wake up whenever there is a change in VUSB to handle connect and - // disconnect events. - GPIO_InitStruct.Pin = USB_POWER_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING_FALLING; - GPIO_InitStruct.Pull = GPIO_NOPULL; // Pulled down on PCB. - HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); + // Wake up whenever there is a change in VUSB to handle connect events. + if (powerOnViaUSB()) { + GPIO_InitStruct.Pin = USB_POWER_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING; + GPIO_InitStruct.Pull = GPIO_NOPULL; // Pulled down on PCB. + HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); + } // Only wake up after the button has been released. This avoids the case // where the TNC is woken up on button down and then immediately put back @@ -1263,57 +1265,7 @@ void stop2() HAL_PWREx_DisableLowPowerRunMode(); HAL_DBGMCU_DisableDBGStopMode(); HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFE); - - uint16_t wakeup_pin = SW_POWER_Pin; - - if (HAL_GPIO_ReadPin(USB_POWER_GPIO_Port, USB_POWER_Pin) != usb) - { - wakeup_pin = USB_POWER_Pin; - } - - HAL_NVIC_ClearPendingIRQ(EXTI0_IRQn); - HAL_NVIC_ClearPendingIRQ(EXTI1_IRQn); - HAL_NVIC_ClearPendingIRQ(EXTI3_IRQn); - HAL_NVIC_ClearPendingIRQ(EXTI4_IRQn); - HAL_NVIC_ClearPendingIRQ(EXTI9_5_IRQn); - - __asm volatile ( "cpsie i" ); - __asm volatile ( "dsb" ); - __asm volatile ( "isb" ); - - MX_GPIO_Init(); - - // Let power stabilize. - for (int i = 0; i < 4800; ++i) asm volatile("nop"); - - SystemClock_Config(); - - if (HAL_CRC_Init(&hcrc) != HAL_OK) Error_Handler(); - // Need to re-init and calibrate after deep power down. - if (HAL_ADC_Init(&hadc1) != HAL_OK) Error_Handler(); - if (HAL_DAC_Init(&hdac1) != HAL_OK) Error_Handler(); - if (HAL_OPAMP_Init(&hopamp1) != HAL_OK) Error_Handler(); - - if (HAL_UART_Init(&huart3) != HAL_OK) Error_Handler(); - __HAL_UART_DISABLE(&huart3); - - HAL_TIM_Base_Init(&htim1); - HAL_TIM_MspPostInit(&htim1); - HAL_TIM_Base_Init(&htim7); - HAL_TIM_Base_Init(&htim6); - - if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED) != HAL_OK) Error_Handler(); - if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_2) != HAL_OK) Error_Handler(); - if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_1) != HAL_OK) Error_Handler(); - if (HAL_OPAMP_Start(&hopamp1) != HAL_OK) Error_Handler(); - HAL_PWR_EnablePVD(); - - if (HAL_GPIO_ReadPin(USB_POWER_GPIO_Port, USB_POWER_Pin) == GPIO_PIN_SET) - { - HAL_PCD_Init(&hpcd_USB_FS); - } - - osThreadResumeAll(); + HAL_NVIC_SystemReset(); } #if 1 diff --git a/TNC/IOEventTask.cpp b/TNC/IOEventTask.cpp index d7963a2..d451257 100644 --- a/TNC/IOEventTask.cpp +++ b/TNC/IOEventTask.cpp @@ -32,6 +32,11 @@ extern "C" void stop2(void); extern "C" void shutdown(void); extern "C" void startLedBlinkerTask(void const*); +static PTT getPttStyle(const mobilinkd::tnc::kiss::Hardware& hardware) +{ + return hardware.options & KISS_OPTION_PTT_SIMPLEX ? PTT::SIMPLEX : PTT::MULTIPLEX; +} + void startIOEventTask(void const*) { using namespace mobilinkd::tnc; @@ -54,6 +59,7 @@ void startIOEventTask(void const*) audio::init_log_volume(); audio::setAudioOutputLevel(); audio::setAudioInputLevels(); + setPtt(getPttStyle(hardware)); // Cannot enable these interrupts until we start the io loop because // they send messages on the queue. @@ -119,11 +125,16 @@ void startIOEventTask(void const*) break; case CMD_USB_DISCONNECTED: INFO("VBUS Lost"); - HAL_PCD_MspDeInit(&hpcd_USB_FS); - HAL_GPIO_WritePin(USB_CE_GPIO_Port, USB_CE_Pin, GPIO_PIN_SET); - if (ioport != getUsbPort()) - { + if (powerOffViaUSB()) { + stop2(); break; + } else { + HAL_PCD_MspDeInit(&hpcd_USB_FS); + HAL_GPIO_WritePin(USB_CE_GPIO_Port, USB_CE_Pin, GPIO_PIN_SET); + if (ioport != getUsbPort()) + { + break; + } } /* Fallthrough*/ // when the CDC part was connected. case CMD_USB_CDC_DISCONNECT: diff --git a/TNC/KissHardware.cpp b/TNC/KissHardware.cpp index d506f7a..330a7bd 100644 --- a/TNC/KissHardware.cpp +++ b/TNC/KissHardware.cpp @@ -15,9 +15,19 @@ extern I2C_HandleTypeDef hi2c1; extern RTC_HandleTypeDef hrtc; +int powerOnViaUSB(void) +{ + return mobilinkd::tnc::kiss::settings().options & KISS_OPTION_VIN_POWER_ON; +} + +int powerOffViaUSB(void) +{ + return mobilinkd::tnc::kiss::settings().options & KISS_OPTION_VIN_POWER_OFF; +} + namespace mobilinkd { namespace tnc { namespace kiss { -const char FIRMWARE_VERSION[] = "0.8.9"; +const char FIRMWARE_VERSION[] = "0.8.10"; const char HARDWARE_VERSION[] = "Mobilinkd TNC3 2.1.1"; Hardware& settings() @@ -373,8 +383,8 @@ void Hardware::handle_request(hdlc::IoFrame* frame) { case hardware::SET_PTT_CHANNEL: DEBUG("SET_PTT_CHANNEL"); - options &= ~KISS_OPTION_PTT_SIMPLEX; if (*it) { + options &= ~KISS_OPTION_PTT_SIMPLEX; osMessagePut(ioEventQueueHandle, CMD_SET_PTT_MULTIPLEX, osWaitForever); } else { options |= KISS_OPTION_PTT_SIMPLEX; @@ -449,8 +459,8 @@ void Hardware::handle_request(hdlc::IoFrame* frame) { sizeof(HARDWARE_VERSION) - 1); reply(hardware::GET_SERIAL_NUMBER, (uint8_t*) serial_number_64, sizeof(serial_number_64) - 1); - reply8(hardware::GET_USB_POWER_OFF, options & KISS_OPTION_VIN_POWER_OFF ? 0 : 1); - reply8(hardware::GET_USB_POWER_ON, options & KISS_OPTION_VIN_POWER_ON ? 0 : 1); + reply8(hardware::GET_USB_POWER_OFF, options & KISS_OPTION_VIN_POWER_OFF ? 1 : 0); + reply8(hardware::GET_USB_POWER_ON, options & KISS_OPTION_VIN_POWER_ON ? 1 : 0); reply16(hardware::GET_OUTPUT_GAIN, output_gain); reply8(hardware::GET_OUTPUT_TWIST, tx_twist); reply16(hardware::GET_INPUT_GAIN, input_gain); diff --git a/TNC/KissHardware.h b/TNC/KissHardware.h new file mode 100644 index 0000000..01c0477 --- /dev/null +++ b/TNC/KissHardware.h @@ -0,0 +1,18 @@ +// Copyright 2018 Rob Riggs +// All rights reserved. + +#ifndef MOBILINKD__TNC__KISS_HARDWARE_H_ +#define MOBILINKD__TNC__KISS_HARDWARE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +int powerOnViaUSB(void); +int powerOffViaUSB(void); + +#ifdef __cplusplus +} +#endif + +#endif // MOBILINKD__TNC__KISS_HARDWARE_H_ diff --git a/TNC/KissHardware.hpp b/TNC/KissHardware.hpp index ab5b481..a32613b 100644 --- a/TNC/KissHardware.hpp +++ b/TNC/KissHardware.hpp @@ -4,6 +4,7 @@ #ifndef MOBILINKD__TNC__KISS_HARDWARE_HPP_ #define MOBILINKD__TNC__KISS_HARDWARE_HPP_ +#include "KissHardware.h" #include "Log.h" #include "HdlcFrame.hpp" @@ -163,8 +164,8 @@ constexpr const uint8_t MODEM_TYPE_MFSK16 = 6; // Boolean options. #define KISS_OPTION_CONN_TRACK 0x01 #define KISS_OPTION_VERBOSE 0x02 -#define KISS_OPTION_VIN_POWER_ON 0x04 // Power on when plugged in to USB -#define KISS_OPTION_VIN_POWER_OFF 0x08 // Power off when plugged in to USB +#define KISS_OPTION_VIN_POWER_ON 0x04 // Power on when plugged into USB +#define KISS_OPTION_VIN_POWER_OFF 0x08 // Power off when unplugged from USB #define KISS_OPTION_PTT_SIMPLEX 0x10 // Simplex PTT (the default) const char TOCALL[] = "APML30"; // Update for every feature change.