diff --git a/applications/accessor/accessor-app.cpp b/applications/accessor/accessor-app.cpp index 5ec1a939..a6f8e59f 100644 --- a/applications/accessor/accessor-app.cpp +++ b/applications/accessor/accessor-app.cpp @@ -28,6 +28,9 @@ void AccessorApp::run(void) { }; scenes[current_scene]->on_exit(this); + + wiegand.end(); + onewire_master.stop(); } AccessorApp::AccessorApp() diff --git a/applications/accessor/helpers/wiegand.cpp b/applications/accessor/helpers/wiegand.cpp index c6ca9fe0..bc618489 100644 --- a/applications/accessor/helpers/wiegand.cpp +++ b/applications/accessor/helpers/wiegand.cpp @@ -35,18 +35,14 @@ bool WIEGAND::available() { return ret; } -void input_isr(void* _pin, void* _ctx) { - // interrupt manager get us pin constant, so... - uint32_t pin = (uint32_t)_pin; +static void input_isr_d0(void* _ctx) { WIEGAND* _this = static_cast(_ctx); + _this->ReadD0(); +} - if(pin == gpio_ext_pa6.pin) { - _this->ReadD0(); - } - - if(pin == gpio_ext_pa7.pin) { - _this->ReadD1(); - } +static void input_isr_d1(void* _ctx) { + WIEGAND* _this = static_cast(_ctx); + _this->ReadD1(); } void WIEGAND::begin() { @@ -60,11 +56,19 @@ void WIEGAND::begin() { const GpioPin* pinD0 = &gpio_ext_pa6; const GpioPin* pinD1 = &gpio_ext_pa7; - hal_gpio_init(pinD0, GpioModeInterruptFall, GpioPullNo, GpioSpeedLow); // Set D0 pin as input - hal_gpio_init(pinD1, GpioModeInterruptFall, GpioPullNo, GpioSpeedLow); // Set D1 pin as input + hal_gpio_init_simple(pinD0, GpioModeInterruptFall); // Set D0 pin as input + hal_gpio_init_simple(pinD1, GpioModeInterruptFall); // Set D1 pin as input - api_interrupt_add( - input_isr, InterruptTypeExternalInterrupt, this); // Hardware interrupt - high to low pulse + hal_gpio_add_int_callback(pinD0, input_isr_d0, this); + hal_gpio_add_int_callback(pinD1, input_isr_d1, this); +} + +void WIEGAND::end() { + hal_gpio_remove_int_callback(&gpio_ext_pa6); + hal_gpio_remove_int_callback(&gpio_ext_pa7); + + hal_gpio_init_simple(&gpio_ext_pa6, GpioModeAnalog); + hal_gpio_init_simple(&gpio_ext_pa7, GpioModeAnalog); } void WIEGAND::ReadD0() { diff --git a/applications/accessor/helpers/wiegand.h b/applications/accessor/helpers/wiegand.h index 782eec77..fed9fa14 100644 --- a/applications/accessor/helpers/wiegand.h +++ b/applications/accessor/helpers/wiegand.h @@ -4,6 +4,7 @@ class WIEGAND { public: WIEGAND(); void begin(); + void end(); bool available(); unsigned long getCode(); unsigned long getCodeHigh(); diff --git a/core/api-hal/api-interrupt-mgr.h b/core/api-hal/api-interrupt-mgr.h index 46c74f03..fb6ffdab 100644 --- a/core/api-hal/api-interrupt-mgr.h +++ b/core/api-hal/api-interrupt-mgr.h @@ -14,7 +14,6 @@ typedef enum { InterruptTypeComparatorTrigger, InterruptTypeTimerOutputCompare, InterruptTypeTimerUpdate, - InterruptTypeExternalInterrupt, } InterruptType; /** Interrupt callback type */ diff --git a/firmware/targets/f5/Inc/main.h b/firmware/targets/f5/Inc/main.h index cfd29113..24844d1c 100644 --- a/firmware/targets/f5/Inc/main.h +++ b/firmware/targets/f5/Inc/main.h @@ -86,6 +86,11 @@ void Error_Handler(void); #define iBTN_GPIO_Port GPIOB #define iBTN_Pin GPIO_PIN_14 +#define USART1_TX_Pin GPIO_PIN_6 +#define USART1_TX_Port GPIOB +#define USART1_RX_Pin GPIO_PIN_7 +#define USART1_RX_Port GPIOB + #define SPI_D_MISO_GPIO_Port GPIOC #define SPI_D_MISO_Pin GPIO_PIN_2 #define SPI_D_MOSI_GPIO_Port GPIOB diff --git a/firmware/targets/f5/Src/gpio.c b/firmware/targets/f5/Src/gpio.c index 2b09ca83..4106d383 100644 --- a/firmware/targets/f5/Src/gpio.c +++ b/firmware/targets/f5/Src/gpio.c @@ -4,12 +4,12 @@ void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOH_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOE_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); + __HAL_RCC_GPIOE_CLK_ENABLE(); + __HAL_RCC_GPIOH_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, RF_SW_0_Pin|RF_SW_1_Pin, GPIO_PIN_RESET); @@ -135,13 +135,22 @@ void MX_GPIO_Init(void) { GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; HAL_GPIO_Init(CC1101_CS_GPIO_Port, &GPIO_InitStruct); - /* EXTI interrupt init*/ + /* Enable all NVIC lines related to gpio */ + HAL_NVIC_SetPriority(EXTI0_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(EXTI0_IRQn); + HAL_NVIC_SetPriority(EXTI1_IRQn, 5, 0); HAL_NVIC_EnableIRQ(EXTI1_IRQn); + HAL_NVIC_SetPriority(EXTI2_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(EXTI2_IRQn); + HAL_NVIC_SetPriority(EXTI3_IRQn, 5, 0); HAL_NVIC_EnableIRQ(EXTI3_IRQn); + HAL_NVIC_SetPriority(EXTI4_IRQn, 5, 0); + HAL_NVIC_EnableIRQ(EXTI4_IRQn); + HAL_NVIC_SetPriority(EXTI9_5_IRQn, 5, 0); HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); diff --git a/firmware/targets/f5/Src/usart.c b/firmware/targets/f5/Src/usart.c index 3acecbb3..ccf0ab17 100644 --- a/firmware/targets/f5/Src/usart.c +++ b/firmware/targets/f5/Src/usart.c @@ -1,121 +1,55 @@ -/** - ****************************************************************************** - * @file usart.c - * @brief This file provides code for the configuration - * of the USART instances. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2021 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ #include "usart.h" -/* USER CODE BEGIN 0 */ - -/* USER CODE END 0 */ - UART_HandleTypeDef huart1; -/* USART1 init function */ +void MX_USART1_UART_Init(void) { + huart1.Instance = USART1; + huart1.Init.BaudRate = 115200; + huart1.Init.WordLength = UART_WORDLENGTH_8B; + huart1.Init.StopBits = UART_STOPBITS_1; + huart1.Init.Parity = UART_PARITY_NONE; + huart1.Init.Mode = UART_MODE_TX_RX; + huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart1.Init.OverSampling = UART_OVERSAMPLING_16; + huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; + huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; + huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; -void MX_USART1_UART_Init(void) -{ + if (HAL_UART_Init(&huart1) != HAL_OK) { + Error_Handler(); + } - huart1.Instance = USART1; - huart1.Init.BaudRate = 115200; - huart1.Init.WordLength = UART_WORDLENGTH_8B; - huart1.Init.StopBits = UART_STOPBITS_1; - huart1.Init.Parity = UART_PARITY_NONE; - huart1.Init.Mode = UART_MODE_TX_RX; - huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; - huart1.Init.OverSampling = UART_OVERSAMPLING_16; - huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; - huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; - huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; - if (HAL_UART_Init(&huart1) != HAL_OK) - { - Error_Handler(); - } - if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) - { - Error_Handler(); - } - if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) - { - Error_Handler(); - } - if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) - { - Error_Handler(); - } + if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK) { + Error_Handler(); + } + if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK) { + Error_Handler(); + } + + if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) { + Error_Handler(); + } } -void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) -{ +void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) { + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(uartHandle->Instance==USART1) { - GPIO_InitTypeDef GPIO_InitStruct = {0}; - if(uartHandle->Instance==USART1) - { - /* USER CODE BEGIN USART1_MspInit 0 */ - - /* USER CODE END USART1_MspInit 0 */ - /* USART1 clock enable */ - __HAL_RCC_USART1_CLK_ENABLE(); - - __HAL_RCC_GPIOB_CLK_ENABLE(); - /**USART1 GPIO Configuration - PB6 ------> USART1_TX - PB7 ------> USART1_RX - */ - GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - GPIO_InitStruct.Alternate = GPIO_AF7_USART1; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - /* USER CODE BEGIN USART1_MspInit 1 */ - - /* USER CODE END USART1_MspInit 1 */ - } + __HAL_RCC_USART1_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + GPIO_InitStruct.Pin = USART1_TX_Pin|USART1_RX_Pin; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF7_USART1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + } } -void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) -{ - - if(uartHandle->Instance==USART1) - { - /* USER CODE BEGIN USART1_MspDeInit 0 */ - - /* USER CODE END USART1_MspDeInit 0 */ - /* Peripheral clock disable */ - __HAL_RCC_USART1_CLK_DISABLE(); - - /**USART1 GPIO Configuration - PB6 ------> USART1_TX - PB7 ------> USART1_RX - */ - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7); - - /* USER CODE BEGIN USART1_MspDeInit 1 */ - - /* USER CODE END USART1_MspDeInit 1 */ - } +void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) { + if(uartHandle->Instance==USART1) { + __HAL_RCC_USART1_CLK_DISABLE(); + HAL_GPIO_DeInit(GPIOB, USART1_TX_Pin|USART1_RX_Pin); + } } - -/* USER CODE BEGIN 1 */ - -/* USER CODE END 1 */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/firmware/targets/f5/api-hal/api-interrupts.c b/firmware/targets/f5/api-hal/api-interrupts.c index 5303224e..e92df4b9 100644 --- a/firmware/targets/f5/api-hal/api-interrupts.c +++ b/firmware/targets/f5/api-hal/api-interrupts.c @@ -18,9 +18,4 @@ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) { /* Timer update event */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { api_interrupt_call(InterruptTypeTimerUpdate, htim); -} - -/* External interrupt event */ -void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { - api_interrupt_call(InterruptTypeExternalInterrupt, (void*)(uint32_t)GPIO_Pin); -} +} \ No newline at end of file diff --git a/firmware/targets/f6/Src/gpio.c b/firmware/targets/f6/Src/gpio.c index c226542d..c5c26d12 100644 --- a/firmware/targets/f6/Src/gpio.c +++ b/firmware/targets/f6/Src/gpio.c @@ -152,7 +152,7 @@ void MX_GPIO_Init(void) { GPIO_InitStruct.Pin = CC1101_CS_Pin; HAL_GPIO_Init(CC1101_CS_GPIO_Port, &GPIO_InitStruct); - /* EXTI interrupt init*/ + /* Enable all NVIC lines related to gpio */ HAL_NVIC_SetPriority(EXTI0_IRQn, 5, 0); HAL_NVIC_EnableIRQ(EXTI0_IRQn); diff --git a/firmware/targets/f6/api-hal/api-hal-gpio.c b/firmware/targets/f6/api-hal/api-hal-gpio.c index bceba009..03d79d4c 100644 --- a/firmware/targets/f6/api-hal/api-hal-gpio.c +++ b/firmware/targets/f6/api-hal/api-hal-gpio.c @@ -41,11 +41,28 @@ static uint8_t hal_gpio_get_pin_num(const GpioPin* gpio) { return pin_num; } +void hal_gpio_init_simple(const GpioPin* gpio, const GpioMode mode) { + hal_gpio_init(gpio, mode, GpioPullNo, GpioSpeedLow); +} + void hal_gpio_init( const GpioPin* gpio, const GpioMode mode, const GpioPull pull, const GpioSpeed speed) { + // we cannot set alternate mode in this function + furi_assert(mode != GpioModeAltFunctionPushPull); + furi_assert(mode != GpioModeAltFunctionOpenDrain); + + hal_gpio_init_ex(gpio, mode, GpioPullNo, GpioSpeedLow, GpioAltFnUnused); +} + +void hal_gpio_init_ex( + const GpioPin* gpio, + const GpioMode mode, + const GpioPull pull, + const GpioSpeed speed, + const GpioAltFn alt_fn) { uint32_t sys_exti_port = GET_SYSCFG_EXTI_PORT(gpio->port); uint32_t sys_exti_line = GET_SYSCFG_EXTI_LINE(gpio->pin); uint32_t exti_line = GET_EXTI_LINE(gpio->pin); @@ -112,27 +129,19 @@ void hal_gpio_init( LL_GPIO_SetPinMode(gpio->port, gpio->pin, LL_GPIO_MODE_ANALOG); } } - __enable_irq(); -} -void hal_gpio_init_alt( - const GpioPin* gpio, - const GpioMode mode, - const GpioPull pull, - const GpioSpeed speed, - const GpioAltFn alt_fn) { - hal_gpio_init(gpio, mode, pull, speed); + if(mode == GpioModeAltFunctionPushPull || mode == GpioModeAltFunctionOpenDrain) { + // enable alternate mode + LL_GPIO_SetPinMode(gpio->port, gpio->pin, LL_GPIO_MODE_ALTERNATE); - __disable_irq(); - // enable alternate mode - LL_GPIO_SetPinMode(gpio->port, gpio->pin, LL_GPIO_MODE_ALTERNATE); - - // set alternate function - if(hal_gpio_get_pin_num(gpio) < 8) { - LL_GPIO_SetAFPin_0_7(gpio->port, gpio->pin, alt_fn); - } else { - LL_GPIO_SetAFPin_8_15(gpio->port, gpio->pin, alt_fn); + // set alternate function + if(hal_gpio_get_pin_num(gpio) < 8) { + LL_GPIO_SetAFPin_0_7(gpio->port, gpio->pin, alt_fn); + } else { + LL_GPIO_SetAFPin_8_15(gpio->port, gpio->pin, alt_fn); + } } + __enable_irq(); } diff --git a/firmware/targets/f6/api-hal/api-hal-gpio.h b/firmware/targets/f6/api-hal/api-hal-gpio.h index 7bed0572..a81afb39 100644 --- a/firmware/targets/f6/api-hal/api-hal-gpio.h +++ b/firmware/targets/f6/api-hal/api-hal-gpio.h @@ -153,6 +153,8 @@ typedef enum { GpioAltFn14LPTIM2 = 14, /*!< LPTIM2 Alternate Function mapping */ GpioAltFn15EVENTOUT = 15, /*!< EVENTOUT Alternate Function mapping */ + + GpioAltFnUnused = 16, /*!< just dummy value */ } GpioAltFn; /** @@ -164,7 +166,14 @@ typedef struct { } GpioPin; /** - * GPIO initialization function + * GPIO initialization function, simple version + * @param gpio GpioPin + * @param mode GpioMode + */ +void hal_gpio_init_simple(const GpioPin* gpio, const GpioMode mode); + +/** + * GPIO initialization function, normal version * @param gpio GpioPin * @param mode GpioMode * @param pull GpioPull @@ -177,14 +186,14 @@ void hal_gpio_init( const GpioSpeed speed); /** - * GPIO initialization with alternative function + * GPIO initialization function, extended version * @param gpio GpioPin * @param mode GpioMode * @param pull GpioPull * @param speed GpioSpeed * @param alt_fn GpioAltFn */ -void hal_gpio_init_alt( +void hal_gpio_init_ex( const GpioPin* gpio, const GpioMode mode, const GpioPull pull, diff --git a/firmware/targets/f6/api-hal/api-hal-rfid.c b/firmware/targets/f6/api-hal/api-hal-rfid.c index 0fdec2d9..7146b927 100644 --- a/firmware/targets/f6/api-hal/api-hal-rfid.c +++ b/firmware/targets/f6/api-hal/api-hal-rfid.c @@ -26,7 +26,7 @@ void api_hal_rfid_pins_emulate() { api_hal_ibutton_pin_low(); // pull pin to timer out - hal_gpio_init_alt( + hal_gpio_init_ex( &gpio_rfid_pull, GpioModeOutputPushPull, GpioSpeedLow, GpioPullNo, GpioAltFn1TIM1); // pull rfid antenna from carrier side @@ -44,7 +44,7 @@ void api_hal_rfid_pins_read() { hal_gpio_write(&gpio_rfid_pull, false); // carrier pin to timer out - hal_gpio_init_alt( + hal_gpio_init_ex( &gpio_rfid_carrier_out, GpioModeOutputPushPull, GpioSpeedLow, GpioPullNo, GpioAltFn1TIM1); // comparator in diff --git a/firmware/targets/f6/api-hal/api-hal-vcp.c b/firmware/targets/f6/api-hal/api-hal-vcp.c index 4c797306..6c2d71df 100644 --- a/firmware/targets/f6/api-hal/api-hal-vcp.c +++ b/firmware/targets/f6/api-hal/api-hal-vcp.c @@ -46,11 +46,15 @@ void _api_hal_vcp_control_line(uint8_t state) { bool rts = state & 0b10; if (rts) { - api_hal_vcp->alive = true; - _api_hal_vcp_rx_callback(&ascii_soh, 1); // SOH + if (!api_hal_vcp->alive) { + api_hal_vcp->alive = true; + _api_hal_vcp_rx_callback(&ascii_soh, 1); // SOH + } } else { - api_hal_vcp->alive = false; - _api_hal_vcp_rx_callback(&ascii_eot, 1); // EOT + if (api_hal_vcp->alive) { + _api_hal_vcp_rx_callback(&ascii_eot, 1); // EOT + api_hal_vcp->alive = false; + } } osSemaphoreRelease(api_hal_vcp->tx_semaphore); diff --git a/firmware/targets/f6/api-hal/api-interrupts.c b/firmware/targets/f6/api-hal/api-interrupts.c index 5303224e..e92df4b9 100644 --- a/firmware/targets/f6/api-hal/api-interrupts.c +++ b/firmware/targets/f6/api-hal/api-interrupts.c @@ -18,9 +18,4 @@ void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim) { /* Timer update event */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { api_interrupt_call(InterruptTypeTimerUpdate, htim); -} - -/* External interrupt event */ -void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { - api_interrupt_call(InterruptTypeExternalInterrupt, (void*)(uint32_t)GPIO_Pin); -} +} \ No newline at end of file diff --git a/firmware/targets/f6/ble-glue/app_common.h b/firmware/targets/f6/ble-glue/app_common.h index cf8a5baa..9248c529 100644 --- a/firmware/targets/f6/ble-glue/app_common.h +++ b/firmware/targets/f6/ble-glue/app_common.h @@ -66,13 +66,6 @@ extern "C"{ * Some useful macro definitions * * -------------------------------- */ -#ifndef MAX -#define MAX( x, y ) (((x)>(y))?(x):(y)) -#endif - -#ifndef MIN -#define MIN( x, y ) (((x)<(y))?(x):(y)) -#endif #define MODINC( a, m ) M_BEGIN (a)++; if ((a)>=(m)) (a)=0; M_END