From 9e699d07e4c31d597bffacb2c4f0b7902ddf62af Mon Sep 17 00:00:00 2001 From: Rob Riggs Date: Sat, 19 Jan 2019 17:41:56 -0600 Subject: [PATCH] Set the MAC address on startup. Support 'reset to defaults' by holding down power button while pressing reset. --- Inc/main.h | 1 + Src/main.c | 13 ++++++++++++- TNC/IOEventTask.cpp | 5 +---- TNC/KissHardware.cpp | 2 +- TNC/bm78.cpp | 9 +++++++++ TNC/bm78.h | 2 ++ 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Inc/main.h b/Inc/main.h index 3f5dcc4..7ef18bd 100644 --- a/Inc/main.h +++ b/Inc/main.h @@ -162,6 +162,7 @@ extern char error_message[80]; extern int go_back_to_sleep; extern int usb_wake_state; extern int charging_enabled; +extern int reset_button; #define CxxErrorHandler() _Error_Handler(const_cast(__FILE__), __LINE__) diff --git a/Src/main.c b/Src/main.c index 8553624..56a97e2 100644 --- a/Src/main.c +++ b/Src/main.c @@ -156,6 +156,7 @@ int go_back_to_sleep __attribute__((section(".bss3"))); int stop_now __attribute__((section(".bss3"))); int charging_enabled __attribute__((section(".bss3"))); int usb_wake_state __attribute__((section(".bss3"))); +int reset_button = 0; /* USER CODE END PV */ @@ -397,6 +398,11 @@ int main(void) stop_now = 0; usb_wake_state = 0; } + if (RCC->CSR & (RCC_CSR_PINRSTF|RCC_CSR_BORRSTF)) { + reset_button = 1; + } else { + reset_button = 0; + } /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ @@ -443,6 +449,9 @@ int main(void) SCB->SHCSR |= 0x70000; // Enable fault handlers; if (!go_back_to_sleep) { indicate_turning_on(); // LEDs on during boot. + if (HAL_GPIO_ReadPin(SW_POWER_GPIO_Port, SW_POWER_Pin) && reset_button) { + reset_requested = 1; + } } encode_serial_number(); @@ -595,10 +604,12 @@ int main(void) if (!go_back_to_sleep) { // Initialize the BM78 Bluetooth module and the RTC date/time the first time we boot. - if (!bm78_initialized()) { + if (!bm78_initialized() || reset_requested) { bm78_initialize(); memset(error_message, 0, sizeof(error_message)); // init_rtc_date_time(); + } else if (reset_button) { + bm78_initialize_mac_address(); } else bm78_wait_until_ready(); } diff --git a/TNC/IOEventTask.cpp b/TNC/IOEventTask.cpp index 4367529..7fdeeb2 100644 --- a/TNC/IOEventTask.cpp +++ b/TNC/IOEventTask.cpp @@ -81,10 +81,6 @@ void startIOEventTask(void const*) HAL_NVIC_SetPriority(EXTI9_5_IRQn, 6, 0); HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); - if (reset_requested) - { - } - // FIXME: this is probably not right if (HAL_GPIO_ReadPin(BT_STATE2_GPIO_Port, BT_STATE2_Pin) == GPIO_PIN_RESET) { @@ -181,6 +177,7 @@ void startIOEventTask(void const*) break; case CMD_POWER_BUTTON_UP: DEBUG("Power Up"); + if (power_button_counter == 0) break; // reset_requested power_button_duration = osKernelSysTick() - power_button_counter; DEBUG("Button pressed for %lums", power_button_duration); shutdown(0); // ***NO RETURN*** diff --git a/TNC/KissHardware.cpp b/TNC/KissHardware.cpp index 5588979..6e473a9 100644 --- a/TNC/KissHardware.cpp +++ b/TNC/KissHardware.cpp @@ -26,7 +26,7 @@ int powerOffViaUSB(void) namespace mobilinkd { namespace tnc { namespace kiss { -const char FIRMWARE_VERSION[] = "1.0.1"; +const char FIRMWARE_VERSION[] = "1.0.2"; const char HARDWARE_VERSION[] = "Mobilinkd TNC3 2.1.1"; Hardware& settings() diff --git a/TNC/bm78.cpp b/TNC/bm78.cpp index 856ed9e..e6efa3c 100644 --- a/TNC/bm78.cpp +++ b/TNC/bm78.cpp @@ -518,6 +518,15 @@ int bm78_initialized() return HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) == crc; } +void bm78_initialize_mac_address() +{ + using namespace mobilinkd::tnc::bm78; + + enter_program_mode(); + read_mac_address(); + exit_program_mode(); +} + int bm78_initialize() { using namespace mobilinkd::tnc; diff --git a/TNC/bm78.h b/TNC/bm78.h index c55a43c..22506aa 100644 --- a/TNC/bm78.h +++ b/TNC/bm78.h @@ -32,6 +32,8 @@ int bm78_disable(void); int bm78_enable(void); int bm78_initialized(void); int bm78_initialize(void); +void bm78_initialize_mac_address(void); + HAL_StatusTypeDef bm78_send(const char* data, uint16_t size, uint32_t timeout); #ifdef __cplusplus